-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUnityBundle.js
65 lines (43 loc) · 1.66 KB
/
UnityBundle.js
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
"use strict";
const BufferStream = require("./BufferStream");
const UnityAsset = require("./UnityAsset");
//==============================================================================
function parseUnityBundle(inBuf) {
let stream = new BufferStream(inBuf);
let signature = stream.readString().toString("ascii");
if(signature !== "UnityRaw") throw `${signature} is not a valid signature`;
let version1 = stream.readInt32();
let version2 = stream.readString().toString("ascii");
let version3 = stream.readString().toString("ascii");
let bundleSize;
if(version1 < 6) { bundleSize = stream.readInt32(); }
else throw "Sorry! Unsupported `version1` ${version1}!";
let dummy2 = stream.readInt16();
let offset = stream.readInt16();
let dummy3 = stream.readInt32();
let lzmaChunks = stream.readInt32();
let lzmaSize = 0, streamSize = 0;
for(let i = 0 ; i < lzmaChunks ; i++) {
lzmaSize = stream.readInt32();
streamSize = stream.readInt32();
}
stream.pos = offset;
// Get asset files
let assets = [];
let fileCount = stream.readInt32();
for(let i = 0 ; i < fileCount ; i++) {
let assetFilename = stream.readString().toString("ascii");
let fileOffset = stream.readInt32();
fileOffset += offset;
let fileSize = stream.readInt32();
let nextFile = stream.pos;
stream.pos = fileOffset;
let assetBuf = stream.read(fileSize);
let assetStream = new BufferStream(assetBuf);
assets.push(UnityAsset.parse(assetFilename, assetStream));
stream.pos = nextFile;
}
return assets;
}
//==============================================================================
module.exports.parse = parseUnityBundle;