0
0
mirror of https://github.com/sp-tarkov/assembly-tool.git synced 2025-02-13 04:50:45 -05:00
assembly-tool/RecodeItLib/Utils/DataProvider.cs

158 lines
4.2 KiB
C#
Raw Normal View History

using dnlib.DotNet;
2024-06-11 19:18:48 -04:00
using Newtonsoft.Json;
2024-12-31 13:46:44 -05:00
using ReCodeItLib.Models;
2024-08-10 13:37:55 +01:00
using ReCodeItLib.Dumper;
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
{
static DataProvider()
{
LoadItems();
}
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-19 15:57:47 -04:00
public static List<RemapModel> Remaps { get; set; } = [];
public static Dictionary<string, ItemTemplateModel>? ItemTemplates { get; private set; }
2024-12-31 03:13:29 -05: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-06-11 19:18:48 -04:00
}
2025-01-01 01:34:11 -05:00
2024-06-14 18:07:01 -04:00
public static void SaveAppSettings()
{
2024-06-22 14:40:04 -04:00
if (IsCli) { return; }
var settingsPath = Path.Combine(DataPath, "Settings.jsonc");
2024-06-14 18:07:01 -04:00
if (!File.Exists(settingsPath))
{
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
{
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);
2025-01-01 01:34:11 -05:00
if (remaps is null) return [];
MigrateMappings(remaps);
return remaps;
}
private static void MigrateMappings(List<RemapModel> models)
{
foreach (var model in models)
{
MigrateMapping(model);
}
2024-12-31 04:48:12 -05:00
2025-01-01 01:34:11 -05:00
UpdateMapping(Path.Combine(DataPath, "Mappings-migrated.jsonc"), models);
2024-06-12 00:05:59 -04:00
}
2025-01-01 01:34:11 -05:00
private static void MigrateMapping(RemapModel model)
{
var searchParams = model.SearchParams;
}
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-12-31 03:13:29 -05:00
var path = Settings?.Remapper?.MappingPath;
2024-06-18 17:35:31 -04:00
2024-06-14 13:47:30 -04:00
var jsonText = JsonConvert.SerializeObject(Remaps, settings);
2024-12-31 03:13:29 -05: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
}
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
{
File.Create(path).Close();
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-07-04 10:32:01 -04:00
var jsonText = JsonConvert.SerializeObject(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
Logger.Log($"Mapping file updated with new type names and saved to {path}", ConsoleColor.Yellow);
2024-06-12 20:40:10 -04:00
}
public static ModuleDefMD LoadModule(string path)
2024-06-11 19:18:48 -04:00
{
var mcOptions = new ModuleCreationOptions(ModuleDef.CreateModuleContext());
var module = ModuleDefMD.Load(path, mcOptions);
2024-06-19 22:33:08 -04:00
module.Context = mcOptions.Context;
2024-06-11 19:18:48 -04:00
if (module is null)
2024-06-11 19:18:48 -04:00
{
throw new NullReferenceException("Module is null...");
2024-06-11 19:18:48 -04:00
}
return module;
2024-06-16 03:43:00 -04:00
}
private static void LoadItems()
{
var itemsPath = Path.Combine(DataPath, "items.json");
var jsonText = File.ReadAllText(itemsPath);
ItemTemplates = JsonConvert.DeserializeObject<Dictionary<string, ItemTemplateModel>>(jsonText);
}
2024-06-11 19:18:48 -04:00
}