Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Misc/GameSpeed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* along with this program.If not, see <http://www.gnu.org/licenses/>.
*/

#include <Main.h>
#include <Spawner/Spawner.h>
#include <Utilities/Macro.h>
#include <HouseClass.h>
Expand Down Expand Up @@ -59,3 +60,14 @@ DEFINE_HOOK(0x4E20BA, GameControlsDialog__Init_SetGameSpeedAvailability, 0x5)

return 0;
}

// Preserve user-selected Campaign game speed when resuming/loading if SpeedControl is enabled.
DEFINE_HOOK(0x69BAE2, SessionClass_Resume_PreserveCampaignGameSpeed, 0x5)
{
enum { SkipDefaultSpeedReset = 0x69BAF1 };

if (Spawner::Enabled && SessionClass::IsCampaign() && Main::GetConfig()->SpeedControl)
return SkipDefaultSpeedReset;

return 0;
}
64 changes: 62 additions & 2 deletions src/Misc/SavedGamesInSubdir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <Utilities/Macro.h>
#include <Spawner/Spawner.h>

#include <GameModeOptionsClass.h>
#include <GameOptionsClass.h>
#include <HouseClass.h>
#include <LoadOptionsClass.h>

Expand Down Expand Up @@ -184,6 +186,21 @@ namespace SavedGames
operator int() const { return Number; }
};

struct GameSpeed
{
static constexpr const wchar_t* SaveName = L"GameSpeed";

int Value;

GameSpeed() : Value { GameOptionsClass::Instance.GameSpeed } { }

GameSpeed(int value) : Value { value } { }

operator int() const { return Value; }
};

inline std::optional<int> LoadedGameSpeed = std::nullopt;


template<typename T> requires std::is_trivially_copyable_v<T>
bool WriteToStorage(IStorage* pStorage)
Expand Down Expand Up @@ -286,10 +303,13 @@ DEFINE_HOOK(0x67D2E3, SaveGame_AdditionalInfoForClient, 0x6)
GET_STACK(IStorage*, pStorage, STACK_OFFSET(0x4A0, -0x490));
using namespace SavedGames;

if (pStorage && SessionClass::IsCampaign())
if (pStorage)
{
if (Spawner::GetConfig()->CustomMissionID)
if (SessionClass::IsCampaign() && Spawner::GetConfig()->CustomMissionID)
WriteToStorage<CustomMissionID>(pStorage);

if (SessionClass::IsSingleplayer())
WriteToStorage<GameSpeed>(pStorage);
}

return 0;
Expand All @@ -305,6 +325,8 @@ DEFINE_HOOK(0x67E4DC, LoadGame_AdditionalInfoForClient, 0x7)
IStoragePtr pStorage = nullptr;
using namespace SavedGames;

LoadedGameSpeed.reset();

if (SUCCEEDED(StgOpenStorage(filename, NULL,
STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
0, 0, &pStorage)
Expand All @@ -321,6 +343,44 @@ DEFINE_HOOK(0x67E4DC, LoadGame_AdditionalInfoForClient, 0x7)
{
Spawner::GetConfig()->CustomMissionID = 0;
}

if (auto speed = ReadFromStorage<GameSpeed>(pStorage))
{
int value = speed->Value;
if (value >= 0 && value <= 6)
{
Debug::Log("sav file GameSpeed = %d\n", value);
LoadedGameSpeed = value;
}
}
}

return 0;
}

DEFINE_HOOK(0x67E6DA, LoadGame_AfterInit_RestoreGameSpeed, 0x6)
{
if (!Spawner::Enabled)
return 0;

if (SavedGames::LoadedGameSpeed.has_value())
{
const int speed = *SavedGames::LoadedGameSpeed;
SavedGames::LoadedGameSpeed.reset();

if (SessionClass::IsSkirmish() || (SessionClass::IsCampaign() && Main::GetConfig()->SpeedControl))
{
GameOptionsClass::Instance.GameSpeed = speed;
GameModeOptionsClass::Instance.GameSpeed = speed;
}
}
else if (SessionClass::IsSkirmish())
{
// Legacy Skirmish saves without a GameSpeed stream still store
// the initial lobby GameSpeed in GameModeOptionsClass::Instance.
const int speed = GameModeOptionsClass::Instance.GameSpeed;
if (speed >= 0 && speed <= 6)
GameOptionsClass::Instance.GameSpeed = speed;
}

return 0;
Expand Down