From de09dd74f04b18032a8df2330af54e96dc913570 Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Sun, 1 Aug 2021 10:15:41 -0500 Subject: [PATCH] Make save loader replacement gen executor once --- PathfinderAPI/Replacements/SaveLoader.cs | 57 +++++++++++++----------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/PathfinderAPI/Replacements/SaveLoader.cs b/PathfinderAPI/Replacements/SaveLoader.cs index 65498faf..10b46c75 100644 --- a/PathfinderAPI/Replacements/SaveLoader.cs +++ b/PathfinderAPI/Replacements/SaveLoader.cs @@ -59,40 +59,18 @@ private struct SaveExecutorHolder CustomExecutors.RemoveAll(x => x.ExecutorType == tType); } - static SaveLoader() - { - EventManager.onPluginUnload += OnPluginUnload; - } - private static void OnPluginUnload(Assembly pluginAsm) { var allTypes = AccessTools.GetTypesFromAssembly(pluginAsm); CustomExecutors.RemoveAll(x => allTypes.Contains(x.ExecutorType)); } - - [HarmonyPrefix] - [HarmonyPatch(typeof(OS), nameof(OS.loadSaveFile))] - internal static bool SaveLoadReplacementPrefix(ref OS __instance) - { - __instance.FirstTimeStartup = false; - Stream saveStream = __instance.ForceLoadOverrideStream ?? - SaveFileManager.GetSaveReadStream(__instance.SaveGameUserName); - if (saveStream == null) - { - return false; - } - - var os = __instance; - - var executor = new EventExecutor(new StreamReader(saveStream).ReadToEnd(), false); + private static EventExecutor executor = new EventExecutor(); + private static OS os = null; - foreach (var custom in CustomExecutors) - { - var customInstance = (SaveExecutor)Activator.CreateInstance(custom.ExecutorType); - customInstance.Init(os); - executor.RegisterExecutor(custom.Element, customInstance.Execute, custom.Options); - } + static SaveLoader() + { + EventManager.onPluginUnload += OnPluginUnload; executor.RegisterExecutor("HacknetSave", (exec, info) => { @@ -185,6 +163,31 @@ internal static bool SaveLoadReplacementPrefix(ref OS __instance) os.homeNodeID = info.Attributes.GetString("homeNode", os.homeNodeID); os.homeAssetServerID = info.Attributes.GetString("homeAssetsNode", os.homeAssetServerID); }); + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(OS), nameof(OS.loadSaveFile))] + internal static bool SaveLoadReplacementPrefix(ref OS __instance) + { + __instance.FirstTimeStartup = false; + + Stream saveStream = __instance.ForceLoadOverrideStream ?? + SaveFileManager.GetSaveReadStream(__instance.SaveGameUserName); + if (saveStream == null) + { + return false; + } + + os = __instance; + + executor.SetText(new StreamReader(saveStream).ReadToEnd(), false); + + foreach (var custom in CustomExecutors) + { + var customInstance = (SaveExecutor)Activator.CreateInstance(custom.ExecutorType); + customInstance.Init(os); + executor.RegisterTempExecutor(custom.Element, customInstance.Execute, custom.Options); + } executor.Parse();