From 330cbf2092ba834c007cc0f548b9131f877ea8ee Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Fri, 6 Aug 2021 18:25:30 -0500 Subject: [PATCH] Replace DAS/FDAS deserialization --- PathfinderAPI/Replacements/ActionsLoader.cs | 43 +++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/PathfinderAPI/Replacements/ActionsLoader.cs b/PathfinderAPI/Replacements/ActionsLoader.cs index 526bba5d..77e05708 100644 --- a/PathfinderAPI/Replacements/ActionsLoader.cs +++ b/PathfinderAPI/Replacements/ActionsLoader.cs @@ -2,8 +2,11 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; +using System.Xml; using Hacknet; using HarmonyLib; +using MonoMod.Cil; using Pathfinder.Action; using Pathfinder.Util; using Pathfinder.Util.XML; @@ -34,6 +37,46 @@ private static bool LoadActionsIntoOSPrefix(string filepath, object OSobj) return false; } + [HarmonyILManipulator] + [HarmonyPatch(typeof(DelayableActionSystem), nameof(DelayableActionSystem.Update))] + [HarmonyPatch(typeof(FastDelayableActionSystem), nameof(FastDelayableActionSystem.DeserializeActions))] + private static void ReplaceDASDeserializeIL(ILContext il, MethodBase method) + { + ILCursor c = new ILCursor(il); + + c.GotoNext(MoveType.Before, + x => x.MatchLdloc(7), + x => x.MatchCallOrCallvirt(AccessTools.Method(typeof(XmlReader), nameof(XmlReader.Create), new Type[] { typeof(Stream) })), + x => x.MatchStloc(8), + x => x.MatchLdloc(8), + x => x.MatchCallOrCallvirt(AccessTools.Method(typeof(SerializableAction), nameof(SerializableAction.Deserialize))) + ); + + c.Index++; + c.RemoveRange(4); + c.EmitDelegate>(stream => + { + var executor = new EventExecutor(new StreamReader(stream).ReadToEnd(), false); + ElementInfo actionInfo = null; + executor.RegisterExecutor("*", (exec, info) => + { + actionInfo = info; + }, ParseOption.ParseInterior); + executor.Parse(); + return ReadAction(actionInfo); + }); + + if (method.Name == nameof(DelayableActionSystem.Update)) + { + c.GotoNext(MoveType.Before, + x => x.MatchLdloc(8), + x => x.MatchCallOrCallvirt(AccessTools.Method(typeof(XmlReader), nameof(XmlReader.Close))) + ); + + c.RemoveRange(2); + } + } + public static RunnableConditionalActions LoadActionSets(ElementInfo root) { var ret = new RunnableConditionalActions();