Files
LanMountainDesktop/LanMountainDesktop/Services/Settings/SettingsService.cs
lincube 0b603384b4 Launcher fix (#6)
* fix.hy3试图修复中

* Resolve dev paths and fix splash UI thread

Compute a solutionRoot and expand development search paths (LanMountainDesktop and dev-test) in DeploymentLocator, add logging when scanning/finding hosts, and return distinct full paths. Ensure backward-compatible path checks. Fix cross-thread UI calls: invoke splashWindow.DismissAsync on the UI thread in LauncherFlowCoordinator, and make SplashWindow.DismissAsync ensure it runs on the UI thread before closing (simplified Close call). These changes improve development host discovery and prevent UI-thread access issues during shutdown.

* Add configurable data location (portable/system)

Introduce support for choosing and resolving the application's data root (system user dir vs. portable app folder). Adds DataLocationConfig model, DataLocationResolver (load/save/resolve/migrate), a UI prompt (DataLocationPromptWindow) and an OOBE step (DataLocationOobeStep) to let users pick and optionally migrate existing data. Wire the chosen data root into the launcher flow and host launch plan (forwarded via --data-root and LMD_DATA_ROOT), and add AppDataPathProvider to let runtime services read the effective data root (initialized in Program.Main). Update various services (logging, settings, DB, plugin/market, startup registry, etc.) to use the new provider/resolver and register the config type in the JSON context. This enables portable installs, safe migration, and runtime overrides via CLI or environment variable.

* Add dev/debug startup flow and launch profiles

Handle design-time initialization and add a developer debug startup path: App now skips normal startup when in design mode and shows a DevDebugWindow when running in debug (unless a preview or apply-update command). CommandContext.IsDebugMode is extended to include DOTNET_ENVIRONMENT=Development via a new IsDevelopmentEnvironment helper. Program.Main and BuildAvaloniaApp are made public to aid tooling. Added multiple launchSettings profiles for debug and preview commands that set DOTNET_ENVIRONMENT=Development to simplify IDE debugging and UI previewing.

* Simplify splash to fade; add themed about banners

Simplify splash startup visuals by removing the multi-mode/slide behavior and always using a fade animation. Update App to create SplashWindow without a StartupVisualMode parameter and remove related fields, layout configuration, slide animation, and easing helpers from SplashWindow. Clean up unused using. Replace the single about_banner asset with theme-aware variants (about_banner_dark.png and about_banner_light.png), delete the old about_banner.png, and update AboutSettingsPage to use a DynamicResource ImageBrush (AboutBannerBrush) that selects the appropriate banner per theme.

* Use AppJsonContext for startup state serialization

Switch serialization to the source-generated System.Text.Json context: add JsonSerializable(typeof(StartupAttemptRecord)) to AppJsonContext and replace the previous JsonSerializerOptions-based Serialize/Deserialize calls with AppJsonContext.Default.StartupAttemptRecord. Also remove the now-unused SerializerOptions field. Additionally, update .gitignore to exclude /test-aot-publish.

* Add OOBE redesign, theme & data location support

Introduce a redesigned OOBE flow and data-location/theme support across the launcher. Adds a new ThemeService for applying light/dark and accent colors; integrates FluentIcons.Avalonia package for icons. Overhauls OobeWindow (UX animations, typing effect, multi-step theme and data-location pages, Monet options, and final welcome step) and its code-behind to handle step navigation, accent selection, and data-location resolution. Adds DataLocation UI and handlers (DataLocationPromptWindow changes, DataLocation resolver usage) and wires a DevDebug UI for toggling/opening the data-location page. UpdateEngineService now resolves the launcher root via DataLocationResolver. Misc: update various view models, localization entries and remove TrimmerRoots.xml.

* Refactor data location paths and add background service

Refactor DataLocationResolver to centralize data path resolution (ResolveLauncherDataPath, ResolveDesktopDataPath, ResolveConfigPath, ResolveLauncherLogsPath, ResolveLauncherStatePath) and replace usages of the previous ".launcher" layout with a "Launcher" folder. Update API: LoadConfig/SaveConfig reorganized and ApplyLocationChoice now accepts an optional custom path and migration flag; migration logic updated accordingly. Update dependent services and views (Logger, DeploymentLocator, UpdateEngineService, OobeStateService, StartupAttemptRegistry, LauncherDebugSettingsStore, OobeWindow) to use the new resolver APIs and paths. Add LauncherBackgroundService to load/validate/cache a custom splash background image and wire it into SplashWindow (AXAML/Axaml.cs) with UI placeholders and overlay. Misc: minor cleanup of Oobe/Splash XAML and related code adjustments and logging improvements.
2026-04-25 18:41:26 +08:00

428 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using LanMountainDesktop.Models;
using LanMountainDesktop.PluginSdk;
namespace LanMountainDesktop.Services.Settings;
internal sealed class SettingsService : ISettingsService
{
private static readonly JsonSerializerOptions SerializerOptions = new()
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
};
private readonly AppSettingsService _appSettingsService = new();
private readonly LauncherSettingsService _launcherSettingsService = new();
private readonly IComponentStateStore _componentStateStore = ComponentDomainStorageProvider.Instance;
private readonly IComponentMessageStore _componentMessageStore = ComponentDomainStorageProvider.Instance;
private readonly string _pluginSettingsPath;
private readonly object _pluginSettingsGate = new();
public SettingsService()
{
var root = AppDataPathProvider.GetDataRoot();
_pluginSettingsPath = Path.Combine(root, "plugin-settings.json");
}
public event EventHandler<SettingsChangedEvent>? Changed;
public T LoadSnapshot<T>(SettingsScope scope, string? subjectId = null, string? placementId = null) where T : new()
{
return scope switch
{
SettingsScope.App => ConvertSnapshot<AppSettingsSnapshot, T>(_appSettingsService.Load()),
SettingsScope.Launcher => ConvertSnapshot<LauncherSettingsSnapshot, T>(_launcherSettingsService.Load()),
SettingsScope.ComponentInstance => LoadComponentSnapshot<T>(subjectId, placementId),
SettingsScope.Plugin => LoadSection<T>(scope, EnsureKey(subjectId), sectionId: "__snapshot__", placementId),
_ => new T()
};
}
public void SaveSnapshot<T>(
SettingsScope scope,
T snapshot,
string? subjectId = null,
string? placementId = null,
string? sectionId = null,
IReadOnlyCollection<string>? changedKeys = null)
{
switch (scope)
{
case SettingsScope.App:
_appSettingsService.Save(ConvertSnapshot<T, AppSettingsSnapshot>(snapshot));
break;
case SettingsScope.Launcher:
_launcherSettingsService.Save(ConvertSnapshot<T, LauncherSettingsSnapshot>(snapshot));
break;
case SettingsScope.ComponentInstance:
SaveComponentSnapshot(subjectId, placementId, snapshot);
break;
case SettingsScope.Plugin:
SaveSection(scope, EnsureKey(subjectId), "__snapshot__", snapshot, placementId, changedKeys);
break;
}
OnChanged(new SettingsChangedEvent(scope, subjectId, placementId, sectionId, changedKeys));
}
public T LoadSection<T>(
SettingsScope scope,
string subjectId,
string sectionId,
string? placementId = null) where T : new()
{
if (scope == SettingsScope.ComponentInstance)
{
return _componentMessageStore.LoadSection<T>(EnsureKey(subjectId), placementId, EnsureKey(sectionId));
}
if (scope != SettingsScope.Plugin)
{
return new T();
}
lock (_pluginSettingsGate)
{
var document = LoadPluginDocumentLocked();
if (!document.Sections.TryGetValue(EnsureKey(subjectId), out var pluginSections) ||
!pluginSections.TryGetValue(EnsureKey(sectionId), out var payload))
{
return new T();
}
return JsonSerializer.Deserialize<T>(payload.GetRawText(), SerializerOptions) ?? new T();
}
}
public void SaveSection<T>(
SettingsScope scope,
string subjectId,
string sectionId,
T section,
string? placementId = null,
IReadOnlyCollection<string>? changedKeys = null)
{
if (scope == SettingsScope.ComponentInstance)
{
_componentMessageStore.SaveSection(EnsureKey(subjectId), placementId, EnsureKey(sectionId), section);
OnChanged(new SettingsChangedEvent(scope, subjectId, placementId, sectionId, changedKeys));
return;
}
if (scope != SettingsScope.Plugin)
{
return;
}
lock (_pluginSettingsGate)
{
var document = LoadPluginDocumentLocked();
var pluginId = EnsureKey(subjectId);
if (!document.Sections.TryGetValue(pluginId, out var pluginSections))
{
pluginSections = new Dictionary<string, JsonElement>(StringComparer.OrdinalIgnoreCase);
document.Sections[pluginId] = pluginSections;
}
pluginSections[EnsureKey(sectionId)] = JsonSerializer.SerializeToElement(section, SerializerOptions).Clone();
PersistPluginDocumentLocked(document);
}
OnChanged(new SettingsChangedEvent(scope, subjectId, placementId, sectionId, changedKeys));
}
public void DeleteSection(SettingsScope scope, string subjectId, string sectionId, string? placementId = null)
{
if (scope == SettingsScope.ComponentInstance)
{
_componentMessageStore.DeleteSection(EnsureKey(subjectId), placementId, EnsureKey(sectionId));
OnChanged(new SettingsChangedEvent(scope, subjectId, placementId, sectionId));
return;
}
if (scope != SettingsScope.Plugin)
{
return;
}
lock (_pluginSettingsGate)
{
var document = LoadPluginDocumentLocked();
var pluginId = EnsureKey(subjectId);
if (document.Sections.TryGetValue(pluginId, out var sections) &&
sections.Remove(EnsureKey(sectionId)))
{
if (sections.Count == 0)
{
document.Sections.Remove(pluginId);
}
PersistPluginDocumentLocked(document);
}
}
OnChanged(new SettingsChangedEvent(scope, subjectId, placementId, sectionId));
}
public T? GetValue<T>(
SettingsScope scope,
string key,
string? subjectId = null,
string? placementId = null,
string? sectionId = null)
{
var snapshot = scope switch
{
SettingsScope.App => JsonSerializer.SerializeToElement(_appSettingsService.Load(), SerializerOptions),
SettingsScope.Launcher => JsonSerializer.SerializeToElement(_launcherSettingsService.Load(), SerializerOptions),
SettingsScope.ComponentInstance => JsonSerializer.SerializeToElement(
LoadSection<Dictionary<string, JsonElement>>(
SettingsScope.ComponentInstance,
EnsureKey(subjectId),
sectionId ?? "__root__",
placementId),
SerializerOptions),
SettingsScope.Plugin => JsonSerializer.SerializeToElement(
LoadSection<Dictionary<string, JsonElement>>(SettingsScope.Plugin, EnsureKey(subjectId), sectionId ?? "__root__", placementId),
SerializerOptions),
_ => default
};
if (snapshot.ValueKind != JsonValueKind.Object)
{
return default;
}
foreach (var property in snapshot.EnumerateObject())
{
if (!string.Equals(property.Name, key, StringComparison.OrdinalIgnoreCase))
{
continue;
}
try
{
return property.Value.Deserialize<T>(SerializerOptions);
}
catch
{
return default;
}
}
return default;
}
public void SetValue<T>(
SettingsScope scope,
string key,
T value,
string? subjectId = null,
string? placementId = null,
string? sectionId = null,
IReadOnlyCollection<string>? changedKeys = null)
{
if (scope == SettingsScope.Plugin)
{
var dict = LoadSection<Dictionary<string, JsonElement>>(
SettingsScope.Plugin,
EnsureKey(subjectId),
sectionId ?? "__root__",
placementId);
dict[key] = JsonSerializer.SerializeToElement(value, SerializerOptions).Clone();
SaveSection(SettingsScope.Plugin, EnsureKey(subjectId), sectionId ?? "__root__", dict, placementId, changedKeys ?? [key]);
return;
}
if (scope == SettingsScope.ComponentInstance)
{
var effectiveSection = sectionId ?? "__root__";
var dict = _componentMessageStore.LoadSection<Dictionary<string, JsonElement>>(EnsureKey(subjectId), placementId, effectiveSection);
dict[key] = JsonSerializer.SerializeToElement(value, SerializerOptions).Clone();
_componentMessageStore.SaveSection(EnsureKey(subjectId), placementId, effectiveSection, dict);
OnChanged(new SettingsChangedEvent(scope, subjectId, placementId, sectionId, changedKeys ?? [key]));
return;
}
if (scope == SettingsScope.App)
{
var snapshot = _appSettingsService.Load();
var updated = UpdateObjectKey(snapshot, key, value);
_appSettingsService.Save(updated);
OnChanged(new SettingsChangedEvent(scope, null, null, sectionId, changedKeys ?? [key]));
return;
}
if (scope == SettingsScope.Launcher)
{
var snapshot = _launcherSettingsService.Load();
var updated = UpdateObjectKey(snapshot, key, value);
_launcherSettingsService.Save(updated);
OnChanged(new SettingsChangedEvent(scope, null, null, sectionId, changedKeys ?? [key]));
}
}
public IComponentSettingsAccessor GetComponentAccessor(string componentId, string? placementId)
{
return new ComponentSettingsAccessor(this, componentId, placementId);
}
private T LoadComponentSnapshot<T>(string? componentId, string? placementId) where T : new()
{
var snapshot = _componentStateStore.LoadState(EnsureKey(componentId), placementId);
return ConvertSnapshot<ComponentSettingsSnapshot, T>(snapshot);
}
private void SaveComponentSnapshot<T>(string? componentId, string? placementId, T snapshot)
{
var converted = ConvertSnapshot<T, ComponentSettingsSnapshot>(snapshot);
_componentStateStore.SaveState(EnsureKey(componentId), placementId, converted);
}
private static TOut ConvertSnapshot<TIn, TOut>(TIn source) where TOut : new()
{
if (source is null)
{
return new TOut();
}
if (source is TOut direct)
{
return direct;
}
try
{
var json = JsonSerializer.Serialize(source, SerializerOptions);
return JsonSerializer.Deserialize<TOut>(json, SerializerOptions) ?? new TOut();
}
catch
{
return new TOut();
}
}
private static TSnapshot UpdateObjectKey<TSnapshot, TValue>(TSnapshot snapshot, string key, TValue value)
where TSnapshot : new()
{
var bag = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(
JsonSerializer.Serialize(snapshot, SerializerOptions),
SerializerOptions) ?? new Dictionary<string, JsonElement>(StringComparer.OrdinalIgnoreCase);
var actualKey = bag.Keys.FirstOrDefault(existing => string.Equals(existing, key, StringComparison.OrdinalIgnoreCase)) ?? key;
bag[actualKey] = JsonSerializer.SerializeToElement(value, SerializerOptions).Clone();
try
{
var json = JsonSerializer.Serialize(bag, SerializerOptions);
return JsonSerializer.Deserialize<TSnapshot>(json, SerializerOptions) ?? new TSnapshot();
}
catch
{
return snapshot is null ? new TSnapshot() : snapshot;
}
}
private PluginSettingsDocument LoadPluginDocumentLocked()
{
try
{
if (!File.Exists(_pluginSettingsPath))
{
return new PluginSettingsDocument();
}
var json = File.ReadAllText(_pluginSettingsPath);
return JsonSerializer.Deserialize<PluginSettingsDocument>(json, SerializerOptions) ?? new PluginSettingsDocument();
}
catch (Exception ex)
{
AppLogger.Warn("SettingsService", $"Failed to load plugin settings '{_pluginSettingsPath}'.", ex);
return new PluginSettingsDocument();
}
}
private void PersistPluginDocumentLocked(PluginSettingsDocument document)
{
try
{
var directory = Path.GetDirectoryName(_pluginSettingsPath);
if (!string.IsNullOrWhiteSpace(directory))
{
Directory.CreateDirectory(directory);
}
File.WriteAllText(_pluginSettingsPath, JsonSerializer.Serialize(document, SerializerOptions));
}
catch (Exception ex)
{
AppLogger.Warn("SettingsService", $"Failed to persist plugin settings '{_pluginSettingsPath}'.", ex);
}
}
private static string EnsureKey(string? value)
{
return string.IsNullOrWhiteSpace(value) ? "__default__" : value.Trim();
}
private void OnChanged(SettingsChangedEvent e)
{
try
{
Changed?.Invoke(this, e);
}
catch
{
// Never let a subscriber break settings persistence.
}
}
private sealed class ComponentSettingsAccessor : IComponentSettingsAccessor
{
private readonly SettingsService _settingsService;
public ComponentSettingsAccessor(SettingsService settingsService, string componentId, string? placementId)
{
_settingsService = settingsService;
ComponentId = componentId;
PlacementId = placementId;
}
public string ComponentId { get; }
public string? PlacementId { get; }
public T LoadSnapshot<T>() where T : new()
=> _settingsService.LoadSnapshot<T>(SettingsScope.ComponentInstance, ComponentId, PlacementId);
public void SaveSnapshot<T>(T snapshot, IReadOnlyCollection<string>? changedKeys = null)
=> _settingsService.SaveSnapshot(SettingsScope.ComponentInstance, snapshot, ComponentId, PlacementId, changedKeys: changedKeys);
public T LoadSection<T>(string sectionId) where T : new()
=> _settingsService.LoadSection<T>(SettingsScope.ComponentInstance, ComponentId, sectionId, PlacementId);
public void SaveSection<T>(string sectionId, T section, IReadOnlyCollection<string>? changedKeys = null)
=> _settingsService.SaveSection(SettingsScope.ComponentInstance, ComponentId, sectionId, section, PlacementId, changedKeys);
public void DeleteSection(string sectionId)
=> _settingsService.DeleteSection(SettingsScope.ComponentInstance, ComponentId, sectionId, PlacementId);
public T? GetValue<T>(string key)
=> _settingsService.GetValue<T>(SettingsScope.ComponentInstance, key, ComponentId, PlacementId);
public void SetValue<T>(string key, T value, IReadOnlyCollection<string>? changedKeys = null)
=> _settingsService.SetValue(SettingsScope.ComponentInstance, key, value, ComponentId, PlacementId, changedKeys: changedKeys);
}
private sealed class PluginSettingsDocument
{
public Dictionary<string, Dictionary<string, JsonElement>> Sections { get; set; } = new(StringComparer.OrdinalIgnoreCase);
}
}