summaryrefslogtreecommitdiffstats
path: root/src/save
diff options
context:
space:
mode:
Diffstat (limited to 'src/save')
-rw-r--r--src/save/GenericGameStorage.cpp348
-rw-r--r--src/save/GenericGameStorage.h55
-rw-r--r--src/save/PCSave.cpp138
-rw-r--r--src/save/PCSave.h38
4 files changed, 579 insertions, 0 deletions
diff --git a/src/save/GenericGameStorage.cpp b/src/save/GenericGameStorage.cpp
new file mode 100644
index 00000000..302b050d
--- /dev/null
+++ b/src/save/GenericGameStorage.cpp
@@ -0,0 +1,348 @@
+#include "common.h"
+#include "main.h"
+#include "patcher.h"
+#include "Camera.h"
+#include "Clock.h"
+#include "FileMgr.h"
+#include "GameLogic.h"
+#include "Garages.h"
+#include "GenericGameStorage.h"
+#include "PCSave.h"
+#include "PlayerPed.h"
+#include "Pools.h"
+#include "Script.h"
+#include "Streaming.h"
+#include "World.h"
+
+
+char (&DefaultPCSaveFileName)[260] = *(char(*)[260])*(uintptr*)0x8E28C0;
+char (&ValidSaveName)[260] = *(char(*)[260])*(uintptr*)0x8E2CBC;
+char (&LoadFileName)[256] = *(char(*)[256])*(uintptr*)0x9403C4;
+wchar (&SlotFileName)[SLOT_COUNT][260] = *(wchar(*)[SLOT_COUNT][260])*(uintptr*)0x6F07C8;
+wchar (&SlotSaveDate)[SLOT_COUNT][70] = *(wchar(*)[SLOT_COUNT][70])*(uintptr*)0x72B858;
+int &CheckSum = *(int*)0x8E2BE0;
+eLevelName &m_LevelToLoad = *(eLevelName*)0x8E29CC;
+char SaveFileNameJustSaved[260];
+int (&Slots)[SLOT_COUNT+1] = *(int(*)[SLOT_COUNT+1])*(uintptr*)0x72803C;
+CDate &CompileDateAndTime = *(CDate*)0x72BCB8;
+
+C_PcSave &PcSaveHelper = *(C_PcSave*)0x8E2C60;
+
+CDate::CDate()
+{
+ m_nYear = 0;
+ m_nSecond = 0;
+ m_nMinute = 0;
+ m_nHour = 0;
+ m_nDay = 0;
+ m_nMonth = 0;
+}
+
+bool
+CDate::operator>(const CDate &right)
+{
+ if (m_nYear > right.m_nYear)
+ return true;
+ if (m_nYear != right.m_nYear)
+ return false;
+
+ if (m_nMonth > right.m_nMonth)
+ return true;
+ if (m_nMonth != right.m_nMonth)
+ return false;
+
+ if (m_nDay > right.m_nDay)
+ return true;
+ if (m_nDay != right.m_nDay)
+ return false;
+
+ if (m_nHour > right.m_nHour)
+ return true;
+ if (m_nHour != right.m_nHour)
+ return false;
+
+ if (m_nMinute > right.m_nMinute)
+ return true;
+ if (m_nMinute != right.m_nMinute)
+ return false;
+ return m_nSecond > right.m_nSecond;
+}
+
+bool
+CDate::operator<(const CDate &right)
+{
+ if (m_nYear < right.m_nYear)
+ return true;
+ if (m_nYear != right.m_nYear)
+ return false;
+
+ if (m_nMonth < right.m_nMonth)
+ return true;
+ if (m_nMonth != right.m_nMonth)
+ return false;
+
+ if (m_nDay < right.m_nDay)
+ return true;
+ if (m_nDay != right.m_nDay)
+ return false;
+
+ if (m_nHour < right.m_nHour)
+ return true;
+ if (m_nHour != right.m_nHour)
+ return false;
+
+ if (m_nMinute < right.m_nMinute)
+ return true;
+ if (m_nMinute != right.m_nMinute)
+ return false;
+ return m_nSecond < right.m_nSecond;
+}
+
+bool
+CDate::operator==(const CDate &right)
+{
+ if (m_nYear != right.m_nYear || m_nMonth != right.m_nMonth || m_nDay != right.m_nDay || m_nHour != right.m_nHour || m_nMinute != right.m_nMinute)
+ return false;
+ return m_nSecond == right.m_nSecond;
+}
+
+void
+CDate::PopulateDateFields(int8 &second, int8 &minute, int8 &hour, int8 &day, int8 &month, int16 year)
+{
+ m_nSecond = second;
+ m_nMinute = minute;
+ m_nHour = hour;
+ m_nDay = day;
+ m_nMonth = month;
+ m_nYear = year;
+}
+
+WRAPPER bool GenericSave(int file) { EAXJMP(0x58F8D0); }
+WRAPPER bool GenericLoad() { EAXJMP(0x590A00); }
+
+bool
+ReadInSizeofSaveFileBuffer(int32 &file, uint32 &size)
+{
+ file = CFileMgr::OpenFile(LoadFileName, "rb");
+ if (file == 0) {
+ PcSaveHelper.nErrorCode = SAVESTATUS_ERR_LOAD_OPEN;
+ return false;
+ }
+ CFileMgr::Read(file, (const char*)&size, 4);
+ if (CFileMgr::GetErrorReadWrite(file)) {
+ PcSaveHelper.nErrorCode = SAVESTATUS_ERR_LOAD_READ;
+ if (!CloseFile(file))
+ PcSaveHelper.nErrorCode = SAVESTATUS_ERR_LOAD_CLOSE;
+ return false;
+ }
+ return true;
+}
+
+bool
+ReadDataFromFile(int32 file, uint8 *buf, uint32 size)
+{
+ if (file == 0) {
+ PcSaveHelper.nErrorCode = SAVESTATUS_ERR_LOAD_OPEN;
+ return false;
+ }
+ size_t read_size = CFileMgr::Read(file, (const char*)buf, size);
+ if (CFileMgr::GetErrorReadWrite(file) || read_size != size) {
+ PcSaveHelper.nErrorCode = SAVESTATUS_ERR_LOAD_READ;
+ if (!CloseFile(file))
+ PcSaveHelper.nErrorCode = SAVESTATUS_ERR_LOAD_CLOSE;
+ return false;
+ }
+ return true;
+}
+
+bool
+CloseFile(int32 file)
+{
+ return CFileMgr::CloseFile(file) == 0;
+}
+
+void
+DoGameSpecificStuffAfterSucessLoad()
+{
+ StillToFadeOut = true;
+ JustLoadedDontFadeInYet = true;
+ CTheScripts::Process();
+}
+
+bool
+CheckSlotDataValid(int32 slot)
+{
+ PcSaveHelper.nErrorCode = SAVESTATUS_SUCCESSFUL;
+ if (CheckDataNotCorrupt(slot, LoadFileName)) {
+ CStreaming::DeleteAllRwObjects();
+ return true;
+ }
+
+ PcSaveHelper.nErrorCode = SAVESTATUS_ERR_DATA_INVALID;
+ return false;
+}
+
+void
+MakeSpaceForSizeInBufferPointer(uint8 *&presize, uint8 *&buf, uint8 *&postsize)
+{
+ presize = buf;
+ buf += 4;
+ postsize = buf;
+}
+
+void
+CopySizeAndPreparePointer(uint8 *&buf, uint8 *&postbuf, uint8 *&postbuf2, uint32 &unused, uint32 &size)
+{
+ *(uint32*)buf = size;
+ size = align4bytes(size);
+ postbuf2 += size;
+ postbuf = postbuf2;
+}
+
+void
+DoGameSpecificStuffBeforeSave()
+{
+ CGameLogic::PassTime(360);
+ CPlayerPed *ped = FindPlayerPed();
+ ped->m_fCurrentStamina = ped->m_fMaxStamina;
+ CGame::TidyUpMemory(true, false);
+}
+
+
+void
+MakeValidSaveName(int32 slot)
+{
+ ValidSaveName[0] = '\0';
+ sprintf(ValidSaveName, "%s%i", DefaultPCSaveFileName, slot + 1);
+ strncat(ValidSaveName, ".b", 5);
+}
+
+wchar *
+GetSavedGameDateAndTime(int32 slot)
+{
+ return SlotSaveDate[slot];
+}
+
+wchar *
+GetNameOfSavedGame(int32 slot)
+{
+ return SlotFileName[slot];
+}
+
+bool
+CheckDataNotCorrupt(int32 slot, char *name)
+{
+ char filename[100];
+
+ int32 blocknum = 0;
+ eLevelName level = LEVEL_NONE;
+ CheckSum = 0;
+ uint32 bytes_pocessed = 0;
+ sprintf(filename, "%s%i%s", DefaultPCSaveFileName, slot + 1, ".b");
+ int file = CFileMgr::OpenFile(filename, "rb");
+ if (file == 0)
+ return false;
+ strcpy(name, filename);
+ while (201729 - bytes_pocessed > 4 && blocknum < 40) {
+ int32 blocksize;
+ if (!ReadDataFromFile(file, (uint8*)&blocksize, 4)) {
+ CloseFile(file);
+ return false;
+ }
+ if (blocksize > align4bytes(sizeof(work_buff)))
+ blocksize = sizeof(work_buff) - 4;
+ if (!ReadDataFromFile(file, work_buff, align4bytes(blocksize))) {
+ CloseFile(file);
+ return false;
+ }
+
+ CheckSum += ((uint8*)&blocksize)[0];
+ CheckSum += ((uint8*)&blocksize)[1];
+ CheckSum += ((uint8*)&blocksize)[2];
+ CheckSum += ((uint8*)&blocksize)[3];
+ uint8 *_work_buf = work_buff;
+ for (int i = 0; i < align4bytes(blocksize); i++) {
+ CheckSum += *_work_buf++;
+ bytes_pocessed++;
+ }
+
+ if (blocknum == 0)
+ level = *(eLevelName*)&work_buff[4];
+ blocknum++;
+ }
+ int32 _checkSum;
+ if (ReadDataFromFile(file, (uint8*)&_checkSum, 4)) {
+ if (CloseFile(file)) {
+ if (CheckSum == _checkSum) {
+ m_LevelToLoad = level;
+ return true;
+ }
+ return false;
+ }
+ return false;
+ }
+
+ CloseFile(file);
+ return false;
+}
+
+bool
+RestoreForStartLoad()
+{
+ uint8 buf[999];
+
+ int file = CFileMgr::OpenFile(LoadFileName, "rb");
+ if (file == 0) {
+ PcSaveHelper.nErrorCode = SAVESTATUS_ERR_LOAD_OPEN;
+ return false;
+ }
+ ReadDataFromFile(file, buf, 999);
+ if (CFileMgr::GetErrorReadWrite(file)) {
+ PcSaveHelper.nErrorCode = SAVESTATUS_ERR_LOAD_READ;
+ if (!CloseFile(file))
+ PcSaveHelper.nErrorCode = SAVESTATUS_ERR_LOAD_CLOSE;
+ return false;
+ } else {
+ CGame::currLevel = *(eLevelName*)&buf[72];
+ TheCamera.GetPosition() = *(CVector*)&buf[76];
+ CStreaming::RemoveUnusedBigBuildings(*(eLevelName*)&buf[72]);
+ CStreaming::RemoveUnusedBuildings(CGame::currLevel);
+ CCollision::SortOutCollisionAfterLoad();
+ CStreaming::RequestBigBuildings(CGame::currLevel);
+ CStreaming::LoadAllRequestedModels(false);
+ CStreaming::HaveAllBigBuildingsLoaded(CGame::currLevel);
+ CGame::TidyUpMemory(true, false);
+
+ if (CloseFile(file)) {
+ return true;
+ } else {
+ PcSaveHelper.nErrorCode = SAVESTATUS_ERR_LOAD_CLOSE;
+ return false;
+ }
+ }
+}
+
+int
+align4bytes(int32 size)
+{
+ return (size + 3) & 0xFFFFFFFC;
+}
+
+STARTPATCHES
+ //InjectHook(0x58F8D0, GenericSave, PATCH_JUMP);
+ //InjectHook(0x590A00, GenericLoad, PATCH_JUMP);
+ InjectHook(0x591910, ReadInSizeofSaveFileBuffer, PATCH_JUMP);
+ InjectHook(0x591990, ReadDataFromFile, PATCH_JUMP);
+ InjectHook(0x591A00, CloseFile, PATCH_JUMP);
+ InjectHook(0x591A20, DoGameSpecificStuffAfterSucessLoad, PATCH_JUMP);
+ InjectHook(0x591A40, CheckSlotDataValid, PATCH_JUMP);
+ InjectHook(0x591A80, MakeSpaceForSizeInBufferPointer, PATCH_JUMP);
+ InjectHook(0x591AA0, CopySizeAndPreparePointer, PATCH_JUMP);
+ InjectHook(0x591AE0, DoGameSpecificStuffBeforeSave, PATCH_JUMP);
+ InjectHook(0x591B10, MakeValidSaveName, PATCH_JUMP);
+ InjectHook(0x591B50, GetSavedGameDateAndTime, PATCH_JUMP);
+ InjectHook(0x591B60, GetNameOfSavedGame, PATCH_JUMP);
+ InjectHook(0x591B70, CheckDataNotCorrupt, PATCH_JUMP);
+ InjectHook(0x591D60, RestoreForStartLoad, PATCH_JUMP);
+ InjectHook(0x591E80, align4bytes, PATCH_JUMP);
+ENDPATCHES \ No newline at end of file
diff --git a/src/save/GenericGameStorage.h b/src/save/GenericGameStorage.h
new file mode 100644
index 00000000..86861e5e
--- /dev/null
+++ b/src/save/GenericGameStorage.h
@@ -0,0 +1,55 @@
+#pragma once
+
+#include "PCSave.h"
+
+class CDate
+{
+public:
+ int m_nSecond;
+ int m_nMinute;
+ int m_nHour;
+ int m_nDay;
+ int m_nMonth;
+ int m_nYear;
+
+ CDate();
+ bool operator>(const CDate &right);
+ bool operator<(const CDate &right);
+ bool operator==(const CDate &right);
+ void PopulateDateFields(int8 &second, int8 &minute, int8 &hour, int8 &day, int8 &month, int16 year);
+};
+
+#define SLOT_COUNT (8)
+
+bool GenericSave(int file);
+bool GenericLoad();
+bool ReadInSizeofSaveFileBuffer(int32 &file, uint32 &size);
+bool ReadDataFromFile(int32 file, uint8 *buf, uint32 size);
+bool CloseFile(int32 file);
+void DoGameSpecificStuffAfterSucessLoad();
+bool CheckSlotDataValid(int32 slot);
+void MakeSpaceForSizeInBufferPointer(uint8 *&presize, uint8 *&buf, uint8 *&postsize);
+void CopySizeAndPreparePointer(uint8 *&buf, uint8 *&postbuf, uint8 *&postbuf2, uint32 &unused, uint32 &size);
+void DoGameSpecificStuffBeforeSave();
+void MakeValidSaveName(int32 slot);
+wchar *GetSavedGameDateAndTime(int32 slot);
+wchar *GetNameOfSavedGame(int32 slot);
+bool CheckDataNotCorrupt(int32 slot, char *name);
+bool RestoreForStartLoad();
+int align4bytes(int32 size);
+
+extern CDate& CompileDateAndTime;
+
+extern char (&DefaultPCSaveFileName)[260];
+extern char (&ValidSaveName)[260];
+extern char (&LoadFileName)[256];
+extern wchar (&SlotFileName)[SLOT_COUNT][260];
+extern wchar (&SlotSaveDate)[SLOT_COUNT][70];
+extern int &CheckSum;
+extern enum eLevelName &m_LevelToLoad;
+extern int (&Slots)[SLOT_COUNT+1];
+
+extern char SaveFileNameJustSaved[260]; // 8F2570
+
+const char TopLineEmptyFile[] = "THIS FILE IS NOT VALID YET";
+extern C_PcSave &PcSaveHelper; \ No newline at end of file
diff --git a/src/save/PCSave.cpp b/src/save/PCSave.cpp
new file mode 100644
index 00000000..6990ad61
--- /dev/null
+++ b/src/save/PCSave.cpp
@@ -0,0 +1,138 @@
+#include "common.h"
+#include "patcher.h"
+#include "FileMgr.h"
+#include "GenericGameStorage.h"
+#include "Messages.h"
+#include "PCSave.h"
+#include "Text.h"
+
+const char* _psGetUserFilesFolder();
+
+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)) {
+ if (CFileMgr::CloseFile(file) != 0)
+ nErrorCode = SAVESTATUS_ERR_SAVE_CLOSE;
+ return true;
+ }
+
+ return false;
+ }
+ PcSaveHelper.nErrorCode = SAVESTATUS_ERR_SAVE_CREATE;
+ return false;
+}
+
+bool
+C_PcSave::PcClassSaveRoutine(int32 file, uint8 *a3, uint32 a4)
+{
+ CFileMgr::Write(file, (const char*)&a4, 4);
+ if (CFileMgr::GetErrorReadWrite(file)) {
+ nErrorCode = SAVESTATUS_ERR_SAVE_WRITE;
+ strncpy(SaveFileNameJustSaved, ValidSaveName, 259);
+ return false;
+ }
+
+ CFileMgr::Write(file, (const char*)a3, align4bytes(a4));
+ CheckSum += ((uint8*)&a4)[0];
+ CheckSum += ((uint8*)&a4)[1];
+ CheckSum += ((uint8*)&a4)[2];
+ CheckSum += ((uint8*)&a4)[3];
+ for (int i = 0; i < align4bytes(a4); i++) {
+ CheckSum += *a3++;
+ }
+ if (CFileMgr::GetErrorReadWrite(file)) {
+ nErrorCode = SAVESTATUS_ERR_SAVE_WRITE;
+ strncpy(SaveFileNameJustSaved, ValidSaveName, 259);
+ 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++) {
+ char savename[52];
+ char v13[68];
+ sprintf(savename, "%s%i%s", DefaultPCSaveFileName, i + 1, ".b");
+ int file = CFileMgr::OpenFile(savename, "rb");
+ if (file != 0) {
+ CFileMgr::Read(file, v13, 68);
+ if (strncmp(v13, TopLineEmptyFile, sizeof(TopLineEmptyFile)-1)) {
+ Slots[i + 1] = SLOT_OK;
+ memcpy(SlotFileName[i], &v13[4], 24 * sizeof(wchar));
+
+ SlotFileName[i][24] = '\0';
+ }
+ CFileMgr::CloseFile(file);
+ }
+ if (Slots[i + 1] == SLOT_OK) {
+ if (CheckDataNotCorrupt(i, savename)) {
+ _SYSTEMTIME st = *(_SYSTEMTIME*)&v13[52];
+ const char *month;
+ switch (*(uint16*)&v13[54])
+ {
+ 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];
+ sprintf(date, "%02d %s %04d %02d:%02d:%02d", st.wDay, UnicodeToAsciiForSaveLoad(TheText.Get(month)), st.wYear, st.wHour, st.wMinute, st.wSecond);
+ 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;
+ }
+ }
+ }
+}
+
+STARTPATCHES
+ InjectHook(0x591EA0, C_PcSave::SetSaveDirectory, PATCH_JUMP);
+ InjectHook(0x5922F0, &C_PcSave::DeleteSlot, PATCH_JUMP);
+ InjectHook(0x591EC0, &C_PcSave::SaveSlot, PATCH_JUMP);
+ InjectHook(0x591F80, &C_PcSave::PcClassSaveRoutine, PATCH_JUMP);
+ InjectHook(0x592090, &C_PcSave::PopulateSlotInfo, PATCH_JUMP);
+ENDPATCHES \ No newline at end of file
diff --git a/src/save/PCSave.h b/src/save/PCSave.h
new file mode 100644
index 00000000..bc64adff
--- /dev/null
+++ b/src/save/PCSave.h
@@ -0,0 +1,38 @@
+#pragma once
+
+enum eSaveStatus
+{
+ SAVESTATUS_SUCCESSFUL = 0,
+ SAVESTATUS_ERR_SAVE_CREATE,
+ SAVESTATUS_ERR_SAVE_WRITE,
+ SAVESTATUS_ERR_SAVE_CLOSE,
+ SAVESTATUS_ERR_LOAD_OPEN,
+ SAVESTATUS_ERR_LOAD_READ,
+ SAVESTATUS_ERR_LOAD_CLOSE,
+ SAVESTATUS_ERR_DATA_INVALID,
+
+ // unused
+ SAVESTATUS_DELETEFAILED8,
+ SAVESTATUS_DELETEFAILED9,
+ SAVESTATUS_DELETEFAILED10,
+};
+
+enum
+{
+ SLOT_OK = 0,
+ SLOT_EMPTY,
+ SLOT_CORRUPTED
+};
+
+class C_PcSave
+{
+public:
+ eSaveStatus nErrorCode;
+
+ C_PcSave() : nErrorCode(SAVESTATUS_SUCCESSFUL) {}
+ void PopulateSlotInfo();
+ bool DeleteSlot(int32 slot);
+ bool SaveSlot(int32 slot);
+ bool PcClassSaveRoutine(int32 a2, uint8 *a3, uint32 a4);
+ static void SetSaveDirectory(const char *path);
+};