Download src-server-go/internal/unityfs/unityfs.go from TQHYG/Prism-Step-SRV: direct link, hf CLI and curl.
- Browser
- Download file 7.11 kB
-
https://huggingface.co/datasets/TQHYG/Prism-Step-SRV/resolve/main/src-server-go/internal/unityfs/unityfs.go
- Command line
-
hf download hf://datasets/TQHYG/Prism-Step-SRV/src-server-go/internal/unityfs/unityfs.go
-
curl -L -o unityfs.go https://huggingface.co/datasets/TQHYG/Prism-Step-SRV/resolve/main/src-server-go/internal/unityfs/unityfs.go
7.11 kB
| // Package unityfs is a minimal reader/writer for UnityFS asset bundles. | |
| // | |
| // Only what the PrismStep offline tooling needs: | |
| // - read v6/v7/v8 headers | |
| // - block compression: 0 = none, 2/3 = LZ4 (LZMA is not supported) | |
| // - write a bundle using a single STORED block | |
| // | |
| // Used by cmd/patchmasterdata and available for future bundle work. | |
| package unityfs | |
| import ( | |
| "crypto/md5" | |
| "encoding/binary" | |
| "errors" | |
| "fmt" | |
| ) | |
| // Block is one entry of the blocksInfo table. | |
| type Block struct { | |
| Uncompressed uint32 | |
| Compressed uint32 | |
| Flags uint16 | |
| } | |
| // Node is one entry of the node (file) table. | |
| type Node struct { | |
| Offset int64 | |
| Size int64 | |
| Flags uint32 | |
| Path string | |
| } | |
| // Bundle is a parsed UnityFS file. | |
| type Bundle struct { | |
| Version uint32 | |
| UnityVersion string | |
| UnityRevision string | |
| TotalSize int64 | |
| Flags uint32 | |
| DataHash [16]byte | |
| Blocks []Block | |
| Nodes []Node | |
| // DataStart is the file offset of the first data block. | |
| DataStart int | |
| raw []byte | |
| } | |
| func (b *Bundle) Raw() []byte { return b.raw } | |
| // ---------------------------------------------------------------- little reader | |
| type reader struct { | |
| d []byte | |
| p int | |
| } | |
| func (r *reader) cstr() (string, error) { | |
| for i := r.p; i < len(r.d); i++ { | |
| if r.d[i] == 0 { | |
| s := string(r.d[r.p:i]) | |
| r.p = i + 1 | |
| return s, nil | |
| } | |
| } | |
| return "", errors.New("unterminated string") | |
| } | |
| func (r *reader) u16() uint16 { v := binary.BigEndian.Uint16(r.d[r.p:]); r.p += 2; return v } | |
| func (r *reader) u32() uint32 { v := binary.BigEndian.Uint32(r.d[r.p:]); r.p += 4; return v } | |
| func (r *reader) i64() int64 { v := int64(binary.BigEndian.Uint64(r.d[r.p:])); r.p += 8; return v } | |
| func (r *reader) take(n int) []byte { | |
| b := r.d[r.p : r.p+n] | |
| r.p += n | |
| return b | |
| } | |
| func (r *reader) align16() { r.p = (r.p + 15) &^ 15 } | |
| // -------------------------------------------------------------------- LZ4 block | |
| // lz4Decompress decodes one LZ4 block (no frame header). | |
| func lz4Decompress(src []byte, expected int) ([]byte, error) { | |
| dst := make([]byte, 0, expected) | |
| i, n := 0, len(src) | |
| for i < n { | |
| token := src[i] | |
| i++ | |
| lit := int(token >> 4) | |
| if lit == 15 { | |
| for { | |
| b := int(src[i]) | |
| i++ | |
| lit += b | |
| if b != 255 { | |
| break | |
| } | |
| } | |
| } | |
| if i+lit > n { | |
| return nil, errors.New("lz4: literal overrun") | |
| } | |
| dst = append(dst, src[i:i+lit]...) | |
| i += lit | |
| if i >= n { | |
| break | |
| } | |
| off := int(src[i]) | int(src[i+1])<<8 | |
| i += 2 | |
| if off == 0 || off > len(dst) { | |
| return nil, errors.New("lz4: bad offset") | |
| } | |
| ml := int(token & 0x0F) | |
| if ml == 15 { | |
| for { | |
| b := int(src[i]) | |
| i++ | |
| ml += b | |
| if b != 255 { | |
| break | |
| } | |
| } | |
| } | |
| ml += 4 | |
| start := len(dst) - off | |
| for k := 0; k < ml; k++ { | |
| dst = append(dst, dst[start+k]) | |
| } | |
| } | |
| if expected > 0 && len(dst) != expected { | |
| return nil, fmt.Errorf("lz4: got %d want %d", len(dst), expected) | |
| } | |
| return dst, nil | |
| } | |
| func decompress(kind uint16, data []byte, expected int) ([]byte, error) { | |
| switch kind { | |
| case 0: | |
| return data, nil | |
| case 2, 3: // LZ4, LZ4HC | |
| return lz4Decompress(data, expected) | |
| default: | |
| return nil, fmt.Errorf("unityfs: unsupported compression %d", kind) | |
| } | |
| } | |
| // ----------------------------------------------------------------------- parse | |
| // Parse reads a UnityFS bundle. | |
| func Parse(raw []byte) (*Bundle, error) { | |
| r := &reader{d: raw} | |
| sig, err := r.cstr() | |
| if err != nil || sig != "UnityFS" { | |
| return nil, fmt.Errorf("unityfs: bad signature %q", sig) | |
| } | |
| b := &Bundle{raw: raw} | |
| b.Version = r.u32() | |
| b.UnityVersion, _ = r.cstr() | |
| b.UnityRevision, _ = r.cstr() | |
| b.TotalSize = r.i64() | |
| ciSize := r.u32() | |
| uiSize := r.u32() | |
| b.Flags = r.u32() | |
| if b.Version >= 7 { | |
| r.align16() | |
| } | |
| biRaw := r.take(int(ciSize)) | |
| bi := biRaw | |
| if ciSize != uiSize { | |
| bi, err = decompress(uint16(b.Flags&0x3F), biRaw, int(uiSize)) | |
| if err != nil { | |
| return nil, err | |
| } | |
| } | |
| br := &reader{d: bi} | |
| copy(b.DataHash[:], br.take(16)) | |
| blockCount := br.u32() | |
| b.Blocks = make([]Block, blockCount) | |
| for i := range b.Blocks { | |
| b.Blocks[i].Uncompressed = br.u32() | |
| b.Blocks[i].Compressed = br.u32() | |
| b.Blocks[i].Flags = br.u16() | |
| } | |
| nodeCount := br.u32() | |
| b.Nodes = make([]Node, nodeCount) | |
| for i := range b.Nodes { | |
| b.Nodes[i].Offset = br.i64() | |
| b.Nodes[i].Size = br.i64() | |
| b.Nodes[i].Flags = br.u32() | |
| b.Nodes[i].Path, _ = br.cstr() | |
| } | |
| if b.Version >= 7 && b.Flags&0x200 != 0 { | |
| r.align16() | |
| } | |
| b.DataStart = r.p | |
| return b, nil | |
| } | |
| // Data returns the fully decompressed payload (all blocks concatenated). | |
| func (b *Bundle) Data() ([]byte, error) { | |
| pos := b.DataStart | |
| out := make([]byte, 0, b.TotalSize) | |
| for _, blk := range b.Blocks { | |
| if pos+int(blk.Compressed) > len(b.raw) { | |
| return nil, errors.New("unityfs: block overruns file") | |
| } | |
| chunk := b.raw[pos : pos+int(blk.Compressed)] | |
| pos += int(blk.Compressed) | |
| d, err := decompress(blk.Flags&0x3F, chunk, int(blk.Uncompressed)) | |
| if err != nil { | |
| return nil, err | |
| } | |
| out = append(out, d...) | |
| } | |
| return out, nil | |
| } | |
| // ----------------------------------------------------------------------- write | |
| // Build emits a bundle with one STORED block holding data. | |
| func Build(version uint32, unityVersion, unityRevision, nodePath string, nodeFlags uint32, | |
| data []byte, baseFlags uint32) []byte { | |
| blocksInfo := make([]byte, 0, 64+len(nodePath)) | |
| sum := md5Sum(data) | |
| blocksInfo = append(blocksInfo, sum[:]...) | |
| blocksInfo = binary.BigEndian.AppendUint32(blocksInfo, 1) // block count | |
| blocksInfo = binary.BigEndian.AppendUint32(blocksInfo, uint32(len(data))) | |
| blocksInfo = binary.BigEndian.AppendUint32(blocksInfo, uint32(len(data))) | |
| blocksInfo = binary.BigEndian.AppendUint16(blocksInfo, 0) // flags: stored | |
| blocksInfo = binary.BigEndian.AppendUint32(blocksInfo, 1) // node count | |
| blocksInfo = binary.BigEndian.AppendUint64(blocksInfo, 0) | |
| blocksInfo = binary.BigEndian.AppendUint64(blocksInfo, uint64(len(data))) | |
| blocksInfo = binary.BigEndian.AppendUint32(blocksInfo, nodeFlags) // preserve verbatim | |
| blocksInfo = append(blocksInfo, nodePath...) | |
| blocksInfo = append(blocksInfo, 0) | |
| flags := baseFlags &^ 0x3F // blocksInfo not compressed | |
| flags &^= 0x200 // no alignment after blocksInfo | |
| hdr := make([]byte, 0, 64) | |
| hdr = append(hdr, []byte("UnityFS\x00")...) | |
| hdr = binary.BigEndian.AppendUint32(hdr, version) | |
| hdr = append(hdr, []byte(unityVersion)...) | |
| hdr = append(hdr, 0) | |
| hdr = append(hdr, []byte(unityRevision)...) | |
| hdr = append(hdr, 0) | |
| totalOff := len(hdr) | |
| hdr = binary.BigEndian.AppendUint64(hdr, 0) // patched below | |
| hdr = binary.BigEndian.AppendUint32(hdr, uint32(len(blocksInfo))) | |
| hdr = binary.BigEndian.AppendUint32(hdr, uint32(len(blocksInfo))) | |
| hdr = binary.BigEndian.AppendUint32(hdr, flags) | |
| if version >= 7 { | |
| for len(hdr)%16 != 0 { | |
| hdr = append(hdr, 0) | |
| } | |
| } | |
| out := make([]byte, 0, len(hdr)+len(blocksInfo)+len(data)) | |
| out = append(out, hdr...) | |
| out = append(out, blocksInfo...) | |
| out = append(out, data...) | |
| binary.BigEndian.PutUint64(out[totalOff:], uint64(len(out))) | |
| return out | |
| } | |
| // md5Sum computes the UnityFS uncompressedDataHash. | |
| func md5Sum(b []byte) [16]byte { | |
| return md5.Sum(b) | |
| } | |