Skip to content

Commit

Permalink
cleans up conditional compiles; tries to fix #533; refactors CodeMatc…
Browse files Browse the repository at this point in the history
…h; adds IEnumerable<CodeInstruction>.Matches()
  • Loading branch information
pardeike committed Jun 17, 2023
1 parent 41e9431 commit b9206db
Show file tree
Hide file tree
Showing 26 changed files with 136 additions and 152 deletions.
2 changes: 1 addition & 1 deletion Harmony/Extras/DelegateTypeFactory.cs
Expand Up @@ -44,7 +44,7 @@ public Type CreateDelegateType(MethodInfo method)
for (var i = 0; i < parameters.Length; i++)
invokeMethod.DefineParameter(i + 1, ParameterAttributes.None, parameters[i].Name);

#if NETSTANDARD2_0
#if NETSTANDARD
return typeBuilder.CreateTypeInfo().AsType();
#else
return typeBuilder.CreateType();
Expand Down
2 changes: 1 addition & 1 deletion Harmony/ForwardingAttributes/Mono.Cecil.Cil.cs
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0
#if NETFRAMEWORK || NETSTANDARD
using System.Runtime.CompilerServices;

[assembly: TypeForwardedTo(typeof(Mono.Cecil.Cil.Code))]
Expand Down
2 changes: 1 addition & 1 deletion Harmony/ForwardingAttributes/Mono.Cecil.Mdb.cs
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0
#if NETFRAMEWORK || NETSTANDARD
using System.Runtime.CompilerServices;

[assembly: TypeForwardedTo(typeof(Mono.Cecil.Mdb.MdbReader))]
Expand Down
2 changes: 1 addition & 1 deletion Harmony/ForwardingAttributes/Mono.Cecil.Pdb.cs
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0
#if NETFRAMEWORK || NETSTANDARD
using System.Runtime.CompilerServices;

[assembly: TypeForwardedTo(typeof(Mono.Cecil.Pdb.NativePdbReader))]
Expand Down
2 changes: 1 addition & 1 deletion Harmony/ForwardingAttributes/Mono.Cecil.Rocks.cs
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0
#if NETFRAMEWORK || NETSTANDARD
using System.Runtime.CompilerServices;

[assembly: TypeForwardedTo(typeof(Mono.Cecil.Rocks.IILVisitor))]
Expand Down
2 changes: 1 addition & 1 deletion Harmony/ForwardingAttributes/Mono.Cecil.cs
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0
#if NETFRAMEWORK || NETSTANDARD
using System.Runtime.CompilerServices;

[assembly: TypeForwardedTo(typeof(Mono.Cecil.AssemblyAttributes))]
Expand Down
2 changes: 1 addition & 1 deletion Harmony/ForwardingAttributes/Mono.Collections.Generic.cs
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0
#if NETFRAMEWORK || NETSTANDARD
using System.Runtime.CompilerServices;

[assembly: TypeForwardedTo(typeof(Mono.Collections.Generic.Collection<>))]
Expand Down
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0
#if NETFRAMEWORK || NETSTANDARD
using System.Runtime.CompilerServices;

[assembly: TypeForwardedTo(typeof(Mono.CompilerServices.SymbolWriter.AnonymousScopeEntry))]
Expand Down
2 changes: 1 addition & 1 deletion Harmony/ForwardingAttributes/MonoMod.Utils.Cil.cs
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0
#if NETFRAMEWORK || NETSTANDARD
using System.Runtime.CompilerServices;

[assembly: TypeForwardedTo(typeof(MonoMod.Utils.Cil.CecilILGenerator))]
Expand Down
2 changes: 1 addition & 1 deletion Harmony/ForwardingAttributes/MonoMod.Utils.cs
@@ -1,4 +1,4 @@
#if NETFRAMEWORK || NETSTANDARD2_0
#if NETFRAMEWORK || NETSTANDARD
using System.Runtime.CompilerServices;

[assembly: TypeForwardedTo(typeof(MonoMod.Utils.DMDEmitDynamicMethodGenerator))]
Expand Down
4 changes: 0 additions & 4 deletions Harmony/Harmony.csproj
Expand Up @@ -84,10 +84,6 @@
<DebugSymbols>false</DebugSymbols>
</PropertyGroup>

<PropertyGroup Condition="$(IsNET5OrGreater)">
<DefineConstants>$(DefineConstants);NET50_OR_GREATER</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MonoMod.Core" Version="$(MonoModCoreVersion)" />
<!-- Reference assemblies are needed for non-Windows .NET Framework targeting builds. -->
Expand Down
3 changes: 0 additions & 3 deletions Harmony/Internal/Emitter.cs
Expand Up @@ -356,16 +356,13 @@ internal void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[] optionalPara
il.EmitCall(opcode, methodInfo, optionalParameterTypes);
}

#if NETSTANDARD2_0 || NETCOREAPP2_0
#else
internal void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv, Type returnType, Type[] parameterTypes)
{
instructions.Add(CurrentPos(), new CodeInstruction(opcode, unmanagedCallConv));
var extra = returnType.FullName + " " + parameterTypes.Description();
LogIL(opcode, unmanagedCallConv, extra);
il.EmitCalli(opcode, unmanagedCallConv, returnType, parameterTypes);
}
#endif

internal void EmitCalli(OpCode opcode, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type[] optionalParameterTypes)
{
Expand Down
4 changes: 2 additions & 2 deletions Harmony/Internal/PatchTools.cs
Expand Up @@ -52,7 +52,7 @@ internal static MethodInfo GetPatchMethod(Type patchType, string attributeName)
internal static AssemblyBuilder DefineDynamicAssembly(string name)
{
var assemblyName = new AssemblyName(name);
#if NETCOREAPP2_0 || NETCOREAPP3_0 || NETCOREAPP3_1 || NETSTANDARD2_0 || NET50_OR_GREATER
#if NETCOREAPP || NETSTANDARD || NET5_0_OR_GREATER
return AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
#else
return AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
Expand Down Expand Up @@ -102,7 +102,7 @@ internal static MethodBase GetOriginalMethod(this HarmonyMethod attr)
var enumMethod = AccessTools.DeclaredMethod(attr.declaringType, attr.methodName, attr.argumentTypes);
return AccessTools.EnumeratorMoveNext(enumMethod);

#if NET45_OR_GREATER
#if NET45_OR_GREATER || NETSTANDARD || NETCOREAPP
case MethodType.Async:
if (attr.methodName is null)
return null;
Expand Down
2 changes: 1 addition & 1 deletion Harmony/Public/Attributes.cs
Expand Up @@ -19,7 +19,7 @@ public enum MethodType
StaticConstructor,
/// <summary>This targets the MoveNext method of the enumerator result, that actually contains the method's implementation</summary>
Enumerator,
#if NET45_OR_GREATER
#if NET45_OR_GREATER || NETSTANDARD || NETCOREAPP
/// <summary>This targets the MoveNext method of the async state machine, that actually contains the method's implementation</summary>
Async
#endif
Expand Down
4 changes: 2 additions & 2 deletions Harmony/Public/Harmony.cs
Expand Up @@ -47,7 +47,7 @@ public Harmony(string id)
var location = assembly.Location;
var environment = Environment.Version.ToString();
var platform = Environment.OSVersion.Platform.ToString();
#if !NET50_OR_GREATER
#if !NET5_0_OR_GREATER
if (string.IsNullOrEmpty(location)) location = new Uri(assembly.CodeBase).LocalPath;
#endif
FileLog.Log($"### Harmony id={id}, version={version}, location={location}, env/clr={environment}, platform={platform}");
Expand All @@ -56,7 +56,7 @@ public Harmony(string id)
{
var callingAssembly = callingMethod.DeclaringType.Assembly;
location = callingAssembly.Location;
#if !NET50_OR_GREATER
#if !NET5_0_OR_GREATER
if (string.IsNullOrEmpty(location)) location = new Uri(callingAssembly.CodeBase).LocalPath;
#endif
FileLog.Log($"### Started from {callingMethod.FullDescription()}, location {location}");
Expand Down
26 changes: 13 additions & 13 deletions Harmony/Public/Patch.cs
Expand Up @@ -5,7 +5,7 @@
using System.Reflection.Emit;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
#if NET50_OR_GREATER
#if NET5_0_OR_GREATER
using System.Text.Json;
using System.Text.Json.Serialization;
#endif
Expand All @@ -16,7 +16,7 @@ namespace HarmonyLib
///
internal static class PatchInfoSerialization
{
#if NET50_OR_GREATER
#if NET5_0_OR_GREATER
internal static bool? useBinaryFormatter = null;
internal static bool UseBinaryFormatter
{
Expand Down Expand Up @@ -68,14 +68,14 @@ public override Type BindToType(string assemblyName, string typeName)
///
internal static byte[] Serialize(this PatchInfo patchInfo)
{
#if NET50_OR_GREATER
#if NET5_0_OR_GREATER
if (UseBinaryFormatter)
{
#endif
using var streamMemory = new MemoryStream();
binaryFormatter.Serialize(streamMemory, patchInfo);
return streamMemory.GetBuffer();
#if NET50_OR_GREATER
#if NET5_0_OR_GREATER
}
else
return JsonSerializer.SerializeToUtf8Bytes(patchInfo);
Expand All @@ -88,13 +88,13 @@ internal static byte[] Serialize(this PatchInfo patchInfo)
///
internal static PatchInfo Deserialize(byte[] bytes)
{
#if NET50_OR_GREATER
#if NET5_0_OR_GREATER
if (UseBinaryFormatter)
{
#endif
using var streamMemory = new MemoryStream(bytes);
return (PatchInfo)binaryFormatter.Deserialize(streamMemory);
#if NET50_OR_GREATER
#if NET5_0_OR_GREATER
}
else
{
Expand Down Expand Up @@ -130,35 +130,35 @@ public class PatchInfo
{
/// <summary>Prefixes as an array of <see cref="Patch"/></summary>
///
#if NET50_OR_GREATER
#if NET5_0_OR_GREATER
[JsonInclude]
#endif
public Patch[] prefixes = new Patch[0];

/// <summary>Postfixes as an array of <see cref="Patch"/></summary>
///
#if NET50_OR_GREATER
#if NET5_0_OR_GREATER
[JsonInclude]
#endif
public Patch[] postfixes = new Patch[0];

/// <summary>Transpilers as an array of <see cref="Patch"/></summary>
///
#if NET50_OR_GREATER
#if NET5_0_OR_GREATER
[JsonInclude]
#endif
public Patch[] transpilers = new Patch[0];

/// <summary>Finalizers as an array of <see cref="Patch"/></summary>
///
#if NET50_OR_GREATER
#if NET5_0_OR_GREATER
[JsonInclude]
#endif
public Patch[] finalizers = new Patch[0];

/// <summary>Returns if any of the patches wants debugging turned on</summary>
///
#if NET50_OR_GREATER
#if NET5_0_OR_GREATER
[JsonIgnore]
#endif
public bool Debugging => prefixes.Any(p => p.debug) || postfixes.Any(p => p.debug) || transpilers.Any(p => p.debug) || finalizers.Any(p => p.debug);
Expand Down Expand Up @@ -306,7 +306,7 @@ private static Patch[] Remove(string owner, Patch[] current)

/// <summary>A serializable patch</summary>
///
#if NET50_OR_GREATER
#if NET5_0_OR_GREATER
[JsonConverter(typeof(PatchJsonConverter))]
#endif
[Serializable]
Expand Down Expand Up @@ -343,7 +343,7 @@ public class Patch : IComparable

/// <summary>The method of the static patch method</summary>
///
#if NET50_OR_GREATER
#if NET5_0_OR_GREATER
[JsonIgnore]
#endif
public MethodInfo PatchMethod
Expand Down
2 changes: 1 addition & 1 deletion Harmony/Public/PatchJsonConverter.cs
@@ -1,4 +1,4 @@
#if NET50_OR_GREATER
#if NET5_0_OR_GREATER
using System;
using System.Collections.Generic;
using System.Text.Json;
Expand Down
4 changes: 2 additions & 2 deletions Harmony/Tools/AccessTools.cs
Expand Up @@ -10,7 +10,7 @@
using System.Runtime.Serialization;
using System.Threading;

#if NET45_OR_GREATER
#if NET45_OR_GREATER || NETSTANDARD || NETCOREAPP
using System.Runtime.CompilerServices;
#endif

Expand Down Expand Up @@ -600,7 +600,7 @@ public static MethodInfo EnumeratorMoveNext(MethodBase method)
return Method(type, nameof(IEnumerator.MoveNext));
}

#if NET45_OR_GREATER
#if NET45_OR_GREATER || NETSTANDARD || NETCOREAPP
/// <summary>Gets the <see cref="IAsyncStateMachine.MoveNext"/> method of an async method's state machine</summary>
/// <param name="method">Async method that creates the state machine internally</param>
/// <returns>The internal <see cref="IAsyncStateMachine.MoveNext"/> method of the async state machine or <b>null</b> if no valid async method is detected</returns>
Expand Down

0 comments on commit b9206db

Please sign in to comment.