using System; using System.Collections.Generic; using System.IO; using System.Text.Json; namespace VibranceHud; public static class GameProfileStore { private enum ReadOutcome { Nothing, Loaded, Failed } private sealed class ProfilesDocument { public int Version { get; set; } = 1; public List Profiles { get; set; } = new List(); } private const int CurrentSchemaVersion = 1; public static string StorePath { get { string local = AppFolders.Local; try { Directory.CreateDirectory(local); } catch (Exception) { } return Path.Combine(local, "profiles.json"); } } public static IReadOnlyList Load() { return Load(StorePath); } public static IReadOnlyList Load(string path) { return Load(path, File.ReadAllText); } internal static IReadOnlyList Load(string path, Func readText) { TryRead(path, readText, out List profiles); return profiles; } private static ReadOutcome TryRead(string path, Func readText, out List profiles) { profiles = new List(); if (!File.Exists(path)) { return ReadOutcome.Nothing; } string text; try { text = readText(path); } catch (Exception) { return ReadOutcome.Failed; } if (string.IsNullOrWhiteSpace(text)) { return ReadOutcome.Nothing; } try { profiles = JsonSerializer.Deserialize(text)?.Profiles ?? new List(); return ReadOutcome.Loaded; } catch (Exception) { profiles = new List(); return ReadOutcome.Failed; } } public static string SerializeAll(IEnumerable profiles) { return JsonSerializer.Serialize(new ProfilesDocument { Version = 1, Profiles = new List(profiles) }, new JsonSerializerOptions { WriteIndented = true }); } public static void Set(GameProfile profile) { TrySet(profile, StorePath, File.ReadAllText); } public static void Set(GameProfile profile, string path) { TrySet(profile, path, File.ReadAllText); } internal static bool TrySet(GameProfile profile, string path, Func readText) { if (TryRead(path, readText, out List profiles) == ReadOutcome.Failed) { return false; } int num = profiles.FindIndex((GameProfile p) => p.GameId == profile.GameId); profile.LastUpdated = DateTime.UtcNow; if (num >= 0) { profiles[num] = profile; } else { profiles.Add(profile); } return WriteAtomically(path, SerializeAll(profiles)); } public static void Remove(string gameId) { TryRemove(gameId, StorePath, File.ReadAllText); } public static void Remove(string gameId, string path) { TryRemove(gameId, path, File.ReadAllText); } internal static bool TryRemove(string gameId, string path, Func readText) { if (TryRead(path, readText, out List profiles) == ReadOutcome.Failed) { return false; } profiles.RemoveAll((GameProfile p) => p.GameId == gameId); return WriteAtomically(path, SerializeAll(profiles)); } private static bool WriteAtomically(string path, string json) { string text = ""; try { string directoryName = Path.GetDirectoryName(path); if (!string.IsNullOrEmpty(directoryName)) { Directory.CreateDirectory(directoryName); } text = path + "." + Guid.NewGuid().ToString("N") + ".tmp"; File.WriteAllText(text, json); try { if (File.Exists(path)) { File.Replace(text, path, null, ignoreMetadataErrors: true); } else { File.Move(text, path); } } catch (Exception) when (File.Exists(text)) { File.Copy(text, path, overwrite: true); } return true; } catch (Exception) { return false; } finally { try { if (text.Length > 0 && File.Exists(text)) { File.Delete(text); } } catch { } } } public static GameProfile? Get(string gameId) { return Get(gameId, StorePath); } public static GameProfile? Get(string gameId, string path) { foreach (GameProfile item in Load(path)) { if (item.GameId == gameId) { return item; } } return null; } }