Clean up some of save.c

This commit is contained in:
GriffinR 2020-01-12 15:27:37 -05:00 committed by huderlem
parent de49445a79
commit 58e64b9524
13 changed files with 190 additions and 182 deletions

View File

@ -10,4 +10,4 @@ gSaveFileStatus
gGameContinueCallback
gRamSaveSectionLocations
gSaveUnusedVar2
gUnknown_03006294
gSaveAttemptStatus

View File

@ -55,13 +55,25 @@ enum
SAVE_HALL_OF_FAME_ERASE_BEFORE // unused
};
#define SECTOR_ID_SAVEBLOCK2 0
#define SECTOR_ID_SAVEBLOCK1_START 1
#define SECTOR_ID_SAVEBLOCK1_END 4
#define SECTOR_ID_PKMN_STORAGE_START 5
#define SECTOR_ID_PKMN_STORAGE_END 13
#define SECTOR_SAVE_SLOT_LENGTH 14
// Save Slot 1: 0-13; Save Slot 2: 14-27
#define SECTOR_ID_HOF_1 28
#define SECTOR_ID_HOF_2 29
#define SECTOR_ID_TRAINER_HILL 30
#define SECTOR_ID_RECORDED_BATTLE 31
#define SECTORS_COUNT 32
#define SAVE_STATUS_EMPTY 0
#define SAVE_STATUS_OK 1
#define SAVE_STATUS_CORRUPT 2
#define SAVE_STATUS_NO_FLASH 4
#define SAVE_STATUS_ERROR 0xFF
extern u16 gLastWrittenSector;
extern u32 gLastSaveCounter;
extern u16 gLastKnownGoodSector;
@ -82,13 +94,12 @@ u8 HandleSavingData(u8 saveType);
u8 TrySavingData(u8 saveType);
bool8 sub_8153380(void);
bool8 sub_81533AC(void);
u8 sub_81533E0(void);
u8 sub_8153408(void);
u8 FullSaveGame(void);
bool8 sub_81533E0(void);
bool8 sub_8153408(void);
bool8 FullSaveGame(void);
bool8 CheckSaveFile(void);
u8 Save_LoadGameData(u8 a1);
u8 Save_LoadGameData(u8 saveType);
u16 sub_815355C(void);
u8 sub_81534D0(u8);
u32 TryReadSpecialSaveSection(u8 sector, u8* dst);
u32 TryWriteSpecialSaveSection(u8 sector, u8* src);
void sub_8153688(u8 taskId);

View File

@ -473,7 +473,7 @@ static bool32 TryWriteTrainerHill_r(struct EReaderTrainerHillSet *ttdata, struct
}
buffer2->checksum = CalcByteArraySum((u8 *)buffer2->floors, 4 * sizeof(struct TrHillFloor));
if (TryWriteSpecialSaveSection(SECTOR_ID_TRAINER_HILL, (u8 *)buffer2) != 1)
if (TryWriteSpecialSaveSection(SECTOR_ID_TRAINER_HILL, (u8 *)buffer2) != SAVE_STATUS_OK)
return FALSE;
return TRUE;
@ -487,13 +487,13 @@ bool32 TryWriteTrainerHill(struct EReaderTrainerHillSet *arg0)
return result;
}
static bool32 TryReadTrainerHill_r(struct EReaderTrainerHillSet *arg0, u8 *arg1)
static bool32 TryReadTrainerHill_r(struct EReaderTrainerHillSet *dst, u8 *buffer)
{
if (TryReadSpecialSaveSection(SECTOR_ID_TRAINER_HILL, arg1) != 1)
if (TryReadSpecialSaveSection(SECTOR_ID_TRAINER_HILL, buffer) != SAVE_STATUS_OK)
return FALSE;
memcpy(arg0, arg1, sizeof(struct EReaderTrainerHillSet));
if (!TrainerHill_VerifyChecksum(arg0))
memcpy(dst, buffer, sizeof(struct EReaderTrainerHillSet));
if (!TrainerHill_VerifyChecksum(dst))
return FALSE;
return TRUE;

View File

@ -467,7 +467,7 @@ static void Task_Hof_InitTeamSaveData(u8 taskId)
}
else
{
if (Save_LoadGameData(3) != TRUE)
if (Save_LoadGameData(SAVE_HALL_OF_FAME) != SAVE_STATUS_OK)
memset(gDecompressionBuffer, 0, 0x2000);
}
@ -498,7 +498,7 @@ static void Task_Hof_InitTeamSaveData(u8 taskId)
static void Task_Hof_TrySaveData(u8 taskId)
{
gGameContinueCallback = CB2_DoHallOfFameScreenDontSaveData;
if (TrySavingData(SAVE_HALL_OF_FAME) == 0xFF && gDamagedSaveSectors != 0)
if (TrySavingData(SAVE_HALL_OF_FAME) == SAVE_STATUS_ERROR && gDamagedSaveSectors != 0)
{
UnsetBgTilemapBuffer(1);
UnsetBgTilemapBuffer(3);
@ -836,7 +836,7 @@ void CB2_DoHallOfFamePC(void)
static void Task_HofPC_CopySaveData(u8 taskId)
{
sub_81980F0(0, 0x1E, 0, 0xC, 0x226);
if (Save_LoadGameData(3) != 1)
if (Save_LoadGameData(SAVE_HALL_OF_FAME) != SAVE_STATUS_OK)
{
gTasks[taskId].func = Task_HofPC_PrintDataIsCorrupted;
}

View File

@ -1070,7 +1070,7 @@ void CB2_InitCopyrightScreenAfterBootup(void)
ResetMenuAndMonGlobals();
Save_ResetSaveCounters();
Save_LoadGameData(SAVE_NORMAL);
if (gSaveFileStatus == 0 || gSaveFileStatus == 2)
if (gSaveFileStatus == SAVE_STATUS_EMPTY || gSaveFileStatus == SAVE_STATUS_CORRUPT)
Sav2_ClearSetDefault();
SetPokemonCryStereo(gSaveBlock2Ptr->optionsSound);
InitHeap(gHeap, HEAP_SIZE);

View File

@ -643,7 +643,7 @@ static void Task_MainMenuCheckSaveFile(u8 taskId)
tWirelessAdapterConnected = TRUE;
switch (gSaveFileStatus)
{
case 1:
case SAVE_STATUS_OK:
tMenuType = HAS_SAVED_GAME;
if (IsMysteryGiftEnabled())
tMenuType++;
@ -654,19 +654,19 @@ static void Task_MainMenuCheckSaveFile(u8 taskId)
tMenuType = HAS_NO_SAVED_GAME;
gTasks[taskId].func = Task_WaitForSaveFileErrorWindow;
break;
case 0xFF:
case SAVE_STATUS_ERROR:
CreateMainMenuErrorWindow(gText_SaveFileCorrupted);
gTasks[taskId].func = Task_WaitForSaveFileErrorWindow;
tMenuType = HAS_SAVED_GAME;
if (IsMysteryGiftEnabled() == TRUE)
tMenuType++;
break;
case 0:
case SAVE_STATUS_EMPTY:
default:
tMenuType = HAS_NO_SAVED_GAME;
gTasks[taskId].func = Task_MainMenuCheckBattery;
break;
case 4:
case SAVE_STATUS_NO_FLASH:
CreateMainMenuErrorWindow(gJPText_No1MSubCircuit);
gTasks[taskId].tMenuType = HAS_NO_SAVED_GAME;
gTasks[taskId].func = Task_WaitForSaveFileErrorWindow;

View File

@ -150,7 +150,7 @@ void ResetMenuAndMonGlobals(void)
void NewGameInitData(void)
{
if (gSaveFileStatus == 0 || gSaveFileStatus == 2)
if (gSaveFileStatus == SAVE_STATUS_EMPTY || gSaveFileStatus == SAVE_STATUS_CORRUPT)
RtcReset();
gDifferentSaveFile = 1;

View File

@ -1714,7 +1714,7 @@ void CB2_ContinueSavedGame(void)
FieldClearVBlankHBlankCallbacks();
StopMapMusic();
ResetSafariZoneFlag_();
if (gSaveFileStatus == 0xFF)
if (gSaveFileStatus == SAVE_STATUS_ERROR)
ResetWinStreaks();
LoadSaveblockMapHeader();

View File

@ -323,7 +323,7 @@ static bool32 RecordedBattleToSave(struct RecordedBattleSave *battleSave, struct
saveSection->checksum = CalcByteArraySum((void*)(saveSection), sizeof(*saveSection) - 4);
if (TryWriteSpecialSaveSection(SECTOR_ID_RECORDED_BATTLE, (void*)(saveSection)) != 1)
if (TryWriteSpecialSaveSection(SECTOR_ID_RECORDED_BATTLE, (void*)(saveSection)) != SAVE_STATUS_OK)
return FALSE;
else
return TRUE;
@ -492,7 +492,7 @@ bool32 MoveRecordedBattleToSaveData(void)
static bool32 TryCopyRecordedBattleSaveData(struct RecordedBattleSave *dst, struct SaveSection *saveBuffer)
{
if (TryReadSpecialSaveSection(SECTOR_ID_RECORDED_BATTLE, (void*)(saveBuffer)) != 1)
if (TryReadSpecialSaveSection(SECTOR_ID_RECORDED_BATTLE, (void*)(saveBuffer)) != SAVE_STATUS_OK)
return FALSE;
memcpy(dst, saveBuffer, sizeof(struct RecordedBattleSave));

View File

@ -559,7 +559,7 @@ static void Task_ResetRtcScreen(u8 taskId)
case 1:
if (!gPaletteFade.active)
{
if (gSaveFileStatus == 0 || gSaveFileStatus == 2)
if (gSaveFileStatus == SAVE_STATUS_EMPTY || gSaveFileStatus == SAVE_STATUS_CORRUPT)
{
ShowMessage(gText_NoSaveFileCantSetTime);
data[0] = 5;
@ -608,7 +608,7 @@ static void Task_ResetRtcScreen(u8 taskId)
}
break;
case 4:
if (TrySavingData(SAVE_NORMAL) == 1)
if (TrySavingData(SAVE_NORMAL) == SAVE_STATUS_OK)
{
ShowMessage(gText_SaveCompleted);
PlaySE(SE_PINPON);

View File

@ -21,8 +21,8 @@ void sub_81700F8(void)
SetSaveBlocksPointers(sub_815355C());
ResetMenuAndMonGlobals();
Save_ResetSaveCounters();
Save_LoadGameData(0);
if (gSaveFileStatus == 0 || gSaveFileStatus == 2)
Save_LoadGameData(SAVE_NORMAL);
if (gSaveFileStatus == SAVE_STATUS_EMPTY || gSaveFileStatus == SAVE_STATUS_CORRUPT)
Sav2_ClearSetDefault();
SetPokemonCryStereo(gSaveBlock2Ptr->optionsSound);
InitHeap(gHeap, HEAP_SIZE);

View File

@ -14,7 +14,7 @@
#include "constants/game_stat.h"
static u16 CalculateChecksum(void *data, u16 size);
static u8 DoReadFlashWholeSection(u8 sector, struct SaveSection *section);
static bool8 DoReadFlashWholeSection(u8 sector, struct SaveSection *section);
static u8 GetSaveValidStatus(const struct SaveSectionLocation *location);
static u8 sub_8152E10(u16 a1, const struct SaveSectionLocation *location);
static u8 ClearSaveData_2(u16 a1, const struct SaveSectionLocation *location);
@ -53,7 +53,7 @@ static u8 HandleWriteSector(u16 a1, const struct SaveSectionLocation *location);
min(sizeof(structure) - chunkNum * SECTOR_DATA_SIZE, SECTOR_DATA_SIZE) \
} \
const struct SaveSectionOffsets gSaveSectionOffsets[] =
static const struct SaveSectionOffsets sSaveSectionOffsets[] =
{
SAVEBLOCK_CHUNK(gSaveblock2, 0),
@ -84,9 +84,9 @@ u16 gUnknown_03006208;
u16 gSaveUnusedVar;
u16 gSaveFileStatus;
void (*gGameContinueCallback)(void);
struct SaveSectionLocation gRamSaveSectionLocations[0xE];
struct SaveSectionLocation gRamSaveSectionLocations[SECTOR_SAVE_SLOT_LENGTH];
u16 gSaveUnusedVar2;
u16 gUnknown_03006294;
u16 gSaveAttemptStatus;
EWRAM_DATA struct SaveSection gSaveDataBuffer = {0};
EWRAM_DATA static u8 sUnusedVar = 0;
@ -130,16 +130,16 @@ static bool32 SetDamagedSectorBits(u8 op, u8 bit)
return retVal;
}
static u8 save_write_to_flash(u16 a1, const struct SaveSectionLocation *location)
static u8 SaveWriteToFlash(u16 a1, const struct SaveSectionLocation *location)
{
u32 retVal;
u32 status;
u16 i;
gFastSaveSection = &gSaveDataBuffer;
if (a1 != 0xFFFF) // for link
{
retVal = HandleWriteSector(a1, location);
status = HandleWriteSector(a1, location);
}
else
{
@ -148,41 +148,41 @@ static u8 save_write_to_flash(u16 a1, const struct SaveSectionLocation *location
gLastWrittenSector++;
gLastWrittenSector = gLastWrittenSector % SECTOR_SAVE_SLOT_LENGTH; // array count save sector locations
gSaveCounter++;
retVal = 1;
status = SAVE_STATUS_OK;
for (i = 0; i < SECTOR_SAVE_SLOT_LENGTH; i++)
HandleWriteSector(i, location);
if (gDamagedSaveSectors != 0) // skip the damaged sector.
{
retVal = 0xFF;
status = SAVE_STATUS_ERROR;
gLastWrittenSector = gLastKnownGoodSector;
gSaveCounter = gLastSaveCounter;
}
}
return retVal;
return status;
}
static u8 HandleWriteSector(u16 a1, const struct SaveSectionLocation *location)
static u8 HandleWriteSector(u16 sectorId, const struct SaveSectionLocation *location)
{
u16 i;
u16 sector;
u8 *data;
u16 size;
sector = a1 + gLastWrittenSector;
sector = sectorId + gLastWrittenSector;
sector %= SECTOR_SAVE_SLOT_LENGTH;
sector += SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % 2);
data = location[a1].data;
size = location[a1].size;
data = location[sectorId].data;
size = location[sectorId].size;
// clear save section.
for (i = 0; i < sizeof(struct SaveSection); i++)
((char *)gFastSaveSection)[i] = 0;
gFastSaveSection->id = a1;
gFastSaveSection->id = sectorId;
gFastSaveSection->security = UNKNOWN_CHECK_VALUE;
gFastSaveSection->counter = gSaveCounter;
@ -215,12 +215,12 @@ static u8 TryWriteSector(u8 sector, u8 *data)
if (ProgramFlashSectorAndVerify(sector, data) != 0) // is damaged?
{
SetDamagedSectorBits(ENABLE, sector); // set damaged sector bits.
return 0xFF;
return SAVE_STATUS_ERROR;
}
else
{
SetDamagedSectorBits(DISABLE, sector); // unset damaged sector bits. it's safe now.
return 1;
return SAVE_STATUS_OK;
}
}
@ -247,46 +247,46 @@ static u32 RestoreSaveBackupVars(const struct SaveSectionLocation *location) //
return 0;
}
static u8 sub_81529D4(u16 a1, const struct SaveSectionLocation *location)
static u8 sub_81529D4(u16 sectorId, const struct SaveSectionLocation *location)
{
u8 retVal;
u8 status;
if (gUnknown_03006208 < a1 - 1)
if (gUnknown_03006208 < sectorId - 1)
{
retVal = 1;
status = SAVE_STATUS_OK;
HandleWriteSector(gUnknown_03006208, location);
gUnknown_03006208++;
if (gDamagedSaveSectors)
{
retVal = 0xFF;
status = SAVE_STATUS_ERROR;
gLastWrittenSector = gLastKnownGoodSector;
gSaveCounter = gLastSaveCounter;
}
}
else
{
retVal = 0xFF;
status = SAVE_STATUS_ERROR;
}
return retVal;
return status;
}
static u8 sub_8152A34(u16 a1, const struct SaveSectionLocation *location)
static u8 sub_8152A34(u16 sectorId, const struct SaveSectionLocation *location)
{
u8 retVal = 1;
u8 status = SAVE_STATUS_OK;
ClearSaveData_2(a1 - 1, location);
ClearSaveData_2(sectorId - 1, location);
if (gDamagedSaveSectors)
{
retVal = 0xFF;
status = SAVE_STATUS_ERROR;
gLastWrittenSector = gLastKnownGoodSector;
gSaveCounter = gLastSaveCounter;
}
return retVal;
return status;
}
static u8 ClearSaveData_2(u16 a1, const struct SaveSectionLocation *location)
static u8 ClearSaveData_2(u16 sectorId, const struct SaveSectionLocation *location)
{
u16 i;
u16 sector;
@ -294,18 +294,18 @@ static u8 ClearSaveData_2(u16 a1, const struct SaveSectionLocation *location)
u16 size;
u8 status;
sector = a1 + gLastWrittenSector;
sector = sectorId + gLastWrittenSector;
sector %= SECTOR_SAVE_SLOT_LENGTH;
sector += SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % 2);
data = location[a1].data;
size = location[a1].size;
data = location[sectorId].data;
size = location[sectorId].size;
// clear temp save section.
for (i = 0; i < sizeof(struct SaveSection); i++)
((char *)gFastSaveSection)[i] = 0;
gFastSaveSection->id = a1;
gFastSaveSection->id = sectorId;
gFastSaveSection->security = UNKNOWN_CHECK_VALUE;
gFastSaveSection->counter = gSaveCounter;
@ -318,53 +318,53 @@ static u8 ClearSaveData_2(u16 a1, const struct SaveSectionLocation *location)
EraseFlashSector(sector);
status = 1;
status = SAVE_STATUS_OK;
for (i = 0; i < sizeof(struct UnkSaveSection); i++)
{
if (ProgramFlashByte(sector, i, ((u8 *)gFastSaveSection)[i]))
{
status = 0xFF;
status = SAVE_STATUS_ERROR;
break;
}
}
if (status == 0xFF)
if (status == SAVE_STATUS_ERROR)
{
SetDamagedSectorBits(ENABLE, sector);
return 0xFF;
return SAVE_STATUS_ERROR;
}
else
{
status = 1;
status = SAVE_STATUS_OK;
for (i = 0; i < 7; i++)
{
if (ProgramFlashByte(sector, 0xFF9 + i, ((u8 *)gFastSaveSection)[0xFF9 + i]))
{
status = 0xFF;
status = SAVE_STATUS_ERROR;
break;
}
}
if (status == 0xFF)
if (status == SAVE_STATUS_ERROR)
{
SetDamagedSectorBits(ENABLE, sector);
return 0xFF;
return SAVE_STATUS_ERROR;
}
else
{
SetDamagedSectorBits(DISABLE, sector);
return 1;
return SAVE_STATUS_OK;
}
}
}
static u8 sav12_xor_get(u16 a1, const struct SaveSectionLocation *location)
static u8 sav12_xor_get(u16 sectorId, const struct SaveSectionLocation *location)
{
u16 sector;
sector = a1 + gLastWrittenSector; // no sub 1?
sector = sectorId + gLastWrittenSector; // no sub 1?
sector %= SECTOR_SAVE_SLOT_LENGTH;
sector += SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % 2);
@ -374,20 +374,20 @@ static u8 sav12_xor_get(u16 a1, const struct SaveSectionLocation *location)
SetDamagedSectorBits(ENABLE, sector);
gLastWrittenSector = gLastKnownGoodSector;
gSaveCounter = gLastSaveCounter;
return 0xFF;
return SAVE_STATUS_ERROR;
}
else
{
SetDamagedSectorBits(DISABLE, sector);
return 1;
return SAVE_STATUS_OK;
}
}
static u8 sub_8152CAC(u16 a1, const struct SaveSectionLocation *location)
static u8 sub_8152CAC(u16 sectorId, const struct SaveSectionLocation *location)
{
u16 sector;
sector = a1 + gLastWrittenSector - 1;
sector = sectorId + gLastWrittenSector - 1;
sector %= SECTOR_SAVE_SLOT_LENGTH;
sector += SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % 2);
@ -397,20 +397,20 @@ static u8 sub_8152CAC(u16 a1, const struct SaveSectionLocation *location)
SetDamagedSectorBits(ENABLE, sector);
gLastWrittenSector = gLastKnownGoodSector;
gSaveCounter = gLastSaveCounter;
return 0xFF;
return SAVE_STATUS_ERROR;
}
else
{
SetDamagedSectorBits(DISABLE, sector);
return 1;
return SAVE_STATUS_OK;
}
}
static u8 sub_8152D44(u16 a1, const struct SaveSectionLocation *location)
static u8 sub_8152D44(u16 sectorId, const struct SaveSectionLocation *location)
{
u16 sector;
sector = a1 + gLastWrittenSector - 1; // no sub 1?
sector = sectorId + gLastWrittenSector - 1; // no sub 1?
sector %= SECTOR_SAVE_SLOT_LENGTH;
sector += SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % 2);
@ -420,30 +420,30 @@ static u8 sub_8152D44(u16 a1, const struct SaveSectionLocation *location)
SetDamagedSectorBits(ENABLE, sector);
gLastWrittenSector = gLastKnownGoodSector;
gSaveCounter = gLastSaveCounter;
return 0xFF;
return SAVE_STATUS_ERROR;
}
else
{
SetDamagedSectorBits(DISABLE, sector);
return 1;
return SAVE_STATUS_OK;
}
}
static u8 sub_8152DD0(u16 a1, const struct SaveSectionLocation *location)
{
u8 retVal;
u8 status;
gFastSaveSection = &gSaveDataBuffer;
if (a1 != 0xFFFF)
{
retVal = 0xFF;
status = SAVE_STATUS_ERROR;
}
else
{
retVal = GetSaveValidStatus(location);
status = GetSaveValidStatus(location);
sub_8152E10(0xFFFF, location);
}
return retVal;
return status;
}
static u8 sub_8152E10(u16 a1, const struct SaveSectionLocation *location)
@ -469,7 +469,7 @@ static u8 sub_8152E10(u16 a1, const struct SaveSectionLocation *location)
}
}
return 1;
return SAVE_STATUS_OK;
}
static u8 GetSaveValidStatus(const struct SaveSectionLocation *location)
@ -502,13 +502,13 @@ static u8 GetSaveValidStatus(const struct SaveSectionLocation *location)
if (securityPassed)
{
if (slotCheckField == 0x3FFF)
saveSlot1Status = 1;
saveSlot1Status = SAVE_STATUS_OK;
else
saveSlot1Status = 255;
saveSlot1Status = SAVE_STATUS_ERROR;
}
else
{
saveSlot1Status = 0;
saveSlot1Status = SAVE_STATUS_EMPTY;
}
slotCheckField = 0;
@ -533,16 +533,16 @@ static u8 GetSaveValidStatus(const struct SaveSectionLocation *location)
if (securityPassed)
{
if (slotCheckField == 0x3FFF)
saveSlot2Status = 1;
saveSlot2Status = SAVE_STATUS_OK;
else
saveSlot2Status = 255;
saveSlot2Status = SAVE_STATUS_ERROR;
}
else
{
saveSlot2Status = 0;
saveSlot2Status = SAVE_STATUS_EMPTY;
}
if (saveSlot1Status == 1 && saveSlot2Status == 1)
if (saveSlot1Status == SAVE_STATUS_OK && saveSlot2Status == SAVE_STATUS_OK)
{
if ((saveSlot1Counter == -1 && saveSlot2Counter == 0) || (saveSlot1Counter == 0 && saveSlot2Counter == -1))
{
@ -558,42 +558,42 @@ static u8 GetSaveValidStatus(const struct SaveSectionLocation *location)
else
gSaveCounter = saveSlot1Counter;
}
return 1;
return SAVE_STATUS_OK;
}
if (saveSlot1Status == 1)
if (saveSlot1Status == SAVE_STATUS_OK)
{
gSaveCounter = saveSlot1Counter;
if (saveSlot2Status == 255)
return 255;
return 1;
if (saveSlot2Status == SAVE_STATUS_ERROR)
return SAVE_STATUS_ERROR;
return SAVE_STATUS_OK;
}
if (saveSlot2Status == 1)
if (saveSlot2Status == SAVE_STATUS_OK)
{
gSaveCounter = saveSlot2Counter;
if (saveSlot1Status == 255)
return 255;
return 1;
if (saveSlot1Status == SAVE_STATUS_ERROR)
return SAVE_STATUS_ERROR;
return SAVE_STATUS_OK;
}
if (saveSlot1Status == 0 && saveSlot2Status == 0)
if (saveSlot1Status == SAVE_STATUS_EMPTY && saveSlot2Status == SAVE_STATUS_EMPTY)
{
gSaveCounter = 0;
gLastWrittenSector = 0;
return 0;
return SAVE_STATUS_EMPTY;
}
gSaveCounter = 0;
gLastWrittenSector = 0;
return 2;
return SAVE_STATUS_CORRUPT;
}
static u8 sub_81530DC(u8 a1, u8 *data, u16 size)
static u8 sub_81530DC(u8 sectorId, u8 *data, u16 size)
{
u16 i;
struct SaveSection *section = &gSaveDataBuffer;
DoReadFlashWholeSection(a1, section);
DoReadFlashWholeSection(sectorId, section);
if (section->security == UNKNOWN_CHECK_VALUE)
{
u16 checksum = CalculateChecksum(section->data, size);
@ -601,23 +601,24 @@ static u8 sub_81530DC(u8 a1, u8 *data, u16 size)
{
for (i = 0; i < size; i++)
data[i] = section->data[i];
return 1;
return SAVE_STATUS_OK;
}
else
{
return 2;
return SAVE_STATUS_CORRUPT;
}
}
else
{
return 0;
return SAVE_STATUS_EMPTY;
}
}
static u8 DoReadFlashWholeSection(u8 sector, struct SaveSection *section)
// Return value always ignored
static bool8 DoReadFlashWholeSection(u8 sector, struct SaveSection *section)
{
ReadFlash(sector, 0, section->data, sizeof(struct SaveSection));
return 1;
return TRUE;
}
static u16 CalculateChecksum(void *data, u16 size)
@ -638,19 +639,19 @@ static void UpdateSaveAddresses(void)
{
int i = 0;
gRamSaveSectionLocations[i].data = (void*)(gSaveBlock2Ptr) + gSaveSectionOffsets[i].toAdd;
gRamSaveSectionLocations[i].size = gSaveSectionOffsets[i].size;
gRamSaveSectionLocations[i].data = (void*)(gSaveBlock2Ptr) + sSaveSectionOffsets[i].toAdd;
gRamSaveSectionLocations[i].size = sSaveSectionOffsets[i].size;
for (i = 1; i < 5; i++)
for (i = SECTOR_ID_SAVEBLOCK1_START; i <= SECTOR_ID_SAVEBLOCK1_END; i++)
{
gRamSaveSectionLocations[i].data = (void*)(gSaveBlock1Ptr) + gSaveSectionOffsets[i].toAdd;
gRamSaveSectionLocations[i].size = gSaveSectionOffsets[i].size;
gRamSaveSectionLocations[i].data = (void*)(gSaveBlock1Ptr) + sSaveSectionOffsets[i].toAdd;
gRamSaveSectionLocations[i].size = sSaveSectionOffsets[i].size;
}
for (i = 5; i < 14; i++)
for (i = SECTOR_ID_PKMN_STORAGE_START; i <= SECTOR_ID_PKMN_STORAGE_END; i++)
{
gRamSaveSectionLocations[i].data = (void*)(gPokemonStoragePtr) + gSaveSectionOffsets[i].toAdd;
gRamSaveSectionLocations[i].size = gSaveSectionOffsets[i].size;
gRamSaveSectionLocations[i].data = (void*)(gPokemonStoragePtr) + sSaveSectionOffsets[i].toAdd;
gRamSaveSectionLocations[i].size = sSaveSectionOffsets[i].size;
i++;i--; // needed to match
}
@ -673,36 +674,36 @@ u8 HandleSavingData(u8 saveType)
if (GetGameStat(GAME_STAT_ENTERED_HOF) < 999)
IncrementGameStat(GAME_STAT_ENTERED_HOF);
SaveSerializedGame();
save_write_to_flash(0xFFFF, gRamSaveSectionLocations);
SaveWriteToFlash(0xFFFF, gRamSaveSectionLocations);
tempAddr = gDecompressionBuffer;
HandleWriteSectorNBytes(SECTOR_ID_HOF_1, tempAddr, 0xF80);
HandleWriteSectorNBytes(SECTOR_ID_HOF_2, tempAddr + 0xF80, 0xF80);
HandleWriteSectorNBytes(SECTOR_ID_HOF_1, tempAddr, SECTOR_DATA_SIZE);
HandleWriteSectorNBytes(SECTOR_ID_HOF_2, tempAddr + SECTOR_DATA_SIZE, SECTOR_DATA_SIZE);
break;
case SAVE_NORMAL: // normal save. also called by overwriting your own save.
default:
SaveSerializedGame();
save_write_to_flash(0xFFFF, gRamSaveSectionLocations);
SaveWriteToFlash(0xFFFF, gRamSaveSectionLocations);
break;
case SAVE_LINK: // Link and Battle Frontier
case SAVE_LINK2: // Unused
SaveSerializedGame();
for(i = 0; i < 5; i++)
for(i = SECTOR_ID_SAVEBLOCK2; i <= SECTOR_ID_SAVEBLOCK1_END; i++)
ClearSaveData_2(i, gRamSaveSectionLocations);
for(i = 0; i < 5; i++)
for(i = SECTOR_ID_SAVEBLOCK2; i <= SECTOR_ID_SAVEBLOCK1_END; i++)
sav12_xor_get(i, gRamSaveSectionLocations);
break;
// Support for Ereader was removed in Emerald.
/*
case EREADER_SAVE: // used in mossdeep "game corner" before/after battling old man e-reader trainer
SaveSerializedGame();
save_write_to_flash(0, gRamSaveSectionLocations);
SaveWriteToFlash(0, gRamSaveSectionLocations);
break;
*/
case SAVE_OVERWRITE_DIFFERENT_FILE:
for (i = SECTOR_ID_HOF_1; i < SECTORS_COUNT; i++)
EraseFlashSector(i); // erase HOF.
SaveSerializedGame();
save_write_to_flash(0xFFFF, gRamSaveSectionLocations);
SaveWriteToFlash(0xFFFF, gRamSaveSectionLocations);
break;
}
gTrainerHillVBlankCounter = backupVar;
@ -713,25 +714,25 @@ u8 TrySavingData(u8 saveType)
{
if (gFlashMemoryPresent != TRUE)
{
gUnknown_03006294 = 0xFF;
return 0xFF;
gSaveAttemptStatus = SAVE_STATUS_ERROR;
return SAVE_STATUS_ERROR;
}
HandleSavingData(saveType);
if (!gDamagedSaveSectors)
{
gUnknown_03006294 = 1;
return 1;
gSaveAttemptStatus = SAVE_STATUS_OK;
return SAVE_STATUS_OK;
}
else
{
DoSaveFailedScreen(saveType);
gUnknown_03006294 = 0xFF;
return 0xFF;
gSaveAttemptStatus = SAVE_STATUS_ERROR;
return SAVE_STATUS_ERROR;
}
}
bool8 sub_8153380(void) // trade.s save
bool8 sub_8153380(void) // trade.c
{
if (gFlashMemoryPresent != TRUE)
return TRUE;
@ -741,92 +742,92 @@ bool8 sub_8153380(void) // trade.s save
return FALSE;
}
bool8 sub_81533AC(void) // trade.s save
bool8 sub_81533AC(void) // trade.c
{
u8 retVal = sub_81529D4(SECTOR_SAVE_SLOT_LENGTH, gRamSaveSectionLocations);
u8 status = sub_81529D4(SECTOR_SAVE_SLOT_LENGTH, gRamSaveSectionLocations);
if (gDamagedSaveSectors)
DoSaveFailedScreen(0);
if (retVal == 0xFF)
DoSaveFailedScreen(SAVE_NORMAL);
if (status == SAVE_STATUS_ERROR)
return TRUE;
else
return FALSE;
}
u8 sub_81533E0(void) // trade.s save
bool8 sub_81533E0(void) // trade.c
{
sub_8152A34(SECTOR_SAVE_SLOT_LENGTH, gRamSaveSectionLocations);
if (gDamagedSaveSectors)
DoSaveFailedScreen(0);
return 0;
DoSaveFailedScreen(SAVE_NORMAL);
return FALSE;
}
u8 sub_8153408(void) // trade.s save
bool8 sub_8153408(void) // trade.c
{
sub_8152CAC(SECTOR_SAVE_SLOT_LENGTH, gRamSaveSectionLocations);
if (gDamagedSaveSectors)
DoSaveFailedScreen(0);
return 0;
DoSaveFailedScreen(SAVE_NORMAL);
return FALSE;
}
u8 FullSaveGame(void)
{
if (gFlashMemoryPresent != TRUE)
return 1;
return TRUE;
UpdateSaveAddresses();
SaveSerializedGame();
RestoreSaveBackupVars(gRamSaveSectionLocations);
sub_8152A34(gUnknown_03006208 + 1, gRamSaveSectionLocations);
return 0;
return FALSE;
}
bool8 CheckSaveFile(void)
{
u8 retVal = FALSE;
u16 val = ++gUnknown_03006208;
if (val <= 4)
u16 sectorId = ++gUnknown_03006208;
if (sectorId <= SECTOR_ID_SAVEBLOCK1_END)
{
sub_8152A34(gUnknown_03006208 + 1, gRamSaveSectionLocations);
sub_8152D44(val, gRamSaveSectionLocations);
sub_8152D44(sectorId, gRamSaveSectionLocations);
}
else
{
sub_8152D44(val, gRamSaveSectionLocations);
sub_8152D44(sectorId, gRamSaveSectionLocations);
retVal = TRUE;
}
if (gDamagedSaveSectors)
DoSaveFailedScreen(1);
DoSaveFailedScreen(SAVE_LINK);
return retVal;
}
u8 Save_LoadGameData(u8 a1)
u8 Save_LoadGameData(u8 saveType)
{
u8 result;
u8 status;
if (gFlashMemoryPresent != TRUE)
{
gSaveFileStatus = 4;
return 0xFF;
gSaveFileStatus = SAVE_STATUS_NO_FLASH;
return SAVE_STATUS_ERROR;
}
UpdateSaveAddresses();
switch (a1)
switch (saveType)
{
case 0:
case SAVE_NORMAL:
default:
result = sub_8152DD0(0xFFFF, gRamSaveSectionLocations);
status = sub_8152DD0(0xFFFF, gRamSaveSectionLocations);
LoadSerializedGame();
gSaveFileStatus = result;
gSaveFileStatus = status;
gGameContinueCallback = 0;
break;
case 3:
result = sub_81530DC(0x1C, gDecompressionBuffer, 0xF80);
if(result == 1)
result = sub_81530DC(0x1D, gDecompressionBuffer + 0xF80, 0xF80);
case SAVE_HALL_OF_FAME:
status = sub_81530DC(SECTOR_ID_HOF_1, gDecompressionBuffer, SECTOR_DATA_SIZE);
if (status == SAVE_STATUS_OK)
status = sub_81530DC(SECTOR_ID_HOF_2, gDecompressionBuffer + SECTOR_DATA_SIZE, SECTOR_DATA_SIZE);
break;
}
return result;
return status;
}
u16 sub_815355C(void)
@ -836,7 +837,7 @@ u16 sub_815355C(void)
savSection = gFastSaveSection = &gSaveDataBuffer;
if (gFlashMemoryPresent != TRUE)
return 0;
return SAVE_STATUS_EMPTY;
UpdateSaveAddresses();
GetSaveValidStatus(gRamSaveSectionLocations);
v3 = SECTOR_SAVE_SLOT_LENGTH * (gSaveCounter % 2);
@ -849,7 +850,7 @@ u16 sub_815355C(void)
savSection->data[12] +
savSection->data[13];
}
return 0;
return SAVE_STATUS_EMPTY;
}
u32 TryReadSpecialSaveSection(u8 sector, u8* dst)
@ -859,17 +860,17 @@ u32 TryReadSpecialSaveSection(u8 sector, u8* dst)
u8* savData;
if (sector != SECTOR_ID_TRAINER_HILL && sector != SECTOR_ID_RECORDED_BATTLE)
return 0xFF;
return SAVE_STATUS_ERROR;
ReadFlash(sector, 0, (u8 *)&gSaveDataBuffer, sizeof(struct SaveSection));
if (*(u32*)(&gSaveDataBuffer.data[0]) != SPECIAL_SECTION_SENTINEL)
return 0xFF;
return SAVE_STATUS_ERROR;
// copies whole save section except u32 counter
i = 0;
size = 0xFFB;
savData = &gSaveDataBuffer.data[4];
for (; i <= size; i++)
dst[i] = savData[i];
return 1;
return SAVE_STATUS_OK;
}
u32 TryWriteSpecialSaveSection(u8 sector, u8* src)
@ -880,7 +881,7 @@ u32 TryWriteSpecialSaveSection(u8 sector, u8* src)
void* savDataBuffer;
if (sector != SECTOR_ID_TRAINER_HILL && sector != SECTOR_ID_RECORDED_BATTLE)
return 0xFF;
return SAVE_STATUS_ERROR;
savDataBuffer = &gSaveDataBuffer;
*(u32*)(savDataBuffer) = SPECIAL_SECTION_SENTINEL;
@ -892,8 +893,8 @@ u32 TryWriteSpecialSaveSection(u8 sector, u8* src)
for (; i <= size; i++)
savData[i] = src[i];
if (ProgramFlashSectorAndVerify(sector, savDataBuffer) != 0)
return 0xFF;
return 1;
return SAVE_STATUS_ERROR;
return SAVE_STATUS_OK;
}
void sub_8153688(u8 taskId)

View File

@ -966,8 +966,8 @@ static u8 SaveConfirmInputCallback(void)
case 0: // Yes
switch (gSaveFileStatus)
{
case 0:
case 2:
case SAVE_STATUS_EMPTY:
case SAVE_STATUS_CORRUPT:
if (gDifferentSaveFile == FALSE)
{
sSaveDialogCallback = SaveFileExistsCallback;
@ -1059,14 +1059,10 @@ static u8 SaveDoSaveCallback(void)
saveStatus = TrySavingData(SAVE_NORMAL);
}
if (saveStatus == 1) // Save succeded
{
if (saveStatus == SAVE_STATUS_OK)
ShowSaveMessage(gText_PlayerSavedGame, SaveSuccessCallback);
}
else // Save error
{
else
ShowSaveMessage(gText_SaveError, SaveErrorCallback);
}
SaveStartTimer();
return SAVE_IN_PROGRESS;