From 22c0b5c867664a2524c18d865dbc54ff067dda22 Mon Sep 17 00:00:00 2001
From: Amos <48657826+Mauler125@users.noreply.github.com>
Date: Thu, 30 Dec 2021 17:20:41 +0100
Subject: [PATCH] Dedicated server improvements
---
r5dev/common/opcodes.cpp | 53 +++++++++++++++++++++----------
r5dev/common/opcodes.h | 8 ++++-
r5dev/core/dllmain.cpp | 3 ++
r5dev/core/init.cpp | 5 ---
r5dev/dedicated.vcxproj | 2 ++
r5dev/dedicated.vcxproj.filters | 6 ++++
r5dev/engine/debugoverlay.cpp | 15 ---------
r5dev/engine/debugoverlay.h | 26 ---------------
r5dev/launcher/IApplication.cpp | 4 +++
r5dev/r5dev.vcxproj | 4 +--
r5dev/r5dev.vcxproj.filters | 8 ++---
r5dev/windows/input.cpp | 4 +--
r5dev/windows/system.cpp | 56 +++++++++++++++++++++++++++++++++
r5dev/windows/system.h | 4 +++
14 files changed, 126 insertions(+), 72 deletions(-)
delete mode 100644 r5dev/engine/debugoverlay.cpp
delete mode 100644 r5dev/engine/debugoverlay.h
create mode 100644 r5dev/windows/system.cpp
create mode 100644 r5dev/windows/system.h
diff --git a/r5dev/common/opcodes.cpp b/r5dev/common/opcodes.cpp
index f4e62c36..da6a3ed8 100644
--- a/r5dev/common/opcodes.cpp
+++ b/r5dev/common/opcodes.cpp
@@ -50,6 +50,7 @@ void Dedicated_Init()
{
*(uintptr_t*)0x14D415040 = 0x1417304E8;
*(uintptr_t*)0x14B37C3C0 = 0x141F10CA0;
+
*(uintptr_t*)0x14B3800D7 = 0x1; // bDedicated
NoShaderApi_Init();
@@ -77,12 +78,13 @@ void Dedicated_Init()
// CENGINEAPI
//-------------------------------------------------------------------------
gCEngineAPI__Init.Offset(0xB7).Patch({ 0xE9, 0xC7, 0x00, 0x00, 0x00 }); // JNE --> JNP | Skip Video Mode validation code.
- gCEngineAPI__OnStartup.Offset(0x5E).Patch({ 0xE9, 0xC6, 0x01, 0x00, 0x00 }); // JNE --> JNP | Skip Video Mode initialization code.
- gCEngineAPI__Connect.Offset(0xDD).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | NOP call to texture and material preloading.
- gCEngineAPI__Connect.Offset(0xF1).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 }); // CAL --> NOP | NOP call to texture and material preloading.
- gCEngineAPI__Connect.Offset(0x1C6).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90 }); // CAL --> NOP | NOP call to texture and material preloading.
+
+ //gCEngineAPI__Connect.Offset(0x3E).Patch({ 0xE9, 0x8F, 0x01, 0x00, 0x00 }); // JE --> JMP | NOP call to texture and material preloading.
+ //gCEngineAPI__Connect.Offset(0xDD).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | NOP call to texture and material preloading.
+ //gCEngineAPI__Connect.Offset(0xF1).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 }); // CAL --> NOP | NOP call to texture and material preloading.
+ //gCEngineAPI__Connect.Offset(0x1C6).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90 }); // CAL --> NOP | NOP call to texture and material preloading.
//gCEngineAPI__ModInit.Offset(0x3DD).Patch({ 0xE9, 0xB5, 0x00, 0x00, 0x00, 0x00 }); // JNE --> JNP | Skip CreateWindow Initialization code.
- gCEngineAPI__ModInit.Offset(0x44C).Patch({ 0xEB, 0x49 }); // JNZ --> JMP | Skip CreateGameWindow validation code.
+ //gCEngineAPI__ModInit.Offset(0x44C).Patch({ 0xEB, 0x49 }); // JNZ --> JMP | Skip CreateGameWindow validation code.
//gCEngineAPI__ModInit.Offset(0x3DD).Patch({ 0xEB, 0x6D }); // JE --> JMP | Skip CreateGameWindow initialization code.
//-------------------------------------------------------------------------
@@ -105,10 +107,22 @@ void Dedicated_Init()
//-------------------------------------------------------------------------
// CSOURCEAPPSYSTEMGROUP
//-------------------------------------------------------------------------
- gCSourceAppSystemGroup__Create.Offset(0x2A5).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90}); // CAL --> NOP | studioRender->Connect().
- gCSourceAppSystemGroup__Create.Offset(0x35D).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | joystickInit?
- gCSourceAppSystemGroup__Create.Offset(0x384).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | PrecacheMaterial.
- gCSourceAppSystemGroup__Create.Offset(0x39E).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | binkBlankTexture.
+ gCSourceAppSystemGroup__Create.Offset(0x248).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | inputSystem->Connect().
+ gCSourceAppSystemGroup__Create.Offset(0x267).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | materials->Connect().
+ //gCSourceAppSystemGroup__Create.Offset(0x286).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | mdlCache->Connect().
+ gCSourceAppSystemGroup__Create.Offset(0x2A5).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | studioRender->Connect().
+ gCSourceAppSystemGroup__Create.Offset(0x2C4).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | avi->Connect().
+ gCSourceAppSystemGroup__Create.Offset(0x2E3).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | engineAPI->Connect().
+ //gCSourceAppSystemGroup__Create.Offset(0x302).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | dataCache->Connect().
+ gCSourceAppSystemGroup__Create.Offset(0x321).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | matSystemSurface->Connect().
+ gCSourceAppSystemGroup__Create.Offset(0x340).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | vgui->Connect().
+ gCSourceAppSystemGroup__Create.Offset(0x35D).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | inputSystem->Init().
+ gCSourceAppSystemGroup__Create.Offset(0x384).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | studioRender->Init().
+ gCSourceAppSystemGroup__Create.Offset(0x39E).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | bik->Init().
+ gCSourceAppSystemGroup__Create.Offset(0x3AB).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | engineAPI->Init().
+ gCSourceAppSystemGroup__Create.Offset(0x3F6).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | vgui->Init().
+ gCSourceAppSystemGroup__Create.Offset(0x3E9).Patch({ 0x90, 0x90, 0x90 }); // CAL --> NOP | matEmbeddedPanel->Init().
+ gCSourceAppSystemGroup__Create.Offset(0x3F9).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90 }); // CAL --> NOP | EAC_ClientInterface_Init().
//-------------------------------------------------------------------------
// CVIDEOMODE_COMMON
@@ -118,17 +132,17 @@ void Dedicated_Init()
//-------------------------------------------------------------------------
// CMATERIALSYSTEM
//-------------------------------------------------------------------------
- gCMaterialSystem__MatsysMode_Init.Offset(0x22).Patch({ 0xEB, 0x66 }); // JE --> JMP | Matsys mode init (CMaterialSystem).
-
- //-------------------------------------------------------------------------
- // CSHADERSYSTEM
- //-------------------------------------------------------------------------
- gCShaderSystem__9.Offset(0x3).Patch({ 0xE9, 0x95, 0x03, 0x00, 0x00 }); // Unnecessary CShaderSystem call?
+ //gCMaterialSystem__MatsysMode_Init.Offset(0x22).Patch({ 0xEB, 0x66 }); // JE --> JMP | Matsys mode init (CMaterialSystem). // TODO: Needed?
//-------------------------------------------------------------------------
// CSHADERGLUE
//-------------------------------------------------------------------------
- gCShaderGlue__Init.Patch({ 0xC3 }); // FUN --> RET | Skip ShaderSetup(). CShaderGlue.
+ //gCShaderGlue__Init.Patch({ 0xC3 }); // FUN --> RET | Skip ShaderSetup(). CShaderGlue.
+
+ //-------------------------------------------------------------------------
+ // RUNTIME: SYS_INITGAME
+ //-------------------------------------------------------------------------
+ Sys_InitGame.Offset(0x70).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 }); // STZNZ --> NOP | Prevent 'bDedicated' from being set to false.
//-------------------------------------------------------------------------
// RUNTIME: HOST_INIT
@@ -137,12 +151,17 @@ void Dedicated_Init()
gHost_Init_0.Offset(0x182).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90 }); // CAL --> JMP | Disable UI material asset initialization.
gHost_Init_0.Offset(0x859).Patch({ 0xE9, 0x19, 0x04, 0x00, 0x00 }); // LEA --> RET | Disable 'client.dll' library initialization.
gHost_Init_0.Offset(0xC77).Patch({ 0xE8, 0x44, 0xCF, 0xFF, 0xFF }); // CAL --> CAL | Disable user config loading and call entitlements.rson initialization instead.
- gHost_Init_1.Offset(0x19).Patch({ 0xEB, 0x6E }); // JNE --> JMP | Take dedicated initialization routine instead.
+
+
+ //gHost_Init_1.Offset(0x19).Patch({ 0xEB, 0x6E }); // JNE --> JMP | Take dedicated initialization routine instead. // REMOVE
gHost_Init_1.Offset(0x609).Patch({ 0xEB, 0x2B }); // JE --> JMP | Skip client.dll Init_PostVideo() validation code.
gHost_Init_1.Offset(0x621).Patch({ 0xEB, 0x0C }); // JNE --> JMP | Skip client.dll Init_PostVideo() validation code.
gHost_Init_1.Offset(0x658).Patch({ 0xE9, 0x8C, 0x00, 0x00, 0x00 }); // JE --> JMP | Skip NULL call as client is never initialized.
gHost_Init_1.Offset(0x6E9).Patch({ 0xE9, 0xB0, 0x00, 0x00, 0x00 }); // JNE --> JMP | Skip shader preloading as cvar can't be checked due to client being NULL.
+
+
//gHost_Init_2.Offset(0x5D8).Patch({ 0xEB, 0x05 }); // JE --> JMP | Render?
+ gHost_Init_2.Offset(0x26F).Patch({ 0xE9, 0x4D, 0x05, 0x00, 0x00 }); // JNE --> JMP | client.dll systems initialization.
//-------------------------------------------------------------------------
// RUNTIME: _HOST_RUNFRAME
diff --git a/r5dev/common/opcodes.h b/r5dev/common/opcodes.h
index be79719e..e35f4581 100644
--- a/r5dev/common/opcodes.h
+++ b/r5dev/common/opcodes.h
@@ -61,13 +61,19 @@ namespace
ADDRESS e4 = 0x0000000140BE1970;
ADDRESS e5 = 0x0000000140DBBAF0;
ADDRESS e6 = 0x0000000140DBE610;
- ADDRESS e7 = 0x000000014044AFA0;
ADDRESS e8 = 0x000000014027EC50; // RenderFrame?
ADDRESS gCEngineAPI__Init = 0x0000000140342FB0; //
ADDRESS gCEngineAPI__ModInit = 0x0000000140343DE0; //
ADDRESS gCEngineAPI__Connect = 0x0000000140342BA0; //
ADDRESS gCEngineAPI__OnStartup = 0x0000000140343860; //
+
+
ADDRESS gCSourceAppSystemGroup__Create = 0x000000014044AFA0; //
+
+
+ ADDRESS Sys_InitGame = 0x1402958D0;
+
+
ADDRESS CShaderSystem__Init = 0x00000001403DF870; //
ADDRESS gInitMaterialSystem = 0x000000014024B390; //
ADDRESS gCVideoMode_Common__DrawStartupGraphic = 0x000000014027F0F0; //
diff --git a/r5dev/core/dllmain.cpp b/r5dev/core/dllmain.cpp
index fd6f8e8e..c6627918 100644
--- a/r5dev/core/dllmain.cpp
+++ b/r5dev/core/dllmain.cpp
@@ -7,6 +7,7 @@
#include "windows/input.h"
#endif // !DEDICATED
#include "windows/console.h"
+#include "windows/system.h"
//#############################################################################
// INITIALIZATION
@@ -16,6 +17,7 @@ void R5Dev_Init()
{
Console_Init();
Systems_Init();
+ WinSys_Attach();
#ifndef DEDICATED
Input_Init();
@@ -32,6 +34,7 @@ void R5Dev_Init()
void R5Dev_Shutdown()
{
Systems_Shutdown();
+ WinSys_Detach();
#ifndef DEDICATED
Input_Shutdown();
diff --git a/r5dev/core/init.cpp b/r5dev/core/init.cpp
index 34091b0a..6415b139 100644
--- a/r5dev/core/init.cpp
+++ b/r5dev/core/init.cpp
@@ -35,9 +35,6 @@
#include "rtech/rtech_game.h"
#include "rtech/stryder.h"
#include "engine/baseclient.h"
-#ifndef DEDICATED
-#include "engine/debugoverlay.h"
-#endif // !DEDICATED
#include "engine/host_cmd.h"
#include "engine/host_state.h"
#include "engine/net_chan.h"
@@ -50,7 +47,6 @@
#endif // !DEDICATED
-
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// ██╗███╗ ██╗██╗████████╗██╗ █████╗ ██╗ ██╗███████╗ █████╗ ████████╗██╗ ██████╗ ███╗ ██╗
@@ -80,7 +76,6 @@ void Systems_Init()
CEngineVGui_Attach();
CFPSPanel_Attach();
CHLClient_Attach();
- DebugOverlays_Init();
#endif // !DEDICATED
CServer_Attach();
diff --git a/r5dev/dedicated.vcxproj b/r5dev/dedicated.vcxproj
index a5aa1bae..c8745802 100644
--- a/r5dev/dedicated.vcxproj
+++ b/r5dev/dedicated.vcxproj
@@ -324,6 +324,7 @@
+
@@ -371,6 +372,7 @@
+
diff --git a/r5dev/dedicated.vcxproj.filters b/r5dev/dedicated.vcxproj.filters
index b4c53d81..6c025bbc 100644
--- a/r5dev/dedicated.vcxproj.filters
+++ b/r5dev/dedicated.vcxproj.filters
@@ -564,6 +564,9 @@
sdk\rtech
+
+ windows
+
@@ -692,6 +695,9 @@
sdk\rtech
+
+ windows
+
diff --git a/r5dev/engine/debugoverlay.cpp b/r5dev/engine/debugoverlay.cpp
deleted file mode 100644
index 098bc61b..00000000
--- a/r5dev/engine/debugoverlay.cpp
+++ /dev/null
@@ -1,15 +0,0 @@
-#include "core/stdafx.h"
-#include "tier0/basetypes.h"
-#include "engine/debugoverlay.h"
-
-//-----------------------------------------------------------------------------
-// Purpose: enables 'DrawAllOverlays()'
-//-----------------------------------------------------------------------------
-void DebugOverlays_Init()
-{
-#if defined (GAMEDLL_S0) || defined (GAMEDLL_S1)
- p_DrawAllOverlays.Offset(0x189).Patch({ 0x83, 0x3F, 0x02 }); // Default value in memory is 0x2, condition is 0x4. Patch to fulfill condition.
-#elif defined (GAMEDLL_S2) || defined (GAMEDLL_S3)
- p_DrawAllOverlays.Offset(0x188).Patch({ 0x83, 0x3F, 0x02 }); // Default value in memory is 0x2, condition is 0x4. Patch to fulfill condition.
-#endif
-}
diff --git a/r5dev/engine/debugoverlay.h b/r5dev/engine/debugoverlay.h
deleted file mode 100644
index ffce292f..00000000
--- a/r5dev/engine/debugoverlay.h
+++ /dev/null
@@ -1,26 +0,0 @@
-#pragma once
-
-namespace
-{
-#if defined (GAMEDLL_S0) || defined (GAMEDLL_S1)
- ADDRESS p_DrawAllOverlays = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x40\x55\x48\x83\xEC\x50\x48\x8B\x05\x00\x00\x00\x00", "xxxxxxxxx????");
- void (*DrawAllOverlays)(char a1) = (void (*)(char))p_DrawAllOverlays.GetPtr(); /*40 55 48 83 EC 50 48 8B 05 ? ? ? ?*/
-#elif defined (GAMEDLL_S2) || defined (GAMEDLL_S3)
- ADDRESS p_DrawAllOverlays = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x40\x55\x48\x83\xEC\x30\x48\x8B\x05\x00\x00\x00\x00\x0F\xB6\xE9", "xxxxxxxxx????xxx");
- void (*DrawAllOverlays)(char a1) = (void (*)(char))p_DrawAllOverlays.GetPtr(); /*40 55 48 83 EC 30 48 8B 05 ? ? ? ? 0F B6 E9*/
-#endif
-}
-
-///////////////////////////////////////////////////////////////////////////////
-class HDebugOverlay : public IDetour
-{
- virtual void debugp()
- {
- std::cout << "| FUN: DrawAllOverlays : 0x" << std::hex << std::uppercase << p_DrawAllOverlays.GetPtr() << std::setw(npad) << " |" << std::endl;
- std::cout << "+----------------------------------------------------------------+" << std::endl;
- }
-};
-///////////////////////////////////////////////////////////////////////////////
-
-void DebugOverlays_Init();
-REGISTER(HDebugOverlay);
diff --git a/r5dev/launcher/IApplication.cpp b/r5dev/launcher/IApplication.cpp
index 5d5f5ee0..e545e12d 100644
--- a/r5dev/launcher/IApplication.cpp
+++ b/r5dev/launcher/IApplication.cpp
@@ -16,6 +16,10 @@ void* HIApplication_Main(void* a1, void* a2)
//-----------------------------------------------------------------------------
bool HIApplication_Create(void* a1)
{
+#ifdef DEDICATED
+ // TODO: Don't hardcode!
+ * (uintptr_t*)0x162C61208 = 0x1; // g_bDedicated
+#endif // DEDICATED
return IAppSystem_Create(a1);
}
diff --git a/r5dev/r5dev.vcxproj b/r5dev/r5dev.vcxproj
index 5962ff77..a584ca24 100644
--- a/r5dev/r5dev.vcxproj
+++ b/r5dev/r5dev.vcxproj
@@ -33,7 +33,6 @@
-
@@ -110,6 +109,7 @@
+
@@ -126,7 +126,6 @@
-
@@ -291,6 +290,7 @@
+
diff --git a/r5dev/r5dev.vcxproj.filters b/r5dev/r5dev.vcxproj.filters
index 9200efb0..20aa9f02 100644
--- a/r5dev/r5dev.vcxproj.filters
+++ b/r5dev/r5dev.vcxproj.filters
@@ -303,8 +303,8 @@
sdk\engine
-
- sdk\engine
+
+ windows
@@ -842,8 +842,8 @@
sdk\engine
-
- sdk\engine
+
+ windows
diff --git a/r5dev/windows/input.cpp b/r5dev/windows/input.cpp
index 9998e92f..4b7d8ec0 100644
--- a/r5dev/windows/input.cpp
+++ b/r5dev/windows/input.cpp
@@ -25,7 +25,7 @@ extern BOOL g_bBlockInput = false;
// INITIALIZATION
//#############################################################################
-void SetupIPHooks()
+void Input_Setup()
{
g_oSetCursorPos = (ISetCursorPos)DetourFindFunction("user32.dll", "SetCursorPos");
g_oClipCursor = (IClipCursor )DetourFindFunction("user32.dll", "ClipCursor" );
@@ -87,7 +87,7 @@ BOOL WINAPI HShowCursor(BOOL bShow)
void Input_Init()
{
- SetupIPHooks();
+ Input_Setup();
///////////////////////////////////////////////////////////////////////////
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
diff --git a/r5dev/windows/system.cpp b/r5dev/windows/system.cpp
new file mode 100644
index 00000000..b531a30e
--- /dev/null
+++ b/r5dev/windows/system.cpp
@@ -0,0 +1,56 @@
+#include "core/stdafx.h"
+#include "windows/system.h"
+
+///////////////////////////////////////////////////////////////////////////////
+typedef BOOL(WINAPI* IGetVersionExA)(_Inout_ LPOSVERSIONINFOA lpVersionInformation);
+static IGetVersionExA g_oGetVersionExA = nullptr;
+
+//#############################################################################
+// SYSTEM HOOKS
+//#############################################################################
+
+BOOL WINAPI HGetVersionExA(_Inout_ LPOSVERSIONINFOA lpVersionInformation)
+{
+#ifdef DEDICATED
+ // Return false for dedicated to skip 'SetPRocessDpiAwareness' in 'CEngineAPI:OnStartup()'.
+ return NULL;
+#else
+ return g_oGetVersionExA(lpVersionInformation);
+#endif // DEDICATED
+}
+
+//#############################################################################
+// MANAGEMENT
+//#############################################################################
+
+void WinSys_Init()
+{
+ g_oGetVersionExA = (IGetVersionExA)DetourFindFunction("KERNEL32.dll", "GetVersionExA");
+}
+
+void WinSys_Attach()
+{
+ WinSys_Init();
+ ///////////////////////////////////////////////////////////////////////////
+ DetourTransactionBegin();
+ DetourUpdateThread(GetCurrentThread());
+
+ ///////////////////////////////////////////////////////////////////////////
+ DetourAttach(&(LPVOID&)g_oGetVersionExA, (PBYTE)HGetVersionExA);
+
+ ///////////////////////////////////////////////////////////////////////////
+ DetourTransactionCommit();
+}
+
+void WinSys_Detach()
+{
+ ///////////////////////////////////////////////////////////////////////////
+ DetourTransactionBegin();
+ DetourUpdateThread(GetCurrentThread());
+
+ ///////////////////////////////////////////////////////////////////////////
+ DetourDetach(&(LPVOID&)g_oGetVersionExA, (PBYTE)HGetVersionExA);
+
+ ///////////////////////////////////////////////////////////////////////////
+ DetourTransactionCommit();
+}
diff --git a/r5dev/windows/system.h b/r5dev/windows/system.h
new file mode 100644
index 00000000..a1622c31
--- /dev/null
+++ b/r5dev/windows/system.h
@@ -0,0 +1,4 @@
+#pragma once
+
+void WinSys_Attach();
+void WinSys_Detach();