AssemblyTool/RecodeItLib/Utils/DataProvider.cs

205 lines
5.9 KiB
C#
Raw Normal View History

2024-06-15 16:21:12 -04:00
using Mono.Cecil;
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()
{
if (!Directory.Exists(ReCodeItProjectsPath))
2024-06-18 17:35:31 -04:00
{
Directory.CreateDirectory(ReCodeItProjectsPath);
2024-06-18 17:35:31 -04:00
}
2024-06-11 19:18:48 -04:00
}
2024-06-18 17:35:31 -04:00
public static readonly string DataPath = Path.Combine(AppContext.BaseDirectory, "Data");
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-11 23:07:59 -04:00
public static Dictionary<string, HashSet<ScoringModel>> ScoringModels { get; set; } = [];
2024-06-13 17:55:06 -04:00
public static Settings Settings { get; private set; }
2024-06-11 19:18:48 -04:00
public static AssemblyDefinition AssemblyDefinition { get; private set; }
2024-06-16 19:40:25 -04:00
public static AssemblyDefinition NameMangledAssemblyDefinition { get; private set; }
2024-06-11 19:18:48 -04:00
public static ModuleDefinition ModuleDefinition { get; private set; }
2024-06-16 19:40:25 -04:00
public static ModuleDefinition NameMangledModuleDefinition { get; private set; }
2024-06-13 20:25:11 -04:00
public static void LoadAppSettings()
2024-06-11 19:18:48 -04:00
{
2024-06-18 17:35:31 -04:00
var settingsPath = Path.Combine(DataPath, "Settings.jsonc");
2024-06-11 19:18:48 -04:00
if (!File.Exists(settingsPath))
{
2024-06-14 18:07:01 -04:00
throw new FileNotFoundException($"path `{settingsPath}` does not exist...");
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()
{
var settingsPath = Path.Combine(AppContext.BaseDirectory, "Data", "Settings.jsonc");
if (!File.Exists(settingsPath))
{
2024-06-18 17:35:31 -04:00
Logger.Log($"path `{settingsPath}` does not exist...", ConsoleColor.Red);
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-17 17:29:26 -04:00
Logger.Log($"Error loading mapping.json from `{path}`, First time running? Please select a mapping path");
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
ScoringModels = [];
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-12 22:31:41 -04:00
var properties = typeof(SearchParams).GetProperties();
foreach (var remap in Remaps)
{
foreach (var property in properties)
{
if (property.PropertyType == typeof(List<string>) && property.GetValue(remap.SearchParams) is null)
{
property.SetValue(remap.SearchParams, new List<string>());
}
}
}
2024-06-13 20:25:11 -04:00
2024-06-17 17:29:26 -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-17 17:29:26 -04:00
public static void UpdateMapping(string path)
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-06-12 22:31:41 -04:00
var properties = typeof(SearchParams).GetProperties();
foreach (var remap in Remaps)
{
foreach (var property in properties)
{
if (property.PropertyType == typeof(List<string>))
{
var val = property.GetValue(remap.SearchParams);
if (val is List<string> list && list.Count > 0) { continue; }
property.SetValue(remap.SearchParams, null);
}
}
}
2024-06-12 20:40:10 -04:00
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-17 17:29:26 -04:00
Logger.Log($"Mapping file saved to {path}");
2024-06-12 20:40:10 -04:00
}
2024-06-17 17:29:26 -04:00
public static void LoadAssemblyDefinition(string path)
2024-06-11 19:18:48 -04:00
{
2024-06-14 14:18:17 -04:00
AssemblyDefinition = null;
ModuleDefinition = null;
2024-06-11 19:18:48 -04:00
DefaultAssemblyResolver resolver = new();
2024-06-16 19:40:25 -04:00
resolver.AddSearchDirectory(Path.GetDirectoryName(path)); // Replace with the correct path : (6/14) I have no idea what I met by that
2024-06-11 19:18:48 -04:00
ReaderParameters parameters = new() { AssemblyResolver = resolver };
2024-06-16 19:40:25 -04:00
var assemblyDefinition = AssemblyDefinition.ReadAssembly(
path,
parameters);
2024-06-11 19:18:48 -04:00
2024-06-16 19:40:25 -04:00
if (assemblyDefinition is null)
2024-06-11 19:18:48 -04:00
{
2024-06-14 13:47:30 -04:00
throw new NullReferenceException("AssemblyDefinition was null...");
2024-06-11 19:18:48 -04:00
}
2024-06-17 17:29:26 -04:00
var fileName = Path.GetFileName(path);
2024-06-11 19:18:48 -04:00
2024-06-16 19:40:25 -04:00
foreach (var module in assemblyDefinition.Modules.ToArray())
2024-06-11 19:18:48 -04:00
{
if (module.Name == fileName)
{
2024-06-17 17:29:26 -04:00
Logger.Log($"Module definition {module.Name} found");
2024-06-16 19:40:25 -04:00
AssemblyDefinition = assemblyDefinition;
2024-06-11 19:18:48 -04:00
ModuleDefinition = module;
2024-06-17 17:29:26 -04:00
return;
2024-06-11 19:18:48 -04:00
}
}
2024-06-17 17:29:26 -04:00
Logger.Log($"Module {fileName} not found in assembly {fileName}");
2024-06-11 19:18:48 -04:00
}
2024-06-16 03:43:00 -04:00
2024-06-19 04:10:46 -04:00
public static string WriteAssemblyDefinition(string path)
2024-06-16 03:43:00 -04:00
{
2024-06-19 04:10:46 -04:00
AssemblyDefinition.Write(path);
2024-06-16 03:43:00 -04:00
2024-06-19 04:10:46 -04:00
return path;
2024-06-16 03:43:00 -04:00
}
2024-06-11 19:18:48 -04:00
}