@gaialabs/core 0.2.2 → 0.2.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/dist/index.cjs CHANGED
@@ -1344,7 +1344,13 @@ var CopCommandProcessor = class {
1344
1344
  * Parses a COP command based on its definition
1345
1345
  */
1346
1346
  parseCopCommand(copDef, operands) {
1347
- for (const partStr of copDef.parts) {
1347
+ for (let partStr of copDef.parts) {
1348
+ let bank = null;
1349
+ const bankHintIx = partStr.indexOf("$");
1350
+ if (bankHintIx > 0) {
1351
+ bank = parseInt(partStr.substring(bankHintIx + 1), 16);
1352
+ partStr = partStr.substring(0, bankHintIx);
1353
+ }
1348
1354
  const addrType = Address.typeFromCode(partStr[0]);
1349
1355
  const isPtr = addrType !== AddressType.Unknown;
1350
1356
  const referenceType = isPtr ? partStr.substring(1) : this._blockReader._currentAsmBlock.structName ?? "Binary";
@@ -1355,7 +1361,7 @@ var CopCommandProcessor = class {
1355
1361
  if (label) {
1356
1362
  this._romDataReader.position += this.getMemberTypeSize(memberType);
1357
1363
  operands.push(label);
1358
- } else operands.push(this.readMemberTypeValue(memberType, partStr, isPtr, referenceType, addrType));
1364
+ } else operands.push(this.readMemberTypeValue(memberType, partStr, isPtr, referenceType, addrType, bank));
1359
1365
  }
1360
1366
  }
1361
1367
  tryParseMemberType(memberTypeName) {
@@ -1372,11 +1378,11 @@ var CopCommandProcessor = class {
1372
1378
  default: throw new Error("Unsupported COP member type");
1373
1379
  }
1374
1380
  }
1375
- readMemberTypeValue(memberType, partStr, isPtr, referenceType, addrType) {
1381
+ readMemberTypeValue(memberType, partStr, isPtr, referenceType, addrType, bank) {
1376
1382
  switch (memberType) {
1377
1383
  case MemberType.Byte: return new Byte(this._romDataReader.readByte());
1378
1384
  case MemberType.Word: return new Word(this._romDataReader.readUShort());
1379
- case MemberType.Offset: return this.createCopLocation(this._romDataReader.readUShort(), null, partStr, isPtr, referenceType, addrType);
1385
+ case MemberType.Offset: return this.createCopLocation(this._romDataReader.readUShort(), bank, partStr, isPtr, referenceType, addrType);
1380
1386
  case MemberType.Address: return this.createCopLocation(this._romDataReader.readUShort(), this._romDataReader.readByte(), partStr, isPtr, referenceType, addrType);
1381
1387
  default: throw new Error("Unsupported COP member type");
1382
1388
  }
@@ -1784,12 +1790,14 @@ var DbStringCommand = class {
1784
1790
  types;
1785
1791
  delimiter;
1786
1792
  halt;
1793
+ dictionary;
1787
1794
  constructor(data) {
1788
1795
  this.id = data.id ?? void 0;
1789
1796
  this.name = data.name ?? "";
1790
1797
  this.types = data.types ?? [];
1791
1798
  this.delimiter = data.delimiter ?? void 0;
1792
1799
  this.halt = data.halt ?? false;
1800
+ this.dictionary = data.dictionary ?? void 0;
1793
1801
  if (!this.id && this.id !== 0) throw new Error("Id is required");
1794
1802
  if (!this.name) throw new Error("Name is required");
1795
1803
  }
@@ -1820,19 +1828,16 @@ var DbStringType = class {
1820
1828
  name;
1821
1829
  delimiter;
1822
1830
  terminator;
1823
- shiftType;
1824
- characterMap;
1825
1831
  commands;
1826
1832
  commandLookup;
1827
1833
  layers;
1828
1834
  greedyTerminator;
1829
1835
  dictionaries;
1836
+ dictionaryLookup;
1830
1837
  constructor(data) {
1831
1838
  this.name = data.name ?? "";
1832
1839
  this.delimiter = data.delimiter ?? "";
1833
1840
  this.terminator = data.terminator ?? 0;
1834
- this.shiftType = data.shiftType ?? void 0;
1835
- this.characterMap = data.characterMap;
1836
1841
  this.commands = data.commands ?? {};
1837
1842
  this.layers = data.layers ?? [];
1838
1843
  this.greedyTerminator = data.greedyTerminator ?? false;
@@ -1841,10 +1846,14 @@ var DbStringType = class {
1841
1846
  acc[x.id] = x;
1842
1847
  return acc;
1843
1848
  }, {});
1849
+ this.dictionaryLookup = Object.values(this.dictionaries).flatMap((y) => y.entries.map((z, ix) => ({
1850
+ text: z,
1851
+ id: y.command << 8 | y.base + ix
1852
+ }))).sort((a, b) => b.text.length - a.text.length);
1844
1853
  if (!this.name) throw new Error("Name is required");
1845
1854
  if (!this.delimiter) throw new Error("Delimiter is required");
1846
1855
  if (!this.terminator && this.terminator !== 0) throw new Error("Terminator is required");
1847
- if (!this.characterMap) throw new Error("Character map is required");
1856
+ if (!this.layers.length) throw new Error("Layers are required");
1848
1857
  }
1849
1858
  };
1850
1859
  /**
@@ -3339,14 +3348,17 @@ var TypeParser = class {
3339
3348
  name: targetType.name,
3340
3349
  parts
3341
3350
  };
3342
- for (let i = 0; i < memberCount; i++) parts[i] = this.parseType(types[i], null, depth + 1, bank);
3351
+ for (let i = 0; i < memberCount; i++) parts[i] = this.parseType(types[i], reg, depth + 1, bank);
3343
3352
  if (discOffset !== void 0 && discOffset === this._romDataReader.position - prevPosition) this._romDataReader.position++;
3344
3353
  objects.push(def);
3345
3354
  }
3346
3355
  let checkPosition = startPosition;
3347
- while (++checkPosition < this._romDataReader.position) if (this._referenceManager.containsStruct(checkPosition)) {
3348
- this._romDataReader.position = checkPosition;
3349
- break;
3356
+ while (++checkPosition < this._romDataReader.position) {
3357
+ const struct = this._referenceManager.tryGetStruct(checkPosition);
3358
+ if (struct.found && struct.chunkType !== "Code") {
3359
+ this._romDataReader.position = checkPosition;
3360
+ break;
3361
+ }
3350
3362
  }
3351
3363
  if (!this._blockReader.partCanContinue()) break;
3352
3364
  }
@@ -4413,6 +4425,8 @@ var DbRootUtils = class {
4413
4425
  group: groupName,
4414
4426
  scene: sceneName
4415
4427
  }));
4428
+ const stringDelimiterLookup = {};
4429
+ const stringDelimiterList = [];
4416
4430
  const stringLookup = Object.entries(module.strings).reduce((acc, x) => {
4417
4431
  const stringType = x[1];
4418
4432
  const name = x[0];
@@ -4424,18 +4438,27 @@ var DbRootUtils = class {
4424
4438
  return acc$1;
4425
4439
  }, {});
4426
4440
  const dictionaries = Object.entries(stringType.dictionaries ?? {}).reduce((acc$1, y) => {
4427
- acc$1[y[0]] = new DbStringDictionary({
4441
+ const dictionary = new DbStringDictionary({
4428
4442
  ...y[1],
4429
4443
  name: y[0]
4430
4444
  });
4445
+ acc$1[y[0]] = dictionary;
4446
+ if (dictionary.command !== void 0) commands[dictionary.command] = new DbStringCommand({
4447
+ id: dictionary.command,
4448
+ name: dictionary.name,
4449
+ dictionary
4450
+ });
4431
4451
  return acc$1;
4432
4452
  }, {});
4433
- acc[name] = new DbStringType({
4453
+ const st = new DbStringType({
4434
4454
  ...stringType,
4435
4455
  name,
4436
4456
  commands,
4437
4457
  dictionaries
4438
4458
  });
4459
+ acc[name] = st;
4460
+ stringDelimiterLookup[st.delimiter] = st;
4461
+ stringDelimiterList.push(st.delimiter);
4439
4462
  return acc;
4440
4463
  }, {});
4441
4464
  const structLookup = Object.entries(module.structs).reduce((acc, x) => {
@@ -4497,7 +4520,8 @@ var DbRootUtils = class {
4497
4520
  addrLookup,
4498
4521
  entryPoints: cfg.entryPoints,
4499
4522
  stringTypes: stringLookup,
4500
- stringDelimiters: Object.values(stringLookup).map((x) => x.delimiter),
4523
+ stringDelimiters: stringDelimiterList,
4524
+ stringDelimiterLookup,
4501
4525
  compression,
4502
4526
  groups: groupLookup,
4503
4527
  scenes: sceneLookup
@@ -4557,6 +4581,7 @@ var DbRootUtils = class {
4557
4581
  var sourceFiles = [];
4558
4582
  for (const path of inPath) sourceFiles = await this.applyFolder(root, path, sourceFiles);
4559
4583
  await saveFileAsBinary(outPath, await new RomWriter(root, "TEST", "TEST").repack(sourceFiles));
4584
+ return sourceFiles;
4560
4585
  }
4561
4586
  };
4562
4587
 
@@ -4603,7 +4628,8 @@ var StringReader = class StringReader {
4603
4628
  this._romDataReader = blockReader._romDataReader;
4604
4629
  }
4605
4630
  resolveCommand(cmd, builder) {
4606
- if (cmd.types && cmd.types.length > 0) {
4631
+ if (cmd.dictionary) builder.push(cmd.dictionary.entries[this._romDataReader.readByte() - (cmd.dictionary.base ?? 0)]);
4632
+ else if (cmd.types && cmd.types.length > 0) {
4607
4633
  builder.push(`[${cmd.name}`);
4608
4634
  let first = true;
4609
4635
  for (const t of cmd.types) {
@@ -4646,8 +4672,8 @@ var StringReader = class StringReader {
4646
4672
  const dictionaries = stringType.dictionaries;
4647
4673
  const builder = [];
4648
4674
  const strLoc = this._romDataReader.position;
4649
- const map = stringType.characterMap;
4650
4675
  const terminator = stringType.terminator;
4676
+ let currentLayer = stringType.layers[0];
4651
4677
  do {
4652
4678
  const c = this._romDataReader.readByte();
4653
4679
  if (c === terminator) {
@@ -4666,8 +4692,25 @@ var StringReader = class StringReader {
4666
4692
  break;
4667
4693
  }
4668
4694
  if (!found) {
4669
- const index = DbStringTypeUtils.getShiftDown(stringType.shiftType)(c);
4670
- if (index >= 0 && index < map.length) builder.push(map[index]);
4695
+ for (const layer of stringType.layers) if (layer.on !== void 0) {
4696
+ if (c === layer.on) {
4697
+ currentLayer = layer;
4698
+ found = true;
4699
+ break;
4700
+ }
4701
+ } else if (c >= (layer.base ?? 0) && c <= (layer.range ?? 255)) {
4702
+ let index = c - (layer.base ?? 0);
4703
+ if (index >= 0 && index < layer.map.length) {
4704
+ if (layer.shift) index = DbStringTypeUtils.getShiftDown(layer.shift)(index);
4705
+ builder.push(layer.map[index]);
4706
+ found = true;
4707
+ break;
4708
+ }
4709
+ }
4710
+ }
4711
+ if (!found) {
4712
+ const index = currentLayer.shift ? DbStringTypeUtils.getShiftDown(currentLayer.shift)(c) : c - (currentLayer.base ?? 0);
4713
+ if (index >= 0 && index < currentLayer.map.length) builder.push(currentLayer.map[index]);
4671
4714
  else builder.push(`[${c.toString(16).toUpperCase()}]`);
4672
4715
  }
4673
4716
  }
@@ -4729,13 +4772,13 @@ var StringProcessor = class {
4729
4772
  context;
4730
4773
  root;
4731
4774
  stringCharLookup;
4775
+ testRegex;
4776
+ inShift = false;
4732
4777
  constructor(context) {
4733
4778
  this.context = context;
4734
4779
  this.root = context.root;
4735
- this.stringCharLookup = Object.values(this.root.stringTypes).reduce((acc, x) => {
4736
- acc[x.delimiter] = x;
4737
- return acc;
4738
- }, {});
4780
+ this.stringCharLookup = context.root.stringDelimiterLookup;
4781
+ this.testRegex = /* @__PURE__ */ new RegExp(/^[0-9A-F]+$/i);
4739
4782
  }
4740
4783
  consumeString(typeChar) {
4741
4784
  let str = null;
@@ -4758,8 +4801,7 @@ var StringProcessor = class {
4758
4801
  this.memBuffer.length = 0;
4759
4802
  this.totalSize = 0;
4760
4803
  this.fixedStr = fixedStr;
4761
- const stringType = this.stringCharLookup[typeChar];
4762
- this.processString(str, stringType);
4804
+ this.processString(str, typeChar);
4763
4805
  }
4764
4806
  flushBuffer(stringType, wrap = false) {
4765
4807
  const size = this.memBuffer.length;
@@ -4779,32 +4821,25 @@ var StringProcessor = class {
4779
4821
  this.memBuffer.length = 0;
4780
4822
  }
4781
4823
  }
4782
- processString(str, stringType) {
4824
+ processString(str, typeChar) {
4825
+ const stringType = this.stringCharLookup[typeChar];
4826
+ const dictionary = stringType.dictionaryLookup;
4783
4827
  const cmdLookup = stringType.commands;
4784
- const dictLookup = stringType.dictionaries;
4828
+ let currentLayer = stringType.layers[0];
4785
4829
  let lastCmd = null;
4786
- for (const dictionary of Object.values(dictLookup)) for (let ix = 0; ix < dictionary.entries.length; ix++) {
4787
- const entry = dictionary.entries[ix];
4788
- if (str.indexOf(entry) >= 0) if (dictionary.command !== void 0) str = str.replace(entry, `[${dictionary.command}:${(ix + dictionary.base).toString(16).toUpperCase()}]`);
4789
- else str = str.replace(entry, `[${(ix + dictionary.base).toString(16).toUpperCase()}]`);
4830
+ for (const entry of dictionary) {
4831
+ let index;
4832
+ while ((index = str.indexOf(entry.text)) >= 0) str = str.substring(0, index) + `[${entry.id.toString(16).toUpperCase()}]` + str.substring(index + entry.text.length);
4790
4833
  }
4791
4834
  for (let x = 0; x < str.length; x++) {
4792
4835
  const c = str[x];
4793
4836
  if (c === "[") {
4794
4837
  const endIx = str.indexOf("]", x + 1);
4795
- const splitChars = [
4838
+ const parts = str.substring(x + 1, endIx).split(/* @__PURE__ */ new RegExp(`[${[
4796
4839
  ":",
4797
4840
  ",",
4798
4841
  " "
4799
- ];
4800
- const subStr = str.substring(x + 1, endIx);
4801
- let ix = subStr.length === 2 ? parseInt(subStr, 16) : NaN;
4802
- if (!isNaN(ix)) {
4803
- this.memBuffer.push(ix);
4804
- x = endIx;
4805
- continue;
4806
- }
4807
- const parts = str.substring(x + 1, endIx).split(/* @__PURE__ */ new RegExp(`[${splitChars.join("")}]`)).filter((p) => p.length > 0);
4842
+ ].join("")}]`)).filter((p) => p.length > 0);
4808
4843
  x = endIx;
4809
4844
  if (parts.length === 0) {
4810
4845
  this.flushBuffer(stringType, true);
@@ -4818,9 +4853,29 @@ var StringProcessor = class {
4818
4853
  this.processStringCommand(cmd, stringType, parts);
4819
4854
  continue;
4820
4855
  }
4856
+ if (parts.length === 1 && this.testRegex.test(parts[0])) {
4857
+ const value = parseInt(parts[0], 16);
4858
+ if (value < 256) this.memBuffer.push(value);
4859
+ else this.memBuffer.push(value >> 8 & 255, value & 255);
4860
+ continue;
4861
+ }
4821
4862
  }
4822
4863
  lastCmd = null;
4823
- if (this.applyLayers(c, stringType)) continue;
4864
+ let found = false;
4865
+ for (const layer of stringType.layers) {
4866
+ for (let i = 0, len = layer.map.length; i < len; i++) if (c === layer.map[i]) {
4867
+ if (layer.on !== void 0 && currentLayer !== layer) {
4868
+ this.memBuffer.push(layer.on);
4869
+ currentLayer = layer;
4870
+ }
4871
+ let value = i + (layer.base ?? 0);
4872
+ if (layer.shift) value = DbStringTypeUtils.getShiftUp(layer.shift)(value);
4873
+ this.memBuffer.push(value);
4874
+ found = true;
4875
+ break;
4876
+ }
4877
+ if (found) break;
4878
+ }
4824
4879
  }
4825
4880
  if (this.fixedStr) {
4826
4881
  let count = this.fixedStr - this.totalSize;
@@ -4828,23 +4883,6 @@ var StringProcessor = class {
4828
4883
  } else if (lastCmd === null || !lastCmd.halt) this.memBuffer.push(stringType.terminator);
4829
4884
  this.flushBuffer(stringType, true);
4830
4885
  }
4831
- applyLayers(c, stringType) {
4832
- if (this.applyMap(c, stringType.characterMap, DbStringTypeUtils.getShiftUp(stringType.shiftType))) return true;
4833
- if (stringType.layers) {
4834
- for (const layer of stringType.layers) if (this.applyMap(c, layer.map, (x) => x + layer.base)) return true;
4835
- }
4836
- return false;
4837
- }
4838
- applyMap(c, map, shift) {
4839
- for (let i = 0, len = map.length; i < len; i++) {
4840
- const v = map[i];
4841
- if (v != null && c === v[0]) {
4842
- this.memBuffer.push(shift(i));
4843
- return true;
4844
- }
4845
- }
4846
- return false;
4847
- }
4848
4886
  processStringCommand(cmd, stringType, parts) {
4849
4887
  const hasPointer = cmd.types.includes(MemberType.Address) || cmd.types.includes(MemberType.Offset);
4850
4888
  if (hasPointer) this.flushBuffer(stringType, true);
@@ -4958,7 +4996,7 @@ var AssemblerState = class AssemblerState {
4958
4996
  this.dbStruct = structType === null ? null : Object.values(this.root.structs).find((x) => x.name.toLowerCase() === structType.toLowerCase()) || null;
4959
4997
  this.parentStruct = !this.dbStruct || !this.dbStruct.parent ? null : Object.values(this.root.structs).find((x) => x.name.toLowerCase() === this.dbStruct.parent.toLowerCase()) || null;
4960
4998
  this.discriminator = this.parentStruct?.discriminator ?? null;
4961
- this.delimiter = this.dbStruct?.delimiter || null;
4999
+ this.delimiter = this.dbStruct?.delimiter ?? null;
4962
5000
  this.memberOffset = 0;
4963
5001
  this.dataOffset = 0;
4964
5002
  this.memberTypes = this.dbStruct?.types || null;