Files
minecraft-modpack-updater/ModpackUpdater/ModpackInfo.cs
2024-09-06 08:45:36 +02:00

58 lines
1.4 KiB
C#

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; }
[JsonIgnore]
public string LocaLPath { get; private set; }
[JsonIgnore]
public bool Exists => File.Exists(GetFilePath(LocaLPath));
public void Save()
{
File.WriteAllText(Conversions.ToString(GetFilePath(LocaLPath)), JsonConvert.SerializeObject(this));
}
public void Save(string mcRoot)
{
LocaLPath = mcRoot;
Save();
}
public static ModpackInfo TryLoad(string mcRoot)
{
if (HasModpackInfo(mcRoot))
return Load(mcRoot);
return new()
{
LocaLPath = mcRoot
};
}
public static ModpackInfo Load(string mcRoot)
{
var info = JsonConvert.DeserializeObject<ModpackInfo>(File.ReadAllText(GetFilePath(mcRoot)));
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);
}
}