diff --git a/src/CustomizeBuildings/Customize_Buildings_ModIntegration.cs b/src/CustomizeBuildings/Customize_Buildings_ModIntegration.cs new file mode 100644 index 0000000..4afaa09 --- /dev/null +++ b/src/CustomizeBuildings/Customize_Buildings_ModIntegration.cs @@ -0,0 +1,78 @@ +using HarmonyLib; +using System; + +namespace CustomizeBuildings +{ + /// + /// This class can be copied by other mods to easily read values from the mod config of CustomizeBuildings + /// + public static class Customize_Buildings_ModIntegration + { + static object ConfigState = null; + public static bool IntegrationActive { get; private set; } = false; + static bool _initializationRun = false; + + /// + /// Fetches a reference to the config state object to later read values from it + /// + public static void Init() + { + if (_initializationRun) + return; + _initializationRun = true; + + ///fetch the config class type + var CustomizeBuildings_CustomizeBuildingsState = Type.GetType("CustomizeBuildings.CustomizeBuildingsState, CustomizeBuildings"); + if (CustomizeBuildings_CustomizeBuildingsState == null) + { + Debug.Log("CustomizeBuildings types not found."); + return; + } + ///fetch the method info of the static getter method + var m_GetModConfigState = AccessTools.Method(CustomizeBuildings_CustomizeBuildingsState.GetType(), "GetModConfigState"); + if (m_GetModConfigState == null) + { + Debug.LogWarning("CustomizeBuildings.CustomizeBuildingsState.GetModConfigState method not found."); + return; + } + ///fetch the mod config state object + ConfigState = m_GetModConfigState.Invoke(null, null); + IntegrationActive = ConfigState != null; + } + + /// + /// wrapper that allows fetching a config state value of type T + /// Can only reliably be called after all mods are loaded, e.g. during a non-transpiler patch or in a building/entity definition method + /// + /// type of the property to fetch + /// name of the property + /// fetched value of the property. default(T) if not found or the types dont match + /// bool if the property was found successfully + public static bool TryGetConfigValue(string propertyName, out T value) + { + value = default(T); + Init(); + if (!IntegrationActive) + return false; + + Traverse property = Traverse.Create(ConfigState).Property(propertyName); + if (!property.PropertyExists()) + { + Debug.LogWarning("Mod Config State did not have a property with the name: " + propertyName); + return false; + + } + object propertyValue = property.GetValue(); + var foundType = propertyValue.GetType(); + var T_Type = typeof(T); + if (foundType != T_Type) + { + Debug.LogWarning("Mod Config State had a property with the name: " + propertyName + ", but it was typeOf " + foundType.Name + ", instead of the expected " + T_Type.Name); + return false; + } + + value = (T)propertyValue; + return true; + } + } +} diff --git a/src/CustomizeBuildings/_CustomizeBuildingsState.cs b/src/CustomizeBuildings/_CustomizeBuildingsState.cs index 6fac1da..5fc2469 100644 --- a/src/CustomizeBuildings/_CustomizeBuildingsState.cs +++ b/src/CustomizeBuildings/_CustomizeBuildingsState.cs @@ -14,6 +14,16 @@ namespace CustomizeBuildings [ModInfo(null, collapse: true)] public class CustomizeBuildingsState : IManualConfig { + /// + /// static method that returns the current config state. + /// Makes it possible for other mods to easily reflect for the config state to read values. + /// + /// + public static object GetModConfigState() + { + return StateManager.State; + } + public int version { get; set; } = 59; #region $Reset Button