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 gGameContinueCallback
gRamSaveSectionLocations gRamSaveSectionLocations
gSaveUnusedVar2 gSaveUnusedVar2
gUnknown_03006294 gSaveAttemptStatus

View File

@ -55,13 +55,25 @@ enum
SAVE_HALL_OF_FAME_ERASE_BEFORE // unused 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 #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_1 28
#define SECTOR_ID_HOF_2 29 #define SECTOR_ID_HOF_2 29
#define SECTOR_ID_TRAINER_HILL 30 #define SECTOR_ID_TRAINER_HILL 30
#define SECTOR_ID_RECORDED_BATTLE 31 #define SECTOR_ID_RECORDED_BATTLE 31
#define SECTORS_COUNT 32 #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 u16 gLastWrittenSector;
extern u32 gLastSaveCounter; extern u32 gLastSaveCounter;
extern u16 gLastKnownGoodSector; extern u16 gLastKnownGoodSector;
@ -82,13 +94,12 @@ u8 HandleSavingData(u8 saveType);
u8 TrySavingData(u8 saveType); u8 TrySavingData(u8 saveType);
bool8 sub_8153380(void); bool8 sub_8153380(void);
bool8 sub_81533AC(void); bool8 sub_81533AC(void);
u8 sub_81533E0(void); bool8 sub_81533E0(void);
u8 sub_8153408(void); bool8 sub_8153408(void);
u8 FullSaveGame(void); bool8 FullSaveGame(void);
bool8 CheckSaveFile(void); bool8 CheckSaveFile(void);
u8 Save_LoadGameData(u8 a1); u8 Save_LoadGameData(u8 saveType);
u16 sub_815355C(void); u16 sub_815355C(void);
u8 sub_81534D0(u8);
u32 TryReadSpecialSaveSection(u8 sector, u8* dst); u32 TryReadSpecialSaveSection(u8 sector, u8* dst);
u32 TryWriteSpecialSaveSection(u8 sector, u8* src); u32 TryWriteSpecialSaveSection(u8 sector, u8* src);
void sub_8153688(u8 taskId); 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)); 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 FALSE;
return TRUE; return TRUE;
@ -487,13 +487,13 @@ bool32 TryWriteTrainerHill(struct EReaderTrainerHillSet *arg0)
return result; 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; return FALSE;
memcpy(arg0, arg1, sizeof(struct EReaderTrainerHillSet)); memcpy(dst, buffer, sizeof(struct EReaderTrainerHillSet));
if (!TrainerHill_VerifyChecksum(arg0)) if (!TrainerHill_VerifyChecksum(dst))
return FALSE; return FALSE;
return TRUE; return TRUE;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -323,7 +323,7 @@ static bool32 RecordedBattleToSave(struct RecordedBattleSave *battleSave, struct
saveSection->checksum = CalcByteArraySum((void*)(saveSection), sizeof(*saveSection) - 4); 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; return FALSE;
else else
return TRUE; return TRUE;
@ -492,7 +492,7 @@ bool32 MoveRecordedBattleToSaveData(void)
static bool32 TryCopyRecordedBattleSaveData(struct RecordedBattleSave *dst, struct SaveSection *saveBuffer) 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; return FALSE;
memcpy(dst, saveBuffer, sizeof(struct RecordedBattleSave)); memcpy(dst, saveBuffer, sizeof(struct RecordedBattleSave));

View File

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

View File

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

View File

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

View File

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