This commit is contained in:
2509_92447106 committed 2026-11-08 17:29:32 +08:00
commit 1ee49b8966
1125 files changed
+32542

No files matched your search

@@ -0,0 +1,280 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using EarthModelStudio.Models;
namespace EarthModelStudio.Services;
/// <summary>
/// 模型自动更新:读远端版本清单,把缺失/过期的模型下载到 cache 后原子替换
/// .GC/model/&lt;id&gt;,本次启动立即生效。任何失败都保留本地已装模型。
/// 启动器更新由 LauncherUpdater / LauncherUpdateFlow 独立负责,不在这里。
/// </summary>
public sealed class UpdateService
{
private readonly AppPaths _paths;
private readonly SettingsService _settings;
private readonly IModelRemote _remote;
public UpdateService(AppPaths paths, SettingsService settings, IModelRemote remote)
{
_paths = paths;
_settings = settings;
_remote = remote;
}
public IModelRemote Remote => _remote;
public static string CurrentLauncherVersion
{
get
{
var v = Assembly.GetEntryAssembly()?.GetName().Version;
if (v == null) return "0.0.0";
return $"{Math.Max(0, v.Major)}.{Math.Max(0, v.Minor)}.{Math.Max(0, v.Build)}";
}
}
public static string CurrentRid
{
get
{
var arch = RuntimeInformation.OSArchitecture switch
{
Architecture.Arm64 => "arm64",
Architecture.X86 => "x86",
_ => "x64"
};
if (OperatingSystem.IsWindows()) return "win-" + arch;
if (OperatingSystem.IsMacOS()) return "osx-" + arch;
if (OperatingSystem.IsLinux()) return "linux-" + arch;
return RuntimeInformation.RuntimeIdentifier;
}
}
public static IModelRemote CreateRemote(AppSettings settings)
{
if (!string.IsNullOrWhiteSpace(settings.UpdateLocalRoot) &&
Directory.Exists(settings.UpdateLocalRoot!))
return new DirectoryRemote(settings.UpdateLocalRoot!); // 离线 U 盘源:仍读本地 version.json
// 在线源:启动器版本与发布包走 GitCode Release(tag + 附件),
// 模型清单/文件仍走仓库 contents API;无 Release 时自动回退仓库 version.json。
return new GitCodeReleaseRemote(settings.UpdateManifestUrl, settings.UpdateRepository);
}
public async Task<UpdateResult> RunAsync(bool force,
IProgress<UpdateProgress>? progress, CancellationToken ct = default)
{
// 开发态的 model/ 里有源码与工程文件,模型自动替换会破坏源码目录,
// 因此开发态只做「启动器版本检查」(下载到 .GC/update 暂存,可验证链路),
// 跳过模型替换。要用正式流程验证请跑 ./build.sh update-test。
var devMode = _paths.IsDevelopment &&
Environment.GetEnvironmentVariable("EMS_DEV_UPDATE") != "1";
if (!force && !_settings.Current.AutoUpdateOnStartup)
return new UpdateResult { Outcome = UpdateOutcome.Disabled, Message = L.I["update.disabled"] };
progress?.Report(new UpdateProgress
{
Value = 0.02,
Message = L.I["update.checking"],
Detail = _remote.DisplayName
});
RepoVersion? remote;
try
{
remote = await _remote.GetVersionAsync(ct);
}
catch (OperationCanceledException) { throw; }
catch (Exception ex)
{
return new UpdateResult
{
Outcome = UpdateOutcome.Unavailable,
Message = L.I["update.offline"],
Error = ex.Message
};
}
if (remote == null)
return new UpdateResult
{
Outcome = UpdateOutcome.Unavailable,
Message = L.I["update.noManifest"]
};
_settings.Current.LastUpdateCheck = DateTime.UtcNow.ToString("O");
_settings.Save();
// 启动器更新由 LauncherUpdater / LauncherUpdateFlow 独立负责,
// 这里只做「模型同步到最新版」。
string? launcherMessage = null;
// ---- 同步所有模型到最新版 ----
// 开发态跳过:仓库 model/ 里有源码,整体替换会破坏工程目录。
var needed = devMode
? new List<RepoModel>()
: remote.Models.Where(NeedsModel).ToList();
var updated = new List<string>();
var errors = new List<string>();
for (var i = 0; i < needed.Count; i++)
{
var model = needed[i];
progress?.Report(new UpdateProgress
{
Value = 0.05 + 0.72 * i / Math.Max(1, needed.Count),
Message = L.I.F("update.downloadingModel", model.Id),
Detail = model.Version
});
try
{
await InstallModelAsync(model, progress, i, needed.Count, ct);
_settings.Current.InstalledModelVersions[model.Id] = model.Version;
_settings.Save();
updated.Add(model.Id);
}
catch (OperationCanceledException) { throw; }
catch (Exception ex)
{
errors.Add($"{model.Id}: {ex.Message}");
Console.Error.WriteLine($"模型更新失败 {model.Id}: {ex.Message}");
}
}
progress?.Report(new UpdateProgress { Value = 1, Message = L.I["update.done"] });
var outcome = errors.Count > 0
? UpdateOutcome.Partial
: (updated.Count > 0 || launcherMessage != null)
? UpdateOutcome.Applied
: UpdateOutcome.NoChange;
var message = updated.Count > 0
? L.I.F("update.applied", updated.Count)
: launcherMessage != null
? L.I["update.modelsLatest"]
: devMode
? "开发态:已检查启动器,模型替换已跳过(保护仓库源码)"
: L.I["update.noChange"];
if (errors.Count > 0) message += L.I.F("update.partial", errors.Count);
return new UpdateResult
{
Outcome = outcome,
Message = message,
UpdatedModels = updated.Count,
ModelNames = updated,
LauncherMessage = launcherMessage,
Error = errors.Count > 0 ? string.Join("; ", errors) : null
};
}
private bool NeedsModel(RepoModel model)
{
if (string.IsNullOrWhiteSpace(model.Id) || string.IsNullOrWhiteSpace(model.Version)) return false;
var dir = Path.Combine(_paths.ModelRoot, model.Id);
if (!Directory.Exists(dir)) return true;
if (!File.Exists(Path.Combine(dir, model.Id + ".json"))) return true;
return !_settings.Current.InstalledModelVersions.TryGetValue(model.Id, out var local) ||
!string.Equals(local, model.Version, StringComparison.OrdinalIgnoreCase);
}
/// <summary>只下载模型清单与模型数据目录,源码 / 文档不进运行目录。</summary>
private static bool IsPayload(string id, string relative)
{
var rel = relative.Replace('\\', '/');
// 拒绝任何形式的父目录穿越与绝对路径
if (rel.StartsWith('/') || rel.Contains("..", StringComparison.Ordinal)) return false;
return rel.Equals(id + ".json", StringComparison.OrdinalIgnoreCase) ||
rel.StartsWith(id + ".model/", StringComparison.OrdinalIgnoreCase);
}
private async Task InstallModelAsync(RepoModel model, IProgress<UpdateProgress>? progress,
int index, int count, CancellationToken ct)
{
var rootPath = string.IsNullOrWhiteSpace(model.Dir) ? "model/" + model.Id : model.Dir;
var entries = await _remote.ListTreeAsync(rootPath, ct);
var files = entries.Where(e => !e.IsDirectory && IsPayload(model.Id, e.RelativePath)).ToList();
if (!files.Any(e => e.RelativePath.Equals(model.Id + ".json", StringComparison.OrdinalIgnoreCase)))
throw new InvalidOperationException($"远端缺少模型清单 {model.Id}.json");
if (!files.Any(e => e.RelativePath.StartsWith(model.Id + ".model/", StringComparison.OrdinalIgnoreCase)))
throw new InvalidOperationException($"远端缺少模型数据 {model.Id}.model/");
var staging = Path.Combine(_paths.CacheRoot, "models", model.Id + "-" + Safe(model.Version));
if (Directory.Exists(staging)) Directory.Delete(staging, true);
Directory.CreateDirectory(staging);
try
{
var total = files.Sum(f => Math.Max(1L, f.Size));
long done = 0;
foreach (var entry in files)
{
ct.ThrowIfCancellationRequested();
var bytes = await _remote.DownloadAsync(entry, ct);
// 路径穿越防护:规范化后必须仍落在 staging 目录内,
// 防止远端清单里的 "../" 或绝对路径写进安装目录之外。
var dest = Path.GetFullPath(Path.Combine(staging,
entry.RelativePath.Replace('/', Path.DirectorySeparatorChar)));
var stagingFull = Path.GetFullPath(staging)
.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
if (!dest.StartsWith(stagingFull, StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException($"非法的模型文件路径: {entry.RelativePath}");
var destDir = Path.GetDirectoryName(dest);
if (!string.IsNullOrEmpty(destDir)) Directory.CreateDirectory(destDir);
await File.WriteAllBytesAsync(dest, bytes, ct);
done += bytes.Length;
progress?.Report(new UpdateProgress
{
Value = 0.05 + 0.72 * (index + Math.Min(1, (double)done / Math.Max(1, total))) / Math.Max(1, count),
Message = L.I.F("update.downloadingModel", model.Id),
Detail = entry.RelativePath
});
}
// 原子替换:先备份旧目录,成功后再删备份;失败自动回滚
Directory.CreateDirectory(_paths.ModelRoot);
var target = Path.Combine(_paths.ModelRoot, model.Id);
// 双保险:任何情况下都不允许把含 src/ 源码的模型目录整体换掉
if (Directory.Exists(Path.Combine(target, "src")))
throw new InvalidOperationException($"目标目录含模型源码 src/,拒绝覆盖: {target}");
var backup = target + ".old";
if (Directory.Exists(backup)) Directory.Delete(backup, true);
if (Directory.Exists(target)) Directory.Move(target, backup);
try
{
Directory.Move(staging, target);
}
catch
{
if (Directory.Exists(backup) && !Directory.Exists(target)) Directory.Move(backup, target);
throw;
}
if (Directory.Exists(backup))
{
try { Directory.Delete(backup, true); } catch { /* 占用时留到下次 */ }
}
}
catch
{
try { if (Directory.Exists(staging)) Directory.Delete(staging, true); } catch { }
throw;
}
}
private static string Safe(string value)
{
foreach (var c in Path.GetInvalidFileNameChars()) value = value.Replace(c, '-');
return value.Replace('/', '-').Replace(' ', '-');
}
}