From 98133a9f7babad12d40b76bf3e1fa6db376fda62 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Fri, 6 Dec 2024 15:01:52 -0500 Subject: [PATCH] Add fix for possible crash when decompressing trainer back pics --- src/battle_gfx_sfx_util.c | 8 ++++++++ src/trainer_pokemon_sprites.c | 20 +++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/battle_gfx_sfx_util.c b/src/battle_gfx_sfx_util.c index 2deced13c00d..37b2a023dd9c 100644 --- a/src/battle_gfx_sfx_util.c +++ b/src/battle_gfx_sfx_util.c @@ -710,9 +710,17 @@ void DecompressTrainerFrontPic(u16 frontPicId, u8 battlerId) void DecompressTrainerBackPic(u16 backPicId, u8 battlerId) { u8 position = GetBattlerPosition(battlerId); +#ifdef BUGFIX + CpuCopy32(gTrainerBackPicTable[backPicId].data, gMonSpritesGfxPtr->sprites.ptr[position], gTrainerBackPicTable[backPicId].size); +#else + // Trainer back pics aren't compressed! + // Attempting to decompress the uncompressed data can softlock or crash the game. + // This is ok in vanilla by chance, because the pixels in the trainer back sprites that correspond + // to the compressed data's header are all 0, so the decompression does nothing. DecompressPicFromTable_2(&gTrainerBackPicTable[backPicId], gMonSpritesGfxPtr->sprites.ptr[position], SPECIES_NONE); +#endif LoadCompressedPalette(gTrainerBackPicPaletteTable[backPicId].data, OBJ_PLTT_ID(battlerId), PLTT_SIZE_4BPP); } diff --git a/src/trainer_pokemon_sprites.c b/src/trainer_pokemon_sprites.c index f36060e8d1c5..909b86ab8c66 100644 --- a/src/trainer_pokemon_sprites.c +++ b/src/trainer_pokemon_sprites.c @@ -57,10 +57,11 @@ bool16 ResetAllPicSprites(void) return FALSE; } -static bool16 DecompressPic(u16 species, u32 personality, bool8 isFrontPic, u8 *dest, bool8 isTrainer, bool8 ignoreDeoxys) +static bool16 DecompressPic(u16 picId, u32 personality, bool8 isFrontPic, u8 *dest, bool8 isTrainer, bool8 ignoreDeoxys) { if (!isTrainer) { + u16 species = picId; if (isFrontPic) { if (!ignoreDeoxys) @@ -78,10 +79,23 @@ static bool16 DecompressPic(u16 species, u32 personality, bool8 isFrontPic, u8 * } else { + u16 trainerPicId = picId; if (isFrontPic) - DecompressPicFromTable(&gTrainerFrontPicTable[species], dest, species); + { + DecompressPicFromTable(&gTrainerFrontPicTable[trainerPicId], dest, trainerPicId); + } else - DecompressPicFromTable(&gTrainerBackPicTable[species], dest, species); + { +#ifdef BUGFIX + CpuCopy32(gTrainerBackPicTable[trainerPicId].data, dest, gTrainerBackPicTable[trainerPicId].size); +#else + // Trainer back pics aren't compressed! + // Attempting to decompress the uncompressed data can softlock or crash the game. + // This is ok in vanilla by chance, because the pixels in the trainer back sprites that correspond + // to the compressed data's header are all 0, so the decompression does nothing. + DecompressPicFromTable(&gTrainerBackPicTable[trainerPicId], dest, trainerPicId); +#endif + } } return FALSE; }