Optimize CMaterialSystem::Init() for dedicated

Hook 'CMaterialSystem::Init()', and only load the 'startup.rpak' file, as that's all we need. This pak file might also not even be needed on the server, but removing this requires rebuilding 'common_early.rpak' to drop all texture/material assets and their references. The dedicated server is now also linked to a newly added 'No-DirectX' version of the materialsystem for patching.
This commit is contained in:
Kawe Mazidjatari 2023-07-17 11:54:14 +02:00
parent e4a05b4a93
commit b84cfc2760
5 changed files with 68 additions and 22 deletions

View File

@ -102,6 +102,7 @@ target_link_libraries( ${PROJECT_NAME} PRIVATE
else()
target_link_libraries( ${PROJECT_NAME} PRIVATE
"engine_ds"
"materialsystem_nodx" # Needs the No-DirectX version for patching.
)
endif()
@ -114,6 +115,7 @@ elseif( ${PROJECT_NAME} STREQUAL "dedicated" )
end_sources()
target_compile_definitions( ${PROJECT_NAME} PRIVATE
"DEDICATED"
"MATERIALSYSTEM_NODX" # Needs the No-DirectX version for patching.
)
elseif( ${PROJECT_NAME} STREQUAL "client" )
end_sources( "${BUILD_OUTPUT_DIR}/bin/x64_retail/" )

View File

@ -440,9 +440,9 @@ void DetourRegister() // Register detour classes to be searched and hooked.
// StaticPropMgr
REGISTER(VStaticPropMgr);
#ifndef DEDICATED
// MaterialSystem
REGISTER(VMaterialSystem);
#ifndef DEDICATED
REGISTER(VMaterialGlue);
REGISTER(VShaderGlue);

View File

@ -19,3 +19,22 @@ add_sources( SOURCE_GROUP "Public"
)
end_sources()
add_module( "lib" "materialsystem_nodx" "vpc" ${FOLDER_CONTEXT} TRUE TRUE )
start_sources()
add_sources( SOURCE_GROUP "Private"
"cmaterialsystem.cpp"
"cmaterialsystem.h"
)
add_sources( SOURCE_GROUP "Public"
"${ENGINE_SOURCE_DIR}/public/imaterialsystem.h"
)
end_sources()
target_compile_definitions( ${PROJECT_NAME} PRIVATE
"MATERIALSYSTEM_NODX"
)

View File

@ -9,10 +9,32 @@
#include "vpc/keyvalues.h"
#include "rtech/rtech_utils.h"
#include "engine/cmodel_bsp.h"
#ifndef MATERIALSYSTEM_NODX
#include "materialsystem/cmaterialglue.h"
#endif // !MATERIALSYSTEM_NODX
#include "materialsystem/cmaterialsystem.h"
#ifndef DEDICATED
//-----------------------------------------------------------------------------
// Purpose: initialization of the material system
//-----------------------------------------------------------------------------
InitReturnVal_t CMaterialSystem::Init(CMaterialSystem* thisptr)
{
#ifdef MATERIALSYSTEM_NODX
// Only load the 'startup.rpak' file, as 'common_early.rpak' has assets
// that references assets in 'startup.rpak'.
RPakHandle_t pakHandle = g_pakLoadApi->LoadAsync("startup.rpak", AlignedMemAlloc(), 5);
g_pakLoadApi->WaitAsync(pakHandle);
// Trick: return INIT_FAILED to disable the loading of hardware
// configuration data, since we don't need it on the dedi.
return INIT_FAILED;
#else
// Initialize as usual.
return CMaterialSystem__Init(thisptr);
#endif
}
#ifndef MATERIALSYSTEM_NODX
//---------------------------------------------------------------------------------
// Purpose: loads and processes STBSP files
// (overrides level name if stbsp field has value in prerequisites file)
@ -60,9 +82,9 @@ void* __fastcall DispatchDrawCall(int64_t a1, uint64_t a2, int a3, int a4, int64
return v_DispatchDrawCall(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14);
#endif
}
#endif // !DEDICATED
#endif // !MATERIALSYSTEM_NODX
#ifndef DEDICATED
#ifndef MATERIALSYSTEM_NODX
//-----------------------------------------------------------------------------
// Purpose: finds a material
// Input : *pMatSys -
@ -96,23 +118,25 @@ Vector2D CMaterialSystem::GetScreenSize(CMaterialSystem* pMatSys)
return vecScreenSize;
}
#endif // !DEDICATED
#endif // !MATERIALSYSTEM_NODX
///////////////////////////////////////////////////////////////////////////////
void VMaterialSystem::Attach() const
{
#ifndef DEDICATED
DetourAttach((LPVOID*)&CMaterialSystem__Init, &CMaterialSystem::Init);
#ifndef MATERIALSYSTEM_NODX
DetourAttach((LPVOID*)&v_StreamDB_Init, &StreamDB_Init);
DetourAttach((LPVOID*)&v_DispatchDrawCall, &DispatchDrawCall);
DetourAttach((LPVOID*)&CMaterialSystem__FindMaterialEx, &CMaterialSystem::FindMaterialEx);
#endif // !DEDICATED
#endif // !MATERIALSYSTEM_NODX
}
void VMaterialSystem::Detach() const
{
#ifndef DEDICATED
DetourDetach((LPVOID*)&CMaterialSystem__Init, &CMaterialSystem::Init);
#ifndef MATERIALSYSTEM_NODX
DetourDetach((LPVOID*)&v_StreamDB_Init, &StreamDB_Init);
DetourDetach((LPVOID*)&v_DispatchDrawCall, &DispatchDrawCall);
DetourDetach((LPVOID*)&CMaterialSystem__FindMaterialEx, &CMaterialSystem::FindMaterialEx);
#endif // !DEDICATED
#endif // !MATERIALSYSTEM_NODX
}

View File

@ -7,19 +7,20 @@
class CMaterialSystem
{
public:
#ifndef DEDICATED
static InitReturnVal_t Init(CMaterialSystem* thisptr);
#ifndef MATERIALSYSTEM_NODX
static CMaterialGlue* FindMaterialEx(CMaterialSystem* pMatSys, const char* pMaterialName, uint8_t nMaterialType, int nUnk, bool bComplain);
static Vector2D GetScreenSize(CMaterialSystem* pMatSys = nullptr);
#endif // !DEDICATED
#endif // !MATERIALSYSTEM_NODX
};
/* ==== MATERIALSYSTEM ================================================================================================================================================== */
inline CMemory p_CMaterialSystem__Init;
inline void*(*CMaterialSystem__Init)(CMaterialSystem* thisptr);
inline InitReturnVal_t(*CMaterialSystem__Init)(CMaterialSystem* thisptr);
inline void* g_pMaterialSystem = nullptr;
inline void* g_pMaterialVFTable = nullptr;
#ifndef DEDICATED
#ifndef MATERIALSYSTEM_NODX
inline CMemory p_CMaterialSystem__FindMaterialEx;
inline CMaterialGlue*(*CMaterialSystem__FindMaterialEx)(CMaterialSystem* pMatSys, const char* pMaterialName, uint8_t nMaterialType, int nUnk, bool bComplain);
@ -44,7 +45,7 @@ inline CMemory s_pRenderContext;
inline int* g_nTotalStreamingTextureMemory = nullptr;
inline int* g_nUnfreeStreamingTextureMemory = nullptr;
inline int* g_nUnusableStreamingTextureMemory = nullptr;
#endif // !DEDICATED
#endif // !MATERIALSYSTEM_NODX
///////////////////////////////////////////////////////////////////////////////
class VMaterialSystem : public IDetour
@ -53,7 +54,7 @@ class VMaterialSystem : public IDetour
{
LogConAdr("CMaterial::`vftable'", reinterpret_cast<uintptr_t>(g_pMaterialVFTable));
LogFunAdr("CMaterialSystem::Init", p_CMaterialSystem__Init.GetPtr());
#ifndef DEDICATED
#ifndef MATERIALSYSTEM_NODX
LogFunAdr("CMaterialSystem::FindMaterialEx", p_CMaterialSystem__FindMaterialEx.GetPtr());
LogFunAdr("CMaterialSystem::GetScreenSize", p_CMaterialSystem_GetScreenSize.GetPtr());
LogFunAdr("CMaterialSystem::DispatchDrawCall", p_DispatchDrawCall.GetPtr());
@ -63,14 +64,14 @@ class VMaterialSystem : public IDetour
LogVarAdr("g_nUnfreeStreamingTextureMemory", reinterpret_cast<uintptr_t>(g_nUnfreeStreamingTextureMemory));
LogVarAdr("g_nUnusableStreamingTextureMemory", reinterpret_cast<uintptr_t>(g_nUnusableStreamingTextureMemory));
LogVarAdr("s_pRenderContext", s_pRenderContext.GetPtr());
#endif // !DEDICATED
#endif // !MATERIALSYSTEM_NODX
LogVarAdr("g_pMaterialSystem", reinterpret_cast<uintptr_t>(g_pMaterialSystem));
}
virtual void GetFun(void) const
{
p_CMaterialSystem__Init = g_GameDll.FindPatternSIMD("48 89 5C 24 ?? 55 56 57 41 54 41 55 41 56 41 57 48 83 EC 70 48 83 3D ?? ?? ?? ?? ??");
CMaterialSystem__Init = p_CMaterialSystem__Init.RCast<void*(*)(CMaterialSystem*)>(); /*48 89 5C 24 ?? 55 56 57 41 54 41 55 41 56 41 57 48 83 EC 70 48 83 3D ?? ?? ?? ?? ??*/
#ifndef DEDICATED
CMaterialSystem__Init = p_CMaterialSystem__Init.RCast<InitReturnVal_t(*)(CMaterialSystem*)>(); /*48 89 5C 24 ?? 55 56 57 41 54 41 55 41 56 41 57 48 83 EC 70 48 83 3D ?? ?? ?? ?? ??*/
#ifndef MATERIALSYSTEM_NODX
p_CMaterialSystem__FindMaterialEx = g_GameDll.FindPatternSIMD("44 89 4C 24 ?? 44 88 44 24 ?? 48 89 4C 24 ??");
CMaterialSystem__FindMaterialEx = p_CMaterialSystem__FindMaterialEx.RCast<CMaterialGlue*(*)(CMaterialSystem*, const char*, uint8_t, int, bool)>(); /*44 89 4C 24 ?? 44 88 44 24 ?? 48 89 4C 24 ??*/
@ -88,17 +89,17 @@ class VMaterialSystem : public IDetour
p_DrawStreamOverlay = g_GameDll.FindPatternSIMD("41 56 B8 ?? ?? ?? ?? E8 ?? ?? ?? ?? 48 2B E0 C6 02 ??");
v_DrawStreamOverlay = p_DrawStreamOverlay.RCast<const char*(*)(void*, uint8_t*, void*, void*)>(); // 41 56 B8 ?? ?? ?? ?? E8 ?? ?? ?? ?? 48 2B E0 C6 02 00 //
#endif // !DEDICATED
#endif // !MATERIALSYSTEM_NODX
}
virtual void GetVar(void) const
{
#ifndef DEDICATED
#ifndef MATERIALSYSTEM_NODX
g_nTotalStreamingTextureMemory = p_DrawStreamOverlay.Offset(0x1C).FindPatternSelf("48 8B 05", CMemory::Direction::DOWN).ResolveRelativeAddressSelf(0x3, 0x7).RCast<int*>();
g_nUnfreeStreamingTextureMemory = p_DrawStreamOverlay.Offset(0x2D).FindPatternSelf("48 8B 05", CMemory::Direction::DOWN).ResolveRelativeAddressSelf(0x3, 0x7).RCast<int*>();
g_nUnusableStreamingTextureMemory = p_DrawStreamOverlay.Offset(0x50).FindPatternSelf("48 8B 05", CMemory::Direction::DOWN).ResolveRelativeAddressSelf(0x3, 0x7).RCast<int*>();
s_pRenderContext = p_DispatchDrawCall.FindPattern("48 8B ?? ?? ?? ?? 01").ResolveRelativeAddressSelf(0x3, 0x7);
#endif // !DEDICATED
#endif // !MATERIALSYSTEM_NODX
g_pMaterialSystem = g_GameDll.FindPatternSIMD("48 8B 0D ?? ?? ?? ?? 48 85 C9 74 11 48 8B 01 48 8D 15 ?? ?? ?? ??").ResolveRelativeAddressSelf(0x3, 0x7).RCast<void*>();
}
virtual void GetCon(void) const
@ -110,4 +111,4 @@ class VMaterialSystem : public IDetour
};
///////////////////////////////////////////////////////////////////////////////
#endif // MATERIALSYSTEM_H
#endif // MATERIALSYSTEM_H