s2cfgtojson 3.2.11 → 3.3.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 +19 -7
- package/Struct.test.mts +56 -3
- package/package.json +1 -1
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
|
|
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,18 @@ 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
|
+
instance[key] = Struct.fromJson(value);
|
|
160
|
+
});
|
|
161
|
+
return instance as any;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return obj as any;
|
|
165
|
+
}
|
|
166
|
+
|
|
155
167
|
toString(): string {
|
|
156
168
|
if (!(this.__internal__ instanceof Refs)) {
|
|
157
169
|
this.__internal__ = new Refs(this.__internal__);
|
|
@@ -336,7 +348,7 @@ export function parseKey(key: string, parent: Struct, index: number) {
|
|
|
336
348
|
normKey = extractKeyFromBrackets(key);
|
|
337
349
|
|
|
338
350
|
if (normKey === "*") {
|
|
339
|
-
parent.__internal__.useAsterisk = true;
|
|
351
|
+
// parent.__internal__.useAsterisk = true; never use asterisk for space-saving reasons
|
|
340
352
|
return Object.keys(parent).length - 1;
|
|
341
353
|
}
|
|
342
354
|
|
package/Struct.test.mts
CHANGED
|
@@ -31,7 +31,7 @@ class TradePrototype extends Struct {
|
|
|
31
31
|
class TradeGenerators extends Struct {
|
|
32
32
|
__internal__ = new Refs({
|
|
33
33
|
isArray: true,
|
|
34
|
-
useAsterisk: true,
|
|
34
|
+
//useAsterisk: true, this option is not supported for now
|
|
35
35
|
});
|
|
36
36
|
"0" = new TradeGenerator();
|
|
37
37
|
}
|
|
@@ -61,7 +61,7 @@ struct.end`,
|
|
|
61
61
|
expect(new TradePrototype().toString()).toBe(
|
|
62
62
|
`TradersDontBuyWeaponsArmor : struct.begin {refurl=../TradePrototypes.cfg;refkey=[0]}
|
|
63
63
|
TradeGenerators : struct.begin
|
|
64
|
-
[
|
|
64
|
+
[0] : struct.begin
|
|
65
65
|
BuyLimitations : struct.begin
|
|
66
66
|
[0] = EItemType::Weapon
|
|
67
67
|
[1] = EItemType::Armor
|
|
@@ -254,7 +254,10 @@ struct.end`;
|
|
|
254
254
|
const a = new TradePrototype().fork(true);
|
|
255
255
|
expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("EItemType::Weapon");
|
|
256
256
|
expect(a.TradeGenerators[0].BuyLimitations[1]).toBe("EItemType::Armor");
|
|
257
|
-
a.TradeGenerators[0].BuyLimitations.addNode(
|
|
257
|
+
a.TradeGenerators[0].BuyLimitations.addNode(
|
|
258
|
+
"EItemType::Artifact",
|
|
259
|
+
undefined,
|
|
260
|
+
);
|
|
258
261
|
expect(a.TradeGenerators[0].BuyLimitations[2]).toBe(
|
|
259
262
|
"EItemType::Artifact",
|
|
260
263
|
);
|
|
@@ -317,6 +320,56 @@ struct.end`;
|
|
|
317
320
|
expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("removenode");
|
|
318
321
|
});
|
|
319
322
|
});
|
|
323
|
+
|
|
324
|
+
describe("fromJson", () => {
|
|
325
|
+
test("1", () => {
|
|
326
|
+
const json = {
|
|
327
|
+
__internal__: { rawName: "Test", isRoot: true },
|
|
328
|
+
MeshGenerator: {
|
|
329
|
+
__internal__: { rawName: "MeshGenerator" },
|
|
330
|
+
Meshes: {
|
|
331
|
+
__internal__: { rawName: "Meshes", isArray: true },
|
|
332
|
+
"0": {
|
|
333
|
+
__internal__: { rawName: "0" },
|
|
334
|
+
MeshPath: "path/to/mesh",
|
|
335
|
+
Offset: {
|
|
336
|
+
__internal__: { rawName: "Offset" },
|
|
337
|
+
X: 0,
|
|
338
|
+
Y: 0,
|
|
339
|
+
Z: 0,
|
|
340
|
+
},
|
|
341
|
+
Rotation: {
|
|
342
|
+
__internal__: { rawName: "Rotation" },
|
|
343
|
+
Pitch: 0,
|
|
344
|
+
Yaw: 0,
|
|
345
|
+
Roll: 0,
|
|
346
|
+
},
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
},
|
|
350
|
+
};
|
|
351
|
+
const struct = Struct.fromJson(json);
|
|
352
|
+
expect(struct.toString()).toBe(`Test : struct.begin
|
|
353
|
+
MeshGenerator : struct.begin
|
|
354
|
+
Meshes : struct.begin
|
|
355
|
+
[0] : struct.begin
|
|
356
|
+
MeshPath = path/to/mesh
|
|
357
|
+
Offset : struct.begin
|
|
358
|
+
X = 0
|
|
359
|
+
Y = 0
|
|
360
|
+
Z = 0
|
|
361
|
+
struct.end
|
|
362
|
+
Rotation : struct.begin
|
|
363
|
+
Pitch = 0
|
|
364
|
+
Yaw = 0
|
|
365
|
+
Roll = 0
|
|
366
|
+
struct.end
|
|
367
|
+
struct.end
|
|
368
|
+
struct.end
|
|
369
|
+
struct.end
|
|
370
|
+
struct.end`);
|
|
371
|
+
});
|
|
372
|
+
});
|
|
320
373
|
});
|
|
321
374
|
|
|
322
375
|
// noinspection JSUnusedLocalSymbols
|