2024-06-26 14:45:54 -04:00
|
|
|
|
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-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
|
|
|
|
{
|
|
|
|
|
static DataProvider()
|
|
|
|
|
{
|
2024-06-18 20:42:58 -04:00
|
|
|
|
if (!Directory.Exists(ReCodeItProjectsPath))
|
2024-06-18 17:35:31 -04:00
|
|
|
|
{
|
2024-06-18 20:42:58 -04:00
|
|
|
|
Directory.CreateDirectory(ReCodeItProjectsPath);
|
2024-06-18 17:35:31 -04:00
|
|
|
|
}
|
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-18 20:42:58 -04:00
|
|
|
|
public static readonly string ReCodeItProjectsPath = Path.Combine(AppContext.BaseDirectory, "Projects");
|
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-07-03 07:03:14 -04:00
|
|
|
|
|
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; }
|
|
|
|
|
|
2024-07-03 07:03:14 -04:00
|
|
|
|
var settingsPath = Path.Combine(DataPath, "Settings.jsonc");
|
2024-06-14 18:07:01 -04:00
|
|
|
|
|
|
|
|
|
if (!File.Exists(settingsPath))
|
|
|
|
|
{
|
2024-06-22 12:12:18 -04:00
|
|
|
|
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
|
|
|
|
{
|
2024-06-22 12:12:18 -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
|
|
|
|
|
2024-06-26 14:45:54 -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
|
|
|
|
}
|
|
|
|
|
|
2024-06-26 14:45:54 -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
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var jsonText = JsonConvert.SerializeObject(Remaps, settings);
|
|
|
|
|
|
2024-06-17 17:29:26 -04:00
|
|
|
|
File.WriteAllText(path, jsonText);
|
2024-06-14 13:47:30 -04:00
|
|
|
|
|
2024-06-26 14:45:54 -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
|
|
|
|
}
|
|
|
|
|
|
2024-06-26 14:45:54 -04:00
|
|
|
|
public static ModuleDefMD LoadModule(string path)
|
2024-06-11 19:18:48 -04:00
|
|
|
|
{
|
2024-06-26 14:45:54 -04:00
|
|
|
|
var mcOptions = new ModuleCreationOptions(ModuleDef.CreateModuleContext());
|
|
|
|
|
ModuleDefMD module = ModuleDefMD.Load(path, mcOptions);
|
2024-06-19 22:33:08 -04:00
|
|
|
|
|
2024-06-26 14:45:54 -04:00
|
|
|
|
module.Context = mcOptions.Context;
|
2024-06-11 19:18:48 -04:00
|
|
|
|
|
2024-06-26 14:45:54 -04:00
|
|
|
|
if (module is null)
|
2024-06-11 19:18:48 -04:00
|
|
|
|
{
|
2024-06-26 14:45:54 -04:00
|
|
|
|
throw new NullReferenceException("Module is null...");
|
2024-06-11 19:18:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-26 14:45:54 -04:00
|
|
|
|
return module;
|
2024-06-16 03:43:00 -04:00
|
|
|
|
}
|
2024-06-11 19:18:48 -04:00
|
|
|
|
}
|