Repository navigation
Conversation
Rewrites an existing Unity Archive into a new one with the same files and a chosen block layout: block size, a cap on the compressed block size, splitting at file boundaries, file alignment, block alignment (format v9) and a minimum compression ratio below which blocks are stored uncompressed. The output keeps the input's format and Unity version and is verified by reading both archives back through UnityFileSystemApi. AI-assisted (Claude); reviewed and tested locally with dotnet test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #64.
Adds
archive repack, which rewrites an existing Unity Archive into a new one with the same files and a block layout you choose. It covers the parameters listed in the issue:--block-size: max uncompressed bytes per block (default 128 KB, like Unity)--max-compressed-block-size: cap on the stored size of a block; a block that would compress to more is shortened (binary search on the prefix length)--split-at-file-boundaries: start a new block for each file instead of spanning--file-alignment: padding between files in the uncompressed data--min-compression-ratio: store a block uncompressed unless it compresses by at least this ratio-c None|Lz4|Lz4HC, and--block-alignmentfor format v9 archives (positions blocks on disk, like the 6.7 writer does with 16 bytes)The content is read through UnityFileSystemApi, so LZMA inputs work too. The output keeps the input's format version, Unity version and node flags, and the metadata is written LZ4HC right after the header. The header/metadata serialization lives in a new
UnityBinaryFormat/ArchiveWriter.cs, the mirror ofArchiveDetector.By default the output is verified: both archives are read back through UnityFileSystemApi and every file is compared by SHA-256; on a mismatch the output is deleted. One thing I ran into: with both archives mounted at the same time (different mount points, same file names), reads of the repacked archive returned the original's bytes, so a tampered file passed. The check now mounts them one after the other, and there is a test that flips a byte and expects the failure.
The command prints a before/after report (size, block count, blocks stored raw, largest block, and a "read amplification" figure: uncompressed bytes of the blocks you have to touch to load each file on its own, divided by total file size). Some numbers on the repo's test data:
LeadingEdgeBuilds/AssetBundlesLz4/scenes(v9, Unity 6.7, 362,352 bytes, read amp 1.00x):AssetBundles/2023.1.0a16/assetbundle(LZMA, 2,211,569 bytes, 3,781,435 uncompressed): LZ4HC 128 KB gives 3,015,300 bytes; with LZ4 (fast), 20 of 29 blocks fall back to uncompressed because the texture data does not compress.These are deterministic sizes from the CLI (
-f Json), not timings. All outputs above verified.Testing:
dotnet build -c Release(0 warnings) anddotnet teston Apple Silicon arm64 (macOS): Analyzer.Tests 67, UnityFileSystem.Tests 442 (+10 skipped), UnityDataTool.Tests 380 including 23 new tests inArchiveRepackTests.cs. The tests cover each option on v8 and v9 inputs (I also repacked the 2019.4 v6 bundle by hand), check the layout throughArchiveDetector, compare content independently viaarchive extract, check thatdumpoutput is identical before and after, and cover the error cases.dotnet format whitespace --verify-no-changesis clean on the changed files.I have not loaded a repacked bundle in a Player; the docs note to test on the target platform before shipping one.
This was written with AI assistance (Claude). I reviewed the code and ran the tests above locally.