From efeaedcf9f69c1186845f9de8d6813534b487262 Mon Sep 17 00:00:00 2001 From: Arne Kiesewetter Date: Fri, 16 Feb 2024 03:12:18 +0100 Subject: [PATCH] Call hooks in correct order. --- .../EngineInitHook.cs | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/MonkeyLoader.Resonite.Integration/EngineInitHook.cs b/MonkeyLoader.Resonite.Integration/EngineInitHook.cs index 142251d..38e4766 100644 --- a/MonkeyLoader.Resonite.Integration/EngineInitHook.cs +++ b/MonkeyLoader.Resonite.Integration/EngineInitHook.cs @@ -19,8 +19,19 @@ internal sealed class EngineInitHook : Monkey { public override string Name { get; } = "Engine Init Hook"; - private static IEnumerable ResoniteMonkeys - => Mod.Loader.Monkeys.SelectCastable(); + private static IResoniteMonkeyInternal[] ResoniteMonkeys + { + get + { + var monkeys = Mod.Loader.Monkeys + .SelectCastable() + .ToArray(); + + Array.Sort(monkeys, Monkey.AscendingComparer); + + return monkeys; + } + } protected override IEnumerable GetFeaturePatches() { @@ -38,7 +49,7 @@ private static void InitializePrefix(Engine __instance) __instance.OnShutdownRequest += OnEngineShutdownRequested; __instance.OnShutdown += OnEngineShutdown; - var resoniteMonkeys = ResoniteMonkeys.ToArray(); + var resoniteMonkeys = ResoniteMonkeys; Logger.Trace(() => "Running EngineInit hooks in this order:"); Logger.Trace(resoniteMonkeys.Select(rM => new Func(() => $"{rM.Mod.Title}/{rM.Name}"))); @@ -49,7 +60,7 @@ private static void InitializePrefix(Engine __instance) var sw = Stopwatch.StartNew(); - foreach (var resoniteMonkey in ResoniteMonkeys) + foreach (var resoniteMonkey in resoniteMonkeys) { LoadProgressIndicator.SetSubphase(resoniteMonkey.Name); resoniteMonkey.EngineInit(); @@ -64,14 +75,14 @@ private static void OnEngineReady() // Potentially move this to be a postfix of init or run as Task as otherwise it's blocking. Info(() => "Engine is ready! Executing EngineReady hooks on ResoniteMonkeys!"); - var resoniteMonkeys = ResoniteMonkeys.ToArray(); + var resoniteMonkeys = ResoniteMonkeys; Logger.Trace(() => "Running EngineReady hooks in this order:"); Logger.Trace(resoniteMonkeys.Select(rM => new Func(() => $"{rM.Mod.Title}/{rM.Name}"))); LoadProgressIndicator.AdvanceFixedPhase("Executing EngineReady Hooks..."); var sw = Stopwatch.StartNew(); - foreach (var resoniteMonkey in ResoniteMonkeys) + foreach (var resoniteMonkey in resoniteMonkeys) { LoadProgressIndicator.SetSubphase(resoniteMonkey.Name); resoniteMonkey.EngineReady(); @@ -93,7 +104,10 @@ private static void OnEngineShutdownRequested(string reason) { Info(() => "Engine shutdown has been requested! Executing EngineShutdownRequested hooks on ResoniteMonkeys!"); - var resoniteMonkeys = ResoniteMonkeys.ToArray(); + // Go through monkeys in reverse order compared to launch + var resoniteMonkeys = ResoniteMonkeys; + Array.Reverse(resoniteMonkeys); + Logger.Trace(() => "Running EngineShutdownRequested hooks in this order:"); Logger.Trace(resoniteMonkeys.Select(rM => new Func(() => $"{rM.Mod.Title}/{rM.Name}")));