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()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 13:47:30 -04:00
|
|
|
|
public static List<RemapModel> Remaps { get; private 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
|
|
|
|
{
|
|
|
|
|
var settingsPath = Path.Combine(AppContext.BaseDirectory, "Data", "Settings.jsonc");
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
|
|
|
|
throw new FileNotFoundException($"path `{settingsPath}` does not exist...");
|
|
|
|
|
}
|
|
|
|
|
|
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-14 13:47:30 -04:00
|
|
|
|
public static void LoadMappingFile(string path = "")
|
2024-06-12 00:05:59 -04:00
|
|
|
|
{
|
2024-06-14 18:07:01 -04:00
|
|
|
|
if (!File.Exists(Settings.AppSettings.MappingPath))
|
2024-06-12 00:05:59 -04:00
|
|
|
|
{
|
2024-06-14 18:07:01 -04:00
|
|
|
|
throw new InvalidOperationException($"path `{Settings.AppSettings.MappingPath}` does not exist...");
|
2024-06-12 00:05:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 13:47:30 -04:00
|
|
|
|
var fpath = path == string.Empty
|
2024-06-14 18:07:01 -04:00
|
|
|
|
? Settings.AppSettings.MappingPath
|
2024-06-14 13:47:30 -04:00
|
|
|
|
: path;
|
|
|
|
|
|
|
|
|
|
var jsonText = File.ReadAllText(fpath);
|
2024-06-12 00:05:59 -04:00
|
|
|
|
|
|
|
|
|
Remaps = [];
|
|
|
|
|
ScoringModels = [];
|
|
|
|
|
|
2024-06-14 13:47:30 -04:00
|
|
|
|
Remaps = JsonConvert.DeserializeObject<List<RemapModel>>(jsonText);
|
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-14 18:07:01 -04:00
|
|
|
|
Logger.Log($"Mapping file loaded from '{Settings.AppSettings.MappingPath}'");
|
2024-06-12 00:05:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 13:47:30 -04:00
|
|
|
|
public static void SaveMapping()
|
|
|
|
|
{
|
|
|
|
|
JsonSerializerSettings settings = new()
|
|
|
|
|
{
|
|
|
|
|
NullValueHandling = NullValueHandling.Ignore,
|
|
|
|
|
Formatting = Formatting.Indented
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var jsonText = JsonConvert.SerializeObject(Remaps, settings);
|
|
|
|
|
|
2024-06-14 18:07:01 -04:00
|
|
|
|
File.WriteAllText(Settings.AppSettings.MappingPath, jsonText);
|
2024-06-14 13:47:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 20:40:10 -04:00
|
|
|
|
public static void UpdateMapping()
|
|
|
|
|
{
|
2024-06-14 18:07:01 -04:00
|
|
|
|
if (!File.Exists(Settings.AppSettings.MappingPath))
|
2024-06-12 20:40:10 -04:00
|
|
|
|
{
|
2024-06-14 18:07:01 -04:00
|
|
|
|
throw new FileNotFoundException($"path `{Settings.AppSettings.MappingPath}` 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-14 18:07:01 -04:00
|
|
|
|
File.WriteAllText(Settings.AppSettings.MappingPath, jsonText);
|
2024-06-14 13:47:30 -04:00
|
|
|
|
|
2024-06-14 18:07:01 -04:00
|
|
|
|
Logger.Log($"Mapping file saved to {Settings.AppSettings.MappingPath}");
|
2024-06-12 20:40:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 19:40:25 -04:00
|
|
|
|
public static void LoadAssemblyDefinition(bool nameMangled = false)
|
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
|
|
|
|
|
|
|
|
|
var path = nameMangled == false ? Settings.AppSettings.AssemblyPath : Settings.AppSettings.NameMangledPath;
|
|
|
|
|
|
|
|
|
|
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-14 18:07:01 -04:00
|
|
|
|
var fileName = Path.GetFileName(Settings.AppSettings.AssemblyPath);
|
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-13 20:25:11 -04:00
|
|
|
|
Logger.Log($"Module definition {module.Name} found'");
|
2024-06-16 19:40:25 -04:00
|
|
|
|
|
|
|
|
|
if (nameMangled)
|
|
|
|
|
{
|
|
|
|
|
NameMangledAssemblyDefinition = assemblyDefinition;
|
|
|
|
|
NameMangledModuleDefinition = module;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AssemblyDefinition = assemblyDefinition;
|
2024-06-11 19:18:48 -04:00
|
|
|
|
ModuleDefinition = module;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 19:40:25 -04:00
|
|
|
|
if (nameMangled)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 19:18:48 -04:00
|
|
|
|
Logger.Log($"Module `{fileName}` not found in assembly {fileName}");
|
|
|
|
|
}
|
2024-06-16 03:43:00 -04:00
|
|
|
|
|
|
|
|
|
public static string WriteAssemblyDefinition(bool updateMapping = false)
|
|
|
|
|
{
|
|
|
|
|
var filename = Path.GetFileNameWithoutExtension(Settings.AppSettings.AssemblyPath);
|
|
|
|
|
var strippedPath = Path.GetDirectoryName(filename);
|
|
|
|
|
|
|
|
|
|
filename = $"{filename}-Remapped.dll";
|
|
|
|
|
|
|
|
|
|
var remappedPath = Path.Combine(strippedPath, filename);
|
|
|
|
|
|
|
|
|
|
AssemblyDefinition.Write(remappedPath);
|
|
|
|
|
|
|
|
|
|
if (updateMapping)
|
|
|
|
|
{
|
|
|
|
|
UpdateMapping();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return remappedPath;
|
|
|
|
|
}
|
2024-06-11 19:18:48 -04:00
|
|
|
|
}
|