Skip to content

Commit

Permalink
Fix incompatibility with HugsLib 9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Byass committed Jul 20, 2021
1 parent 450ffac commit 35f8bc3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 32 deletions.
Binary file modified 1.3/Assemblies/BetterLoading.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion About/Manifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Manifest>
<identifier>BetterLoading</identifier>
<version>3.1.3.0</version>
<version>3.1.3.1</version>
<loadBefore>
<li>Core &gt;= 1.0</li>
<li>Startupimpact</li>
Expand Down
43 changes: 13 additions & 30 deletions Source/Compat/HugsLib/StageHugsLibInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ public class StageHugsLibInit : LoadingStage

private static List<object>? _children;
private static int _numChildrenInitialized;
private static int _numChildrenCheckedForUpdate;
private static int _numChildrenDefLoaded;

private static object? _currentChildMod;
private static Harmony? hInstance;
private static Harmony hInstance;

private static PropertyInfo _modIdentifierProperty;

Expand All @@ -42,27 +41,23 @@ public override string GetStageName()

if (_currentChildMod != null)
{
if (_numChildrenInitialized < _children.Count)
if (_numChildrenInitialized < _children!.Count)
return $"Initializing child mods: {_numChildrenInitialized} / {_children}: {_modIdentifierProperty.GetValue(_currentChildMod, null)}";

if (_numChildrenCheckedForUpdate < _children.Count)
return $"Checking for mod updates: {_numChildrenCheckedForUpdate} / {_children}: {_modIdentifierProperty.GetValue(_currentChildMod, null)}";

if (_numChildrenDefLoaded < _children.Count)
return $"Invoking post-load callbacks: {_numChildrenDefLoaded} / {_children}: {_modIdentifierProperty.GetValue(_currentChildMod, null)}";
}

return _numChildrenDefLoaded == _children.Count ? "Finishing up" : null;
return _numChildrenDefLoaded == _children!.Count ? "Finishing up" : null;
}

public override int GetCurrentProgress()
{
if (_done) return GetMaximumProgress() + 1;

var result = _hasEnumeratedChildren ? 1 : 0;

result += _numChildrenInitialized;
result += _numChildrenCheckedForUpdate;
result += _numChildrenDefLoaded;
// result += _done ? 1 : 0;

Expand All @@ -87,19 +82,19 @@ public override void DoPatching(Harmony instance)

var hlAssembly = LoadedModManager.RunningMods.First(m => m.Name == "HugsLib").assemblies.loadedAssemblies.Find(a => a.GetName().Name == "HugsLib");

var controllerType = hlAssembly.GetTypes().First(t => t.Name == "HugsLibController");
var updateFeatureManagerType = hlAssembly.GetTypes().First(t => t.Name == "UpdateFeatureManager");
var controllerType = hlAssembly.GetTypes().FirstOrDefault(t => t.Name == "HugsLibController") ?? throw new Exception("Type HugsLibController is missing");

_modIdentifierProperty = hlAssembly.GetTypes().First(t => t.Name == "ModBase").GetProperty("ModIdentifier");
_modIdentifierProperty = hlAssembly.GetTypes().First(t => t.Name == "ModBase").GetProperty("ModIdentifier") ?? throw new Exception("Property ModBase.ModIdentifier is missing");

Log.Message($"[BetterLoading:HugsLib Compat] Resolved required HugsLib types as follows: Controller: {controllerType?.FullName} / Update Manager: {updateFeatureManagerType?.FullName} / Mod Identifier (Property): {_modIdentifierProperty?.Name}");
Log.Message($"[BetterLoading:HugsLib Compat] Resolved required HugsLib types as follows: Controller: {controllerType.FullName} / Mod Identifier (Property): {_modIdentifierProperty.Name}");

hInstance.Patch(AccessTools.Method(controllerType, "LoadReloadInitialize"), postfix: new HarmonyMethod(typeof(StageHugsLibInit), nameof(PostLRI)));
hInstance.Patch(AccessTools.Method(controllerType, "EnumerateChildMods"), postfix: new HarmonyMethod(typeof(StageHugsLibInit), nameof(PostEnumerateChildren)));
hInstance.Patch(
AccessTools.Method(updateFeatureManagerType, "InspectActiveMod"),
new HarmonyMethod(typeof(StageHugsLibInit), nameof(PreUpdateCheck)),
new HarmonyMethod(typeof(StageHugsLibInit), nameof(PostUpdateCheck))
AccessTools.Method(controllerType, "LoadReloadInitialize") ?? throw new Exception("Method HugsLibController.LoadReloadInitialize is missing"),
postfix: new HarmonyMethod(typeof(StageHugsLibInit), nameof(PostLRI))
);
hInstance.Patch(
AccessTools.Method(controllerType, "EnumerateChildMods") ?? throw new Exception("Method HugsLibController.EnumerateChildMods is missing"),
postfix: new HarmonyMethod(typeof(StageHugsLibInit), nameof(PostEnumerateChildren))
);

Log.Message("[BetterLoading:HugsLib Compat] Successfully blind-patched HugsLib.");
Expand Down Expand Up @@ -146,18 +141,6 @@ public static void PostChildInit()
BetterLoadingApi.DispatchChange(inst);
}

public static void PreUpdateCheck(string modId)
{
_currentChildMod = _children?.Find(m => (string) _modIdentifierProperty.GetValue(m, null) == modId);
BetterLoadingApi.DispatchChange(inst);
}

public static void PostUpdateCheck()
{
_numChildrenCheckedForUpdate++;
BetterLoadingApi.DispatchChange(inst);
}

public static void PreDefsLoaded(object __instance)
{
if (__instance.GetType().GetProperty("ModIdentifier") == null) return;
Expand Down
1 change: 0 additions & 1 deletion Source/Stage/InitialLoad/9StageRunPostFinalizeCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ public static bool PreExecToExecWhenFinished(List<Action> ___toExecuteWhenFinish
Thread.Sleep(0);
GlobalTimingData.TicksFinishedPostFinalize = DateTime.UtcNow.Ticks;
Log.Message("Finished post-finalize at " + GlobalTimingData.TicksFinishedPostFinalize);
_finishedExecuting = true;
}, null, true, null);
Expand Down

0 comments on commit 35f8bc3

Please sign in to comment.