using Microsoft.VisualBasic.CompilerServices; using Newtonsoft.Json; using Newtonsoft.Json.Converters; namespace ModpackUpdater; public class ModpackInfo { private const string FILENAME_MODPACKINFO = "modpack-info.json"; [JsonConverter(typeof(VersionConverter))] public Version? Version { get; set; } public string? ConfigUrl { get; set; } public string? ExtrasKey { get; set; } public InstallOptionValueDictionary Options { get; } = []; [JsonIgnore] public string? LocalPath { get; private set; } [JsonIgnore] public bool Exists => LocalPath != null && File.Exists(GetFilePath(LocalPath)); public void Save() { if (LocalPath != null) File.WriteAllText(GetFilePath(LocalPath), JsonConvert.SerializeObject(this)); } public void Save(string mcRoot) { LocalPath = mcRoot; Save(); } public static ModpackInfo TryLoad(string? mcRoot) { if (mcRoot != null && HasModpackInfo(mcRoot)) return Load(mcRoot); return new() { LocalPath = mcRoot }; } public static ModpackInfo Load(string mcRoot) { var info = JsonConvert.DeserializeObject(File.ReadAllText(GetFilePath(mcRoot))) ?? new(); info.LocalPath = mcRoot; return info; } public static bool HasModpackInfo(string mcRoot) { return File.Exists(Conversions.ToString(GetFilePath(mcRoot))); } private static string GetFilePath(string mcRoot) { return Path.Combine(mcRoot, FILENAME_MODPACKINFO); } }