Skip to content

Commit

Permalink
Added isG3 check to G3SAV.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
evanwporter committed Nov 17, 2024
1 parent b437d2c commit ee9bd67
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
29 changes: 29 additions & 0 deletions src/types/SAVTypes/G3SAV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,32 @@ export class G3SAV implements SAV<PK3> {
return undefined
}
}

export function isG3(data: Uint8Array, SECTION_DATA_SIZE: number = 3968, MON_ENTRY_SIZE: number = 80): boolean {
const SECTION_COUNT = 14;
const SECTION_SIZE = 0x1000;
const MON_START_OFFSET = 4;
const NUM_POKEMON = 30;
const TID_OFFSET = 0x0A;

// Extract and sort sections by Section ID
const sections = Array.from({ length: SECTION_COUNT }, (_, i) => {
const offset = i * SECTION_SIZE;
const sectionData = data.slice(offset, offset + SECTION_DATA_SIZE);
const sectionID = data[offset + 0xff4] | (data[offset + 0xff5] << 8);
return { sectionID, data: sectionData };
}).sort((a, b) => a.sectionID - b.sectionID).map((section) => section.data);

// Extract save file Trainer ID from the first section
const saveTID = sections[0][TID_OFFSET] | (sections[0][TID_OFFSET + 1] << 8);

// Extract Trainer IDs for Pokémon 1 and onwards from Section 5
const section5 = sections[5];
const pokemonTIDs = Array.from({ length: NUM_POKEMON - 1 }, (_, i) => {
const pokemonOffset = MON_START_OFFSET + (i + 1) * MON_ENTRY_SIZE;
return section5[pokemonOffset + 0x0c] | (section5[pokemonOffset + 0x0d] << 8);
});

// Check if any Pokémon TID matches the save file TID
return pokemonTIDs.some((tid) => tid === saveTID);
}
31 changes: 2 additions & 29 deletions src/types/SAVTypes/radicalred/G3RRSAV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Box, BoxCoordinates, PluginSAV } from '../SAV'
import { PathData, splitPath } from '../path'
import PK3RR from './PK3RR'
import { RRTransferMon } from './conversion/RRTransferMons'
import { isG3 } from '../G3SAV'

const SAVE_SIZE_BYTES = 0x20000

Expand Down Expand Up @@ -354,32 +355,4 @@ const findFirstSaveIndexZero = (bytes: Uint8Array): number => {
// or that there are no pokemon in the first box. The result
// of both checks failing can&should be a prompt to specify
// which game the Save belongs too.
export function isRR(data: Uint8Array): boolean {
const SECTION_COUNT = 14;
const SECTION_SIZE = 0x1000;
const POKEMON_ENTRY_SIZE = 58;
const POKEMON_START_OFFSET = 4;
const NUM_POKEMON = 30; // Total number of Pokémon
const TID_OFFSET = 0x0A;

// Extract and sort sections by Section ID
const sections = Array.from({ length: SECTION_COUNT }, (_, i) => {
const offset = i * SECTION_SIZE;
const sectionData = data.slice(offset, offset + 4080);
const sectionID = data[offset + 0xff4] | (data[offset + 0xff5] << 8);
return { sectionID, data: sectionData };
}).sort((a, b) => a.sectionID - b.sectionID).map((section) => section.data);

// Extract save file Trainer ID from the first section
const saveTID = sections[0][TID_OFFSET] | (sections[0][TID_OFFSET + 1] << 8);

// Extract TID for Pokémon 2-30 from Section 5
const section5 = sections[5];
const pokemonTIDs = Array.from({ length: NUM_POKEMON - 1 }, (_, i) => {
const pokemonOffset = POKEMON_START_OFFSET + (i + 1) * POKEMON_ENTRY_SIZE;
return section5[pokemonOffset + 0x0c] | (section5[pokemonOffset + 0x0d] << 8);
});

// Check if any Pokémon TID matches the save file TID
return pokemonTIDs.some((tid) => tid === saveTID);
}
export function isRR(data: Uint8Array): boolean { return isG3(data, 4080, 58) }

0 comments on commit ee9bd67

Please sign in to comment.