s2cfgtojson 2.0.3 → 2.1.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 +23 -12
- package/Struct.test.mts +18 -0
- package/package.json +1 -1
package/Struct.mts
CHANGED
|
@@ -8,17 +8,20 @@ export type GetTsType<In extends Struct, E = In["entries"]> = {
|
|
|
8
8
|
? GetTsType<E[key]>
|
|
9
9
|
: E[key];
|
|
10
10
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
11
|
+
type RKey<In> = Exclude<keyof In, keyof DefaultEntries>;
|
|
12
|
+
|
|
13
|
+
export type GetStructType<In> =
|
|
14
|
+
In extends Array<any>
|
|
15
|
+
? Struct<{ [key: number]: GetStructType<In[number]> }>
|
|
16
|
+
: In extends Record<any, any>
|
|
17
|
+
? Struct<{ [key in RKey<In>]: GetStructType<In[key]> }>
|
|
18
|
+
: In extends string
|
|
19
|
+
? string
|
|
20
|
+
: In extends number
|
|
21
|
+
? number
|
|
22
|
+
: In extends boolean
|
|
23
|
+
? boolean
|
|
24
|
+
: never;
|
|
22
25
|
|
|
23
26
|
/**
|
|
24
27
|
* This file is part of the Stalker 2 Modding Tools project.
|
|
@@ -225,7 +228,15 @@ export abstract class Struct<T extends Entries = {}> {
|
|
|
225
228
|
const key = match[1].trim();
|
|
226
229
|
let value: string | number = match[3].trim();
|
|
227
230
|
try {
|
|
228
|
-
|
|
231
|
+
// understand 0.1f / 1. / 0.f / .1 / .1f -> ((\d*)\.?(\d+)|(\d+)\.?(\d*))f?
|
|
232
|
+
const matches = value.match(/(\d*)\.?(\d*)f?/);
|
|
233
|
+
if (matches) {
|
|
234
|
+
const first = matches[1];
|
|
235
|
+
const second = matches[2];
|
|
236
|
+
value = parseFloat(`${first || 0}${second ? `.${second}` : ""}`);
|
|
237
|
+
} else {
|
|
238
|
+
value = JSON.parse(value);
|
|
239
|
+
}
|
|
229
240
|
} catch (e) {}
|
|
230
241
|
Struct.addEntry(parent, key, value, index);
|
|
231
242
|
};
|
package/Struct.test.mts
CHANGED
|
@@ -187,5 +187,23 @@ struct.end`;
|
|
|
187
187
|
dynamicItemGeneratorText,
|
|
188
188
|
);
|
|
189
189
|
});
|
|
190
|
+
|
|
191
|
+
test("4", () => {
|
|
192
|
+
const dynamicItemGeneratorText = `A : struct.begin
|
|
193
|
+
N1 = 0.1f
|
|
194
|
+
N2 = 1.
|
|
195
|
+
N3 = 0.f
|
|
196
|
+
N4 = .1
|
|
197
|
+
N5 = .1f
|
|
198
|
+
struct.end`;
|
|
199
|
+
const str = Struct.fromString<
|
|
200
|
+
Struct<{ N1: number; N2: number; N3: number; N4: number; N5: number }>
|
|
201
|
+
>(dynamicItemGeneratorText);
|
|
202
|
+
expect(str[0].entries.N1).toBe(0.1);
|
|
203
|
+
expect(str[0].entries.N2).toBe(1);
|
|
204
|
+
expect(str[0].entries.N3).toBe(0);
|
|
205
|
+
expect(str[0].entries.N4).toBe(0.1);
|
|
206
|
+
expect(str[0].entries.N5).toBe(0.1);
|
|
207
|
+
});
|
|
190
208
|
});
|
|
191
209
|
});
|