From 141fb6807acba3ece906485d4311a127f8cb3aeb Mon Sep 17 00:00:00 2001 From: Cj <161484149+CJ-SPT@users.noreply.github.com> Date: Sat, 22 Jun 2024 12:12:18 -0400 Subject: [PATCH] Command work + dont panic when settings are not found --- ReCodeItCLI/Commands/BuildCommand.cs | 4 +- ReCodeItCLI/Commands/BuildRef.cs | 2 +- ReCodeItCLI/Commands/ReMapCommand.cs | 43 ++++++++++ RecodeItGUI/GUI/Main.cs | 2 +- RecodeItLib/Remapper/ReCodeItRemapper.cs | 11 +-- RecodeItLib/Utils/DataProvider.cs | 105 +++++++++++++++++++++-- 6 files changed, 151 insertions(+), 16 deletions(-) create mode 100644 ReCodeItCLI/Commands/ReMapCommand.cs diff --git a/ReCodeItCLI/Commands/BuildCommand.cs b/ReCodeItCLI/Commands/BuildCommand.cs index 2fb1d1f..01eab8d 100644 --- a/ReCodeItCLI/Commands/BuildCommand.cs +++ b/ReCodeItCLI/Commands/BuildCommand.cs @@ -7,12 +7,12 @@ using ReCodeItLib.Utils; namespace ReCodeIt.Commands; -[Command("Build", Description = "Build your project and get a dll output for the original assembly. You dont need to provide a path if the last project you built is the one you want to target, or you are running this command from inside a directory where a project file exists.")] +[Command("Build", Description = "(Compile Time Reflection) Build your project and get a dll output for the original assembly.")] public class BuildCommand : ICommand { private ReCodeItCrossCompiler CrossCompiler { get; set; } - [CommandParameter(0, IsRequired = false, Description = "the location of your project file (ReCodeItProj.json)")] + [CommandParameter(0, IsRequired = false, Description = "the location of your project file (ReCodeItProj.json). You don't need to provide a path if the last project you built is the one you want to target, or you are running this command from inside a directory where a project file exists.")] public string ProjectJsonPath { get; init; } public async ValueTask ExecuteAsync(IConsole console) diff --git a/ReCodeItCLI/Commands/BuildRef.cs b/ReCodeItCLI/Commands/BuildRef.cs index 2895f5f..5ebaf7d 100644 --- a/ReCodeItCLI/Commands/BuildRef.cs +++ b/ReCodeItCLI/Commands/BuildRef.cs @@ -7,7 +7,7 @@ using ReCodeItLib.Utils; namespace ReCodeIt.Commands; -[Command("BuildRef", Description = "Builds or rebuilds a new reference DLL for your project")] +[Command("BuildRef", Description = "(Compile Time Reflection) Builds or rebuilds a new reference DLL for your project")] public class BuildRef : ICommand { private ReCodeItCrossCompiler CrossCompiler { get; set; } diff --git a/ReCodeItCLI/Commands/ReMapCommand.cs b/ReCodeItCLI/Commands/ReMapCommand.cs new file mode 100644 index 0000000..2a3c854 --- /dev/null +++ b/ReCodeItCLI/Commands/ReMapCommand.cs @@ -0,0 +1,43 @@ +using CliFx; +using CliFx.Attributes; +using CliFx.Infrastructure; +using ReCodeIt.ReMapper; +using ReCodeIt.Utils; + +namespace ReCodeIt.Commands; + +[Command("ReMap", Description = "Generates a re-mapped dll provided a mapping file and de-obfuscated dll")] +public class ReMapCommand : ICommand +{ + private ReCodeItRemapper _remapper { get; set; } = new(); + + [CommandParameter(0, IsRequired = true, Description = "The absolute path to your mapping.json file, supports .json and .jsonc")] + public string MappingJsonPath { get; init; } + + [CommandParameter(1, IsRequired = true, Description = "The absolute path to your de-obfuscated dll, containing all references that it needs to resolve.")] + public string AssemblyPath { get; init; } + + [CommandParameter(2, IsRequired = true, Description = "If true, the re-mapper will publicize all types, methods, and properties")] + public bool Publicize { get; init; } + + [CommandParameter(3, IsRequired = false, Description = "If true, the re-mapper will rename all changed types associated variable names to be the same as the declaring type")] + public bool? ReName { get; init; } + + public ValueTask ExecuteAsync(IConsole console) + { + DataProvider.LoadAppSettings(); + DataProvider.IsCli = true; + + var remapperSettings = DataProvider.Settings.Remapper.MappingSettings; + + remapperSettings.RenameFields = ReName ?? false; + remapperSettings.RenameProperties = ReName ?? false; + remapperSettings.Publicize = Publicize; + + var remaps = DataProvider.LoadMappingFile(MappingJsonPath); + + _remapper.InitializeRemap(remaps, AssemblyPath, Path.GetDirectoryName(AssemblyPath)); + + return default; + } +} \ No newline at end of file diff --git a/RecodeItGUI/GUI/Main.cs b/RecodeItGUI/GUI/Main.cs index ffc40fb..fba0a70 100644 --- a/RecodeItGUI/GUI/Main.cs +++ b/RecodeItGUI/GUI/Main.cs @@ -385,7 +385,7 @@ public partial class ReCodeItForm : Form Remapper.InitializeRemap( DataProvider.Remaps, AppSettings.Remapper.AssemblyPath, - AppSettings.Remapper.OutputPath); + Path.GetDirectoryName(AppSettings.Remapper.OutputPath)); ReloadRemapTreeView(DataProvider.Remaps); } diff --git a/RecodeItLib/Remapper/ReCodeItRemapper.cs b/RecodeItLib/Remapper/ReCodeItRemapper.cs index 05a2344..b8295d5 100644 --- a/RecodeItLib/Remapper/ReCodeItRemapper.cs +++ b/RecodeItLib/Remapper/ReCodeItRemapper.cs @@ -305,17 +305,14 @@ public class ReCodeItRemapper /// private void WriteAssembly() { - if (!OutPath.EndsWith(".dll")) - { - var moduleName = DataProvider.AssemblyDefinition.MainModule.Name; - moduleName = moduleName.Replace(".dll", "-Remapped.dll"); + var moduleName = DataProvider.AssemblyDefinition.MainModule.Name; + moduleName = moduleName.Replace(".dll", "-Remapped.dll"); - OutPath = Path.Combine(OutPath, moduleName); - } + OutPath = Path.Combine(OutPath, moduleName); var path = DataProvider.WriteAssemblyDefinition(OutPath); - Logger.Log("-----------------------------------------------", ConsoleColor.Green); + Logger.Log("Creating Hollow...", ConsoleColor.Yellow); Hollow(); var hollowedDir = Path.GetDirectoryName(OutPath); diff --git a/RecodeItLib/Utils/DataProvider.cs b/RecodeItLib/Utils/DataProvider.cs index 88b264a..9d8d9f6 100644 --- a/RecodeItLib/Utils/DataProvider.cs +++ b/RecodeItLib/Utils/DataProvider.cs @@ -1,4 +1,5 @@ -using Mono.Cecil; +using Microsoft.Win32; +using Mono.Cecil; using Newtonsoft.Json; using ReCodeIt.Models; using ReCodeItLib.Utils; @@ -28,7 +29,7 @@ public static class DataProvider public static Dictionary> ScoringModels { get; set; } = []; - public static Settings Settings { get; private set; } + public static Settings Settings { get; set; } public static AssemblyDefinition AssemblyDefinition { get; private set; } @@ -40,7 +41,14 @@ public static class DataProvider if (!File.Exists(settingsPath)) { - throw new FileNotFoundException($"path `{settingsPath}` does not exist..."); + Logger.Log($"Could not find settings path `{settingsPath}`, loading defaults"); + Settings = CreateFakeSettings(); + + RegistryHelper.SetRegistryValue("SettingsPath", Path.Combine(DataPath, "Settings.json"), RegistryValueKind.String); + RegistryHelper.SetRegistryValue("LogPath", Path.Combine(DataPath, "Log.log"), RegistryValueKind.String); + + SaveAppSettings(); + return; } var jsonText = File.ReadAllText(settingsPath); @@ -61,7 +69,7 @@ public static class DataProvider if (!File.Exists(settingsPath)) { - Logger.Log($"path `{settingsPath}` does not exist...", ConsoleColor.Red); + Logger.Log($"path `{settingsPath}` does not exist. Could not save settings", ConsoleColor.Red); } JsonSerializerSettings settings = new() @@ -80,7 +88,7 @@ public static class DataProvider { if (!File.Exists(path)) { - Logger.Log($"Error loading mapping.json from `{path}`, First time running? Please select a mapping path"); + Logger.Log($"Error loading mapping.json from `{path}`, First time running? Please select a mapping path in the gui", ConsoleColor.Red); } var jsonText = File.ReadAllText(path); @@ -195,4 +203,91 @@ public static class DataProvider return path; } + + private static Settings CreateFakeSettings() + { + var settings = new Settings + { + AppSettings = new AppSettings + { + Debug = false, + SilentMode = true + }, + Remapper = new RemapperSettings + { + MappingPath = string.Empty, + OutputPath = string.Empty, + UseProjectMappings = false, + MappingSettings = new MappingSettings + { + RenameFields = false, + RenameProperties = false, + Publicize = false, + Unseal = false, + } + }, + AutoMapper = new AutoMapperSettings + { + AssemblyPath = string.Empty, + OutputPath = string.Empty, + RequiredMatches = 5, + MinLengthToMatch = 7, + SearchMethods = true, + MappingSettings = new MappingSettings + { + RenameFields = false, + RenameProperties = false, + Publicize = false, + Unseal = false, + }, + TypesToIgnore = [ + "Boolean", + "List", + "Dictionary", + "Byte", + "Int16", + "Int32", + "Func", + "Action", + "Object", + "String", + "Vector2", + "Vector3", + "Vector4", + "Stream", + "HashSet", + "Double", + "IEnumerator" + ], + TokensToMatch = [ + "Class", + "GClass", + "GStruct", + "Interface", + "GInterface" + ], + PropertyFieldBlackList = [ + "Columns", + "mColumns", + "Template", + "Condition", + "Conditions", + "Counter", + "Instance", + "Command", + "_template" + ], + MethodParamaterBlackList = [ + + ], + }, + CrossCompiler = new CrossCompilerSettings + { + LastLoadedProject = string.Empty, + AutoLoadLastActiveProject = true + } + }; + + return settings; + } } \ No newline at end of file