- 启动:健康检查与设置并行请求,页面按需构建并 Offstage 保活,封面按展示尺寸解码 - UI:模型卡片增加悬停动效、封面渐变遮罩、版本/作者/标签胶囊,空简介占位 - 主题:设置页新增主色调选择(预设 + 自定义 #RRGGBB),前后端持久化并实时生效 - CI:新增 PR 审核流水线(后端编译/自检、Flutter 分析测试、漏洞与密钥扫描、汇总门禁) - CI:新增每晚 21:00(UTC 13:00)预发布流水线,prerelease 且不设为 Latest,先删旧再建新 - 安全:后端新增安全响应头、Host/Origin 校验、写入限流、请求体上限、ID 与设置白名单、错误信息脱敏 - 源:暂时只启用 Xhmcl;GitCode 实现保留但不再调用,旧配置自动迁移
88 lines
2.5 KiB
C#
88 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using GC.Models;
|
|
|
|
namespace GC.Services;
|
|
|
|
public sealed class SettingsService
|
|
{
|
|
private readonly AppPaths _paths;
|
|
|
|
public AppSettings Current { get; private set; } = new();
|
|
public event Action? Changed;
|
|
|
|
public SettingsService(AppPaths paths) => _paths = paths;
|
|
|
|
public void Load()
|
|
{
|
|
Current = Json.Read<AppSettings>(_paths.SettingsFile) ?? new AppSettings();
|
|
|
|
// 暂时停用 GitCode:旧配置里的 GitCode 仓库地址统一迁到 Xhmcl。
|
|
if (Current.UpdateRepository.Contains("gitcode.com", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
Current.UpdateRepository = SourcePreset.Xhmcl.HomeUrl;
|
|
Save();
|
|
}
|
|
SeedDefaultSource();
|
|
if (Current.MemoryMb < 256) Current.MemoryMb = 2048;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当前只启用 Xhmcl 一个内置源。GitCode 的定义与下载实现保留在代码里,
|
|
/// 但不再参与测速 / 下载 / 更新;恢复时把 GitCode 加回 <see cref="SourcePreset.All"/> 即可。
|
|
/// </summary>
|
|
public const string DefaultSourceId = "xhmcl";
|
|
public const string DefaultSourceUrl = "https://git.xhmcl.com/Morris/GC";
|
|
|
|
private void SeedDefaultSource()
|
|
{
|
|
var rebuilt = new List<ModelSource>();
|
|
foreach (var preset in SourcePreset.All)
|
|
{
|
|
// 保留用户对「启用/停用」的选择,其余字段以内置定义为准
|
|
var old = Current.Sources.FirstOrDefault(s =>
|
|
SourcePreset.Detect(s.Url).Id == preset.Id);
|
|
rebuilt.Add(new ModelSource
|
|
{
|
|
Id = preset.Id,
|
|
Name = preset.Name,
|
|
Url = preset.HomeUrl,
|
|
Enabled = old?.Enabled ?? true,
|
|
BuiltIn = true
|
|
});
|
|
}
|
|
|
|
var changed = Current.Sources.Count != rebuilt.Count ||
|
|
Current.Sources.Any(s => !s.BuiltIn);
|
|
Current.Sources = rebuilt;
|
|
if (changed) Save();
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
try
|
|
{
|
|
Json.Write(_paths.SettingsFile, Current);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.Error.WriteLine($"保存设置失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public void Apply(Action<AppSettings> mutate)
|
|
{
|
|
mutate(Current);
|
|
Save();
|
|
Changed?.Invoke();
|
|
}
|
|
|
|
public void Notify()
|
|
{
|
|
Save();
|
|
Changed?.Invoke();
|
|
}
|
|
}
|