s2cfgtojson 3.2.12 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Struct.mts CHANGED
@@ -1,6 +1,8 @@
1
+ import { type } from "node:os";
2
+
1
3
  export * from "./types.mts";
2
4
  export * from "./enums.mts";
3
- import { DefaultEntries, Internal } from "./types.mts";
5
+ import { DefaultEntries, GetStructType, Internal } from "./types.mts";
4
6
 
5
7
  const TAB = " ";
6
8
  const WILDCARD = "_wildcard";
@@ -41,7 +43,7 @@ export class Struct {
41
43
  Object.assign(this, Struct.fromString(parentOrRaw)[0]);
42
44
  }
43
45
  if (typeof parentOrRaw === "object" && parentOrRaw !== null) {
44
- Object.assign(this, parentOrRaw);
46
+ Object.assign(this, Struct.fromJson(parentOrRaw));
45
47
  }
46
48
  }
47
49
 
@@ -122,10 +124,8 @@ export class Struct {
122
124
  filter<
123
125
  K extends Exclude<keyof this, Internal>,
124
126
  V extends (typeof this)[K],
125
- S extends [K, V],
126
- >(
127
- callback: (value: [K, V], index: number, array: [K, V][]) => value is S,
128
- ): Record<K, V> & Struct {
127
+ S extends this,
128
+ >(callback: (value: [K, V], index: number, array: [K, V][]) => boolean): S {
129
129
  const clone = this.clone();
130
130
  clone.entries().forEach((entry, i, arr) => {
131
131
  if (!callback(entry as any, i, arr as any)) {
@@ -152,6 +152,36 @@ export class Struct {
152
152
  return clone;
153
153
  }
154
154
 
155
+ static fromJson<T>(obj: T): T extends object ? GetStructType<T> : T {
156
+ if (typeof obj === "object" && !!obj) {
157
+ const instance = new Struct();
158
+ Object.entries(obj).forEach(([key, value]) => {
159
+ if (key === "__internal__") {
160
+ instance[key] = new Refs(value);
161
+ } else {
162
+ instance[key] = Struct.fromJson(value);
163
+ }
164
+ });
165
+ return instance as any;
166
+ }
167
+
168
+ return obj as any;
169
+ }
170
+
171
+ toJson<T extends object>() {
172
+ const obj = {};
173
+ Object.entries(this).forEach(([key, value]) => {
174
+ if (value instanceof Struct) {
175
+ obj[key] = value.toJson();
176
+ } else if (value instanceof Refs) {
177
+ obj[key] = { ...value };
178
+ } else {
179
+ obj[key] = value;
180
+ }
181
+ });
182
+ return obj as T;
183
+ }
184
+
155
185
  toString(): string {
156
186
  if (!(this.__internal__ instanceof Refs)) {
157
187
  this.__internal__ = new Refs(this.__internal__);
package/Struct.test.mts CHANGED
@@ -31,11 +31,15 @@ class TradePrototype extends Struct {
31
31
  class TradeGenerators extends Struct {
32
32
  __internal__ = new Refs({
33
33
  isArray: true,
34
- useAsterisk: true,
34
+ rawName: "TradeGenerators",
35
+ //useAsterisk: true, this option is not supported for now
35
36
  });
36
37
  "0" = new TradeGenerator();
37
38
  }
38
39
  class TradeGenerator extends Struct {
40
+ __internal__ = {
41
+ rawName: "0",
42
+ };
39
43
  BuyLimitations = new BuyLimitations();
40
44
  }
41
45
 
@@ -61,7 +65,7 @@ struct.end`,
61
65
  expect(new TradePrototype().toString()).toBe(
62
66
  `TradersDontBuyWeaponsArmor : struct.begin {refurl=../TradePrototypes.cfg;refkey=[0]}
63
67
  TradeGenerators : struct.begin
64
- [*] : struct.begin
68
+ [0] : struct.begin
65
69
  BuyLimitations : struct.begin
66
70
  [0] = EItemType::Weapon
67
71
  [1] = EItemType::Armor
@@ -254,7 +258,10 @@ struct.end`;
254
258
  const a = new TradePrototype().fork(true);
255
259
  expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("EItemType::Weapon");
256
260
  expect(a.TradeGenerators[0].BuyLimitations[1]).toBe("EItemType::Armor");
257
- a.TradeGenerators[0].BuyLimitations.addNode("EItemType::Artifact");
261
+ a.TradeGenerators[0].BuyLimitations.addNode(
262
+ "EItemType::Artifact",
263
+ undefined,
264
+ );
258
265
  expect(a.TradeGenerators[0].BuyLimitations[2]).toBe(
259
266
  "EItemType::Artifact",
260
267
  );
@@ -267,7 +274,7 @@ struct.end`;
267
274
  expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("EItemType::Weapon");
268
275
  expect(a.TradeGenerators[0].BuyLimitations[1]).toBe("EItemType::Armor");
269
276
  a.TradeGenerators[0].BuyLimitations.forEach(([k]) => {
270
- a.TradeGenerators[0].BuyLimitations[k] = "forEach";
277
+ a.TradeGenerators[0].BuyLimitations[k] = "forEach" as any;
271
278
  });
272
279
  expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("forEach");
273
280
  expect(a.TradeGenerators[0].BuyLimitations[1]).toBe("forEach");
@@ -317,6 +324,81 @@ struct.end`;
317
324
  expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("removenode");
318
325
  });
319
326
  });
327
+
328
+ describe("toJson", () => {
329
+ test("1", () => {
330
+ const struct = new TradePrototype();
331
+ expect(struct.toJson()).toEqual({
332
+ __internal__: {
333
+ rawName: "TradersDontBuyWeaponsArmor",
334
+ isRoot: true,
335
+ refurl: "../TradePrototypes.cfg",
336
+ refkey: 0,
337
+ },
338
+ TradeGenerators: {
339
+ __internal__: { rawName: "TradeGenerators", isArray: true },
340
+ "0": {
341
+ __internal__: { rawName: "0" },
342
+ BuyLimitations: {
343
+ __internal__: { rawName: "BuyLimitations", isArray: true },
344
+ "0": "EItemType::Weapon",
345
+ "1": "EItemType::Armor",
346
+ },
347
+ },
348
+ },
349
+ });
350
+ });
351
+ });
352
+
353
+ describe("fromJson", () => {
354
+ test("1", () => {
355
+ const json = {
356
+ __internal__: { rawName: "Test", isRoot: true },
357
+ MeshGenerator: {
358
+ __internal__: { rawName: "MeshGenerator" },
359
+ Meshes: {
360
+ __internal__: { rawName: "Meshes", isArray: true },
361
+ "0": {
362
+ __internal__: { rawName: "0" },
363
+ MeshPath: "path/to/mesh",
364
+ Offset: {
365
+ __internal__: { rawName: "Offset" },
366
+ X: 0,
367
+ Y: 0,
368
+ Z: 0,
369
+ },
370
+ Rotation: {
371
+ __internal__: { rawName: "Rotation" },
372
+ Pitch: 0,
373
+ Yaw: 0,
374
+ Roll: 0,
375
+ },
376
+ },
377
+ },
378
+ },
379
+ };
380
+ const struct = Struct.fromJson(json);
381
+ expect(struct.toString()).toBe(`Test : struct.begin
382
+ MeshGenerator : struct.begin
383
+ Meshes : struct.begin
384
+ [0] : struct.begin
385
+ MeshPath = path/to/mesh
386
+ Offset : struct.begin
387
+ X = 0
388
+ Y = 0
389
+ Z = 0
390
+ struct.end
391
+ Rotation : struct.begin
392
+ Pitch = 0
393
+ Yaw = 0
394
+ Roll = 0
395
+ struct.end
396
+ struct.end
397
+ struct.end
398
+ struct.end
399
+ struct.end`);
400
+ });
401
+ });
320
402
  });
321
403
 
322
404
  // noinspection JSUnusedLocalSymbols
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "s2cfgtojson",
3
- "version": "3.2.12",
3
+ "version": "3.4.0",
4
4
  "description": "Converts Stalker 2 Cfg file into POJOs",
5
5
  "keywords": [
6
6
  "stalker",