diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/control/CarCtrl.cpp | 4 | ||||
-rw-r--r-- | src/control/Remote.cpp | 54 | ||||
-rw-r--r-- | src/control/Script.cpp | 10 | ||||
-rw-r--r-- | src/core/AnimViewer.cpp | 2 | ||||
-rw-r--r-- | src/core/Camera.h | 14 | ||||
-rw-r--r-- | src/core/ControllerConfig.cpp | 2 | ||||
-rw-r--r-- | src/core/CutsceneMgr.cpp | 2 | ||||
-rw-r--r-- | src/core/Frontend.cpp | 31 | ||||
-rw-r--r-- | src/core/PCSave.cpp | 20 | ||||
-rw-r--r-- | src/core/PCSave.h | 23 | ||||
-rw-r--r-- | src/core/main.h | 2 | ||||
-rw-r--r-- | src/save/Date.cpp | 91 | ||||
-rw-r--r-- | src/save/Date.h | 18 | ||||
-rw-r--r-- | src/save/GenericGameStorage.cpp | 267 | ||||
-rw-r--r-- | src/save/GenericGameStorage.h | 38 | ||||
-rw-r--r-- | src/save/PCSave.cpp | 143 | ||||
-rw-r--r-- | src/save/PCSave.h | 38 |
17 files changed, 687 insertions, 72 deletions
diff --git a/src/control/CarCtrl.cpp b/src/control/CarCtrl.cpp index cd657815..fe727f72 100644 --- a/src/control/CarCtrl.cpp +++ b/src/control/CarCtrl.cpp @@ -2541,7 +2541,7 @@ bool CCarCtrl::JoinCarWithRoadSystemGotoCoors(CVehicle* pVehicle, CVector vecTar pVehicle->AutoPilot.m_aPathFindNodesInfo, &pVehicle->AutoPilot.m_nPathFindNodesCount); if (pVehicle->AutoPilot.m_nPathFindNodesCount < 2){ pVehicle->AutoPilot.m_nPrevRouteNode = pVehicle->AutoPilot.m_nCurrentRouteNode = pVehicle->AutoPilot.m_nNextRouteNode = 0; - return 1; + return true; } pVehicle->AutoPilot.m_nPrevRouteNode = 0; pVehicle->AutoPilot.m_nCurrentRouteNode = pVehicle->AutoPilot.m_aPathFindNodesInfo[0] - ThePaths.m_pathNodes; @@ -2550,7 +2550,7 @@ bool CCarCtrl::JoinCarWithRoadSystemGotoCoors(CVehicle* pVehicle, CVector vecTar pVehicle->AutoPilot.RemoveOnePathNode(); FindLinksToGoWithTheseNodes(pVehicle); pVehicle->AutoPilot.m_nNextLane = pVehicle->AutoPilot.m_nCurrentLane = 0; - return 0; + return false; } void CCarCtrl::FindLinksToGoWithTheseNodes(CVehicle* pVehicle) diff --git a/src/control/Remote.cpp b/src/control/Remote.cpp index 32ee4eda..e3891502 100644 --- a/src/control/Remote.cpp +++ b/src/control/Remote.cpp @@ -1,6 +1,56 @@ #include "common.h" #include "patcher.h" +#include "Automobile.h" +#include "CarCtrl.h" +#include "Camera.h" #include "Remote.h" +#include "Timer.h" +#include "World.h" +#include "PlayerInfo.h" +#include "Vehicle.h" -WRAPPER void CRemote::GivePlayerRemoteControlledCar(float, float, float, float, uint16) { EAXJMP(0x435C30); } -WRAPPER void CRemote::TakeRemoteControlledCarFromPlayer(void) { EAXJMP(0x435DA0); } +void +CRemote::GivePlayerRemoteControlledCar(float x, float y, float z, float rot, uint16 model) +{ + CAutomobile *car = new CAutomobile(model, MISSION_VEHICLE); + bool found; + + z = car->GetDistanceFromCentreOfMassToBaseOfModel() + CWorld::FindGroundZFor3DCoord(x, y, z + 2.0f, &found); + + car->GetMatrix().SetRotateZOnly(rot); + car->GetPosition() = CVector(x, y, z); + car->m_status = STATUS_PLAYER_REMOTE; + car->bIsLocked = true; + + CCarCtrl::JoinCarWithRoadSystem(car); + car->AutoPilot.m_nCarMission = MISSION_NONE; + car->AutoPilot.m_nTempAction = TEMPACT_NONE; + car->AutoPilot.m_nDrivingStyle = DRIVINGSTYLE_STOP_FOR_CARS; + car->AutoPilot.m_nCruiseSpeed = car->AutoPilot.m_fMaxTrafficSpeed = 9.0f; + car->AutoPilot.m_nNextLane = car->AutoPilot.m_nCurrentLane = 0; + car->bEngineOn = true; + CWorld::Add(car); + if (FindPlayerVehicle() != nil) + FindPlayerVehicle()->m_status = STATUS_PLAYER_DISABLED; + + CWorld::Players[CWorld::PlayerInFocus].m_pRemoteVehicle = car; + CWorld::Players[CWorld::PlayerInFocus].m_pRemoteVehicle->RegisterReference((CEntity**)&CWorld::Players[CWorld::PlayerInFocus].m_pRemoteVehicle); + TheCamera.TakeControl(car, CCam::MODE_BEHINDCAR, INTERPOLATION, CAM_CONTROLLER_1); +} + +void +CRemote::TakeRemoteControlledCarFromPlayer(void) +{ + CWorld::Players[CWorld::PlayerInFocus].m_pRemoteVehicle->VehicleCreatedBy = RANDOM_VEHICLE; + CCarCtrl::NumMissionCars--; + CCarCtrl::NumRandomCars++; + CWorld::Players[CWorld::PlayerInFocus].m_pRemoteVehicle->bIsLocked = false; + CWorld::Players[CWorld::PlayerInFocus].m_nTimeLostRemoteCar = CTimer::GetTimeInMilliseconds(); + CWorld::Players[CWorld::PlayerInFocus].m_bInRemoteMode = true; + CWorld::Players[CWorld::PlayerInFocus].m_pRemoteVehicle->bRemoveFromWorld = true; +} + +STARTPATCHES + InjectHook(0x435C30, &CRemote::GivePlayerRemoteControlledCar, PATCH_JUMP); + InjectHook(0x435DA0, &CRemote::TakeRemoteControlledCarFromPlayer, PATCH_JUMP); +ENDPATCHES
\ No newline at end of file diff --git a/src/control/Script.cpp b/src/control/Script.cpp index f74c5656..2b3fdbc0 100644 --- a/src/control/Script.cpp +++ b/src/control/Script.cpp @@ -3081,7 +3081,7 @@ int8 CRunningScript::ProcessCommandsFrom300To399(int32 command) { CollectParameters(&m_nIp, 3); // ScriptParams[0] is unused. - TheCamera.TakeControl(nil, ScriptParams[1], ScriptParams[2], 1); + TheCamera.TakeControl(nil, ScriptParams[1], ScriptParams[2], CAM_CONTROLLER_1); return 0; } case COMMAND_POINT_CAMERA_AT_CAR: @@ -3089,7 +3089,7 @@ int8 CRunningScript::ProcessCommandsFrom300To399(int32 command) CollectParameters(&m_nIp, 3); CVehicle* pVehicle = CPools::GetVehiclePool()->GetAt(ScriptParams[0]); assert(pVehicle); - TheCamera.TakeControl(pVehicle, ScriptParams[1], ScriptParams[2], 1); + TheCamera.TakeControl(pVehicle, ScriptParams[1], ScriptParams[2], CAM_CONTROLLER_1); return 0; } case COMMAND_POINT_CAMERA_AT_CHAR: @@ -3097,7 +3097,7 @@ int8 CRunningScript::ProcessCommandsFrom300To399(int32 command) CollectParameters(&m_nIp, 3); CPed* pPed = CPools::GetPedPool()->GetAt(ScriptParams[0]); assert(pPed); - TheCamera.TakeControl(pPed, ScriptParams[1], ScriptParams[2], 1); + TheCamera.TakeControl(pPed, ScriptParams[1], ScriptParams[2], CAM_CONTROLLER_1); return 0; } case COMMAND_RESTORE_CAMERA: @@ -3148,7 +3148,7 @@ int8 CRunningScript::ProcessCommandsFrom300To399(int32 command) CVector pos = *(CVector*)&ScriptParams[0]; if (pos.z <= MAP_Z_LOW_LIMIT) pos.z = CWorld::FindGroundZForCoord(pos.x, pos.y); - TheCamera.TakeControlNoEntity(pos, ScriptParams[3], 1); + TheCamera.TakeControlNoEntity(pos, ScriptParams[3], CAM_CONTROLLER_1); return 0; } case COMMAND_ADD_BLIP_FOR_CAR_OLD: @@ -5046,7 +5046,7 @@ int8 CRunningScript::ProcessCommandsFrom500To599(int32 command) return 0; } case COMMAND_IS_PLAYER_IN_REMOTE_MODE: - CollectParameters(&m_nIp, 2); + CollectParameters(&m_nIp, 1); UpdateCompareFlag(CWorld::Players[ScriptParams[0]].IsPlayerInRemoteMode()); return 0; case COMMAND_ARM_CAR_WITH_BOMB: diff --git a/src/core/AnimViewer.cpp b/src/core/AnimViewer.cpp index 86b22ec5..a2d7b94a 100644 --- a/src/core/AnimViewer.cpp +++ b/src/core/AnimViewer.cpp @@ -289,7 +289,7 @@ CAnimViewer::Update(void) } newEntity->GetPosition() = CVector(0.0f, 0.0f, 0.0f); CWorld::Add(newEntity); - TheCamera.TakeControl(pTarget, 9, 2, 1); + TheCamera.TakeControl(pTarget, CCam::MODE_MODELVIEW, JUMP_CUT, CAM_CONTROLLER_1); } if (pTarget->m_type == ENTITY_TYPE_VEHICLE || pTarget->m_type == ENTITY_TYPE_PED || pTarget->m_type == ENTITY_TYPE_OBJECT) { ((CPhysical*)pTarget)->m_vecMoveSpeed = CVector(0.0f, 0.0f, 0.0f); diff --git a/src/core/Camera.h b/src/core/Camera.h index 16d2aaf3..3e67903f 100644 --- a/src/core/Camera.h +++ b/src/core/Camera.h @@ -239,6 +239,20 @@ enum MBLUR_UNUSED, // pinkish }; +enum +{ + NONE = 0, + INTERPOLATION, + JUMP_CUT +}; + +enum +{ + CAM_CONTROLLER_0, + CAM_CONTROLLER_1, + CAM_CONTROLLER_2 +}; + struct CCamera : public CPlaceable { bool m_bAboveGroundTrainNodesLoaded; diff --git a/src/core/ControllerConfig.cpp b/src/core/ControllerConfig.cpp index 26db4e5e..02230df7 100644 --- a/src/core/ControllerConfig.cpp +++ b/src/core/ControllerConfig.cpp @@ -16,7 +16,7 @@ #include "ModelIndices.h" #include "Camera.h" #include "win.h" -#include "PCSave.h" +#include "GenericGameStorage.h" CControllerConfigManager &ControlsManager = *(CControllerConfigManager*)0x8F43A4; diff --git a/src/core/CutsceneMgr.cpp b/src/core/CutsceneMgr.cpp index 2fbc5186..3df81b2b 100644 --- a/src/core/CutsceneMgr.cpp +++ b/src/core/CutsceneMgr.cpp @@ -256,7 +256,7 @@ void CCutsceneMgr::SetupCutsceneToStart(void) { TheCamera.SetCamCutSceneOffSet(ms_cutsceneOffset);
- TheCamera.TakeControlWithSpline(2);
+ TheCamera.TakeControlWithSpline(JUMP_CUT);
TheCamera.SetWideScreenOn(); ms_cutsceneOffset.z++; diff --git a/src/core/Frontend.cpp b/src/core/Frontend.cpp index ed424b98..a75c464b 100644 --- a/src/core/Frontend.cpp +++ b/src/core/Frontend.cpp @@ -15,7 +15,7 @@ #include "Streaming.h" #include "TxdStore.h" #include "General.h" -#include "PCSave.h" +#include "GenericGameStorage.h" #include "Script.h" #include "Camera.h" #include "MenuScreens.h" @@ -448,7 +448,7 @@ void CMenuManager::Draw() str = TheText.Get(aScreens[m_nCurrScreen].m_aEntries[0].m_EntryName); break; case MENUPAGE_SAVE_OVERWRITE_CONFIRM: - if (Slots[m_nCurrSaveSlot] == 1) + if (Slots[m_nCurrSaveSlot] == SLOT_EMPTY) str = TheText.Get("FESZ_QZ"); else str = TheText.Get(aScreens[m_nCurrScreen].m_aEntries[0].m_EntryName); @@ -588,7 +588,7 @@ void CMenuManager::Draw() CFont::SetRightJustifyOff(); textToPrint[MENUCOLUMN_LEFT] = GetNameOfSavedGame(i - 1); - if (Slots[i-1] != 1) + if (Slots[i-1] != SLOT_EMPTY) textToPrint[MENUCOLUMN_RIGHT] = GetSavedGameDateAndTime(i - 1); if (textToPrint[MENUCOLUMN_LEFT][0] == '\0') { @@ -2228,40 +2228,37 @@ void CMenuManager::ResetHelperText() void CMenuManager::SaveLoadFileError_SetUpErrorScreen() { - // TO-DO: Enum - switch (PcSaveHelper.m_nHelper) { - case 1: - case 2: - case 3: + switch (PcSaveHelper.nErrorCode) { + case SAVESTATUS_ERR_SAVE_CREATE: + case SAVESTATUS_ERR_SAVE_WRITE: + case SAVESTATUS_ERR_SAVE_CLOSE: m_nPrevScreen = m_nCurrScreen; m_nCurrScreen = MENUPAGE_SAVE_FAILED; m_nCurrOption = 0; m_nScreenChangeDelayTimer = CTimer::GetTimeInMillisecondsPauseMode(); break; - case 4: - case 5: - case 6: + case SAVESTATUS_ERR_LOAD_OPEN: + case SAVESTATUS_ERR_LOAD_READ: + case SAVESTATUS_ERR_LOAD_CLOSE: m_nPrevScreen = m_nCurrScreen; m_nCurrScreen = MENUPAGE_LOAD_FAILED; m_nCurrOption = 0; m_nScreenChangeDelayTimer = CTimer::GetTimeInMillisecondsPauseMode(); break; - case 7: + case SAVESTATUS_ERR_DATA_INVALID: m_nPrevScreen = m_nCurrScreen; m_nCurrScreen = MENUPAGE_LOAD_FAILED_2; m_nCurrOption = 0; m_nScreenChangeDelayTimer = CTimer::GetTimeInMillisecondsPauseMode(); break; - case 8: - case 9: - case 10: + case SAVESTATUS_DELETEFAILED8: + case SAVESTATUS_DELETEFAILED9: + case SAVESTATUS_DELETEFAILED10: m_nPrevScreen = m_nCurrScreen; m_nCurrScreen = MENUPAGE_DELETE_FAILED; m_nCurrOption = 0; m_nScreenChangeDelayTimer = CTimer::GetTimeInMillisecondsPauseMode(); break; - default: - return; } } diff --git a/src/core/PCSave.cpp b/src/core/PCSave.cpp deleted file mode 100644 index 628e1218..00000000 --- a/src/core/PCSave.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "common.h" -#include "patcher.h" -#include "Frontend.h" -#include "PCSave.h" - -WRAPPER void C_PcSave::SetSaveDirectory(const char *path) { EAXJMP(0x591EA0); } -WRAPPER int8 C_PcSave::PopulateSlotInfo() { EAXJMP(0x592090); } -WRAPPER int8 C_PcSave::DeleteSlot(int) { EAXJMP(0x5922F0); } -WRAPPER int8 C_PcSave::SaveSlot(int) { EAXJMP(0x591EC0); } - -WRAPPER int8 CheckSlotDataValid(int) { EAXJMP(0x591A40); } - -WRAPPER wchar *GetNameOfSavedGame(int counter) { EAXJMP(0x591B60); } -WRAPPER wchar *GetSavedGameDateAndTime(int counter) { EAXJMP(0x591B50); } - - -C_PcSave PcSaveHelper = *(C_PcSave*)0x8E2C60; -int *Slots = (int*)0x728040; -int *SlotFileName = (int*)0x6F07C8; -int *SlotSaveDate = (int*)0x72B858; diff --git a/src/core/PCSave.h b/src/core/PCSave.h deleted file mode 100644 index 42239744..00000000 --- a/src/core/PCSave.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -class C_PcSave -{ -public: - int32 m_nHelper; - - static void SetSaveDirectory(const char *path); - int8 PopulateSlotInfo(); - int8 DeleteSlot(int); - int8 SaveSlot(int); -}; - -extern int8 CheckSlotDataValid(int); -extern wchar *GetNameOfSavedGame(int counter); -extern wchar *GetSavedGameDateAndTime(int counter); - -extern C_PcSave PcSaveHelper; -extern int *Slots; -extern int *SlotFileName; -extern int *SlotSaveDate; - -const char TopLineEmptyFile[] = "THIS FILE IS NOT VALID YET"; diff --git a/src/core/main.h b/src/core/main.h index 45ba441f..d6724d2b 100644 --- a/src/core/main.h +++ b/src/core/main.h @@ -19,6 +19,8 @@ extern wchar *gUString2; extern bool &b_FoundRecentSavedGameWantToLoad; extern bool gbPrintShite; extern bool &gbModelViewer; +extern bool &StillToFadeOut; +extern bool &JustLoadedDontFadeInYet; class CSprite2d; diff --git a/src/save/Date.cpp b/src/save/Date.cpp new file mode 100644 index 00000000..ca75bb5e --- /dev/null +++ b/src/save/Date.cpp @@ -0,0 +1,91 @@ +#include "common.h" +#include "Date.h" + +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; +}
\ No newline at end of file diff --git a/src/save/Date.h b/src/save/Date.h new file mode 100644 index 00000000..3e022d09 --- /dev/null +++ b/src/save/Date.h @@ -0,0 +1,18 @@ +#pragma once
+
+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);
+};
\ No newline at end of file diff --git a/src/save/GenericGameStorage.cpp b/src/save/GenericGameStorage.cpp new file mode 100644 index 00000000..52733b67 --- /dev/null +++ b/src/save/GenericGameStorage.cpp @@ -0,0 +1,267 @@ +#include "common.h" +#include "main.h" +#include "patcher.h" +#include "Camera.h" +#include "Clock.h" +#include "Date.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" + +const int SIZE_OF_ONE_GAME_IN_BYTES = 201729; + +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; + +#define ReadDataFromBufferPointer(buf, to) memcpy(&to, buf, sizeof(to)); buf += align4bytes(sizeof(to)); +#define WriteDataToBufferPointer(buf, from) memcpy(buf, &from, sizeof(from)); buf += align4bytes(sizeof(from)); + +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, sizeof(size)); + 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 += sizeof(uint32); + postsize = buf; +} + +void +CopySizeAndPreparePointer(uint8 *&buf, uint8 *&postbuf, uint8 *&postbuf2, uint32 &unused, uint32 &size) +{ + memcpy(buf, &size, sizeof(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 (SIZE_OF_ONE_GAME_IN_BYTES - sizeof(uint32) - bytes_pocessed > 0 && blocknum < 40) { + int32 blocksize; + if (!ReadDataFromFile(file, (uint8*)&blocksize, sizeof(blocksize))) { + CloseFile(file); + return false; + } + if (blocksize > align4bytes(sizeof(work_buff))) + blocksize = sizeof(work_buff) - sizeof(uint32); + 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) + memcpy(&level, work_buff+4, sizeof(level)); + blocknum++; + } + int32 _checkSum; + if (ReadDataFromFile(file, (uint8*)&_checkSum, sizeof(_checkSum))) { + 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, sizeof(buf)); + if (CFileMgr::GetErrorReadWrite(file)) { + PcSaveHelper.nErrorCode = SAVESTATUS_ERR_LOAD_READ; + if (!CloseFile(file)) + PcSaveHelper.nErrorCode = SAVESTATUS_ERR_LOAD_CLOSE; + return false; + } else { + uint8 *_buf = buf + sizeof(int32) + sizeof(wchar[24]) + sizeof(SYSTEMTIME) + sizeof(SIZE_OF_ONE_GAME_IN_BYTES); + ReadDataFromBufferPointer(_buf, CGame::currLevel); + ReadDataFromBufferPointer(_buf, TheCamera.GetPosition().x); + ReadDataFromBufferPointer(_buf, TheCamera.GetPosition().y); + ReadDataFromBufferPointer(_buf, TheCamera.GetPosition().z); + CStreaming::RemoveUnusedBigBuildings(CGame::currLevel); + 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..b8be1e79 --- /dev/null +++ b/src/save/GenericGameStorage.h @@ -0,0 +1,38 @@ +#pragma once
+
+#include "PCSave.h"
+
+#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 class 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..02bd08ad --- /dev/null +++ b/src/save/PCSave.cpp @@ -0,0 +1,143 @@ +#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 *data, uint32 size) +{ + CFileMgr::Write(file, (const char*)&size, sizeof(size)); + if (CFileMgr::GetErrorReadWrite(file)) { + nErrorCode = SAVESTATUS_ERR_SAVE_WRITE; + strncpy(SaveFileNameJustSaved, ValidSaveName, 259); + return false; + } + + CFileMgr::Write(file, (const char*)data, align4bytes(size)); + CheckSum += ((uint8*)&size)[0]; + CheckSum += ((uint8*)&size)[1]; + CheckSum += ((uint8*)&size)[2]; + CheckSum += ((uint8*)&size)[3]; + for (int i = 0; i < align4bytes(size); i++) { + CheckSum += *data++; + } + 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]; + struct { + int size; + wchar FileName[24]; + _SYSTEMTIME SaveDateTime; + } header; + sprintf(savename, "%s%i%s", DefaultPCSaveFileName, i + 1, ".b"); + int file = CFileMgr::OpenFile(savename, "rb"); + if (file != 0) { + CFileMgr::Read(file, (char*)&header, sizeof(header)); + if (strncmp((char*)&header, TopLineEmptyFile, sizeof(TopLineEmptyFile)-1) != 0) { + Slots[i + 1] = SLOT_OK; + memcpy(SlotFileName[i], &header.FileName, sizeof(header.FileName)); + + SlotFileName[i][24] = '\0'; + } + CFileMgr::CloseFile(file); + } + if (Slots[i + 1] == SLOT_OK) { + if (CheckDataNotCorrupt(i, savename)) { + _SYSTEMTIME st; + memcpy(&st, &header.SaveDateTime, sizeof(_SYSTEMTIME)); + const char *month; + switch (st.wMonth) + { + 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..a11d6b86 --- /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 *data, uint32 size); + static void SetSaveDirectory(const char *path); +}; |