Move reloading to DebugReload.cs

This commit is contained in:
Ben C 2022-03-12 14:21:28 -05:00
parent 9dcbf24a4e
commit 73f4dbbd5d
2 changed files with 67 additions and 50 deletions

View File

@ -37,7 +37,6 @@ namespace NewHorizons
public static Main Instance { get; private set; }
public static bool Debug;
private static IModButton _reloadButton;
public static Dictionary<string, NewHorizonsSystem> SystemDict = new Dictionary<string, NewHorizonsSystem>();
public static Dictionary<string, List<NewHorizonsBody>> BodyDict = new Dictionary<string, List<NewHorizonsBody>>();
@ -75,7 +74,7 @@ namespace NewHorizons
public override void Configure(IModConfig config)
{
Debug = config.GetSettingsValue<bool>("Debug");
UpdateReloadButton();
DebugReload.UpdateReloadButton();
string logLevel = config.GetSettingsValue<string>("LogLevel");
Logger.LogType logType;
switch (logLevel)
@ -129,56 +128,10 @@ namespace NewHorizons
Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => OnSceneLoaded(SceneManager.GetActiveScene(), LoadSceneMode.Single));
Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => _firstLoad = false);
Instance.ModHelper.Menus.PauseMenu.OnInit += InitializePauseMenu;
Instance.ModHelper.Menus.PauseMenu.OnInit += DebugReload.InitializePauseMenu;
}
#region Reloading
private void InitializePauseMenu()
{
_reloadButton = ModHelper.Menus.PauseMenu.OptionsButton.Duplicate(TranslationHandler.GetTranslation("Reload Configs", TranslationHandler.TextType.UI).ToUpper());
_reloadButton.OnClick += ReloadConfigs;
UpdateReloadButton();
}
private void UpdateReloadButton()
{
if (_reloadButton != null)
{
if (Debug) _reloadButton.Show();
else _reloadButton.Hide();
}
}
private void ReloadConfigs()
{
BodyDict = new Dictionary<string, List<NewHorizonsBody>>();
SystemDict = new Dictionary<string, NewHorizonsSystem>();
BodyDict["SolarSystem"] = new List<NewHorizonsBody>();
SystemDict["SolarSystem"] = new NewHorizonsSystem("SolarSystem", new StarSystemConfig(null), this);
foreach (AssetBundle bundle in AssetBundles.Values)
{
bundle.Unload(true);
}
AssetBundles.Clear();
Logger.Log("Begin reload of config files...", Logger.LogType.Log);
try
{
foreach (IModBehaviour mountedAddon in MountedAddons)
{
LoadConfigs(mountedAddon);
}
}
catch (Exception)
{
Logger.LogWarning("Error While Reloading");
}
ChangeCurrentStarSystem(_currentStarSystem);
}
#endregion
public void OnDestroy()
{

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using NewHorizons.External.Configs;
using NewHorizons.Handlers;
using OWML.Common;
using OWML.Common.Menus;
using UnityEngine;
namespace NewHorizons.Utility
{
public static class DebugReload
{
private static IModButton _reloadButton;
public static void InitializePauseMenu()
{
_reloadButton = Main.Instance.ModHelper.Menus.PauseMenu.OptionsButton.Duplicate(TranslationHandler.GetTranslation("Reload Configs", TranslationHandler.TextType.UI).ToUpper());
_reloadButton.OnClick += ReloadConfigs;
UpdateReloadButton();
}
public static void UpdateReloadButton()
{
if (_reloadButton != null)
{
if (Main.Debug) _reloadButton.Show();
else _reloadButton.Hide();
}
}
private static void ReloadConfigs()
{
Main.BodyDict.Clear();
Main.SystemDict.Clear();
Main.BodyDict["SolarSystem"] = new List<NewHorizonsBody>();
Main.SystemDict["SolarSystem"] = new NewHorizonsSystem("SolarSystem", new StarSystemConfig(null), Main.Instance);
foreach (AssetBundle bundle in Main.AssetBundles.Values)
{
bundle.Unload(true);
}
Main.AssetBundles.Clear();
Logger.Log("Begin reload of config files...", Logger.LogType.Log);
try
{
foreach (IModBehaviour mountedAddon in Main.MountedAddons)
{
Main.Instance.LoadConfigs(mountedAddon);
}
}
catch (Exception)
{
Logger.LogWarning("Error While Reloading");
}
GameObject.Find("/PauseMenu/PauseMenuManagers").GetComponent<PauseMenuManager>().OnSkipToNextTimeLoop();
Main.Instance.ChangeCurrentStarSystem(Main.Instance.CurrentStarSystem);
}
}
}