Skip to content

Commit

Permalink
Stop serializing static fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Windows10CE committed Jul 30, 2021
1 parent f474d4a commit 0b8e036
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions PathfinderAPI/Replacements/ObjectSerializerReplacement.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using Hacknet;
Expand Down Expand Up @@ -40,6 +42,17 @@ public static bool SerializeObject(object o, bool preventOuterTag, ref string __
return true;
}

[HarmonyILManipulator]
[HarmonyPatch(typeof(ObjectSerializer), nameof(ObjectSerializer.SerializeObject), new Type[] {typeof(object), typeof(bool)})]
private static void DontSerializeStaticFields(ILContext il)
{
ILCursor c = new ILCursor(il);

c.GotoNext(MoveType.After, x => x.MatchCallOrCallvirt(AccessTools.Method(typeof(Type), nameof(Type.GetFields), new Type[0])));

c.EmitDelegate<Func<FieldInfo[], FieldInfo[]>>(fields => fields.Where(x => !x.IsStatic).ToArray());
}

[HarmonyPrefix]
[HarmonyPatch(typeof(ObjectSerializer), nameof(ObjectSerializer.DeserializeObject), new Type[] { typeof(XmlReader), typeof(Type) })]
public static bool DeserializeObject(XmlReader rdr, Type t, out object __result)
Expand Down

0 comments on commit 0b8e036

Please sign in to comment.