Skip to content

Commit

Permalink
Move the disabled check to a method instead of a delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
cheese3660 committed Jan 12, 2024
1 parent 59bdd47 commit 430dff3
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/SpaceWarp.Patches/ChainloaderPatch.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Reflection;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
Expand Down Expand Up @@ -40,12 +41,25 @@ private static void PreStartActions()
}
}

private static bool CheckIfModIsDisabled(PluginInfo plugin, HashSet<string> deniedSet)
{
if (Array.IndexOf(ModList.DisabledPluginGuids, plugin.Metadata.GUID) == -1)
{
return true;
}

deniedSet.Add(plugin.Metadata.GUID);
ModList.DisabledPlugins.Add(plugin);
Entrypoint.LogSource.LogInfo($"{plugin.Metadata.GUID} was disabled, skipping loading...");
return false;

}
[HarmonyILManipulator]
[HarmonyPatch(typeof(Chainloader), nameof(Chainloader.Start))]
private static void DisablePluginsIL(ILContext il)
{
ILCursor c = new(il);

ILLabel continueLabel = default;
c.GotoNext(
MoveType.After,
Expand All @@ -54,23 +68,11 @@ private static void DisablePluginsIL(ILContext il)
x => x.MatchLdcI4(0), // false
x => x.MatchStloc(24) // someBool = false
);

c.Emit(OpCodes.Ldloc, 23); // current PluginInfo
c.Emit(OpCodes.Ldloc, 5); // set of denied plugins so far
// false means skip to this plugin, true means continue loading it
c.EmitDelegate(static bool (PluginInfo plugin, HashSet<string> deniedSet) =>
{
if (Array.IndexOf(ModList.DisabledPluginGuids, plugin.Metadata.GUID) == -1)
{
return true;
}

deniedSet.Add(plugin.Metadata.GUID);
ModList.DisabledPlugins.Add(plugin);
Entrypoint.LogSource.LogInfo($"{plugin.Metadata.GUID} was disabled, skipping loading...");
return false;

});
c.EmitDelegate(CheckIfModIsDisabled);
c.Emit(OpCodes.Brfalse, continueLabel);
}
}

0 comments on commit 430dff3

Please sign in to comment.