From 0197431baf53cb8f071ecfc5bca005b589615c15 Mon Sep 17 00:00:00 2001 From: CWX Date: Sun, 11 Aug 2024 11:33:43 +0100 Subject: [PATCH] Rename to reflectionHelper --- RecodeItLib/Dumper/DumpyInstructionsHelper.cs | 189 ------------------ ...TypeHelper.cs => DumpyReflectionHelper.cs} | 37 +++- 2 files changed, 36 insertions(+), 190 deletions(-) delete mode 100644 RecodeItLib/Dumper/DumpyInstructionsHelper.cs rename RecodeItLib/Dumper/{DumpyTypeHelper.cs => DumpyReflectionHelper.cs} (62%) diff --git a/RecodeItLib/Dumper/DumpyInstructionsHelper.cs b/RecodeItLib/Dumper/DumpyInstructionsHelper.cs deleted file mode 100644 index d03c783..0000000 --- a/RecodeItLib/Dumper/DumpyInstructionsHelper.cs +++ /dev/null @@ -1,189 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection.Metadata; -using System.Runtime.CompilerServices; -using System.Threading.Tasks; -using dnlib.DotNet; -using dnlib.DotNet.Emit; -using DumpLib; -using ReCodeIt.Utils; - -namespace ReCodeItLib.Dumper; - -public static class DumpyInstructionsHelper -{ - /// - /// Sets up local variables and returns a List of instructions to add. - /// - /// AssemblyDefinition - /// MethodDefinition - /// List - public static List GetBackRequestInstructions(ModuleDefMD assembly, MethodDef method) - { - var importer = new Importer(assembly); - - return new List - { - Instruction.Create(OpCodes.Ldarg_1), - Instruction.Create(OpCodes.Ldloc_S, method.Body.Variables[6]), - Instruction.Create(OpCodes.Call, importer.Import(typeof(DumpLib.DumpyTool).GetMethod("LogRequestResponse", new[] { typeof(object), typeof(object) }))) - }; - } - - /// - /// Returns a List of instructions to be added to the method. - /// This is an Async method so there is two parts, this part and a RunValidation method. - /// - /// AssemblyDefinition - /// MethodDefinition - /// List - public static List GetRunValidationInstructionsMoveNext(ModuleDefMD assembly, MethodDef method) - { - // TODO: [CWX] TRIED CHANGING OPTIONS - var importer = new Importer(assembly, ImporterOptions.TryToUseExistingAssemblyRefs); - var test = ModuleDefMD.Load("C:\\Battlestate Games\\Escape from Tarkov\\EscapeFromTarkov_Data\\Managed\\mscorlib.dll"); - - // Add our own local variables - - // var1 index0 class1159Type - var sptClassType = assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType); - var sptClass = new Local(sptClassType.ToTypeSig()); - method.Body.Variables.Add(sptClass); - - // var2 index1 ExceptionType - // TODO: [CWX] this is the problem, Exception is being imported via System.Private.CoreLib... Needs to come from MsCorLib in EFT managed Dir - var typer = test.GetTypes().First(x => x.Name.ToLower() == "exception"); - var sptExceptionType = importer.Import(typer); - var sptException = new Local(sptExceptionType.ToTypeSig()); - method.Body.Variables.Add(sptException); - - return new List - { - // most of this is to keep the Async happy - - Instruction.Create(OpCodes.Ldarg_0), - Instruction.Create(OpCodes.Ldfld, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[2]), - Instruction.Create(OpCodes.Stloc_0), - - // this.Succeed = true; - Instruction.Create(OpCodes.Ldloc_0), - Instruction.Create(OpCodes.Ldc_I4_1), - Instruction.Create(OpCodes.Call, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).Methods.First(x => x.Name == "set_Succeed")), - - Instruction.Create(OpCodes.Stloc_1), - Instruction.Create(OpCodes.Ldarg_0), - Instruction.Create(OpCodes.Ldc_I4_S, (sbyte)-2), - Instruction.Create(OpCodes.Stfld, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[0]), - Instruction.Create(OpCodes.Ldarg_0), - Instruction.Create(OpCodes.Ldflda, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]), - Instruction.Create(OpCodes.Ldloc_1), - Instruction.Create(OpCodes.Call, - importer.Import(assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1].FieldType.ScopeType.ResolveTypeDef().Methods.First(x => x.Name == "SetException"))), - - Instruction.Create(OpCodes.Ldarg_0), - Instruction.Create(OpCodes.Ldc_I4_S, (sbyte)-2), - Instruction.Create(OpCodes.Stfld, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[0]), - - Instruction.Create(OpCodes.Ldarg_0), - Instruction.Create(OpCodes.Ldflda, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]), - Instruction.Create(OpCodes.Call, importer.Import(assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1].FieldType.ScopeType.ResolveTypeDef().Methods.First(x => x.Name == "SetResult"))), - - Instruction.Create(OpCodes.Ret), - }; - } - - /// - /// Returns a List of instructions to be added to the method. - /// This is an Async method so there is two parts, this part and a RunValidation method. - /// - /// AssemblyDefinition - /// MethodDefinition - /// List - public static List GetEnsureConsistencyInstructions(ModuleDefMD assembly, ModuleDefMD fileChecker, MethodDef method) - { - var importer = new Importer(assembly); - var test = ModuleDefMD.Load("C:\\Battlestate Games\\Escape from Tarkov\\EscapeFromTarkov_Data\\Managed\\mscorlib.dll"); - - // init local vars - // var1 index0 TimeSpan type - var sptTimeSpanType = importer.Import(test.GetTypes().First(x => x.Name == "TimeSpan")); - var sptClass = new Local(sptTimeSpanType.ToTypeSig()); - method.Body.Variables.Add(sptClass); - - // Create genericInstance of a method - var type = fileChecker.GetTypes().First(DumpyTypeHelper.GetEnsureConsistencyType).NestedTypes[0].Interfaces[0].Interface; - var typeMethod = importer.Import(test.GetTypes().First(x => x.Name == "Task").Methods.First(x => x.Name == "FromResult")); - var generac = new MethodSpecUser(typeMethod as IMethodDefOrRef, new GenericInstMethodSig(type.ToTypeSig())); - - return new List - { - // return Task.FromResult(ConsistencyController.CheckResult.Succeed(default(TimeSpan))); - Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]), - Instruction.Create(OpCodes.Initobj, sptTimeSpanType), - Instruction.Create(OpCodes.Ldloc_0), - Instruction.Create(OpCodes.Call, fileChecker.GetTypes().First(DumpyTypeHelper.GetEnsureConsistencyType).NestedTypes[0].Methods.First(x => x.Name == "Succeed")), - Instruction.Create(OpCodes.Call, generac), - Instruction.Create(OpCodes.Ret) - }; - } - - /// - /// Returns a List of instructions to be added to the method. - /// This is an Async method so there is two parts, this part and a MoveNext method. - /// - /// AssemblyDefinition - /// MethodDefinition - /// List - public static List GetRunValidationInstructions(ModuleDefMD assembly, MethodDef method) - { - var importer = new Importer(assembly); - var test = ModuleDefMD.Load("C:\\Battlestate Games\\Escape from Tarkov\\EscapeFromTarkov_Data\\Managed\\mscorlib.dll"); - - // Create genericInstance of a method - var type = assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0]; - var typeMethod = importer.Import(test.GetTypes().First(x => x.Name == "AsyncTaskMethodBuilder").Methods.First(x => x.Name == "Start")); - var generac = new MethodSpecUser(typeMethod as IMethodDefOrRef, new GenericInstMethodSig(type.ToTypeSig())); - - return new List - { - // d__.<>t__builder = AsyncTaskMethodBuilder.Create(); - Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]), - Instruction.Create(OpCodes.Call, importer.Import(assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1].FieldType.ScopeType.ResolveTypeDef().Methods.First(x => x.Name == "Create"))), - Instruction.Create(OpCodes.Stfld, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]), - - // d__.<>4__this = this; - Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]), - Instruction.Create(OpCodes.Ldarg_0), - Instruction.Create(OpCodes.Stfld, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[2]), - - // d__.<>1__state = -1; - Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]), - Instruction.Create(OpCodes.Ldc_I4_M1), - Instruction.Create(OpCodes.Stfld, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[0]), - - // d__.<>t__builder.Startd__0>(ref d__); - Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]), - Instruction.Create(OpCodes.Ldflda, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]), - Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]), - Instruction.Create(OpCodes.Call, generac), - - // return d__.<>t__builder.Task; - Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]), - Instruction.Create(OpCodes.Ldflda, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]), - Instruction.Create(OpCodes.Call, importer.Import(assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1].FieldType.ScopeType.ResolveTypeDef().Methods.First(x => x.Name == "get_Task"))), - Instruction.Create(OpCodes.Ret), - }; - } - - public static List GetDumpyTaskInstructions(ModuleDefMD assembly, MethodDef method) - { - var importer = new Importer(assembly); - - return new List - { - Instruction.Create(OpCodes.Call, importer.Import(typeof(DumpyTool).GetMethod("StartDumpyTask"))), - Instruction.Create(OpCodes.Pop) - }; - } -} \ No newline at end of file diff --git a/RecodeItLib/Dumper/DumpyTypeHelper.cs b/RecodeItLib/Dumper/DumpyReflectionHelper.cs similarity index 62% rename from RecodeItLib/Dumper/DumpyTypeHelper.cs rename to RecodeItLib/Dumper/DumpyReflectionHelper.cs index 14064d9..1226f82 100644 --- a/RecodeItLib/Dumper/DumpyTypeHelper.cs +++ b/RecodeItLib/Dumper/DumpyReflectionHelper.cs @@ -3,7 +3,7 @@ using dnlib.DotNet; namespace ReCodeItLib.Dumper; -public static class DumpyTypeHelper +public static class DumpyReflectionHelper { /// /// Gets the type that has a method called SendAndHandleRetries. @@ -51,4 +51,39 @@ public static class DumpyTypeHelper { return type.Name == "MenuScreen"; } + + public static bool GetBackRequestMethod(MethodDef method) + { + return method.Parameters.Any(p => p.Name is "backRequest") && method.Parameters.Any(p => p.Name is "bResponse"); + } + + public static bool GetValidateCertMethods(MethodDef method) + { + return method.Name == "ValidateCertificate"; + } + + public static bool GetRunValidationMethod(MethodDef method) + { + return method.Name == "RunValidation"; + } + + public static bool GetRunValidationNextMethod(MethodDef method) + { + return method.Name == "MoveNext"; + } + + public static bool GetMenuscreenMethod(MethodDef method) + { + return method.Name == "Awake"; + } + + public static bool GetEnsureConMethod(MethodDef method) + { + return method.Name == "EnsureConsistency"; + } + + public static bool GetEnsureConSingleMethod(MethodDef method) + { + return method.Name == "EnsureConsistencySingle"; + } } \ No newline at end of file