9bb4f85802
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
92 lines
3.0 KiB
C#
92 lines
3.0 KiB
C#
using System.Text.Json;
|
|
using CVRManager.Models;
|
|
|
|
namespace CVRManager.Services;
|
|
|
|
public static class ConfigStore
|
|
{
|
|
static readonly JsonSerializerOptions Options = new() { WriteIndented = true };
|
|
|
|
public static AppConfig Load()
|
|
{
|
|
AppConfig cfg;
|
|
try
|
|
{
|
|
cfg = File.Exists(Paths.ConfigFile)
|
|
? JsonSerializer.Deserialize<AppConfig>(File.ReadAllText(Paths.ConfigFile), Options) ?? new AppConfig()
|
|
: new AppConfig();
|
|
}
|
|
catch
|
|
{
|
|
File.Copy(Paths.ConfigFile, Paths.ConfigFile + ".broken", true);
|
|
cfg = new AppConfig();
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(cfg.LibraryRoot)) cfg.LibraryRoot = Paths.Root;
|
|
Paths.Root = cfg.LibraryRoot;
|
|
|
|
if (cfg.Mods.All(m => m.Id != ModDef.MelonLoaderId))
|
|
cfg.Mods.Insert(0, new ModDef
|
|
{
|
|
Id = ModDef.MelonLoaderId,
|
|
Name = "MelonLoader",
|
|
Repo = "LavaGang/MelonLoader",
|
|
Type = "Root",
|
|
AssetFilter = "MelonLoader.x64.zip",
|
|
});
|
|
EnsureSources(cfg);
|
|
return cfg;
|
|
}
|
|
|
|
static readonly (string Name, string Repo)[] DefaultRepos =
|
|
{
|
|
("NAK CVR Mods", "NotAKidoS/NAK_CVR_Mods"),
|
|
("Kafe CVR Mods", "kafeijao/Kafe_CVR_Mods"),
|
|
("SDraw mods", "SDraw/ml_mods_cvr"),
|
|
("Nirv mods", "Nirv-git/CVRMods-Nirv"),
|
|
("Daky mods", "dakyneko/DakyModsCVR"),
|
|
("ddakebono mods", "ddakebono/Chillout-VR-Mods"),
|
|
};
|
|
|
|
static void EnsureSources(AppConfig cfg)
|
|
{
|
|
if (cfg.Sources.All(s => s.Id != SourceDef.CvrmgId))
|
|
cfg.Sources.Insert(0, new SourceDef { Id = SourceDef.CvrmgId, Name = "CVRMG (approved mods)", Kind = "cvrmg", Builtin = true });
|
|
foreach (var (name, repo) in DefaultRepos)
|
|
if (cfg.Sources.All(s => !s.Repo.Equals(repo, StringComparison.OrdinalIgnoreCase)) && !cfg.SeededSources.Contains(repo))
|
|
{
|
|
cfg.Sources.Add(new SourceDef { Id = repo.Replace('/', '_'), Name = name, Kind = "github", Repo = repo, Builtin = true });
|
|
cfg.SeededSources.Add(repo);
|
|
}
|
|
}
|
|
|
|
public static List<BranchInfo> LoadBranches()
|
|
{
|
|
try
|
|
{
|
|
return File.Exists(Paths.BranchCache)
|
|
? JsonSerializer.Deserialize<List<BranchInfo>>(File.ReadAllText(Paths.BranchCache), Options) ?? new()
|
|
: new();
|
|
}
|
|
catch { return new(); }
|
|
}
|
|
|
|
public static void SaveBranches(IEnumerable<BranchInfo> branches)
|
|
{
|
|
try
|
|
{
|
|
Directory.CreateDirectory(Path.GetDirectoryName(Paths.BranchCache)!);
|
|
File.WriteAllText(Paths.BranchCache, JsonSerializer.Serialize(branches, Options));
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
public static void Save(AppConfig cfg)
|
|
{
|
|
Directory.CreateDirectory(Paths.ConfigDir);
|
|
var tmp = Paths.ConfigFile + ".tmp";
|
|
File.WriteAllText(tmp, JsonSerializer.Serialize(cfg, Options));
|
|
File.Move(tmp, Paths.ConfigFile, true);
|
|
}
|
|
}
|