r5sdk/r5dev/core/dllmain.cpp

181 lines
4.5 KiB
C++
Raw Normal View History

#include "core/stdafx.h"
Code base refactor + major performance and readability improvement. Read description for details. * Codebase restructured to SourceSDK codebase style and .cpp/.h assertion paths in the game executable. * Document most functions with valve style 'Purpose' blocks. * Rename variables to match the rest of the codebase and Valve's naming convention. * Dedicated DLL and the SDKLauncher now share the same codebase as the DevSDK. * Obtain globals or pointers directly instead of waiting for runtime initialized data. * Dynamically search for all functions and globals (this doesn't count for dedicated yet!). * Initialize most in-SDK variables. * Move certain prints and other utilities under ConVars to reduce verbosity and increase performance. * Print all pattern scan results through a virtual function to make it easier to add and debug new patterns in the future. * Type global var pointers appropriately if class or type is known and implemented. * Forward declare 'CClient' class to avoid having 2 'g_pClient' copies. * Add IDA's pseudo definitions for easier prototyping with decompiled assembly code. * RPAK decompress Command callback implementation. * Load decompressed RPaks from 'paks\Win32\' overriding the ones in 'paks\Win64\' (the decompress callback will automatically fix the header and write it to 'paks\Win32\'). * VPK decompress Command callback implementation. * Move CRC32 ands Adler32 to implementation files. * Server will print out more details about the connecting client. * Upgrade ImGui lib to v1.86. * Don't compile id3dx.h for dedicated. * Don't compile id3dx.cpp for dedicated * Implement DevMsg print function allowing to print information to the in-game VGUI/RUI console overlay, ImGui console overlay and the external windows console * Fixed bug where the Error function would not properly terminate the process when an error is called. This caused access violations for critical/non-recoverable errors. * Fixed bug where the game would crash if the console or server browser was enabled while the game was still starting up. * Several bug fixes for the dedicated server (warning: dedicated is still considered work-in-progress!).
2021-12-25 22:36:38 +01:00
#include "core/r5dev.h"
#include "core/init.h"
#include "core/logdef.h"
#include "core/logger.h"
#include "tier0/basetypes.h"
#include "tier0/crashhandler.h"
#include "tier0/commandline.h"
Code base refactor + major performance and readability improvement. Read description for details. * Codebase restructured to SourceSDK codebase style and .cpp/.h assertion paths in the game executable. * Document most functions with valve style 'Purpose' blocks. * Rename variables to match the rest of the codebase and Valve's naming convention. * Dedicated DLL and the SDKLauncher now share the same codebase as the DevSDK. * Obtain globals or pointers directly instead of waiting for runtime initialized data. * Dynamically search for all functions and globals (this doesn't count for dedicated yet!). * Initialize most in-SDK variables. * Move certain prints and other utilities under ConVars to reduce verbosity and increase performance. * Print all pattern scan results through a virtual function to make it easier to add and debug new patterns in the future. * Type global var pointers appropriately if class or type is known and implemented. * Forward declare 'CClient' class to avoid having 2 'g_pClient' copies. * Add IDA's pseudo definitions for easier prototyping with decompiled assembly code. * RPAK decompress Command callback implementation. * Load decompressed RPaks from 'paks\Win32\' overriding the ones in 'paks\Win64\' (the decompress callback will automatically fix the header and write it to 'paks\Win32\'). * VPK decompress Command callback implementation. * Move CRC32 ands Adler32 to implementation files. * Server will print out more details about the connecting client. * Upgrade ImGui lib to v1.86. * Don't compile id3dx.h for dedicated. * Don't compile id3dx.cpp for dedicated * Implement DevMsg print function allowing to print information to the in-game VGUI/RUI console overlay, ImGui console overlay and the external windows console * Fixed bug where the Error function would not properly terminate the process when an error is called. This caused access violations for critical/non-recoverable errors. * Fixed bug where the game would crash if the console or server browser was enabled while the game was still starting up. * Several bug fixes for the dedicated server (warning: dedicated is still considered work-in-progress!).
2021-12-25 22:36:38 +01:00
/*****************************************************************************/
#ifndef DEDICATED
#include "windows/id3dx.h"
#include "windows/input.h"
#endif // !DEDICATED
#include "windows/console.h"
2021-12-30 17:20:41 +01:00
#include "windows/system.h"
#include "mathlib/mathlib.h"
#include "launcher/launcher.h"
#include "protobuf/stubs/common.h"
2021-04-13 04:45:22 -07:00
#ifndef DEDICATED
#define SDK_DEFAULT_CFG "cfg/system/startup_default.cfg"
#else
#define SDK_DEFAULT_CFG "cfg/system/startup_dedi_default.cfg"
#endif
bool g_bSdkInitialized = false;
HMODULE s_hModuleHandle = NULL;
2021-06-28 15:51:32 -07:00
//#############################################################################
// UTILITY
2021-06-28 15:51:32 -07:00
//#############################################################################
2021-04-13 04:45:22 -07:00
void Crash_Callback()
{
// Shutdown SpdLog to flush all buffers.
SpdLog_Shutdown();
// TODO[ AMOS ]: This is where we want to call backtrace from.
}
void Show_Emblem()
{
// Logged as 'SYSTEM_ERROR' for its red color.
for (size_t i = 0; i < SDK_ARRAYSIZE(R5R_EMBLEM); i++)
{
Msg(eDLL_T::SYSTEM_ERROR, "%s\n", R5R_EMBLEM[i]);
}
// Log the SDK's 'build_id' under the emblem.
Msg(eDLL_T::SYSTEM_ERROR,
2023-07-01 21:27:37 +02:00
"+------------------------------------------------[%s%010d%s]-+\n",
g_svYellowF, g_SDKDll.GetNTHeaders()->FileHeader.TimeDateStamp, g_svRedF);
Msg(eDLL_T::SYSTEM_ERROR, "\n");
}
//#############################################################################
// INITIALIZATION
//#############################################################################
void Tier0_Init()
{
#if !defined (DEDICATED)
g_RadVideoToolsDll.InitFromName("bink2w64.dll");
g_RadAudioDecoderDll.InitFromName("binkawin64.dll");
g_RadAudioSystemDll.InitFromName("mileswin64.dll");
#endif // !DEDICATED
g_CoreMsgVCallback = &EngineLoggerSink; // Setup logger callback sink.
g_pCmdLine->CreateCmdLine(GetCommandLineA());
g_CrashHandler.SetCrashCallback(&Crash_Callback);
// This prevents the game from recreating it,
// see 'CCommandLine::StaticCreateCmdLine' for
// more information.
g_bCommandLineCreated = true;
}
2022-08-29 14:31:05 +02:00
void SDK_Init()
2021-04-13 04:45:22 -07:00
{
CheckCPU(); // Check CPU as early as possible; error out if CPU isn't supported.
MathLib_Init(); // Initialize Mathlib.
PEB64* pEnv = CModule::GetProcessEnvironmentBlock();
g_GameDll.InitFromBase(pEnv->ImageBaseAddress);
g_SDKDll.InitFromBase((QWORD)s_hModuleHandle);
Tier0_Init();
if (!CommandLine()->CheckParm("-launcher"))
{
CommandLine()->AppendParametersFromFile(SDK_DEFAULT_CFG);
}
const bool bAnsiColor = CommandLine()->CheckParm("-ansicolor") ? true : false;
#ifndef DEDICATED
if (CommandLine()->CheckParm("-wconsole"))
#endif // !DEDICATED
{
Console_Init(bAnsiColor);
}
SpdLog_Init(bAnsiColor);
Show_Emblem();
Winsock_Startup(); // Initialize Winsock.
DirtySDK_Startup();
Code base refactor + major performance and readability improvement. Read description for details. * Codebase restructured to SourceSDK codebase style and .cpp/.h assertion paths in the game executable. * Document most functions with valve style 'Purpose' blocks. * Rename variables to match the rest of the codebase and Valve's naming convention. * Dedicated DLL and the SDKLauncher now share the same codebase as the DevSDK. * Obtain globals or pointers directly instead of waiting for runtime initialized data. * Dynamically search for all functions and globals (this doesn't count for dedicated yet!). * Initialize most in-SDK variables. * Move certain prints and other utilities under ConVars to reduce verbosity and increase performance. * Print all pattern scan results through a virtual function to make it easier to add and debug new patterns in the future. * Type global var pointers appropriately if class or type is known and implemented. * Forward declare 'CClient' class to avoid having 2 'g_pClient' copies. * Add IDA's pseudo definitions for easier prototyping with decompiled assembly code. * RPAK decompress Command callback implementation. * Load decompressed RPaks from 'paks\Win32\' overriding the ones in 'paks\Win64\' (the decompress callback will automatically fix the header and write it to 'paks\Win32\'). * VPK decompress Command callback implementation. * Move CRC32 ands Adler32 to implementation files. * Server will print out more details about the connecting client. * Upgrade ImGui lib to v1.86. * Don't compile id3dx.h for dedicated. * Don't compile id3dx.cpp for dedicated * Implement DevMsg print function allowing to print information to the in-game VGUI/RUI console overlay, ImGui console overlay and the external windows console * Fixed bug where the Error function would not properly terminate the process when an error is called. This caused access violations for critical/non-recoverable errors. * Fixed bug where the game would crash if the console or server browser was enabled while the game was still starting up. * Several bug fixes for the dedicated server (warning: dedicated is still considered work-in-progress!).
2021-12-25 22:36:38 +01:00
Systems_Init();
WinSys_Init();
#ifndef DEDICATED
Input_Init();
#endif // !DEDICATED
2023-06-18 01:16:58 +02:00
GOOGLE_PROTOBUF_VERIFY_VERSION;
curl_global_init(CURL_GLOBAL_ALL);
2023-06-18 01:16:58 +02:00
lzham_enable_fail_exceptions(true);
g_bSdkInitialized = true;
2021-04-13 04:45:22 -07:00
}
//#############################################################################
// SHUTDOWN
//#############################################################################
2022-08-29 14:31:05 +02:00
void SDK_Shutdown()
2021-04-13 04:45:22 -07:00
{
assert(g_bSdkInitialized);
if (!g_bSdkInitialized)
{
spdlog::error("Recursive shutdown!\n");
return;
}
g_bSdkInitialized = false;
Msg(eDLL_T::NONE, "GameSDK shutdown initiated\n");
2023-01-26 19:58:21 +01:00
curl_global_cleanup();
Code base refactor + major performance and readability improvement. Read description for details. * Codebase restructured to SourceSDK codebase style and .cpp/.h assertion paths in the game executable. * Document most functions with valve style 'Purpose' blocks. * Rename variables to match the rest of the codebase and Valve's naming convention. * Dedicated DLL and the SDKLauncher now share the same codebase as the DevSDK. * Obtain globals or pointers directly instead of waiting for runtime initialized data. * Dynamically search for all functions and globals (this doesn't count for dedicated yet!). * Initialize most in-SDK variables. * Move certain prints and other utilities under ConVars to reduce verbosity and increase performance. * Print all pattern scan results through a virtual function to make it easier to add and debug new patterns in the future. * Type global var pointers appropriately if class or type is known and implemented. * Forward declare 'CClient' class to avoid having 2 'g_pClient' copies. * Add IDA's pseudo definitions for easier prototyping with decompiled assembly code. * RPAK decompress Command callback implementation. * Load decompressed RPaks from 'paks\Win32\' overriding the ones in 'paks\Win64\' (the decompress callback will automatically fix the header and write it to 'paks\Win32\'). * VPK decompress Command callback implementation. * Move CRC32 ands Adler32 to implementation files. * Server will print out more details about the connecting client. * Upgrade ImGui lib to v1.86. * Don't compile id3dx.h for dedicated. * Don't compile id3dx.cpp for dedicated * Implement DevMsg print function allowing to print information to the in-game VGUI/RUI console overlay, ImGui console overlay and the external windows console * Fixed bug where the Error function would not properly terminate the process when an error is called. This caused access violations for critical/non-recoverable errors. * Fixed bug where the game would crash if the console or server browser was enabled while the game was still starting up. * Several bug fixes for the dedicated server (warning: dedicated is still considered work-in-progress!).
2021-12-25 22:36:38 +01:00
#ifndef DEDICATED
Input_Shutdown();
#endif // !DEDICATED
WinSys_Shutdown();
Systems_Shutdown();
DirtySDK_Shutdown();
Winsock_Shutdown();
SpdLog_Shutdown();
Console_Shutdown();
2021-04-13 04:45:22 -07:00
}
2021-06-28 15:51:32 -07:00
//#############################################################################
// ENTRYPOINT
//#############################################################################
BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
2021-04-13 04:45:22 -07:00
{
NOTE_UNUSED(lpReserved);
2021-04-13 04:45:22 -07:00
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
{
s_hModuleHandle = hModule;
2021-04-13 04:45:22 -07:00
break;
}
2021-04-13 04:45:22 -07:00
case DLL_PROCESS_DETACH:
{
s_hModuleHandle = NULL;
2021-04-13 04:45:22 -07:00
break;
}
2021-04-13 04:45:22 -07:00
}
return TRUE;
}