P0 - GC-SAVEDOG:更新改为整目录替换(旧文件备份到 .GC/update/backup, 失败可完整回滚),不再只搬一个 exe;回滚会删掉新包独有文件 - GC-SAVEDOG:包名校验放宽为单文件名模式,Linux 启动器(无扩展名)可更新 - GC-DOWNLOAD:测速全失败时不再回退 GitCode,改为返回「不可用」并保留上次可用源 - GC-DOWNLOAD:识别并赋予 Linux 无扩展名启动器可执行位;兼容脚本清理旧版时 也覆盖无扩展名产物 - CI:删除 nightly-prerelease(绕开 build.sh,产出无前端空壳);alpha 工作流 去掉非法 cron,只保留手动触发 P1 - 前端:主题设置真正生效(深色/浅色/跟随系统 + 持久化),去掉死字段 - 前端:下载页轮询改为自适应单次链,页面不可见或任务结束后降频/停止 - 前端:下载完成 / 删除模型后首页列表自动刷新 - 前端:复用 http.Client,不再每次轮询新建连接 - 后端:/api/models 走缓存,不再每次全盘递归统计体积 - 设置页:外部改动能同步进表单,不再只同步一次 P2 - 错误响应统一为 ErrorDto,不再复用 LaunchResultDto - 限流桶定期清理,避免无限增长 - 播放器窗口去重,避免同一模型重复开窗 - 封面按实际格式返回,去掉错误的 image/png 与 no-store - 端口集中到 GcUiContract,前端可用 GC_UI_PORT 或 ui-backend.json 发现后端 - 构建:Windows 包改为散开发布,与后端共用运行时(体积约 109MB -> 72MB) - 文档:更新仓库地址与更新流程说明 测试:dotnet build 全绿;GC.Tests 更新链路全通过;flutter analyze 无问题; flutter test 11 项全通过。
198 lines
6.4 KiB
C#
198 lines
6.4 KiB
C#
using System;
|
||
using System.Diagnostics;
|
||
using System.IO;
|
||
using System.Net.Http;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace GC;
|
||
|
||
/// <summary>
|
||
/// 新界面启动壳。
|
||
///
|
||
/// 发布包同时带 <c>GC.UI.Server</c>(本地 HTTP 后端 + 模型窗口宿主)和
|
||
/// <c>GC-UI</c>(Flutter 前端)时,启动器只做三件事:
|
||
/// 1. 拉起后端并等 <c>/api/health</c> 就绪;
|
||
/// 2. 拉起 Flutter 窗口;
|
||
/// 3. 界面退出后关掉后端。
|
||
///
|
||
/// 两个产物缺任意一个就返回 <c>false</c>,由调用方回退到内置 Avalonia 界面——
|
||
/// 这样 `dotnet run`、只编了启动器的开发场景、以及交叉编译缺前端产物的包都能正常打开。
|
||
/// </summary>
|
||
internal static class FlutterShell
|
||
{
|
||
/// <summary>与前端、后端共用的默认端口(定义在 GC.Services.GcUiContract)。</summary>
|
||
private const int Port = Services.GcUiContract.DefaultPort;
|
||
|
||
private static readonly HttpClient Http = new() { Timeout = TimeSpan.FromSeconds(1.5) };
|
||
private static Process? _backend;
|
||
|
||
public static bool TryRun(string[] args)
|
||
{
|
||
var dir = AppContext.BaseDirectory;
|
||
var backendPath = FindBackend(dir);
|
||
var uiPath = FindUi(dir);
|
||
if (backendPath == null || uiPath == null) return false;
|
||
|
||
// 用户可能重复启动:已有后端在跑就直接复用,退出时也不要关掉别人的进程。
|
||
var port = Port;
|
||
|
||
if (!IsHealthy(port))
|
||
{
|
||
// 端口可能被别的程序占用:17890 起向后试几个,选第一个能起来且健康的。
|
||
var start = Environment.GetEnvironmentVariable(Services.GcUiContract.PortEnvVar) is { Length: > 0 }
|
||
? Services.GcUiContract.PortFromEnvironment()
|
||
: Port;
|
||
|
||
for (var candidate = start; candidate < start + Services.GcUiContract.PortScanRange; candidate++)
|
||
{
|
||
var process = Start(backendPath, $"--port {candidate}", dir);
|
||
if (process == null) continue;
|
||
|
||
if (WaitHealthy(candidate, process, TimeSpan.FromSeconds(25)))
|
||
{
|
||
port = candidate;
|
||
_backend = process;
|
||
break;
|
||
}
|
||
|
||
Stop(process);
|
||
}
|
||
|
||
if (_backend == null) return false;
|
||
}
|
||
|
||
AppDomain.CurrentDomain.ProcessExit += (_, _) => Stop(_backend);
|
||
|
||
var ui = Start(uiPath, null, dir, port);
|
||
if (ui == null)
|
||
{
|
||
Stop(_backend);
|
||
_backend = null;
|
||
return false;
|
||
}
|
||
|
||
ui.WaitForExit();
|
||
Stop(_backend);
|
||
_backend = null;
|
||
return true;
|
||
}
|
||
|
||
// ---------------------------------------------------------------- 产物定位
|
||
|
||
private static string? FindBackend(string dir)
|
||
{
|
||
foreach (var name in OperatingSystem.IsWindows()
|
||
? new[] { "GC.UI.Server.exe", "GC.UI.Server" }
|
||
: new[] { "GC.UI.Server", "GC.UI.Server.exe" })
|
||
{
|
||
var path = Path.Combine(dir, name);
|
||
if (File.Exists(path)) return path;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Flutter 桌面产物。
|
||
/// macOS 是 <c>GC-UI.app/Contents/MacOS/GC-UI</c>;Windows 是 <c>GC-UI.exe</c>;
|
||
/// Linux 是 <c>GC-UI</c>。
|
||
/// </summary>
|
||
private static string? FindUi(string dir)
|
||
{
|
||
if (OperatingSystem.IsMacOS())
|
||
{
|
||
var exe = Path.Combine(dir, "GC-UI.app", "Contents", "MacOS", "GC-UI");
|
||
if (File.Exists(exe)) return exe;
|
||
}
|
||
|
||
foreach (var name in OperatingSystem.IsWindows() ? new[] { "GC-UI.exe" } : new[] { "GC-UI" })
|
||
{
|
||
var path = Path.Combine(dir, name);
|
||
if (File.Exists(path)) return path;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
// ---------------------------------------------------------------- 进程管理
|
||
|
||
private static Process? Start(string fileName, string? arguments, string workingDirectory, int port = 0)
|
||
{
|
||
try
|
||
{
|
||
var info = new ProcessStartInfo(fileName)
|
||
{
|
||
WorkingDirectory = workingDirectory,
|
||
UseShellExecute = false,
|
||
CreateNoWindow = true,
|
||
};
|
||
if (!string.IsNullOrEmpty(arguments)) info.Arguments = arguments;
|
||
|
||
// 把实际端口传给子进程:后端读它监听,前端读它拼 baseUrl。
|
||
// 这样换端口不需要重新编译前端。
|
||
if (port > 0)
|
||
info.Environment[Services.GcUiContract.PortEnvVar] = port.ToString();
|
||
|
||
// macOS .app 内的二进制也需要可执行位;发布包由 zip 解开时可能丢失。
|
||
if (!OperatingSystem.IsWindows())
|
||
{
|
||
try { File.SetUnixFileMode(fileName, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute); }
|
||
catch { /* 权限不足时交给系统判断 */ }
|
||
}
|
||
|
||
return Process.Start(info);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.Error.WriteLine($"[shell] 启动失败 {fileName}: {ex.Message}");
|
||
return null;
|
||
}
|
||
}
|
||
|
||
private static void Stop(Process? process)
|
||
{
|
||
if (process == null) return;
|
||
try
|
||
{
|
||
if (!process.HasExited) process.Kill(entireProcessTree: true);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.Error.WriteLine($"[shell] 关闭后端失败: {ex.Message}");
|
||
}
|
||
finally
|
||
{
|
||
process.Dispose();
|
||
}
|
||
}
|
||
|
||
// ---------------------------------------------------------------- 健康检查
|
||
|
||
private static bool IsHealthy(int port)
|
||
{
|
||
try
|
||
{
|
||
using var response = Http.GetAsync($"http://127.0.0.1:{port}/api/health")
|
||
.GetAwaiter().GetResult();
|
||
return response.IsSuccessStatusCode;
|
||
}
|
||
catch
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
private static bool WaitHealthy(int port, Process backend, TimeSpan timeout)
|
||
{
|
||
var deadline = DateTime.UtcNow + timeout;
|
||
while (DateTime.UtcNow < deadline)
|
||
{
|
||
// 后端进程已经退出就没必要再等:端口被占、依赖缺失都会走这里。
|
||
if (backend.HasExited) return false;
|
||
|
||
if (IsHealthy(port)) return true;
|
||
Thread.Sleep(250);
|
||
}
|
||
return false;
|
||
}
|
||
}
|