diff --git a/MonsterModifiers/MonsterModifiers.csproj b/MonsterModifiers/MonsterModifiers.csproj index bc690da..4d2e330 100644 --- a/MonsterModifiers/MonsterModifiers.csproj +++ b/MonsterModifiers/MonsterModifiers.csproj @@ -2,6 +2,16 @@ + + + all + + + + all + + + Debug AnyCPU @@ -63,9 +73,6 @@ - - $(BepInExPath)\core\0Harmony.dll - $(PublicizedAssembliesPath)\assembly_guiutils_publicized.dll @@ -125,6 +132,7 @@ + diff --git a/MonsterModifiers/Src/Config/ModifierConfigHandler.cs b/MonsterModifiers/Src/Config/ModifierConfigHandler.cs new file mode 100644 index 0000000..38c6e35 --- /dev/null +++ b/MonsterModifiers/Src/Config/ModifierConfigHandler.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using BepInEx.Configuration; +using Jotunn.Extensions; +using UnityEngine; + +namespace MonsterModifiers.Config; + +/// +/// Handler for the values determined by the Bepinex Config for this mod. +/// +public class ModifierConfigHandler +{ + ConfigFile? _configFile; + readonly Dictionary> _configModifierWeights = new(); + + static ModifierConfigHandler? _instance; + + /// + /// Gets the singleton instance of the Handler. + /// + public static ModifierConfigHandler Instance + { + get + { + if (_instance == null) + { + _instance = new ModifierConfigHandler(); + } + return _instance; + } + } + + /// + /// Initializes the config by setting the ConfigFile of the mod, allowing for calling of the config API methods. + /// + /// The Config File the handler will be responsible for. + public void InitConfig(ConfigFile configFile) + { + _configFile = configFile; + InitConfigParams(); + } + + /// + /// Initializes the weighting values of all the Monster Modifiers in the Mod. The intention is that, regardless of + /// how many modifiers are added to the mod, the config will dynamically add the new modifiers to the weighting + /// section. + /// + void InitConfigParams() + { + if (_configFile == null) + { + throw new InvalidOperationException( + "The initialization of config parameters has been called, but the" + + " configuration file has not been initialized in the config handler!" + ); + } + foreach (MonsterModifierTypes modifierType in Enum.GetValues(typeof(MonsterModifierTypes))) + { + ConfigEntry modifierEntry = _configFile.BindConfig( + "Modifier Weights", + modifierType.ToString(), + 1, + "The application weight of the modifier. Value should be any positive integer." + ); + if (modifierEntry.Value <= 0) + { + MonsterModifiersPlugin.MonsterModifiersLogger.LogWarning( + $"Weight for {modifierType.ToString()} modifier was set to less than 0 ({modifierEntry.Value}), weight has been ignored, please change this weight to an integer value that is higher than 0" + ); + continue; + } + _configModifierWeights.Add(modifierType, modifierEntry); + } + } + + /// + /// Takes in a dictionary of modifier type - modifier data key-value pairs and dynamically alters the weighting of + /// the modifier data, if custom config values have been chosen by the user via the mod bepinex config. + /// + /// The default modifiers to attempt altering weighting data for. + /// The modified modifier data dictionary. + public Dictionary InitCustomConfigModifiers( + Dictionary defaultModifiers + ) + { + if (_configFile == null) + { + throw new InvalidOperationException( + "Config modifier weights are being queried by the Modifier Config Handler, but the initialization of the Mod's config file has not succesfully executed!" + ); + } + foreach (var modiferEntry in _configModifierWeights) + { + if (!defaultModifiers.TryGetValue(modiferEntry.Key, out ModifierData defaultData)) + { + continue; + } + defaultData.weight = modiferEntry.Value.Value; + } + + return defaultModifiers; + } +} diff --git a/MonsterModifiers/Src/Plugin.cs b/MonsterModifiers/Src/Plugin.cs index 9090dca..cdaa52e 100644 --- a/MonsterModifiers/Src/Plugin.cs +++ b/MonsterModifiers/Src/Plugin.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; @@ -11,6 +12,7 @@ using Jotunn.Managers; using Jotunn.Utils; using LocalizationManager; +using MonsterModifiers.Config; using MonsterModifiers.Modifiers; using UnityEngine; using Paths = BepInEx.Paths; @@ -59,10 +61,10 @@ public void Awake() if (saveOnSet) { Config.SaveOnConfigSet = saveOnSet; + Config.Save(); } - - YamlUtils.ParseDefaultYamls(); + InitModifiers(); TranslationUtils.AddLocalizations(); ModifierAssetUtils.Setup(); ModifierAssetUtils.LoadAllIcons(); @@ -77,6 +79,15 @@ public void Awake() PrefabManager.OnVanillaPrefabsAvailable += PrefabUtils.CreateCustomPrefabs; } + + void InitModifiers() + { + ModifierConfigHandler.Instance.InitConfig(Config); + Dictionary defaultModifiers= YamlUtils.ParseDefaultYamls(); + ModifierUtils.modifiers = ModifierConfigHandler.Instance.InitCustomConfigModifiers(defaultModifiers); + } + + public static ConfigEntry Configurations_MaxModifiers; diff --git a/MonsterModifiers/Src/Utils/YamlUtils.cs b/MonsterModifiers/Src/Utils/YamlUtils.cs index 5d183d2..fde3fc7 100644 --- a/MonsterModifiers/Src/Utils/YamlUtils.cs +++ b/MonsterModifiers/Src/Utils/YamlUtils.cs @@ -12,7 +12,7 @@ public class YamlUtils { public static string defaultModifierValues; - public static void ParseDefaultYamls() + public static Dictionary ParseDefaultYamls() { defaultModifierValues = AssetUtils.LoadTextFromResources("modifierValues.yml"); @@ -20,6 +20,7 @@ public static void ParseDefaultYamls() .WithNamingConvention(CamelCaseNamingConvention.Instance) .Build(); - ModifierUtils.modifiers = deserializer.Deserialize>(new StringReader(defaultModifierValues)); + return deserializer.Deserialize>(new StringReader(defaultModifierValues)); } + } \ No newline at end of file