Repository navigation
Expand file tree
/
Copy pathStruct.test.mts
More file actions
819 lines (752 loc) · 26 KB
/
Copy pathStruct.test.mts
File metadata and controls
819 lines (752 loc) · 26 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
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
import { describe, test, expect } from "vitest";
import {
createDynamicClassInstance,
ERank,
pad,
Struct,
ArmorPrototype,
Refs,
QuestNodePrototypeRandom,
QuestNodePrototypeLaunchers,
toBaseGameRefurl,
toMountRefurl,
} from "./Struct.mjs";
import fs from "node:fs";
class ChimeraHPFix extends Struct {
__internal__ = new Refs({
rawName: "ChimeraHPFix",
isRoot: true,
bskipref: true,
});
MaxHP = 750;
}
class TradePrototype extends Struct {
__internal__ = new Refs({
rawName: "TradersDontBuyWeaponsArmor",
refurl: "../TradePrototypes.cfg",
refkey: 0,
isRoot: true,
});
TradeGenerators = new TradeGenerators();
}
class TradeGenerators extends Struct {
__internal__ = new Refs({
isArray: true,
rawName: "TradeGenerators",
//useAsterisk: true, this option is not supported for now
});
"0" = new TradeGenerator();
}
class TradeGenerator extends Struct {
__internal__ = new Refs({
rawName: "0",
});
BuyLimitations = new BuyLimitations();
}
class BuyLimitations extends Struct {
__internal__ = new Refs({
rawName: "BuyLimitations",
isArray: true,
});
"0" = "EItemType::Weapon";
"1" = "EItemType::Armor";
"2" = new Struct({
__internal__: {
removenode: true,
},
});
}
describe("Struct", () => {
test("toString()", () => {
const c = new ChimeraHPFix();
expect(c.MaxHP).toBe(750);
expect(new ChimeraHPFix().toString()).toBe(
`ChimeraHPFix : struct.begin {bskipref}
MaxHP = 750
struct.end`,
);
expect(new TradePrototype().toString()).toBe(
`TradersDontBuyWeaponsArmor : struct.begin {refurl=../TradePrototypes.cfg;refkey=[0]}
TradeGenerators : struct.begin
[0] : struct.begin
BuyLimitations : struct.begin
[0] = EItemType::Weapon
[1] = EItemType::Armor
[2] : removenode
struct.end
struct.end
struct.end
struct.end`,
);
});
test("pad()", () => {
expect(pad("test")).toBe(" test");
expect(pad(pad("test"))).toBe(" test");
});
describe("createDynamicClassInstance", () => {
test("1", () => {
const instance = createDynamicClassInstance("DynamicClass");
instance.__internal__.isRoot = true;
expect(instance).toBeInstanceOf(Struct);
expect(instance.toString()).toBe(
`DynamicClass : struct.begin\nstruct.end`,
);
});
test("reserved keyword name", () => {
const instance = createDynamicClassInstance("default");
instance.__internal__.isRoot = true;
expect(instance).toBeInstanceOf(Struct);
expect(instance.toString()).toBe(`default : struct.begin\nstruct.end`);
});
});
describe("fromString()", () => {
test("1", () => {
const chimeraText = new ChimeraHPFix().toString();
expect(Struct.fromString(chimeraText)[0].toString()).toBe(chimeraText);
});
test("2", () => {
const complexStructText = `BasePhantomAttack : struct.begin {refkey=BaseAttackAbility}
TriggeredCooldowns : struct.begin
[0] : struct.begin
CooldownTag = Ability.Cooldown.RunAttack
Duration = 50
struct.end
struct.end
Effects : struct.begin
[0] : struct.begin
EffectPrototypeSID = MutantMediumAttackCameraShake
Chance = 1
struct.end
struct.end
struct.end`;
expect(Struct.fromString(complexStructText)[0].toString()).toBe(
complexStructText,
);
});
test("3", () => {
const dynamicItemGeneratorText = `DynamicTraderItemGenerator : struct.begin {refurl=../ItemGeneratorPrototypes.cfg;refkey=[0]}
SID = DynamicTraderItemGenerator
ItemGenerator : struct.begin
[0] : struct.begin
Category = EItemGenerationCategory::WeaponPrimary
PlayerRank = ERank::Newbie
bAllowSameCategoryGeneration = true
ReputationThreshold = -500
RefreshTime = 1h
PossibleItems : struct.begin
[0] : struct.begin
ItemPrototypeSID = GunMark_SP
Weight = 1
MinDurability = 0.3
MaxDurability = 0.6
AmmoMinCount = 600
AmmoMaxCount = 900
struct.end
struct.end
struct.end
[1] : struct.begin
Category = EItemGenerationCategory::BodyArmor
PlayerRank = ERank::Newbie
bAllowSameCategoryGeneration = true
ReputationThreshold = -500
RefreshTime = 1h
PossibleItems : struct.begin
[*] : struct.begin
ItemPrototypeSID = DutyArmor_3_U1
Weight = 1
MinDurability = 1
MaxDurability = 1
struct.end
struct.end
struct.end
[2] : struct.begin
Category = EItemGenerationCategory::WeaponPistol
ReputationThreshold = -500
bAllowSameCategoryGeneration = true
RefreshTime = 1h
PossibleItems : struct.begin
[0] : struct.begin
ItemPrototypeSID = GunAPB_HG
Weight = 1
MinDurability = 0.25
MaxDurability = 0.5
AmmoMinCount = 60
AmmoMaxCount = 100
struct.end
struct.end
struct.end
[3] : struct.begin
Category = EItemGenerationCategory::WeaponSecondary
PlayerRank = ERank::Veteran
bAllowSameCategoryGeneration = true
RefreshTime = 1h
PossibleItems : struct.begin
[0] : struct.begin
ItemPrototypeSID = GunTOZ_SG
Weight = 1
MinDurability = 0.3
MaxDurability = 0.6
AmmoMinCount = 60
AmmoMaxCount = 90
struct.end
struct.end
struct.end
[4] : struct.begin
Category = EItemGenerationCategory::Head
PlayerRank = ERank::Veteran
bAllowSameCategoryGeneration = true
ReputationThreshold = -500
RefreshTime = 1h
PossibleItems : struct.begin
[0] : struct.begin
ItemPrototypeSID = DutyMask_1
Weight = 2
MinDurability = 1
MaxDurability = 1
struct.end
struct.end
struct.end
struct.end
struct.end`;
expect(Struct.fromString(dynamicItemGeneratorText)[0].toString()).toBe(
dynamicItemGeneratorText,
);
});
test("4", () => {
const dynamicItemGeneratorText = `A : struct.begin
N1 = 0.1f
N2 = 1.
N3 = 0.f
N4 = .1
N5 = .1f
N6 = -2.22f
struct.end`;
const str = Struct.fromString<{ [key: `N${number}`]: number }>(
dynamicItemGeneratorText,
);
expect(str[0].N1).toBe(0.1);
expect(str[0].N2).toBe(1);
expect(str[0].N3).toBe(0);
expect(str[0].N4).toBe(0.1);
expect(str[0].N5).toBe(0.1);
expect(str[0].N6).toBe(-2.22);
});
test("5", () => {
const testCfg = fs.readFileSync("./test.cfg", "utf-8").trim();
const structs = Struct.fromString(testCfg);
expect(structs.map((s) => s.toString()).join("\n")).toBe(testCfg);
});
});
describe("fromBinary()", () => {
test("1", () => {
const binary = fs.readFileSync("./test.cfg.bin");
const bin_data = Struct.fromBinary(binary);
const cfg = `
[0] : struct.begin
ID = 0
SID = empty
HiddenLevels : struct.begin
[0] =
struct.end
struct.end
[1] : struct.begin {refkey=[0]}
ID = 1
SID = WorldMap
HiddenLevels : struct.begin
[0] = 06_Church_VaultOpen_block
[1] = E06_MQ02_C03_LogicLevel
struct.end
struct.end`.trim();
const cfg_data = Struct.fromString(cfg);
expect(bin_data.length).toBe(cfg_data.length);
expect(bin_data.map((s) => s.toString()).join("\n")).toBe(
cfg_data.map((s) => s.toString()).join("\n"),
);
expect(bin_data.map((s) => s.toJson())).toEqual(
cfg_data.map((s) => s.toJson()),
);
});
test("field block without int32 terminator", () => {
// Regression: some field blocks end with only a 1-byte marker instead of
// a 4-byte zero terminator. The following block's bytes then read as an
// out-of-range value (a pool-index shifted by the 1-byte drift), which
// used to make the reader over-read and eventually throw
// "Unexpected end of binary cfg data." Values are 1-based string-pool
// indices, so anything beyond the pool size marks the block boundary.
// The QuadKey field below is such a block ([4,4,5,2] with no int32 zero).
const bin = Buffer.from(
"010000000600000000000000040000005b305d000a00000053696d706c654b657900" +
"0a00000053696d706c6556616c0008000000517561644b657900080000005175616456" +
"616c00000000000000000000010000000100000001000000000000000003000000" +
"020000000200000003000000000000000004000000040000000500000002000000" +
"000500000005000000030000000000000000",
"hex",
);
const cfg = `
[0] : struct.begin
SimpleKey = SimpleVal
QuadKey = QuadVal
QuadVal = SimpleVal
struct.end`.trim();
const bin_data = Struct.fromBinary(bin);
expect(bin_data.map((s) => s.toString()).join("\n")).toBe(cfg);
});
test("serializes adjacent roots on separate lines", () => {
const binary = fs.readFileSync(
"./CharacterWeaponSettingsPrototypes.cfg.bin",
);
const cfg = Struct.fromBinary(binary)
.map((s) => s.toString())
.join("\n");
expect(cfg).toContain("struct.end\nGunDniproDuty_ST_Player :");
expect(cfg).not.toContain("struct.end,GunDniproDuty_ST_Player");
});
test("keeps the original float literal", () => {
const binary = fs.readFileSync(
"./CharacterWeaponSettingsPrototypes.cfg.bin",
);
const [, dnipro] = Struct.fromBinary(binary);
expect(dnipro.toString()).toContain("ArmorPiercing = 3.2");
expect(dnipro["ArmorPiercing"]).toBe(3.2);
});
test("v2 header: PassiveDetectorPrototypes", () => {
const binary = fs.readFileSync("./test-v2.cfg.bin");
const bin_data = Struct.fromBinary(binary);
// Binary may include inherited fields from refkey=[0] that the
// authored text cfg omits (e.g. DangerLevelSoundParameter on [1]/[3]).
const cfg = `
[0] : struct.begin
ID = 0
SID = Empty
Type = EPassiveDetectorType::None
DangerLevelSoundParameter = AkRtpc'/Game/_STALKER2/Audio/WwiseAudio/Game_Parameters/Equipment/AnomalyDetectorDangerLevel.AnomalyDetectorDangerLevel'
DetectorRadius = 0.f
DetectorWorkSFX = AkAudioEvent''
SignalCurve = CurveFloat''
DetectorWorkCurve = CurveFloat''
struct.end
[1] : struct.begin {refkey=[0]}
ID = 1
SID = PlayerDetector
Type = EPassiveDetectorType::Anomaly
DetectorRadius = 1000.0
DetectorWorkSFX = AkAudioEvent'/Game/_STALKER2/Audio/WwiseAudio/Events/Detectors/AnomalyDetectorSignal/SFX_AnomalyDetector_Signal.SFX_AnomalyDetector_Signal'
SignalCurve = CurveFloat'/Game/GameLite/Resources/Detector/Gilka/DetectAnomalySoundPitchCurve.DetectAnomalySoundPitchCurve'
DetectorWorkCurve = CurveFloat'/Game/GameLite/Resources/Detector/Gilka/DetectAnomalySoundFrequencyCurve.DetectAnomalySoundFrequencyCurve'
ExclusionAnomalyList : struct.begin
[0] = RazorAnomaly
[1] = PsyAnomaly
[2] = ArchiBulbAnomaly
[3] = PillowAnomaly
[4] = ClickerAnomaly
[5] = LightningBallSmallAnomaly
[6] = LightningBallMediumAnomaly
[7] = LightningBallBigAnomaly
[8] = FlycatcherAnomaly
struct.end
DangerLevelSoundParameter = AkRtpc'/Game/_STALKER2/Audio/WwiseAudio/Game_Parameters/Equipment/AnomalyDetectorDangerLevel.AnomalyDetectorDangerLevel'
struct.end
[2] : struct.begin {refkey=[0]}
ID = 2
SID = PlayerSearchpointDetector
Type = EPassiveDetectorType::Searchpoint
DetectorRadius = 1000.0
DetectorWorkSFX = /Script/AkAudio.AkAudioEvent'/Game/_STALKER2/Audio/WwiseAudio/Events/Equipment/PDAScaner/SFX_NT_SearchPoint_Signal.SFX_NT_SearchPoint_Signal'
DangerLevelSoundParameter = /Script/AkAudio.AkRtpc'/Game/_STALKER2/Audio/WwiseAudio/Game_Parameters/Equipment/RTPC_NT_SearchPoint_Distance.RTPC_NT_SearchPoint_Distance'
SignalCurve = CurveFloat'/Game/GameLite/Resources/Detector/Gilka/DetectAnomalySoundPitchCurve.DetectAnomalySoundPitchCurve'
DetectorWorkCurve = CurveFloat'/Game/GameLite/Resources/Detector/Gilka/DetectAnomalySoundFrequencyCurve.DetectAnomalySoundFrequencyCurve'
struct.end
[3] : struct.begin {refkey=[0]}
ID = 3
SID = CollarSearchpointDetector
Type = EPassiveDetectorType::Searchpoint
DetectorRadius = 1000.0
DetectorWorkSFX = AkAudio.AkAudioEvent'/Game/_STALKER2/Audio/WwiseAudio/Events/Quests/TestEvents/TEST_Device_WhiteNoise.TEST_Device_WhiteNoise'
SignalCurve = CurveFloat'/Game/GameLite/Resources/Detector/Gilka/DetectAnomalySoundPitchCurve.DetectAnomalySoundPitchCurve'
DangerLevelSoundParameter = AkRtpc'/Game/_STALKER2/Audio/WwiseAudio/Game_Parameters/Equipment/AnomalyDetectorDangerLevel.AnomalyDetectorDangerLevel'
DetectorWorkCurve = CurveFloat''
struct.end`.trim();
const cfg_data = Struct.fromString(cfg);
expect(bin_data.length).toBe(4);
expect(bin_data.map((s) => s.toString()).join("\n")).toBe(cfg);
expect(bin_data.map((s) => s.toJson())).toEqual(
cfg_data.map((s) => s.toJson()),
);
});
});
describe("raw literals", () => {
test("round-trips trailing zeros and exponents", () => {
const cfg = [
"A : struct.begin",
" X = 35.0",
" Y = 1e3",
" Z = 0.5",
"struct.end",
].join("\n");
const [a] = Struct.fromString(cfg);
expect(a.toString()).toBe(cfg);
expect(a["X"]).toBe(35);
// parseValue() leaves exponent notation alone, so it survives as-is.
expect(a["Y"]).toBe("1e3");
});
test("a reassigned field renders its new value", () => {
const [a] = Struct.fromString(
"A : struct.begin\n X = 35.0\nstruct.end",
);
a["X"] = 12;
expect(a.toString()).toContain("X = 12");
});
test("clone keeps the literal, toJson does not expose it", () => {
const [a] = Struct.fromString(
"A : struct.begin\n X = 35.0\nstruct.end",
);
expect(a.clone().toString()).toContain("X = 35.0");
expect(a.toJson()).toEqual({ __internal__: a.__internal__, X: 35 });
});
});
describe("fork", () => {
test("1", () => {
const a = new TradePrototype();
const b = a.fork() as TradePrototype;
expect(a === b).toBe(false);
b.TradeGenerators = new TradeGenerators().fork();
expect(b.toString()).toBe(
"TradersDontBuyWeaponsArmor : struct.begin {bpatch}\n" +
" TradeGenerators : struct.begin {bpatch}\n" +
" struct.end\n" +
"struct.end",
);
});
});
describe("addNode", () => {
test("1", () => {
const a = new TradePrototype().fork(true);
expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("EItemType::Weapon");
expect(a.TradeGenerators[0].BuyLimitations[1]).toBe("EItemType::Armor");
a.TradeGenerators[0].BuyLimitations.addNode(
"EItemType::Artifact",
undefined,
);
expect(a.TradeGenerators[0].BuyLimitations[3]).toBe(
"EItemType::Artifact",
);
a.addNode({ someProp: 1 });
expect(a[0]).instanceof(Struct);
});
});
describe("forEach", () => {
test("1", () => {
const a = new TradePrototype().fork(true);
expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("EItemType::Weapon");
expect(a.TradeGenerators[0].BuyLimitations[1]).toBe("EItemType::Armor");
a.TradeGenerators[0].BuyLimitations.forEach(([k]) => {
a.TradeGenerators[0].BuyLimitations[k] = "forEach" as any;
});
expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("forEach");
expect(a.TradeGenerators[0].BuyLimitations[1]).toBe("forEach");
});
});
describe("filter", () => {
test("1", () => {
const a = new TradePrototype().fork(true);
expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("EItemType::Weapon");
expect(a.TradeGenerators[0].BuyLimitations[1]).toBe("EItemType::Armor");
const b = a.TradeGenerators[0].BuyLimitations.filter(([k]) => k === "0");
expect(b[0]).toBe("EItemType::Weapon");
expect(b[1]).toBeUndefined();
});
});
describe("map", () => {
test("1", () => {
const a = new TradePrototype();
expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("EItemType::Weapon");
expect(a.TradeGenerators[0].BuyLimitations[1]).toBe("EItemType::Armor");
const b = a.TradeGenerators[0].BuyLimitations.map(
([k, v]) => `${v}-mapped-${k}`,
);
expect(b[0]).toBe("EItemType::Weapon-mapped-0");
expect(b[1]).toBe("EItemType::Armor-mapped-1");
});
});
describe("clone", () => {
test("1", () => {
const a = new TradePrototype();
const b = a.clone() as TradePrototype;
expect(a === b).toBe(false);
expect(a.TradeGenerators === b.TradeGenerators).toBe(false);
expect(a.toString()).toBe(b.toString());
});
});
describe("removeNode", () => {
test("1", () => {
const a = new TradePrototype().fork(true);
expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("EItemType::Weapon");
expect(a.TradeGenerators[0].BuyLimitations[1]).toBe("EItemType::Armor");
a.TradeGenerators[0].BuyLimitations.removeNode("0");
expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("empty");
});
test("a removed struct renders as `removenode`", () => {
const a = new TradePrototype().fork(true);
a.TradeGenerators.removeNode("0");
expect(a.toString()).toBe(
"TradersDontBuyWeaponsArmor : struct.begin {refurl=../TradePrototypes.cfg;refkey=[0];bpatch}\n" +
" TradeGenerators : struct.begin {bpatch}\n" +
" [0] : removenode\n" +
" struct.end\n" +
"struct.end",
);
});
test("`removenode` survives fromString()", () => {
const text =
"A : struct.begin {bpatch}\n" +
" B : struct.begin {bpatch}\n" +
" [0] : removenode\n" +
" C : removenode\n" +
" struct.end\n" +
"struct.end\n" +
"D : removenode";
const parsed = Struct.fromString<any>(text);
expect(parsed[0].B[0].__internal__.removenode).toBe(true);
expect(parsed.map((s) => s.toString()).join("\n")).toBe(text);
});
test("`=` values ending in removenode stay values", () => {
const a = Struct.fromString<any>(
"A : struct.begin\n X = a:removenode\nstruct.end",
)[0];
expect(a.X).toBe("a:removenode");
});
});
describe("patch semantics", () => {
// The game replaces a whole node when a patch restates its key without
// bpatch, so a struct that wasn't forked must not gain the flag.
test("a non-bpatch struct serializes without bpatch", () => {
const text =
"CorpusMedlak : struct.begin\n" +
" TradePrototypeSID = MyTrade\n" +
"struct.end";
const s = Struct.fromString(text)[0];
expect(s.__internal__.bpatch).toBeUndefined();
expect(s.toString()).toBe(text);
expect(s.clone().toString()).not.toContain("bpatch");
});
test.each([
"{refurl=@BaseGame/ObjPrototypes/QuestObjPrototypes.cfg;refkey=CorpusMedlak}",
"{refurl=@MyMod/ItemPrototypes/Foo.cfg;refkey=[0];bpatch}",
"{refurl=@BaseGame/DialogPrototypes/Bar.cfg;refkey=Baz;bskipref}",
])("`@` mount refurl round-trips: %s", (refs) => {
const text =
`CorpusMedlak : struct.begin ${refs}\n` +
" TradePrototypeSID = MyTrade\n" +
"struct.end";
expect(Struct.fromString(text)[0].toString()).toBe(text);
});
test("a refkey and a struct key that start with digits stay keys", () => {
const text =
"38475DEF00000000000000000000000A : struct.begin {refkey=38475DEF00000000000000000000000B}\n" +
" SID = 38475DEF00000000000000000000000A\n" +
"struct.end";
expect(Struct.fromString(text)[0].toString()).toBe(text);
});
test("an empty struct with refs has no blank line", () => {
const text =
"A : struct.begin {refurl=@MyMod/Foo.cfg;refkey=[0];bpatch}\nstruct.end";
expect(Struct.fromString(text)[0].toString()).toBe(text);
});
});
describe("refurl helpers", () => {
test.each([
["ObjPrototypes/QuestObjPrototypes.cfg"],
["GameLite/GameData/ObjPrototypes/QuestObjPrototypes.cfg"],
[
"Stalker2\\Content\\GameLite\\GameData\\ObjPrototypes\\QuestObjPrototypes.cfg",
],
["./ObjPrototypes/QuestObjPrototypes.cfg"],
])("toBaseGameRefurl(%s)", (path) => {
expect(toBaseGameRefurl(path)).toBe(
"@BaseGame/ObjPrototypes/QuestObjPrototypes.cfg",
);
});
test.each([
["ItemPrototypes/Foo.cfg"],
[
"Stalker2/Mods/MyMod/Content/GameLite/ModGameData/MyMod/ItemPrototypes/Foo.cfg",
],
])("toMountRefurl('MyMod', %s)", (path) => {
expect(toMountRefurl("MyMod", path)).toBe(
"@MyMod/ItemPrototypes/Foo.cfg",
);
});
test("rejects paths that leave the mount", () => {
expect(() => toBaseGameRefurl("../Foo.cfg")).toThrow();
});
});
describe("toJson", () => {
test("1", () => {
const struct = new TradePrototype();
expect(struct.toJson()).toEqual({
__internal__: {
rawName: "TradersDontBuyWeaponsArmor",
isRoot: true,
refurl: "../TradePrototypes.cfg",
refkey: 0,
},
TradeGenerators: {
__internal__: { rawName: "TradeGenerators", isArray: true },
"0": {
__internal__: { rawName: "0" },
BuyLimitations: {
__internal__: { rawName: "BuyLimitations", isArray: true },
"0": "EItemType::Weapon",
"1": "EItemType::Armor",
"2": {
__internal__: {
removenode: true,
},
},
},
},
},
});
});
test("2", () => {
const struct = new ChimeraHPFix();
expect(struct.toJson(true)).toEqual({
_: { w: "ChimeraHPFix", r: true, s: true },
MaxHP: 750,
});
});
});
describe("fromJson", () => {
test("1", () => {
const json = {
__internal__: { rawName: "Test", isRoot: true },
MeshGenerator: {
__internal__: { rawName: "MeshGenerator" },
Meshes: {
__internal__: { rawName: "Meshes", isArray: true },
"0": {
__internal__: { rawName: "0" },
MeshPath: "path/to/mesh",
Offset: {
__internal__: { rawName: "Offset" },
X: 0,
Y: 0,
Z: 0,
},
Rotation: {
__internal__: { rawName: "Rotation" },
Pitch: 0,
Yaw: 0,
Roll: 0,
},
},
},
},
};
const struct = Struct.fromJson(json);
expect(struct.toString()).toBe(`Test : struct.begin
MeshGenerator : struct.begin
Meshes : struct.begin
[0] : struct.begin
MeshPath = path/to/mesh
Offset : struct.begin
X = 0
Y = 0
Z = 0
struct.end
Rotation : struct.begin
Pitch = 0
Yaw = 0
Roll = 0
struct.end
struct.end
struct.end
struct.end
struct.end`);
});
test("2", () => {
const json = {
_: { w: "Test", r: true },
MeshGenerator: {
_: { w: "MeshGenerator" },
Meshes: {
_: { w: "Meshes", a: true },
"0": {
_: { w: "0" },
MeshPath: "path/to/mesh",
Offset: {
_: { w: "Offset" },
X: 0,
Y: 0,
Z: 0,
},
Rotation: {
_: { w: "Rotation" },
Pitch: 0,
Yaw: 0,
Roll: 0,
},
},
},
},
};
const struct = Struct.fromJson(json, true);
expect(struct.toString()).toBe(`Test : struct.begin
MeshGenerator : struct.begin
Meshes : struct.begin
[0] : struct.begin
MeshPath = path/to/mesh
Offset : struct.begin
X = 0
Y = 0
Z = 0
struct.end
Rotation : struct.begin
Pitch = 0
Yaw = 0
Roll = 0
struct.end
struct.end
struct.end
struct.end
struct.end`);
});
});
});
// noinspection JSUnusedLocalSymbols
const MyRank: ERank = "ERank::Experienced, ERank::Veteran, ERank::Master";
// noinspection BadExpressionStatementJS
({}) as ArmorPrototype["MeshGenerator"];
(new Struct() as QuestNodePrototypeRandom).Launchers =
{} as QuestNodePrototypeLaunchers;
describe("comments", () => {
// GameData/PhysicsInteractionPrototypes.cfg uses `;` and `//` on adjacent lines
test("`;` starts a line comment, like `#` and `//`", () => {
const [struct] = Struct.fromString<{
WaterImpulseReduction: number;
PlayerPushImpulse: number;
}>(
[
"PhysicsInteraction : struct.begin",
" WaterImpulseReduction = 3.0",
" ;Max impulse to apply to the object when pushed by player",
" PlayerPushImpulse = 1000.0",
" // Default distance to cut all sounds is 10m",
"struct.end",
].join("\n"),
);
expect(struct.WaterImpulseReduction).toBe(3);
expect(struct.PlayerPushImpulse).toBe(1000);
expect(Object.keys(struct).filter((k) => k !== "__internal__")).toEqual([
"WaterImpulseReduction",
"PlayerPushImpulse",
]);
});
// a commented-out entry would otherwise be read back as real data under the key ";MaxHP"
test("a commented-out entry is not parsed", () => {
const [struct] = Struct.fromString(
["Foo : struct.begin", " ;MaxHP = 750", "struct.end"].join("\n"),
);
expect(Object.keys(struct).filter((k) => k !== "__internal__")).toEqual([]);
});
});