From ee9bd6789c6a1d806c63c68db45e625701459aff Mon Sep 17 00:00:00 2001 From: evanwporter Date: Sun, 17 Nov 2024 11:26:23 -0800 Subject: [PATCH] Added `isG3` check to `G3SAV.ts` --- src/types/SAVTypes/G3SAV.ts | 29 ++++++++++++++++++++++ src/types/SAVTypes/radicalred/G3RRSAV.ts | 31 ++---------------------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/types/SAVTypes/G3SAV.ts b/src/types/SAVTypes/G3SAV.ts index c85e5d4a..8c907894 100644 --- a/src/types/SAVTypes/G3SAV.ts +++ b/src/types/SAVTypes/G3SAV.ts @@ -354,3 +354,32 @@ export class G3SAV implements SAV { 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); +} diff --git a/src/types/SAVTypes/radicalred/G3RRSAV.ts b/src/types/SAVTypes/radicalred/G3RRSAV.ts index b6a14ecf..49f07cdb 100644 --- a/src/types/SAVTypes/radicalred/G3RRSAV.ts +++ b/src/types/SAVTypes/radicalred/G3RRSAV.ts @@ -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 @@ -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) }