Document trainer hill ereader

This commit is contained in:
GriffinR 2021-04-24 18:04:27 -04:00
parent 004c37bf02
commit 86475c5ded
4 changed files with 62 additions and 57 deletions

View file

@ -37,8 +37,9 @@
#define TRAINER_HILL_TEXT_PLAYER_WON 4 #define TRAINER_HILL_TEXT_PLAYER_WON 4
#define TRAINER_HILL_TEXT_AFTER 5 #define TRAINER_HILL_TEXT_AFTER 5
#define NUM_TRAINER_HILL_TRAINERS (NUM_TRAINER_HILL_FLOORS * 2) #define TRAINER_HILL_TRAINERS_PER_FLOOR 2
#define NUM_TRAINER_HILL_TRAINERS_JP (NUM_TRAINER_HILL_FLOORS_JP * 2) #define NUM_TRAINER_HILL_TRAINERS (NUM_TRAINER_HILL_FLOORS * TRAINER_HILL_TRAINERS_PER_FLOOR)
#define NUM_TRAINER_HILL_TRAINERS_JP (NUM_TRAINER_HILL_FLOORS_JP * TRAINER_HILL_TRAINERS_PER_FLOOR)
// Values returned by TrainerHillGetChallengeStatus // Values returned by TrainerHillGetChallengeStatus
#define TRAINER_HILL_PLAYER_STATUS_LOST 0 #define TRAINER_HILL_PLAYER_STATUS_LOST 0

View file

@ -5,24 +5,23 @@
struct EReaderTrainerHillTrainer struct EReaderTrainerHillTrainer
{ {
u8 unk0; u8 trainerNum;
struct TrainerHillTrainer unk4; struct TrainerHillTrainer trainer;
struct TrHillDisplay unk14C; struct TrHillDisplay display;
u32 checksum; u32 checksum;
}; // size=0x274 }; // size=0x274
struct EReaderTrainerHillSet struct EReaderTrainerHillSet
{ {
u8 count; u8 numTrainers;
u8 id; u8 id;
u16 dummy;
u32 checksum; u32 checksum;
struct EReaderTrainerHillTrainer unk_8[6]; struct EReaderTrainerHillTrainer trainers[6];
u8 unk_ec0[40]; u8 unk_ec0[40];
}; // size = 0xf00 }; // size = 0xf00
bool8 EReader_IsReceivedDataValid(struct EReaderTrainerHillSet *buffer); bool8 ValidateTrainerHillData(struct EReaderTrainerHillSet *);
bool32 TryWriteTrainerHill(struct EReaderTrainerHillSet *arg0); bool32 TryWriteTrainerHill(struct EReaderTrainerHillSet *);
bool32 ReadTrainerHillAndValidate(void); bool32 ReadTrainerHillAndValidate(void);
int EReaderHandleTransfer(u8, size_t, const void *, void *); int EReaderHandleTransfer(u8, size_t, const void *, void *);
void EReaderHelper_Timer3Callback(void); void EReaderHelper_Timer3Callback(void);

View file

@ -14,6 +14,7 @@
#include "constants/trainers.h" #include "constants/trainers.h"
#include "constants/moves.h" #include "constants/moves.h"
#include "constants/items.h" #include "constants/items.h"
#include "constants/trainer_hill.h"
enum { enum {
EREADER_XFR_STATE_INIT = 0, EREADER_XFR_STATE_INIT = 0,
@ -419,127 +420,131 @@ static const struct TrainerHillTrainer sTrainerHillTrainerTemplates_JP[] = {
}, },
}; };
static u8 sub_81D38D4(void) static u8 GetTrainerHillUnkVal(void)
{ {
return (gSaveBlock1Ptr->trainerHill.unused + 1) % 256; return (gSaveBlock1Ptr->trainerHill.unused + 1) % 256;
} }
static bool32 Struct_EReaderTrainerHillTrainer_ValidateChecksum(struct EReaderTrainerHillTrainer *arg0) static bool32 ValidateTrainerChecksum(struct EReaderTrainerHillTrainer * hillTrainer)
{ {
int checksum = CalcByteArraySum((u8 *)arg0, 0x270); int checksum = CalcByteArraySum((u8 *)hillTrainer, offsetof(typeof(*hillTrainer), checksum));
if (checksum != arg0->checksum) if (checksum != hillTrainer->checksum)
return FALSE; return FALSE;
return TRUE; return TRUE;
} }
bool8 EReader_IsReceivedDataValid(struct EReaderTrainerHillSet *buffer) bool8 ValidateTrainerHillData(struct EReaderTrainerHillSet * hillSet)
{ {
u32 i; u32 i;
u32 checksum; u32 checksum;
int var0 = buffer->count; int numTrainers = hillSet->numTrainers;
if (var0 < 1 || var0 > 8)
// Validate number of trainers
if (numTrainers < 1 || numTrainers > NUM_TRAINER_HILL_TRAINERS)
return FALSE; return FALSE;
for (i = 0; i < var0; i++) // Validate trainers
for (i = 0; i < numTrainers; i++)
{ {
if (!Struct_EReaderTrainerHillTrainer_ValidateChecksum(&buffer->unk_8[i])) if (!ValidateTrainerChecksum(&hillSet->trainers[i]))
return FALSE; return FALSE;
} }
checksum = CalcByteArraySum((u8 *)buffer->unk_8, var0 * sizeof(struct EReaderTrainerHillTrainer)); // Validate checksum
if (checksum != buffer->checksum) checksum = CalcByteArraySum((u8 *)hillSet->trainers, numTrainers * sizeof(struct EReaderTrainerHillTrainer));
if (checksum != hillSet->checksum)
return FALSE; return FALSE;
return TRUE; return TRUE;
} }
static bool32 TrainerHill_VerifyChecksum(struct EReaderTrainerHillSet *buffer) static bool32 ValidateTrainerHillChecksum(struct EReaderTrainerHillSet *hillSet)
{ {
u32 checksum; u32 checksum;
int var0 = buffer->count; int numTrainers = hillSet->numTrainers;
if (var0 < 1 || var0 > 8) if (numTrainers < 1 || numTrainers > NUM_TRAINER_HILL_TRAINERS)
return FALSE; return FALSE;
checksum = CalcByteArraySum((u8 *)buffer->unk_8, sizeof(struct EReaderTrainerHillSet) - offsetof(struct EReaderTrainerHillSet, unk_8)); checksum = CalcByteArraySum((u8 *)hillSet->trainers, sizeof(struct EReaderTrainerHillSet) - offsetof(struct EReaderTrainerHillSet, trainers));
if (checksum != buffer->checksum) if (checksum != hillSet->checksum)
return FALSE; return FALSE;
return TRUE; return TRUE;
} }
static bool32 TryWriteTrainerHill_r(struct EReaderTrainerHillSet *ttdata, struct TrHillTag *buffer2) static bool32 TryWriteTrainerHill_Internal(struct EReaderTrainerHillSet * hillSet, struct TrHillTag * hillTag)
{ {
int i; int i;
AGB_ASSERT_EX(ttdata->dummy == 0, "cereader_tool.c", 450); AGB_ASSERT_EX(hillSet->dummy == 0, "cereader_tool.c", 450);
AGB_ASSERT_EX(ttdata->id == 0, "cereader_tool.c", 452); AGB_ASSERT_EX(hillSet->id == 0, "cereader_tool.c", 452);
memset(buffer2, 0, 0x1000); memset(hillTag, 0, SECTOR_SIZE);
buffer2->numTrainers = ttdata->count; hillTag->numTrainers = hillSet->numTrainers;
buffer2->unused1 = sub_81D38D4(); hillTag->unused1 = GetTrainerHillUnkVal();
buffer2->numFloors = (ttdata->count + 1) / 2; hillTag->numFloors = (hillSet->numTrainers + 1) / TRAINER_HILL_TRAINERS_PER_FLOOR;
for (i = 0; i < ttdata->count; i++) for (i = 0; i < hillSet->numTrainers; i++)
{ {
if (!(i & 1)) if (!(i & 1))
{ {
buffer2->floors[i / 2].trainerNum1 = ttdata->unk_8[i].unk0; hillTag->floors[i / TRAINER_HILL_TRAINERS_PER_FLOOR].trainerNum1 = hillSet->trainers[i].trainerNum;
buffer2->floors[i / 2].display = ttdata->unk_8[i].unk14C; hillTag->floors[i / TRAINER_HILL_TRAINERS_PER_FLOOR].display = hillSet->trainers[i].display;
buffer2->floors[i / 2].trainers[0] = ttdata->unk_8[i].unk4; hillTag->floors[i / TRAINER_HILL_TRAINERS_PER_FLOOR].trainers[0] = hillSet->trainers[i].trainer;
} }
else else
{ {
buffer2->floors[i / 2].trainerNum2 = ttdata->unk_8[i].unk0; hillTag->floors[i / TRAINER_HILL_TRAINERS_PER_FLOOR].trainerNum2 = hillSet->trainers[i].trainerNum;
buffer2->floors[i / 2].trainers[1] = ttdata->unk_8[i].unk4; hillTag->floors[i / TRAINER_HILL_TRAINERS_PER_FLOOR].trainers[1] = hillSet->trainers[i].trainer;
} }
} }
if (i & 1) if (i & 1)
{ {
buffer2->floors[i / 2].trainers[1] = sTrainerHillTrainerTemplates_JP[i / 2]; hillTag->floors[i / TRAINER_HILL_TRAINERS_PER_FLOOR].trainers[1] = sTrainerHillTrainerTemplates_JP[i / TRAINER_HILL_TRAINERS_PER_FLOOR];
} }
buffer2->checksum = CalcByteArraySum((u8 *)buffer2->floors, 4 * sizeof(struct TrHillFloor)); hillTag->checksum = CalcByteArraySum((u8 *)hillTag->floors, NUM_TRAINER_HILL_FLOORS * sizeof(struct TrHillFloor));
if (TryWriteSpecialSaveSection(SECTOR_ID_TRAINER_HILL, (u8 *)buffer2) != SAVE_STATUS_OK) if (TryWriteSpecialSaveSection(SECTOR_ID_TRAINER_HILL, (u8 *)hillTag) != SAVE_STATUS_OK)
return FALSE; return FALSE;
return TRUE; return TRUE;
} }
bool32 TryWriteTrainerHill(struct EReaderTrainerHillSet *arg0) bool32 TryWriteTrainerHill(struct EReaderTrainerHillSet * hillSet)
{ {
void *var0 = AllocZeroed(0x1000); void *buffer = AllocZeroed(SECTOR_SIZE);
bool32 result = TryWriteTrainerHill_r(arg0, var0); bool32 result = TryWriteTrainerHill_Internal(hillSet, buffer);
Free(var0); Free(buffer);
return result; return result;
} }
static bool32 TryReadTrainerHill_r(struct EReaderTrainerHillSet *dst, u8 *buffer) static bool32 TryReadTrainerHill_Internal(struct EReaderTrainerHillSet * dest, u8 * buffer)
{ {
if (TryReadSpecialSaveSection(SECTOR_ID_TRAINER_HILL, buffer) != SAVE_STATUS_OK) if (TryReadSpecialSaveSection(SECTOR_ID_TRAINER_HILL, buffer) != SAVE_STATUS_OK)
return FALSE; return FALSE;
memcpy(dst, buffer, sizeof(struct EReaderTrainerHillSet)); memcpy(dest, buffer, sizeof(struct EReaderTrainerHillSet));
if (!TrainerHill_VerifyChecksum(dst)) if (!ValidateTrainerHillChecksum(dest))
return FALSE; return FALSE;
return TRUE; return TRUE;
} }
static bool32 TryReadTrainerHill(struct EReaderTrainerHillSet *arg0) static bool32 TryReadTrainerHill(struct EReaderTrainerHillSet * hillSet)
{ {
u8 *var0 = AllocZeroed(0x1000); u8 *buffer = AllocZeroed(SECTOR_SIZE);
bool32 result = TryReadTrainerHill_r(arg0, var0); bool32 result = TryReadTrainerHill_Internal(hillSet, buffer);
Free(var0); Free(buffer);
return result; return result;
} }
bool32 ReadTrainerHillAndValidate(void) bool32 ReadTrainerHillAndValidate(void)
{ {
struct EReaderTrainerHillSet *var0 = AllocZeroed(0x1000); struct EReaderTrainerHillSet *hillSet = AllocZeroed(SECTOR_SIZE);
bool32 result = TryReadTrainerHill(var0); bool32 result = TryReadTrainerHill(hillSet);
Free(var0); Free(hillSet);
return result; return result;
} }

View file

@ -401,7 +401,7 @@ static void sub_81D5084(u8 taskId)
} }
break; break;
case 15: case 15:
data->unkE = EReader_IsReceivedDataValid((struct EReaderTrainerHillSet *)gDecompressionBuffer); data->unkE = ValidateTrainerHillData((struct EReaderTrainerHillSet *)gDecompressionBuffer);
SetCloseLinkCallbackAndType(data->unkE); SetCloseLinkCallbackAndType(data->unkE);
data->unk8 = 16; data->unk8 = 16;
break; break;