Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions MonsterModifiers/MonsterModifiers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="environment.props" />
<ItemGroup>
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.10.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>

<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand Down Expand Up @@ -63,9 +73,6 @@
</When>
</Choose>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>$(BepInExPath)\core\0Harmony.dll</HintPath>
</Reference>
<Reference Include="assembly_guiutils">
<HintPath>$(PublicizedAssembliesPath)\assembly_guiutils_publicized.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -125,6 +132,7 @@
<Compile Include="Src\Custom Components\MonsterModifier.cs" />
<Compile Include="Src\Modifiers\Absorption.cs" />
<Compile Include="Src\Modifiers\BloodLoss.cs" />
<Compile Include="Src\Config\ModifierConfigHandler.cs" />
<Compile Include="Src\Modifiers\DistantDetection.cs" />
<Compile Include="Src\Modifiers\EitrSiphon.cs" />
<Compile Include="Src\Modifiers\FastAttackSpeed.cs" />
Expand Down
104 changes: 104 additions & 0 deletions MonsterModifiers/Src/Config/ModifierConfigHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using System.Collections.Generic;
using BepInEx.Configuration;
using Jotunn.Extensions;
using UnityEngine;

namespace MonsterModifiers.Config;

/// <summary>
/// Handler for the values determined by the Bepinex Config for this mod.
/// </summary>
public class ModifierConfigHandler
{
ConfigFile? _configFile;
readonly Dictionary<MonsterModifierTypes, ConfigEntry<int>> _configModifierWeights = new();

static ModifierConfigHandler? _instance;

/// <summary>
/// Gets the singleton instance of the Handler.
/// </summary>
public static ModifierConfigHandler Instance
{
get
{
if (_instance == null)
{
_instance = new ModifierConfigHandler();
}
return _instance;
}
}

/// <summary>
/// Initializes the config by setting the ConfigFile of the mod, allowing for calling of the config API methods.
/// </summary>
/// <param name="configFile">The Config File the handler will be responsible for.</param>
public void InitConfig(ConfigFile configFile)
{
_configFile = configFile;
InitConfigParams();
}

/// <summary>
/// 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.
/// </summary>
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<int> 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);
}
}

/// <summary>
/// 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.
/// </summary>
/// <param name="defaultModifiers">The default modifiers to attempt altering weighting data for.</param>
/// <returns>The modified modifier data dictionary.</returns>
public Dictionary<MonsterModifierTypes, ModifierData> InitCustomConfigModifiers(
Dictionary<MonsterModifierTypes, ModifierData> 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;
}
}
15 changes: 13 additions & 2 deletions MonsterModifiers/Src/Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
Expand All @@ -11,6 +12,7 @@
using Jotunn.Managers;
using Jotunn.Utils;
using LocalizationManager;
using MonsterModifiers.Config;
using MonsterModifiers.Modifiers;
using UnityEngine;
using Paths = BepInEx.Paths;
Expand Down Expand Up @@ -59,10 +61,10 @@ public void Awake()
if (saveOnSet)
{
Config.SaveOnConfigSet = saveOnSet;

Config.Save();
}

YamlUtils.ParseDefaultYamls();
InitModifiers();
TranslationUtils.AddLocalizations();
ModifierAssetUtils.Setup();
ModifierAssetUtils.LoadAllIcons();
Expand All @@ -77,6 +79,15 @@ public void Awake()

PrefabManager.OnVanillaPrefabsAvailable += PrefabUtils.CreateCustomPrefabs;
}

void InitModifiers()
{
ModifierConfigHandler.Instance.InitConfig(Config);
Dictionary<MonsterModifierTypes, ModifierData> defaultModifiers= YamlUtils.ParseDefaultYamls();
ModifierUtils.modifiers = ModifierConfigHandler.Instance.InitCustomConfigModifiers(defaultModifiers);
}



public static ConfigEntry<int> Configurations_MaxModifiers;

Expand Down
5 changes: 3 additions & 2 deletions MonsterModifiers/Src/Utils/YamlUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ public class YamlUtils
{
public static string defaultModifierValues;

public static void ParseDefaultYamls()
public static Dictionary<MonsterModifierTypes, ModifierData> ParseDefaultYamls()
{
defaultModifierValues = AssetUtils.LoadTextFromResources("modifierValues.yml");

var deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();

ModifierUtils.modifiers = deserializer.Deserialize<Dictionary<MonsterModifierTypes, ModifierData>>(new StringReader(defaultModifierValues));
return deserializer.Deserialize<Dictionary<MonsterModifierTypes, ModifierData>>(new StringReader(defaultModifierValues));
}

}