84 lines
2.5 KiB
C#
Raw Normal View History

2024-06-11 19:18:48 -04:00
using AssemblyRemapper.Models;
using Mono.Cecil;
using Newtonsoft.Json;
namespace AssemblyRemapper.Utils;
internal static class DataProvider
{
static DataProvider()
{
LoadAppSettings();
}
2024-06-12 00:05:59 -04:00
public static HashSet<RemapModel> Remaps { get; private set; } = [];
2024-06-11 23:07:59 -04:00
public static Dictionary<string, HashSet<ScoringModel>> ScoringModels { get; set; } = [];
2024-06-11 19:18:48 -04:00
public static AppSettings AppSettings { get; private set; }
public static AssemblyDefinition AssemblyDefinition { get; private set; }
public static ModuleDefinition ModuleDefinition { get; private set; }
private static void LoadAppSettings()
{
var settingsPath = Path.Combine(AppContext.BaseDirectory, "Data", "Settings.jsonc");
if (!File.Exists(settingsPath))
{
throw new InvalidOperationException($"path `{settingsPath}` does not exist...");
}
var jsonText = File.ReadAllText(settingsPath);
JsonSerializerSettings settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
AppSettings = JsonConvert.DeserializeObject<AppSettings>(jsonText, settings);
}
2024-06-12 00:05:59 -04:00
public static void LoadMappingFile()
{
if (!File.Exists(AppSettings.MappingPath))
{
throw new InvalidOperationException($"path `{AppSettings.MappingPath}` does not exist...");
}
var jsonText = File.ReadAllText(AppSettings.MappingPath);
Remaps = [];
ScoringModels = [];
Remaps = JsonConvert.DeserializeObject<HashSet<RemapModel>>(jsonText);
}
public static void LoadAssemblyDefinition()
2024-06-11 19:18:48 -04:00
{
DefaultAssemblyResolver resolver = new();
resolver.AddSearchDirectory(Path.GetDirectoryName(AppSettings.AssemblyPath)); // Replace with the correct path
ReaderParameters parameters = new() { AssemblyResolver = resolver };
AssemblyDefinition = AssemblyDefinition.ReadAssembly(AppSettings.AssemblyPath, parameters);
if (AssemblyDefinition is null)
{
throw new InvalidOperationException("AssemblyDefinition was null...");
}
var fileName = Path.GetFileName(AppSettings.AssemblyPath);
foreach (var module in AssemblyDefinition.Modules.ToArray())
{
if (module.Name == fileName)
{
ModuleDefinition = module;
2024-06-11 23:07:59 -04:00
return;
2024-06-11 19:18:48 -04:00
}
}
Logger.Log($"Module `{fileName}` not found in assembly {fileName}");
}
}