AssemblyTool/RecodeItLib/Utils/DataProvider.cs
CJ-SPT d33f1f3c9b
Dnlib refactor
* First compiling build

* fix out path

* fix hollow

* Traditional loops in favor of linq for clarity

* Start refactor

* Refactor part 2

* Rename variable

* Better error reason handling

* Clean up enum

* Refactor part 3

* Use combo boxes in favor of updowns

* Update tooltips

* fix is nested tree view display

* Capitialization

* Refactor part ??

* remove unused methods

* Expanded IsNested Check

* TypeFilter class + Fix CLI bug

* Remove reflection, change IsDerived and IsNested checks

* Remove optional out for IsPublic

* Remove nullable from IsPublic

* fix logger not resetting color

* actually fix it...

* remove redundant if else on IsPublic check

* Add logging to indicate all types have been filtered out

* Default IsPublic to true

* remove duplicate method call

* Refactor logger to be on its own thread

* Multithread remapping and grouped logging for threads

* 5 more filters

* Finish migrating to the new system

* bug fixes

* Add empty string validation to text fields

* re-enable renamer

* restore renamer

* Multi threaded renaming, still broken

* Basis for method renaming

* More renamer work, might get a passing build now?

* Re-enable publicizer

* Rework logging

* re-enable mapping updates

* fix hollow

* Iterate over all types instead of just one to re-link fields

* Add reference list command

---------

Co-authored-by: clodan <clodan@clodan.com>
2024-06-26 14:45:54 -04:00

240 lines
6.8 KiB
C#

using dnlib.DotNet;
using Newtonsoft.Json;
using ReCodeIt.Models;
using ReCodeItLib.Utils;
namespace ReCodeIt.Utils;
public static class DataProvider
{
static DataProvider()
{
if (!Directory.Exists(ReCodeItProjectsPath))
{
Directory.CreateDirectory(ReCodeItProjectsPath);
}
}
/// <summary>
/// Is this running in the CLI?
/// </summary>
public static bool IsCli { get; set; } = false;
public static string DataPath => Path.Combine(AppContext.BaseDirectory, "Data");
public static readonly string ReCodeItProjectsPath = Path.Combine(AppContext.BaseDirectory, "Projects");
public static List<RemapModel> Remaps { get; set; } = [];
public static Settings Settings { get; private set; }
public static void LoadAppSettings()
{
if (IsCli)
{
Settings = CreateFakeSettings();
return;
}
var settingsPath = Path.Combine(DataPath, "Settings.jsonc");
var jsonText = File.ReadAllText(settingsPath);
JsonSerializerSettings settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
Settings = JsonConvert.DeserializeObject<Settings>(jsonText, settings);
if (Settings is null)
{
Logger.Log("Settings were null, creating new settings", ConsoleColor.Red);
Settings = CreateFakeSettings();
SaveAppSettings();
}
Logger.Log($"Settings loaded from '{settingsPath}'");
}
public static void SaveAppSettings()
{
if (IsCli) { return; }
var settingsPath = RegistryHelper.GetRegistryValue<string>("SettingsPath");
if (!File.Exists(settingsPath))
{
Logger.Log($"path `{settingsPath}` does not exist. Could not save settings", ConsoleColor.Red);
return;
}
JsonSerializerSettings settings = new()
{
Formatting = Formatting.Indented
};
var jsonText = JsonConvert.SerializeObject(Settings, settings);
File.WriteAllText(settingsPath, jsonText);
//Logger.Log($"App settings saved to {settingsPath}");
}
public static List<RemapModel> LoadMappingFile(string path)
{
if (!File.Exists(path))
{
Logger.Log($"Error loading mapping.json from `{path}`, First time running? Please select a mapping path in the gui", ConsoleColor.Red);
return [];
}
var jsonText = File.ReadAllText(path);
var remaps = JsonConvert.DeserializeObject<List<RemapModel>>(jsonText);
if (remaps == null) { return []; }
Logger.Log($"Mapping file loaded from '{path}' containing {remaps.Count} remaps");
return remaps;
}
public static void SaveMapping()
{
JsonSerializerSettings settings = new()
{
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented
};
var path = Settings.Remapper.MappingPath;
var jsonText = JsonConvert.SerializeObject(Remaps, settings);
File.WriteAllText(path, jsonText);
Logger.Log($"Mapping File Saved To {path}");
}
public static void UpdateMapping(string path, List<RemapModel> remaps)
{
if (!File.Exists(path))
{
throw new FileNotFoundException($"path `{path}` does not exist...");
}
JsonSerializerSettings settings = new()
{
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented
};
var jsonText = JsonConvert.SerializeObject(Remaps, settings);
File.WriteAllText(path, jsonText);
Logger.Log($"Mapping file updated with new type names and saved to {path}", ConsoleColor.Yellow);
}
public static ModuleDefMD LoadModule(string path)
{
var mcOptions = new ModuleCreationOptions(ModuleDef.CreateModuleContext());
ModuleDefMD module = ModuleDefMD.Load(path, mcOptions);
module.Context = mcOptions.Context;
if (module is null)
{
throw new NullReferenceException("Module is null...");
}
return module;
}
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;
}
}