-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathmain.cpp
More file actions
292 lines (239 loc) · 8.76 KB
/
Copy pathmain.cpp
File metadata and controls
292 lines (239 loc) · 8.76 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
#include <stdafx.h>
#include <cpuid.h>
#include <cpu/guest_thread.h>
#include <gpu/video.h>
#include <kernel/function.h>
#include <kernel/memory.h>
#include <kernel/heap.h>
#include <kernel/xam.h>
#include <kernel/io/file_system.h>
#include <file.h>
#include <xex.h>
#include <apu/audio.h>
#include <hid/hid.h>
#include <user/config.h>
#include <user/paths.h>
#include <user/registry.h>
#include <kernel/xdbf.h>
#include <install/installer.h>
#include <install/update_checker.h>
#include <os/logger.h>
#include <os/process.h>
#include <os/registry.h>
#include <ui/game_window.h>
#include <ui/installer_wizard.h>
#include <mod/mod_loader.h>
#ifdef _WIN32
#include <timeapi.h>
#endif
#if defined(_WIN32) && defined(UNLEASHED_RECOMP_D3D12)
static std::array<std::string_view, 3> g_D3D12RequiredModules =
{
"D3D12/D3D12Core.dll",
"dxcompiler.dll",
"dxil.dll"
};
#endif
const size_t XMAIOBegin = 0x7FEA0000;
const size_t XMAIOEnd = XMAIOBegin + 0x0000FFFF;
Memory g_memory;
Heap g_userHeap;
XDBFWrapper g_xdbfWrapper;
std::unordered_map<uint16_t, GuestTexture*> g_xdbfTextureCache;
void HostStartup()
{
#ifdef _WIN32
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
#endif
g_userHeap.Init();
hid::Init();
}
// Name inspired from nt's entry point
void KiSystemStartup()
{
const auto gameContent = XamMakeContent(XCONTENTTYPE_RESERVED, "Game");
const auto updateContent = XamMakeContent(XCONTENTTYPE_RESERVED, "Update");
XamRegisterContent(gameContent, (g_gameInstallPath / "game").string());
XamRegisterContent(updateContent, (g_gameInstallPath / "update").string());
const auto saveFilePath = GetSaveFilePath(true);
bool saveFileExists = std::filesystem::exists(saveFilePath);
if (!saveFileExists)
{
// Copy base save data to modded save as fallback.
std::error_code ec;
std::filesystem::create_directories(saveFilePath.parent_path(), ec);
if (!ec)
{
std::filesystem::copy_file(GetSaveFilePath(false), saveFilePath, ec);
saveFileExists = !ec;
}
}
if (saveFileExists)
{
std::u8string savePathU8 = saveFilePath.parent_path().u8string();
XamRegisterContent(XamMakeContent(XCONTENTTYPE_SAVEDATA, "SYS-DATA"), (const char*)(savePathU8.c_str()));
}
// Mount game
XamContentCreateEx(0, "game", &gameContent, OPEN_EXISTING, nullptr, nullptr, 0, 0, nullptr);
XamContentCreateEx(0, "update", &updateContent, OPEN_EXISTING, nullptr, nullptr, 0, 0, nullptr);
// OS mounts game data to D:
XamContentCreateEx(0, "D", &gameContent, OPEN_EXISTING, nullptr, nullptr, 0, 0, nullptr);
std::error_code ec;
for (auto& file : std::filesystem::directory_iterator(g_gameInstallPath / "dlc", ec))
{
if (file.is_directory())
{
std::u8string fileNameU8 = file.path().filename().u8string();
std::u8string filePathU8 = file.path().u8string();
XamRegisterContent(XamMakeContent(XCONTENTTYPE_DLC, (const char*)(fileNameU8.c_str())), (const char*)(filePathU8.c_str()));
}
}
XAudioInitializeSystem();
}
uint32_t LdrLoadModule(const std::filesystem::path &path)
{
auto loadResult = LoadFile(path);
if (loadResult.empty())
{
assert("Failed to load module" && false);
return 0;
}
auto* header = reinterpret_cast<const Xex2Header*>(loadResult.data());
auto* security = reinterpret_cast<const Xex2SecurityInfo*>(loadResult.data() + header->securityOffset);
const auto* fileFormatInfo = reinterpret_cast<const Xex2OptFileFormatInfo*>(getOptHeaderPtr(loadResult.data(), XEX_HEADER_FILE_FORMAT_INFO));
auto entry = *reinterpret_cast<const uint32_t*>(getOptHeaderPtr(loadResult.data(), XEX_HEADER_ENTRY_POINT));
ByteSwapInplace(entry);
auto srcData = loadResult.data() + header->headerSize;
auto destData = reinterpret_cast<uint8_t*>(g_memory.Translate(security->loadAddress));
if (fileFormatInfo->compressionType == XEX_COMPRESSION_NONE)
{
memcpy(destData, srcData, security->imageSize);
}
else if (fileFormatInfo->compressionType == XEX_COMPRESSION_BASIC)
{
auto* blocks = reinterpret_cast<const Xex2FileBasicCompressionBlock*>(fileFormatInfo + 1);
const size_t numBlocks = (fileFormatInfo->infoSize / sizeof(Xex2FileBasicCompressionInfo)) - 1;
for (size_t i = 0; i < numBlocks; i++)
{
memcpy(destData, srcData, blocks[i].dataSize);
srcData += blocks[i].dataSize;
destData += blocks[i].dataSize;
memset(destData, 0, blocks[i].zeroSize);
destData += blocks[i].zeroSize;
}
}
else
{
assert(false && "Unknown compression type.");
}
auto res = reinterpret_cast<const Xex2ResourceInfo*>(getOptHeaderPtr(loadResult.data(), XEX_HEADER_RESOURCE_INFO));
g_xdbfWrapper = XDBFWrapper((uint8_t*)g_memory.Translate(res->offset.get()), res->sizeOfData);
return entry;
}
__attribute__((constructor(101), target("no-avx,no-avx2"), noinline))
void init()
{
#ifdef __x86_64__
uint32_t eax, ebx, ecx, edx;
// Execute CPUID for processor info and feature bits.
__get_cpuid(1, &eax, &ebx, &ecx, &edx);
// Check for AVX support.
if ((ecx & (1 << 28)) == 0)
{
printf("[*] CPU does not support the AVX instruction set.\n");
#ifdef _WIN32
MessageBoxA(nullptr, "Your CPU does not meet the minimum system requirements.", "Unleashed Recompiled", MB_ICONERROR);
#endif
std::_Exit(1);
}
#endif
}
int main(int argc, char *argv[])
{
#ifdef _WIN32
timeBeginPeriod(1);
#endif
os::process::CheckConsole();
if (!os::registry::Init())
LOGN_WARNING("OS does not support registry.");
os::logger::Init();
bool forceInstaller = false;
bool forceDLCInstaller = false;
const char *sdlVideoDriver = nullptr;
for (uint32_t i = 1; i < argc; i++)
{
forceInstaller = forceInstaller || (strcmp(argv[i], "--install") == 0);
forceDLCInstaller = forceDLCInstaller || (strcmp(argv[i], "--install-dlc") == 0);
if (strcmp(argv[i], "--sdl-video-driver") == 0)
{
if ((i + 1) < argc)
sdlVideoDriver = argv[++i];
else
LOGN_WARNING("No argument was specified for --sdl-video-driver. Option will be ignored.");
}
}
Config::Load();
#if defined(_WIN32) && defined(UNLEASHED_RECOMP_D3D12)
for (auto& dll : g_D3D12RequiredModules)
{
if (!std::filesystem::exists(g_executableRoot / dll))
{
char text[512];
snprintf(text, sizeof(text), Localise("System_Win32_MissingDLLs").c_str(), dll.data());
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, GameWindow::GetTitle(), text, GameWindow::s_pWindow);
std::_Exit(1);
}
}
#endif
// Check the time since the last time an update was checked. Store the new time if the difference is more than six hours.
constexpr double TimeBetweenUpdateChecksInSeconds = 6 * 60 * 60;
time_t timeNow = std::time(nullptr);
double timeDifferenceSeconds = difftime(timeNow, Config::LastChecked);
if (timeDifferenceSeconds > TimeBetweenUpdateChecksInSeconds)
{
UpdateChecker::initialize();
UpdateChecker::start();
Config::LastChecked = timeNow;
Config::Save();
}
if (Config::ShowConsole)
os::process::ShowConsole();
HostStartup();
std::filesystem::path modulePath;
bool isGameInstalled = Installer::checkGameInstall(g_gameInstallPath, modulePath);
bool runInstallerWizard = forceInstaller || forceDLCInstaller || !isGameInstalled;
if (runInstallerWizard)
{
if (!Video::CreateHostDevice(sdlVideoDriver))
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, GameWindow::GetTitle(), Localise("Video_BackendError").c_str(), GameWindow::s_pWindow);
std::_Exit(1);
}
if (!InstallerWizard::Run(g_gameInstallPath, isGameInstalled && forceDLCInstaller))
{
std::_Exit(0);
}
}
ModLoader::Init();
KiSystemStartup();
uint32_t entry = LdrLoadModule(modulePath);
if (!runInstallerWizard)
{
if (!Video::CreateHostDevice(sdlVideoDriver))
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, GameWindow::GetTitle(), Localise("Video_BackendError").c_str(), GameWindow::s_pWindow);
std::_Exit(1);
}
}
Video::StartPipelinePrecompilation();
GuestThread::Start({ entry, 0, 0 });
return 0;
}
GUEST_FUNCTION_STUB(__imp__vsprintf);
GUEST_FUNCTION_STUB(__imp___vsnprintf);
GUEST_FUNCTION_STUB(__imp__sprintf);
GUEST_FUNCTION_STUB(__imp___snprintf);
GUEST_FUNCTION_STUB(__imp___snwprintf);
GUEST_FUNCTION_STUB(__imp__vswprintf);
GUEST_FUNCTION_STUB(__imp___vscwprintf);
GUEST_FUNCTION_STUB(__imp__swprintf);