@@ -64,20 +64,20 @@ int SDK::internalVers = SDK::VLATEST;
64
64
using namespace std ;
65
65
66
66
namespace {
67
- std::shared_ptr< Eventing> g_eventing ;
68
- std::shared_ptr< Latite> g_latite ;
69
- std::shared_ptr< Renderer> g_renderer ;
70
- std::shared_ptr< ModuleManager> g_moduleManager ;
71
- std::shared_ptr< ClientMessageQueue> g_clientMessageQueue ;
72
- std::shared_ptr< CommandManager> g_commandManager ;
73
- std::shared_ptr< ConfigManager> g_configManager ;
74
- std::shared_ptr< SettingGroup> g_mainSettingGroup ;
75
- std::shared_ptr< LatiteHooks> g_hooks ;
76
- std::shared_ptr< ScreenManager> g_screenManager ;
77
- std::shared_ptr< Assets> g_assets ;
78
- std::shared_ptr< PluginManager> g_pluginManager ;
79
- std::shared_ptr< Keyboard> g_keyboard ;
80
- std::shared_ptr< Notifications> g_notifications ;
67
+ alignas ( Eventing) char eventing[ sizeof (Eventing)] = {} ;
68
+ alignas ( Latite) char latiteBuf[ sizeof (Latite)] = {} ;
69
+ alignas ( Renderer) char rendererBuf[ sizeof (Renderer)] = {} ;
70
+ alignas ( ModuleManager) char mmgrBuf[ sizeof (ModuleManager)] = {} ;
71
+ alignas ( ClientMessageQueue) char messageSinkBuf[ sizeof (ClientMessageQueue)] = {} ;
72
+ alignas ( CommandManager) char commandMgrBuf[ sizeof (CommandManager)] = {} ;
73
+ alignas ( ConfigManager) char configMgrBuf[ sizeof (ConfigManager)] = {} ;
74
+ alignas ( SettingGroup) char mainSettingGroup[ sizeof (SettingGroup)] = {} ;
75
+ alignas ( LatiteHooks) char hooks[ sizeof (LatiteHooks)] = {} ;
76
+ alignas ( ScreenManager) char scnMgrBuf[ sizeof (ScreenManager)] = {} ;
77
+ alignas ( Assets) char assetsBuf[ sizeof (Assets)] = {} ;
78
+ alignas ( PluginManager) char scriptMgrBuf[ sizeof (PluginManager)] = {} ;
79
+ alignas ( Keyboard) char keyboardBuf[ sizeof (Keyboard)] = {} ;
80
+ alignas ( Notifications) char notificaitonsBuf[ sizeof (Notifications)] = {} ;
81
81
82
82
bool hasInjected = false ;
83
83
}
@@ -95,11 +95,11 @@ namespace shared {
95
95
)()
96
96
97
97
DWORD __stdcall startThread (HINSTANCE dll) {
98
- g_clientMessageQueue = std::make_shared<ClientMessageQueue>(); // needed for Logger
99
- g_eventing = std::make_shared<Eventing>() ;
100
- g_latite = std::make_shared<Latite> ();
101
- g_notifications = std::make_shared<Notifications>() ;
102
- g_screenManager = std::make_shared<ScreenManager>(); // needs to be before renderer
98
+ // Needed for Logger
99
+ new (messageSinkBuf) ClientMessageQueue ;
100
+ new (eventing) Eventing ();
101
+ new (latiteBuf) Latite ;
102
+ new (notificaitonsBuf) Notifications;
103
103
104
104
std::filesystem::create_directory (util::GetLatitePath ());
105
105
std::filesystem::create_directory (util::GetLatitePath () / " Assets" );
@@ -258,14 +258,14 @@ DWORD __stdcall startThread(HINSTANCE dll) {
258
258
MVSIG (GuiData_displayClientMessage)
259
259
};
260
260
261
- g_configManager = std::make_shared< ConfigManager> ();
261
+ new (configMgrBuf) ConfigManager ();
262
262
if (!Latite::getConfigManager ().loadMaster ()) {
263
263
Logger::Fatal (XOR_STRING (" Could not load master config!" ));
264
264
}
265
265
else {
266
266
Logger::Info (XOR_STRING (" Loaded master config" ));
267
267
}
268
- g_mainSettingGroup = std::make_shared< SettingGroup> (" global" );
268
+ new (mainSettingGroup) SettingGroup (" global" );
269
269
270
270
// The Language setting is a special case because we need it to apply names to other global settings.
271
271
Latite::get ().initLanguageSetting ();
@@ -276,12 +276,13 @@ DWORD __stdcall startThread(HINSTANCE dll) {
276
276
277
277
Latite::get ().detectLanguage ();
278
278
279
- g_moduleManager = std::make_shared<ModuleManager>();
280
- g_commandManager = std::make_shared<CommandManager>();
279
+ new (mmgrBuf) ModuleManager;
280
+ new (commandMgrBuf) CommandManager;
281
+ new (scnMgrBuf) ScreenManager (); // needs to be initialized before renderer
281
282
282
- g_pluginManager = std::make_shared< PluginManager> ();
283
- g_renderer = std::make_shared< Renderer> ();
284
- g_assets = std::make_shared< Assets> ();
283
+ new (scriptMgrBuf) PluginManager ();
284
+ new (rendererBuf) Renderer ();
285
+ new (assetsBuf) Assets ();
285
286
286
287
for (auto & entry : sigList) {
287
288
if (!entry.first ->mod ) continue ;
@@ -302,17 +303,12 @@ DWORD __stdcall startThread(HINSTANCE dll) {
302
303
Logger::Info (" Resolved {} signatures ({} dead)" , sigCount, deadCount);
303
304
#endif
304
305
305
- MH_Initialize ();
306
- g_hooks = std::make_shared<LatiteHooks>();
307
-
308
- if (Signatures::KeyMap.result ) {
309
- g_keyboard = std::make_shared<Keyboard>(reinterpret_cast <int *>(Signatures::KeyMap.result ));
310
- } else {
311
- Logger::Fatal (" KeyMap signature failed to resolve, Keyboard cannot be initialized!" );
312
- }
306
+ new (hooks) LatiteHooks ();
313
307
308
+ new (keyboardBuf) Keyboard (reinterpret_cast <int *>(Signatures::KeyMap.result ));
314
309
315
310
Logger::Info (XOR_STRING (" Waiting for game to load.." ));
311
+
316
312
while (!SDK::ClientInstance::get ()) {
317
313
std::this_thread::sleep_for (10ms);
318
314
}
@@ -331,127 +327,106 @@ BOOL WINAPI DllMain(
331
327
if (GetModuleHandleA (" Minecraft.Windows.exe" ) != GetModuleHandleA (NULL )) return TRUE ;
332
328
333
329
if (fdwReason == DLL_PROCESS_ATTACH) {
330
+
334
331
if (hasInjected) {
335
332
FreeLibrary (hinstDLL);
336
333
return TRUE ;
337
334
}
335
+
338
336
hasInjected = true ;
339
- DisableThreadLibraryCalls (hinstDLL);
340
337
341
- HANDLE hThread = CreateThread (nullptr , 0 , (LPTHREAD_START_ROUTINE)startThread, hinstDLL, 0 , nullptr );
342
- if (hThread) {
343
- CloseHandle (hThread);
344
- }
345
- else {
346
- hasInjected = false ;
347
- return FALSE ;
348
- }
338
+ DisableThreadLibraryCalls (hinstDLL);
339
+ CloseHandle (CreateThread (nullptr , 0 , (LPTHREAD_START_ROUTINE)startThread, hinstDLL, 0 , nullptr ));
349
340
}
350
341
else if (fdwReason == DLL_PROCESS_DETACH) {
351
- Logger::Info ( " Detaching Latite Client... " );
342
+ // Remove singletons
352
343
353
- if (g_hooks) g_hooks-> disable ();
344
+ Latite::getHooks (). disable ();
354
345
355
- // sleep is not ideal i think
346
+ // Wait for all running hooks accross different threads to stop executing
356
347
std::this_thread::sleep_for (200ms);
357
348
358
- g_configManager-> saveCurrentConfig ();
359
-
360
- if (g_keyboard) g_keyboard. reset ();
361
- if (g_moduleManager) g_moduleManager. reset ();
362
- if (g_clientMessageQueue) g_clientMessageQueue. reset ();
363
- if (g_commandManager) g_commandManager. reset ();
364
- if (g_mainSettingGroup) g_mainSettingGroup. reset ();
365
- if (g_hooks) g_hooks. reset ();
366
- if (g_renderer) g_renderer. reset ();
367
- if (g_assets) g_assets. reset (); // this stops the rest from running for some reason idk why
368
- if (g_screenManager) g_screenManager. reset ();
369
- if (g_pluginManager) g_pluginManager. reset ();
370
- if (g_configManager) g_configManager. reset ();
371
- if (g_notifications) g_notifications. reset ();
372
- if (g_latite) g_latite. reset ();
349
+ Latite::getConfigManager (). saveCurrentConfig ();
350
+
351
+ Latite::getKeyboard (). ~Keyboard ();
352
+ Latite::getModuleManager (). ~ModuleManager ();
353
+ Latite::getClientMessageQueue (). ~ClientMessageQueue ();
354
+ Latite::getCommandManager (). ~CommandManager ();
355
+ Latite::getSettings (). ~SettingGroup ();
356
+ Latite::getHooks (). ~LatiteHooks ();
357
+ Latite::getEventing (). ~Eventing ();
358
+ Latite::getRenderer (). ~Renderer ();
359
+ Latite::getAssets (). ~Assets ();
360
+ Latite::getScreenManager (). ~ScreenManager ();
361
+ Latite::getPluginManager (). ~PluginManager ();
362
+ Latite::getNotifications (). ~Notifications ();
363
+ Latite::get (). ~Latite ();
373
364
374
365
MH_Uninitialize ();
375
366
376
367
hasInjected = false ;
377
368
Logger::Info (" Latite Client detached." );
378
369
}
379
- return TRUE ;
370
+ return TRUE ; // Successful DLL_PROCESS_ATTACH.
380
371
}
381
372
382
-
383
373
Latite& Latite::get () noexcept {
384
- assert (g_latite != nullptr && " Latite accessed before initialization!" );
385
- return *g_latite;
374
+ return *std::launder (reinterpret_cast <Latite*>(latiteBuf));
386
375
}
387
376
388
377
ModuleManager& Latite::getModuleManager () noexcept {
389
- assert (g_moduleManager != nullptr && " ModuleManager accessed before initialization!" );
390
- return *g_moduleManager;
378
+ return *std::launder (reinterpret_cast <ModuleManager*>(mmgrBuf));
391
379
}
392
380
393
381
CommandManager& Latite::getCommandManager () noexcept {
394
- assert (g_commandManager != nullptr && " CommandManager accessed before initialization!" );
395
- return *g_commandManager;
382
+ return *std::launder (reinterpret_cast <CommandManager*>(commandMgrBuf));
396
383
}
397
384
398
385
ConfigManager& Latite::getConfigManager () noexcept {
399
- assert (g_configManager != nullptr && " ConfigManager accessed before initialization!" );
400
- return *g_configManager;
386
+ return *std::launder (reinterpret_cast <ConfigManager*>(configMgrBuf));
401
387
}
402
388
403
389
ClientMessageQueue& Latite::getClientMessageQueue () noexcept {
404
- assert (g_clientMessageQueue != nullptr && " ClientMessageQueue accessed before initialization!" );
405
- return *g_clientMessageQueue;
390
+ return *std::launder (reinterpret_cast <ClientMessageQueue*>(messageSinkBuf));
406
391
}
407
392
408
393
SettingGroup& Latite::getSettings () noexcept {
409
- assert (g_mainSettingGroup != nullptr && " SettingGroup accessed before initialization!" );
410
- return *g_mainSettingGroup;
394
+ return *std::launder (reinterpret_cast <SettingGroup*>(mainSettingGroup));
411
395
}
412
396
413
397
LatiteHooks& Latite::getHooks () noexcept {
414
- assert (g_hooks != nullptr && " LatiteHooks accessed before initialization!" );
415
- return *g_hooks;
398
+ return *std::launder (reinterpret_cast <LatiteHooks*>(hooks));
416
399
}
417
400
418
401
Eventing& Latite::getEventing () noexcept {
419
- assert (g_eventing != nullptr && " Eventing accessed before initialization!" );
420
- return *g_eventing;
402
+ return *std::launder (reinterpret_cast <Eventing*>(eventing));
421
403
}
422
404
423
405
Renderer& Latite::getRenderer () noexcept {
424
- assert (g_renderer != nullptr && " Renderer accessed before initialization!" );
425
- return *g_renderer;
406
+ return *std::launder (reinterpret_cast <Renderer*>(rendererBuf));
426
407
}
427
408
428
409
ScreenManager& Latite::getScreenManager () noexcept {
429
- assert (g_screenManager != nullptr && " ScreenManager accessed before initialization!" );
430
- return *g_screenManager;
410
+ return *std::launder (reinterpret_cast <ScreenManager*>(scnMgrBuf));
431
411
}
432
412
433
413
Assets& Latite::getAssets () noexcept {
434
- assert (g_assets != nullptr && " Assets accessed before initialization!" );
435
- return *g_assets;
414
+ return *std::launder (reinterpret_cast <Assets*>(assetsBuf));
436
415
}
437
416
438
417
PluginManager& Latite::getPluginManager () noexcept {
439
- assert (g_pluginManager != nullptr && " PluginManager accessed before initialization!" );
440
- return *g_pluginManager;
418
+ return *std::launder (reinterpret_cast <PluginManager*>(scriptMgrBuf));
441
419
}
442
420
443
421
Keyboard& Latite::getKeyboard () noexcept {
444
- assert (g_keyboard != nullptr && " Keyboard accessed before initialization!" );
445
- return *g_keyboard;
422
+ return *std::launder (reinterpret_cast <Keyboard*>(keyboardBuf));
446
423
}
447
424
448
425
Notifications& Latite::getNotifications () noexcept {
449
- assert (g_notifications != nullptr && " Notifications accessed before initialization!" );
450
- return *g_notifications;
426
+ return *std::launder (reinterpret_cast <Notifications*>(notificaitonsBuf));
451
427
}
452
428
453
429
std::optional<float > Latite::getMenuBlur () {
454
- if (!g_latite) return std::nullopt;
455
430
if (std::get<BoolValue>(this ->menuBlurEnabled )) {
456
431
return std::get<FloatValue>(this ->menuBlur );
457
432
}
0 commit comments