Skip to content

Commit

Permalink
Fix patch only mods not being affected by enable/disable
Browse files Browse the repository at this point in the history
  • Loading branch information
xen-42 committed Oct 10, 2023
1 parent 53aad93 commit a6735c4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 41 deletions.
88 changes: 49 additions & 39 deletions Winch/Core/ModAssemblyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,39 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Winch.Util;

namespace Winch.Core
{
class ModAssemblyLoader
{
public static Dictionary<string, ModAssembly> RegisteredAssemblies = new();
public static Dictionary<string, ModAssembly> EnabledModAssemblies = new();
private static Dictionary<string, ModAssembly> _installedAssemblies = new();
public static Dictionary<string, bool> EnabledMods = new();
public static List<string> LoadedMods = new();
public static List<string> ErrorMods = new();

internal static void LoadModAssemblies()
{

if (!Directory.Exists("Mods"))
Directory.CreateDirectory("Mods");
{
Directory.CreateDirectory("Mods");
}

string[] modDirs = Directory.GetDirectories("Mods");
WinchCore.Log.Info($"Loading {modDirs.Length} mod assemblies...");
foreach (string modDir in modDirs)
RegisterModAssembly(modDir);
{
RegisterModAssembly(modDir);
}

try
{
GetEnabledMods();
}
catch (Exception ex)
{
WinchCore.Log.Error($"Error fetching enabled mods: {ex}");
}
}
GetEnabledMods();

EnabledModAssemblies = EnabledMods == null ? _installedAssemblies
: _installedAssemblies.Where(x => EnabledMods[(string)x.Value.Metadata["ModGUID"]])
.ToDictionary(x => x.Key, x => x.Value);
}

private static void RegisterModAssembly(string path)
{
Expand All @@ -42,7 +44,7 @@ private static void RegisterModAssembly(string path)
{
ModAssembly mod = ModAssembly.FromPath(path);
mod.LoadAssembly();
RegisteredAssemblies.Add(modName, mod);
_installedAssemblies.Add(modName, mod);
}
catch(Exception ex)
{
Expand All @@ -53,7 +55,7 @@ private static void RegisterModAssembly(string path)

internal static void ExecuteModAssemblies()
{
foreach (string modName in RegisteredAssemblies.Keys)
foreach (string modName in EnabledModAssemblies.Keys)
ExecuteModAssembly(modName);
}

Expand All @@ -62,7 +64,7 @@ internal static void ExecuteModAssembly(string modName, string? minVersion = nul
if (LoadedMods.Contains(modName) || ErrorMods.Contains(modName))
return;

if (!RegisteredAssemblies.ContainsKey(modName))
if (!EnabledModAssemblies.ContainsKey(modName))
{
ErrorMods.Add(modName);
WinchCore.Log.Error($"Mod not loaded: {modName}");
Expand All @@ -71,11 +73,11 @@ internal static void ExecuteModAssembly(string modName, string? minVersion = nul

if(minVersion != null)
{
if (!VersionUtil.IsSameOrNewer(RegisteredAssemblies[modName].Metadata["Version"].ToString(), minVersion))
if (!VersionUtil.IsSameOrNewer(EnabledModAssemblies[modName].Metadata["Version"].ToString(), minVersion))
throw new Exception($"Cannot satisfy minimum version constraint {minVersion} for {modName}");
}

var modGUID = (string)RegisteredAssemblies[modName].Metadata["ModGUID"];
var modGUID = (string)EnabledModAssemblies[modName].Metadata["ModGUID"];
if (!EnabledMods[modGUID])
{
WinchCore.Log.Info($"Mod '{modName}' disabled.");
Expand All @@ -84,7 +86,7 @@ internal static void ExecuteModAssembly(string modName, string? minVersion = nul

try
{
RegisteredAssemblies[modName].ExecuteAssembly();
EnabledModAssemblies[modName].ExecuteAssembly();
LoadedMods.Add(modName);
WinchCore.Log.Info($"Successfully initialized {modName}.");
}
Expand All @@ -97,26 +99,34 @@ internal static void ExecuteModAssembly(string modName, string? minVersion = nul

internal static void GetEnabledMods()
{
string modListPath = Path.Combine(Directory.GetCurrentDirectory(), "mod_list.json");

if (File.Exists(modListPath))
{
string modList = File.ReadAllText(modListPath);
EnabledMods = JsonConvert.DeserializeObject<Dictionary<string, bool>>(modList)
?? throw new InvalidOperationException("Unable to parse mod_list.json file.");
}

foreach (string mod in RegisteredAssemblies.Keys)
{
string modGUID = (string) RegisteredAssemblies[mod].Metadata["ModGUID"];
if (!EnabledMods.ContainsKey(modGUID))
{
EnabledMods.Add(modGUID, true);
}
}

string serializedEnabledMods = JsonConvert.SerializeObject(EnabledMods, Formatting.Indented);
File.WriteAllText(modListPath, serializedEnabledMods);
try
{
string modListPath = Path.Combine(Directory.GetCurrentDirectory(), "mod_list.json");

if (File.Exists(modListPath))
{
string modList = File.ReadAllText(modListPath);
EnabledMods = JsonConvert.DeserializeObject<Dictionary<string, bool>>(modList)
?? throw new InvalidOperationException("Unable to parse mod_list.json file.");
}

foreach (string mod in _installedAssemblies.Keys)
{
string modGUID = (string)_installedAssemblies[mod].Metadata["ModGUID"];
if (!EnabledMods.ContainsKey(modGUID))
{
EnabledMods.Add(modGUID, true);
}
}

string serializedEnabledMods = JsonConvert.SerializeObject(EnabledMods, Formatting.Indented);
File.WriteAllText(modListPath, serializedEnabledMods);
}
catch (Exception ex)
{
WinchCore.Log.Error($"Unable to parse mod_list.json file: {ex}");
EnabledMods = null;

Check warning on line 128 in Winch/Core/ModAssemblyLoader.cs

View workflow job for this annotation

GitHub Actions / Build debug / Create artifacts

Cannot convert null literal to non-nullable reference type.

Check warning on line 128 in Winch/Core/ModAssemblyLoader.cs

View workflow job for this annotation

GitHub Actions / Build / Create artifacts

Cannot convert null literal to non-nullable reference type.
}
}
}
}
2 changes: 1 addition & 1 deletion Winch/Core/WinchCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void Main()
Log.Debug("Created Harmony Instance 'com.dredge.winch'. Patching...");
harmony.PatchAll();

foreach(ModAssembly modAssembly in ModAssemblyLoader.RegisteredAssemblies.Values)
foreach(ModAssembly modAssembly in ModAssemblyLoader.EnabledModAssemblies.Values)
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion Winch/mod_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"Name": "Winch",
"Author": "Hacktix",
"ModGUID": "hacktix.winch",
"Version": "0.2.2"
"Version": "0.2.3"
}

0 comments on commit a6735c4

Please sign in to comment.