Skip to content

Commit

Permalink
Replace DAS/FDAS deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Windows10CE committed Aug 6, 2021
1 parent af43018 commit 330cbf2
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions PathfinderAPI/Replacements/ActionsLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Func<Stream, SerializableAction>>(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();
Expand Down

0 comments on commit 330cbf2

Please sign in to comment.