AssemblyTool/RecodeItLib/Utils/DataProvider.cs

289 lines
8.3 KiB
C#
Raw Normal View History

2024-06-22 14:40:04 -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-19 21:11:42 -04:00
using ReCodeItLib.Utils;
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-19 21:11:42 -04:00
/// <summary>
/// Is this running in the CLI?
/// </summary>
public static bool IsCli { get; set; } = false;
2024-06-20 22:13:34 -04:00
public static string DataPath => RegistryHelper.GetRegistryValue<string>("DataPath") ?? Path.Combine(AppContext.BaseDirectory, "Data");
2024-06-18 17:35:31 -04:00
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-22 14:40:04 -04:00
public static Settings Settings { get; private set; }
2024-06-11 19:18:48 -04:00
public static AssemblyDefinition AssemblyDefinition { get; private set; }
public static ModuleDefinition ModuleDefinition { 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-22 14:40:04 -04:00
if (IsCli)
2024-06-11 19:18:48 -04:00
{
Settings = CreateFakeSettings();
return;
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-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()
{
2024-06-22 14:40:04 -04:00
if (IsCli) { return; }
2024-06-19 21:11:42 -04:00
var settingsPath = RegistryHelper.GetRegistryValue<string>("SettingsPath");
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-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
2024-06-19 22:33:08 -04:00
Console.WriteLine(path);
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-20 23:30:17 -04:00
AssemblyDefinition = assemblyDefinition;
ModuleDefinition = assemblyDefinition.Modules.First();
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
}
private static Settings CreateFakeSettings()
{
var settings = new Settings
{
AppSettings = new AppSettings
{
Debug = false,
SilentMode = true
},
Remapper = new RemapperSettings
{
MappingPath = string.Empty,
OutputPath = string.Empty,
UseProjectMappings = false,
MappingSettings = new MappingSettings
{
RenameFields = false,
RenameProperties = false,
Publicize = false,
Unseal = false,
}
},
AutoMapper = new AutoMapperSettings
{
AssemblyPath = string.Empty,
OutputPath = string.Empty,
RequiredMatches = 5,
MinLengthToMatch = 7,
SearchMethods = true,
MappingSettings = new MappingSettings
{
RenameFields = false,
RenameProperties = false,
Publicize = false,
Unseal = false,
},
TypesToIgnore = [
"Boolean",
"List",
"Dictionary",
"Byte",
"Int16",
"Int32",
"Func",
"Action",
"Object",
"String",
"Vector2",
"Vector3",
"Vector4",
"Stream",
"HashSet",
"Double",
"IEnumerator"
],
TokensToMatch = [
"Class",
"GClass",
"GStruct",
"Interface",
"GInterface"
],
PropertyFieldBlackList = [
"Columns",
"mColumns",
"Template",
"Condition",
"Conditions",
"Counter",
"Instance",
"Command",
"_template"
],
MethodParamaterBlackList = [
],
},
CrossCompiler = new CrossCompilerSettings
{
LastLoadedProject = string.Empty,
AutoLoadLastActiveProject = true
}
};
return settings;
}
2024-06-11 19:18:48 -04:00
}