2025-01-11 02:30:18 -05:00
|
|
|
|
using System.Text.Json;
|
2025-01-11 05:34:45 -05:00
|
|
|
|
using System.Text.Json.Serialization;
|
2025-01-11 02:30:18 -05:00
|
|
|
|
using dnlib.DotNet;
|
2025-01-11 13:11:07 -05:00
|
|
|
|
using Newtonsoft.Json;
|
2024-12-31 13:46:44 -05:00
|
|
|
|
using ReCodeItLib.Models;
|
2025-01-11 13:11:07 -05:00
|
|
|
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
2024-06-11 19:18:48 -04:00
|
|
|
|
|
2024-12-31 13:46:44 -05:00
|
|
|
|
namespace ReCodeItLib.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-11-05 20:19:35 -05:00
|
|
|
|
static DataProvider()
|
|
|
|
|
{
|
2025-01-08 22:08:00 -05:00
|
|
|
|
Settings = LoadAppSettings();
|
|
|
|
|
ItemTemplates = LoadItems();
|
2024-11-05 20:19:35 -05:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-08 22:08:00 -05:00
|
|
|
|
public static Settings Settings { get; }
|
2024-11-05 20:19:35 -05:00
|
|
|
|
|
2025-01-08 22:08:00 -05:00
|
|
|
|
public static Dictionary<string, ItemTemplateModel> ItemTemplates { get; private set; }
|
|
|
|
|
|
|
|
|
|
private static readonly string DataPath = Path.Combine(AppContext.BaseDirectory, "Data");
|
2025-01-01 01:34:11 -05: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);
|
2025-01-11 02:30:18 -05:00
|
|
|
|
|
2025-01-11 05:34:45 -05:00
|
|
|
|
JsonSerializerOptions settings = new()
|
|
|
|
|
{
|
|
|
|
|
AllowTrailingCommas = true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var remaps = JsonSerializer.Deserialize<List<RemapModel>>(jsonText, settings);
|
2025-01-01 01:34:11 -05:00
|
|
|
|
|
2025-01-01 02:13:19 -05:00
|
|
|
|
return remaps ?? [];
|
2025-01-01 01:34:11 -05:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-10 06:35:51 -05:00
|
|
|
|
public static void UpdateMapping(string path, List<RemapModel> remaps, bool ignoreNull = true)
|
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-08-29 19:35:27 -04:00
|
|
|
|
File.Create(path).Close();
|
2024-06-12 20:40:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-11 02:30:18 -05:00
|
|
|
|
JsonSerializerOptions settings = new()
|
2024-06-12 20:40:10 -04:00
|
|
|
|
{
|
2025-01-11 02:30:18 -05:00
|
|
|
|
WriteIndented = true,
|
2025-01-11 05:34:45 -05:00
|
|
|
|
RespectNullableAnnotations = ignoreNull,
|
2024-06-12 20:40:10 -04:00
|
|
|
|
};
|
|
|
|
|
|
2025-01-11 02:30:18 -05:00
|
|
|
|
var jsonText = JsonSerializer.Serialize(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
|
|
|
|
|
2025-01-01 02:13:19 -05:00
|
|
|
|
Logger.Log($"Mapping file updated with new type names and saved to {path}", ConsoleColor.Green);
|
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());
|
2024-11-05 20:19:35 -05:00
|
|
|
|
var 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-11-05 20:19:35 -05:00
|
|
|
|
|
2025-01-08 22:08:00 -05:00
|
|
|
|
private static Settings LoadAppSettings()
|
|
|
|
|
{
|
|
|
|
|
var settingsPath = Path.Combine(DataPath, "Settings.jsonc");
|
|
|
|
|
var jsonText = File.ReadAllText(settingsPath);
|
|
|
|
|
|
2025-01-11 05:34:45 -05:00
|
|
|
|
JsonSerializerOptions settings = new()
|
|
|
|
|
{
|
|
|
|
|
AllowTrailingCommas = true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return JsonSerializer.Deserialize<Settings>(jsonText, settings)!;
|
2025-01-08 22:08:00 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Dictionary<string, ItemTemplateModel> LoadItems()
|
2024-11-05 20:19:35 -05:00
|
|
|
|
{
|
|
|
|
|
var itemsPath = Path.Combine(DataPath, "items.json");
|
|
|
|
|
var jsonText = File.ReadAllText(itemsPath);
|
|
|
|
|
|
2025-01-11 13:11:07 -05:00
|
|
|
|
JsonSerializerOptions settings = new()
|
|
|
|
|
{
|
|
|
|
|
RespectNullableAnnotations = true,
|
|
|
|
|
PropertyNameCaseInsensitive = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return JsonSerializer.Deserialize<Dictionary<string, ItemTemplateModel>>(jsonText, settings)!;
|
2024-11-05 20:19:35 -05:00
|
|
|
|
}
|
2024-06-11 19:18:48 -04:00
|
|
|
|
}
|