-
Notifications
You must be signed in to change notification settings - Fork 63
/
handlers.js
411 lines (336 loc) · 11.7 KB
/
handlers.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
const ByteBuffer = require('bytebuffer');
const Long = require('long');
const SteamID = require('steamid');
const GlobalOffensive = require('./index.js');
const Language = require('./language.js');
const Protos = require('./protobufs/generated/_load.js');
let handlers = GlobalOffensive.prototype._handlers;
// ClientWelcome and ClientConnectionStatus
handlers[Language.ClientLogonFatalError] = function(body) {
let proto = decodeProto(Protos.CMsgGCCStrike15_v2_ClientLogonFatalError, body);
clearTimeout(this._helloTimer);
let err = new Error(`Logon Fatal Error: ${proto.message || proto.errorcode}`);
err.code = proto.errorcode;
err.country = proto.country;
this.emit('error', err);
};
handlers[Language.ClientWelcome] = function(body) {
let proto = decodeProto(Protos.CMsgClientWelcome, body);
if (proto.outofdate_subscribed_caches && proto.outofdate_subscribed_caches.length) {
proto.outofdate_subscribed_caches[0].objects.forEach((cache) => {
switch (cache.type_id) {
case 1:
// Inventory
let items = cache.object_data.map((object) => {
let item = decodeProto(Protos.CSOEconItem, object);
this._processSOEconItem(item);
return item;
});
this.inventory = items;
break;
/*case 7:
// Account metadata - this doesn't appear to be useful in CS:GO
let data = decodeProto(Protos.CSOEconGameAccountClient, cache.object_data[0]);
break;*/
/*case 43:
// Most likely item presets (multiple)
let data = decodeProto(Protos.CSOSelectedItemPreset, cache.object_data[0]);
break;*/
default:
this.emit('debug', "Unknown SO type " + cache.type_id + " with " + cache.object_data.length + " items");
break;
}
});
}
this.inventory = this.inventory || [];
this.emit('debug', "GC connection established");
this.haveGCSession = true;
clearTimeout(this._helloTimer);
this._helloTimer = null;
this._helloTimerMs = 1000;
this.emit('connectedToGC');
};
handlers[Language.MatchmakingGC2ClientHello] = function(body) {
let proto = decodeProto(Protos.CMsgGCCStrike15_v2_MatchmakingGC2ClientHello, body);
this.emit('accountData', proto);
this.accountData = proto;
};
handlers[Language.ClientConnectionStatus] = function(body) {
let proto = decodeProto(Protos.CMsgConnectionStatus, body);
this.emit('connectionStatus', proto.status, proto);
let statusStr = proto.status;
for (let i in GlobalOffensive.GCConnectionStatus) {
if (GlobalOffensive.GCConnectionStatus.hasOwnProperty(i) && GlobalOffensive.GCConnectionStatus[i] == proto.status) {
statusStr = i;
}
}
this.emit('debug', "Connection status: " + statusStr + " (" + proto.status + "); have session: " + (this.haveGCSession ? 'yes' : 'no'));
if (proto.status != GlobalOffensive.GCConnectionStatus.HAVE_SESSION && this.haveGCSession) {
this.emit('disconnectedFromGC', proto.status);
this.haveGCSession = false;
this._connect(); // Try to reconnect
}
};
// MatchList
handlers[Language.MatchList] = function(body) {
let proto = decodeProto(Protos.CMsgGCCStrike15_v2_MatchList, body);
this.emit('matchList', proto.matches, proto);
};
// PlayersProfile
handlers[Language.PlayersProfile] = function(body) {
let proto = decodeProto(Protos.CMsgGCCStrike15_v2_PlayersProfile, body);
if (!proto.account_profiles[0]) {
return;
}
let profile = proto.account_profiles[0];
let sid = SteamID.fromIndividualAccountID(profile.account_id);
this.emit('playersProfile', profile);
this.emit('playersProfile#' + sid.getSteamID64(), profile);
};
// Inspecting items
handlers[Language.Client2GCEconPreviewDataBlockResponse] = function(body) {
let proto = decodeProto(Protos.CMsgGCCStrike15_v2_Client2GCEconPreviewDataBlockResponse, body);
if (!proto.iteminfo) {
return;
}
let item = proto.iteminfo;
// decode the wear
let buf = Buffer.alloc(4);
buf.writeUInt32BE(item.paintwear, 0);
item.paintwear = buf.readFloatBE(0);
this.emit('inspectItemInfo', item);
this.emit('inspectItemInfo#' + item.itemid, item);
};
// Item manipulation
handlers[Language.CraftResponse] = function(body) {
let blueprint = body.readInt16(); // recipe ID
let unknown = body.readUint32(); // always 0 in my experience
let idCount = body.readUint16();
let idList = []; // let's form an array of IDs
for (let i = 0; i < idCount; i++) {
let id = body.readUint64().toString(); // grab the next id
idList.push(id); // item id
}
this.emit('craftingComplete', blueprint, idList);
};
handlers[Language.ItemCustomizationNotification] = function(body) {
let proto = decodeProto(Protos.CMsgGCItemCustomizationNotification, body);
if (!proto.item_id || proto.item_id.length == 0 || !proto.request) {
return;
}
this.emit('itemCustomizationNotification', proto.item_id, proto.request);
};
// SO
GlobalOffensive.prototype._processSOEconItem = function(item) {
// Inventory position
let isNew = (item.inventory >>> 30) & 1;
item.position = (isNew ? 0 : item.inventory & 0xFFFF);
// Is this item contained in a casket?
let casketIdLow = getAttributeValueBytes(272);
let casketIdHigh = getAttributeValueBytes(273);
if (casketIdLow && casketIdHigh) {
let casketIdLong = new Long(casketIdLow.readUInt32LE(0), casketIdHigh.readUInt32LE(0));
item.casket_id = casketIdLong.toString();
}
// Item custom names
let customNameBytes = getAttributeValueBytes(111);
if (customNameBytes && !item.custom_name) {
item.custom_name = customNameBytes.slice(2).toString('utf8');
}
// Paint index/seed/wear
let paintIndexBytes = getAttributeValueBytes(6);
if (paintIndexBytes) {
item.paint_index = paintIndexBytes.readFloatLE(0);
}
let paintSeedBytes = getAttributeValueBytes(7);
if (paintSeedBytes) {
item.paint_seed = Math.floor(paintSeedBytes.readFloatLE(0));
}
let paintWearBytes = getAttributeValueBytes(8);
if (paintWearBytes) {
item.paint_wear = paintWearBytes.readFloatLE(0);
}
let tradableAfterDateBytes = getAttributeValueBytes(75);
if (tradableAfterDateBytes) {
item.tradable_after = new Date(tradableAfterDateBytes.readUInt32LE(0) * 1000);
}
let killEaterBytes = getAttributeValueBytes(80);
if (killEaterBytes) {
item.kill_eater_value = killEaterBytes.readUInt32LE(0);
}
let killEaterScoreTypeBytes = getAttributeValueBytes(81);
if (killEaterScoreTypeBytes) {
item.kill_eater_score_type = killEaterScoreTypeBytes.readUInt32LE(0);
}
let questIdBytes = getAttributeValueBytes(168);
if (questIdBytes) {
item.quest_id = questIdBytes.readUInt32LE(0);
}
let stickers = [];
for (let i = 0; i <= 5; i++) {
let stickerIdBytes = getAttributeValueBytes(113 + (i * 4));
if (stickerIdBytes) {
let sticker = {
slot: i,
sticker_id: stickerIdBytes.readUInt32LE(0),
wear: null,
scale: null,
rotation: null,
offset_x: null,
offset_y: null
};
// As of the 2024-02-06 update, the value of the "sticker slot x schema" attribute (290-295) seems to indicate
// which slot the sticker occupies, rather than the actual slot named by the attribute. Why? Who knows?
// I sure hope no items exist with schema set for some stickers but not for others.
let schemaBytes = getAttributeValueBytes(290 + i);
if (schemaBytes) {
sticker.slot = schemaBytes.readUInt32LE(0);
}
['wear', 'scale', 'rotation'].forEach((attrib, idx) => {
let bytes = getAttributeValueBytes(114 + (i * 4) + idx);
if (bytes) {
sticker[attrib] = bytes.readFloatLE(0);
}
});
['offset_x', 'offset_y'].forEach((attrib, idx) => {
let bytes = getAttributeValueBytes(278 + (i * 2) + idx);
if (bytes) {
sticker[attrib] = bytes.readFloatLE(0);
}
});
stickers.push(sticker);
}
}
if (stickers.length > 0) {
item.stickers = stickers;
}
// def_index-specific attribute parsing
switch (item.def_index) {
case 1201:
// Storage Unit
item.casket_contained_item_count = 0;
let itemCountBytes = getAttributeValueBytes(270);
if (itemCountBytes) {
item.casket_contained_item_count = itemCountBytes.readUInt32LE(0);
}
break;
}
/**
* @param {int} attribDefIndex
* @returns {null|Buffer}
*/
function getAttributeValueBytes(attribDefIndex) {
let attrib = (item.attribute || []).find(attrib => attrib.def_index == attribDefIndex);
return attrib ? attrib.value_bytes : null;
}
};
handlers[Language.SO_Create] = function(body) {
let proto = decodeProto(Protos.CMsgSOSingleObject, body);
this._handleSOCreate(proto);
};
GlobalOffensive.prototype._handleSOCreate = function(proto) {
if (proto.type_id != 1) {
return; // Not an item
}
if (!this.inventory) {
return; // We don't have our inventory yet! (this shouldn't be possible in CS:GO, but wutevs)
}
let item = decodeProto(Protos.CSOEconItem, proto.object_data);
this._processSOEconItem(item);
this.inventory.push(item);
this.emit('itemAcquired', item);
};
handlers[Language.SO_Update] = function(body) {
let proto = decodeProto(Protos.CMsgSOSingleObject, body);
this._handleSOUpdate(proto);
};
GlobalOffensive.prototype._handleSOUpdate = function(so) {
if (so.type_id != 1) {
return; // Not an item, we don't care
}
if (!this.inventory) {
return; // We somehow don't have our inventory yet!
}
let item = decodeProto(Protos.CSOEconItem, so.object_data);
this._processSOEconItem(item);
for (let i = 0; i < this.inventory.length; i++) {
if (this.inventory[i].id == item.id) {
let oldItem = this.inventory[i];
this.inventory[i] = item;
this.emit('itemChanged', oldItem, item);
break;
}
}
};
handlers[Language.SO_Destroy] = function(body) {
let proto = decodeProto(Protos.CMsgSOSingleObject, body);
this._handleSODestroy(proto);
};
GlobalOffensive.prototype._handleSODestroy = function(proto) {
if (proto.type_id != 1) {
return; // Not an item
}
if (!this.inventory) {
return; // Inventory not loaded yet
}
let item = decodeProto(Protos.CSOEconItem, proto.object_data);
item.id = item.id.toString();
let itemData = null;
for (let i = 0; i < this.inventory.length; i++) {
if (this.inventory[i].id == item.id) {
itemData = this.inventory[i];
this.inventory.splice(i, 1);
break;
}
}
this.emit('itemRemoved', itemData);
};
handlers[Language.SO_UpdateMultiple] = function(body) {
let proto = decodeProto(Protos.CMsgSOMultipleObjects, body);
(proto.objects_added || []).forEach(item => this._handleSOCreate(item));
(proto.objects_modified || []).forEach(item => this._handleSOUpdate(item));
(proto.objects_removed || []).forEach(item => this._handleSODestroy(item));
};
function decodeProto(proto, encoded) {
if (ByteBuffer.isByteBuffer(encoded)) {
encoded = encoded.toBuffer();
}
let decoded = proto.decode(encoded);
let objNoDefaults = proto.toObject(decoded, {"longs": String});
let objWithDefaults = proto.toObject(decoded, {"defaults": true, "longs": String});
return replaceDefaults(objNoDefaults, objWithDefaults);
function replaceDefaults(noDefaults, withDefaults) {
if (Array.isArray(withDefaults)) {
return withDefaults.map((val, idx) => replaceDefaults(noDefaults[idx], val));
}
for (let i in withDefaults) {
if (!withDefaults.hasOwnProperty(i)) {
continue;
}
if (withDefaults[i] && typeof withDefaults[i] === 'object' && !Buffer.isBuffer(withDefaults[i])) {
// Covers both object and array cases, both of which will work
// Won't replace empty arrays, but that's desired behavior
withDefaults[i] = replaceDefaults(noDefaults[i], withDefaults[i]);
} else if (typeof noDefaults[i] === 'undefined' && isReplaceableDefaultValue(withDefaults[i])) {
withDefaults[i] = null;
}
}
return withDefaults;
}
function isReplaceableDefaultValue(val) {
if (Buffer.isBuffer(val) && val.length == 0) {
// empty buffer is replaceable
return true;
}
if (Array.isArray(val)) {
// empty array is not replaceable (empty repeated fields)
return false;
}
if (val === '0') {
// Zero as a string is replaceable (64-bit integer)
return true;
}
// Anything falsy is true
return !val;
}
}