Skip to content

Commit

Permalink
Unify quicksave with initial save
Browse files Browse the repository at this point in the history
probably fixing #863
  • Loading branch information
chaserli committed Jun 19, 2024
1 parent 3fe6326 commit 83d3b2a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 50 deletions.
25 changes: 6 additions & 19 deletions src/Commands/QuickSave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,15 @@ void QuickSaveCommandClass::Execute(WWKey eInput) const

if (SessionClass::IsSingleplayer())
{
char fName[0x80];
*reinterpret_cast<bool*>(0xABCE08) = false;
Phobos::ShouldQuickSave = true;

SYSTEMTIME time;
GetLocalTime(&time);

_snprintf_s(fName, 0x7F, "Map.%04u%02u%02u-%02u%02u%02u-%05u.sav",
time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);

PrintMessage(StringTable::LoadString(GameStrings::TXT_SAVING_GAME));

wchar_t fDescription[0x80] = { 0 };
if (SessionClass::IsCampaign())
wcscpy_s(fDescription, ScenarioClass::Instance->UINameLoaded);
else
wcscpy_s(fDescription, ScenarioClass::Instance->Name);
wcscat_s(fDescription, L" - ");
wcscat_s(fDescription, GeneralUtils::LoadStringUnlessMissing("TXT_QUICKSAVE_SUFFIX", L"Quicksaved"));

if (ScenarioClass::SaveGame(fName, fDescription))
PrintMessage(StringTable::LoadString(GameStrings::TXT_GAME_WAS_SAVED));
Phobos::CustomGameSaveDescription = ScenarioClass::Instance->UINameLoaded;
else
PrintMessage(StringTable::LoadString(GameStrings::TXT_ERROR_SAVING_GAME));
Phobos::CustomGameSaveDescription = ScenarioClass::Instance->Name;
Phobos::CustomGameSaveDescription += L" - ";
Phobos::CustomGameSaveDescription += GeneralUtils::LoadStringUnlessMissing("TXT_QUICKSAVE_SUFFIX", L"Quicksaved");
}
else
{
Expand Down
34 changes: 7 additions & 27 deletions src/Ext/TAction/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,35 +103,15 @@ bool TActionExt::SaveGame(TActionClass* pThis, HouseClass* pHouse, ObjectClass*
{
if (SessionClass::IsSingleplayer())
{
auto PrintMessage = [](const wchar_t* pMessage)
{
MessageListClass::Instance->PrintMessage(
pMessage,
RulesClass::Instance->MessageDelay,
HouseClass::CurrentPlayer->ColorSchemeIndex,
true
);
};

char fName[0x80];

SYSTEMTIME time;
GetLocalTime(&time);

_snprintf_s(fName, 0x7F, "Map.%04u%02u%02u-%02u%02u%02u-%05u.sav",
time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);

PrintMessage(StringTable::LoadString(GameStrings::TXT_SAVING_GAME));

wchar_t fDescription[0x80] = { 0 };
wcscpy_s(fDescription, ScenarioClass::Instance->UINameLoaded);
wcscat_s(fDescription, L" - ");
wcscat_s(fDescription, StringTable::LoadString(pThis->Text));
*reinterpret_cast<bool*>(0xABCE08) = false;
Phobos::ShouldQuickSave = true;

if (ScenarioClass::Instance->SaveGame(fName, fDescription))
PrintMessage(StringTable::LoadString(GameStrings::TXT_GAME_WAS_SAVED));
if (SessionClass::IsCampaign())
Phobos::CustomGameSaveDescription = ScenarioClass::Instance->UINameLoaded;
else
PrintMessage(StringTable::LoadString(GameStrings::TXT_ERROR_SAVING_GAME));
Phobos::CustomGameSaveDescription = ScenarioClass::Instance->Name;
Phobos::CustomGameSaveDescription += L" - ";
Phobos::CustomGameSaveDescription += StringTable::LoadString(pThis->Text);
}

return true;
Expand Down
56 changes: 53 additions & 3 deletions src/Phobos.INI.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "Phobos.h"

#include <GameStrings.h>
#include <CCINIClass.h>
#include <ScenarioClass.h>
#include <SessionClass.h>
#include <MessageListClass.h>
#include <HouseClass.h>

#include <Utilities/Parser.h>
#include <Utilities/GeneralUtils.h>
Expand Down Expand Up @@ -199,7 +202,54 @@ DEFINE_HOOK(0x5FACDF, OptionsClass_LoadSettings_LoadPhobosSettings, 0x5)
return 0;
}

DEFINE_HOOK(0x55DBF5, MainLoop_SaveGame, 0xA)
bool Phobos::ShouldQuickSave = false;
std::wstring Phobos::CustomGameSaveDescription {};

void Phobos::PassiveSaveGame()
{
auto PrintMessage = [](const wchar_t* pMessage)
{
MessageListClass::Instance->PrintMessage(
pMessage,
RulesClass::Instance->MessageDelay,
HouseClass::CurrentPlayer->ColorSchemeIndex,
true
);
};

PrintMessage(StringTable::LoadString(GameStrings::TXT_SAVING_GAME));
char fName[0x80];

SYSTEMTIME time;
GetLocalTime(&time);

_snprintf_s(fName, 0x7F, "Map.%04u%02u%02u-%02u%02u%02u-%05u.sav",
time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);

if (ScenarioClass::SaveGame(fName, Phobos::CustomGameSaveDescription.c_str()))
PrintMessage(StringTable::LoadString(GameStrings::TXT_GAME_WAS_SAVED));
else
PrintMessage(StringTable::LoadString(GameStrings::TXT_ERROR_SAVING_GAME));
}

DEFINE_HOOK(0x55DBCD, MainLoop_SaveGame, 0x6)
{
return Phobos::Config::SaveGameOnScenarioStart ? 0 : 0x55DC99;
// This happens right before LogicClass::Update()
enum { SkipSave = 0x55DC99, InitialSave = 0x55DBE6 };

bool& scenario_saved = *reinterpret_cast<bool*>(0xABCE08);
if (SessionClass::IsSingleplayer() && !scenario_saved)
{
scenario_saved = true;
if (Phobos::ShouldQuickSave)
{
Phobos::PassiveSaveGame();
Phobos::ShouldQuickSave = false;
Phobos::CustomGameSaveDescription.clear();
}
else if (Phobos::Config::SaveGameOnScenarioStart && SessionClass::IsCampaign())
return InitialSave;
}

return SkipSave;
}
6 changes: 5 additions & 1 deletion src/Phobos.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <Phobos.version.h>
#include <Windows.h>

#include <string>

#define CAN_USE_ARES 1

class CCINIClass;
Expand Down Expand Up @@ -33,7 +35,9 @@ class Phobos
static const wchar_t* VersionDescription;
static bool DisplayDamageNumbers;
static bool IsLoadingSaveGame;

static bool ShouldQuickSave;
static std::wstring CustomGameSaveDescription;
static void PassiveSaveGame();
#ifdef DEBUG
static bool DetachFromDebugger();
#endif
Expand Down

0 comments on commit 83d3b2a

Please sign in to comment.