AssemblyTool/RecodeItLib/Utils/DataProvider.cs

158 lines
4.4 KiB
C#
Raw Normal View History

using dnlib.DotNet;
2024-06-11 19:18:48 -04:00
using Newtonsoft.Json;
2024-06-15 16:21:12 -04:00
using ReCodeIt.Models;
2024-08-10 13:37:55 +01:00
using ReCodeItLib.Dumper;
2024-06-11 19:18:48 -04:00
2024-06-14 19:06:21 -04:00
namespace ReCodeIt.Utils;
2024-06-11 19:18:48 -04:00
2024-06-13 18:15:52 -04:00
public static class DataProvider
2024-06-11 19:18:48 -04:00
{
2024-06-19 21:11:42 -04:00
/// <summary>
/// Is this running in the CLI?
/// </summary>
public static bool IsCli { get; set; } = false;
2024-06-23 03:30:33 -04:00
public static string DataPath => Path.Combine(AppContext.BaseDirectory, "Data");
2024-06-18 17:35:31 -04:00
2024-06-19 15:57:47 -04:00
public static List<RemapModel> Remaps { get; set; } = [];
2024-06-12 00:05:59 -04:00
2024-06-22 14:40:04 -04:00
public static Settings Settings { get; private set; }
2024-06-11 19:18:48 -04:00
2024-06-13 20:25:11 -04:00
public static void LoadAppSettings()
2024-06-11 19:18:48 -04:00
{
2024-06-22 14:40:04 -04:00
var settingsPath = Path.Combine(DataPath, "Settings.jsonc");
2024-06-11 19:18:48 -04:00
var jsonText = File.ReadAllText(settingsPath);
JsonSerializerSettings settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
2024-06-13 17:55:06 -04:00
Settings = JsonConvert.DeserializeObject<Settings>(jsonText, settings);
2024-06-13 20:25:11 -04:00
Logger.Log($"Settings loaded from '{settingsPath}'");
2024-06-11 19:18:48 -04:00
}
2024-06-14 18:07:01 -04:00
public static void SaveAppSettings()
{
2024-06-22 14:40:04 -04:00
if (IsCli) { return; }
var settingsPath = Path.Combine(DataPath, "Settings.jsonc");
2024-06-14 18:07:01 -04:00
if (!File.Exists(settingsPath))
{
Logger.Log($"path `{settingsPath}` does not exist. Could not save settings", ConsoleColor.Red);
2024-06-22 14:40:04 -04:00
return;
2024-06-14 18:07:01 -04:00
}
2024-06-15 16:21:12 -04:00
JsonSerializerSettings settings = new()
{
Formatting = Formatting.Indented
};
var jsonText = JsonConvert.SerializeObject(Settings, settings);
2024-06-14 18:07:01 -04:00
File.WriteAllText(settingsPath, jsonText);
2024-06-18 17:35:31 -04:00
2024-06-19 09:54:12 -04:00
//Logger.Log($"App settings saved to {settingsPath}");
2024-06-14 18:07:01 -04:00
}
2024-06-19 15:57:47 -04:00
public static List<RemapModel> LoadMappingFile(string path)
2024-06-12 00:05:59 -04:00
{
2024-06-17 17:29:26 -04:00
if (!File.Exists(path))
2024-06-12 00:05:59 -04:00
{
Logger.Log($"Error loading mapping.json from `{path}`, First time running? Please select a mapping path in the gui", ConsoleColor.Red);
2024-06-22 16:36:49 -04:00
return [];
2024-06-12 00:05:59 -04:00
}
2024-06-17 17:29:26 -04:00
var jsonText = File.ReadAllText(path);
2024-06-12 00:05:59 -04:00
2024-06-19 15:57:47 -04:00
var remaps = JsonConvert.DeserializeObject<List<RemapModel>>(jsonText);
2024-06-12 22:31:41 -04:00
2024-06-19 15:57:47 -04:00
if (remaps == null) { return []; }
2024-06-18 17:35:31 -04:00
Logger.Log($"Mapping file loaded from '{path}' containing {remaps.Count} remaps");
2024-06-19 15:57:47 -04:00
return remaps;
2024-06-12 00:05:59 -04:00
}
2024-06-18 17:35:31 -04:00
public static void SaveMapping()
2024-06-14 13:47:30 -04:00
{
JsonSerializerSettings settings = new()
{
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented
};
2024-06-18 17:35:31 -04:00
var path = Settings.Remapper.MappingPath;
2024-06-14 13:47:30 -04:00
var jsonText = JsonConvert.SerializeObject(Remaps, settings);
2024-06-17 17:29:26 -04:00
File.WriteAllText(path, jsonText);
2024-06-18 17:35:31 -04:00
Logger.Log($"Mapping File Saved To {path}");
2024-06-14 13:47:30 -04:00
}
public static void UpdateMapping(string path, List<RemapModel> remaps)
2024-06-12 20:40:10 -04:00
{
2024-06-17 17:29:26 -04:00
if (!File.Exists(path))
2024-06-12 20:40:10 -04:00
{
2024-06-17 17:29:26 -04:00
throw new FileNotFoundException($"path `{path}` does not exist...");
2024-06-12 20:40:10 -04:00
}
2024-06-14 13:47:30 -04:00
JsonSerializerSettings settings = new()
2024-06-12 20:40:10 -04:00
{
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented
};
2024-07-04 10:32:01 -04:00
var jsonText = JsonConvert.SerializeObject(remaps, settings);
2024-06-12 20:40:10 -04:00
2024-06-17 17:29:26 -04:00
File.WriteAllText(path, jsonText);
2024-06-14 13:47:30 -04:00
Logger.Log($"Mapping file updated with new type names and saved to {path}", ConsoleColor.Yellow);
2024-06-12 20:40:10 -04:00
}
public static ModuleDefMD LoadModule(string path)
2024-06-11 19:18:48 -04:00
{
var mcOptions = new ModuleCreationOptions(ModuleDef.CreateModuleContext());
ModuleDefMD module = ModuleDefMD.Load(path, mcOptions);
2024-06-19 22:33:08 -04:00
module.Context = mcOptions.Context;
2024-06-11 19:18:48 -04:00
if (module is null)
2024-06-11 19:18:48 -04:00
{
throw new NullReferenceException("Module is null...");
2024-06-11 19:18:48 -04:00
}
return module;
2024-06-16 03:43:00 -04:00
}
2024-08-10 13:37:55 +01:00
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;
}
2024-06-11 19:18:48 -04:00
}