跳到主要内容

🔧 GeneralUpdate 高级定制参考

涵盖扩展点架构、Pipeline 管道、差分引擎、Bowl 崩溃守护、事件系统、文件系统工具等。

⚠️ API 版本说明:本指南基于 NuGet v10.5.0-beta.7。 以下功能在 v10.5.0-beta.7 中全部可用

  • IUpdateHooks 生命周期钩子(Hooks<T>()
  • IStrategy 自定义策略注入(Strategy<T>()
  • SilentPollOrchestrator 静默轮询器(Option.Silent
  • Option 可编程配置系统
  • ISslValidationPolicy SSL 策略接口
  • IHttpAuthProvider HTTP 认证提供者
  • DiffPipelineBuilder 差分管道配置

各功能的命名空间和用法在文中已标注。


📋 用户需求提取

### 定制目标(必需)
- 需要什么定制: ______(Bowl 崩溃守护 / IPC 替换 / Pipeline 定制 / 自定义策略 / AOT / Drivelution / 黑名单 / 认证提供者 / 差分引擎)
- 使用的 GeneralUpdate 版本: ______(v10.4.6 稳定版 / v10.5.0+ 开发分支)
- .NET 版本: ______(.NET 6/8/9/10)

### Bowl(如果选择)
- 被监控进程名: ______
- 工作模式: ______(Normal / Upgrade)
- 是否需要崩溃 Dump: ______(是/否)
- 备份目录路径: ______

### IPC 替换(如果选择)
- 替换方式: ______(NamedPipe / SharedMemory / 自定义)
- 目标平台: ______(Windows / Linux / macOS / 跨平台)
- 安全要求: ______(加密 / 签名 / 无额外安全)

### AOT(如果选择)
- 当前剪裁警告: ______(有/无)
- 是否使用反射: ______(是/否)
- JSON 序列化需求: ______(有/无)

1. Pipeline 管道系统(v10.5.0-beta.7 可用)

GeneralUpdate 使用 Pipeline 管道模式处理更新包的校验、解压、补丁应用。

PipelineBuilder API

using GeneralUpdate.Core.Pipeline;

// 创建管道上下文
var context = new PipelineContext();
context.Add("ZipFilePath", @"C:\temp\update.zip");
context.Add("Hash", "sha256-hex-value");
context.Add("Format", 0); // 0=Zip
context.Add("Encoding", System.Text.Encoding.UTF8);
context.Add("SourcePath", @"C:\Program Files\MyApp");
context.Add("PatchEnabled", true);

// 构建并执行管道
await new PipelineBuilder(context)
.UseMiddleware<HashMiddleware>() // 哈希校验
.UseMiddleware<CompressMiddleware>() // 解压
.UseMiddleware<PatchMiddleware>() // 差分补丁
.Build();
中间件类名命名空间功能
哈希校验HashMiddlewareGeneralUpdate.Core.PipelineSHA256 完整性校验
解压CompressMiddlewareGeneralUpdate.Core.Pipeline解压 ZIP 包
差分补丁PatchMiddlewareGeneralUpdate.Core.Pipeline应用 BSDIFF/HDiffPatch 补丁
驱动更新DrivelutionMiddlewareGeneralUpdate.Core.PipelineWindows 驱动安装

2. 策略系统(v10.5.0-beta.7 可用)

GeneralUpdate 内置三种平台策略,通过 IStrategy 接口实现:

策略类名平台
WindowsWindowsStrategyWindows
LinuxLinuxStrategyLinux
OSSOSSStrategy跨平台(对象存储)

✅ 支持通过 bootstrap.Strategy<T>() 注入自定义策略。 自定义策略需要实现 IStrategy 接口。


3. Bowl 崩溃守护(v10.5.0-beta.7)

Bowl 是一个崩溃监控组件,通过 BowlContext 配置。

using GeneralUpdate.Bowl;

var context = new BowlContext
{
ProcessNameOrId = "MyApp.exe",
DumpFileName = "v1.0.0.0_fail.dmp",
FailFileName = "v1.0.0.0_fail.json",
TargetPath = @"C:\Program Files\MyApp",
FailDirectory = @"C:\Program Files\MyApp\fail",
BackupDirectory = @"C:\Program Files\MyApp\backup",
WorkModel = "Upgrade",
TimeoutMs = 30_000,
AutoRestore = true,
OnCrash = async (info, ct) => Console.WriteLine($"Crash: {info.DumpFilePath}"),
};

var bowl = new Bowl();
var result = await bowl.LaunchAsync(context);
Console.WriteLine($"Result: Success={result.Success}, Restored={result.Restored}");
属性类型说明
ProcessNameOrIdstring被监控的进程名或 PID(必填)
TargetPathstring应用安装根目录(必填)
DumpFileNamestringDump 文件名(必填)
FailFileNamestring故障报告文件名(必填)
FailDirectorystring崩溃报告输出目录(必填)
BackupDirectorystring备份目录(必填)
WorkModelstring"Upgrade" 或 "Normal"
TimeoutMsint监控超时(毫秒),默认 30000
AutoRestorebool崩溃后自动回滚
DumpTypeDumpTypeMini / Full
OnCrashdelegate崩溃回调

⚠️ NuGet v10.5.0-beta.7 中 Bowl 和 Core 无类型冲突,可以同时引用。


4. EventManager 事件系统(v10.5.0-beta.7 可用)

EventManager 是一个全局单例,提供事件的发布和订阅:

using GeneralUpdate.Core.Event;

// 添加监听
EventManager.Instance.AddListener((object? sender, UpdateInfoEventArgs e) =>
{
// 处理版本发现事件
});

// 手动分发事件
EventManager.Instance.Dispatch(this, new ExceptionEventArgs(ex, "自定义错误"));

// 清空所有监听
EventManager.Instance.Clear();

// 释放
EventManager.Instance.Dispose();

⚠️ EventManager 是全局单例,Dispose()Instance 仍然可访问。


5. 文件系统工具(v10.5.0-beta.7 可用)

BlackList(黑名单)

UpdateRequest 支持通过以下属性排除文件:

var config = new UpdateRequest
{
// ...
Files = new List<string> { "*.log", "*.tmp" },
Formats = new List<string> { ".pdb", ".vshost.exe" },
Directories = new List<string> { "logs", "cache", "temp" },
};

黑名单内部通过 ToBlackPolicy() 转换为 BlackPolicy 记录。

FileTree(文件树对比)

using GeneralUpdate.Core.FileSystem;

var tree = new FileTree();
var snapshot = tree.CreateSnapshot(@"C:\Program Files\MyApp");

6. 差分引擎(v10.5.0-beta.7 可用,无需额外安装包)

差分类型已内嵌在 GeneralUpdate.Core 中,无需额外安装 GeneralUpdate.Differential 包。

DiffPipelineBuilder 方式(推荐)

using GeneralUpdate.Core.Pipeline;

var pipeline = new DiffPipelineBuilder()
.UseDiffer(new StreamingHdiffDiffer()) // 差分算法
.UseCleanMatcher(new DefaultCleanMatcher()) // 文件匹配器(服务端)
.UseDirtyMatcher(new DefaultDirtyMatcher()) // 文件匹配器(客户端)
.WithParallelism(4)
.WithStopOnFirstError(true)
.WithProgress(new Progress<DiffProgress>(p =>
Console.WriteLine($"[{p.Completed}/{p.Total}] {p.FileName}")))
.Build();

// 服务端:生成补丁
await pipeline.CleanAsync(oldDir, newDir, patchDir);

// 客户端:应用补丁
await pipeline.DirtyAsync(appDir, patchDir);

Bootstrap 集成方式

new GeneralUpdateBootstrap()
.SetConfig(config)
.UseDiffPipeline(pipeline =>
{
pipeline.WithParallelism(2)
.WithStopOnFirstError(true);
})
.LaunchAsync();

自定义匹配器

using GeneralUpdate.Core.Differential;

// 自定义清理匹配器
var cleanMatcher = new DefaultCleanMatcher(); // 或实现 ICleanMatcher
var dirtyMatcher = new DefaultDirtyMatcher(); // 或实现 IDirtyMatcher

7. AOT / NativeAOT 兼容性

GeneralUpdate.Core v10.5.0-beta.7 支持 .NET Native AOT(net8.0net10.0):

<PropertyGroup>
<IsAotCompatible>true</IsAotCompatible>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
</PropertyGroup>

JSON 序列化上下文(减少 AOT 大小):

using GeneralUpdate.Core.JsonContext;

// 使用内置的 JsonSerializerContext
// VersionRespJsonContext, ProcessContractJsonContext, HttpParameterJsonContext 等

8. Drivelution(Windows 驱动更新)

GeneralUpdate.Drivelution 包提供 Windows 驱动管理:

using GeneralUpdate.Drivelution;

// 扫描驱动目录
var allDrivers = GeneralDrivelution.ScanDirectory(driverDir);

// 验证驱动
var isValid = GeneralDrivelution.ValidateDriver(driverPath);

// 安装驱动(DIFx → SetupAPI → PnPUtil 级联)
var result = GeneralDrivelution.InstallDriver(driverPath);

内容索引

主题可用性参考
Pipeline 管道✅ v10.5.0-beta.7GeneralUpdate.Core.Pipeline
策略系统✅ v10.5.0-beta.7GeneralUpdate.Core.Strategy
FileTree✅ v10.5.0-beta.7GeneralUpdate.Core.FileSystem
BlackList✅ v10.5.0-beta.7UpdateRequest.Files/Formats/DirectoriesToBlackPolicy()
差分引擎✅ 内嵌 CoreDiffPipelineBuilder / DiffPipeline
AOT✅ v10.5.0-beta.7JsonSerializerContext 子类
EventManager✅ v10.5.0-beta.7GeneralUpdate.Core.Event
Bowl 崩溃守护✅ v10.5.0-beta.7GeneralUpdate.Bowl.Bowl
IUpdateHooks✅ v10.5.0-beta.7GeneralUpdate.Core.HooksHooks<T>()
自定义 Strategy 注入✅ v10.5.0-beta.7Strategy<T>()
IPC 替换接口❌ 暂不支持使用 NamedPipe 替代方案
SilentPollOrchestrator✅ v10.5.0-beta.7Option.Silent + SetOption()
Option 系统✅ v10.5.0-beta.7SetOption<T>(Option<T>, T)

✅ 高级定制验证清单

Bowl 崩溃守护

  • 如果用 Bowl:项目中同时引用 GeneralUpdate.CoreGeneralUpdate.Bowl(v10.5.0-beta.7 无冲突)
  • BowlContextProcessNameOrId 与实际进程名匹配
  • TargetPath 设置为应用安装根目录,非子目录
  • WorkModel 根据场景选择 Correct(Normal/Upgrade)
  • FailDirectory 有写入权限
  • Linux/macOS 无此功能(Bowl 仅 Windows)

Pipeline 定制

  • PipelineContext 中的 Key 名称使用字符串常量拼写正确
  • 中间件注册顺序正确:Hash → Compress → Patch → Drivelution
  • Encoding 设置为 Encoding.UTF8

AOT/NativeAOT

  • 启用了 <IsAotCompatible>true</IsAotCompatible>
  • 对反射路径添加了 [DynamicDependency][RequiresUnreferencedCode]
  • 使用了内置的 JsonSerializerContext 子类(减少裁剪)

⚠️ 反模式清单

#反模式后果正确做法
1在 v10.4.6 稳定版上使用开发分支 API(IUpdateHooks 等)编译失败 / 运行时 MissingMethodException检查 API 可用性表
2PipelineContext Key 拼写错误Pipeline 运行异常,值未传递使用类库公开的常量或文档中的 Key 名
3Bowl 的 WorkModel 设为 Upgrade 但进程是主程序监控逻辑错误Normal=主线进程,Upgrade=升级进程
4Windows 上 IPC 使用默认加密密钥加密可被破解使用强密钥(≥ 32 字符)
5差分包生成时使用不同版本的源文件结构补丁应用失败源和目标版本的文件结构必须一致
6AOT 项目中使用了大量反射且未标记 DynamicDependency运行时 TypeLoadException使用源代码生成器或显式标记保留
7Pipeline 中 PatchMiddleware 排在 CompressMiddleware 前面未解压就试图打补丁顺序必须是 Compress→Patch

相关技能

  • /generalupdate-init — Bootstrap 配置
  • /generalupdate-strategy — 更新策略选择
  • /generalupdate-troubleshoot — 问题诊断