using System.Text.Json; namespace XFEToolBox.Client.Utilities; internal static class ToolLaunchPreferenceStore { private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web); public static bool GetRunAsAdministrator(string? json, string toolId) { if (string.IsNullOrWhiteSpace(toolId)) return false; return Read(json).TryGetValue(toolId, out var preference) && preference.RunAsAdministrator; } public static string SetRunAsAdministrator(string? json, string toolId, bool runAsAdministrator) { ArgumentException.ThrowIfNullOrWhiteSpace(toolId); var preferences = Read(json); if (runAsAdministrator) preferences[toolId] = new ToolLaunchPreference(true); else preferences.Remove(toolId); return JsonSerializer.Serialize(preferences, JsonOptions); } private static Dictionary Read(string? json) { try { if (string.IsNullOrWhiteSpace(json)) return new Dictionary(StringComparer.OrdinalIgnoreCase); var stored = JsonSerializer.Deserialize>(json, JsonOptions); return stored is null ? new Dictionary(StringComparer.OrdinalIgnoreCase) : new Dictionary(stored, StringComparer.OrdinalIgnoreCase); } catch (JsonException) { return new Dictionary(StringComparer.OrdinalIgnoreCase); } } private sealed record ToolLaunchPreference(bool RunAsAdministrator); }