@gaialabs/core 0.1.5 → 0.1.7

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.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { BitStream, MemberType, Address, AddressSpace, AddressType, RomProcessingConstants, ChunkFileUtils, Op, RegisterType, BlockReaderConstants, createTableEntry, ChunkFile, BinType, createChunkFileFromDbFile, createChunkFileFromDbBlock, LocationWrapper, CompressionRegistry, Word, Byte, StatusFlags, readJsonFile, getDirectory, TypedNumber, AsmBlock, Long, crc32_buffer, DbRootUtils } from '@gaialabs/shared';
1
+ import { BitStream, MemberType, Address, AddressSpace, AddressType, RomProcessingConstants, ChunkFileUtils, Op, RegisterType, BlockReaderConstants, createTableEntry, ChunkFile, BinType, createChunkFileFromDbFile, createChunkFileFromDbBlock, LocationWrapper, CompressionRegistry, Word, Byte, StatusFlags, readJsonFile, getDirectory, TypedNumber, AsmBlock, Long, summaryFromSupabaseByProject, crc32_buffer, DbRootUtils } from '@gaialabs/shared';
2
2
 
3
3
  // src/rom/project.ts
4
4
  var QuintetLZ = class _QuintetLZ {
@@ -3658,88 +3658,72 @@ var Assembler = class {
3658
3658
  }
3659
3659
  };
3660
3660
  var RomGenerator = class {
3661
- constructor(modules, sourceData) {
3662
- this.moduleLookup = /* @__PURE__ */ new Map();
3663
- this.moduleList = ["jp-viper", "title-enhanced"];
3664
- this.patchFiles = [];
3665
- this.chunkFiles = [];
3666
- this.asmFiles = [];
3661
+ constructor(projectName) {
3662
+ this.crc = 0;
3663
+ this.branchId = "";
3664
+ //private chunkFiles: ChunkFile[] = [];
3665
+ //private asmFiles: ChunkFile[] = [];
3667
3666
  this.dbRoot = {};
3668
- this.sourceData = sourceData;
3669
- this.moduleList = modules;
3670
- const crc = crc32_buffer(this.sourceData);
3671
- if (crc !== 473450688) {
3672
- throw new Error("CRC mismatch, please provide the correct ROM");
3673
- }
3674
- }
3675
- async generateProject(projectName) {
3676
- await this.initializeDatabase(projectName);
3677
- const reader = this.initializeChunks();
3678
- this.writeScriptTexts(reader);
3679
- this.applyBaseRom();
3680
- this.applyProjectInit();
3681
- this.applySelectedModules();
3682
- this.assembleCodeFromText();
3683
- this.applyCodePatches();
3684
- this.calculateSizes();
3685
- this.performLayout();
3686
- this.rebaseAssemblies();
3687
- this.generateAsmIncludeLookups();
3688
- const blockLookup = this.generateBlockLookup();
3689
- const outRom = await this.writeRom(blockLookup);
3690
- return outRom;
3691
- }
3692
- //Grab structures from supabase
3693
- async initializeDatabase(projectName) {
3694
- this.dbRoot = await DbRootUtils.fromSupabaseProject({ projectName });
3667
+ this.sourceData = new Uint8Array();
3668
+ this.projectName = projectName;
3669
+ }
3670
+ async initialize() {
3671
+ var projectData = await summaryFromSupabaseByProject(this.projectName);
3672
+ this.crc = projectData.project.baseRom.gameRom.crc;
3673
+ this.branchId = projectData.id;
3674
+ }
3675
+ async validateAndDownload(sourceData) {
3676
+ const calc = crc32_buffer(sourceData);
3677
+ if (calc === this.crc) {
3678
+ this.dbRoot = await DbRootUtils.fromSupabaseProject(this.projectName, this.branchId);
3679
+ this.sourceData = sourceData;
3680
+ return true;
3681
+ }
3682
+ return false;
3695
3683
  }
3696
- //Read graph from rom
3697
- initializeChunks() {
3684
+ async generateProject(modules, manualFiles) {
3685
+ if (!this.dbRoot) throw new Error("Database not initialized");
3686
+ if (!this.sourceData) throw new Error("Source data not initialized");
3698
3687
  const reader = new BlockReader(this.sourceData, this.dbRoot);
3699
- this.chunkFiles = reader.analyzeAndResolve();
3700
- this.asmFiles = this.chunkFiles.filter((b) => b.type === BinType.Assembly);
3701
- return reader;
3702
- }
3703
- //Convert blocks to text
3704
- writeScriptTexts(reader) {
3688
+ const chunkFiles = reader.analyzeAndResolve();
3689
+ const asmFiles = chunkFiles.filter((b) => b.type === BinType.Assembly);
3690
+ const patchFiles = [];
3705
3691
  const writer = new BlockWriter(reader);
3706
- for (const block of this.asmFiles) {
3707
- block.textData = writer.generateAsm(block);
3708
- }
3709
- }
3710
- //Apply base rom files
3711
- applyBaseRom() {
3712
- for (const chunkFile of this.dbRoot.baseRomFiles) {
3713
- this.applyPatchFile(chunkFile);
3714
- }
3692
+ for (const block of asmFiles) block.textData = writer.generateAsm(block);
3693
+ for (const chunkFile of this.dbRoot.baseRomFiles) this.applyPatchFile(chunkFile, chunkFiles, asmFiles, patchFiles);
3694
+ const moduleLookup = this.applyProjectInit(chunkFiles, asmFiles, patchFiles);
3695
+ for (const module of modules) {
3696
+ for (const file of moduleLookup.get(module)) this.applyPatchFile(file, chunkFiles, asmFiles, patchFiles);
3697
+ }
3698
+ for (const file of manualFiles ?? []) this.applyPatchFile(file, chunkFiles, asmFiles, patchFiles);
3699
+ this.assembleCodeFromText(asmFiles);
3700
+ RomProcessor.applyPatches(asmFiles, patchFiles);
3701
+ for (const asm of asmFiles) ChunkFileUtils.calculateSize(asm);
3702
+ const layout = new RomLayout(chunkFiles);
3703
+ layout.organize();
3704
+ for (const file of asmFiles) ChunkFileUtils.rebase(file);
3705
+ this.generateAsmIncludeLookups(asmFiles);
3706
+ const blockLookup = /* @__PURE__ */ new Map();
3707
+ for (const f of chunkFiles) blockLookup.set(f.name.toUpperCase(), f.location);
3708
+ const outRom = await this.writeRom(blockLookup, this.dbRoot.entryPoints, chunkFiles, asmFiles);
3709
+ return outRom;
3715
3710
  }
3716
- applyProjectInit() {
3711
+ applyProjectInit(chunkFiles, asmFiles, patchFiles) {
3712
+ const moduleLookup = /* @__PURE__ */ new Map();
3717
3713
  for (const chunkFile of this.dbRoot.projectFiles) {
3718
3714
  console.log(`Processing patch: ${chunkFile.name}`);
3719
3715
  if (chunkFile.group) {
3720
3716
  let modArray;
3721
- if (!this.moduleLookup.has(chunkFile.group)) {
3722
- this.moduleLookup.set(chunkFile.group, modArray = []);
3723
- } else {
3724
- modArray = this.moduleLookup.get(chunkFile.group);
3725
- }
3717
+ if (!moduleLookup.has(chunkFile.group)) moduleLookup.set(chunkFile.group, modArray = []);
3718
+ else modArray = moduleLookup.get(chunkFile.group);
3726
3719
  modArray.push(chunkFile);
3727
- } else {
3728
- this.applyPatchFile(chunkFile);
3729
- }
3730
- }
3731
- }
3732
- //Apply selected project modules
3733
- applySelectedModules() {
3734
- for (const module of this.moduleList) {
3735
- for (const file of this.moduleLookup.get(module)) {
3736
- this.applyPatchFile(file);
3737
- }
3720
+ } else this.applyPatchFile(chunkFile, chunkFiles, asmFiles, patchFiles);
3738
3721
  }
3722
+ return moduleLookup;
3739
3723
  }
3740
3724
  //Assemble code from script text
3741
- assembleCodeFromText() {
3742
- for (const block of this.asmFiles) {
3725
+ assembleCodeFromText(asmFiles) {
3726
+ for (const block of asmFiles) {
3743
3727
  const assembler = new Assembler(this.dbRoot, block.textData);
3744
3728
  const { blocks, includes, reqBank } = assembler.parseAssembly();
3745
3729
  block.parts = blocks;
@@ -3747,31 +3731,10 @@ var RomGenerator = class {
3747
3731
  block.bank = reqBank ?? void 0;
3748
3732
  }
3749
3733
  }
3750
- //Apply patches
3751
- applyCodePatches() {
3752
- RomProcessor.applyPatches(this.asmFiles, this.patchFiles);
3753
- }
3754
- //Calculate sizes
3755
- calculateSizes() {
3756
- for (const asm of this.chunkFiles) {
3757
- ChunkFileUtils.calculateSize(asm);
3758
- }
3759
- }
3760
- // Assign locations
3761
- performLayout() {
3762
- const layout = new RomLayout(this.chunkFiles);
3763
- layout.organize();
3764
- }
3765
- // Rebase assemblies
3766
- rebaseAssemblies() {
3767
- for (const file of this.asmFiles) {
3768
- ChunkFileUtils.rebase(file);
3769
- }
3770
- }
3771
3734
  // Build include lookup map per asm file
3772
- generateAsmIncludeLookups() {
3773
- for (const f of this.asmFiles) {
3774
- const includeBlocks = this.asmFiles.filter((x) => f.includes?.has(x.name.toUpperCase())).flatMap((x) => x.parts).filter((b) => !!b.label);
3735
+ generateAsmIncludeLookups(asmFiles) {
3736
+ for (const f of asmFiles) {
3737
+ const includeBlocks = asmFiles.filter((x) => f.includes?.has(x.name.toUpperCase())).flatMap((x) => x.parts).filter((b) => !!b.label);
3775
3738
  f.includeLookup = /* @__PURE__ */ new Map();
3776
3739
  for (const b of includeBlocks) {
3777
3740
  if (b.label) f.includeLookup.set(b.label.toUpperCase(), b);
@@ -3781,43 +3744,33 @@ var RomGenerator = class {
3781
3744
  }
3782
3745
  }
3783
3746
  }
3784
- applyPatchFile(chunkFile) {
3747
+ applyPatchFile(chunkFile, chunkFiles, asmFiles, patchFiles) {
3785
3748
  console.log(`Processing patch: ${chunkFile.name}`);
3786
- const existing = this.chunkFiles.find((x) => x.name === chunkFile.name);
3749
+ const existing = chunkFiles.find((x) => x.name === chunkFile.name);
3787
3750
  if (chunkFile.type === BinType.Patch || chunkFile.type === BinType.Assembly) {
3788
3751
  if (existing) {
3789
3752
  existing.textData = chunkFile.textData;
3790
3753
  } else {
3791
3754
  if (chunkFile.type === BinType.Patch) {
3792
- this.patchFiles.push(chunkFile);
3755
+ patchFiles.push(chunkFile);
3793
3756
  }
3794
- this.asmFiles.push(chunkFile);
3795
- this.chunkFiles.push(chunkFile);
3757
+ asmFiles.push(chunkFile);
3758
+ chunkFiles.push(chunkFile);
3796
3759
  }
3797
3760
  } else {
3798
3761
  if (existing) {
3799
3762
  existing.rawData = chunkFile.rawData;
3800
3763
  existing.size = chunkFile.size;
3801
3764
  } else {
3802
- this.chunkFiles.push(chunkFile);
3765
+ chunkFiles.push(chunkFile);
3803
3766
  }
3804
3767
  }
3805
3768
  }
3806
- // Create block lookup for resolving labels to locations
3807
- generateBlockLookup() {
3808
- const blockLookup = /* @__PURE__ */ new Map();
3809
- for (const f of this.chunkFiles) {
3810
- blockLookup.set(f.name.toUpperCase(), f.location);
3811
- }
3812
- return blockLookup;
3813
- }
3814
- async writeRom(blockLookup) {
3815
- const romWriter = new RomWriter(this.dbRoot.entryPoints, "GAIALABS", "01JG ");
3816
- for (const file of this.chunkFiles) {
3817
- await romWriter.writeFile(file, blockLookup);
3818
- }
3769
+ async writeRom(blockLookup, entryPoints, chunkFiles, asmFiles) {
3770
+ const romWriter = new RomWriter(entryPoints, "GAIALABS", "01JG ");
3771
+ for (const file of chunkFiles) await romWriter.writeFile(file, blockLookup);
3819
3772
  romWriter.writeHeader();
3820
- romWriter.writeEntryPoints(this.asmFiles);
3773
+ romWriter.writeEntryPoints(asmFiles);
3821
3774
  romWriter.writeChecksum();
3822
3775
  return romWriter.outBuffer;
3823
3776
  }
@@ -4022,7 +3975,7 @@ var SpriteMap = class _SpriteMap {
4022
3975
  };
4023
3976
 
4024
3977
  // src/index.ts
4025
- var GAIA_CORE_VERSION = "0.1.5";
3978
+ var GAIA_CORE_VERSION = "0.1.6";
4026
3979
  var isPlatformBrowser = typeof window !== "undefined";
4027
3980
  var isPlatformNode = typeof process !== "undefined" && process.versions?.node;
4028
3981
  var isPlatformWebWorker = typeof importScripts !== "undefined";