-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathUnityBundle.php
383 lines (365 loc) · 11.4 KB
/
UnityBundle.php
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
abstract class Stream {
abstract protected function read($length);
abstract public function seek($position);
abstract public function position();
abstract protected function getPos();
abstract protected function setPos($pos);
public function __get($name) {
switch($name) {
case 'position': return $this->getPos();
case 'bool': return $this->readBoolean();
case 'byte': return $this->readData(1);
case 'short': return $this->readInt16();
case 'ushort': return $this->readUint16();
case 'long': return $this->readInt32();
case 'ulong': return $this->readUint32();
case 'longlong': return $this->readInt64();
case 'ulonglong': return $this->readUint64();
case 'float': return $this->readFloat();
case 'double': return $this->readDouble();
case 'string': return $this->readStringToNull();
case 'line': return $this->readStringToReturn();
default: throw new Exception("Access undefined field ${name} of class ".get_class($this));
}
}
public function __set($name, $val) {
switch($name) {
case 'position': return $this->setPos($val);
default: throw new Exception("Assign value to undefined field ${name} of class ".get_class($this));
}
}
public $littleEndian = false;
public $size;
public function readStringToNull() {
$s = '';
while (ord($char = $this->read(1)) != 0) {
$s .= $char;
}
return $s;
}
public function readStringAt($pos) {
$current = $this->position;
$this->position = $pos;
$data = $this->string;
$this->position = $current;
return $data;
}
public function readStringToReturn() {
$s = '';
while ($this->position < $this->size && ($char = $this->read(1)) != "\n") {
$s .= $char;
}
return trim($s,"\r");
}
public function readBoolean() {
return ord($this->byte)>0;
}
public function readInt16() {
$uint = $this->readUint16();
$sint = unpack('s', pack('S', $uint))[1];
return $sint;
}
public function readUint16() {
$int = $this->read(2);
if (strlen($int) != 2) return 0;
return unpack($this->littleEndian?'v':'n', $int)[1];
}
public function readInt32() {
$uint = $this->readUint32();
$sint = unpack('l', pack('L', $uint))[1];
return $sint;
}
public function readUint32() {
$int = $this->read(4);
if (strlen($int) != 4) return 0;
return unpack($this->littleEndian?'V':'N', $int)[1];
}
public function readInt64() {
$uint = $this->readUint64();
$sint = unpack('q', pack('Q', $uint))[1];
return $sint;
}
public function readUint64() {
$int = $this->read(8);
if (strlen($int) != 8) return 0;
return unpack($this->littleEndian?'P':'J', $int)[1];
}
public function readFloat() {
$int = $this->read(4);
if (strlen($int) != 4) return 0;
if (!$this->littleEndian) $int = $int[3].$int[2].$int[1].$int[0];
return unpack(/*$this->littleEndian?'g':'G'*/ 'f', $int)[1];
}
public function readDouble() {
$int = $this->read(8);
if (strlen($int) != 8) return 0;
if (!$this->littleEndian) $int = $int[7].$int[6].$int[5].$int[4].$int[3].$int[2].$int[1].$int[0];
return unpack(/*$this->littleEndian?'e':'E'*/ 'd', $int)[1];
}
public function readData($size) {
if ($size <= 0) return '';
return $this->read($size);
}
public function readDataAt($pos, $size) {
$current = $this->position;
$this->position = $pos;
$data = $this->readData($size);
$this->position = $current;
return $data;
}
public function alignStream($alignment) {
$mod = $this->position % $alignment;
if ($mod != 0) {
$this->position += $alignment - $mod;
}
}
public function readAlignedString($len) {
$string = $this->readData($len);
$this->alignStream(4);
return $string;
}
}
class FileStream extends Stream {
private $f;
function __construct($file) {
$this->f = fopen($file, 'rb+');
if ($this->f === false) {
throw new Exception('Unable to open file');
}
$this->size = filesize($file);
}
function __destruct() {
fclose($this->f);
}
protected function read($length) {
return fread($this->f, $length);
}
public function write($newData) {
fwrite($this->f, $newData);
$pos = $this->position;
fseek($this->f, 0, SEEK_END);
$this->size = ftell($this->f);
$this->position = $pos;
}
public function seek($position) {
fseek($this->f, $position);
}
public function position() {
return ftell($this->f);
}
protected function getPos() {
return ftell($this->f);
}
protected function setPos($pos) {
fseek($this->f, $pos);
}
}
class MemoryStream extends Stream {
private $data;
private $offset;
function __construct($data) {
$this->data = $data;
$this->size = strlen($data);
}
function __destruct() {
$this->data = NULL;
}
protected function read($length) {
$data = substr($this->data, $this->offset, $length);
$this->offset += $length;
return $data;
}
public function seek($position) {
$this->offset = $position;
}
public function write($newData) {
$this->data .= $newData;
$this->size += strlen($newData);
}
public function position() {
return $this->offset;
}
protected function getPos() {
return $this->offset;
}
protected function setPos($pos) {
$this->offset = $pos;
}
}
function checkAndMoveFile(string $current, string $saveTo, int $modifiedTime = 0) {
if (!file_exists($current)) return;
$dir = pathinfo($saveTo, PATHINFO_DIRNAME);
$format = pathinfo($saveTo, PATHINFO_EXTENSION);
$saveTo = $dir.'/'.pathinfo($saveTo, PATHINFO_FILENAME);
if (!file_exists($dir)) mkdir($dir, 0777, true);
$saveToFull = $saveTo.'.'.$format;
if (file_exists($saveToFull)) {
$hash_current = hash_file('sha1', $current);
$hash_previous = hash_file('sha1', $saveToFull);
if ($hash_current === $hash_previous) {
unlink($current);
return;
}
$ftime = date('_Ymd_Hi', filemtime($saveToFull));
rename($saveToFull, $saveTo.$ftime.'.'.$format);
}
rename($current, $saveToFull);
if ($modifiedTime !== 0) {
touch($saveToFull, $modifiedTime);
}
}
function checkAndCreateFile(string $saveTo, string $data, int $modifiedTime = 0) {
$dir = pathinfo($saveTo, PATHINFO_DIRNAME);
$format = pathinfo($saveTo, PATHINFO_EXTENSION);
$saveTo = $dir.'/'.pathinfo($saveTo, PATHINFO_FILENAME);
if (!file_exists($dir)) mkdir($dir, 0777, true);
$saveToFull = $saveTo.'.'.$format;
if (file_exists($saveToFull)) {
$hash_current = hash('sha1', $data);
$hash_previous = hash_file('sha1', $saveToFull);
if ($hash_current === $hash_previous) {
return;
}
$ftime = date('_Ymd_Hi', filemtime($saveToFull));
rename($saveToFull, $saveTo.$ftime.'.'.$format);
}
file_put_contents($saveToFull, $data);
if ($modifiedTime !== 0) {
touch($saveToFull, $modifiedTime);
}
}
function lz4_uncompress_stream($data, $uncompressedSize) {
return lz4_uncompress(pack('V', $uncompressedSize).$data);
}
function encrypt($string = '', $key = '', $iv = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0") {
return openssl_encrypt($string, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
}
function decrypt($string = '', $key = '', $iv = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0") {
$data = openssl_decrypt($string, 'AES-128-CBC', $key, OPENSSL_RAW_DATA+OPENSSL_ZERO_PADDING, $iv);
$pad = ord(substr($data, -1, 1));
return substr($data, 0, -$pad);
}
function extractBundle($bundle) {
$header = $bundle->string;
if ($header != 'UnityFS' && $header != 'UnityRaw') throw new Exception('unknown header: '.$header);
$format = $bundle->long;
$versionPlayer = $bundle->string;
$versionEngine = $bundle->string;
if ($format < 6) {
$bundle->ulong;
$bundle->ushort;
$offset = $bundle->short;
$bundle->ulong;
$lzmaChunks = $bundle->long;
$lzmaSize = 0;
for($i=0; $i<$lzmaChunks; $i++) {
$lzmaSize = $bundle->long;
$bundle->ulong;
}
$bundle->position = $offset;
// getFiles
$fileCount = $bundle->long;
$fileList = [];
for ($i=0; $i<$fileCount; $i++) {
$filename = $bundle->string;
$fileOffset = $bundle->long + $offset;
$fileSize = $bundle->long;
$nextFile = $bundle->position;
$bundle->position = $fileOffset;
file_put_contents($filename, $bundle->readData($fileSize));
$fileList[] = $filename;
$bundle->position = $nextFile;
}
return $fileList;
} else if ($format == 6) {
} else if ($format == 7) {
} else if ($format == 8) {
} else {
throw new Exception('unknown version: '.$format);
}
$bundle->longlong;
$compressedSize = $bundle->long;
$uncompressedSize = $bundle->long;
$flag = $bundle->long;
if (($flag & 128) != 0) {
//throw new Exception('block info at end');
if ($format >= 7) {
$bundle->alignStream(16);
}
$pos = $bundle->position;
$bundle->position = $bundle->size - $compressedSize;
$blocksInfoBytes = $bundle->readData($compressedSize);
$bundle->position = $pos;
} else {
if ($format >= 7) {
$bundle->alignStream(16);
}
$blocksInfoBytes = $bundle->readData($compressedSize);
if (($flag & 512) != 0) {
$bundle->alignStream(16);
}
}
switch ($flag & 63) {
case 0:
// Not compressed
$uncompressedData = &$blocksInfoBytes;
break;
case 1:
throw new Exception('lzma compressed block info');
case 2:
case 3:
$uncompressedData = lz4_uncompress_stream($blocksInfoBytes, $uncompressedSize);
break;
default:
throw 'unknown flag';
}
$blocksInfo = new MemoryStream($uncompressedData);
unset($uncompressedData, $blocksInfoBytes);
$blocksInfo->seek(16);
$blockCount = $blocksInfo->long;
fclose(fopen('--temp_decompress','wb'));
$assetsData = new FileStream('--temp_decompress');
for ($i=0; $i<$blockCount; $i++) {
$uncompressedSize = $blocksInfo->long;
$compressedSize = $blocksInfo->long;
$flag = $blocksInfo->readInt16();
//echo "${uncompressedSize}\t${compressedSize}\t${flag}\n";
$chunkData = $bundle->readData($compressedSize);
switch ($flag & 63) {
case 0:
// not compressed
$assetsData->write($chunkData);
break;
case 1:
// 7zip
throw new Exception('lzma compressed chunk');
case 2:
case 3:
$assetsData->write(lz4_uncompress_stream($chunkData, $uncompressedSize));
}
unset($chunkData);
}
//echo "\n";
$entryInfo_count = $blocksInfo->long;
$fileList = [];
for ($i=0; $i<$entryInfo_count; $i++) {
$entryInfoOffset = $blocksInfo->longlong;
$entryInfoSize = $blocksInfo->longlong;
$blocksInfo->long;
$filename = $blocksInfo->string;
//echo "${entryInfoOffset}\t${entryInfoSize}\t${filename}\n";
$assetsData->position = $entryInfoOffset;
$file = $assetsData->readData($entryInfoSize);
/*$f = fopen($filename, 'wb');
fwrite($f, $file);
fclose($f);*/
file_put_contents($filename, $file);
unset($file);
$fileList[] = $filename;
}
unset($assetsData);
unlink('--temp_decompress');
gc_collect_cycles();
return $fileList;
}