From 996e06d3736ba19560d27810e84674e006b5580f Mon Sep 17 00:00:00 2001 From: AuriRex Date: Fri, 15 Sep 2023 04:21:48 +0200 Subject: [PATCH] Fix double initialization with MTFO "Reload Game Data" button --- TheArchive.Core/ArchiveMod.cs | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/TheArchive.Core/ArchiveMod.cs b/TheArchive.Core/ArchiveMod.cs index 1e69a179..67e3105b 100644 --- a/TheArchive.Core/ArchiveMod.cs +++ b/TheArchive.Core/ArchiveMod.cs @@ -58,6 +58,7 @@ public static JsonSerializerSettings JsonSerializerSettings public static GameBuildInfo CurrentBuildInfo { get; private set; } public static int CurrentGameState { get; private set; } public static bool IsOnALTBuild { get; private set; } + public static bool IsInitialized { get; private set; } = false; /// /// The currently selected rundown on the rundown screen.
/// Is equal to on the "Select Rundown" screen.
@@ -273,6 +274,15 @@ public static bool RegisterModule(Type moduleType) internal static void InvokeGameDataInitialized() { + if (IsInitialized) + { + // Most likely a reload has been triggered via the MTFO `Reload Game Data` button + ArchiveLogger.Info($"Reload triggered, skipping init."); + return; + } + + IsInitialized = true; + ArchiveLogger.Info($"GameData has been initialized, invoking event."); try @@ -405,7 +415,8 @@ private static void InitInitializables(Type type, out IInitializable initializab bool init = true; var initSingletonbase_Type = typeof(InitSingletonBase<>).MakeGenericType(type); - if (initSingletonbase_Type.IsAssignableFrom(type)) + var isInitSingleton = initSingletonbase_Type.IsAssignableFrom(type); + if (isInitSingleton) { initSingletonbase_Type.GetProperty(nameof(InitSingletonBase.Instance), AnyBindingFlagss).SetValue(null, instance); } @@ -421,7 +432,17 @@ private static void InitInitializables(Type type, out IInitializable initializab { var conditional = (IInitCondition)instance; - init = conditional.InitCondition(); + try + { + init = conditional.InitCondition(); + } + catch(Exception ex) + { + ArchiveLogger.Error($"{nameof(IInitCondition.InitCondition)} method on Type \"{type.FullName}\" failed!"); + ArchiveLogger.Warning($"This {nameof(IInitializable)} won't be initialized."); + ArchiveLogger.Exception(ex); + init = false; + } } if (init) @@ -429,7 +450,10 @@ private static void InitInitializables(Type type, out IInitializable initializab try { instance.Init(); - initSingletonbase_Type.GetProperty(nameof(InitSingletonBase.HasBeenInitialized), AnyBindingFlagss).SetValue(null, true); + if(isInitSingleton) + { + initSingletonbase_Type.GetProperty(nameof(InitSingletonBase.HasBeenInitialized), AnyBindingFlagss).SetValue(null, true); + } } catch(Exception ex) {