From 39636217713267210289a7704ec041756c44c5b7 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Sat, 8 Jan 2022 02:39:32 -0800 Subject: [PATCH] clean up message test a bit --- QSBTests/MessageTests.cs | 42 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/QSBTests/MessageTests.cs b/QSBTests/MessageTests.cs index 4eefe65f..9dfe3233 100644 --- a/QSBTests/MessageTests.cs +++ b/QSBTests/MessageTests.cs @@ -2,6 +2,7 @@ using Mono.Cecil; using Mono.Cecil.Cil; using Mono.Cecil.Rocks; +using MonoMod.Cil; using QSB.Messaging; using QSB.Utility; using System; @@ -34,7 +35,7 @@ namespace QSBTests foreach (var field in fields) { - if (!field.Eq(fromField) && !field.Eq(toField) && !field.Eq(objectIdField)) + if (!field.GenericEq(fromField) && !field.GenericEq(toField) && !field.GenericEq(objectIdField)) { constructor.CheckUses(field, Util.UseType.Store); } @@ -46,60 +47,45 @@ namespace QSBTests } } - internal static class Util + public static partial class Util { public const BindingFlags Flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; - public static bool Eq(this MemberReference a, MemberReference b) => + /// + /// ignores open vs closed generic type + /// + public static bool GenericEq(this MemberReference a, MemberReference b) => a.DeclaringType.Namespace == b.DeclaringType.Namespace && a.DeclaringType.Name == b.DeclaringType.Name && a.Name == b.Name; - public static bool IsOp(this Instruction instruction, OpCode opCode, out T operand) - { - if (instruction.OpCode == opCode) - { - operand = (T)instruction.Operand; - return true; - } - - operand = default; - return false; - } - - public enum UseType { Store, Load }; + public enum UseType { Store, Load } public static void CheckUses(this MethodDefinition method, FieldReference field, UseType useType) { - var opCode = useType switch + Func matches = useType switch { - UseType.Store => OpCodes.Stfld, - UseType.Load => OpCodes.Ldfld, + UseType.Store => x => x.MatchStfld(out var f) && f.GenericEq(field), + UseType.Load => x => x.MatchLdfld(out var f) && f.GenericEq(field), _ => throw new ArgumentOutOfRangeException(nameof(useType), useType, null) }; while (true) { var il = method.Body.Instructions; - var uses = il.Any(x => - x.IsOp(opCode, out FieldReference f) && - f.Eq(field) - ); + var uses = il.Any(matches); if (uses) { return; } var baseMethod = method.GetBaseMethod(); - if (baseMethod.Eq(method)) + if (baseMethod == method) { break; } - var callsBase = il.Any(x => - x.IsOp(OpCodes.Call, out MethodReference m) && - m.Eq(baseMethod) - ); + var callsBase = il.Any(x => x.MatchCall(out var m) && m.GenericEq(baseMethod)); if (!callsBase) { break;