forked from Step7750/node-csgo-cdn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
914 lines (719 loc) · 27.8 KB
/
index.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
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
const Promise = require('bluebird');
const EventEmitter = require('events');
const fs = Promise.promisifyAll(require('fs'));
const vpk = require('vpk');
const vdf = require('simple-vdf');
const hasha = require('hasha');
const winston = require('winston');
const defaultConfig = {
directory: 'data',
updateInterval: 30000,
stickers: true,
graffiti: true,
characters: true,
musicKits: true,
cases: true,
tools: true,
statusIcons: true,
logLevel: 'info',
customLangFile: 'csgo_russian.txt'
};
const wears = ['Factory New', 'Minimal Wear', 'Field-Tested', 'Well-Worn', 'Battle-Scarred'];
const neededDirectories = {
stickers: 'resource/flash/econ/stickers',
graffiti: 'resource/flash/econ/stickers/default',
characters: 'resource/flash/econ/characters',
musicKits: 'resource/flash/econ/music_kits',
cases: 'resource/flash/econ/weapon_cases',
tools: 'resource/flash/econ/tools',
statusIcons: 'resource/flash/econ/status_icons',
};
function bytesToMB(bytes) {
return (bytes/1000000).toFixed(2);
}
class CSGOCdn extends EventEmitter {
get ready() {
return this.ready_ || false;
}
get steamReady() {
return !!this.user.steamID;
}
get phase() {
return {
ruby: 'am_ruby_marbleized',
sapphire: 'am_sapphire_marbleized',
blackpearl: 'am_blackpearl_marbleized',
emerald: 'am_emerald_marbleized',
phase1: 'phase1',
phase2: 'phase2',
phase3: 'phase3',
phase4: 'phase4'
}
}
set ready(r) {
const old = this.ready;
this.ready_ = r;
if (r !== old && r) {
this.log.debug('Ready');
this.emit('ready');
}
}
constructor(steamUser, config={}) {
super();
this.config = Object.assign(defaultConfig, config);
this.createDataDirectory();
this.user = Promise.promisifyAll(steamUser, {multiArgs: true});
this.log = winston.createLogger({
level: config.logLevel,
transports: [
new winston.transports.Console({
colorize: true,
format: winston.format.printf((info) => {
return `[csgo-image-cdn] ${info.level}: ${info.message}`;
})
})
]
});
if (!this.steamReady) {
this.log.debug('Steam not ready, waiting for logon');
this.user.once('loggedOn', () => {
this.updateLoop();
});
}
else {
this.updateLoop();
}
}
/**
* Creates the data directory specified in the config if it doesn't exist
*/
createDataDirectory() {
const dir = `./${this.config.directory}`;
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
}
/**
* Runs the update loop at the specified config interval
* @return {Promise<undefined>|void}
*/
updateLoop() {
if (this.config.updateInterval > 0) {
return this.update().then(() => Promise.delay(this.config.updateInterval*1000))
.then(() => this.updateLoop());
}
else {
this.log.info('Auto-updates disabled, checking if required files exist');
// Try to load the resources locally
try {
this.loadResources();
this.loadVPK();
this.ready = true;
} catch(e) {
this.log.warn('Needed CS:GO files not installed');
this.update();
}
}
}
/**
* Returns the product info for CSGO, with its depots and packages
*/
getProductInfo() {
this.log.debug('Obtaining CS:GO product info');
return new Promise((resolve, reject) => {
this.user.getProductInfo([730], [], true, (apps, packages, unknownApps, unknownPackages) => {
resolve([apps, packages, unknownApps, unknownPackages]);
});
});
}
/**
* Returns the latest CSGO manifest ID for the public 731 depot
* @return {*|PromiseLike<*[]>|Promise<*[]>} 731 Depot Manifest ID
*/
getLatestManifestId() {
this.log.debug('Obtaining latest manifest ID');
return this.getProductInfo().then(([apps, packages, unknownApps, unknownPackages]) => {
const csgo = packages['730'].appinfo;
const commonDepot = csgo.depots['731'];
return commonDepot.manifests.public;
});
}
/**
* Retrieves and updates the sticker file directory from Valve
*
* Ensures that only the required VPK files are downloaded and that files with the same SHA1 aren't
* redownloaded
*
* @return {Promise<void>}
*/
async update() {
this.log.info('Checking for CS:GO file updates');
if (!this.steamReady) {
this.log.warn(`Steam not ready, can't check for updates`);
return;
}
const manifestId = await this.getLatestManifestId();
this.log.debug(`Obtained latest manifest ID: ${manifestId}`);
const [manifest] = await this.user.getManifestAsync(730, 731, manifestId);
const manifestFiles = manifest.files;
const dirFile = manifest.files.find((file) => file.filename.endsWith("csgo\\pak01_dir.vpk"));
const itemsGameFile = manifest.files.find((file) => file.filename.endsWith("items_game.txt"));
const itemsGameCDNFile = manifest.files.find((file) => file.filename.endsWith("items_game_cdn.txt"));
const csgoEnglishFile = manifest.files.find((file) => file.filename.endsWith("csgo_english.txt"));
const csgoCustomLangFile = manifest.files.find((file) => file.filename.endsWith(this.config.customLangFile));
this.log.debug(`Downloading required static files`);
await this.downloadFiles([dirFile, itemsGameFile, itemsGameCDNFile, csgoEnglishFile, csgoCustomLangFile]);
this.log.debug('Loading static file resources');
this.loadResources();
this.loadVPK();
await this.downloadVPKFiles(this.vpkDir, manifestFiles);
this.ready = true;
}
loadResources() {
this.itemsGame = vdf.parse(fs.readFileSync(`${this.config.directory}/items_game.txt`, 'utf8'))['items_game'];
this.csgoEnglish = vdf.parse(fs.readFileSync(`${this.config.directory}/csgo_english.txt`, 'ucs2'))['lang']['Tokens'];
this.csgoCustomLang = vdf.parse(fs.readFileSync(`${this.config.directory}/${this.config.customLangFile}`, 'ucs2'))['lang']['Tokens'];
this.itemsGameCDN = this.parseItemsCDN(fs.readFileSync(`${this.config.directory}/items_game_cdn.txt`, 'utf8'));
this.weaponNameMap = Object.keys(this.csgoEnglish).filter(n => n.startsWith("SFUI_WPNHUD"));
this.csgoEnglishKeys = Object.keys(this.csgoEnglish);
// Ensure paint kit descriptions are lowercase to resolve inconsistencies in the language and items_game file
Object.keys(this.itemsGame.paint_kits).forEach((n) => {
const kit = this.itemsGame.paint_kits[n];
if ('description_tag' in kit) {
kit.description_tag = kit.description_tag.toLowerCase();
}
});
this.invertDictionary(this.csgoEnglish);
}
/**
* Inverts the key mapping of a dictionary recursively while preserving the original keys
*
* Duplicate values with be an array
*
* @param dict Dictionary to invert
*/
invertDictionary(dict) {
dict['inverted'] = {};
for (const prop in dict) {
if (prop === 'inverted' || !dict.hasOwnProperty(prop)) continue;
const val = dict[prop];
if (typeof val === 'object' && !(val instanceof Array)) {
this.invertDictionary(val);
}
else {
if (dict['inverted'][val] === undefined) {
dict['inverted'][val] = [prop];
}
else {
dict['inverted'][val].push(prop);
}
}
}
}
parseItemsCDN(data) {
let lines = data.split('\n');
const items_game_cdn = {};
for (let line of lines) {
let kv = line.split('=');
if (kv[1]) {
items_game_cdn[kv[0]] = kv[1];
}
}
return items_game_cdn;
}
/**
* Downloads the given VPK files from the Steam CDN
* @param files Steam Manifest File Array
* @return {Promise<>} Fulfilled when completed downloading
*/
async downloadFiles(files) {
const promises = [];
for (const file of files) {
let name = file.filename.split('\\');
name = name[name.length-1];
const path = `${this.config.directory}/${name}`;
const isDownloaded = await this.isFileDownloaded(path, file.sha_content);
if (isDownloaded) {
continue;
}
const promise = this.user.downloadFile(730, 731, file, `${this.config.directory}/${name}`);
promises.push(promise);
}
return Promise.all(promises);
}
/**
* Loads the CSGO dir VPK specified in the config
*/
loadVPK() {
this.vpkDir = new vpk(this.config.directory + '/pak01_dir.vpk');
this.vpkDir.load();
this.vpkFiles = this.vpkDir.files.filter((f) => f.startsWith('resource/flash/econ/stickers'));
}
/**
* Given the CSGO VPK Directory, returns the necessary indices for the chosen options
* @param vpkDir CSGO VPK Directory
* @return {Array} Necessary Sticker VPK Indices
*/
getRequiredVPKFiles(vpkDir) {
const requiredIndices = [];
const neededDirs = Object.keys(neededDirectories).filter((f) => !!this.config[f]).map((f) => neededDirectories[f]);
for (const fileName of vpkDir.files) {
for (const dir of neededDirs) {
if (fileName.startsWith(dir)) {
const archiveIndex = vpkDir.tree[fileName].archiveIndex;
if (!requiredIndices.includes(archiveIndex)) {
requiredIndices.push(archiveIndex);
}
break;
}
}
}
return requiredIndices.sort();
}
/**
* Downloads the required VPK files
* @param vpkDir CSGO VPK Directory
* @param manifestFiles Manifest files
* @return {Promise<void>}
*/
async downloadVPKFiles(vpkDir, manifestFiles) {
this.log.debug('Computing required VPK files for selected packages');
const requiredIndices = this.getRequiredVPKFiles(vpkDir);
this.log.debug(`Required VPK files ${requiredIndices}`);
for (let index in requiredIndices) {
index = parseInt(index);
// pad to 3 zeroes
const archiveIndex = requiredIndices[index];
const paddedIndex = '0'.repeat(3-archiveIndex.toString().length) + archiveIndex;
const fileName = `pak01_${paddedIndex}.vpk`;
const file = manifestFiles.find((f) => f.filename.endsWith(fileName));
const filePath = `${this.config.directory}/${fileName}`;
const isDownloaded = await this.isFileDownloaded(filePath, file.sha_content);
if (isDownloaded) {
this.log.info(`Already downloaded ${filePath}`);
continue;
}
const status = `[${index+1}/${requiredIndices.length}]`;
this.log.info(`${status} Downloading ${fileName} - ${bytesToMB(file.size)} MB`);
await this.user.downloadFile(730, 731, file, filePath, (none, { type, bytesDownloaded, totalSizeBytes }) => {
if (type === 'progress') {
this.log.info(`${status} ${(bytesDownloaded*100/totalSizeBytes).toFixed(2)}% - ${bytesToMB(bytesDownloaded)}/${bytesToMB(totalSizeBytes)} MB`);
}
});
this.log.info(`${status} Downloaded ${fileName}`);
}
}
/**
* Returns whether a file at the given path has the given sha1
* @param path File path
* @param sha1 File SHA1 hash
* @return {Promise<boolean>} Whether the file has the hash
*/
async isFileDownloaded(path, sha1) {
try {
const hash = await hasha.fromFile(path, {algorithm: 'sha1'});
return hash === sha1;
}
catch (e) {
return false;
}
}
/**
* Given a VPK path, returns the CDN URL
* @param path VPK path
* @return {string|void} CDN URL
*/
getPathURL(path) {
const file = this.vpkDir.getFile(path);
if (!file) {
this.log.error(`Failed to retrieve ${path} in VPK, do you have the package category enabled in options?`);
return;
}
const sha1 = hasha(file, {
'algorithm': 'sha1'
});
path = path.replace('resource/flash', 'icons');
path = path.replace('.png', `.${sha1}.png`);
return `https://steamcdn-a.akamaihd.net/apps/730/${path}`;
}
/**
* Returns the item Steam CDN URL for the specified name
*
* Example Sticker Names: cologne2016/nv, cologne2016/fntc_holo, cologne2016/fntc_foil, cluj2015/sig_olofmeister_gold
*
* You can find the sticker names from their relevant "sticker_material" fields in items_game.txt
* items_game.txt can be found in the core game files of CS:GO or as itemsGame here
*
* @param name The item name (the sticker_material field in items_game.txt, or the cdn file format)
* @param large Whether to obtain the "large" CDN version of the item
* @return {string|void} If successful, the HTTPS CDN URL for the item
*/
getStickerURL(name, large=true) {
if (!this.ready) {
return;
}
const fileName = large ? `${name}_large.png` : `${name}.png`;
const path = this.vpkFiles.find((t) => t.endsWith(fileName));
if (path) return this.getPathURL(path);
}
/**
* Given the specified defindex and paintindex, returns the CDN URL
*
* The item properties can be found in items_game.txt
*
* @param defindex Item Definition Index (weapon type)
* @param paintindex Item Paint Index (skin type)
* @return {string|void} Weapon CDN URL
*/
getWeaponURL(defindex, paintindex) {
if (!this.ready) return;
const paintKits = this.itemsGame.paint_kits;
// Get the skin name
let skinName = '';
if (paintindex in paintKits) {
skinName = paintKits[paintindex].name;
if (skinName === 'default') {
skinName = '';
}
}
// Get the weapon name
let weaponName;
const items = this.itemsGame.items;
if (defindex in items) {
weaponName = items[defindex].name;
}
// Get the image url
const cdnName = `${weaponName}_${skinName}`;
return this.itemsGameCDN[cdnName];
}
/**
* Returns whether the given name is a weapon by checking
* the prefab and whether it is used by one of the sides
* @param marketHashName Item name
* @return {boolean} Whether a weapon
*/
isWeapon(marketHashName) {
const prefabs = this.itemsGame.prefabs;
const items = this.itemsGame.items;
const weaponName = marketHashName.split('|')[0].trim();
const weaponTags = this.csgoEnglish['inverted'][weaponName];
if (!weaponTags) return false;
// For every matching weapon tag...
for (const t of weaponTags) {
const weaponTag = `#${t}`;
const prefab = Object.keys(prefabs).find((n) => {
const fab = prefabs[n];
return fab.item_name === weaponTag;
});
let fab;
if (!prefab) {
// special knives aren't in the prefab (karambits, etc...)
const item = Object.keys(items).find((n) => {
const i = items[n];
return i.item_name === weaponTag;
});
fab = items[item];
}
else {
fab = prefabs[prefab];
}
if (fab && fab.used_by_classes) {
const used = fab.used_by_classes;
// Ensure that the item is used by one of the sides
if (used['terrorists'] || used['counter-terrorists']) {
return true;
}
}
}
return false;
}
/**
* Returns the sticker URL given the market hash name
* @param marketHashName Sticker name
* @return {string|void} Sticker image URL
*/
getStickerNameURL(marketHashName) {
const reg = /Sticker \| (.*)/;
const match = marketHashName.match(reg);
if (!match) return;
const stickerName = match[1];
for (const tag of this.csgoEnglish['inverted'][stickerName] || []) {
const stickerTag = `#${tag}`;
const stickerKits = this.itemsGame.sticker_kits;
const kitIndex = Object.keys(stickerKits).find((n) => {
const k = stickerKits[n];
return k.item_name === stickerTag;
});
const kit = stickerKits[kitIndex];
if (!kit || !kit.sticker_material) continue;
const url = this.getStickerURL(stickerKits[kitIndex].sticker_material, true);
if (url) {
return url;
}
}
}
/**
* Returns the graffiti URL given the market hash name
* @param marketHashName Graffiti name (optional tint)
* @param large Whether to obtain the "large" CDN version of the item
* @return {string|void} CDN Image URL
*/
getGraffitiNameURL(marketHashName, large=true) {
const reg = /Sealed Graffiti \| ([^(]*)/;
const match = marketHashName.match(reg);
if (!match) return;
const graffitiName = match[1].trim();
for (const tag of this.csgoEnglish['inverted'][graffitiName] || []) {
const stickerTag = `#${tag}`;
const stickerKits = this.itemsGame.sticker_kits;
const kitIndices = Object.keys(stickerKits).filter((n) => {
const k = stickerKits[n];
return k.item_name === stickerTag;
});
// prefer kit indices with "graffiti" in the name
kitIndices.sort((a, b) => {
const index1 = !!stickerKits[a].name && stickerKits[a].name.indexOf('graffiti');
const index2 = !!stickerKits[b].name && stickerKits[b].name.indexOf('graffiti');
if (index1 === index2) {
return 0
} else if (index1 > -1) {
return -1
} else {
return 1
}
});
for (const kitIndex of kitIndices) {
const kit = stickerKits[kitIndex];
if (!kit || !kit.sticker_material) continue;
const url = this.getStickerURL(kit.sticker_material, true);
if (url) {
return url;
}
}
}
}
/**
* Returns the weapon URL given the market hash name
* @param marketHashName Weapon name
* @param {string?} phase Optional Doppler Phase from the phase enum
* @return {string|void} Weapon image URL
*/
getWeaponNameURL(marketHashName, phase) {
const hasWear = wears.findIndex((n) => marketHashName.includes(n)) > -1;
if (hasWear) {
// remove it
marketHashName = marketHashName.replace(/\([^)]*\)$/, '');
}
const match = marketHashName.split('|').map((m) => m.trim());
const weaponName = match[0];
const skinName = match[1];
if (!weaponName) return;
const weaponTags = this.csgoEnglish['inverted'][weaponName] || [];
const prefabs = this.itemsGame.prefabs;
const items = this.itemsGame.items;
// For every matching weapon tag...
for (const t of weaponTags) {
const weaponTag = `#${t}`;
const prefab = Object.keys(prefabs).find((n) => {
const fab = prefabs[n];
return fab.item_name === weaponTag;
});
let weaponClass;
if (!prefab) {
// special knives aren't in the prefab (karambits, etc...)
const item = Object.keys(items).find((n) => {
const i = items[n];
return i.item_name === weaponTag;
});
if (items[item]) {
weaponClass = items[item].name;
}
}
else {
const item = Object.keys(items).find((n) => {
const i = items[n];
return i.prefab === prefab;
});
if (items[item]) {
weaponClass = items[item].name;
}
}
if (!weaponClass) continue;
// Check if this is a vanilla weapon
if (!skinName) {
if (weaponClass && this.itemsGameCDN[weaponClass]) {
return this.itemsGameCDN[weaponClass];
}
else {
continue;
}
}
// For every matching skin name...
for (const key of this.csgoEnglish['inverted'][skinName] || []) {
const skinTag = `#${key.toLowerCase()}`;
const paintKits = this.itemsGame.paint_kits;
const paintindexes = Object.keys(paintKits).filter((n) => {
const kit = paintKits[n];
const isPhase = !phase || kit.name.endsWith(phase);
return isPhase && kit.description_tag === skinTag;
});
// For every matching paint index...
for (const paintindex of paintindexes) {
const paintKit = paintKits[paintindex].name;
const path = (paintKit ? `${weaponClass}_${paintKit}` : weaponClass).toLowerCase();
if (this.itemsGameCDN[path]) {
return this.itemsGameCDN[path];
}
}
}
}
}
/**
* Returns the music kit URL given the market hash name
* @param marketHashName Music kit name
* @return {string|void} Music kit image URL
*/
getMusicKitNameURL(marketHashName) {
const reg = /Music Kit \| (.*)/;
const match = marketHashName.match(reg);
if (!match) return;
const kitName = match[1];
for (const t of this.csgoEnglish['inverted'][kitName] || []) {
const tag = `#${t}`;
const musicDefs = this.itemsGame.music_definitions;
const kitIndex = Object.keys(musicDefs).find((n) => {
const k = musicDefs[n];
return k.loc_name === tag;
});
const kit = musicDefs[kitIndex];
if (!kit || !kit.image_inventory) continue;
const path = `resource/flash/${kit.image_inventory}.png`;
const url = this.getPathURL(path);
if (url) {
return url;
}
}
}
/**
* Retrieves the given item CDN URL given its market_hash_name
*
* Examples: M4A4 | 龍王 (Dragon King) (Field-Tested), Sticker | Robo, AWP | Redline (Field-Tested)
*
* Note: For a weapon, the name MUST have the associated wear
*
* @param marketHashName Item name
* @param {string?} phase Optional Doppler Phase from the phase enum
*/
getItemNameURL(marketHashName, phase) {
marketHashName = marketHashName.trim();
let strippedMarketHashName = marketHashName;
// Weapons and Music Kits can have extra tags we need to ignore
const extraTags = ['★ ', 'StatTrak™ ', 'Souvenir '];
for (const tag of extraTags) {
if (strippedMarketHashName.startsWith(tag)) {
strippedMarketHashName = strippedMarketHashName.replace(tag, '');
}
}
if (this.isWeapon(strippedMarketHashName)) {
return this.getWeaponNameURL(strippedMarketHashName, phase);
}
else if (strippedMarketHashName.startsWith('Music Kit |')) {
return this.getMusicKitNameURL(strippedMarketHashName);
}
else if (marketHashName.startsWith('Sticker |')) {
return this.getStickerNameURL(marketHashName);
}
else if (marketHashName.startsWith('Sealed Graffiti |')) {
return this.getGraffitiNameURL(marketHashName);
}
else {
// Other in items
for (const t of this.csgoEnglish['inverted'][marketHashName] || []) {
const tag = `#${t.toLowerCase()}`;
const items = this.itemsGame.items;
const prefabs = this.itemsGame.prefabs;
let item = Object.keys(items).find((n) => {
const i = items[n];
return i.item_name && i.item_name.toLowerCase() === tag;
});
let path;
if (!items[item] || !items[item].image_inventory) {
// search the prefabs (ex. CS:GO Case Key)
item = Object.keys(prefabs).find((n) => {
const i = prefabs[n];
return i.item_name && i.item_name.toLowerCase() === tag;
});
if (!prefabs[item] || !prefabs[item].image_inventory) continue;
path = `resource/flash/${prefabs[item].image_inventory}.png`;
}
else {
path = `resource/flash/${items[item].image_inventory}.png`;
}
const url = this.getPathURL(path);
if (url) {
return url;
}
}
}
}
/**
*
* @returns {Array}
*/
getStickersPack() {
const pack = [];
const kits = this.itemsGame.sticker_kits;
for (let defindex in kits) {
if (!kits.hasOwnProperty(defindex)) continue;
const sticker_material = kits[defindex].sticker_material;
if (!sticker_material) continue;
const hashes = this.getStickerHashes(sticker_material);
const names = this.getStickerNames(defindex);
if (hashes.hasOwnProperty(0) && hashes.hasOwnProperty(1)) {
pack.push({
defindex: defindex,
material: sticker_material,
hash: hashes[0],
hash_large: hashes[1],
custom_name: names.custom,
english_name: names.english
});
}
}
return pack;
}
/**
*
* @param defindex
* @returns {*}
*/
getStickerNames(defindex) {
const name = this.itemsGame.sticker_kits[defindex].item_name.slice(1);
return {
custom: this.csgoCustomLang[name],
english: this.csgoEnglish[name],
};
}
/**
*
* @param material
* @returns []
*/
getStickerHashes(material) {
const names = [`${material}.png`, `${material}_large.png`];
return names.filter((name) => {
let path = this.vpkFiles.find((t) => t.endsWith(name));
if (!path) {
return false;
}
return !!this.vpkDir.getFile(path);
}).map((name) => {
let path = this.vpkFiles.find((t) => t.endsWith(name));
let file = this.vpkDir.getFile(path);
return hasha(file, {
'algorithm': 'sha1'
});
});
}
}
module.exports = CSGOCdn;