Files
XplorePlane/XplorePlane/Services/Debug/DebugPanelConfigService.cs
T
2026-05-18 09:38:29 +08:00

142 lines
4.5 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Windows;
using XP.Common.Logging.Interfaces;
using XplorePlane.Models;
using XplorePlane.ViewModels.Debug;
namespace XplorePlane.Services.Debug
{
public class DebugPanelConfigService : IDebugPanelConfigService
{
private readonly ILoggerService _logger;
private readonly JsonSerializerOptions _jsonOptions;
private string _configPath;
public DebugPanelConfigService(ILoggerService logger)
{
ArgumentNullException.ThrowIfNull(logger);
_logger = logger.ForModule<DebugPanelConfigService>();
_jsonOptions = new JsonSerializerOptions
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
_configPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"XplorePlane",
"DebugPanel.config");
}
public DebugPanelConfig LoadConfig()
{
try
{
if (!File.Exists(_configPath))
{
_logger.Info("调试面板配置不存在,使用默认配置 | Debug panel config missing, using defaults");
return CreateDefaultConfig();
}
var json = File.ReadAllText(_configPath);
var config = JsonSerializer.Deserialize<DebugPanelConfig>(json, _jsonOptions);
return NormalizeConfig(config);
}
catch (Exception ex)
{
_logger.Error(ex, "加载调试面板配置失败,回退默认配置 | Failed to load debug panel config, falling back to defaults");
return CreateDefaultConfig();
}
}
public void SaveConfig(DebugPanelConfig config)
{
if (config == null)
{
_logger.Warn("保存调试面板配置被忽略:配置为空 | Skip saving debug panel config because config is null");
return;
}
try
{
config = NormalizeConfig(config);
var directory = Path.GetDirectoryName(_configPath);
if (!string.IsNullOrWhiteSpace(directory) && !Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
var json = JsonSerializer.Serialize(config, _jsonOptions);
File.WriteAllText(_configPath, json);
_logger.Info("调试面板配置已保存到 {Path} | Debug panel config saved to {Path}", _configPath);
}
catch (Exception ex)
{
_logger.Error(ex, "保存调试面板配置失败 | Failed to save debug panel config");
}
}
private DebugPanelConfig NormalizeConfig(DebugPanelConfig config)
{
var normalized = config ?? CreateDefaultConfig();
normalized.Window ??= CreateDefaultWindowConfig();
if (normalized.Window.Width <= 0)
{
normalized.Window.Width = 1200;
}
if (normalized.Window.Height <= 0)
{
normalized.Window.Height = 800;
}
normalized.EventFilters ??= new Dictionary<string, bool>();
foreach (var key in DebugPanelStateMetadata.StateCategories)
{
if (!normalized.EventFilters.ContainsKey(key))
{
normalized.EventFilters[key] = true;
}
}
return normalized;
}
private static DebugPanelConfig CreateDefaultConfig()
{
var filters = new Dictionary<string, bool>();
foreach (var key in DebugPanelStateMetadata.StateCategories)
{
filters[key] = true;
}
return new DebugPanelConfig
{
Window = CreateDefaultWindowConfig(),
EventFilters = filters,
DockingLayout = string.Empty
};
}
private static WindowConfig CreateDefaultWindowConfig()
{
return new WindowConfig
{
Left = 100,
Top = 100,
Width = 1200,
Height = 800,
State = WindowState.Normal
};
}
}
}