From d7dc9eab44792ec5d08f9280bd1d38edbb82c38f Mon Sep 17 00:00:00 2001 From: Juster Zhu Date: Thu, 19 Nov 2026 23:59:43 +0800 Subject: [PATCH] refactor!: remove manual Initialize in favor of implicit bootstrap Native bootstrap now runs automatically when GeneralZipFormatRegistry.Default is first constructed: the Rust core is loaded, 7z/RAR providers are registered per capability bits, and the NativeBridge codec delegates are injected. The codec router additionally calls EnsureBridge() as a fallback for custom registry scenarios. BREAKING CHANGE: GeneralZip.Initialize() and GeneralZipNative.Initialize() have been removed. Use NativeCoreLoader.TryLoad() to probe native core availability. Version bumped to 2.0.0. --- .github/workflows/release.yml | 2 +- Directory.Build.props | 2 +- README.en.md | 12 +-- README.md | 12 +-- README.zh-CN.md | 10 +-- docs/ARCHITECTURE.md | 13 ++-- samples/GeneralZip.Bench/Program.cs | 3 +- samples/GeneralZip.Fixtures/Program.cs | 3 +- samples/GeneralZip.Samples/Program.cs | 11 ++- src/GeneralZip/ArchiveFormatRegistry.cs | 13 +++- .../Formats/Codecs/BuiltinCodecs.cs | 25 ++++-- src/GeneralZip/GeneralZip.cs | 29 +------ src/GeneralZip/Native/GeneralZipNative.cs | 76 ++++++++----------- .../NativeCodecTests.cs | 3 +- .../NativeCoreTests.cs | 19 ++++- .../NativeFixtureTests.cs | 2 - 16 files changed, 112 insertions(+), 123 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9374c2b..5e62413 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -160,7 +160,7 @@ jobs: using GeneralZip; using static GeneralZip.GeneralZip; - Initialize(); + // 无任何显式引导调用:验证零初始化的隐式引导端到端可用 Console.WriteLine($"native={GeneralZip.Native.NativeCoreLoader.IsAvailable}"); var dir = Path.Combine(Path.GetTempPath(), "gzp-smoke"); diff --git a/Directory.Build.props b/Directory.Build.props index eba2781..dcd97d7 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -4,7 +4,7 @@ enable true GeneralZip - 1.2.0 + 2.0.0 GeneralZip GeneralZip diff --git a/README.en.md b/README.en.md index d6805b6..9177d23 100644 --- a/README.en.md +++ b/README.en.md @@ -33,9 +33,10 @@ dotnet add package GeneralZip Targets `netstandard2.0` and `net8.0`. > One package, everything included. The library bundles prebuilt Rust native cores for -> win-x64, linux-x64, linux-musl-x64, osx-x64 and osx-arm64; `GeneralZip.Initialize()` -> loads them automatically. If the core is missing for your platform, 7z/RAR and the -> native codecs degrade gracefully; everything else keeps working. +> win-x64, linux-x64, linux-musl-x64, osx-x64 and osx-arm64; they are detected and loaded +> automatically on first use — no initialization call needed. If the core is missing for +> your platform, 7z/RAR and the native codecs degrade gracefully; everything else keeps +> working. ## Quick Start @@ -44,8 +45,6 @@ Targets `netstandard2.0` and `net8.0`. ```csharp using GeneralZip; -GeneralZip.Initialize(); // idempotent bootstrap - // Directory → archive (format is chosen by the file extension) await GeneralZip.CompressAsync(@"C:\data", @"D:\backup.zip"); @@ -187,7 +186,8 @@ Place `generalzip_native.dll/.so/.dylib` in one of: 2. `runtimes//native/` under the app directory, 3. any path referenced by the `GENERALZIP_NATIVE` environment variable, -then call `GeneralZip.Initialize()` once (already done implicitly by the one-shot APIs). +The core is picked up automatically the first time you use the library — no +initialization call is needed. ## Sample Files diff --git a/README.md b/README.md index 8209c3c..51e16de 100644 --- a/README.md +++ b/README.md @@ -33,9 +33,10 @@ dotnet add package GeneralZip Targets `netstandard2.0` and `net8.0`. > One package, everything included. The library bundles prebuilt Rust native cores for -> win-x64, linux-x64, linux-musl-x64, osx-x64 and osx-arm64; `GeneralZip.Initialize()` -> loads them automatically. If the core is missing for your platform, 7z/RAR and the -> native codecs degrade gracefully; everything else keeps working. +> win-x64, linux-x64, linux-musl-x64, osx-x64 and osx-arm64; they are detected and loaded +> automatically on first use — no initialization call needed. If the core is missing for +> your platform, 7z/RAR and the native codecs degrade gracefully; everything else keeps +> working. ## Quick Start @@ -44,8 +45,6 @@ Targets `netstandard2.0` and `net8.0`. ```csharp using GeneralZip; -GeneralZip.Initialize(); // idempotent bootstrap - // Directory → archive (format is chosen by the file extension) await GeneralZip.CompressAsync(@"C:\data", @"D:\backup.zip"); @@ -189,7 +188,8 @@ Place `generalzip_native.dll/.so/.dylib` in one of: 2. `runtimes//native/` under the app directory, 3. any path referenced by the `GENERALZIP_NATIVE` environment variable, -then call `GeneralZip.Initialize()` once (already done implicitly by the one-shot APIs). +The core is picked up automatically the first time you use the library — no +initialization call is needed. ## Sample Files diff --git a/README.zh-CN.md b/README.zh-CN.md index b312179..f486f13 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -33,7 +33,7 @@ dotnet add package GeneralZip 目标框架 `netstandard2.0` 与 `net8.0`。 > 一个包,能力齐全。包内已捆绑 win-x64、linux-x64、linux-musl-x64、osx-x64、osx-arm64 -> 五个平台的预编译 Rust 原生核心,`GeneralZip.Initialize()` 会自动加载; +> 五个平台的预编译 Rust 原生核心,首次使用时自动探测并加载,**无需任何初始化调用**; > 若平台无对应核心,7z/RAR 与原生编解码优雅降级,其余能力不受影响。 ## 快速开始 @@ -43,8 +43,6 @@ dotnet add package GeneralZip ```csharp using GeneralZip; -GeneralZip.Initialize(); // 幂等引导 - // 目录 → 归档(格式由扩展名决定) await GeneralZip.CompressAsync(@"C:\data", @"D:\backup.zip"); @@ -133,8 +131,8 @@ GeneralZip.Registry.Register(new MyArchiveFormat()); GeneralZip.Encryption.Register(new MyEncryptionProvider()); ``` -> 引导类与组件同名(`namespace GeneralZip` 中的 `class GeneralZip`)。 -> 若需要与其它类型限定写法混用,可用 `using static GeneralZip.GeneralZip;` 裸调用 `Initialize()` / `CompressAsync()` 等。 +> 门面类与组件同名(`namespace GeneralZip` 中的 `class GeneralZip`)。 +> 若需要与其它类型限定写法混用,可用 `using static GeneralZip.GeneralZip;` 裸调用 `CompressAsync()` 等。 ## 格式与加密矩阵 @@ -186,7 +184,7 @@ GeneralZip.Encryption.Register(new MyEncryptionProvider()); 2. 应用目录下的 `runtimes//native/`; 3. `GENERALZIP_NATIVE` 环境变量指向的路径, -然后启动时调用一次 `GeneralZip.Initialize()`(一行式 API 内部已自动调用)。 +首次使用库时核心会被自动探测并加载,无需任何初始化调用。 ## 示例文件 diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index b0c48ee..abeff82 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -17,7 +17,7 @@ ``` ┌────────────────────────────────────────────────────────────┐ -│ 门面层 GeneralZip (静态引导/门面) / │ 一行式与流式 API +│ 门面层 GeneralZip (静态门面) / │ 一行式与流式 API │ GeneralZipReader / GeneralZipWriter │ ├────────────────────────────────────────────────────────────┤ │ 事件层 EntryStarted / EntryProgress / EntryCompleted / │ 进度 @@ -65,10 +65,10 @@ GeneralZip.sln │ │ ├─ Tar/ TAR / TAR.GZ / TAR.BZ2 │ │ └─ Codecs/ GZ/BZ2/XZ/Zstd/LZ4 单文件编解码格式 │ ├─ Native/ Rust 核心 P/Invoke 绑定(能力协商 + 降级) -│ ├─ GeneralZip.cs 启动引导(含原生自动装配)+ 静态门面 +│ ├─ GeneralZip.cs 静态门面(零初始化) │ ├─ ArchiveReader.cs 读取封装(事件源) │ ├─ ArchiveWriter.cs 写入封装(事件源) -│ └─ ArchiveFormatRegistry.cs 格式注册表 + 魔数探测 +│ └─ ArchiveFormatRegistry.cs 格式注册表 + 魔数探测(Default 装配时隐式原生引导) ├─ tests/ │ ├─ GeneralZip.Tests/ 核心测试(含互操作 oracle) │ └─ GeneralZip.Native.Tests/ Rust 核心集成测试 @@ -301,14 +301,15 @@ gzp_close(handle) -> status ``` NativeCoreLoader.TryLoad() ├─ 按 RID 探测 runtimes/{rid}/native/generalzip_native.{dll|so|dylib} - │ 以及应用目录 / GENERALZIP_NATIVE_PATH 环境变量 + │ 以及应用目录 / GENERALZIP_NATIVE 环境变量 ├─ 载入成功 → gzp_capabilities() → 按 bit 注册 SevenZipFormat / RarFormat └─ 载入失败 → 不注册;用户访问 7z/RAR 时抛 NativeCoreMissingException("...如何安装/部署 native core...") ``` -原生绑定与托管核心同包发布:`GeneralZip.Initialize()` 自动调用 -`GeneralZipNative.Initialize()`(幂等)完成能力注册;核心二进制缺失时仅降级、不抛异常。 +原生绑定与托管核心同包发布,引导完全隐式:首次访问任何 API 时, +`GeneralZipFormatRegistry.Default` 的装配自动调用 `GeneralZipNative.Bootstrap()` +(幂等)完成能力注册与编解码桥接注入;核心二进制缺失时仅降级、不抛异常。 ## 8. 关键流程 diff --git a/samples/GeneralZip.Bench/Program.cs b/samples/GeneralZip.Bench/Program.cs index 408d8f6..0aae56b 100644 --- a/samples/GeneralZip.Bench/Program.cs +++ b/samples/GeneralZip.Bench/Program.cs @@ -16,8 +16,7 @@ var warmups = args.Length > 1 && int.TryParse(args[1], out var w) ? w : 1; var runs = args.Length > 2 && int.TryParse(args[2], out var r) ? r : 3; -var nativeOk = GeneralZipNative.Initialize(); -Initialize(); +var nativeOk = NativeCoreLoader.TryLoad(); Console.WriteLine($"GeneralZip benchmark native={(nativeOk ? "available" : "missing (skips 7z/BZ2/XZ/ZSTD/LZ4)")}"); Console.WriteLine($"Runtime: {Environment.Version} OS: {Environment.OSVersion.VersionString} Logical cores: {Environment.ProcessorCount}"); diff --git a/samples/GeneralZip.Fixtures/Program.cs b/samples/GeneralZip.Fixtures/Program.cs index 2e89462..efa6bad 100644 --- a/samples/GeneralZip.Fixtures/Program.cs +++ b/samples/GeneralZip.Fixtures/Program.cs @@ -14,8 +14,7 @@ // 用法: dotnet run -c Release // ===================================================================== -Initialize(); // 幂等引导(using static 裸调用) -var nativeOk = GeneralZipNative.Initialize(); +var nativeOk = NativeCoreLoader.TryLoad(); // 引导已隐式完成,此处仅探测核心可用性 var repoRoot = FindRepoRoot(); Env.FixturesDir = Path.Combine(repoRoot, "samples", "fixtures"); diff --git a/samples/GeneralZip.Samples/Program.cs b/samples/GeneralZip.Samples/Program.cs index 8f2fc3d..c48fa9c 100644 --- a/samples/GeneralZip.Samples/Program.cs +++ b/samples/GeneralZip.Samples/Program.cs @@ -6,9 +6,8 @@ // 演示用口令:运行时拼接派生(避免源码硬编码;实际应用请从密钥管理服务/用户输入获取) static string DemoPassword(string label) => "GZ-" + label + "-Demo"; -// ===== 0) 启动引导:核心注册表 + 可选原生核心 ===== -Initialize(); // 幂等:确保内置格式/加密算法注册 -if (GeneralZipNative.Initialize()) // 可选:Rust 原生核心(7z/xz 能力;缺失时优雅降级) +// ===== 0) 原生核心状态展示(引导已隐式完成,无需手动调用) ===== +if (NativeCoreLoader.TryLoad()) // 可选:Rust 原生核心(7z/xz 能力;缺失时优雅降级) { Console.WriteLine($"[native core] v{GeneralZipNative.CoreVersion} loaded (7z/xz available)"); } @@ -31,9 +30,9 @@ await CompressAsync(src, zip, o => o.Level = CompressionLevelChoice.Optimal); Console.WriteLine($"[one-liner] compressed: {zip} ({new FileInfo(zip).Length}B, format {DetectFormat(zip)?.Name})"); -// ===== 1.5) 启动引导说明 ===== -// GeneralZip.Initialize() 为幂等引导入口(确保内置格式/算法注册,并自动加载内置 Rust 原生核心), -// 放在程序启动处调用一次即可;一行式 API 内部也会自动调用。 +// ===== 1.5) 引导说明 ===== +// GeneralZip 零初始化:内置格式/算法注册与 Rust 原生核心加载都在首次使用 API 时隐式完成 +//(Default 注册表装配时自动引导),核心缺失时优雅降级,纯托管能力不受影响。 // ===== 2) 国密 SM4 加密 ZIP(加密写 → 解密读) ===== var sm4Zip = Path.Combine(work, "secret-sm4.zip"); diff --git a/src/GeneralZip/ArchiveFormatRegistry.cs b/src/GeneralZip/ArchiveFormatRegistry.cs index df5b65b..0f691d0 100644 --- a/src/GeneralZip/ArchiveFormatRegistry.cs +++ b/src/GeneralZip/ArchiveFormatRegistry.cs @@ -30,7 +30,18 @@ internal GeneralZipFormatRegistry(bool populateBuiltins) } } - public static GeneralZipFormatRegistry Default { get; } = new(populateBuiltins: true); + public static GeneralZipFormatRegistry Default { get; } = CreateDefault(); + + /// + /// 默认注册表装配:内置格式注册后即触发原生引导(幂等)。 + /// 原生核心在位时追加 7z/RAR 并注入编解码桥接;缺失时静默降级,纯托管能力不受影响。 + /// + private static GeneralZipFormatRegistry CreateDefault() + { + var registry = new GeneralZipFormatRegistry(populateBuiltins: true); + global::GeneralZip.Native.GeneralZipNative.Bootstrap(registry); + return registry; + } /// 注册格式提供者(重复注册同名格式将覆盖旧的)。 public void Register(IArchiveFormat format) diff --git a/src/GeneralZip/Formats/Codecs/BuiltinCodecs.cs b/src/GeneralZip/Formats/Codecs/BuiltinCodecs.cs index 95d4b59..1aede19 100644 --- a/src/GeneralZip/Formats/Codecs/BuiltinCodecs.cs +++ b/src/GeneralZip/Formats/Codecs/BuiltinCodecs.cs @@ -14,17 +14,26 @@ internal static class NativeCodecRouter public static void EnsureCore() { // 通过桥接委托间接访问原生核心,保持编解码实现与 P/Invoke 细节的边界: - // GeneralZipNative.Initialize()(GeneralZip.Initialize() 已自动调用)注入 NativeBridge 的实现。 + // NativeBridge 的实现由隐式引导注入(Default 注册表装配时自动调用 Bootstrap)。 if (!NativeBridgeProbe.IsAvailableSafe()) { - throw new NativeCoreMissingException( - "This codec format requires the Rust native core (generalzip_native), which was not found next to the application. " + - "Place generalzip_native.dll/.so/.dylib in the app directory or under runtimes//native/, " + - "or re-save the archive as ZIP/TAR.GZ or another pure-managed format."); + // 兜底:默认注册表未被触碰时(自定义注册表场景)桥接尚未注入,显式拉起一次。 + global::GeneralZip.Native.GeneralZipNative.EnsureBridge(); + if (!NativeBridgeProbe.IsAvailableSafe()) + { + throw new NativeCoreMissingException( + "This codec format requires the Rust native core (generalzip_native), which was not found next to the application. " + + "Place generalzip_native.dll/.so/.dylib in the app directory or under runtimes//native/, " + + "or re-save the archive as ZIP/TAR.GZ or another pure-managed format."); + } } } - internal static bool IsCoreAvailable() => NativeBridge.IsAvailable?.Invoke() ?? false; + internal static bool IsCoreAvailable() + { + global::GeneralZip.Native.GeneralZipNative.EnsureBridge(); + return NativeBridgeProbe.IsAvailableSafe(); + } internal static void Compress(string codec, string source, string destination) { @@ -48,12 +57,12 @@ internal static void Decompress(string codec, string source, string destination) } /// -/// 原生核心桥接点:GeneralZip.Native 包在 Initialize 时注入实现。 +/// 原生核心桥接点:隐式引导(GeneralZipNative.Bootstrap)注入实现。 /// 核心包经此间接使用原生能力,保持"核心零 P/Invoke"的架构边界。 /// public static class NativeBridge { - /// 原生核心是否已加载。GeneralZipNative.Initialize 注入。 + /// 原生核心是否已加载。隐式引导注入。 public static Func? IsAvailable { get; set; } /// 整文件编解码:codec ∈ {bzip2, xz, zstd, lz4}。返回是否成功(false = 不支持该 codec)。 diff --git a/src/GeneralZip/GeneralZip.cs b/src/GeneralZip/GeneralZip.cs index 457374b..1031679 100644 --- a/src/GeneralZip/GeneralZip.cs +++ b/src/GeneralZip/GeneralZip.cs @@ -8,43 +8,18 @@ namespace GeneralZip; /// -/// 组件启动引导与门面入口(与组件同名)。 +/// 静态门面入口(与组件同名,零初始化:内置格式/加密算法与原生能力均在首次使用时隐式装配)。 /// -/// 完成内置格式/加密算法注册(幂等,可重复调用); -/// 随后即可使用一行式 API(/ 等), +/// 直接使用一行式 API(/ 等), /// 需要事件与精细控制时使用 / 。 /// /// -/// GeneralZip.Initialize(); /// await GeneralZip.CompressAsync(@"C:\data", @"D:\out.zip", /// o => { o.EncryptionAlgorithm = "SM4-GM"; o.Password = "p@ss"; }); /// /// public static class GeneralZip { - private static int _initialized; - - /// - /// 启动引导:确保内置格式提供者与加密算法已注册(幂等、线程安全)。 - /// 同时尝试加载内置 Rust 原生核心(7z/RAR/原生编解码);核心二进制缺失时静默降级, - /// 纯托管能力不受影响(可用 探测)。 - /// - public static void Initialize() - { - if (Interlocked.Exchange(ref _initialized, 1) == 1) - { - return; - } - - // 触发两个默认注册表的静态装配 - _ = GeneralZipFormatRegistry.Default; - _ = GeneralZipEncryptionRegistry.Default; - - // 原生引导(幂等;加载失败仅返回 false,不抛异常)。 - // global:: 限定:类名 GeneralZip 会遮蔽同名命名空间。 - _ = global::GeneralZip.Native.GeneralZipNative.Initialize(); - } - /// 组件版本。 public static Version Version => typeof(GeneralZip).Assembly.GetName().Version ?? new Version(0, 0, 0); diff --git a/src/GeneralZip/Native/GeneralZipNative.cs b/src/GeneralZip/Native/GeneralZipNative.cs index a16f154..6a8f783 100644 --- a/src/GeneralZip/Native/GeneralZipNative.cs +++ b/src/GeneralZip/Native/GeneralZipNative.cs @@ -5,61 +5,51 @@ namespace GeneralZip.Native; /// -/// GeneralZip 原生能力入口:一行注册 7z/RAR 提供者与原生编解码桥接。 -/// -/// GeneralZip.Initialize(); // 核心引导(幂等);已并入原生引导调用 -/// GeneralZipNative.Initialize(); // 原生引导(幂等,可显式调用;7z/RAR/bz2/xz/zstd/lz4) -/// +/// GeneralZip 原生能力入口:原生引导已隐式完成——首次访问任何 API 时, +/// 的装配会自动加载核心并注册 +/// 7z/RAR 提供者与原生编解码桥接(幂等;7z/RAR/bz2/xz/zstd/lz4),无需手动调用。 /// 原生核心缺失时安全降级:7z/RAR 不注册、原生编解码格式报明确异常;纯托管能力不受影响。 /// public static class GeneralZipNative { - private static readonly object Lock = new(); - private static bool _initialized; - - /// 加载原生核心并注册能力(幂等、线程安全)。返回核心是否可用。 - public static bool Initialize() + /// + /// 原生引导(幂等):加载原生核心;成功时向 注册 7z/RAR + /// (registry 为 null 则跳过注册)并注入编解码桥接委托,失败静默降级。 + /// 不访问 ——由调用方传入实例, + /// 避免静态构造器重入拿到半初始化的注册表。 + /// + internal static void Bootstrap(GeneralZipFormatRegistry? registry) { - lock (Lock) + if (!NativeCoreLoader.TryLoad()) { - if (_initialized) - { - return NativeCoreLoader.IsAvailable; - } - - _initialized = true; - - if (!NativeCoreLoader.TryLoad()) - { - return false; - } - - var registry = GeneralZipFormatRegistry.Default; - - // 7z 能力(sevenz-rust2,纯 Rust) - if ((NativeCoreLoader.Capabilities & NativeCoreLoader.CapabilitySevenZ) != 0) - { - registry.Register(new SevenZipNativeFormat()); - } + return; + } - // RAR 能力(unrar 封装,需 C++ 工具链构建的原生核心) - if ((NativeCoreLoader.Capabilities & NativeCoreLoader.CapabilityRar) != 0) - { - registry.Register(new RarNativeFormat()); - } + var caps = NativeCoreLoader.Capabilities; - // 单文件编解码桥接(bzip2 / xz / zstd / lz4):核心经 NativeBridge 间接调用 - var caps = NativeCoreLoader.Capabilities; - NativeBridge.IsAvailable = () => NativeCoreLoader.IsAvailable; - NativeBridge.Compress = (codec, src, dst) => - InvokeCodec(codec, src, dst, compress: true, caps); - NativeBridge.Decompress = (codec, src, dst) => - InvokeCodec(codec, src, dst, compress: false, caps); + // 7z 能力(sevenz-rust2,纯 Rust) + if (registry != null && (caps & NativeCoreLoader.CapabilitySevenZ) != 0) + { + registry.Register(new SevenZipNativeFormat()); + } - return true; + // RAR 能力(unrar 封装,需 C++ 工具链构建的原生核心) + if (registry != null && (caps & NativeCoreLoader.CapabilityRar) != 0) + { + registry.Register(new RarNativeFormat()); } + + // 单文件编解码桥接(bzip2 / xz / zstd / lz4):核心经 NativeBridge 间接调用 + NativeBridge.IsAvailable = () => NativeCoreLoader.IsAvailable; + NativeBridge.Compress = (codec, src, dst) => + InvokeCodec(codec, src, dst, compress: true, caps); + NativeBridge.Decompress = (codec, src, dst) => + InvokeCodec(codec, src, dst, compress: false, caps); } + /// 确保编解码桥接已注入(自定义注册表场景的兜底,幂等)。 + internal static void EnsureBridge() => Bootstrap(null); + private static bool InvokeCodec(string codec, string src, string dst, bool compress, uint caps) { // bzip2/xz/lz4/zstd 全部挂在 XZ 能力位之下(同一批纯 Rust 编解码器) diff --git a/tests/GeneralZip.Native.Tests/NativeCodecTests.cs b/tests/GeneralZip.Native.Tests/NativeCodecTests.cs index 2882b87..4d4e565 100644 --- a/tests/GeneralZip.Native.Tests/NativeCodecTests.cs +++ b/tests/GeneralZip.Native.Tests/NativeCodecTests.cs @@ -19,8 +19,7 @@ public class NativeCodecTests : IDisposable public NativeCodecTests() { Directory.CreateDirectory(_dir); - NativeCoreLoader.TryLoad(); - GeneralZipNative.Initialize(); // 注入原生编解码桥接(BZ2/XZ/ZSTD/LZ4 路由需要) + NativeCoreLoader.TryLoad(); // 预热核心探测(引导本身已随 Default 注册表装配隐式完成) } public void Dispose() diff --git a/tests/GeneralZip.Native.Tests/NativeCoreTests.cs b/tests/GeneralZip.Native.Tests/NativeCoreTests.cs index bacd9bc..d0d9d67 100644 --- a/tests/GeneralZip.Native.Tests/NativeCoreTests.cs +++ b/tests/GeneralZip.Native.Tests/NativeCoreTests.cs @@ -43,6 +43,21 @@ private bool SkipIfNoCore() private static string NewPassword() => "Nk" + Convert.ToBase64String(RandomNumberGenerator.GetBytes(8)).TrimEnd('='); + [Fact] + public void DefaultRegistry_ContainsNativeFormats_WithoutExplicitBootstrap() + { + if (SkipIfNoCore()) return; + + // 隐式引导回归防护:不调用任何引导 API,Default 注册表装配时应已注册 7z/RAR + //(RAR 按能力位协商,本地核心未开启 rar feature 时不注册) + var names = GeneralZipFormatRegistry.Default.Snapshot().Select(f => f.Info.Name).ToHashSet(StringComparer.OrdinalIgnoreCase); + Assert.Contains("7Z", names); + if ((NativeCoreLoader.Capabilities & NativeCoreLoader.CapabilityRar) != 0) + { + Assert.Contains("RAR", names); + } + } + [Fact] public void CoreLoads_WithSevenZCapability() { @@ -60,7 +75,6 @@ public void CoreLoads_WithSevenZCapability() public void SevenZip_Roundtrip_Plain() { if (SkipIfNoCore()) return; - GeneralZipNative.Initialize(); var src = Path.Combine(_dir, "src"); Directory.CreateDirectory(Path.Combine(src, "sub")); @@ -91,7 +105,6 @@ public void SevenZip_Roundtrip_Plain() public void SevenZip_Encrypted_Roundtrip_And_WrongPassword() { if (SkipIfNoCore()) return; - GeneralZipNative.Initialize(); var archive = Path.Combine(_dir, "enc.7z"); var password = NewPassword(); @@ -136,7 +149,6 @@ public void SevenZip_Encrypted_Roundtrip_And_WrongPassword() public void SevenZip_TestAll() { if (SkipIfNoCore()) return; - GeneralZipNative.Initialize(); var archive = Path.Combine(_dir, "t.7z"); using (var w = GeneralZipWriter.CreateNew(archive, formatName: "7Z")) @@ -154,7 +166,6 @@ public void SevenZip_TestAll() public void Xz_Compress_Decompress_Roundtrip() { if (SkipIfNoCore()) return; - GeneralZipNative.Initialize(); var src = Path.Combine(_dir, "in.txt"); var compressed = Path.Combine(_dir, "out.xz"); diff --git a/tests/GeneralZip.Native.Tests/NativeFixtureTests.cs b/tests/GeneralZip.Native.Tests/NativeFixtureTests.cs index f419603..af29502 100644 --- a/tests/GeneralZip.Native.Tests/NativeFixtureTests.cs +++ b/tests/GeneralZip.Native.Tests/NativeFixtureTests.cs @@ -61,7 +61,6 @@ public void NativeFixture_Roundtrips(string fileName, string formatName, bool si return; } - GeneralZipNative.Initialize(); // 显式 "\n" 拼接:内容字节不随源码行尾(CRLF/LF)漂移,保证跨平台一致 var noteText = string.Join("\n", @@ -131,7 +130,6 @@ public void SevenZ_Fixture_Decrypts_And_ContentMatches() return; } - GeneralZipNative.Initialize(); // 显式 "\n" 拼接:内容字节不随源码行尾(CRLF/LF)漂移,保证跨平台一致 var noteText = string.Join("\n",