-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStream.cpp
899 lines (748 loc) · 34.2 KB
/
Stream.cpp
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
// ** TODO A lot of this should probably be moved into op2ext due to overlap with mod loader functionality.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <vfw.h>
#include "Patcher.h"
#include "Util.h"
#include "Stream.h"
#include "Tethys/Common/Library.h"
#include "Tethys/Game/TApp.h"
#include "Tethys/Game/MissionManager.h"
#include "Tethys/Resource/CConfig.h"
#include "Tethys/Resource/ResManager.h"
#include "Tethys/Resource/SoundManager.h"
#include "Tethys/Resource/MemoryMappedFile.h"
#include "Tethys/UI/IWnd.h"
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <iostream>
#include <sstream>
#include <charconv>
#include <filesystem>
#include <memory>
using namespace Tethys;
using namespace TethysAPI;
using namespace Tethys::TethysUtil;
using namespace Patcher::Util;
using namespace Patcher::Registers;
using MissionList = std::vector<std::pair<std::string, std::unique_ptr<AIModDesc, void(*)(AIModDesc*)>>>;
static constexpr wchar_t PathVar[] = L"PATH";
static constexpr char OPUDir[] = "OPU";
static constexpr char BaseDir[] = "base";
static std::filesystem::path g_curMapPath;
static bool g_searchForMission = false;
// =====================================================================================================================
// Gets op2ext mod directories.
static std::vector<std::filesystem::path> GetModPaths() {
static std::vector<std::filesystem::path> paths;
static Library op2Ext("op2ext.dll");
static auto*const pfnGetModuleDirectoryCount = op2Ext.Get<size_t CDECL()>("GetModuleDirectoryCount");
static auto*const pfnGetModuleDirectory = op2Ext.Get<size_t CDECL(size_t, char*, size_t)>("GetModuleDirectory");
if (paths.empty() && (pfnGetModuleDirectoryCount != nullptr) && (pfnGetModuleDirectory != nullptr)) {
char buf[MAX_PATH] = "";
for (size_t i = 0, count = pfnGetModuleDirectoryCount(); i < count; ++i) {
if (pfnGetModuleDirectory(i, &buf[0], MAX_PATH) == 0) {
paths.emplace_back(&buf[0]);
}
}
}
return paths;
}
// =====================================================================================================================
// Gets base subdirectory paths: current map, mods, OPU/base, then OPU.
static std::vector<std::filesystem::path> GetBasePaths() {
static std::vector<std::filesystem::path> bases;
static std::filesystem::path mapPath;
if ((bases.empty()) || (mapPath != g_curMapPath)) {
static const auto installPath(std::filesystem::path(g_resManager.installedDir_));
static const auto opuPath(installPath/OPUDir);
bases.clear();
mapPath = g_curMapPath;
if ((mapPath != opuPath/BaseDir) && (mapPath != opuPath) && (mapPath != installPath)) {
bases.push_back(mapPath);
}
const auto& modPaths = GetModPaths();
bases.insert(bases.end(), modPaths.begin(), modPaths.end());
bases.push_back(opuPath/BaseDir);
bases.push_back(opuPath);
}
return bases;
}
// =====================================================================================================================
// Gets file search paths for a particular asset type.
std::vector<std::filesystem::path> GetSearchPaths(
std::string extension,
bool searchForMission,
bool excludeStockDirs)
{
constexpr auto SearchOptions = std::filesystem::directory_options::follow_directory_symlink |
std::filesystem::directory_options::skip_permission_denied;
std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
std::vector<std::filesystem::path> searchPaths;
std::set<std::filesystem::path> knownPaths;
auto AddPath = [&searchPaths, &knownPaths](const std::filesystem::path& path) {
if (path.is_absolute() && (knownPaths.count(path) == 0)) {
searchPaths.emplace_back(path);
knownPaths.insert(path);
}
};
// Search 1) ini debug path if set.
{
char debugPath[MAX_PATH] = "";
g_configFile.GetString("DEBUG", "ART_PATH", &debugPath[0], MAX_PATH);
if (debugPath[0] != '\0') {
AddPath(debugPath);
}
}
// Search OPU subdirectories under 2) map dir, 3) mod dirs, 4) base dir, then 5) OPU dir.
// ** TODO For now mod dirs also get checked with highest priority by op2ext's hook
const auto& bases = GetBasePaths();
for (const auto& base : bases) {
static const std::multimap<std::string, std::string> assetDirs = {
{ ".ax", "libs" },
{ ".dll", "libs" },
{ ".dll", "maps" },
{ ".map", "maps" },
{ ".txt", "sheets" },
{ ".txt", "techs" },
{ ".txt", "story" },
{ ".rtf", "story" },
{ ".ttf", "ui" },
{ ".ttc", "ui" },
{ ".otf", "ui" },
{ ".png", "ui" },
{ ".bmp", "ui" },
{ ".bmp", "tilesets" },
{ ".bmp", "sprites" },
{ ".prt", "sprites" },
{ ".raw", "sprites" },
{ ".ani", "cursors" },
{ ".wav", "sounds" },
{ ".wav", "voices" },
{ ".wav", "music" },
{ ".mp3", "music" },
{ ".ogg", "music" },
{ ".flac", "music" },
{ ".avi", "movies" },
{ ".mp4", "movies" },
{ ".op2", "saves" }
};
const auto range = assetDirs.equal_range(extension);
for (auto it = range.first; it != range.second; ++it) {
const auto assetDir = base/it->second;
if (std::filesystem::exists(assetDir)) {
if (searchForMission && (it->first == ".dll") && (it->second == "maps")) {
// Search a) top level of [base]/maps/* subdirectories iff we are searching for mission DLLs.
// This lets e.g. OPU/base/maps/AxensHome/ml6_21.dll be found.
for (auto& p : std::filesystem::directory_iterator(assetDir, SearchOptions)) {
if (p.is_directory()) {
AddPath(p);
}
}
}
// Search b) [base]/[asset] directory.
AddPath(assetDir);
}
}
if (std::filesystem::exists(base)) {
// Search c) [base] directory.
AddPath(base);
}
}
// Search 6) all OPU/[base]/maps/* subdirectories if we are searching for .map files.
// This lets e.g. OPU/base/maps/misc/ademo1.dll refer to OPU/base/maps/campaign/eden04.map.
if (extension == ".map") {
for (const auto& base : bases) {
if (std::filesystem::exists(base/"maps")) {
for (auto& p : std::filesystem::recursive_directory_iterator(base/"maps", SearchOptions)) {
if (p.is_directory()) {
AddPath(p);
}
}
}
}
}
if (excludeStockDirs == false) {
// Search 7) Outpost2.exe directory, then 8) Outpost 2 CD directory if available.
AddPath(g_resManager.installedDir_);
AddPath(g_resManager.cdDir_);
}
return searchPaths;
}
// =====================================================================================================================
// Gets the path to a game file as per GetSearchPaths().
std::filesystem::path GetFilePath(
const std::filesystem::path& filename,
bool searchForMission)
{
constexpr auto SearchOptions = std::filesystem::directory_options::follow_directory_symlink |
std::filesystem::directory_options::skip_permission_denied;
static const auto opuPath(std::filesystem::path(g_resManager.installedDir_)/OPUDir);
std::filesystem::path path = filename;
bool result = filename.is_absolute() && std::filesystem::exists(filename);
if (filename.is_relative()) {
for (auto& searchPath : GetSearchPaths(filename.extension().string(), searchForMission)) {
path = searchPath/filename;
if (std::filesystem::exists(path)) {
result = true;
break;
}
}
}
return result ? path : std::filesystem::path();
}
// =====================================================================================================================
// Replacement function for ResManager::GetFilePath().
static ibool __fastcall GetFilePathHook(
ResManager* pThis, int,
char* pResName,
char* pOutputFilename)
{
const std::filesystem::path path = GetFilePath(pResName, g_searchForMission);
if (pOutputFilename != nullptr) {
pOutputFilename[0] = '\0';
if (path.empty() == false) {
strncpy_s(pOutputFilename, MAX_PATH, path.string().data(), _TRUNCATE);
}
}
return (path.empty() == false);
}
// =====================================================================================================================
// Gets the list of mission DLLs matching the specified type(s) and player count.
static MissionList GetMissionList(
MissionType minMissionType,
MissionType maxMissionType,
int maxPlayers = 0)
{
constexpr auto SearchOptions = std::filesystem::directory_options::follow_directory_symlink |
std::filesystem::directory_options::skip_permission_denied;
if (minMissionType > maxMissionType) {
std::swap(minMissionType, maxMissionType);
}
std::unordered_set<std::string> tested;
MissionList missions;
for (const auto& searchPath : GetSearchPaths(".dll", true)) {
if (std::filesystem::exists(searchPath)) {
for (auto& p : std::filesystem::directory_iterator(searchPath, SearchOptions)) {
const auto& path = p.path();
if (path.has_extension() && (path.extension() == ".dll")) {
std::string filename = path.filename().string();
std::transform(filename.begin(), filename.end(), filename.begin(), ::tolower);
if (tested.count(filename) == 0) {
AIModDesc*const pAIModDesc = MissionManager::GetModuleDesc(path.string().data());
if (pAIModDesc != nullptr) {
if ((pAIModDesc->descBlock.missionType >= minMissionType) &&
(pAIModDesc->descBlock.missionType <= maxMissionType) &&
(pAIModDesc->descBlock.numPlayers >= maxPlayers))
{
missions.emplace_back(
std::piecewise_construct,
std::forward_as_tuple(path.filename().string()),
std::forward_as_tuple(pAIModDesc, [](AIModDesc* p) { MissionManager::FreeModuleDesc(p); }));
}
else {
MissionManager::FreeModuleDesc(pAIModDesc);
}
}
tested.emplace(filename);
}
}
}
}
}
return missions;
}
// =====================================================================================================================
static MissionList GetMissionList(
MissionType missionType,
int maxPlayers = 0)
{
return GetMissionList(missionType, missionType, maxPlayers);
}
// =====================================================================================================================
static MissionList GetMissionList(
int maxPlayers = 0)
{
return GetMissionList(MissionType{INT32_MIN}, MissionType{INT32_MAX}, maxPlayers);
}
// =====================================================================================================================
// malloc using OP2Shell's heap.
static void* ShellAlloc(
size_t size)
{
static auto*const pfnAlloc =
reinterpret_cast<void*(CDECL*)(size_t)>(0x1300A320 - OP2ShellBase + uintptr(GetModuleHandleA("OP2Shell.dll")));
return pfnAlloc(size);
}
// =====================================================================================================================
// Replacement function for PopulateMultiplayerMissionList().
static int __fastcall PopulateMultiplayerMissionListHook(
HWND hComboBoxWnd,
int maxPlayers,
MissionType maxMissionType,
MissionType minMissionType)
{
SendMessageA(hComboBoxWnd, CB_RESETCONTENT, 0, 0);
SendMessageA(hComboBoxWnd, WM_SETREDRAW, 0, 0);
const auto missions = GetMissionList(minMissionType, maxMissionType, maxPlayers);
for (const auto& [filename, pAIModDesc] : missions) {
if (char*const pFilenameBuf = static_cast<char*>(OP2Alloc(filename.length() + 1)); pFilenameBuf != nullptr) {
strncpy_s(pFilenameBuf, filename.length() + 1, filename.data(), _TRUNCATE);
const LRESULT entry = SendMessageA(hComboBoxWnd, CB_ADDSTRING, 0, LPARAM(pAIModDesc->pLevelDesc));
SendMessageA(hComboBoxWnd, CB_SETITEMDATA, entry, LPARAM(pFilenameBuf));
}
}
SendMessageA(hComboBoxWnd, CB_SETCURSEL, 0, 0);
SendMessageA(hComboBoxWnd, WM_SETREDRAW, 1, 0);
return missions.size();
}
// =====================================================================================================================
// Replacement function for SinglePlayerGameDialog::PopulateMissionList().
static void __fastcall PopulateSinglePlayerMissionListHook(
IDlgWnd* pThis)
{
const HWND hListBoxWnd = GetDlgItem(pThis->hWnd_, 1076);
auto*const pFileFilter = static_cast<char*>(PtrInc(pThis, 0xAA)); // ** TODO Define SinglePlayerGameDialog
const MissionType type = (pFileFilter[0] == 't') ? MissionType::Tutorial : MissionType::Colony;
SendMessageA(hListBoxWnd, LB_RESETCONTENT, 0, 0);
SendMessageA(hListBoxWnd, WM_SETREDRAW, 0, 0);
for (const auto& [filename, pAIModDesc] : GetMissionList(type)) {
if (char*const pFilenameBuf = static_cast<char*>(ShellAlloc(filename.length() + 1)); pFilenameBuf != nullptr) {
strncpy_s(pFilenameBuf, filename.length() + 1, filename.data(), _TRUNCATE);
const LRESULT listEntry = SendMessageA(hListBoxWnd, LB_ADDSTRING, 0, LPARAM(pAIModDesc->pLevelDesc));
SendMessageA(hListBoxWnd, LB_SETITEMDATA, listEntry, LPARAM(pFilenameBuf));
}
}
SendMessageA(hListBoxWnd, LB_SETCURSEL, 0, 0);
SendMessageA(hListBoxWnd, WM_SETREDRAW, 1, 0);
}
// =====================================================================================================================
// Gets PATH environment variable.
static std::wstring GetPathEnv() {
std::unique_ptr<wchar_t[]> pTmp(new wchar_t[_MAX_ENV]);
pTmp[0] = L'\0';
GetEnvironmentVariableW(PathVar, pTmp.get(), _MAX_ENV);
return std::wstring(pTmp.get());
}
// =====================================================================================================================
// Adds DLL search paths.
// This does not use AddDllDirectory() because it's not WinXP-compatible, and multiple path search order is undefined.
// See https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order
void AddModuleSearchPaths(
const std::vector<std::filesystem::path>& paths,
bool ifUnique)
{
std::wstring pathEnv = GetPathEnv();
bool modified = false;
std::wstringstream ss(ifUnique ? pathEnv : L"");
for (auto& src: paths) {
const std::filesystem::path path = (src.is_relative() || src.empty()) ? (g_resManager.installedDir_/src) : src;
if (std::filesystem::exists(path)) {
bool found = false;
if (ifUnique) {
ss.seekg(0);
for (std::wstring token; ((found == false) && std::getline(ss, token, L';'));) {
found = (path == token) || ((path/"") == token);
}
}
if (found == false) {
pathEnv.insert(0, path.wstring() + (pathEnv.empty() ? L"" : L";"));
modified = true;
}
}
}
if (modified) {
SetEnvironmentVariableW(PathVar, pathEnv.data());
}
}
// =====================================================================================================================
// Removes DLL search paths.
void RemoveModuleSearchPaths(
const std::vector<std::filesystem::path>& paths)
{
std::wstring pathEnv = GetPathEnv();
bool modified = false;
std::wstringstream ss(pathEnv);
std::vector<std::pair<std::wstring::iterator, std::wstring::iterator>> toDelete;
for (auto& src: paths) {
const std::filesystem::path path = src.is_relative() ? (g_resManager.installedDir_/src) : src;
ss.seekg(0);
for (std::wstring token; std::getline(ss, token, L';');) {
if ((path == token) || ((path/"") == token)) {
auto end = pathEnv.begin() + ss.tellg();
toDelete.emplace_back(end - token.length(), (end != pathEnv.end()) ? (end + 1) : end);
modified = true;
}
}
for (auto it = toDelete.rbegin(); it != toDelete.rend(); ++it) {
pathEnv.erase(it->first, it->second);
}
}
if (modified) {
SetEnvironmentVariableW(PathVar, pathEnv.data());
}
}
// =====================================================================================================================
// Prefers loading DLL import dependencies from the new module's file directory first, instead of the base module's.
// Also adds module directory to PATH for the sake of LoadLibrary().
// See https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order
HMODULE WINAPI LoadLibraryAltSearchPath(
const char* pFilename)
{
std::filesystem::path path = GetFilePath(pFilename, g_searchForMission);
if (path.is_absolute()) {
AddModuleSearchPaths({ path.parent_path() }, true);
}
else {
path = pFilename;
}
return LoadLibraryExW(path.wstring().data(), NULL, path.is_absolute() ? LOAD_WITH_ALTERED_SEARCH_PATH : 0);
}
// =====================================================================================================================
void InitModuleSearchPaths() {
static std::vector<HMODULE> hModules;
const std::filesystem::path opuPath(OPUDir);
const std::filesystem::path base(opuPath/BaseDir);
// If this is called before TApp::Init(), then we need to init g_resManager's directories.
g_resManager.InitInstalledDir();
g_resManager.InitCDDir();
// Set current directory to [install dir]/OPU if current directory was set to default.
if ((std::filesystem::current_path()/"") == (std::filesystem::path(g_resManager.installedDir_)/"")) {
SetCurrentDirectoryW((g_resManager.installedDir_/opuPath).wstring().data());
}
// Add default DLL search paths.
AddModuleSearchPaths({ "", opuPath, opuPath/"libs" }, true); // ** TODO remove this, op2ext handles these
AddModuleSearchPaths({ base, base/"libs" });
const auto& modPaths = GetModPaths();
for (auto it = modPaths.rbegin(); it != modPaths.rend(); ++it) {
AddModuleSearchPaths({ *it, (*it)/"libs" }, true);
}
// OPUPatch needs to keep some DLLs loaded to patch them; load them here so they get loaded from the correct path.
if (hModules.empty()) {
for (const char* pFilename : { "OP2Shell.dll", "odasl.dll" }) {
if (const HMODULE hModule = LoadLibraryAltSearchPath(pFilename); hModule != NULL) {
hModules.push_back(hModule);
}
}
if (hModules.empty() == false) {
static const auto cleanup = atexit([] { for (HMODULE h : hModules) { FreeLibrary(h); } hModules.clear(); });
}
}
}
// =====================================================================================================================
// Hooks game file search path logic, allowing for loading files from context-based subdirs under the Outpost2/OPU dir.
bool SetFileSearchPathPatch(
bool enable)
{
static Patcher::PatchContext op2Patcher;
static Patcher::PatchContext shellPatcher;
static const std::wstring oldPathEnv(GetPathEnv());
static const auto oldCwd(std::filesystem::current_path());
static bool inited = false;
bool success = true;
if (enable) {
// Replace ResManager::GetFilePath()
op2Patcher.Hook(0x471590, &GetFilePathHook);
if (inited == false) {
InitModuleSearchPaths();
if (shellPatcher.GetModule() == GetModuleHandleA(nullptr)) {
shellPatcher.SetModule("OP2Shell.dll", true);
}
inited = true;
}
// Replace PopulateMultiplayerMissionList()
op2Patcher.Hook(0x497780, &PopulateMultiplayerMissionListHook);
// Replace {ColonyGameDialog,TutorialDialog}::PopulateMissionList()
shellPatcher.Hook(0x13006860, &PopulateSinglePlayerMissionListHook);
// In RunScriptDialog::DlgProc()
shellPatcher.LowLevelHook(0x13004A79, [](Ebx<IDlgWnd*> pThis) {
const HWND hListBoxWnd = GetDlgItem(pThis->hWnd_, 1076);
for (const auto& [filename, pAIModDesc] : GetMissionList()) {
SendMessageA(hListBoxWnd, LB_ADDSTRING, 0, LPARAM(filename.data()));
}
return 0x13004C38;
});
// In TethysGame::PrepareGame()
op2Patcher.HookCall(0x489433, SetCapturedTrampoline, ThiscallFunctor(
[F = (ibool(__thiscall*)(ResManager*, char*, char*))0](ResManager* pResManager, char* pResName, char* pOut) {
g_searchForMission = true;
const ibool result = F(pResManager, pResName, pOut);
g_searchForMission = false;
if (const std::filesystem::path path(result ? pOut : ""); path.is_absolute()) {
g_curMapPath = path.parent_path();
}
return result;
}));
static std::wstring preMissionPathEnv;
// In MissionManager::LoadScript()
op2Patcher.HookCall(0x402B44, StdcallLambdaPtr([](const char* pFilename) {
std::filesystem::path path(pFilename);
if (path.is_relative()) {
char buf[MAX_PATH] = "";
if (g_resManager.GetFilePath(pFilename, &buf[0])) {
path = buf;
}
}
path = std::filesystem::absolute(path);
g_curMapPath = path.parent_path();
preMissionPathEnv = GetPathEnv();
AddModuleSearchPaths({ g_curMapPath, g_curMapPath/"libs" }, true);
g_searchForMission = true;
const HMODULE hModule = LoadLibraryAltSearchPath(path.string().data());
g_searchForMission = false;
return hModule;
}));
// In MissionManager::Deinit()
op2Patcher.HookCall(0x402C2A, StdcallLambdaPtr([](HMODULE hModule) -> BOOL {
const BOOL result = FreeLibrary(hModule);
if (preMissionPathEnv.empty() == false) {
SetEnvironmentVariableW(PathVar, preMissionPathEnv.data());
preMissionPathEnv.clear();
}
g_curMapPath.clear();
return result;
}));
// In MissionManager::Load(), ChecksumScript(), MultiplayerLobbyDialog::SetControlInfo(), ???_460770()
for (const uintptr loc : { 0x402E4B, 0x45003C, 0x460B13, 0x46078C }) {
op2Patcher.HookCall(loc, SetCapturedTrampoline, ThiscallFunctor(
[F = (ibool(__thiscall*)(ResManager*, char*, char*))0](ResManager* pResManager, char* pResName, char* pOut) {
g_searchForMission = true;
const ibool result = F(pResManager, pResName, pOut);
g_searchForMission = false;
return result;
}));
}
// In StartSierraNW() (SierraNW.dll)
op2Patcher.HookCall(0x47D2B1, &LoadLibraryAltSearchPath);
// In StartSNWValid() (SNWValid.dll)
op2Patcher.LowLevelHook(0x47D56C, [](Ebp<decltype(&LoadLibraryA)>& pfn) { pfn = &LoadLibraryAltSearchPath; });
// In TApp::Init() (Out2res.dll)
op2Patcher.HookCall(0x485C76, &LoadLibraryAltSearchPath);
// In OP2Shell::Init() (op2shres.dll)
shellPatcher.LowLevelHook(0x13007B9D, [](Ebx<decltype(&LoadLibraryA)>& pfn) { pfn = &LoadLibraryAltSearchPath; });
// Try to load sounds from files instead of only from VOLs
// In SoundManager::Init()
op2Patcher.HookCall(0x47E027, ThiscallLambdaPtr([](ResManager* pThis, const char* pFilename, size_t* pSize) {
static std::map<std::string, MemoryMappedFile> soundData;
const std::string filename(pFilename);
auto it = soundData.find(filename);
bool found = (it != soundData.end()) && (it->second.pMappedAddress_ != nullptr);
if (found == false) {
const auto path = GetFilePath(filename);
found = (path.empty() == false) && soundData[filename].OpenFile(path.string().data(), false);
it = soundData.find(filename);
}
if (found) {
*pSize = it->second.size_;
}
return found ? it->second.pMappedAddress_ : pThis->LockStream(pFilename, pSize);
}));
// Look for saved games under OPU/saves
// In TFileDialog::???()
op2Patcher.HookCall(0x416F78, ThiscallLambdaPtr([](ResManager* pThis, char* pOut) {
static const auto savesPath(std::filesystem::path(g_resManager.installedDir_)/OPUDir/"saves"/"");
std::filesystem::create_directories(savesPath);
strncpy_s(pOut, MAX_PATH, savesPath.string().data(), _TRUNCATE);
}));
success = (op2Patcher.GetStatus() == PatcherStatus::Ok) && (shellPatcher.GetStatus() == PatcherStatus::Ok);
if (success) {
static const auto cleanup = atexit([] { SetFileSearchPathPatch(false); });
}
}
if ((enable == false) || (success == false)) {
if (inited) {
SetEnvironmentVariableW(PathVar, oldPathEnv.data());
std::error_code error;
std::filesystem::current_path(oldCwd, error);
inited = false;
}
success &= (op2Patcher.RevertAll() == PatcherStatus::Ok);
success &= (shellPatcher.RevertAll() == PatcherStatus::Ok);
}
return success;
}
// =====================================================================================================================
// Checksums a tech tree, factoring out non-breaking deltas like localization, definition order, whitespace, etc.
static uint32 ChecksumTech(
const char* pFilename)
{
uint32 checksum = 0;
// Get tech file contents (use OpenStream to search in VOLs too).
std::stringstream techFile;
if (auto*const pStream = g_resManager.OpenStream(pFilename); pStream != nullptr) {
for (char c = '\0'; pStream->Read(1, &c); techFile << c);
g_resManager.ReleaseStream(pStream);
}
// Use a sorted map, since OP2 sorts techs by ID. Map of TechID: list of normalized strings within BEGIN_TECH/END_TECH
std::map<int, std::vector<std::string>> techInfos;
int curTech = -1;
for (std::string l; std::getline(techFile, l);) {
// Strip side whitespace.
const size_t left = l.find_first_not_of(" \t\r");
const size_t right = l.find_last_not_of(" \t\r");
const std::string_view line((left != std::string::npos) ? (l.begin() + left) : l.end(),
(right != std::string::npos) ? (l.begin() + right + 1) : l.end());
if (line.empty() || (line[0] == ';')) {
// Ignore blank lines and comments.
continue;
}
else if (curTech == -1) {
if (line.find("BEGIN_TECH") == 0) {
// Extract tech ID, ignore name string.
if (size_t pos = line.find_last_of(" \t"); pos != std::string::npos) {
const auto [p, ec] = std::from_chars(line.data() + pos + 1, line.data() + line.size(), curTech);
}
}
}
else {
if (line.find("END_TECH") == 0) {
curTech = -1;
}
else if ((line.find("TEASER") == 0) || (line.find("DESCRIPTION") == 0)) {
// Ignore TEASER and DESCRIPTION to factor out localization.
continue;
}
else if (line.find("IMPROVE_DESC") == 0) {
// Include the IMPROVE_DESC token in the checksum since it has side effects, but ignore the string value.
techInfos[curTech].emplace_back("IMPROVE_DESC");
}
else {
// Normalize whitespace in inner parts of line.
for (size_t pos = 0; ((pos = l.find("\t", pos)) != std::string::npos); l[pos++] = ' ');
const auto end = std::copy_if(
line.begin(), line.end(), l.begin(), [](const char& c) { return (c != ' ') || ((&c)[1] != ' '); });
techInfos[curTech].emplace_back(l.begin(), end);
}
}
}
// Checksum the normalized tech definition contents.
for (auto& [techID, techInfo] : techInfos) {
std::sort(techInfo.begin(), techInfo.end());
checksum ^= TApp::Checksum(techID);
for (const auto& line : techInfo) {
checksum ^= TApp::ChecksumData(line.data(), line.size());
}
}
return checksum;
}
// =====================================================================================================================
// Changes netplay game start checksum validation logic.
// ** TODO output checksums to log file when checksum mismatch occurs, for now there's debug messages
bool SetChecksumPatch(
bool enable)
{
static Patcher::PatchContext patcher;
bool success = true;
if (enable) {
// Reimplement ChecksumScript()
patcher.Hook(0x44FFE0, FastcallLambdaPtr([](int pOut[14], const char* pFilename) -> ibool {
static const uint32 op2ExtChecksum = g_resManager.ChecksumStream("op2ext.dll");
static const uint32 opuPatchChecksum = g_resManager.ChecksumStream("OPUPatch.dll");
AIModDesc* pAIModDesc = nullptr;
if (const std::filesystem::path scriptPath = GetFilePath(pFilename, true); scriptPath.empty() == false) {
pAIModDesc = MissionManager::GetModuleDesc(scriptPath.string().data());
if (scriptPath.is_absolute()) {
g_curMapPath = scriptPath.parent_path();
}
}
int i = 0;
bool result = true;
auto AddChecksum = [pOut, &result, &i](uint32 checksum, const char* pName = nullptr) {
pOut[i++] = checksum; result &= (checksum != 0); DEBUG_MSG("%s checksum = %08X", pName ? pName : "", checksum);
};
// Checksum sheets
for (const char* p : { "building.txt", "mines.txt", "morale.txt", "space.txt", "vehicles.txt", "weapons.txt" }) {
AddChecksum(g_resManager.ChecksumStream(p), p);
}
// Normally, edentek.txt, ply_tek.txt, and multitek.txt get checksummed here.
// Use these 3 slots instead for the actual map's tech checksum, op2ext.dll, and OPUPatch.dll.
AddChecksum((pAIModDesc != nullptr) ? ChecksumTech(pAIModDesc->pTechtreeName) : 0,
(pAIModDesc != nullptr) ? pAIModDesc->pTechtreeName : "Invalid script's tech");
AddChecksum(op2ExtChecksum, "op2ext.dll");
AddChecksum(opuPatchChecksum, "OPUPatch.dll");
// Checksum OP2Shell.dll (seems pointless, multiplayer setup dialogs are all handled inside Outpost2.exe)
AddChecksum(g_tApp.ChecksumShell(), "OP2Shell.dll");
// Checksum Outpost2.exe (seems pointless as long as this is a const, maybe we can reuse this for something else)
AddChecksum(0x59010E28, "Outpost2.exe");
// Checksum mission script
AddChecksum((pAIModDesc != nullptr) ? pAIModDesc->checksum : 0, pFilename);
// Checksum map file
AddChecksum((pAIModDesc != nullptr) ? g_resManager.ChecksumStream(pAIModDesc->pMapName) : 0,
(pAIModDesc != nullptr) ? pAIModDesc->pMapName : "Invalid script's map");
// Overall checksum
AddChecksum(TApp::ChecksumData(pOut, sizeof(int) * i), "Overall");
assert(i == 14);
if (pAIModDesc != nullptr) {
MissionManager::FreeModuleDesc(pAIModDesc);
}
return result;
}));
success = (patcher.GetStatus() == PatcherStatus::Ok);
}
if ((enable == false) || (success == false)) {
success &= (patcher.RevertAll() == PatcherStatus::Ok);
}
return success;
}
// =====================================================================================================================
// Locally injects the Indeo 4.1 codec needed for game cutscenes if it is not registered in the OS.
bool SetCodecFix(
bool enable)
{
constexpr DWORD IndeoFourCC = mmioFOURCC('i', 'v', '4', '1');
static bool useLocalIndeo = false;
bool success = true;
if (enable) {
// Check for Indeo 4.1 codec and install as a function if missing.
if (useLocalIndeo == false) {
ICINFO icInfo;
const bool indeo41Present = ICInfo(ICTYPE_VIDEO, IndeoFourCC, &icInfo);
if (indeo41Present == false) {
static const HMODULE hMod = LoadLibraryAltSearchPath("ir41_32.ax");
static auto*const pfnDriverProc = (hMod != NULL) ? DRIVERPROC(GetProcAddress(hMod, "DriverProc")) : nullptr;
static const auto cleanup = (hMod != NULL) ? atexit([] { FreeLibrary(hMod); }) : 0;
// Work around some issues that cause the Indeo codec to crash when registered as a function via ICInstall().
auto IndeoDriverProc = [](DWORD_PTR dwDriverId, HDRVR hDrvr, UINT msg, LONG lParam1, LONG lParam2) {
LRESULT result = 0;
// When registered as a function, the codec crashes if ICM_GETINFO is sent, so we handle that ourselves here.
// This is also the first message sent, so we send the real codec DRV_LOAD and DRV_ENABLE messages as would
// get sent otherwise (for some reason they aren't sent to function-based codecs).
if (msg == ICM_GETINFO) {
if ((pfnDriverProc(dwDriverId, hDrvr, DRV_LOAD, lParam1, lParam2) != 0) &&
(pfnDriverProc(dwDriverId, hDrvr, DRV_ENABLE, lParam1, lParam2) != 0) &&
(lParam2 >= sizeof(ICINFO)))
{
auto*const pInfo = (ICINFO*)(lParam1);
pInfo->dwSize = sizeof(ICINFO);
pInfo->fccType = ICTYPE_VIDEO;
pInfo->fccHandler = IndeoFourCC;
pInfo->dwFlags = VIDCF_DRAW;
pInfo->dwVersion = 0;
pInfo->dwVersionICM = ICVERSION;
pInfo->szName[0] = '\0';
pInfo->szDescription[0] = '\0';
result = sizeof(ICINFO);
}
}
else {
result = pfnDriverProc(dwDriverId, hDrvr, msg, lParam1, lParam2);
}
return result;
};
useLocalIndeo = (pfnDriverProc != NULL) && ICInstall(
ICTYPE_VIDEO, IndeoFourCC, LPARAM(StdcallLambdaPtr(IndeoDriverProc)), nullptr, ICINSTALL_FUNCTION);
success &= useLocalIndeo;
}
}
if (success) {
static const auto cleanup = atexit([] { SetCodecFix(false); });
}
}
if (((enable == false) || (success == false)) && useLocalIndeo) {
useLocalIndeo = (ICRemove(ICTYPE_VIDEO, IndeoFourCC, 0) == false);
success &= (useLocalIndeo == false);
}
return true; // We currently don't treat this as fatal.
}