using System.Text.Json; using System.Text.Json.Serialization; namespace XFEEDToolBox.Core.Models; public sealed record ToolboxSettings { public string JournalDirectory { get; init; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Saved Games", "Frontier Developments", "Elite Dangerous"); public string EddnEndpoint { get; init; } = "tcp://eddn.edcd.io:9500"; public bool ConnectEddnOnStart { get; init; } = true; public double MarketCacheRadiusLy {get;init;}=500; public string MarketCacheCenterName {get;init;}=""; public Coordinates? MarketCacheCenterPosition {get;init;} public XFEEDToolBox.Core.Session.HotkeySettings Hotkeys { get; init; } = new(); public int HotkeyDefaultsVersion { get; init; } = 1; public bool ShieldZeroRestartEnabled {get;init;} public string PluginGameExecutable { get; init; } = ""; public string TranslationGameExecutable { get; init; } = ""; public string TranslationSourceDirectory { get; init; } = ""; public string ProfileDisplayName { get; init; } = ""; public string ProfileAvatarPath { get; init; } = ""; public string NavigationDestination { get; init; } = ""; public double NavigationFuelReserve { get; init; } = .5; public XFEEDToolBox.Core.Navigation.GameRoutePlotOptions NavigationRouteOptions {get;init;}=new(); public PlannerRequest Planner { get; init; } = new(); public static JsonSerializerOptions JsonOptions { get; } = new(JsonSerializerDefaults.Web) { WriteIndented = true, Converters = { new JsonStringEnumConverter() } }; public static ToolboxSettings Load(string path) { if (!File.Exists(path)) return new(); var text=File.ReadAllText(path); var settings=JsonSerializer.Deserialize(text, JsonOptions) ?? new(); using var doc=JsonDocument.Parse(text); if(!doc.RootElement.TryGetProperty("marketCacheRadiusLy",out _)) { // The former 150 ly value was the shipped default. Preserve an // explicitly customised older planner radius when introducing the // shared cache/planning scope, but migrate the old default to 500. var oldRadius=settings.Planner.SearchRadiusLy; var radius=oldRadius==150?500:oldRadius; if(!double.IsFinite(radius)||radius<=0)radius=500; settings=settings with{MarketCacheRadiusLy=radius,Planner=settings.Planner with{SearchRadiusLy=radius}}; } if(!doc.RootElement.TryGetProperty("hotkeyDefaultsVersion",out _)&&!settings.Hotkeys.Enabled&&settings.Hotkeys.Relog.Length==0&&settings.Hotkeys.Restart.Length==0) settings=settings with{Hotkeys=new()}; return settings; } public void Save(string path) { if(!double.IsFinite(MarketCacheRadiusLy)||MarketCacheRadiusLy<=0)throw new ArgumentException("行情范围半径必须为有效的正数。"); if(MarketCacheCenterPosition is{IsValid:false})throw new ArgumentException("行情中心星系坐标无效。"); Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(path))!); var temp = path + ".tmp"; File.WriteAllText(temp, JsonSerializer.Serialize(this, JsonOptions)); File.Move(temp, path, true); } }