Skip to content

Commit

Permalink
Fix double initialization with MTFO "Reload Game Data" button
Browse files Browse the repository at this point in the history
  • Loading branch information
AuriRex committed Sep 15, 2023
1 parent 52a679c commit 996e06d
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions TheArchive.Core/ArchiveMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/// <summary>
/// The currently selected rundown on the rundown screen.<br/>
/// Is equal to <seealso cref="string.Empty"/> on the "Select Rundown" screen.<br/>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String>.Instance), AnyBindingFlagss).SetValue(null, instance);
}
Expand All @@ -421,15 +432,28 @@ 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)
{
try
{
instance.Init();
initSingletonbase_Type.GetProperty(nameof(InitSingletonBase<String>.HasBeenInitialized), AnyBindingFlagss).SetValue(null, true);
if(isInitSingleton)
{
initSingletonbase_Type.GetProperty(nameof(InitSingletonBase<String>.HasBeenInitialized), AnyBindingFlagss).SetValue(null, true);
}
}
catch(Exception ex)
{
Expand Down

0 comments on commit 996e06d

Please sign in to comment.