Skip to content

Commit

Permalink
fixes #444
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Jan 5, 2022
1 parent 37d083f commit cee4ece
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 91 deletions.
2 changes: 2 additions & 0 deletions Harmony/Documentation/examples/patching-transpiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
}
// </typical>

#pragma warning disable CS0649
class SomeType { public string someField; }
#pragma warning restore CS0649

class Tools
{
Expand Down
23 changes: 11 additions & 12 deletions Harmony/Public/Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
#if NET50_OR_GREATER
using System.Runtime;
using System.Text.Json;
using System.Text.Json.Serialization;
#endif
Expand All @@ -23,11 +22,11 @@ static bool UseBinaryFormatter
{
get
{
if(!useBinaryFormatter.HasValue)
if (!useBinaryFormatter.HasValue)
{
// https://github.com/dotnet/runtime/blob/208e377a5329ad6eb1db5e5fb9d4590fa50beadd/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/LocalAppContextSwitches.cs#L14
var hasSwitch = AppContext.TryGetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", out var isEnabled);
if(hasSwitch)
if (hasSwitch)
useBinaryFormatter = isEnabled;
else
{
Expand Down Expand Up @@ -61,7 +60,7 @@ public override Type BindToType(string assemblyName, string typeName)
return typeToDeserialize;
}
}
internal static BinaryFormatter binaryFormatter = new BinaryFormatter { Binder = new Binder() };
internal static readonly BinaryFormatter binaryFormatter = new BinaryFormatter { Binder = new Binder() };

/// <summary>Serializes a patch info</summary>
/// <param name="patchInfo">The <see cref="PatchInfo"/></param>
Expand All @@ -70,16 +69,16 @@ public override Type BindToType(string assemblyName, string typeName)
internal static byte[] Serialize(this PatchInfo patchInfo)
{
#if NET50_OR_GREATER
if(UseBinaryFormatter)
if (UseBinaryFormatter)
{
#endif
using var streamMemory = new MemoryStream();
binaryFormatter.Serialize(streamMemory, patchInfo);
return streamMemory.GetBuffer();
using var streamMemory = new MemoryStream();
binaryFormatter.Serialize(streamMemory, patchInfo);
return streamMemory.GetBuffer();
#if NET50_OR_GREATER
}
else
return JsonSerializer.SerializeToUtf8Bytes<PatchInfo>(patchInfo);
return JsonSerializer.SerializeToUtf8Bytes(patchInfo);
#endif
}

Expand All @@ -90,11 +89,11 @@ internal static byte[] Serialize(this PatchInfo patchInfo)
internal static PatchInfo Deserialize(byte[] bytes)
{
#if NET50_OR_GREATER
if(UseBinaryFormatter)
if (UseBinaryFormatter)
{
#endif
using var streamMemory = new MemoryStream(bytes);
return (PatchInfo)binaryFormatter.Deserialize(streamMemory);
using var streamMemory = new MemoryStream(bytes);
return (PatchInfo)binaryFormatter.Deserialize(streamMemory);
#if NET50_OR_GREATER
}
else
Expand Down
79 changes: 79 additions & 0 deletions Harmony/Public/PatchJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#if NET50_OR_GREATER
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace HarmonyLib
{
internal class PatchJsonConverter : JsonConverter<Patch>
{
public override Patch Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
_ = reader.Read(); // start object

_ = reader.Read(); // index
var index = reader.GetInt32();
_ = reader.Read();

_ = reader.Read(); // debug
var debug = reader.GetBoolean();
_ = reader.Read();

_ = reader.Read(); // owner
var owner = reader.GetString();
_ = reader.Read();

_ = reader.Read(); // priority
var priority = reader.GetInt32();
_ = reader.Read();

_ = reader.Read(); // methodToken
var methodToken = reader.GetInt32();
_ = reader.Read();

_ = reader.Read(); // moduleGUID
var moduleGUID = reader.GetString();
_ = reader.Read();

_ = reader.Read(); // after
var after = new List<string>();
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
after.Add(reader.GetString());
_ = reader.Read();

_ = reader.Read(); // before
var before = new List<string>();
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
before.Add(reader.GetString());
_ = reader.Read();

_ = reader.Read(); // end object

return new Patch(index, owner, priority, before.ToArray(), after.ToArray(), debug, methodToken, moduleGUID);
}

public override void Write(Utf8JsonWriter writer, Patch patchValue, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteNumber("index", patchValue.index);
writer.WriteBoolean("debug", patchValue.debug);
writer.WriteString("owner", patchValue.owner);
writer.WriteNumber("priority", patchValue.priority);
writer.WriteNumber("methodToken", patchValue.PatchMethod.MetadataToken);
writer.WriteString("moduleGUID", patchValue.PatchMethod.Module.ModuleVersionId.ToString());
WriteStringArray(writer, "after", patchValue.after);
WriteStringArray(writer, "before", patchValue.before);
writer.WriteEndObject();
}

static void WriteStringArray(Utf8JsonWriter writer, string propertyName, IEnumerable<string> strings)
{
writer.WriteStartArray(propertyName);
foreach (var str in strings)
writer.WriteStringValue(str);
writer.WriteEndArray();
}
}
}
#endif
49 changes: 0 additions & 49 deletions Harmony/Public/PatchJsonConverter.net5.cs

This file was deleted.

30 changes: 0 additions & 30 deletions Harmony/Public/UtfJsonReaderExtensions.net5.cs

This file was deleted.

0 comments on commit cee4ece

Please sign in to comment.