Download src-server-go/cmd/patchmasterdata/main.go from TQHYG/Prism-Step-SRV: direct link, hf CLI and curl.
- Browser
- Download file 14.1 kB
-
https://huggingface.co/datasets/TQHYG/Prism-Step-SRV/resolve/main/src-server-go/cmd/patchmasterdata/main.go
- Command line
-
hf download hf://datasets/TQHYG/Prism-Step-SRV/src-server-go/cmd/patchmasterdata/main.go
-
curl -L -o main.go https://huggingface.co/datasets/TQHYG/Prism-Step-SRV/resolve/main/src-server-go/cmd/patchmasterdata/main.go
14.1 kB
| // Command patchmasterdata produces a patched copy of Android2/masterdata_encrypted | |
| // in which the end-of-service window is moved into the past. | |
| // | |
| // Why | |
| // --- | |
| // The game decides whether to show the "サービスを終了いたしました" notice by | |
| // comparing masterdata.moduleActivation start_date/end_date against the *device | |
| // clock*, inside IL2CPP native code. A Java (LSPlant) hook cannot reach that, and | |
| // permanently changing the device clock is intrusive. Rewriting the data we already | |
| // serve is the only device-independent lever. | |
| // | |
| // id=4 サービス終了 start 2026-07-31 12:00:00 end 2030-09-11 12:00:00 | |
| // id=5 返金受付 start 2026-07-31 12:00:00 end 2026-09-30 12:00:00 | |
| // | |
| // Both dates are 19 ASCII bytes, so the replacement is length-preserving and the | |
| // serialized layout (offsets, sizes) stays valid. | |
| // | |
| // Layout | |
| // ------ | |
| // masterdata_encrypted (UnityFS, served to the client) | |
| // └─ SerializedFile | |
| // └─ asset ".../appdata/masterdata_encrypted.bytes" (Rijndael-256 CBC) | |
| // └─ UnityFS (inner, LZ4) | |
| // └─ SerializedFile holding masterdata as Unity-serialized binary | |
| // int32 id | int32 len "name" | int32 len "desc" | |
| // | int32 len "start_date" | int32 len "end_date" | |
| // | |
| // The encrypted asset runs exactly to the end of the outer SerializedFile, so its | |
| // length is free: only the little-endian int32 length prefix in front of it has to | |
| // be rewritten. | |
| // | |
| // Usage: | |
| // go run ./cmd/patchmasterdata -workspace "D:\tqhyg\Project\Prism Step" | |
| package main | |
| import ( | |
| "encoding/binary" | |
| "encoding/hex" | |
| "flag" | |
| "fmt" | |
| "os" | |
| "path/filepath" | |
| "prismstep/server/internal/rijndael" | |
| "prismstep/server/internal/unityfs" | |
| ) | |
| const ( | |
| manifestKeyHex = "4a6d636454635737726d41764c686767665265714c787a377170324750777558" | |
| manifestIVHex = "597379693364674d46394b557556524a396a6a344c6766755764564737374543" | |
| oldStart = "2026-07-31 12:00:00" | |
| newStart = "2016-07-31 12:00:00" | |
| newEnd = "2016-09-11 12:00:00" | |
| ) | |
| func main() { | |
| ws := flag.String("workspace", ".", "workspace root") | |
| flag.Parse() | |
| src := filepath.Join(*ws, "Game Data", "step3_wget下載後遊戲原始資料", "DL", | |
| "rs.rst-game.com", "Android2", "masterdata_encrypted") | |
| dstDir := filepath.Join(*ws, "src-server-go", "patched", "Android2") | |
| dst := filepath.Join(dstDir, "masterdata_encrypted") | |
| key, _ := hex.DecodeString(manifestKeyHex) | |
| iv, _ := hex.DecodeString(manifestIVHex) | |
| raw, err := os.ReadFile(src) | |
| check(err, "read source") | |
| outer, err := unityfs.Parse(raw) | |
| check(err, "parse outer bundle") | |
| outerData, err := outer.Data() | |
| check(err, "decompress outer bundle") | |
| fmt.Printf("outer bundle : v%d blocks=%d data=%d\n", | |
| outer.Version, len(outer.Blocks), len(outerData)) | |
| assetOff, assetCT, innerTotal, err := findEncryptedAsset(outerData, key, iv) | |
| check(err, "locate encrypted asset") | |
| fmt.Printf("encrypted asset : offset=%d len=%d inner_total=%d\n", assetOff, len(assetCT), innerTotal) | |
| innerRaw := rijndael.RawDecrypt(assetCT, key, iv)[:innerTotal] | |
| inner, err := unityfs.Parse(innerRaw) | |
| check(err, "parse inner bundle") | |
| innerData, err := inner.Data() | |
| check(err, "decompress inner bundle") | |
| fmt.Printf("inner bundle : len=%d data=%d\n", len(innerRaw), len(innerData)) | |
| // ---- length-preserving patch ---- | |
| startStr := indexOf(innerData, append(append([]byte{0x13, 0, 0, 0}, []byte(oldStart)...), 0)) | |
| if startStr < 0 { | |
| fatal("id=4 start_date record not found") | |
| } | |
| startStr += 4 | |
| endLenOff := startStr + len(oldStart) + 1 | |
| endStr := endLenOff + 4 | |
| if binary.LittleEndian.Uint32(innerData[endLenOff:endStr]) != 19 { | |
| fatal(fmt.Sprintf("unexpected end_date length prefix %x", innerData[endLenOff:endStr])) | |
| } | |
| fmt.Printf("patch start_date : %q -> %q @%d\n", string(innerData[startStr:startStr+19]), newStart, startStr) | |
| fmt.Printf("patch end_date : %q -> %q @%d\n", string(innerData[endStr:endStr+19]), newEnd, endStr) | |
| patched := make([]byte, len(innerData)) | |
| copy(patched, innerData) | |
| copy(patched[startStr:], newStart) | |
| copy(patched[endStr:], newEnd) | |
| // ---- rebuild inner bundle (single stored block) ---- | |
| in := inner.Nodes[0] | |
| newInner := unityfs.Build(inner.Version, inner.UnityVersion, inner.UnityRevision, | |
| in.Path, in.Flags, patched, inner.Flags) | |
| fmt.Printf("inner rebuilt : %d -> %d bytes\n", len(innerRaw), len(newInner)) | |
| newCT := rijndael.CBCEncryptZeroPad(newInner, key, iv) | |
| // ---- splice into the outer SerializedFile ---- | |
| if assetOff+len(assetCT) != len(outerData) { | |
| fatal(fmt.Sprintf("asset is not at the end of outer data (%d+%d != %d)", | |
| assetOff, len(assetCT), len(outerData))) | |
| } | |
| prefixOff := assetOff - 4 | |
| if int(binary.LittleEndian.Uint32(outerData[prefixOff:assetOff])) != len(assetCT) { | |
| fatal("asset length prefix mismatch, refusing to patch") | |
| } | |
| newOuterData := make([]byte, 0, prefixOff+4+len(newCT)) | |
| newOuterData = append(newOuterData, outerData[:prefixOff]...) | |
| newOuterData = binary.LittleEndian.AppendUint32(newOuterData, uint32(len(newCT))) | |
| newOuterData = append(newOuterData, newCT...) | |
| // The SerializedFile object table stores byteStart/byteSize for every object. | |
| // We changed the last object's length, so its byteSize must be bumped by the | |
| // same delta or Unity rejects the file (it bounds the TextAsset read by it). | |
| delta := int64(len(newOuterData)) - int64(len(outerData)) | |
| // SerializedFile header carries its own fileSize; bump it first. | |
| if fileSizeOff := findFileSizeField(outerData, int64(len(outerData))); fileSizeOff >= 0 { | |
| binary.BigEndian.PutUint64(newOuterData[fileSizeOff:], uint64(len(newOuterData))) | |
| fmt.Printf("serializedfile : fileSize @%d %d -> %d\n", fileSizeOff, len(outerData), len(newOuterData)) | |
| } else { | |
| fmt.Println("serializedfile : fileSize field not found (header may be non-standard)") | |
| } | |
| // locate the SerializedFile object table; meta starts right after the 48-byte header | |
| metaStart := 48 | |
| dataOffset := len(outerData) | |
| if v := int(binary.BigEndian.Uint64(outerData[32:])); v > metaStart && v <= len(outerData) { | |
| dataOffset = v | |
| } | |
| metaEnd := metaStart + int(binary.BigEndian.Uint64(outerData[16:])) | |
| if metaEnd > len(outerData) { | |
| metaEnd = dataOffset | |
| } | |
| sizeOff, _, objCount, found := findObjectTable(outerData, metaStart, metaEnd, dataOffset) | |
| if !found { | |
| dbgDumpHeader(outerData, prefixOff) | |
| fatal("could not locate SerializedFile object table") | |
| } | |
| oldSize := int64(binary.BigEndian.Uint32(outerData[sizeOff:])) | |
| binary.BigEndian.PutUint32(newOuterData[sizeOff:], uint32(oldSize+delta)) | |
| fmt.Printf("object table : %d objects, meta=[%d,%d) dataOffset=%d\n", objCount, metaStart, dataOffset, dataOffset) | |
| fmt.Printf("object byteSize : @%d %d -> %d (%+d)\n", sizeOff, oldSize, oldSize+delta, delta) | |
| on := outer.Nodes[0] | |
| newOuter := unityfs.Build(outer.Version, outer.UnityVersion, outer.UnityRevision, | |
| on.Path, on.Flags, newOuterData, outer.Flags) | |
| fmt.Printf("outer rebuilt : %d -> %d bytes\n", len(raw), len(newOuter)) | |
| // ---- verify by round-trip ---- | |
| vo, err := unityfs.Parse(newOuter) | |
| check(err, "verify: parse outer") | |
| vod, err := vo.Data() | |
| check(err, "verify: decompress outer") | |
| vOff, vCT, vTotal, err := findEncryptedAsset(vod, key, iv) | |
| check(err, "verify: locate asset") | |
| if vOff != prefixOff+4 { | |
| fatal(fmt.Sprintf("asset moved: %d != %d", vOff, prefixOff+4)) | |
| } | |
| vInner := rijndael.RawDecrypt(vCT, key, iv)[:vTotal] | |
| vi, err := unityfs.Parse(vInner) | |
| check(err, "verify: parse inner") | |
| vid, err := vi.Data() | |
| check(err, "verify: decompress inner") | |
| if countOf(vid, []byte(oldStart)) != countOf(innerData, []byte(oldStart))-1 { | |
| fatal("expected exactly one start_date occurrence to change") | |
| } | |
| if countOf(vid, []byte(newStart)) != 1 || countOf(vid, []byte(newEnd)) != 1 { | |
| fatal("patched dates missing") | |
| } | |
| if len(vid) != len(innerData) { | |
| fatal("inner data length changed") | |
| } | |
| check(os.MkdirAll(dstDir, 0o755), "mkdir patched") | |
| check(os.WriteFile(dst, newOuter, 0o644), "write patched bundle") | |
| overrides := fmt.Sprintf("{\n \"masterdata_encrypted\": %d\n}\n", len(newOuter)) | |
| check(os.WriteFile(filepath.Join(*ws, "src-server-go", "patched", "overrides.json"), []byte(overrides), 0o644), "write overrides") | |
| fmt.Println() | |
| fmt.Printf("OK -> %s\n", dst) | |
| fmt.Printf(" size %d (source %d)\n", len(newOuter), len(raw)) | |
| fmt.Println(" overrides.json written (manifest size override)") | |
| } | |
| // findEncryptedAsset scans for a 32-byte block that decrypts to a UnityFS header. | |
| func findEncryptedAsset(data, key, iv []byte) (off int, ct []byte, total int64, err error) { | |
| block := rijndael.NewRijndael256(key) | |
| for o := 0; o+rijndael.BlockSize <= len(data); o++ { | |
| dec := block.DecryptBlock(data[o : o+rijndael.BlockSize]) | |
| head := make([]byte, 8) | |
| for i := range head { | |
| head[i] = dec[i] ^ iv[i] | |
| } | |
| if string(head) != "UnityFS\x00" { | |
| continue | |
| } | |
| need := 256 | |
| ctLen := ((need + 31) / 32) * 32 | |
| if o+ctLen > len(data) { | |
| return 0, nil, 0, fmt.Errorf("truncated asset header") | |
| } | |
| head2 := rijndael.RawDecrypt(data[o:o+ctLen], key, iv) | |
| t, e := innerTotalSize(head2) | |
| if e != nil { | |
| return 0, nil, 0, e | |
| } | |
| full := int(((t + 31) / 32) * 32) | |
| if o+full > len(data) { | |
| return 0, nil, 0, fmt.Errorf("asset overruns data") | |
| } | |
| return o, data[o : o+full], t, nil | |
| } | |
| return 0, nil, 0, fmt.Errorf("encrypted inner bundle not found") | |
| } | |
| // innerTotalSize reads total_size from a decrypted UnityFS header. | |
| func innerTotalSize(b []byte) (int64, error) { | |
| p := 0 | |
| readCStr := func() (string, error) { | |
| for i := p; i < len(b); i++ { | |
| if b[i] == 0 { | |
| s := string(b[p:i]) | |
| p = i + 1 | |
| return s, nil | |
| } | |
| } | |
| return "", fmt.Errorf("bad cstring") | |
| } | |
| if _, err := readCStr(); err != nil { // "UnityFS" | |
| return 0, err | |
| } | |
| p += 4 // version | |
| if _, err := readCStr(); err != nil { | |
| return 0, err | |
| } | |
| if _, err := readCStr(); err != nil { | |
| return 0, err | |
| } | |
| if p+8 > len(b) { | |
| return 0, fmt.Errorf("short header") | |
| } | |
| return int64(binary.BigEndian.Uint64(b[p:])), nil | |
| } | |
| func indexOf(h, n []byte) int { | |
| for i := 0; i+len(n) <= len(h); i++ { | |
| match := true | |
| for j := range n { | |
| if h[i+j] != n[j] { | |
| match = false | |
| break | |
| } | |
| } | |
| if match { | |
| return i | |
| } | |
| } | |
| return -1 | |
| } | |
| // findObjectTable locates the SerializedFile object table (m_Objects). | |
| // | |
| // IMPORTANT: the SerializedFile *header* is big-endian, but the metadata that | |
| // follows it (type trees, m_Objects) is little-endian in this file - verified by | |
| // decoding typeCount=2, classID=142 (AssetBundle) and nodeCount=58 in LE. | |
| // | |
| // We validate the whole table rather than hunting a single number: objectCount | |
| // followed by `count` entries, each (pathID, byteStart, byteSize), in one of the | |
| // historical widths. A candidate is accepted only if every entry is sane, entries | |
| // are ascending and non-overlapping, and the last one reaches the end of data. | |
| func findObjectTable(data []byte, metaStart, metaEnd, dataOffset int) (sizeOff, sizeW int, count int, ok bool) { | |
| type layout struct{ entryW, startOff, startW, sizeOff int } | |
| layouts := []layout{ | |
| {20, 8, 8, 16}, // v22+ : pathID i64, byteStart i64, byteSize u32 | |
| {16, 8, 4, 12}, // v17+ : pathID i64, byteStart u32, byteSize u32 | |
| {12, 4, 4, 8}, // older: pathID i32, byteStart u32, byteSize u32 | |
| } | |
| dataLen := len(data) | |
| if metaEnd > dataLen { | |
| metaEnd = dataLen | |
| } | |
| best := -1 | |
| for _, bo := range []binary.ByteOrder{binary.LittleEndian, binary.BigEndian} { | |
| for _, l := range layouts { | |
| for o := metaStart; o+4+l.entryW <= metaEnd; o++ { | |
| n := int(bo.Uint32(data[o:])) | |
| if n < 1 || n > 64 { | |
| continue | |
| } | |
| if o+4+n*l.entryW > metaEnd { | |
| continue | |
| } | |
| good := true | |
| var prevEnd int64 | |
| lastSizeOff := -1 | |
| for k := 0; k < n; k++ { | |
| base := o + 4 + k*l.entryW | |
| var start int64 | |
| if l.startW == 8 { | |
| start = int64(bo.Uint64(data[base+l.startOff:])) | |
| } else { | |
| start = int64(bo.Uint32(data[base+l.startOff:])) | |
| } | |
| size := int64(bo.Uint32(data[base+l.sizeOff:])) | |
| if start < int64(dataOffset) || size <= 0 || start+size > int64(dataLen) { | |
| good = false | |
| break | |
| } | |
| if k > 0 && start < prevEnd { | |
| good = false | |
| break | |
| } | |
| prevEnd = start + size | |
| lastSizeOff = base + l.sizeOff | |
| } | |
| if !good || int64(dataLen)-prevEnd > 16 { | |
| continue | |
| } | |
| score := o | |
| if score > best { | |
| best = score | |
| sizeOff = lastSizeOff | |
| sizeW = 4 | |
| count = n | |
| ok = true | |
| } | |
| } | |
| } | |
| } | |
| return | |
| } | |
| // findFileSizeField returns the offset of the SerializedFile header `fileSize` | |
| // field: the u64 equal to the current data length near the header start. | |
| func findFileSizeField(data []byte, dataLen int64) int { | |
| limit := 128 | |
| if limit > len(data)-8 { | |
| limit = len(data) - 8 | |
| } | |
| for o := 0; o < limit; o++ { | |
| if int64(binary.BigEndian.Uint64(data[o:])) == dataLen { | |
| return o | |
| } | |
| } | |
| return -1 | |
| } | |
| // dbgDumpHeader prints the header words when the object table cannot be located. | |
| func dbgDumpHeader(data []byte, assetStart int) { | |
| fmt.Println("--- debug: header ---") | |
| fmt.Printf("data len=%d assetStart=%d\n", len(data), assetStart) | |
| n := 112 | |
| if n > len(data) { | |
| n = len(data) | |
| } | |
| for i := 0; i < n; i += 16 { | |
| end := i + 16 | |
| if end > n { | |
| end = n | |
| } | |
| fmt.Printf("%04x % x\n", i, data[i:end]) | |
| } | |
| } | |
| func countOf(h, n []byte) int { c := 0 | |
| for i := 0; i+len(n) <= len(h); i++ { | |
| match := true | |
| for j := range n { | |
| if h[i+j] != n[j] { | |
| match = false | |
| break | |
| } | |
| } | |
| if match { | |
| c++ | |
| i += len(n) - 1 | |
| } | |
| } | |
| return c | |
| } | |
| func fatal(msg string) { | |
| fmt.Fprintf(os.Stderr, "[fatal] %s\n", msg) | |
| os.Exit(1) | |
| } | |
| func check(err error, what string) { | |
| if err != nil { | |
| fatal(what + ": " + err.Error()) | |
| } | |
| } | |