Skip to content

Commit

Permalink
campaignid only for non official missions
Browse files Browse the repository at this point in the history
  • Loading branch information
chaserli committed Mar 7, 2024
1 parent ffa0f9f commit 6378ce9
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
152 changes: 152 additions & 0 deletions src/Misc/SavedGamesInSubdir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
#include <Utilities/Macro.h>
#include <Spawner/Spawner.h>

#include <HouseClass.h>
#include <AnimClass.h>

#include <filesystem>
#include <optional>

namespace SavedGames
{
Expand Down Expand Up @@ -161,3 +165,151 @@ DEFINE_HOOK(0x67FD26, LoadOptionsClass_ReadSaveInfo_SGInSubdir, 0x5)

return 0;
}


namespace SavedGames
{
//issue #18
struct CampaignID
{
static constexpr wchar_t* SaveName = L"Campaign ID";

int Number;
explicit CampaignID() :
Number { SessionClass::IsCampaign() ? Spawner::GetConfig()->CampaignID : 0 }
{
}

operator int () const
{
return Number;
}

CampaignID(noinit_t()) { }
};

// More fun
struct ExtraMetaInfo
{
static constexpr wchar_t* SaveName = L"Spawner extra info";

int CurrentFrame;
int PlayerCount;
int TechnoCount;
int AnimCount;

explicit ExtraMetaInfo()
:CurrentFrame { Unsorted::CurrentFrame }
, PlayerCount { HouseClass::Array->Count }
, TechnoCount { TechnoClass::Array->Count }
, AnimCount { AnimClass::Array->Count }
{
}

ExtraMetaInfo(noinit_t()) { }
};

template<typename T>
bool AppendToStorage(IStorage* pStorage)
{
IStream* pStream = nullptr;
bool ret = false;
HRESULT hr = pStorage->CreateStream(
T::SaveName,
STGM_WRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
0,
0,
&pStream
);

if (SUCCEEDED(hr) && pStream != nullptr)
{
T info {};
ULONG written = 0;
hr = pStream->Write(&info, sizeof(info), &written);
ret = SUCCEEDED(hr) && written == sizeof(info);
pStream->Release();
}

return ret;
}


template<typename T>
std::optional<T> ReadFromStorage(IStorage* pStorage)
{
IStream* pStream = nullptr;
bool hasValue = false;
HRESULT hr = pStorage->OpenStream(
T::SaveName,
NULL,
STGM_READ | STGM_SHARE_EXCLUSIVE,
0,
&pStream
);

T info;

if (SUCCEEDED(hr) && pStream != nullptr)
{
ULONG read = 0;
hr = pStream->Read(&info, sizeof(info), &read);
hasValue = SUCCEEDED(hr) && read == sizeof(info);

pStream->Release();
}

return hasValue ? std::make_optional(info) : std::nullopt;
}

}

// Write : A la fin
DEFINE_HOOK(0x67D2E3, GameSave_AdditionalInfoForClient, 0x6)
{
GET_STACK(IStorage*, pStorage, STACK_OFFSET(0x4A0, -0x490));
using namespace SavedGames;

if (pStorage)
{
if (CampaignID {} != 0)
AppendToStorage<CampaignID>(pStorage);
if (AppendToStorage<ExtraMetaInfo>(pStorage))
Debug::Log("[Spawner] Extra meta info appended on sav file\n");
}

return 0;
}

// Read : Au debut
DEFINE_HOOK(0x67E4DC, LoadGame_AdditionalInfoForClient, 0x7)
{
LEA_STACK(const wchar_t*, filename, STACK_OFFSET(0x518, -0x4F4));
IStorage* pStorage = nullptr;
using namespace SavedGames;

if (SUCCEEDED(StgOpenStorage(filename,NULL,
STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
0,0,&pStorage)
))
{
if (auto id = ReadFromStorage<CampaignID>(pStorage))
{
int num = id.value().Number;
Debug::Log("[Spawner] sav file CampaignID = %d\n", num);
Spawner::GetConfig()->CampaignID = num;
}
if (auto info = ReadFromStorage<ExtraMetaInfo>(pStorage))
{
Debug::Log("[Spawner] CurrentFrame = %d, TechnoCount = %d, AnimCount = %d \n"
, info.value().CurrentFrame
, info.value().TechnoCount
, info.value().AnimCount
);
}
}
if (pStorage)
pStorage->Release();

return 0;
}
1 change: 1 addition & 0 deletions src/Spawner/Spawner.Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void SpawnerConfig::LoadFromINIFile(CCINIClass* pINI)
LoadSaveGame = pINI->ReadBool(pSettingsSection, "LoadSaveGame", LoadSaveGame);
/* SavedGameDir */ pINI->ReadString(pSettingsSection, "SavedGameDir", SavedGameDir, SavedGameDir, sizeof(SavedGameDir));
/* SaveGameName */ pINI->ReadString(pSettingsSection, "SaveGameName", SaveGameName, SaveGameName, sizeof(SaveGameName));
CampaignID = pINI->ReadInteger(pSettingsSection, "CampaignID", 0);

{ // Scenario Options
Seed = pINI->ReadInteger(pSettingsSection, "Seed", Seed);
Expand Down
2 changes: 2 additions & 0 deletions src/Spawner/Spawner.Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class SpawnerConfig
bool LoadSaveGame;
char SavedGameDir[MAX_PATH]; // Nested paths are also supported, e.g. "Saved Games\\Yuri's Revenge"
char SaveGameName[60];
int CampaignID;

// Scenario Options
int Seed;
Expand Down Expand Up @@ -161,6 +162,7 @@ class SpawnerConfig
, LoadSaveGame { false }
, SavedGameDir { "Saved Games" }
, SaveGameName { "" }
, CampaignID { 0 }

// Scenario Options
, Seed { 0 }
Expand Down

0 comments on commit 6378ce9

Please sign in to comment.