-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathSavedGamesInSubdir.cpp
More file actions
450 lines (374 loc) · 10.7 KB
/
SavedGamesInSubdir.cpp
File metadata and controls
450 lines (374 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/**
* yrpp-spawner
*
* Copyright(C) 2022-present CnCNet
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.If not, see <http://www.gnu.org/licenses/>.
*/
#include <Main.h>
#include <Utilities/Debug.h>
#include <Utilities/Macro.h>
#include <Spawner/Spawner.h>
#include <HouseClass.h>
#include <LoadOptionsClass.h>
#include <filesystem>
#include <optional>
namespace SavedGames
{
inline bool CreateSubdir()
{
if (!std::filesystem::exists(Spawner::GetConfig()->SavedGameDir))
{
Debug::LogGame("\n");
Debug::Log("Folder Saved Games does not exist, creating...\n");
if (!std::filesystem::create_directories(Spawner::GetConfig()->SavedGameDir))
{
Debug::LogGame("\tCannot create folder Saved Games!\n");
return false;
}
Debug::LogGame("\tDone.\n");
}
return true;
}
inline void FormatPath(char* buffer, const char* pFileName)
{
sprintf(buffer, "%s\\%s", Spawner::GetConfig()->SavedGameDir, pFileName);
}
}
DEFINE_HOOK(0x67E475, LoadGame_SGInSubdir, 0x5)
{
if (Spawner::Enabled)
{
GET(char*, pFileName, ESI);
SavedGames::FormatPath(Main::readBuffer, pFileName);
R->ESI(Main::readBuffer);
}
return 0;
}
DEFINE_HOOK(0x559EB0, DeleteSave_SGInSubdir, 0x5)
{
if (Spawner::Enabled)
{
REF_STACK(char*, pFileName, 0x4);
SavedGames::FormatPath(Main::readBuffer, pFileName);
pFileName = Main::readBuffer;
}
return 0;
}
DEFINE_HOOK(0x67CF11, SaveGame_SGInSubdir, 0x5)
{
if (Spawner::Enabled)
{
if (!SavedGames::CreateSubdir())
return 0;
GET(char*, pFileName, EDI);
SavedGames::FormatPath(Main::readBuffer, pFileName);
R->EDI(Main::readBuffer);
}
return 0;
}
// Create random filename for save
// Not used. But it does not hurt in case of using a third-party library
// WW compiler made inline in LoadOptionsClass_Dialog
DEFINE_HOOK(0x55961C, LoadOptionsClass_RandomFilename_SGInSubdir, 0x5)
{
if (Spawner::Enabled)
{
GET(char*, pFileName, ESI);
SavedGames::FormatPath(Main::readBuffer, pFileName);
R->ESI(Main::readBuffer);
}
return 0;
}
// Finds free file name
DEFINE_HOOK(0x5592D2, LoadOptionsClass_Dialog_SGInSubdir, 0x5)
{
if (Spawner::Enabled)
{
GET(char*, pFileName, EDX);
SavedGames::FormatPath(Main::readBuffer, pFileName);
R->EDX(Main::readBuffer);
}
return 0;
}
// Used for disable buttons in a dialogs
DEFINE_HOOK(0x559C98, LoadOptionsClass_HasSaves_SGInSubdir, 0xB)
{
LEA_STACK(void*, pFindFileData, STACK_OFFSET(0x348, -0x140));
LEA_STACK(char*, pFileName, STACK_OFFSET(0x348, -0x33C));
if (Spawner::Enabled)
{
SavedGames::FormatPath(Main::readBuffer, pFileName);
pFileName = Main::readBuffer; // Always "Saved Games\*.SAV"
}
R->EAX(pFileName);
R->EDX(pFindFileData);
return 0x559C98 + 0xB;
}
// Fill a list of files
DEFINE_HOOK(0x559886, LoadOptionsClass_FillList_SGInSubdir, 0x8)
{
GET(struct _WIN32_FIND_DATAA*, pFind, EDX);
GET(char*, pFileName, EAX);
if (Spawner::Enabled)
{
SavedGames::FormatPath(Main::readBuffer, pFileName);
pFileName = Main::readBuffer; // Always "Saved Games\*.SAV"
}
HANDLE result = FindFirstFileA(pFileName, pFind);
R->EAX(result);
return 0x559886 + 0x8;
}
DEFINE_HOOK(0x67FD26, LoadOptionsClass_ReadSaveInfo_SGInSubdir, 0x5)
{
if (Spawner::Enabled)
{
GET(char*, pFileName, ECX);
SavedGames::FormatPath(Main::readBuffer, pFileName);
R->ECX(Main::readBuffer);
}
return 0;
}
//issue #18 : Save game filter for 3rd party campaigns
namespace SavedGames
{
struct CustomMissionID
{
static constexpr const wchar_t* SaveName = L"CustomMissionID";
int Number;
CustomMissionID() : Number { Spawner::GetConfig()->CustomMissionID } { }
CustomMissionID(int num) : Number { num } { }
operator int() const { return Number; }
};
template<typename T> requires std::is_trivially_copyable_v<T>
bool WriteToStorage(IStorage* pStorage)
{
IStreamPtr 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);
}
return ret;
}
template<typename T> requires std::is_trivially_copyable_v<T>
std::optional<T> ReadFromStorage(IStorage* pStorage)
{
IStreamPtr 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);
}
return hasValue ? std::make_optional(info) : std::nullopt;
}
}
DEFINE_HOOK(0x559921, LoadOptionsClass_FillList_FilterFiles, 0x6)
{
GET(FileEntryClass*, pEntry, EBP);
enum { NullThisEntry = 0x559959 };
/*
// there was a qsort later and filters out these but we could have just removed them right here
if (pEntry->IsWrongVersion || !pEntry->IsValid)
{
GameDelete(pEntry);
return NullThisEntry;
};
*/
OLECHAR wNameBuffer[0x100] {};
SavedGames::FormatPath(Main::readBuffer, pEntry->Filename.data());
MultiByteToWideChar(CP_UTF8, 0, Main::readBuffer, -1, wNameBuffer, std::size(wNameBuffer));
IStoragePtr pStorage = nullptr;
bool shouldDelete = false;
if (SUCCEEDED(StgOpenStorage(wNameBuffer, NULL,
STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
0, 0, &pStorage)
))
{
auto id = SavedGames::ReadFromStorage<SavedGames::CustomMissionID>(pStorage);
if (Spawner::GetConfig()->CustomMissionID != id.value_or(0))
shouldDelete = true;
}
if (shouldDelete)
{
GameDelete(pEntry);
return NullThisEntry;
}
return 0;
}
// Write : A la fin
DEFINE_HOOK(0x67D2E3, SaveGame_AdditionalInfoForClient, 0x6)
{
GET_STACK(IStorage*, pStorage, STACK_OFFSET(0x4A0, -0x490));
using namespace SavedGames;
if (pStorage && SessionClass::IsCampaign())
{
if (Spawner::GetConfig()->CustomMissionID)
WriteToStorage<CustomMissionID>(pStorage);
}
return 0;
}
// Read : Au debut
DEFINE_HOOK(0x67E4DC, LoadGame_AdditionalInfoForClient, 0x7)
{
LEA_STACK(const wchar_t*, filename, STACK_OFFSET(0x518, -0x4F4));
IStoragePtr pStorage = nullptr;
using namespace SavedGames;
if (SUCCEEDED(StgOpenStorage(filename, NULL,
STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
0, 0, &pStorage)
))
{
if (auto id = ReadFromStorage<CustomMissionID>(pStorage))
{
int num = id->Number;
Debug::Log("sav file CustomMissionID = %d\n", num);
Spawner::GetConfig()->CustomMissionID = num;
ScenarioClass::Instance->EndOfGame = true;
}
else
{
Spawner::GetConfig()->CustomMissionID = 0;
}
}
return 0;
}
// Custom missions especially can contain paths in scenario filenames which cause
// the initial save game to fail, remove the paths before filename and make the
// filename uppercase to match with usual savegame names.
DEFINE_HOOK(0x55DC85, MainLoop_SaveGame_SanitizeFilename, 0x7)
{
LEA_STACK(char*, pFilename, STACK_OFFSET(0x1C4, -0x178));
LEA_STACK(const wchar_t*, pDescription, STACK_OFFSET(0x1C4, -0x70));
char* slash1 = strrchr(pFilename, '/');
char* slash2 = strrchr(pFilename, '\\');
char* lastSlash = (slash1 > slash2) ? slash1 : slash2;
if (lastSlash != NULL)
{
pFilename = lastSlash + 1;
*lastSlash = '\0';
}
for (char* p = pFilename; *p; ++p)
*p = (char)toupper((unsigned char)*p);
R->ECX(pFilename);
R->EDX(pDescription);
return 0x55DC90;
}
#pragma region nosaveload
#include <WWMessageBox.h>
#include <LoadProgressManager.h>
const wchar_t* Fetch_CSF_Text(const char* label, const wchar_t* defaultText)
{
std::wstring_view msg = StringTable::LoadString(label);
if (msg.empty() || msg.starts_with(L"MISSING"))
return defaultText;
return msg.data();
}
DEFINE_HOOK(0x686089, DoLose_RetryDialogForCampaigns, 0x7)
{
if (!Spawner::GetConfig()->DisableSaveLoad) return 0;
WWMessageBox::Instance.Process(
Fetch_CSF_Text("TXT_HARDCORE_FAILURE", L"GG"),
StringTable::LoadString("TXT_OK"), nullptr, nullptr);
return 0x6860EE;
}
// disable load, save and delete buttons on the ingame menu
DEFINE_HOOK(0x4F17F6, sub_4F1720_DisableSaves, 0x6)
{
if (!Spawner::GetConfig()->DisableSaveLoad) return 0;
GET(HWND, hDlg, EBP);
enum { LoadGameButton = 1310, DeleteGameButton = 1312 };
for (int item = LoadGameButton; item <= DeleteGameButton; ++item)
{
if (HWND hItem = GetDlgItem(hDlg, item))
EnableWindow(hItem, FALSE);
}
return 0x4F1834;
}
std::wstring HardCoreText {};
DEFINE_HOOK(0x553076, LoadProgressMgr_Draw_ExtraText_Campaign, 0x5)
{
GET(LoadProgressManager*, self, EBP);
if (!Spawner::GetConfig()->DisableSaveLoad) return 0;
Point2D pos
{
self->TitleBarRect.X + self->TitleBarRect.Width - 100,
self->TitleBarRect.Y + 10
};
if(HardCoreText.empty())
HardCoreText = Fetch_CSF_Text("TXT_HARDCORE_MODE", L"HardCore");
LEA_STACK(RectangleStruct*, pBnd, STACK_OFFSET(0x1268, -0x1204));
if (auto logo = FileSystem::LoadSHPFile("hardcorelogo.shp"))
{
self->ProgressSurface->DrawSHP(FileSystem::PALETTE_PAL, logo, 0, &pos, pBnd, BlitterFlags::bf_400, 0, 0, ZGradient::Ground, 1000, 0, nullptr, 0, 0, 0);
}
else
{
self->ProgressSurface->DrawText(HardCoreText.c_str(), &pos, COLOR_RED);
}
return 0;
}
DEFINE_HOOK(0x4F4573, GScreenClass_Draw_SpawnerShit, 0x5)
{
if (!Spawner::GetConfig()->DisableSaveLoad) return 0;
wchar_t buffer[0x20] {};
int total_seconds = Unsorted::CurrentFrame / 15;
int hours = total_seconds / 3600;
int minutes = (total_seconds / 60) % 60;
int seconds = total_seconds % 60;
if (hours > 0)
{
swprintf(buffer, std::size(buffer), L"%ls %d:%02d:%02d", HardCoreText.c_str(), hours, minutes, seconds);
}
else
{
swprintf(buffer, std::size(buffer), L"%ls %02d:%02d", HardCoreText.c_str(), minutes, seconds);
}
auto wanted = Drawing::GetTextDimensions(buffer, { 0,0 }, 0, 2, 0);
RectangleStruct rect = {
DSurface::Composite->GetWidth() - wanted.Width - 30,
0,
wanted.Width + 10,
wanted.Height + 10
};
Point2D location { rect.X +5 ,5 };
ColorStruct color { 0x0, 0x0 ,0x0};
DSurface::Composite->FillRectTrans(&rect, &color, 50);
DSurface::Composite->DrawRect(&rect, COLOR_WHITE);
DSurface::Composite->DrawText(buffer, &location, COLOR_WHITE);
//Phobos' extended tooltips interferred
R->ECX(*(int*)0x887640);
return 0x4F4589;
}
#pragma endregion