-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.js
More file actions
208 lines (161 loc) · 5.53 KB
/
parser.js
File metadata and controls
208 lines (161 loc) · 5.53 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
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
'use strict';
/**
* Survarium options files parser after version 0.45
*/
const ENDIANNESS = require('os').endianness();
function parse(buffer, options) {
function getLength(entry) {
return entry.readUInt32LE(0);
}
const ENTRY_LENGTH = getLength(buffer);
const ENTRY_OFFSET_SIZE = 6;
const ENTRY_OFFSET_TYPE = 5;
const ENTRY_OFFSET_NAME = 8;
const ENTRY_OFFSET_VALUE = 0;
function correct(entry) {
return entry && entry.length === ENTRY_LENGTH;
}
function getEntry(offset) {
let entry = buffer.slice(offset, offset + ENTRY_LENGTH);
if (!correct(entry)) {
return;
}
entry.__offset = offset;
return entry;
}
function getSize(entry) {
return entry[`readUInt16${ENDIANNESS}`](ENTRY_OFFSET_SIZE);
}
function getType(entry) {
return Number(entry.slice(ENTRY_OFFSET_TYPE, ENTRY_OFFSET_TYPE + 1).toString('hex')[0]);
}
function getString(offset, size) {
let stringEnd = buffer.indexOf(0, offset, 'hex');
stringEnd === -1 && size && (stringEnd = size);
return buffer.slice(offset, stringEnd).toString('utf8');
}
function getNameOffset(entry) {
return entry[`readUInt32${ENDIANNESS}`](ENTRY_OFFSET_NAME);
}
function getName(entry, parent) {
let name = getString(getNameOffset(entry));
if (name === '\u0010') {
name = parent ?
options.longNames ? `${parent.name}_${parent.value.length}` :
parent.value.length :
'root';
}
return name;
}
function getValueInt(entry) {
return entry[`readInt32${ENDIANNESS}`](ENTRY_OFFSET_VALUE);
}
function getValueFloat(entry, shift) {
let offset = ENTRY_OFFSET_VALUE;
shift && (offset += shift);
return Math.round(entry[`readFloat${ENDIANNESS}`](offset) * 10000) / 10000;
}
function getValueString(entry, size) {
return getString(entry[`readUInt32${ENDIANNESS}`](ENTRY_OFFSET_VALUE), size);
}
function getValueBool(entry) {
return !!entry[`readInt32${ENDIANNESS}`](ENTRY_OFFSET_VALUE);
}
function getChild(entry) {
return getEntry(getValueInt(entry));
}
function getArray(entry) {
let offset = getValueInt(entry);
let value = buffer.slice(offset, offset + ENTRY_LENGTH);
let array = [];
for (let i = 0, size = getSize(entry); i < size; i += 4) {
array.push(getValueFloat(value, i));
}
return array;
}
function stringifyBuffer(entry) {
let result = entry.toString('hex').toUpperCase();
let length = result.length;
let out = '';
for (let i = 0, j = 2; i < length; i += j) {
out += (i ? '-' : '') + result.substr(i, j);
}
return out;
}
const result = [];
let pointer = result;
function decode(entry, parent) {
if (!entry) {
return;
}
let type = getType(entry);
let decoded = {
size: getSize(entry),
name: getName(entry, parent)
};
options.debug && (decoded.buffer = stringifyBuffer(entry));
switch (type) {
case 0:
type = 'bool';
decoded.value = getValueBool(entry);
break;
case 1:
type = 'int';
decoded.value = getValueInt(entry);
break;
case 2:
type = 'float';
decoded.value = getValueFloat(entry);
break;
case 3:
case 4:
let __pointer = pointer;
decoded.value = [];
if (decoded.size) {
pointer = decoded.value;
let safe = decoded.size - 1;
let innerOffset = getChild(entry).__offset;
do {
decode(getEntry(innerOffset), decoded);
innerOffset += ENTRY_LENGTH;
} while (safe-- && decoded.value.length < decoded.size);
}
if (!options.full) {
decoded.value = decoded.value.reduce((hash, elem) => {
hash[elem.name] = elem.value;
return hash;
}, type === 3 ? {} : []);
}
pointer = __pointer;
break;
case 5:
type = 'string';
decoded.value = getValueString(entry, decoded.size);
break;
case 6:
case 7:
case 8:
decoded.size = type - 4;
type = `arr`;
decoded.value = getArray(entry);
break;
default:
decoded.value = 'UNKNOWN';
break;
}
decoded.type = type;
if (!options.debug && !options.full) {
decoded = { name: decoded.name, value: decoded.value };
}
if (!options.debug && options.full && options.format && options.format.length) {
decoded = options.format.reduce((formatted, field) => {
formatted[field] = decoded[field];
return formatted;
}, {});
}
return pointer.push(decoded);
}
decode(getEntry(0));
return (!options.debug && !options.full) ? result[0].value : result;
}
module.exports = parse;