re3/src/save/PCSave.cpp

152 lines
3.9 KiB
C++
Raw Normal View History

2020-05-11 17:10:01 +00:00
#define WITHWINDOWS
2020-01-13 23:13:42 +00:00
#include "common.h"
2020-04-21 10:28:06 +00:00
#include "crossplatform.h"
2020-04-17 13:31:11 +00:00
2020-01-13 23:13:42 +00:00
#include "FileMgr.h"
2020-04-26 20:49:24 +00:00
#ifdef MORE_LANGUAGES
#include "Game.h"
#endif
2020-01-13 23:13:42 +00:00
#include "GenericGameStorage.h"
#include "Messages.h"
#include "PCSave.h"
#include "Text.h"
const char* _psGetUserFilesFolder();
2020-04-17 05:54:14 +00:00
C_PcSave PcSaveHelper;
2020-02-12 23:33:21 +00:00
2020-01-13 23:13:42 +00:00
void
C_PcSave::SetSaveDirectory(const char *path)
{
sprintf(DefaultPCSaveFileName, "%s\\%s", path, "GTA3sf");
}
bool
C_PcSave::DeleteSlot(int32 slot)
{
char FileName[200];
PcSaveHelper.nErrorCode = SAVESTATUS_SUCCESSFUL;
sprintf(FileName, "%s%i.b", DefaultPCSaveFileName, slot + 1);
DeleteFile(FileName);
SlotSaveDate[slot][0] = '\0';
return true;
}
bool
C_PcSave::SaveSlot(int32 slot)
{
MakeValidSaveName(slot);
PcSaveHelper.nErrorCode = SAVESTATUS_SUCCESSFUL;
_psGetUserFilesFolder();
int file = CFileMgr::OpenFile(ValidSaveName, "wb");
if (file != 0) {
DoGameSpecificStuffBeforeSave();
if (GenericSave(file)) {
2020-03-25 14:13:06 +00:00
if (!!CFileMgr::CloseFile(file))
2020-01-13 23:13:42 +00:00
nErrorCode = SAVESTATUS_ERR_SAVE_CLOSE;
return true;
}
return false;
}
PcSaveHelper.nErrorCode = SAVESTATUS_ERR_SAVE_CREATE;
return false;
}
bool
2020-01-27 16:31:20 +00:00
C_PcSave::PcClassSaveRoutine(int32 file, uint8 *data, uint32 size)
2020-01-13 23:13:42 +00:00
{
2020-01-31 08:12:36 +00:00
CFileMgr::Write(file, (const char*)&size, sizeof(size));
2020-01-13 23:13:42 +00:00
if (CFileMgr::GetErrorReadWrite(file)) {
nErrorCode = SAVESTATUS_ERR_SAVE_WRITE;
2020-03-25 14:13:06 +00:00
strncpy(SaveFileNameJustSaved, ValidSaveName, sizeof(ValidSaveName) - 1);
2020-01-13 23:13:42 +00:00
return false;
}
2020-01-27 16:31:20 +00:00
CFileMgr::Write(file, (const char*)data, align4bytes(size));
2020-03-25 14:13:06 +00:00
CheckSum += (uint8) size;
CheckSum += (uint8) (size >> 8);
CheckSum += (uint8) (size >> 16);
CheckSum += (uint8) (size >> 24);
2020-01-27 16:31:20 +00:00
for (int i = 0; i < align4bytes(size); i++) {
CheckSum += *data++;
2020-01-13 23:13:42 +00:00
}
if (CFileMgr::GetErrorReadWrite(file)) {
nErrorCode = SAVESTATUS_ERR_SAVE_WRITE;
2020-03-25 14:13:06 +00:00
strncpy(SaveFileNameJustSaved, ValidSaveName, sizeof(ValidSaveName) - 1);
2020-01-13 23:13:42 +00:00
return false;
}
return true;
}
void
C_PcSave::PopulateSlotInfo()
{
for (int i = 0; i < SLOT_COUNT; i++) {
Slots[i + 1] = SLOT_EMPTY;
SlotFileName[i][0] = '\0';
SlotSaveDate[i][0] = '\0';
}
for (int i = 0; i < SLOT_COUNT; i++) {
2020-04-18 08:31:53 +00:00
#ifdef FIX_BUGS
char savename[MAX_PATH];
#else
2020-01-13 23:13:42 +00:00
char savename[52];
2020-04-18 08:31:53 +00:00
#endif
2020-01-31 08:12:36 +00:00
struct {
int size;
wchar FileName[24];
2020-04-21 10:28:06 +00:00
SYSTEMTIME SaveDateTime;
2020-01-31 08:12:36 +00:00
} header;
2020-01-13 23:13:42 +00:00
sprintf(savename, "%s%i%s", DefaultPCSaveFileName, i + 1, ".b");
int file = CFileMgr::OpenFile(savename, "rb");
if (file != 0) {
2020-01-31 08:12:36 +00:00
CFileMgr::Read(file, (char*)&header, sizeof(header));
if (strncmp((char*)&header, TopLineEmptyFile, sizeof(TopLineEmptyFile)-1) != 0) {
2020-01-13 23:13:42 +00:00
Slots[i + 1] = SLOT_OK;
2020-01-31 08:12:36 +00:00
memcpy(SlotFileName[i], &header.FileName, sizeof(header.FileName));
2020-01-13 23:13:42 +00:00
SlotFileName[i][24] = '\0';
}
CFileMgr::CloseFile(file);
}
if (Slots[i + 1] == SLOT_OK) {
if (CheckDataNotCorrupt(i, savename)) {
2020-04-21 10:28:06 +00:00
SYSTEMTIME st;
memcpy(&st, &header.SaveDateTime, sizeof(SYSTEMTIME));
2020-01-13 23:13:42 +00:00
const char *month;
2020-01-31 08:12:36 +00:00
switch (st.wMonth)
2020-01-13 23:13:42 +00:00
{
case 1: month = "JAN"; break;
case 2: month = "FEB"; break;
case 3: month = "MAR"; break;
case 4: month = "APR"; break;
case 5: month = "MAY"; break;
case 6: month = "JUN"; break;
case 7: month = "JUL"; break;
case 8: month = "AUG"; break;
case 9: month = "SEP"; break;
case 10: month = "OCT"; break;
case 11: month = "NOV"; break;
case 12: month = "DEC"; break;
default: assert(0);
}
char date[70];
2020-04-26 20:49:24 +00:00
#ifdef MORE_LANGUAGES
if (CGame::japaneseGame)
sprintf(date, "%02d %02d %04d %02d:%02d:%02d", st.wDay, st.wMonth, st.wYear, st.wHour, st.wMinute, st.wSecond);
else
#endif // MORE_LANGUAGES
sprintf(date, "%02d %s %04d %02d:%02d:%02d", st.wDay, UnicodeToAsciiForSaveLoad(TheText.Get(month)), st.wYear, st.wHour, st.wMinute, st.wSecond);
2020-01-13 23:13:42 +00:00
AsciiToUnicode(date, SlotSaveDate[i]);
} else {
CMessages::InsertNumberInString(TheText.Get("FEC_SLC"), i + 1, -1, -1, -1, -1, -1, SlotFileName[i]);
Slots[i + 1] = SLOT_CORRUPTED;
}
}
}
}