-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
stringtables.js
217 lines (217 loc) · 8.39 KB
/
stringtables.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
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const assert = require("assert");
const ByteBuffer = require("bytebuffer");
const events_1 = require("events");
const _ = require("lodash");
const Long = require("long");
const assert_exists_1 = require("./assert-exists");
const consts = require("./consts");
const consts_1 = require("./consts");
const bitbuffer_1 = require("./ext/bitbuffer");
function parseUserInfoData(buf) {
const bytebuf = ByteBuffer.wrap(buf, ByteBuffer.BIG_ENDIAN);
bytebuf.skip(8);
const hi = bytebuf.readUint32();
const lo = bytebuf.readUint32();
const xuid = Long.fromBits(lo, hi);
const name = bytebuf
.readString(consts_1.MAX_PLAYER_NAME_LENGTH, ByteBuffer.METRICS_BYTES)
.split("\0", 2)[0];
const userId = bytebuf.readUint32();
const guid = bytebuf
.readString(consts_1.SIGNED_GUID_LEN + 1, ByteBuffer.METRICS_BYTES)
.split("\0", 2)[0];
bytebuf.skip(3);
const friendsId = bytebuf.readUint32();
const friendsName = bytebuf
.readString(consts_1.MAX_PLAYER_NAME_LENGTH, ByteBuffer.METRICS_BYTES)
.split("\0", 2)[0];
const fakePlayer = bytebuf.readByte() !== 0;
bytebuf.skip(3);
const isHltv = bytebuf.readByte() !== 0;
bytebuf.skip(3);
return {
xuid,
name,
userId,
guid,
friendsId,
friendsName,
fakePlayer,
isHltv
};
}
/**
* Handles string tables for a demo file.
*/
class StringTables extends events_1.EventEmitter {
constructor() {
super();
this.tables = [];
/**
* User data handlers. Transform raw buffer into any other value.
* By default, `userinfo` user data is transformed to {@link PlayerInfo}
* objects.
*/
this.userDataCallbacks = {
userinfo: parseUserInfoData
};
}
listen(messageEvents) {
messageEvents.on("svc_UpdateStringTable", this._handleUpdateStringTable.bind(this));
messageEvents.on("svc_CreateStringTable", this._handleCreateStringTable.bind(this));
}
findTableByName(name) {
return this.tables.find(table => table.name === name);
}
handleStringTables(bitbuf) {
const numTables = bitbuf.readUInt8();
for (let i = 0; i < numTables; ++i) {
const tableName = bitbuf.readCString();
this._handleStringTable(tableName, bitbuf);
}
}
_handleStringTable(name, bitbuf) {
const userDataCallback = this.userDataCallbacks[name];
const table = assert_exists_1.default(this.findTableByName(name));
const numEntries = bitbuf.readUInt16();
for (let entryIndex = 0; entryIndex < numEntries; ++entryIndex) {
const entry = bitbuf.readCString();
let userData = null;
// has user data?
if (bitbuf.readOneBit()) {
const userDataSize = bitbuf.readUInt16();
const userDataBuf = bitbuf.readBytes(userDataSize);
userData =
userDataCallback === undefined
? userDataBuf
: userDataCallback(userDataBuf);
}
table.entries[entryIndex] = { entry, userData };
this.emit("update", {
table,
entryIndex,
entry,
userData
});
}
// parse client-side entries
if (bitbuf.readOneBit()) {
const numStrings = bitbuf.readUInt16();
for (let i = 0; i < numStrings; ++i) {
const entry = bitbuf.readCString(); // tslint:disable-line no-dead-store
let userData = null;
if (bitbuf.readOneBit()) {
const userDataSize = bitbuf.readUInt16();
const userDataBuf = bitbuf.readBytes(userDataSize);
// tslint:disable-next-line no-dead-store
userData =
userDataCallback === undefined
? userDataBuf
: userDataCallback(userDataBuf);
}
// TODO: do something with client-side entries
}
}
// table is ready
this.emit("postcreate", table);
}
_parseStringTableUpdate(bitbuf, table, entries) {
// overflow silently. this is how the official parser handles overflows...
bitbuf.view.silentOverflow = true;
const history = [];
const entryBits = Math.ceil(Math.log2(table.maxEntries));
const userDataCallback = this.userDataCallbacks[table.name];
assert(!bitbuf.readOneBit(), "dictionary encoding unsupported");
_.reduce(_.range(entries), lastEntry => {
let entryIndex = lastEntry + 1;
if (!bitbuf.readOneBit()) {
entryIndex = bitbuf.readUBits(entryBits);
}
assert(entryIndex >= 0 && entryIndex < table.maxEntries, "bogus string index");
const existingEntry = table.entries[entryIndex];
let entry = null;
// has the entry changed?
if (bitbuf.readOneBit()) {
// substring check
if (bitbuf.readOneBit()) {
const index = bitbuf.readUBits(5);
const bytesToCopy = bitbuf.readUBits(consts.SUBSTRING_BITS);
const last = history[index];
if (last == null) {
throw new Error("string table entry is delta from non existent entry");
}
const subStr = last.slice(0, bytesToCopy);
const suffix = bitbuf.readCString();
entry = subStr + suffix;
}
else {
entry = bitbuf.readCString();
}
}
else {
// If the string itself hasn't changed, this entry must already exist
entry = assert_exists_1.default(existingEntry).entry;
}
// read in the user data
let userData = null;
if (bitbuf.readOneBit()) {
// don't read the length, it's fixed length and the length was networked down already
if (table.userDataFixedSize) {
const userDataArray = [bitbuf.readUBits(table.userDataSizeBits)];
userData = Buffer.from(userDataArray);
}
else {
const bytes = bitbuf.readUBits(consts.MAX_USERDATA_BITS);
userData = bitbuf.readBytes(bytes);
}
if (userDataCallback !== undefined) {
userData = userDataCallback(userData);
}
}
else {
userData = existingEntry ? existingEntry.userData : null;
}
table.entries[entryIndex] = { entry, userData };
// add to history
if (history.length > 31) {
history.shift();
}
history.push(entry);
this.emit("update", {
table,
entryIndex,
entry,
userData
});
return entryIndex;
}, -1);
}
_handleCreateStringTable(msg) {
const bitbuf = bitbuffer_1.BitStream.from(msg.stringData);
// table shouldn't already exist
assert(this.findTableByName(msg.name) === undefined, "table already exists");
assert(msg.userDataSize === Math.ceil(msg.userDataSizeBits / 8), "invalid user data byte size");
assert(msg.userDataSizeBits <= 32, "userdata value too large");
// create an empty table
const table = {
name: msg.name,
entries: [],
userDataSizeBits: msg.userDataSizeBits,
userDataFixedSize: msg.userDataFixedSize,
maxEntries: msg.maxEntries
};
this.emit("create", table);
this._parseStringTableUpdate(bitbuf, table, msg.numEntries);
this.tables.push(table);
}
_handleUpdateStringTable(msg) {
const bitbuf = bitbuffer_1.BitStream.from(msg.stringData);
const table = this.tables[msg.tableId];
assert(table !== undefined, "bad table index");
this._parseStringTableUpdate(bitbuf, table, msg.numChangedEntries);
}
}
exports.StringTables = StringTables;
//# sourceMappingURL=stringtables.js.map