-
Notifications
You must be signed in to change notification settings - Fork 6
/
demo.js
164 lines (125 loc) · 2.78 KB
/
demo.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
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
var fs = require("fs");
chunkTypeNames = [];
chunkTypeNames[0] = "invalid";
chunkTypeNames[1] = "snapshot";
chunkTypeNames[2] = "message";
chunkTypeNames[3] = "snapshot_delta";
function Demo(filePath)
{
this.path = filePath;
var reader = fs.createReadStream(filePath, {start: 0, end: 176-1});
var self = this;
reader.on('data', function(chunk) { self.parseHeader(chunk); });
reader.on('error', function(e) {
console.log(e);
});
}
Buffer.prototype.readerInit = function() {
this._readerOffs = 0;
}
Buffer.prototype.readRaw = function(size) {
var data = this.slice(this._readerOffs, this._readerOffs+size);
this._readerOffs += size;
return data;
}
Buffer.prototype.readCString = function(size) {
var data = this.readRaw(size);
// null termination
for (var i = 0; i < data.length; i++)
{
if (data[i] == 0)
{
data = data.slice(0, i);
break;
}
}
return data.toString();
}
Buffer.prototype.uint32 = function() {
return this.readRaw(4).readUInt32BE(0);
}
Demo.prototype.parseHeader = function(data) {
if (data.length != 176)
{
this.error = "read invalid size "+data.length;
return;
}
data.readerInit();
this.header = {};
var h = this.header;
// magic
h.magic = data.readCString(7);
// version
h.version = data.readRaw(1)[0];
// netversion
h.netVersion = data.readCString(64);
// mapname
h.mapName = data.readCString(64);
// mapsize
h.mapSize = data.uint32();
// length
h.mapCrc = data.uint32();
// type
h.type = data.readCString(8);
// length
h.length = data.uint32(4);
// timestamp
h.timeStamp = data.readCString(20);
console.log(h);
this.test();
}
Demo.prototype.test = function() {
// find offset of first chunk
this.firstChunk = /*header*/ 176 +
/*timeline markers*/ 4 + 4*64 +
/*mapdata*/ this.header.mapSize;
console.log("first chunk", this.firstChunk);
var data = fs.readFileSync(this.path);
data = data.slice(this.firstChunk);
data.readerInit();
var offs = 0;
while (offs < data.length)
{
var lastOffs = offs;
var chunk = data[offs];
offs++;
// CHUNKTYPEFLAG_TICKMARKER
if ((chunk&0x80))
{
//console.log("tickmarker");
// CHUNKMASK_TICK
if ((chunk&0x3f) == 0)
{
// tickdata
offs += 4; // skip tick data
}
/*else
{
}*/
}
else
{
// normal chunk
// CHUNKMASK_TYPE
var type = (chunk&0x60)>>5;
// CHUNKMASK_SIZE
var size = (chunk&0x1f);
// check for extended size
if (size == 30)
{
// char
size = data[offs++];
}
else if (size == 31)
{
// short
size = data.readUInt16LE(offs);
offs += 2;
}
console.log((lastOffs+this.firstChunk).toString(16), "type", chunkTypeNames[type], "size", size);
offs += size;
}
}
}
//var d = new Demo("./nice_scene.demo");
var d = new Demo("./autorecord_2014-08-06_17-35-06.demo");