Skip to content

Commit

Permalink
feat: added ConfigManagerUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
MSchmoecker committed Aug 2, 2024
1 parent 2b5c9c9 commit 69a4d03
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Version 2.21.0
* Added ConfigManagerUtils for soft access to common ConfigurationManager functionality
* Added PieceManager.AddPieceCategory(string name) and PieceManager.RemovePieceCategory(string name)
* Deprecated PieceManager.AddPieceCategory(string table, string name) and PieceManager.RemovePieceCategory(string table, string name), use the new overloads without the table parameter

Expand Down
16 changes: 4 additions & 12 deletions JotunnLib/Managers/SynchronizationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class SynchronizationManager : IManager
private readonly Dictionary<string, ConfigFile> CustomConfigs = new Dictionary<string, ConfigFile>();
private List<Tuple<string, string, string, string>> CachedConfigValues = new List<Tuple<string, string, string, string>>();
private readonly Dictionary<string, string> CachedCustomConfigGUIDs = new Dictionary<string, string>();
private BaseUnityPlugin ConfigurationManager;
private bool ConfigurationManagerWindowShown;

/// <summary>
Expand Down Expand Up @@ -89,20 +88,15 @@ void IManager.Init()
// Hook start scene to reset config
SceneManager.sceneLoaded += SceneManager_sceneLoaded;

// Find Configuration manager plugin and add to DisplayingWindowChanged event
const string configManagerGuid = "com.bepis.bepinex.configurationmanager";
if (Chainloader.PluginInfos.TryGetValue(configManagerGuid, out var configManagerInfo) && configManagerInfo.Instance)
if (ConfigManagerUtils.Plugin)
{
ConfigurationManager = configManagerInfo.Instance;

Logger.LogDebug("Configuration manager found, trying to hook DisplayingWindowChanged");
var eventinfo = ConfigurationManager.GetType().GetEvent("DisplayingWindowChanged");
var eventinfo = ConfigManagerUtils.Plugin.GetType().GetEvent("DisplayingWindowChanged");
if (eventinfo != null)
{
Action<object, object> local = ConfigurationManager_DisplayingWindowChanged;
var converted = Delegate.CreateDelegate(eventinfo.EventHandlerType, local.Target, local.Method);

eventinfo.AddEventHandler(ConfigurationManager, converted);
eventinfo.AddEventHandler(ConfigManagerUtils.Plugin, converted);
}
}

Expand Down Expand Up @@ -573,9 +567,7 @@ private void Menu_IsVisible(ref bool result)
/// <param name="e"></param>
private void ConfigurationManager_DisplayingWindowChanged(object sender, object e)
{
// Read configuration manager's DisplayingWindow property
var pi = ConfigurationManager.GetType().GetProperty("DisplayingWindow");
ConfigurationManagerWindowShown = (bool)pi.GetValue(ConfigurationManager, null);
ConfigurationManagerWindowShown = ConfigManagerUtils.DisplayingWindow;

if (!ConfigurationManagerWindowShown)
{
Expand Down
54 changes: 54 additions & 0 deletions JotunnLib/Utils/ConfigManagerUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Reflection;
using BepInEx;
using BepInEx.Bootstrap;
using HarmonyLib;

namespace Jotunn.Utils
{
/// <summary>
/// Utility class for the BepInEx ConfigurationManager plugin, without requiring a hard dependency
/// </summary>
public static class ConfigManagerUtils
{
/// <summary>
/// The ConfigurationManager plugin instance if installed, otherwise null
/// </summary>
public static BaseUnityPlugin Plugin { get; private set; }

private static PropertyInfo displayingWindowInfo;
private static MethodInfo buildSettingListMethodInfo;

static ConfigManagerUtils()
{
if (Chainloader.PluginInfos.TryGetValue("com.bepis.bepinex.configurationmanager", out var configManagerInfo) && configManagerInfo.Instance)
{
Plugin = configManagerInfo.Instance;
displayingWindowInfo = AccessTools.Property(Plugin.GetType(), "DisplayingWindow");
buildSettingListMethodInfo = AccessTools.Method(Plugin.GetType(), "BuildSettingList");
}
}

/// <summary>
/// Is the config manager main window displayed on screen<br />
/// Safe to use even if ConfigurationManager is not installed.
/// </summary>
public static bool DisplayingWindow
{
get => Plugin && (bool)displayingWindowInfo.GetValue(Plugin);
set => displayingWindowInfo?.SetValue(Plugin, value);
}

/// <summary>
/// Rebuild the setting list. Use to update the config manager window if config settings were removed or added while it was open.<br />
/// Safe to call even if ConfigurationManager is not installed.
/// </summary>
public static void BuildSettingList()
{
if (Plugin)
{
buildSettingListMethodInfo.Invoke(Plugin, null);
}
}
}
}

0 comments on commit 69a4d03

Please sign in to comment.