Skip to content

Commit

Permalink
Make save loader replacement gen executor once
Browse files Browse the repository at this point in the history
  • Loading branch information
Windows10CE committed Aug 1, 2021
1 parent 6e760e5 commit de09dd7
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions PathfinderAPI/Replacements/SaveLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
{
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit de09dd7

Please sign in to comment.