forked from DoctorMcKay/node-globaloffensive
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhandlers.js
More file actions
613 lines (518 loc) · 17.3 KB
/
Copy pathhandlers.js
File metadata and controls
613 lines (518 loc) · 17.3 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
const decodeProto = require('./lib/proto-decode.js');
const Long = require('long');
const SteamID = require('steamid');
const NodeCS2 = require('./index.js');
const Language = require('./language.js');
const Protos = require('./protobufs/generated/_load.js');
const Constants = require('./constants.js');
const handlers = NodeCS2.prototype._handlers;
/**
* Helper function to map sticker-like items (stickers, keychains, variations)
* Ensures all fields including highlight_reel and wrapped_sticker are properly handled
* @param {Object} stickerLike - The sticker/keychain/variation object from protobuf
* @returns {Object} Normalized sticker-like object with all fields
*/
NodeCS2.prototype._mapStickerLikeItem = function (stickerLike) {
return {
slot: stickerLike.slot || 0,
sticker_id: stickerLike.sticker_id || 0,
wear: stickerLike.wear || null,
scale: stickerLike.scale || null,
rotation: stickerLike.rotation || null,
tint_id: stickerLike.tint_id || null,
offset_x: stickerLike.offset_x || null,
offset_y: stickerLike.offset_y || null,
offset_z: stickerLike.offset_z || null,
pattern: stickerLike.pattern || null,
highlight_reel: stickerLike.highlight_reel || null,
wrapped_sticker: stickerLike.wrapped_sticker || null
};
};
// ClientWelcome and ClientConnectionStatus
handlers[Language.ClientLogonFatalError] = function (body) {
let proto;
try {
proto = decodeProto(Protos.CMsgGCCStrike15_v2_ClientLogonFatalError, body);
} catch (err) {
this.emit('error', new Error(`Failed to decode ClientLogonFatalError: ${err.message}`));
return;
}
clearTimeout(this._helloTimer);
const 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;
try {
proto = decodeProto(Protos.CMsgClientWelcome, body);
} catch (err) {
this.emit('error', new Error(`Failed to decode ClientWelcome: ${err.message}`));
return;
}
if (proto.outofdate_subscribed_caches && proto.outofdate_subscribed_caches.length) {
proto.outofdate_subscribed_caches[0].objects.forEach((cache) => {
switch (cache.type_id) {
case Constants.SO_TYPE_ECON_ITEM:
// Inventory
const items = cache.object_data
.map((object) => {
try {
const item = decodeProto(Protos.CSOEconItem, object);
this._processSOEconItem(item);
return item;
} catch (err) {
this.emit('debug', `Failed to decode inventory item: ${err.message}`);
return null;
}
})
.filter((item) => item !== null);
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;
try {
proto = decodeProto(Protos.CMsgGCCStrike15_v2_MatchmakingGC2ClientHello, body);
} catch (err) {
this.emit('error', new Error(`Failed to decode MatchmakingGC2ClientHello: ${err.message}`));
return;
}
this.emit('accountData', proto);
this.accountData = proto;
};
handlers[Language.ClientConnectionStatus] = function (body) {
let proto;
try {
proto = decodeProto(Protos.CMsgConnectionStatus, body);
} catch (err) {
this.emit('error', new Error(`Failed to decode ClientConnectionStatus: ${err.message}`));
return;
}
if (!proto || typeof proto.status === 'undefined') {
this.emit('debug', 'ClientConnectionStatus missing status field');
return;
}
this.emit('connectionStatus', proto.status, proto);
let statusStr = proto.status;
for (const i in NodeCS2.GCConnectionStatus) {
if (NodeCS2.GCConnectionStatus.hasOwnProperty(i) && NodeCS2.GCConnectionStatus[i] == proto.status) {
statusStr = i;
}
}
this.emit(
'debug',
'Connection status: ' + statusStr + ' (' + proto.status + '); have session: ' + (this.haveGCSession ? 'yes' : 'no')
);
if (proto.status != NodeCS2.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;
try {
proto = decodeProto(Protos.CMsgGCCStrike15_v2_MatchList, body);
} catch (err) {
this.emit('error', new Error(`Failed to decode MatchList: ${err.message}`));
return;
}
this.emit('matchList', proto.matches || [], proto);
};
// PlayersProfile
handlers[Language.PlayersProfile] = function (body) {
let proto;
try {
proto = decodeProto(Protos.CMsgGCCStrike15_v2_PlayersProfile, body);
} catch (err) {
this.emit('error', new Error(`Failed to decode PlayersProfile: ${err.message}`));
return;
}
if (!proto.account_profiles || !proto.account_profiles[0]) {
this.emit('debug', 'PlayersProfile missing account_profiles');
return;
}
const profile = proto.account_profiles[0];
if (!profile.account_id) {
this.emit('debug', 'PlayersProfile missing account_id');
return;
}
const 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;
try {
proto = decodeProto(Protos.CMsgGCCStrike15_v2_Client2GCEconPreviewDataBlockResponse, body);
} catch (err) {
this.emit('error', new Error(`Failed to decode Client2GCEconPreviewDataBlockResponse: ${err.message}`));
return;
}
if (!proto || !proto.iteminfo) {
this.emit('debug', 'Client2GCEconPreviewDataBlockResponse missing iteminfo');
return;
}
const item = proto.iteminfo;
// Validate critical fields
if (typeof item.itemid === 'undefined') {
this.emit('debug', 'Item inspection missing itemid');
return;
}
this._normalizeInspectItem(item);
this.emit('inspectItemInfo', item);
this.emit('inspectItemInfo#' + item.itemid, item);
};
// XP Shop & Rewards
handlers[Language.GC2ClientNotifyXPShop] = function (body) {
let proto;
try {
proto = decodeProto(Protos.CMsgGCCStrike15_v2_GC2ClientNotifyXPShop, body);
} catch (err) {
this.emit('error', new Error(`Failed to decode GC2ClientNotifyXPShop: ${err.message}`));
return;
}
if (!proto) {
this.emit('debug', 'GC2ClientNotifyXPShop missing data');
return;
}
this.emit('xpShopNotification', proto);
};
// Recurring Missions
handlers[Language.RecurringMissionSchema] = function (body) {
let proto;
try {
proto = decodeProto(Protos.CMsgRecurringMissionSchema, body);
} catch (err) {
this.emit('error', new Error(`Failed to decode RecurringMissionSchema: ${err.message}`));
return;
}
if (!proto) {
this.emit('debug', 'RecurringMissionSchema missing data');
return;
}
this.emit('recurringMissionSchema', proto);
};
// Premier Season
handlers[Language.PremierSeasonSummary] = function (body) {
let proto;
try {
proto = decodeProto(Protos.CMsgGCCStrike15_v2_PremierSeasonSummary, body);
} catch (err) {
this.emit('error', new Error(`Failed to decode PremierSeasonSummary: ${err.message}`));
return;
}
if (!proto) {
this.emit('debug', 'PremierSeasonSummary missing data');
return;
}
this.emit('premierSeasonSummary', proto);
};
// Matchmaking Search Stats
handlers[Language.MatchmakingGC2ClientSearchStats] = function (body) {
let proto;
try {
proto = decodeProto(Protos.CMsgGCCStrike15_v2_MatchmakingGC2ClientSearchStats, body);
} catch (err) {
this.emit('error', new Error(`Failed to decode MatchmakingGC2ClientSearchStats: ${err.message}`));
return;
}
if (!proto) {
this.emit('debug', 'MatchmakingGC2ClientSearchStats missing data');
return;
}
this.emit('matchmakingSearchStats', proto);
};
// Item manipulation
handlers[Language.CraftResponse] = function (body) {
const blueprint = body.readInt16(); // recipe ID
const unknown = body.readUint32(); // always 0 in my experience
const idCount = body.readUint16();
const idList = []; // let's form an array of IDs
for (let i = 0; i < idCount; i++) {
const 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;
try {
proto = decodeProto(Protos.CMsgGCItemCustomizationNotification, body);
} catch (err) {
this.emit('error', new Error(`Failed to decode ItemCustomizationNotification: ${err.message}`));
return;
}
if (!proto.item_id || proto.item_id.length == 0 || !proto.request) {
this.emit('debug', 'ItemCustomizationNotification missing required fields');
return;
}
this.emit('itemCustomizationNotification', proto.item_id, proto.request);
};
// SO
NodeCS2.prototype._processSOEconItem = function (item) {
// Inventory position
const isNew = (item.inventory >>> 30) & 1;
item.position = isNew ? 0 : item.inventory & 0xffff;
// Is this item contained in a casket?
const casketIdLow = getAttributeValueBytes(Constants.ATTRIB_CASKET_ID_LOW);
const casketIdHigh = getAttributeValueBytes(Constants.ATTRIB_CASKET_ID_HIGH);
if (casketIdLow && casketIdHigh) {
const casketIdLong = new Long(casketIdLow.readUInt32LE(0), casketIdHigh.readUInt32LE(0));
item.casket_id = casketIdLong.toString();
}
// Item custom names
const customNameBytes = getAttributeValueBytes(Constants.ATTRIB_CUSTOM_NAME);
if (customNameBytes && !item.custom_name) {
item.custom_name = customNameBytes.slice(2).toString('utf8');
}
// Paint index/seed/wear
const paintIndexBytes = getAttributeValueBytes(Constants.ATTRIB_PAINT_INDEX);
if (paintIndexBytes) {
item.paint_index = paintIndexBytes.readFloatLE(0);
}
const paintSeedBytes = getAttributeValueBytes(Constants.ATTRIB_PAINT_SEED);
if (paintSeedBytes) {
item.paint_seed = Math.floor(paintSeedBytes.readFloatLE(0));
}
const paintWearBytes = getAttributeValueBytes(Constants.ATTRIB_PAINT_WEAR);
if (paintWearBytes) {
item.paint_wear = paintWearBytes.readFloatLE(0);
}
const tradableAfterDateBytes = getAttributeValueBytes(Constants.ATTRIB_TRADABLE_AFTER_DATE);
if (tradableAfterDateBytes) {
item.tradable_after = new Date(tradableAfterDateBytes.readUInt32LE(0) * 1000);
}
const killEaterBytes = getAttributeValueBytes(Constants.ATTRIB_KILL_EATER_VALUE);
if (killEaterBytes) {
item.kill_eater_value = killEaterBytes.readUInt32LE(0);
}
const killEaterScoreTypeBytes = getAttributeValueBytes(Constants.ATTRIB_KILL_EATER_SCORE_TYPE);
if (killEaterScoreTypeBytes) {
item.kill_eater_score_type = killEaterScoreTypeBytes.readUInt32LE(0);
}
const questIdBytes = getAttributeValueBytes(Constants.ATTRIB_QUEST_ID);
if (questIdBytes) {
item.quest_id = questIdBytes.readUInt32LE(0);
}
const stickers = [];
for (let i = 0; i <= 5; i++) {
const stickerIdBytes = getAttributeValueBytes(Constants.ATTRIB_STICKER_ID_BASE + i * 4);
if (stickerIdBytes) {
const 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.
const schemaBytes = getAttributeValueBytes(Constants.ATTRIB_STICKER_SCHEMA_BASE + i);
if (schemaBytes) {
sticker.slot = schemaBytes.readUInt32LE(0);
}
['wear', 'scale', 'rotation'].forEach((attrib, idx) => {
const bytes = getAttributeValueBytes(Constants.ATTRIB_STICKER_WEAR_BASE + i * 4 + idx);
if (bytes) {
sticker[attrib] = bytes.readFloatLE(0);
}
});
['offset_x', 'offset_y'].forEach((attrib, idx) => {
const bytes = getAttributeValueBytes(Constants.ATTRIB_STICKER_OFFSET_BASE + 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 Constants.DEFINDEX_STORAGE_UNIT:
// Storage Unit
item.casket_contained_item_count = 0;
const itemCountBytes = getAttributeValueBytes(Constants.ATTRIB_CASKET_ITEM_COUNT);
if (itemCountBytes) {
item.casket_contained_item_count = itemCountBytes.readUInt32LE(0);
}
break;
}
/**
* @param {int} attribDefIndex
* @returns {null|Buffer}
*/
function getAttributeValueBytes(attribDefIndex) {
const attrib = (item.attribute || []).find((attrib) => attrib.def_index == attribDefIndex);
return attrib ? attrib.value_bytes : null;
}
};
handlers[Language.SO_Create] = function (body) {
let proto;
try {
proto = decodeProto(Protos.CMsgSOSingleObject, body);
} catch (err) {
this.emit('error', new Error(`Failed to decode SO_Create: ${err.message}`));
return;
}
this._handleSOCreate(proto);
};
NodeCS2.prototype._handleSOCreate = function (proto) {
if (!proto || proto.type_id != Constants.SO_TYPE_ECON_ITEM) {
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;
try {
item = decodeProto(Protos.CSOEconItem, proto.object_data);
} catch (err) {
this.emit('error', new Error(`Failed to decode SO_Create item: ${err.message}`));
return;
}
this._processSOEconItem(item);
this.inventory.push(item);
this.emit('itemAcquired', item);
};
handlers[Language.SO_Update] = function (body) {
let proto;
try {
proto = decodeProto(Protos.CMsgSOSingleObject, body);
} catch (err) {
this.emit('error', new Error(`Failed to decode SO_Update: ${err.message}`));
return;
}
this._handleSOUpdate(proto);
};
NodeCS2.prototype._handleSOUpdate = function (so) {
if (!so || so.type_id != Constants.SO_TYPE_ECON_ITEM) {
return; // Not an item, we don't care
}
if (!this.inventory) {
return; // We somehow don't have our inventory yet!
}
let item;
try {
item = decodeProto(Protos.CSOEconItem, so.object_data);
} catch (err) {
this.emit('error', new Error(`Failed to decode SO_Update item: ${err.message}`));
return;
}
if (!item || !item.id) {
this.emit('debug', 'SO_Update item missing id');
return;
}
this._processSOEconItem(item);
for (let i = 0; i < this.inventory.length; i++) {
if (this.inventory[i].id == item.id) {
const oldItem = this.inventory[i];
this.inventory[i] = item;
this.emit('itemChanged', oldItem, item);
break;
}
}
};
handlers[Language.SO_Destroy] = function (body) {
let proto;
try {
proto = decodeProto(Protos.CMsgSOSingleObject, body);
} catch (err) {
this.emit('error', new Error(`Failed to decode SO_Destroy: ${err.message}`));
return;
}
this._handleSODestroy(proto);
};
NodeCS2.prototype._handleSODestroy = function (proto) {
if (!proto || proto.type_id != Constants.SO_TYPE_ECON_ITEM) {
return; // Not an item
}
if (!this.inventory) {
return; // Inventory not loaded yet
}
let item;
try {
item = decodeProto(Protos.CSOEconItem, proto.object_data);
} catch (err) {
this.emit('error', new Error(`Failed to decode SO_Destroy item: ${err.message}`));
return;
}
if (!item || !item.id) {
this.emit('debug', 'SO_Destroy item missing id');
return;
}
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;
try {
proto = decodeProto(Protos.CMsgSOMultipleObjects, body);
} catch (err) {
this.emit('error', new Error(`Failed to decode SO_UpdateMultiple: ${err.message}`));
return;
}
(proto.objects_added || []).forEach((item) => this._handleSOCreate(item));
(proto.objects_modified || []).forEach((item) => this._handleSOUpdate(item));
(proto.objects_removed || []).forEach((item) => this._handleSODestroy(item));
};
NodeCS2.prototype._normalizeInspectItem = function (item) {
// decode the wear
if (typeof item.paintwear !== 'undefined') {
const buf = Buffer.alloc(4);
buf.writeUInt32BE(item.paintwear, 0);
item.paintwear = buf.readFloatBE(0);
}
// Process stickers array - using helper function for consistency
if (item.stickers && Array.isArray(item.stickers)) {
item.stickers = item.stickers.map((sticker) => this._mapStickerLikeItem(sticker));
}
// Process keychains array - using helper function for consistency
if (item.keychains && Array.isArray(item.keychains)) {
item.keychains = item.keychains.map((keychain) => this._mapStickerLikeItem(keychain));
}
// Process variations array - using helper function for consistency
if (item.variations && Array.isArray(item.variations)) {
item.variations = item.variations.map((variation) => this._mapStickerLikeItem(variation));
}
return item;
};