Repository navigation
Expand file tree
/
Copy pathextract-rcc.ts
More file actions
178 lines (152 loc) · 5.94 KB
/
Copy pathextract-rcc.ts
File metadata and controls
178 lines (152 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* Extract files from a Qt binary resource container (.rcc).
*
* The .rcc format stores files in three sections:
* - Tree: hierarchical node descriptors
* - Names: UTF-16BE strings with 2-byte length + 4-byte hash prefix
* - Data: raw or zlib-compressed file contents
*
* Usage:
* npx tsx extract-rcc.ts --rcc=client-files/<version>/graphics_resources.rcc
* npx tsx extract-rcc.ts --rcc=client-files/15.24.845104/graphics_resources.rcc --out=client-files/15.24.845104/resource-export
*
* Output defaults to client-files/<version>/resource-export/
*/
import * as fs from 'fs';
import * as path from 'path';
import * as zlib from 'zlib';
// ── Args ───────────────────────────────────────────────────────────────────────
const args = Object.fromEntries(
process.argv.slice(2)
.filter(a => a.startsWith('--'))
.map(a => a.slice(2).split('=') as [string, string])
);
if (!args['rcc']) {
console.error('Usage: npx tsx extract-rcc.ts --rcc=<path/to/graphics_resources.rcc> [--out=<output-dir>]');
process.exit(1);
}
const RCC_PATH = path.resolve(args['rcc']);
const OUT_DIR = path.resolve(
args['out'] ?? path.join(path.dirname(RCC_PATH), 'resource-export')
);
const MAGIC = Buffer.from('qres');
const FLAG_COMPRESSED = 0x01;
const FLAG_DIRECTORY = 0x02;
const FLAG_COMPRESSED_ZSTD = 0x04;
interface RccHeader {
version: number;
treeOff: number;
dataOff: number;
namesOff: number;
nodeSize: number;
}
function readHeader(buf: Buffer): RccHeader {
if (!buf.subarray(0, 4).equals(MAGIC)) {
throw new Error('Not a valid .rcc file (missing "qres" magic)');
}
const version = buf.readUInt32BE(4);
const treeOff = buf.readUInt32BE(8);
const dataOff = buf.readUInt32BE(12);
const namesOff = buf.readUInt32BE(16);
if (version < 1 || version > 3) {
throw new Error(`Unsupported .rcc format version: ${version}`);
}
const nodeSize = version >= 2 ? 22 : 14;
return { version, treeOff, dataOff, namesOff, nodeSize };
}
function readName(buf: Buffer, namesOff: number, nameOffset: number): string {
const base = namesOff + nameOffset;
const length = buf.readUInt16BE(base); // char count (not bytes)
// skip 4-byte hash at base+2
const strStart = base + 6;
const raw = buf.subarray(strStart, strStart + length * 2);
return raw.toString('utf16le').split('').map((_, i) =>
// utf16le on LE platform, but the data is BE — swap bytes per char
String.fromCharCode(raw.readUInt16BE(i * 2))
).join('');
}
interface FileEntry {
path: string;
dataOff: number; // byte offset into data section (past the 4-byte size field)
storedSize: number; // bytes in the data section (compressed or raw)
realSize: number; // uncompressed size as stated in data section header
compressed: boolean;
zstd: boolean;
}
function walkTree(buf: Buffer, hdr: RccHeader): FileEntry[] {
const entries: FileEntry[] = [];
function visit(nodeIdx: number, parts: string[]): void {
const off = hdr.treeOff + nodeIdx * hdr.nodeSize;
const nameOff = buf.readUInt32BE(off);
const flags = buf.readUInt16BE(off + 4);
const name = readName(buf, hdr.namesOff, nameOff);
if (flags & FLAG_DIRECTORY) {
const childCount = buf.readUInt32BE(off + 6);
const firstChild = buf.readUInt32BE(off + 10);
for (let i = 0; i < childCount; i++) {
visit(firstChild + i, parts.concat(name));
}
} else {
const dataOffset = buf.readUInt32BE(off + 10);
// The data section entry: [4-byte uncompressed-size][payload]
const realSize = buf.readUInt32BE(hdr.dataOff + dataOffset);
// We don't know stored size here... pass offset+4 for the payload start
const filePath = parts.slice(1).concat(name).join('/'); // drop leading ''
entries.push({
path: filePath,
dataOff: hdr.dataOff + dataOffset + 4,
storedSize: 0, // filled below
realSize,
compressed: !!(flags & FLAG_COMPRESSED),
zstd: !!(flags & FLAG_COMPRESSED_ZSTD),
});
}
}
visit(0, []);
return entries;
}
function extractData(buf: Buffer, entry: FileEntry): Buffer {
if (entry.zstd) {
throw new Error(`zstd compression not supported for: ${entry.path}`);
}
if (entry.compressed) {
// Qt uses qCompress which prepends an extra 4-byte big-endian uncompressed
// size before the standard zlib stream. Skip those 4 bytes before inflating.
const payload = buf.subarray(entry.dataOff + 4);
return zlib.inflateSync(payload);
}
// Uncompressed: realSize bytes follow the 4-byte header
return buf.subarray(entry.dataOff, entry.dataOff + entry.realSize);
}
function main(): void {
if (!fs.existsSync(RCC_PATH)) {
console.error(`File not found: ${RCC_PATH}`);
process.exit(1);
}
console.log(`Reading ${RCC_PATH} ...`);
const buf = fs.readFileSync(RCC_PATH);
console.log(` ${(buf.length / 1024 / 1024).toFixed(2)} MB`);
const hdr = readHeader(buf);
console.log(` Format version ${hdr.version}, ${hdr.nodeSize}-byte nodes`);
const entries = walkTree(buf, hdr);
console.log(` ${entries.length} files found\n`);
fs.mkdirSync(OUT_DIR, { recursive: true });
let saved = 0;
let skipped = 0;
for (const entry of entries) {
const outPath = path.join(OUT_DIR, entry.path);
fs.mkdirSync(path.dirname(outPath), { recursive: true });
try {
const data = extractData(buf, entry);
fs.writeFileSync(outPath, data);
const tag = entry.zstd ? '[zstd]' : entry.compressed ? '[zlib]' : ' ';
console.log(`${tag} ${String(data.length).padStart(9)} ${entry.path}`);
saved++;
} catch (err: any) {
console.warn(`[SKIP] ${entry.path}: ${err.message}`);
skipped++;
}
}
console.log(`\nDone — ${saved} file(s) extracted to ${OUT_DIR}${skipped ? `, ${skipped} skipped` : ''}`);
}
main();