66#include < iostream>
77#include < print>
88#include < thread>
9- #include < shlobj_core.h>
109
1110#include < winrt/windows.applicationmodel.h>
1211#include < winrt/windows.foundation.h>
1312#include < winrt/windows.storage.h>
1413
1514#include < runtime.hpp>
1615
17- std::thread::id mc_thread_id;
18- HANDLE mc_thread_handle = nullptr ;
16+ #include < platform/windows.hpp>
1917
20- DWORD WINAPI SelauraRuntimeLoaderProc () {
21- AllocConsole () ;
18+ std::thread::id mcThreadId;
19+ HANDLE mcThreadHandle = nullptr ;
2220
23- AttachConsole (GetCurrentProcessId ());
24- SetConsoleTitleA (" Selaura Runtime Console" );
21+ DWORD WINAPI SelauraRuntimeLoaderProc () {
22+ auto platform = std::make_unique<Selaura::WindowsPlatform>(mcThreadHandle);
23+ auto * ctx = new Selaura::RuntimeContext;
24+ ctx->mPlatform = std::move (platform);
2525
26- FILE* fp;
27- freopen_s (&fp, " CONOUT$" , " w" , stdout);
28- freopen_s (&fp, " CONOUT$" , " w" , stderr);
29- freopen_s (&fp, " CONIN$" , " r" , stdin);
26+ ctx->mPlatform ->InitConsole ();
3027
31- std::println (" [Selaura Runtime Loader] Thread ID: {}, Thread Handle: {}" , mc_thread_id, mc_thread_handle );
28+ std::println (" [Selaura Runtime Loader] Thread ID: {}, Thread Handle: {}" , mcThreadId, mcThreadHandle );
3229 std::println (" [Selaura Runtime Loader] Press Numpad1 to End" );
3330
34- PWSTR appDataPath = nullptr ;
35- if (FAILED (SHGetKnownFolderPath (FOLDERID_RoamingAppData, 0 , nullptr , &appDataPath))) {
36- throw std::runtime_error (" Failed to get AppData path" );
37- }
38-
39- std::filesystem::path folder = appDataPath;
40- CoTaskMemFree (appDataPath);
41-
42- folder /= R"( Minecraft Bedrock\Users\Shared\games\com.mojang\Selaura)" ;
43-
44-
45- HANDLE hOut = GetStdHandle (STD_OUTPUT_HANDLE);
46- if (hOut != INVALID_HANDLE_VALUE) {
47- DWORD mode = 0 ;
48- if (GetConsoleMode (hOut, &mode)) {
49- SetConsoleMode (hOut, mode | 0x0004 );
50- }
51- }
52-
53-
54- auto * ctx = new Selaura::RuntimeContext;
31+ auto folder = ctx->mPlatform ->GetSelauraFolder ();
5532
5633 const auto runtime_path = folder / " selaura_runtime.dll" ;
5734 if (!std::filesystem::exists (runtime_path)) {
@@ -106,9 +83,9 @@ DWORD WINAPI SelauraRuntimeLoaderProc() {
10683 }
10784 };
10885
109- SuspendThread (mc_thread_handle );
86+ SuspendThread (mcThreadHandle );
11087
111- ctx->thread_id = mc_thread_id ;
88+ ctx->mThreadId = mcThreadId ;
11289
11390 DWORD handle = 0 ;
11491 wchar_t path[MAX_PATH];
@@ -121,10 +98,10 @@ DWORD WINAPI SelauraRuntimeLoaderProc() {
12198 VS_FIXEDFILEINFO* file_info = nullptr ;
12299 UINT len = 0 ;
123100 if (VerQueryValueW (buffer.data (), L" \\ " , reinterpret_cast <LPVOID*>(&file_info), &len) && file_info) {
124- ctx->version_major = HIWORD (file_info->dwFileVersionMS );
125- ctx->version_minor = LOWORD (file_info->dwFileVersionMS );
126- ctx->version_build = HIWORD (file_info->dwFileVersionLS );
127- ctx->version_revision = LOWORD (file_info->dwFileVersionLS );
101+ ctx->mVersionMajor = HIWORD (file_info->dwFileVersionMS );
102+ ctx->mVersionMinor = LOWORD (file_info->dwFileVersionMS );
103+ ctx->mVersionBuild = HIWORD (file_info->dwFileVersionLS );
104+ ctx->mVersionRevision = LOWORD (file_info->dwFileVersionLS );
128105 }
129106 }
130107 }
@@ -134,7 +111,7 @@ DWORD WINAPI SelauraRuntimeLoaderProc() {
134111 auto runtime_init = reinterpret_cast <runtime_init_fn>(GetProcAddress (mod, " SelauraRuntimeInit" ));
135112 runtime_init (ctx, load_mods);
136113
137- ResumeThread (mc_thread_handle );
114+ ResumeThread (mcThreadHandle );
138115 }
139116
140117 HWND hwnd = nullptr ;
@@ -196,9 +173,9 @@ DWORD WINAPI SelauraRuntimeLoaderProc() {
196173 (void )DwmSetWindowAttribute (hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &value_true, sizeof (value_true));
197174
198175 std::string title = std::format (" Selaura Runtime ({}.{}.{}/{}-{})" ,
199- ctx->version_major ,
200- ctx->version_minor ,
201- ctx->version_build ,
176+ ctx->mVersionMajor ,
177+ ctx->mVersionMinor ,
178+ ctx->mVersionBuild ,
202179 RUNTIME_VERSION,
203180 GIT_BRANCH
204181 );
@@ -218,7 +195,6 @@ DWORD WINAPI SelauraRuntimeLoaderProc() {
218195 if (GetAsyncKeyState (VK_NUMPAD1)) break ;
219196 }
220197
221- fclose (fp);
222198 FreeConsole ();
223199
224200 ExitProcess (0 );
@@ -229,8 +205,8 @@ DWORD WINAPI SelauraRuntimeLoaderProc() {
229205BOOL APIENTRY DllMain (HINSTANCE, DWORD fdwReason, LPVOID) {
230206 if (fdwReason == DLL_PROCESS_ATTACH) {
231207 proxy::init_runtime ();
232- mc_thread_id = std::this_thread::get_id ();
233- mc_thread_handle = OpenThread (THREAD_ALL_ACCESS, FALSE , GetCurrentThreadId ());
208+ mcThreadId = std::this_thread::get_id ();
209+ mcThreadHandle = OpenThread (THREAD_ALL_ACCESS, FALSE , GetCurrentThreadId ());
234210
235211 CreateThread (nullptr , 0 , (LPTHREAD_START_ROUTINE)SelauraRuntimeLoaderProc, nullptr , 0 , nullptr );
236212 }
0 commit comments