You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Sonarr/src/Sonarr.RuntimePatches/RuntimePatchExtensions.cs

79 lines
2.1 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using HarmonyLib;
namespace NzbDrone.RuntimePatches
{
public static class RuntimePatchExtensions
{
public static bool Matches(this List<CodeInstruction> instructions, params OpCode[] opcodes)
{
var codes = instructions.Select(v => v.opcode).Where(v => v != OpCodes.Nop).ToList();
if (codes.Count != opcodes.Length)
{
return false;
}
for (var i = 0; i < codes.Count; i++)
{
if (codes[i] != opcodes[i])
{
return false;
}
}
return true;
}
public static Type[] GetParameterTypes(this MethodBase method)
{
return Array.ConvertAll(method.GetParameters(), v => v.ParameterType);
}
public static string GetSimplifiedName(this MethodBase method, bool includeNamespace = false)
{
return $"{method.DeclaringType.GetSimplifiedName()}.{method.Name}";
}
public static string GetSimplifiedName(this Type t, bool includeNamespace = false)
{
var sb = new StringBuilder();
if (includeNamespace && string.IsNullOrEmpty(t.Namespace))
{
sb.Append(t.Namespace);
sb.Append('.');
}
if (t.IsGenericType)
{
sb.Append(t.Name, 0, t.Name.LastIndexOf('`'));
sb.Append('<');
var args = t.GetGenericArguments();
for (var i = 0; i < args.Length; i++)
{
if (i != 0)
{
sb.Append(", ");
}
sb.Append(GetSimplifiedName(args[i], includeNamespace));
}
sb.Append('>');
}
else
{
sb.Append(t.Name);
}
return sb.ToString();
}
}
}