|
22 | 22 | #include <Utilities/Macro.h> |
23 | 23 | #include <Spawner/Spawner.h> |
24 | 24 |
|
| 25 | +#include <HouseClass.h> |
| 26 | +#include <LoadOptionsClass.h> |
| 27 | + |
25 | 28 | #include <filesystem> |
| 29 | +#include <optional> |
26 | 30 |
|
27 | 31 | namespace SavedGames |
28 | 32 | { |
@@ -161,3 +165,199 @@ DEFINE_HOOK(0x67FD26, LoadOptionsClass_ReadSaveInfo_SGInSubdir, 0x5) |
161 | 165 |
|
162 | 166 | return 0; |
163 | 167 | } |
| 168 | + |
| 169 | + |
| 170 | +//issue #18 : Save game filter for 3rd party campaigns |
| 171 | +namespace SavedGames |
| 172 | +{ |
| 173 | + struct CustomMissionID |
| 174 | + { |
| 175 | + static constexpr wchar_t* SaveName = L"CustomMissionID"; |
| 176 | + |
| 177 | + int Number; |
| 178 | + |
| 179 | + CustomMissionID() : Number { Spawner::GetConfig()->CustomMissionID } { } |
| 180 | + |
| 181 | + CustomMissionID(int num) : Number { num } { } |
| 182 | + |
| 183 | + operator int() const { return Number; } |
| 184 | + }; |
| 185 | + |
| 186 | + // More fun |
| 187 | + int HowManyTimesISavedForThisScenario = 0; |
| 188 | + |
| 189 | + struct ExtraMetaInfo |
| 190 | + { |
| 191 | + static constexpr wchar_t* SaveName = L"Spawner Extra Info"; |
| 192 | + |
| 193 | + int CurrentFrame; |
| 194 | + int SavedCount; |
| 195 | + int TechnoCount; |
| 196 | + |
| 197 | + explicit ExtraMetaInfo() |
| 198 | + :CurrentFrame { Unsorted::CurrentFrame } |
| 199 | + , SavedCount { HowManyTimesISavedForThisScenario } |
| 200 | + , TechnoCount { TechnoClass::Array->Count } |
| 201 | + { |
| 202 | + } |
| 203 | + }; |
| 204 | + |
| 205 | + template<typename T> |
| 206 | + bool AppendToStorage(IStorage* pStorage) |
| 207 | + { |
| 208 | + IStream* pStream = nullptr; |
| 209 | + bool ret = false; |
| 210 | + HRESULT hr = pStorage->CreateStream( |
| 211 | + T::SaveName, |
| 212 | + STGM_WRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE, |
| 213 | + 0, |
| 214 | + 0, |
| 215 | + &pStream |
| 216 | + ); |
| 217 | + |
| 218 | + if (SUCCEEDED(hr) && pStream != nullptr) |
| 219 | + { |
| 220 | + T info {}; |
| 221 | + ULONG written = 0; |
| 222 | + hr = pStream->Write(&info, sizeof(info), &written); |
| 223 | + ret = SUCCEEDED(hr) && written == sizeof(info); |
| 224 | + pStream->Release(); |
| 225 | + } |
| 226 | + |
| 227 | + return ret; |
| 228 | + } |
| 229 | + |
| 230 | + |
| 231 | + template<typename T> |
| 232 | + std::optional<T> ReadFromStorage(IStorage* pStorage) |
| 233 | + { |
| 234 | + IStream* pStream = nullptr; |
| 235 | + bool hasValue = false; |
| 236 | + HRESULT hr = pStorage->OpenStream( |
| 237 | + T::SaveName, |
| 238 | + NULL, |
| 239 | + STGM_READ | STGM_SHARE_EXCLUSIVE, |
| 240 | + 0, |
| 241 | + &pStream |
| 242 | + ); |
| 243 | + |
| 244 | + T info; |
| 245 | + |
| 246 | + if (SUCCEEDED(hr) && pStream != nullptr) |
| 247 | + { |
| 248 | + ULONG read = 0; |
| 249 | + hr = pStream->Read(&info, sizeof(info), &read); |
| 250 | + hasValue = SUCCEEDED(hr) && read == sizeof(info); |
| 251 | + |
| 252 | + pStream->Release(); |
| 253 | + } |
| 254 | + |
| 255 | + return hasValue ? std::make_optional(info) : std::nullopt; |
| 256 | + } |
| 257 | + |
| 258 | +} |
| 259 | + |
| 260 | +DEFINE_HOOK(0x559921, LoadOptionsClass_FillList_FilterFiles, 0x6) |
| 261 | +{ |
| 262 | + GET(FileEntryClass*, pEntry, EBP); |
| 263 | + enum { NullThisEntry = 0x559959 }; |
| 264 | + |
| 265 | + // there was a qsort later and filters out these but we could have just removed them right here |
| 266 | + if (pEntry->IsWrongVersion || !pEntry->IsValid) |
| 267 | + { |
| 268 | + GameDelete(pEntry); |
| 269 | + return NullThisEntry; |
| 270 | + }; |
| 271 | + |
| 272 | + static OLECHAR wNameBuffer[0x100] {}; |
| 273 | + SavedGames::FormatPath(Main::readBuffer, pEntry->Filename.data()); |
| 274 | + MultiByteToWideChar(CP_UTF8, 0, Main::readBuffer, -1, wNameBuffer, std::size(wNameBuffer)); |
| 275 | + IStorage* pStorage = nullptr; |
| 276 | + bool shouldDelete = false; |
| 277 | + if (SUCCEEDED(StgOpenStorage(wNameBuffer, NULL, |
| 278 | + STGM_READWRITE | STGM_SHARE_EXCLUSIVE, |
| 279 | + 0, 0, &pStorage) |
| 280 | + )) |
| 281 | + { |
| 282 | + auto id = SavedGames::ReadFromStorage<SavedGames::CustomMissionID>(pStorage); |
| 283 | + |
| 284 | + if (Spawner::GetConfig()->CustomMissionID != id.value_or(0)) |
| 285 | + shouldDelete = true; |
| 286 | + } |
| 287 | + |
| 288 | + if (pStorage) |
| 289 | + pStorage->Release(); |
| 290 | + |
| 291 | + if (shouldDelete) |
| 292 | + { |
| 293 | + GameDelete(pEntry); |
| 294 | + return NullThisEntry; |
| 295 | + } |
| 296 | + |
| 297 | + return 0; |
| 298 | +} |
| 299 | + |
| 300 | +DEFINE_HOOK(0x683AFE, StartNewScenario_ClearCounter, 0x6) |
| 301 | +{ |
| 302 | + SavedGames::HowManyTimesISavedForThisScenario = 0; |
| 303 | + return 0; |
| 304 | +} |
| 305 | + |
| 306 | + |
| 307 | +// Write : A la fin |
| 308 | +DEFINE_HOOK(0x67D2E3, SaveGame_AdditionalInfoForClient, 0x6) |
| 309 | +{ |
| 310 | + GET_STACK(IStorage*, pStorage, STACK_OFFSET(0x4A0, -0x490)); |
| 311 | + using namespace SavedGames; |
| 312 | + HowManyTimesISavedForThisScenario++; |
| 313 | + |
| 314 | + if (pStorage) |
| 315 | + { |
| 316 | + if (SessionClass::IsCampaign() && Spawner::GetConfig()->CustomMissionID) |
| 317 | + AppendToStorage<CustomMissionID>(pStorage); |
| 318 | + if (AppendToStorage<ExtraMetaInfo>(pStorage)) |
| 319 | + Debug::Log("[Spawner] Extra meta info appended on sav file\n"); |
| 320 | + } |
| 321 | + |
| 322 | + return 0; |
| 323 | +} |
| 324 | + |
| 325 | +// Read : Au debut |
| 326 | +DEFINE_HOOK(0x67E4DC, LoadGame_AdditionalInfoForClient, 0x7) |
| 327 | +{ |
| 328 | + LEA_STACK(const wchar_t*, filename, STACK_OFFSET(0x518, -0x4F4)); |
| 329 | + IStorage* pStorage = nullptr; |
| 330 | + using namespace SavedGames; |
| 331 | + |
| 332 | + if (SUCCEEDED(StgOpenStorage(filename, NULL, |
| 333 | + STGM_READWRITE | STGM_SHARE_EXCLUSIVE, |
| 334 | + 0, 0, &pStorage) |
| 335 | + )) |
| 336 | + { |
| 337 | + if (auto id = ReadFromStorage<CustomMissionID>(pStorage)) |
| 338 | + { |
| 339 | + int num = id->Number; |
| 340 | + Debug::Log("[Spawner] sav file CustomMissionID = %d\n", num); |
| 341 | + Spawner::GetConfig()->CustomMissionID = num; |
| 342 | + ScenarioClass::Instance->EndOfGame = true; |
| 343 | + } |
| 344 | + else |
| 345 | + { |
| 346 | + Spawner::GetConfig()->CustomMissionID = 0; |
| 347 | + } |
| 348 | + |
| 349 | + if (auto info = ReadFromStorage<ExtraMetaInfo>(pStorage)) |
| 350 | + { |
| 351 | + Debug::Log("[Spawner] CurrentFrame = %d, TechnoCount = %d, HowManyTimesSaved = %d \n" |
| 352 | + , info->CurrentFrame |
| 353 | + , info->TechnoCount |
| 354 | + , info->SavedCount |
| 355 | + ); |
| 356 | + HowManyTimesISavedForThisScenario = info->SavedCount; |
| 357 | + } |
| 358 | + } |
| 359 | + if (pStorage) |
| 360 | + pStorage->Release(); |
| 361 | + |
| 362 | + return 0; |
| 363 | +} |
0 commit comments