From 1b780b33308f3a28aa91b003e409aa0fb1e6e483 Mon Sep 17 00:00:00 2001 From: CWX Date: Sat, 10 Aug 2024 13:37:55 +0100 Subject: [PATCH] attempt at fixing imports --- RecodeItLib/Dumper/DumperClass.cs | 7 +++-- RecodeItLib/Dumper/DumpyInstructionsHelper.cs | 9 ++++-- RecodeItLib/Dumper/TestAssResolver.cs | 27 +++++++++++++++++ RecodeItLib/Utils/DataProvider.cs | 29 +++++++++++++++++++ 4 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 RecodeItLib/Dumper/TestAssResolver.cs diff --git a/RecodeItLib/Dumper/DumperClass.cs b/RecodeItLib/Dumper/DumperClass.cs index 4bb46ee..9cc30f6 100644 --- a/RecodeItLib/Dumper/DumperClass.cs +++ b/RecodeItLib/Dumper/DumperClass.cs @@ -1,4 +1,5 @@ using System.Collections; +using System.Runtime.CompilerServices; using dnlib.DotNet; using dnlib.DotNet.Emit; using ReCodeIt.Utils; @@ -32,8 +33,9 @@ public class DumperClass } // will explode if they are not there? - _gameModule = DataProvider.LoadModule(_assemblyPath); - _checkerModule = DataProvider.LoadModule(_fileCheckerPath); + // TODO: [CWX] TRIED OVERRDING + _gameModule = DataProvider.LoadModule(_assemblyPath, _managedPath); + _checkerModule = DataProvider.LoadModule(_fileCheckerPath, _managedPath); _gameTypes = _gameModule.GetTypes().ToList(); _checkerTypes = _checkerModule.GetTypes().ToList(); } @@ -72,6 +74,7 @@ public class DumperClass SetDumpyTaskCode(dumpyTaskType[0]); // TODO: Write game assembly to file + _gameModule.Write(Path.Combine(_managedPath, "Assembly-CSharp-dumper.dll")); // get types diff --git a/RecodeItLib/Dumper/DumpyInstructionsHelper.cs b/RecodeItLib/Dumper/DumpyInstructionsHelper.cs index c398255..7c65786 100644 --- a/RecodeItLib/Dumper/DumpyInstructionsHelper.cs +++ b/RecodeItLib/Dumper/DumpyInstructionsHelper.cs @@ -39,7 +39,8 @@ public static class DumpyInstructionsHelper /// List public static List GetRunValidationInstructionsMoveNext(ModuleDefMD assembly, MethodDef method) { - var importer = new Importer(assembly); + // TODO: [CWX] TRIED CHANGING OPTIONS + var importer = new Importer(assembly, ImporterOptions.TryToUseExistingAssemblyRefs); // Add our own local variables @@ -49,8 +50,10 @@ public static class DumpyInstructionsHelper method.Body.Variables.Add(sptClass); // var2 index1 ExceptionType - var sptExceptionType = importer.Import(typeof(Exception)); - var sptException = new Local(sptExceptionType.ToTypeSig()); + // 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 = typeof(System.Exception); + var sptExceptionType = importer.Import(typer); + var sptException = new Local(sptExceptionType.ToTypeSig(true)); method.Body.Variables.Add(sptException); return new List diff --git a/RecodeItLib/Dumper/TestAssResolver.cs b/RecodeItLib/Dumper/TestAssResolver.cs new file mode 100644 index 0000000..a8e228a --- /dev/null +++ b/RecodeItLib/Dumper/TestAssResolver.cs @@ -0,0 +1,27 @@ +using dnlib.DotNet; + +namespace ReCodeItLib.Dumper; + +public class TestAssResolver : AssemblyResolver +{ + // TODO: [CWX] tried overriding a few things, even passing back all assemblies from managed folder + public TestAssResolver(string path, ModuleContext context = null) : base(context) + { + ManagedPath = path; + } + + public string? ManagedPath { get; set; } + + protected override IEnumerable PreFindAssemblies(IAssembly assembly, ModuleDef sourceModule, bool matchExactly) + { + // get all files in dir + // return them as list of strings + Console.WriteLine("FUCKING HELL"); + + var array = Directory.GetFiles(ManagedPath, "*.dll"); + var array2 = base.PreFindAssemblies(assembly, sourceModule, matchExactly).ToArray(); + Array.Copy(array2, array, array2.Length); + + return array; + } +} \ No newline at end of file diff --git a/RecodeItLib/Utils/DataProvider.cs b/RecodeItLib/Utils/DataProvider.cs index fb24a17..8612875 100644 --- a/RecodeItLib/Utils/DataProvider.cs +++ b/RecodeItLib/Utils/DataProvider.cs @@ -1,6 +1,7 @@ using dnlib.DotNet; using Newtonsoft.Json; using ReCodeIt.Models; +using ReCodeItLib.Dumper; namespace ReCodeIt.Utils; @@ -126,4 +127,32 @@ public static class DataProvider return module; } + + public static ModuleDefMD LoadModule(string path, string dir) + { + var mcOptions = new ModuleCreationOptions(CreateModuleContext(path, dir)); + ModuleDefMD module = ModuleDefMD.Load(path, mcOptions); + + module.Context = mcOptions.Context; + + if (module is null) + { + throw new NullReferenceException("Module is null..."); + } + + return module; + } + + public static ModuleContext CreateModuleContext(string path, string dir) { + var ctx = new ModuleContext(); + var asmRe = new TestAssResolver(dir, ctx); + asmRe.EnableFrameworkRedirect = false; + asmRe.FindExactMatch = false; + var res = new Resolver(asmRe); + res.ProjectWinMDRefs = false; + ctx.AssemblyResolver = asmRe; + ctx.Resolver = res; + asmRe.DefaultModuleContext = ctx; + return ctx; + } } \ No newline at end of file