initial changes to dnlib from cecil

This commit is contained in:
CWX 2024-08-10 11:00:55 +01:00
parent a21347b21d
commit c32163404c
6 changed files with 213 additions and 217 deletions

Binary file not shown.

View File

@ -17,9 +17,11 @@ public class Dumper : ICommand
DataProvider.IsCli = true; DataProvider.IsCli = true;
DataProvider.LoadAppSettings(); DataProvider.LoadAppSettings();
Logger.Log("Creating Dumper..."); Logger.Log("Creating DumperClass...");
ReCodeItLib.Dumper.Dumper.CreateDumper(ManagedDirectory); var dumper = new DumperClass(ManagedDirectory);
dumper.CreateDumpFolders();
dumper.CreateDumper();
Logger.Log("Complete", ConsoleColor.Green); Logger.Log("Complete", ConsoleColor.Green);

View File

@ -1,88 +1,114 @@
using Mono.Cecil; using System.Collections;
using Mono.Cecil.Cil; using dnlib.DotNet;
using dnlib.DotNet.Emit;
using ReCodeIt.Utils; using ReCodeIt.Utils;
namespace ReCodeItLib.Dumper; namespace ReCodeItLib.Dumper;
public class Dumper public class DumperClass
{ {
public static void CreateDumper(string managedPath) private ModuleDefMD? _gameModule { get; set; }
private ModuleDefMD? _checkerModule { get; set; }
private string _assemblyPath { get; set; }
private string _fileCheckerPath { get; set; }
private string _managedPath { get; set; }
private List<TypeDef>? _gameTypes { get; set; }
private List<TypeDef>? _checkerTypes { get; set; }
public DumperClass(string managedPath)
{ {
var resolver = new DefaultAssemblyResolver(); _managedPath = managedPath;
resolver.AddSearchDirectory(managedPath); _assemblyPath = Path.Combine(managedPath, "Assembly-Csharp-cleaned.dll"); // TODO: must be the cleaned one
_fileCheckerPath = Path.Combine(managedPath, "FilesChecker.dll");
// remove these dirs so it resolves to managed folder only if (!File.Exists(_assemblyPath))
resolver.RemoveSearchDirectory("."); {
resolver.RemoveSearchDirectory("bin"); Logger.Log($"File Assembly-CSharp-cleaned.dll does not exist at {_assemblyPath}", ConsoleColor.Red);
}
var readerParameters = new ReaderParameters { AssemblyResolver = resolver }; if (!File.Exists(_fileCheckerPath))
{
Logger.Log($"File FilesChecker.dll does not exist at {_fileCheckerPath}", ConsoleColor.Red);
}
// Assembly-CSharp section // will explode if they are not there?
var assemblyPath = Path.Combine(managedPath, "Assembly-Csharp.dll"); _gameModule = DataProvider.LoadModule(_assemblyPath);
var dumperOutputPath = Path.Combine(managedPath, "dumper"); _checkerModule = DataProvider.LoadModule(_fileCheckerPath);
var dumperBackupPath = Path.Combine(dumperOutputPath, "backup"); _gameTypes = _gameModule.GetTypes().ToList();
var dumperDataFolder = Path.Combine(dumperOutputPath, "DUMPDATA"); _checkerTypes = _checkerModule.GetTypes().ToList();
var newAssemblyPath = Path.Combine(dumperOutputPath, "Assembly-CSharp.dll"); }
Directory.CreateDirectory(dumperOutputPath);
Directory.CreateDirectory(dumperBackupPath);
Directory.CreateDirectory(dumperDataFolder);
File.Copy(Path.Combine(managedPath, "Assembly-CSharp.dll"), Path.Combine(dumperBackupPath, "Assembly-CSharp.dll"), true); public void CreateDumper()
File.Copy(Path.Combine(managedPath, "FilesChecker.dll"), Path.Combine(dumperBackupPath, "FilesChecker.dll"), true); {
File.Copy(".\\Assets\\Dumper\\DumpLib.dll", Path.Combine(dumperOutputPath, "DumpLib.dll"), true); if (_gameModule == null || _gameTypes == null)
File.Copy(".\\Assets\\Dumper\\DUMPDATA\\botReqData.json", dumperDataFolder + "\\botreqData.json", true); {
File.Copy(".\\Assets\\Dumper\\DUMPDATA\\config.json", dumperDataFolder + "\\config.json", true); Logger.Log($"_gameModule or _gameTypes in DumpyRemake was null", ConsoleColor.Red);
File.Copy(".\\Assets\\Dumper\\DUMPDATA\\raidSettings.json", dumperDataFolder + "\\raidSettings.json", true); return;
}
// loads assembly if (_checkerModule == null || _checkerTypes == null)
var oldAssembly = AssemblyDefinition.ReadAssembly(assemblyPath, readerParameters); {
Logger.Log($"_checkerModule or _checkerTypes in DumpyRemake was null", ConsoleColor.Red);
return;
}
// gets all types // get required types
var types = oldAssembly.MainModule.GetTypes().ToList(); var backRequestType = _gameTypes.Where(DumpyTypeHelper.GetBackRequestType).ToList();
var validateCertType = _gameTypes.Where(DumpyTypeHelper.GetValidateCertType).ToList();
var runValidationType = _gameTypes.Where(DumpyTypeHelper.GetRunValidationType).ToList();
var dumpyTaskType = _gameTypes.Where(DumpyTypeHelper.GetMenuscreenType).ToList();
// finds and checks for type with backRequest // check types
var backRequestType = types.Where(DumpyTypeHelper.GetBackRequestType).ToList();
CheckNullOrMulti(backRequestType, "BackRequest"); CheckNullOrMulti(backRequestType, "BackRequest");
CheckNullOrMulti(validateCertType, "ValidateCertificate");
// finds and checks for type with ValidateCertificate
var validateCertificateType = types.Where(DumpyTypeHelper.GetValidateCertificateType).ToList();
CheckNullOrMulti(validateCertificateType, "ValidateCertificate");
// finds and checks for type with RunValidation
var runValidationType = types.Where(DumpyTypeHelper.GetRunValidationType).ToList();
CheckNullOrMulti(runValidationType, "RunValidation"); CheckNullOrMulti(runValidationType, "RunValidation");
var dumpyTaskType = types.Where(DumpyTypeHelper.GetMenuScreenType).ToList();
CheckNullOrMulti(dumpyTaskType, "DumpyTask"); CheckNullOrMulti(dumpyTaskType, "DumpyTask");
// apply code changes // apply code changes
SetBackRequestCode(oldAssembly, backRequestType[0]); SetBackRequestCode(backRequestType[0]);
SetValidateCertificateCode(validateCertificateType[0]); SetValidateCertCode(validateCertType[0]);
SetRunValidationCode(oldAssembly, runValidationType[0]); SetRunValidationCode(runValidationType[0]);
SetDumpyTaskCode(oldAssembly, dumpyTaskType[0]); SetDumpyTaskCode(dumpyTaskType[0]);
// write modified assembly to file // TODO: Write game assembly to file
oldAssembly.Write(newAssemblyPath); _gameModule.Write(Path.Combine(_managedPath, "Assembly-CSharp-dumper.dll"));
// FilesChecker section // get types
var oldFilesCheckerPath = Path.Combine(managedPath, "FilesChecker.dll"); var ensureConsistencyTypes = _checkerTypes.Where(DumpyTypeHelper.GetEnsureConsistencyType).ToList();
var oldFilesChecker = AssemblyDefinition.ReadAssembly(oldFilesCheckerPath, readerParameters);
var newFilesCheckerPath = Path.Combine(dumperOutputPath, Path.GetFileName(oldFilesCheckerPath));
// gets all types // check types
types = oldFilesChecker.MainModule.GetTypes().ToList(); CheckNullOrMulti(ensureConsistencyTypes, "EnsureConsistency");
// finds and checks for type called EnsureConsistency
var ensureConsistencyType = types.Where(DumpyTypeHelper.GetEnsureConsistencyType).ToList();
CheckNullOrMulti(ensureConsistencyType, "EnsureConsistency");
// apply code changes // apply code changes
SetEnsureConsistencyCode(oldFilesChecker, ensureConsistencyType[0]); SetEnsureConsistencyCode(ensureConsistencyTypes[0]);
SetEnsureConsistencySingleCode(oldFilesChecker, ensureConsistencyType[0]); SetEnsureConsistencySingleCode(ensureConsistencyTypes[0]);
// Write modified assembly to file // TODO: Write fileChecker assembly to file
oldFilesChecker.Write(newFilesCheckerPath); _checkerModule.Write(Path.Combine(_managedPath, "FilesChecker-dumper.dll"));
}
public void CreateDumpFolders()
{
// create dumper folders
}
/// <summary>
/// Checks for null or multiple types
/// </summary>
/// <param name="types">ICollection</param>
/// <param name="name">string</param>
private void CheckNullOrMulti(ICollection types, string name = "")
{
if (types == null)
{
Logger.Log($"{name} was null");
}
if (types.Count > 1)
{
Logger.Log($"{name} count was more than 1");
}
} }
/// <summary> /// <summary>
@ -93,34 +119,31 @@ public class Dumper
/// </summary> /// </summary>
/// <param name="oldAssembly"></param> /// <param name="oldAssembly"></param>
/// <param name="type"></param> /// <param name="type"></param>
private static void SetBackRequestCode(AssemblyDefinition oldAssembly, TypeDefinition type) private void SetBackRequestCode(TypeDef type)
{ {
// find method // find method
var method = type.Methods.First(x => var method = type.Methods.First(x => x.Parameters.Any(p => p.Name is "backRequest" && x.Parameters.Any(p => p.Name == "bResponse")));
x.Parameters.Any(p => p.Name is "backRequest") && x.Parameters.Any(p => p.Name == "bResponse"));
if (method == null || method.Body.Instructions.Count != 269) if (method == null || method.Body.Instructions.Count != 269)
{ {
Logger.Log($"BackRequest Instructions count has changed from 269 to {method.Body.Instructions.Count}", ConsoleColor.Red); Logger.Log($"BackRequest Instructions count has changed from 269 to {method.Body.Instructions.Count}", ConsoleColor.Red);
} }
// where we insert the new instructions
var startOfInstructions = 252; var startOfInstructions = 252;
var liList = DumpyInstructionsHelper.GetBackRequestInstructions(_gameModule, method);
var processor = method.Body.GetILProcessor();
var liList = DumpyInstructionsHelper.GetBackRequestInstructions(oldAssembly, method);
var index = method.Body.Instructions[startOfInstructions]; var index = method.Body.Instructions[startOfInstructions];
foreach (var item in liList) foreach (var li in liList)
{ {
processor.InsertBefore(index, item); // something along these lines, this needs to be tested
method.Body.Instructions.InsertBefore(index, li);
} }
var ins = Instruction.Create(OpCodes.Brfalse_S, method.Body.Instructions[startOfInstructions]); // create instruction to jump to index 252 // create instruction
var ins = Instruction.Create(OpCodes.Brfalse_S, method.Body.Instructions[startOfInstructions]);
method.Body.Instructions[220] = ins; // instruction to jump from 202 to 252 // replace instruction at 220 with this
method.Body.Instructions[220] = ins;
} }
/// <summary> /// <summary>
@ -131,7 +154,7 @@ public class Dumper
/// </summary> /// </summary>
/// <param name="oldAssembly"></param> /// <param name="oldAssembly"></param>
/// <param name="type"></param> /// <param name="type"></param>
private static void SetValidateCertificateCode(TypeDefinition type) private void SetValidateCertCode(TypeDef type)
{ {
var methods = type.Methods.Where(x => var methods = type.Methods.Where(x =>
x.Name == "ValidateCertificate"); // should be 2 x.Name == "ValidateCertificate"); // should be 2
@ -142,10 +165,8 @@ public class Dumper
if (firstMethod?.Body.Instructions.Count != 55 || secondMethod?.Body.Instructions.Count != 14) if (firstMethod?.Body.Instructions.Count != 55 || secondMethod?.Body.Instructions.Count != 14)
{ {
var errorMessage = Logger.Log($"Instruction count has changed, method with 'certificate' as a param - before: 51, now: {firstMethod.Body.Instructions.Count}, " +
$"Instruction count has changed, method with 'certificate' as a param - before: 51, now: {firstMethod.Body.Instructions.Count}, " + $"method with 'certificateData' as a param - before: 14, now: {secondMethod.Body.Instructions.Count}", ConsoleColor.Red);
$"method with 'certificateData' as a param - before: 14, now: {secondMethod.Body.Instructions.Count}";
Logger.Log(errorMessage, ConsoleColor.Red);
} }
if (methods.Count() != 2) if (methods.Count() != 2)
@ -178,19 +199,21 @@ public class Dumper
/// </summary> /// </summary>
/// <param name="oldAssembly"></param> /// <param name="oldAssembly"></param>
/// <param name="type"></param> /// <param name="type"></param>
private static void SetRunValidationCode(AssemblyDefinition oldAssembly, TypeDefinition type) private void SetRunValidationCode(TypeDef type)
{ {
var importer = new Importer(_gameModule);
var method = type.Methods.First(x => x.Name == "RunValidation"); var method = type.Methods.First(x => x.Name == "RunValidation");
var method2 = type.NestedTypes[0].Methods.First(x => x.Name == "MoveNext"); var method2 = type.NestedTypes[0].Methods.First(x => x.Name == "MoveNext");
if (method == null || method.Body.Instructions.Count != 25) if (method == null || method.Body.Instructions.Count != 25)
{ {
Logger.Log($"RunValidation Instructions count has changed from 25 to {method.Body.Instructions.Count}", ConsoleColor.Red); Logger.Log($"RunValidation Instructions count has changed from 25 to {method.Body.Instructions.Count}");
} }
if (method2 == null || method2.Body.Instructions.Count != 171) if (method2 == null || method2.Body.Instructions.Count != 171)
{ {
Logger.Log($"RunValidation's MoveNext Instructions count has changed from 171 to {method2.Body.Instructions.Count}", ConsoleColor.Red); Logger.Log($"RunValidation's MoveNext Instructions count has changed from 171 to {method2.Body.Instructions.Count}");
} }
// Clear these from the body of each method respectively // Clear these from the body of each method respectively
@ -199,27 +222,24 @@ public class Dumper
method2.Body.Variables.Clear(); method2.Body.Variables.Clear();
method2.Body.ExceptionHandlers.Clear(); method2.Body.ExceptionHandlers.Clear();
var processor = method.Body.GetILProcessor(); var liList = DumpyInstructionsHelper.GetRunValidationInstructions(_gameModule, method);
var processor2 = method2.Body.GetILProcessor(); var liList2 = DumpyInstructionsHelper.GetRunValidationInstructionsMoveNext(_gameModule, method2);
var liList = DumpyInstructionsHelper.GetRunValidationInstructions(oldAssembly, method);
var liList2 = DumpyInstructionsHelper.GetRunValidationInstructionsMoveNext(oldAssembly, method2);
foreach (var instruction in liList) foreach (var instruction in liList)
{ {
processor.Append(instruction); method.Body.Instructions.Add(instruction);
} }
foreach (var instruction in liList2) foreach (var instruction in liList2)
{ {
processor2.Append(instruction); method2.Body.Instructions.Add(instruction);
} }
var ins = Instruction.Create(OpCodes.Leave_S, method2.Body.Instructions[14]); // Create instruction to jump to index 14 var ins = Instruction.Create(OpCodes.Leave_S, method2.Body.Instructions[14]); // Create instruction to jump to index 14
var ins1 = Instruction.Create(OpCodes.Leave_S, method2.Body.Instructions[method2.Body.Instructions.IndexOf(method2.Body.Instructions.Last())]); // Create instruction to jump to last index var ins1 = Instruction.Create(OpCodes.Leave_S, method2.Body.Instructions[method2.Body.Instructions.IndexOf(method2.Body.Instructions.Last())]); // Create instruction to jump to last index
processor2.InsertAfter(method2.Body.Instructions[5], ins); // Instruction to jump from 5 to 14 method2.Body.Instructions.InsertAfter(method2.Body.Instructions[5], ins); // Instruction to jump from 5 to 14
processor2.InsertAfter(method2.Body.Instructions[14], ins1); // Instruction to jump from 14 to last index method2.Body.Instructions.InsertAfter(method2.Body.Instructions[14], ins1); // Instruction to jump from 14 to last index
// Create exception handler with defined indexes // Create exception handler with defined indexes
var handler = new ExceptionHandler(ExceptionHandlerType.Catch) var handler = new ExceptionHandler(ExceptionHandlerType.Catch)
@ -228,13 +248,32 @@ public class Dumper
TryEnd = method2.Body.Instructions[7], TryEnd = method2.Body.Instructions[7],
HandlerStart = method2.Body.Instructions[7], HandlerStart = method2.Body.Instructions[7],
HandlerEnd = method2.Body.Instructions[16], HandlerEnd = method2.Body.Instructions[16],
CatchType = method2.Module.ImportReference(typeof(Exception)), CatchType = importer.Import(typeof(Exception)),
}; };
// Add exception handler to method body // Add exception handler to method body
method2.Body.ExceptionHandlers.Add(handler); method2.Body.ExceptionHandlers.Add(handler);
} }
private void SetDumpyTaskCode(TypeDef type)
{
var method = type.Methods.First(x => x.Name == "Awake");
if (method == null || method.Body.Instructions.Count != 62)
{
Logger.Log($"MainMenu is null or isnt 62 instructions, SOMETHING HAD CHANGED!", ConsoleColor.Red);
}
var liList = DumpyInstructionsHelper.GetDumpyTaskInstructions(_gameModule, method);
var index = method.Body.Instructions.First(x => x.OpCode == OpCodes.Ret);
foreach (var item in liList)
{
method.Body.Instructions.InsertBefore(index, item);
}
}
/// <summary> /// <summary>
/// <para>Finds the method called EnsureConsistency.</para> /// <para>Finds the method called EnsureConsistency.</para>
/// <para>if this is not the case, this needs to be checked.</para> /// <para>if this is not the case, this needs to be checked.</para>
@ -242,27 +281,25 @@ public class Dumper
/// </summary> /// </summary>
/// <param name="oldFileChecker"></param> /// <param name="oldFileChecker"></param>
/// <param name="type"></param> /// <param name="type"></param>
private static void SetEnsureConsistencyCode(AssemblyDefinition oldFileChecker, TypeDefinition type) private void SetEnsureConsistencyCode(TypeDef type)
{ {
var method = type.Methods.First(x => x.Name == "EnsureConsistency"); var method = type.Methods.First(x => x.Name == "EnsureConsistency");
if (method == null || method.Body.Instructions.Count != 152) if (method == null || method.Body.Instructions.Count != 152)
{ {
Logger.Log($"EnsureConsistency is null or Instructions count has changed from 152 to {method.Body.Instructions.Count}", ConsoleColor.Red); Logger.Log($"EnsureConsistency Instructions count has changed from 152 to {method.Body.Instructions.Count}", ConsoleColor.Red);
} }
var processor = method.Body.GetILProcessor();
// clear these from the method body // clear these from the method body
method.Body.Instructions.Clear(); method.Body.Instructions.Clear();
method.Body.Variables.Clear(); method.Body.Variables.Clear();
method.Body.ExceptionHandlers.Clear(); method.Body.ExceptionHandlers.Clear();
var liList = DumpyInstructionsHelper.GetEnsureConsistencyInstructions(oldFileChecker, method); var liList = DumpyInstructionsHelper.GetEnsureConsistencyInstructions(_gameModule, _checkerModule, method);
foreach (var li in liList) foreach (var li in liList)
{ {
processor.Append(li); method.Body.Instructions.Append(li);
} }
} }
@ -273,13 +310,13 @@ public class Dumper
/// </summary> /// </summary>
/// <param name="oldFileChecker"></param> /// <param name="oldFileChecker"></param>
/// <param name="type"></param> /// <param name="type"></param>
private static void SetEnsureConsistencySingleCode(AssemblyDefinition oldFileChecker, TypeDefinition type) private void SetEnsureConsistencySingleCode(TypeDef type)
{ {
var method = type.Methods.First(x => x.Name == "EnsureConsistencySingle"); var method = type.Methods.First(x => x.Name == "EnsureConsistencySingle");
if (method == null || method.Body.Instructions.Count != 101) if (method == null || method.Body.Instructions.Count != 101)
{ {
Logger.Log($"EnsureConsistencySingle is null or Instructions count has changed from 101 to {method.Body.Instructions.Count}", ConsoleColor.Red); Logger.Log($"EnsureConsistencySingle Instructions count has changed from 101 to {method.Body.Instructions.Count}", ConsoleColor.Red);
} }
// clear these from the method body // clear these from the method body
@ -287,52 +324,11 @@ public class Dumper
method.Body.Variables.Clear(); method.Body.Variables.Clear();
method.Body.ExceptionHandlers.Clear(); method.Body.ExceptionHandlers.Clear();
var processor = method.Body.GetILProcessor(); var liList = DumpyInstructionsHelper.GetEnsureConsistencyInstructions(_gameModule, _checkerModule, method);
var liList = DumpyInstructionsHelper.GetEnsureConsistencyInstructions(oldFileChecker, method);
foreach (var li in liList) foreach (var li in liList)
{ {
processor.Append(li); method.Body.Instructions.Append(li);
}
}
private static void SetDumpyTaskCode(AssemblyDefinition oldAssembly, TypeDefinition type)
{
var method = type.Methods.First(x => x.Name == "Awake");
if (method == null || method.Body.Instructions.Count != 62)
{
Logger.Log($"MainMenu is null or instructions have changed from 62 to {method.Body.Instructions.Count}", ConsoleColor.Red);
}
var processor = method.Body.GetILProcessor();
var liList = DumpyInstructionsHelper.GetDumpyTaskInstructions(oldAssembly, method);
var index = method.Body.Instructions.First(x => x.OpCode == OpCodes.Ret);
foreach (var item in liList)
{
processor.InsertBefore(index, item);
}
}
/// <summary>
/// Checks for null or multiple types
/// </summary>
/// <param name="types">ICollection</param>
/// <param name="name">string</param>
public static void CheckNullOrMulti(List<TypeDefinition> types, string name = "")
{
if (types == null)
{
Logger.Log($"{name} was null", ConsoleColor.Red);
}
if (types.Count > 1)
{
Logger.Log($"{name} count was more than 1", ConsoleColor.Red);
} }
} }
} }

View File

@ -1,10 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection.Metadata;
using System.Runtime.CompilerServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
using DumpLib; using DumpLib;
using Mono.Cecil;
using Mono.Cecil.Cil;
namespace ReCodeItLib.Dumper; namespace ReCodeItLib.Dumper;
@ -16,13 +18,15 @@ public static class DumpyInstructionsHelper
/// <param name="assembly">AssemblyDefinition</param> /// <param name="assembly">AssemblyDefinition</param>
/// <param name="method">MethodDefinition</param> /// <param name="method">MethodDefinition</param>
/// <returns>List<Instruction></returns> /// <returns>List<Instruction></returns>
public static List<Instruction> GetBackRequestInstructions(AssemblyDefinition assembly, MethodDefinition method) public static List<Instruction> GetBackRequestInstructions(ModuleDefMD assembly, MethodDef method)
{ {
var importer = new Importer(assembly);
return new List<Instruction> return new List<Instruction>
{ {
Instruction.Create(OpCodes.Ldarg_1), Instruction.Create(OpCodes.Ldarg_1),
Instruction.Create(OpCodes.Ldloc_S, method.Body.Variables[6]), Instruction.Create(OpCodes.Ldloc_S, method.Body.Variables[6]),
Instruction.Create(OpCodes.Call, assembly.MainModule.ImportReference(typeof(DumpLib.DumpyTool).GetMethod("LogRequestResponse", new[] { typeof(object), typeof(object) }))) Instruction.Create(OpCodes.Call, importer.Import(typeof(DumpLib.DumpyTool).GetMethod("LogRequestResponse", new[] { typeof(object), typeof(object) })))
}; };
} }
@ -33,18 +37,20 @@ public static class DumpyInstructionsHelper
/// <param name="assembly">AssemblyDefinition</param> /// <param name="assembly">AssemblyDefinition</param>
/// <param name="method">MethodDefinition</param> /// <param name="method">MethodDefinition</param>
/// <returns>List<Instruction></returns> /// <returns>List<Instruction></returns>
public static List<Instruction> GetRunValidationInstructionsMoveNext(AssemblyDefinition assembly, MethodDefinition method) public static List<Instruction> GetRunValidationInstructionsMoveNext(ModuleDefMD assembly, MethodDef method)
{ {
var importer = new Importer(assembly);
// Add our own local variables // Add our own local variables
// var1 index0 class1159Type // var1 index0 class1159Type
var sptClassType = assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType); var sptClassType = assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType);
var sptClass = new VariableDefinition(sptClassType); var sptClass = new Local(sptClassType.ToTypeSig());
method.Body.Variables.Add(sptClass); method.Body.Variables.Add(sptClass);
// var2 index1 ExceptionType // var2 index1 ExceptionType
var sptExceptionType = method.Module.ImportReference(typeof(Exception)); var sptExceptionType = importer.Import(typeof(Exception));
var sptException = new VariableDefinition(sptExceptionType); var sptException = new Local(sptExceptionType.ToTypeSig());
method.Body.Variables.Add(sptException); method.Body.Variables.Add(sptException);
return new List<Instruction> return new List<Instruction>
@ -52,31 +58,31 @@ public static class DumpyInstructionsHelper
// most of this is to keep the Async happy // most of this is to keep the Async happy
Instruction.Create(OpCodes.Ldarg_0), Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldfld, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[2]), Instruction.Create(OpCodes.Ldfld, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[2]),
Instruction.Create(OpCodes.Stloc_0), Instruction.Create(OpCodes.Stloc_0),
// this.Succeed = true; // this.Succeed = true;
Instruction.Create(OpCodes.Ldloc_0), Instruction.Create(OpCodes.Ldloc_0),
Instruction.Create(OpCodes.Ldc_I4_1), Instruction.Create(OpCodes.Ldc_I4_1),
Instruction.Create(OpCodes.Call, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).Methods.First(x => x.Name == "set_Succeed")), Instruction.Create(OpCodes.Call, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).Methods.First(x => x.Name == "set_Succeed")),
Instruction.Create(OpCodes.Stloc_1), Instruction.Create(OpCodes.Stloc_1),
Instruction.Create(OpCodes.Ldarg_0), Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldc_I4_S, (sbyte)-2), Instruction.Create(OpCodes.Ldc_I4_S, (sbyte)-2),
Instruction.Create(OpCodes.Stfld, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[0]), Instruction.Create(OpCodes.Stfld, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[0]),
Instruction.Create(OpCodes.Ldarg_0), Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldflda, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]), Instruction.Create(OpCodes.Ldflda, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]),
Instruction.Create(OpCodes.Ldloc_1), Instruction.Create(OpCodes.Ldloc_1),
Instruction.Create(OpCodes.Call, Instruction.Create(OpCodes.Call,
method.Module.ImportReference(assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1].FieldType.Resolve().Methods.First(x => x.Name == "SetException"))), 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.Ldarg_0),
Instruction.Create(OpCodes.Ldc_I4_S, (sbyte)-2), Instruction.Create(OpCodes.Ldc_I4_S, (sbyte)-2),
Instruction.Create(OpCodes.Stfld, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[0]), Instruction.Create(OpCodes.Stfld, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[0]),
Instruction.Create(OpCodes.Ldarg_0), Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldflda, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]), Instruction.Create(OpCodes.Ldflda, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]),
Instruction.Create(OpCodes.Call, method.Module.ImportReference(assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1].FieldType.Resolve().Methods.First(x => x.Name == "SetResult"))), 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), Instruction.Create(OpCodes.Ret),
}; };
@ -89,28 +95,29 @@ public static class DumpyInstructionsHelper
/// <param name="assembly">AssemblyDefinition</param> /// <param name="assembly">AssemblyDefinition</param>
/// <param name="method">MethodDefinition</param> /// <param name="method">MethodDefinition</param>
/// <returns>List<Instruction></returns> /// <returns>List<Instruction></returns>
public static List<Instruction> GetEnsureConsistencyInstructions(AssemblyDefinition oldFileChecker, MethodDefinition method) public static List<Instruction> GetEnsureConsistencyInstructions(ModuleDefMD assembly, ModuleDefMD fileChecker, MethodDef method)
{ {
var importer = new Importer(assembly);
// init local vars // init local vars
// var1 index0 TimeSpan type // var1 index0 TimeSpan type
var sptTimeSpanType = method.Module.ImportReference(typeof(TimeSpan)); var sptTimeSpanType = importer.Import(typeof(TimeSpan));
var sptClass = new VariableDefinition(sptTimeSpanType); var sptClass = new Local(sptTimeSpanType.ToTypeSig());
method.Body.Variables.Add(sptClass); method.Body.Variables.Add(sptClass);
// Create genericInstance of a method // Create genericInstance of a method
var type = oldFileChecker.MainModule.GetTypes().First(DumpyTypeHelper.GetEnsureConsistencyType).NestedTypes[0].Interfaces[0].InterfaceType; var type = fileChecker.GetTypes().First(DumpyTypeHelper.GetEnsureConsistencyType).NestedTypes[0].Interfaces[0].Interface;
var typeMethod = method.Module.ImportReference(typeof(Task).GetMethod("FromResult")); var typeMethod = importer.Import(typeof(Task).GetMethod("FromResult"));
var instanceType = new GenericInstanceMethod(typeMethod); var generac = new MethodSpecUser(typeMethod.ResolveMethodDef(), new GenericInstMethodSig(type.ToTypeSig()));
instanceType.GenericArguments.Add(type);
return new List<Instruction> return new List<Instruction>
{ {
// return Task.FromResult<ICheckResult>(ConsistencyController.CheckResult.Succeed(default(TimeSpan))); // return Task.FromResult<ICheckResult>(ConsistencyController.CheckResult.Succeed(default(TimeSpan)));
Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]), Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]),
Instruction.Create(OpCodes.Initobj, method.Module.ImportReference(typeof(TimeSpan))), Instruction.Create(OpCodes.Initobj, importer.Import(typeof(TimeSpan))),
Instruction.Create(OpCodes.Ldloc_0), Instruction.Create(OpCodes.Ldloc_0),
Instruction.Create(OpCodes.Call, oldFileChecker.MainModule.GetTypes().First(DumpyTypeHelper.GetEnsureConsistencyType).NestedTypes[0].Methods.First(x => x.Name == "Succeed")), Instruction.Create(OpCodes.Call, fileChecker.GetTypes().First(DumpyTypeHelper.GetEnsureConsistencyType).NestedTypes[0].Methods.First(x => x.Name == "Succeed")),
Instruction.Create(OpCodes.Call, instanceType), Instruction.Create(OpCodes.Call, generac),
Instruction.Create(OpCodes.Ret) Instruction.Create(OpCodes.Ret)
}; };
} }
@ -122,50 +129,53 @@ public static class DumpyInstructionsHelper
/// <param name="assembly">AssemblyDefinition</param> /// <param name="assembly">AssemblyDefinition</param>
/// <param name="method">MethodDefinition</param> /// <param name="method">MethodDefinition</param>
/// <returns>List<Instruction></returns> /// <returns>List<Instruction></returns>
public static List<Instruction> GetRunValidationInstructions(AssemblyDefinition assembly, MethodDefinition method) public static List<Instruction> GetRunValidationInstructions(ModuleDefMD assembly, MethodDef method)
{ {
var importer = new Importer(assembly);
// Create genericInstance of a method // Create genericInstance of a method
var type = assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0]; var type = assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0];
var typeMethod = method.Module.ImportReference(assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1].FieldType.Resolve().Methods.First(x => x.Name == "Start")); var typeMethod = importer.Import(typeof(AsyncTaskMethodBuilder).GetMethod("Start"));
var instanceMethod = new GenericInstanceMethod(typeMethod); var generac = new MethodSpecUser(typeMethod as IMethodDefOrRef, new GenericInstMethodSig(type.ToTypeSig()));
instanceMethod.GenericArguments.Add(type);
return new List<Instruction> return new List<Instruction>
{ {
// <RunValidation>d__.<>t__builder = AsyncTaskMethodBuilder.Create(); // <RunValidation>d__.<>t__builder = AsyncTaskMethodBuilder.Create();
Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]), Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]),
Instruction.Create(OpCodes.Call, method.Module.ImportReference(assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1].FieldType.Resolve().Methods.First(x => x.Name == "Create"))), 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.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]), Instruction.Create(OpCodes.Stfld, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]),
// <RunValidation>d__.<>4__this = this; // <RunValidation>d__.<>4__this = this;
Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]), Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]),
Instruction.Create(OpCodes.Ldarg_0), Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Stfld, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[2]), Instruction.Create(OpCodes.Stfld, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[2]),
// <RunValidation>d__.<>1__state = -1; // <RunValidation>d__.<>1__state = -1;
Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]), Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]),
Instruction.Create(OpCodes.Ldc_I4_M1), Instruction.Create(OpCodes.Ldc_I4_M1),
Instruction.Create(OpCodes.Stfld, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[0]), Instruction.Create(OpCodes.Stfld, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[0]),
// <RunValidation>d__.<>t__builder.Start<Class1159.<RunValidation>d__0>(ref <RunValidation>d__); // <RunValidation>d__.<>t__builder.Start<Class1159.<RunValidation>d__0>(ref <RunValidation>d__);
Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]), Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]),
Instruction.Create(OpCodes.Ldflda, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]), 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.Ldloca_S, method.Body.Variables[0]),
Instruction.Create(OpCodes.Call, instanceMethod), Instruction.Create(OpCodes.Call, generac),
// return <RunValidation>d__.<>t__builder.Task; // return <RunValidation>d__.<>t__builder.Task;
Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]), Instruction.Create(OpCodes.Ldloca_S, method.Body.Variables[0]),
Instruction.Create(OpCodes.Ldflda, assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]), Instruction.Create(OpCodes.Ldflda, assembly.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1]),
Instruction.Create(OpCodes.Call, method.Module.ImportReference(assembly.MainModule.GetTypes().First(DumpyTypeHelper.GetRunValidationType).NestedTypes[0].Fields[1].FieldType.Resolve().Methods.First(x => x.Name == "get_Task"))), 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), Instruction.Create(OpCodes.Ret),
}; };
} }
public static List<Instruction> GetDumpyTaskInstructions(AssemblyDefinition oldAssembly, MethodDefinition method) public static List<Instruction> GetDumpyTaskInstructions(ModuleDefMD assembly, MethodDef method)
{ {
var importer = new Importer(assembly);
return new List<Instruction> return new List<Instruction>
{ {
Instruction.Create(OpCodes.Call, oldAssembly.MainModule.ImportReference(typeof(DumpyTool).GetMethod("StartDumpyTask"))), Instruction.Create(OpCodes.Call, importer.Import(typeof(DumpyTool).GetMethod("StartDumpyTask"))),
Instruction.Create(OpCodes.Pop) Instruction.Create(OpCodes.Pop)
}; };
} }

View File

@ -1,5 +1,5 @@
using System.Linq; using System.Linq;
using Mono.Cecil; using dnlib.DotNet;
namespace ReCodeItLib.Dumper; namespace ReCodeItLib.Dumper;
@ -11,7 +11,7 @@ public static class DumpyTypeHelper
/// </summary> /// </summary>
/// <param name="type">TypeDefinition</param> /// <param name="type">TypeDefinition</param>
/// <returns>boolean</returns> /// <returns>boolean</returns>
public static bool GetBackRequestType(TypeDefinition type) public static bool GetBackRequestType(TypeDef type)
{ {
return type.Methods.Any(m => m.Name == "SendAndHandleRetries"); return type.Methods.Any(m => m.Name == "SendAndHandleRetries");
} }
@ -21,7 +21,7 @@ public static class DumpyTypeHelper
/// </summary> /// </summary>
/// <param name="type">TypeDefinition</param> /// <param name="type">TypeDefinition</param>
/// <returns>boolean</returns> /// <returns>boolean</returns>
public static bool GetValidateCertificateType(TypeDefinition type) public static bool GetValidateCertType(TypeDef type)
{ {
return type.Methods.Any(m => m.Name == "ValidateCertificate"); return type.Methods.Any(m => m.Name == "ValidateCertificate");
} }
@ -31,7 +31,7 @@ public static class DumpyTypeHelper
/// </summary> /// </summary>
/// <param name="type">TypeDefinition</param> /// <param name="type">TypeDefinition</param>
/// <returns>boolean</returns> /// <returns>boolean</returns>
public static bool GetRunValidationType(TypeDefinition type) public static bool GetRunValidationType(TypeDef type)
{ {
return type.Methods.Any(m => m.Name == "RunValidation"); return type.Methods.Any(m => m.Name == "RunValidation");
} }
@ -42,12 +42,12 @@ public static class DumpyTypeHelper
/// </summary> /// </summary>
/// <param name="type">TypeDefinition</param> /// <param name="type">TypeDefinition</param>
/// <returns>boolean</returns> /// <returns>boolean</returns>
public static bool GetEnsureConsistencyType(TypeDefinition type) public static bool GetEnsureConsistencyType(TypeDef type)
{ {
return type.Name == "ConsistencyController"; return type.Name == "ConsistencyController";
} }
public static bool GetMenuScreenType(TypeDefinition type) public static bool GetMenuscreenType(TypeDef type)
{ {
return type.Name == "MenuScreen"; return type.Name == "MenuScreen";
} }

View File

@ -178,16 +178,4 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\DumpLib\DumpLib.csproj" /> <ProjectReference Include="..\DumpLib\DumpLib.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="DumpLib">
<HintPath>Assets\DumpLib.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil">
<HintPath>..\Assets\MonoCecil\Mono.Cecil.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Rocks">
<HintPath>..\Assets\MonoCecil\Mono.Cecil.Rocks.dll</HintPath>
</Reference>
</ItemGroup>
</Project> </Project>