s2cfgtojson 1.0.1 → 1.0.3

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.
Files changed (2) hide show
  1. package/Struct.mts +11 -8
  2. package/package.json +1 -1
package/Struct.mts CHANGED
@@ -68,7 +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
- return name;
71
+ return name
72
+ .replace(/\W/g, "_")
73
+ .replace(/^\d+/, "_")
74
+ .replace(/_+/g, "_")
75
+ .replace(/^_+/, "");
72
76
  }
73
77
 
74
78
  static createDynamicClass = (name: string): new () => Struct =>
@@ -76,7 +80,7 @@ export abstract class Struct<T extends Entries = {}> {
76
80
 
77
81
  toString(): string {
78
82
  let text: string;
79
- text = this.isRoot ? `${Struct.renderStructName(this._id)} : ` : "";
83
+ text = this.isRoot ? `${this._id} : ` : "";
80
84
  text += "struct.begin";
81
85
  const refs = ["refurl", "refkey", "bskipref"]
82
86
  .map((k) => [k, this[k]])
@@ -148,19 +152,18 @@ export abstract class Struct<T extends Entries = {}> {
148
152
  ): IntendedType[] {
149
153
  const lines = text.trim().split("\n");
150
154
 
151
- const parseHead = (line: string): Struct => {
155
+ const parseHead = (line: string, index: number): Struct => {
152
156
  const match = line.match(
153
157
  /^(.*)\s*:\s*struct\.begin\s*({\s*((refurl|refkey|bskipref)\s*(=.+)?)\s*})?/,
154
158
  );
155
159
  if (!match) {
156
160
  throw new Error(`Invalid struct head: ${line}`);
157
161
  }
158
- let name = Struct.parseStructName(match[1].trim());
162
+ let name =
163
+ Struct.parseStructName(match[1].trim()) || `UnnamedStruct${index}`;
159
164
 
160
165
  const dummy = new (Struct.createDynamicClass(name))();
161
- if (name === match[1].trim()) {
162
- dummy._id = name;
163
- }
166
+ dummy._id = match[1];
164
167
  if (match[3]) {
165
168
  const refs = match[3]
166
169
  .split(";")
@@ -202,7 +205,7 @@ export abstract class Struct<T extends Entries = {}> {
202
205
  }
203
206
  const current = stack[stack.length - 1];
204
207
  if (line.includes("struct.begin")) {
205
- const newStruct = parseHead(line);
208
+ const newStruct = parseHead(line, index);
206
209
  if (current) {
207
210
  const key = Struct.renderStructName(newStruct.constructor.name);
208
211
  Struct.addEntry(current, key, newStruct, index);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "s2cfgtojson",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Converts Stalker 2 Cfg file into a POJO",
5
5
  "keywords": [
6
6
  "stalker",