Skip to content

Commit

Permalink
Call hooks in correct order.
Browse files Browse the repository at this point in the history
  • Loading branch information
Banane9 committed Feb 16, 2024
1 parent c290ea4 commit efeaedc
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions MonkeyLoader.Resonite.Integration/EngineInitHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,19 @@ internal sealed class EngineInitHook : Monkey<EngineInitHook>
{
public override string Name { get; } = "Engine Init Hook";

private static IEnumerable<IResoniteMonkeyInternal> ResoniteMonkeys
=> Mod.Loader.Monkeys.SelectCastable<IMonkey, IResoniteMonkeyInternal>();
private static IResoniteMonkeyInternal[] ResoniteMonkeys
{
get
{
var monkeys = Mod.Loader.Monkeys
.SelectCastable<IMonkey, IResoniteMonkeyInternal>()
.ToArray();

Array.Sort(monkeys, Monkey.AscendingComparer);

return monkeys;
}
}

protected override IEnumerable<IFeaturePatch> GetFeaturePatches()
{
Expand All @@ -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<object>(() => $"{rM.Mod.Title}/{rM.Name}")));

Expand All @@ -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();
Expand All @@ -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<object>(() => $"{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();
Expand All @@ -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<object>(() => $"{rM.Mod.Title}/{rM.Name}")));

Expand Down

0 comments on commit efeaedc

Please sign in to comment.