@gaialabs/core 0.2.2 → 0.2.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.
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
  }
@@ -1827,6 +1833,7 @@ var DbStringType = class {
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 ?? "";
@@ -1841,6 +1848,10 @@ var DbStringType = class {
1841
1848
  acc[x.id] = x;
1842
1849
  return acc;
1843
1850
  }, {});
1851
+ this.dictionaryLookup = Object.values(this.dictionaries).flatMap((y) => y.entries.map((z, ix) => ({
1852
+ text: z,
1853
+ id: y.command << 8 | y.base + ix
1854
+ }))).sort((a, b) => b.text.length - a.text.length);
1844
1855
  if (!this.name) throw new Error("Name is required");
1845
1856
  if (!this.delimiter) throw new Error("Delimiter is required");
1846
1857
  if (!this.terminator && this.terminator !== 0) throw new Error("Terminator is required");
@@ -4413,6 +4424,8 @@ var DbRootUtils = class {
4413
4424
  group: groupName,
4414
4425
  scene: sceneName
4415
4426
  }));
4427
+ const stringDelimiterLookup = {};
4428
+ const stringDelimiterList = [];
4416
4429
  const stringLookup = Object.entries(module.strings).reduce((acc, x) => {
4417
4430
  const stringType = x[1];
4418
4431
  const name = x[0];
@@ -4430,12 +4443,15 @@ var DbRootUtils = class {
4430
4443
  });
4431
4444
  return acc$1;
4432
4445
  }, {});
4433
- acc[name] = new DbStringType({
4446
+ const st = new DbStringType({
4434
4447
  ...stringType,
4435
4448
  name,
4436
4449
  commands,
4437
4450
  dictionaries
4438
4451
  });
4452
+ acc[name] = st;
4453
+ stringDelimiterLookup[st.delimiter] = st;
4454
+ stringDelimiterList.push(st.delimiter);
4439
4455
  return acc;
4440
4456
  }, {});
4441
4457
  const structLookup = Object.entries(module.structs).reduce((acc, x) => {
@@ -4497,7 +4513,8 @@ var DbRootUtils = class {
4497
4513
  addrLookup,
4498
4514
  entryPoints: cfg.entryPoints,
4499
4515
  stringTypes: stringLookup,
4500
- stringDelimiters: Object.values(stringLookup).map((x) => x.delimiter),
4516
+ stringDelimiters: stringDelimiterList,
4517
+ stringDelimiterLookup,
4501
4518
  compression,
4502
4519
  groups: groupLookup,
4503
4520
  scenes: sceneLookup
@@ -4557,6 +4574,7 @@ var DbRootUtils = class {
4557
4574
  var sourceFiles = [];
4558
4575
  for (const path of inPath) sourceFiles = await this.applyFolder(root, path, sourceFiles);
4559
4576
  await saveFileAsBinary(outPath, await new RomWriter(root, "TEST", "TEST").repack(sourceFiles));
4577
+ return sourceFiles;
4560
4578
  }
4561
4579
  };
4562
4580
 
@@ -4729,13 +4747,12 @@ var StringProcessor = class {
4729
4747
  context;
4730
4748
  root;
4731
4749
  stringCharLookup;
4750
+ testRegex;
4732
4751
  constructor(context) {
4733
4752
  this.context = context;
4734
4753
  this.root = context.root;
4735
- this.stringCharLookup = Object.values(this.root.stringTypes).reduce((acc, x) => {
4736
- acc[x.delimiter] = x;
4737
- return acc;
4738
- }, {});
4754
+ this.stringCharLookup = context.root.stringDelimiterLookup;
4755
+ this.testRegex = /* @__PURE__ */ new RegExp(/^[0-9A-F]+$/i);
4739
4756
  }
4740
4757
  consumeString(typeChar) {
4741
4758
  let str = null;
@@ -4758,8 +4775,7 @@ var StringProcessor = class {
4758
4775
  this.memBuffer.length = 0;
4759
4776
  this.totalSize = 0;
4760
4777
  this.fixedStr = fixedStr;
4761
- const stringType = this.stringCharLookup[typeChar];
4762
- this.processString(str, stringType);
4778
+ this.processString(str, typeChar);
4763
4779
  }
4764
4780
  flushBuffer(stringType, wrap = false) {
4765
4781
  const size = this.memBuffer.length;
@@ -4779,32 +4795,24 @@ var StringProcessor = class {
4779
4795
  this.memBuffer.length = 0;
4780
4796
  }
4781
4797
  }
4782
- processString(str, stringType) {
4798
+ processString(str, typeChar) {
4799
+ const stringType = this.stringCharLookup[typeChar];
4800
+ const dictionary = stringType.dictionaryLookup;
4783
4801
  const cmdLookup = stringType.commands;
4784
- const dictLookup = stringType.dictionaries;
4785
4802
  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()}]`);
4803
+ for (const entry of dictionary) {
4804
+ let index;
4805
+ while ((index = str.indexOf(entry.text)) >= 0) str = str.substring(0, index) + `[${entry.id.toString(16).toUpperCase()}]` + str.substring(index + entry.text.length);
4790
4806
  }
4791
4807
  for (let x = 0; x < str.length; x++) {
4792
4808
  const c = str[x];
4793
4809
  if (c === "[") {
4794
4810
  const endIx = str.indexOf("]", x + 1);
4795
- const splitChars = [
4811
+ const parts = str.substring(x + 1, endIx).split(/* @__PURE__ */ new RegExp(`[${[
4796
4812
  ":",
4797
4813
  ",",
4798
4814
  " "
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);
4815
+ ].join("")}]`)).filter((p) => p.length > 0);
4808
4816
  x = endIx;
4809
4817
  if (parts.length === 0) {
4810
4818
  this.flushBuffer(stringType, true);
@@ -4818,6 +4826,12 @@ var StringProcessor = class {
4818
4826
  this.processStringCommand(cmd, stringType, parts);
4819
4827
  continue;
4820
4828
  }
4829
+ if (parts.length === 1 && this.testRegex.test(parts[0])) {
4830
+ const value = parseInt(parts[0], 16);
4831
+ if (value < 256) this.memBuffer.push(value);
4832
+ else this.memBuffer.push(value >> 8 & 255, value & 255);
4833
+ continue;
4834
+ }
4821
4835
  }
4822
4836
  lastCmd = null;
4823
4837
  if (this.applyLayers(c, stringType)) continue;
@@ -4958,7 +4972,7 @@ var AssemblerState = class AssemblerState {
4958
4972
  this.dbStruct = structType === null ? null : Object.values(this.root.structs).find((x) => x.name.toLowerCase() === structType.toLowerCase()) || null;
4959
4973
  this.parentStruct = !this.dbStruct || !this.dbStruct.parent ? null : Object.values(this.root.structs).find((x) => x.name.toLowerCase() === this.dbStruct.parent.toLowerCase()) || null;
4960
4974
  this.discriminator = this.parentStruct?.discriminator ?? null;
4961
- this.delimiter = this.dbStruct?.delimiter || null;
4975
+ this.delimiter = this.dbStruct?.delimiter ?? null;
4962
4976
  this.memberOffset = 0;
4963
4977
  this.dataOffset = 0;
4964
4978
  this.memberTypes = this.dbStruct?.types || null;