-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathlibraries.ts
More file actions
52 lines (49 loc) · 1.85 KB
/
libraries.ts
File metadata and controls
52 lines (49 loc) · 1.85 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
import { Resource } from '../load';
import { bits } from '../../utils';
const parseLibrary = (resource: Resource, index: number) => {
const buffer = resource.getEntry(resource.first + index);
const dataView = new DataView(buffer);
const numLayouts = dataView.getUint32(0, true) / 4;
const layouts = [];
for (let i = 0; i < numLayouts; i += 1) {
const offset = dataView.getUint32(i * 4, true);
const nextOffset = i === numLayouts - 1 ?
dataView.byteLength
: dataView.getUint32((i + 1) * 4, true);
const layoutDataView = new DataView(buffer, offset, nextOffset - offset);
layouts.push(parseLayout(layoutDataView, i, resource.type === 'BL1'));
}
return layouts;
};
const parseLayout = (dataView: DataView, index: number, isLBA1: boolean) => {
const nX = dataView.getUint8(0);
const nY = dataView.getUint8(1);
const nZ = dataView.getUint8(2);
const numBricks = nX * nY * nZ;
const blocks = [];
const offset = 3;
for (let i = 0; i < numBricks; i += 1) {
const type = dataView.getUint8(offset + (i * 4) + 1);
const shape = dataView.getUint8(offset + (i * 4));
const brick = dataView.getUint16(offset + (i * 4) + 2, true);
blocks.push({
shape,
sound: bits(type, 0, 4),
// sound 2 only exist in LBA1
// for LBA1 each sound maps each step in the animation state (left or right)
// note, these are not the sample index, instead is just a list of sound types
sound2: isLBA1 ? bits(type, 4, 4) : -1,
// only water (1) exist as groupType in LBA1
groundType: isLBA1 ? (type & 0xFF) === 0xF1 ? 1 : 0 : bits(type, 4, 4),
brick,
});
}
return {
index,
nX,
nY,
nZ,
blocks
};
};
export { parseLibrary };