Repository navigation
feat: 配置备份恢复 #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: 配置备份恢复 #83
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,11 +29,16 @@ public ConfigurationController(StaticSettings settings, ILogger<ConfigurationCon | |
| { | ||
| var dllPath = await modConfigService.GetAquaMaiDllPath(); | ||
| var configSortYaml = ReadConfigSortYaml(dllPath); | ||
| var shouldSkipSignatureCheck = skipSignatureCheck || !string.Equals(dllPath, ModPaths.AquaMaiDllInstalledPath, StringComparison.OrdinalIgnoreCase); | ||
| var config = await modConfigService.GetCurrentAquaMaiConfig(forceDefault, shouldSkipSignatureCheck); | ||
| var config = await modConfigService.GetCurrentAquaMaiConfig(forceDefault, skipSignatureCheck); | ||
| return AquaMaiConfigDto.From(config, configSortYaml); | ||
| } | ||
|
|
||
| [HttpGet] | ||
| public async Task<IReadOnlyList<AquaMaiConfigBackupDto>> GetAquaMaiConfigBackups(bool skipSignatureCheck = false, CancellationToken ct = default) | ||
| { | ||
| return await modConfigService.GetAquaMaiConfigBackups(skipSignatureCheck, ct); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: 该端点会直接返回所有备份中的历史覆盖值;请在 Prompt for AI agents |
||
| } | ||
|
|
||
| [HttpPut] | ||
| public async Task SetAquaMaiConfig(AquaMaiConfigDto.ConfigSave config) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,9 +1,20 @@ | ||||||
| using System.Globalization; | ||||||
| using System.Text.Json; | ||||||
| using AquaMai.Config.Dto; | ||||||
| using AquaMai.Config.HeadlessLoader; | ||||||
| using AquaMai.Config.Interfaces; | ||||||
| using MaiChartManager.Utils; | ||||||
|
|
||||||
| namespace MaiChartManager.Controllers.Mod; | ||||||
|
|
||||||
| public record AquaMaiConfigBackupDto( | ||||||
| string FileName, | ||||||
| DateTimeOffset Timestamp, | ||||||
| IReadOnlyDictionary<string, bool>? SectionOverrides, | ||||||
| IReadOnlyDictionary<string, JsonElement>? EntryOverrides, | ||||||
| string? ParseError | ||||||
| ); | ||||||
|
|
||||||
| public class ModConfigService | ||||||
| { | ||||||
| private const string ExclusiveFullscreenSectionPath = "GameSystem.Window"; | ||||||
|
|
@@ -98,12 +109,12 @@ public void SyncExclusiveFullscreenCompatFlag(IConfig config) | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| public async Task<IConfig> GetCurrentAquaMaiConfig(bool forceDefault = false, bool skipSignatureCheck = false, CancellationToken ct = default) | ||||||
| private async Task<HeadlessConfigInterface> LoadConfigInterface(bool skipSignatureCheck, CancellationToken ct) | ||||||
| { | ||||||
| var dllPath = await GetAquaMaiDllPath(ct); | ||||||
|
|
||||||
| var binary = await File.ReadAllBytesAsync(dllPath, ct); | ||||||
| if (!skipSignatureCheck) | ||||||
| var shouldSkipSignatureCheck = skipSignatureCheck || !string.Equals(dllPath, ModPaths.AquaMaiDllInstalledPath, StringComparison.OrdinalIgnoreCase); | ||||||
| if (!shouldSkipSignatureCheck) | ||||||
| { | ||||||
| var sigResult = AquaMaiSignatureV2.VerifySignature(binary); | ||||||
| if (sigResult.Status != AquaMaiSignatureV2.VerifyStatus.Valid) | ||||||
|
|
@@ -112,8 +123,14 @@ public async Task<IConfig> GetCurrentAquaMaiConfig(bool forceDefault = false, bo | |||||
| } | ||||||
| } | ||||||
| var configInterface = HeadlessConfigLoader.LoadFromPacked(binary); | ||||||
| var config = configInterface.CreateConfig(); | ||||||
| CheckConfigApiVersion(configInterface); | ||||||
| return configInterface; | ||||||
| } | ||||||
|
|
||||||
| public async Task<IConfig> GetCurrentAquaMaiConfig(bool forceDefault = false, bool skipSignatureCheck = false, CancellationToken ct = default) | ||||||
| { | ||||||
| var configInterface = await LoadConfigInterface(skipSignatureCheck, ct); | ||||||
| var config = configInterface.CreateConfig(); | ||||||
| if (File.Exists(ModPaths.AquaMaiConfigPath) && !forceDefault) | ||||||
| { | ||||||
| try | ||||||
|
|
@@ -146,4 +163,102 @@ public async Task<IConfig> GetCurrentAquaMaiConfig(bool forceDefault = false, bo | |||||
|
|
||||||
| return config; | ||||||
| } | ||||||
|
|
||||||
| public async Task<IReadOnlyList<AquaMaiConfigBackupDto>> GetAquaMaiConfigBackups(bool skipSignatureCheck = false, CancellationToken ct = default) | ||||||
| { | ||||||
| if (string.IsNullOrEmpty(StaticSettings.GamePath) || !Directory.Exists(ModPaths.AquaMaiConfigBackupDirPath)) | ||||||
| { | ||||||
| return []; | ||||||
| } | ||||||
|
|
||||||
| var files = Directory.GetFiles(ModPaths.AquaMaiConfigBackupDirPath, "*.toml"); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · 大量备份历史会拖慢恢复 当备份目录中存在许多或很大的文件,或者端点被反复调用时, 请限制备份历史记录的数量,或对列表进行分页,使每个请求读取并返回有界数量的结果。 另见 面向 AI 智能体的提示在 `MaiChartManager/Controllers/Mod/ModConfigService.cs` 第 174 行:
**大量备份历史会拖慢恢复**
当备份目录中存在许多或很大的文件,或者端点被反复调用时,`GetAquaMaiConfigBackups` 会在每次请求时读取并解析所有备份,然后返回全部结果;由于历史记录没有上限,工作量和内存用量会不断增长,最终导致请求变慢或服务无响应。
请限制备份历史记录的数量,或对列表进行分页,使每个请求读取并返回有界数量的结果。
另见 `MaiChartManager/Controllers/Mod/ModConfigService.cs:206`。Original comment in English🟡 Medium · Large backup histories stall recovery When the backup directory contains many or large files, or the endpoint is called repeatedly, Limit backup history or paginate the listing so each request reads and returns a bounded set. Also at Prompt for AI agentsIn `MaiChartManager/Controllers/Mod/ModConfigService.cs` at line 174:
**Large backup histories stall recovery**
When the backup directory contains many or large files, or the endpoint is called repeatedly, `GetAquaMaiConfigBackups` reads and parses every backup and returns all results on each request, making work and memory use grow with the uncapped history until the request slows or the service becomes unresponsive.
Limit backup history or paginate the listing so each request reads and returns a bounded set.
Also at `MaiChartManager/Controllers/Mod/ModConfigService.cs:206`. |
||||||
| if (files.Length == 0) | ||||||
| { | ||||||
| return []; | ||||||
| } | ||||||
|
|
||||||
| var backupFiles = files | ||||||
| .Select(filePath => | ||||||
| { | ||||||
| var fileName = Path.GetFileName(filePath); | ||||||
| var stem = Path.GetFileNameWithoutExtension(filePath); | ||||||
| var timestamp = DateTime.TryParseExact( | ||||||
| stem, | ||||||
| "yyyy-MM-dd_HH-mm-ss", | ||||||
| CultureInfo.InvariantCulture, | ||||||
| DateTimeStyles.AssumeLocal, | ||||||
| out var parsed) | ||||||
| ? new DateTimeOffset(parsed) | ||||||
| : new DateTimeOffset(File.GetLastWriteTime(filePath)); | ||||||
| return (filePath, fileName, timestamp); | ||||||
| }) | ||||||
| .OrderByDescending(x => x.timestamp) | ||||||
| .ThenByDescending(x => x.fileName, StringComparer.OrdinalIgnoreCase) | ||||||
| .ToArray(); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: 此列表会逐个读取并解析目录中的所有备份,历史规模会直接放大每次请求的 I/O、内存和响应体;请限制每次处理的数量或实现分页。 Prompt for AI agents
Suggested change
|
||||||
|
|
||||||
| var configInterface = await LoadConfigInterface(skipSignatureCheck, ct); | ||||||
| // SetEntryValue 会写回 ReflectionField.Value,复用同一个 ReflectionManager 时每次解析前必须恢复默认值 | ||||||
| var defaultFieldValues = configInterface.ReflectionManager.Entries | ||||||
| .Select(entry => (entry, value: entry.Field.GetValue(null))) | ||||||
| .ToArray(); | ||||||
|
|
||||||
| var results = new List<AquaMaiConfigBackupDto>(backupFiles.Length); | ||||||
| foreach (var (filePath, fileName, timestamp) in backupFiles) | ||||||
| { | ||||||
| foreach (var (entry, defaultValue) in defaultFieldValues) | ||||||
| { | ||||||
| entry.Field.SetValue(null, defaultValue); | ||||||
| } | ||||||
|
|
||||||
| try | ||||||
| { | ||||||
| var toml = await File.ReadAllTextAsync(filePath, ct); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · 有效备份会被误判为格式错误 当备份列表在配置保存正在写入文件时读取该文件, 请以原子方式写入备份,确保列表请求只读取完整文件。 面向 AI 智能体的提示在 `MaiChartManager/Controllers/Mod/ModConfigService.cs` 第 215 行:
**有效备份会被误判为格式错误**
当备份列表在配置保存正在写入文件时读取该文件,`GetAquaMaiConfigBackups` 可能会读到不完整的 TOML 文件,并对有效备份返回 `ParseError`,导致界面将其显示为失败状态,直到重新加载列表。
请以原子方式写入备份,确保列表请求只读取完整文件。Original comment in English🟡 Medium · Valid backups appear malformed When the backup list reads a file while a config save is writing it, Write backups atomically so the listing request reads only complete files. Prompt for AI agentsIn `MaiChartManager/Controllers/Mod/ModConfigService.cs` at line 215:
**Valid backups appear malformed**
When the backup list reads a file while a config save is writing it, `GetAquaMaiConfigBackups` can read a partial TOML file and return a `ParseError` for a valid backup, so the UI shows it as failed until the list is reloaded.
Write backups atomically so the listing request reads only complete files.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: 列表读取可能与配置保存对同一备份文件的写入重叠,从而读到不完整 TOML 并暂时返回 Prompt for AI agents |
||||||
| var config = configInterface.CreateConfig(); | ||||||
| var view = configInterface.CreateConfigView(toml); | ||||||
| var migrationManager = configInterface.GetConfigMigrationManager(); | ||||||
|
|
||||||
| if (migrationManager.GetVersion(view) != migrationManager.LatestVersion) | ||||||
| { | ||||||
| view = migrationManager.Migrate(view); | ||||||
| } | ||||||
|
|
||||||
| configInterface.GetConfigParser().Parse(config, view); | ||||||
|
|
||||||
| var sectionOverrides = new Dictionary<string, bool>(); | ||||||
| var entryOverrides = new Dictionary<string, JsonElement>(); | ||||||
| foreach (var section in config.ReflectionManager.Sections) | ||||||
| { | ||||||
| var sectionState = config.GetSectionState(section); | ||||||
| var effectiveEnabled = section.Attribute.AlwaysEnabled || sectionState.Enabled; | ||||||
| if (!section.Attribute.AlwaysEnabled && sectionState.Enabled != sectionState.DefaultEnabled) | ||||||
| { | ||||||
| sectionOverrides[section.Path] = sectionState.Enabled; | ||||||
| } | ||||||
|
|
||||||
| // 禁用栏目下的条目不会生效,保存时也会跳过 | ||||||
| if (!effectiveEnabled) | ||||||
| { | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| foreach (var entry in section.Entries) | ||||||
| { | ||||||
| var entryState = config.GetEntryState(entry); | ||||||
| if (!Equals(entryState.Value, entryState.DefaultValue)) | ||||||
| { | ||||||
| entryOverrides[entry.Path] = AquaMaiConfigDto.SerializeValue(entryState.Value); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| results.Add(new AquaMaiConfigBackupDto(fileName, timestamp, sectionOverrides, entryOverrides, null)); | ||||||
| } | ||||||
| catch (Exception ex) when (ex is not OperationCanceledException || !ct.IsCancellationRequested) | ||||||
| { | ||||||
| results.Add(new AquaMaiConfigBackupDto(fileName, timestamp, null, null, ex.Message)); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return results; | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium · 通过局域网暴露历史设置
当应用在禁用身份验证的导出模式下运行,且备份文件包含历史覆盖值时,未经过身份验证的局域网客户端可以在启用导出模式且
UseAuth为 false 时调用此端点;该服务会返回所有备份中解析出的覆盖值,从而暴露当前配置中已不存在的历史配置。请对此端点要求身份验证,或以其他方式阻止未经过身份验证的局域网客户端获取备份内容。
面向 AI 智能体的提示
Original comment in English
🟡 Medium · Historical settings exposed over LAN
When the application is running in export mode with authentication disabled and backup files contain historical overrides, an unauthenticated LAN client can call this endpoint when export mode is enabled and
UseAuthis false; the service returns parsed override values from every backup, exposing historical configuration that is no longer present in the current config.Require authentication for this endpoint or otherwise prevent unauthenticated LAN clients from retrieving backup contents.
Prompt for AI agents