forked from tiltedphoques/TiltedEvolution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLauncher.cpp
151 lines (115 loc) · 3.82 KB
/
Launcher.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
#include <TiltedReverse/Code/reverse/include/Debug.hpp>
#include <BranchInfo.h>
#include "TargetConfig.h"
#include "launcher.h"
#include "loader/ExeLoader.h"
#include "loader/PathRerouting.h"
#include "Utils/Error.h"
#include "Utils/FileVersion.inl"
#include "oobe/PathSelection.h"
#include "oobe/SupportChecks.h"
#include "steam/SteamLoader.h"
#include "base/dialogues/win/TaskDialog.h"
// These symbols are defined within the client code skyrimtogetherclient
extern void InstallStartHook();
extern void RunTiltedApp();
extern void RunTiltedInit(const std::filesystem::path& acGamePath, const TiltedPhoques::String& aExeVersion);
// Defined in EarlyLoad.dll
bool __declspec(dllimport) EarlyInstallSucceeded();
HICON g_SharedWindowIcon = nullptr;
namespace launcher
{
static LaunchContext* g_context = nullptr;
LaunchContext* GetLaunchContext()
{
#if 0
if (!g_context)
__debugbreak();
#endif
return g_context;
}
// Everything is nothing, life is worth living, just look to the stars
#define DIE_NOW(err) \
{ \
Die(err); \
return false; \
}
void SetMaxstdio()
{
const auto handle = GetModuleHandleW(L"API-MS-WIN-CRT-STDIO-L1-1-0.DLL");
if (!handle)
return;
const auto setmaxstdioFunc = reinterpret_cast<decltype(&_setmaxstdio)>(GetProcAddress(handle, "_setmaxstdio"));
if (!setmaxstdioFunc)
return;
setmaxstdioFunc(8192);
}
int StartUp(int argc, char** argv)
{
bool askSelect = (GetAsyncKeyState(VK_SPACE) & 0x8000);
for (int i = 1; i < argc; i++)
{
if (std::strcmp(argv[i], "-r") == 0)
askSelect = true;
}
// TODO(Force): Make some InitSharedResources func.
g_SharedWindowIcon = LoadIconW(GetModuleHandleW(nullptr), MAKEINTRESOURCEW(102));
#if (!IS_MASTER)
TiltedPhoques::Debug::CreateConsole();
#endif
SetMaxstdio();
if (!EarlyInstallSucceeded())
DIE_NOW(L"Early load install failed. Tell Force about this.");
auto LC = std::make_unique<LaunchContext>();
g_context = LC.get();
{
const wchar_t* ec = nullptr;
const auto status = oobe::ReportModCompatabilityStatus();
switch (status)
{
case oobe::CompatabilityStatus::kDX11Unsupported: ec = L"Device does not support DirectX 11"; break;
case oobe::CompatabilityStatus::kOldOS: ec = L"Operating system unsupported. Please upgrade to Windows 8.1 or greater"; break;
}
if (ec)
DIE_NOW(ec);
}
if (!oobe::SelectInstall(askSelect))
DIE_NOW(L"Failed to select game install.");
// Bind path environment.
loader::InstallPathRouting(LC->gamePath);
steam::Load(LC->gamePath);
if (!LoadProgram(*LC))
return 3;
InstallStartHook();
// Initialize all hooks before calling game init
// TiltedPhoques::Initializer::RunAll();
RunTiltedInit(LC->gamePath, LC->Version);
// This shouldn't return until the game is killed
LC->gameMain();
return 0;
}
bool LoadProgram(LaunchContext& LC)
{
auto content = TiltedPhoques::LoadFile(LC.exePath);
if (content.empty())
DIE_NOW(L"Failed to mount game executable");
LC.Version = QueryFileVersion(LC.exePath.c_str());
if (LC.Version.empty())
DIE_NOW(L"Failed to query game version");
ExeLoader loader(CurrentTarget.exeLoadSz);
if (!loader.Load(reinterpret_cast<uint8_t*>(content.data())))
DIE_NOW(L"Fatal error while mapping executable");
LC.gameMain = loader.GetEntryPoint();
return true;
}
void InitClient()
{
// Jump into client code.
RunTiltedApp();
}
} // namespace launcher
// CreateProcess in suspended mode.
// Inject usvfs_64.dll -> invoke InitHooks
// (https://github.com/ModOrganizer2/usvfs/blob/f8051c179dee114b7e06c5dab2482977c285d611/src/usvfs_dll/usvfs.cpp#L352)
// Resume proc
// InjectDLLRemoteThread ->SkipInit