s2cfgtojson 1.0.2 → 1.0.4
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 +14 -8
- package/package.json +1 -1
package/Struct.mts
CHANGED
|
@@ -68,9 +68,11 @@ export abstract class Struct<T extends Entries = {}> {
|
|
|
68
68
|
if (Struct.isNumber(Struct.extractKeyFromBrackets(name))) {
|
|
69
69
|
return `_${name.match(/\[(\d+)]/)[1]}`; // Special case for indexed structs
|
|
70
70
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
return name
|
|
72
|
+
.replace(/\W/g, "_")
|
|
73
|
+
.replace(/^\d+/, "_")
|
|
74
|
+
.replace(/_+/g, "_")
|
|
75
|
+
.replace(/^_+/, "");
|
|
74
76
|
}
|
|
75
77
|
|
|
76
78
|
static createDynamicClass = (name: string): new () => Struct =>
|
|
@@ -150,17 +152,18 @@ export abstract class Struct<T extends Entries = {}> {
|
|
|
150
152
|
): IntendedType[] {
|
|
151
153
|
const lines = text.trim().split("\n");
|
|
152
154
|
|
|
153
|
-
const parseHead = (line: string): Struct => {
|
|
155
|
+
const parseHead = (line: string, index: number): Struct => {
|
|
154
156
|
const match = line.match(
|
|
155
157
|
/^(.*)\s*:\s*struct\.begin\s*({\s*((refurl|refkey|bskipref)\s*(=.+)?)\s*})?/,
|
|
156
158
|
);
|
|
157
159
|
if (!match) {
|
|
158
160
|
throw new Error(`Invalid struct head: ${line}`);
|
|
159
161
|
}
|
|
160
|
-
let name =
|
|
162
|
+
let name =
|
|
163
|
+
Struct.parseStructName(match[1].trim()) || `UnnamedStruct${index}`;
|
|
161
164
|
|
|
162
165
|
const dummy = new (Struct.createDynamicClass(name))();
|
|
163
|
-
dummy._id = match[1];
|
|
166
|
+
dummy._id = match[1].trim();
|
|
164
167
|
if (match[3]) {
|
|
165
168
|
const refs = match[3]
|
|
166
169
|
.split(";")
|
|
@@ -187,7 +190,10 @@ export abstract class Struct<T extends Entries = {}> {
|
|
|
187
190
|
throw new Error(`Invalid key-value pair: ${line}`);
|
|
188
191
|
}
|
|
189
192
|
const key = match[1].trim();
|
|
190
|
-
|
|
193
|
+
let value: string | number = match[3].trim();
|
|
194
|
+
try {
|
|
195
|
+
value = JSON.parse(value);
|
|
196
|
+
} catch (e) {}
|
|
191
197
|
Struct.addEntry(parent, key, value, index);
|
|
192
198
|
};
|
|
193
199
|
let index = 0;
|
|
@@ -202,7 +208,7 @@ export abstract class Struct<T extends Entries = {}> {
|
|
|
202
208
|
}
|
|
203
209
|
const current = stack[stack.length - 1];
|
|
204
210
|
if (line.includes("struct.begin")) {
|
|
205
|
-
const newStruct = parseHead(line);
|
|
211
|
+
const newStruct = parseHead(line, index);
|
|
206
212
|
if (current) {
|
|
207
213
|
const key = Struct.renderStructName(newStruct.constructor.name);
|
|
208
214
|
Struct.addEntry(current, key, newStruct, index);
|