r5sdk/r5dev/game/server/gameinterface.cpp
Kawe Mazidjatari a618990937 Detour code refactor
This change was planned for a long time. This moves all REGISTER calls to a single translation unit, this is required as we currently added a very dirty workaround for not registering duplicates by checking if VFTable pointer was already present in the vector... Registering from single translation unit prevents duplicate instances that gets created if header is included by more cpp files.
Reworking this reduced 100kb+ of compiled code. This commit also reworked the way functions/variables/constant gets logged with their addresses; the new code formats them on the fly, and allows for resize at any time. Formatting is no longer required by programmer.

TODO: currently there are some compile errors for dedicated and client dll's. These will be resolved very soon as they need to be properly worked out still (server & client only stuff needs to be properly split). Use the 'main' (stable) branch for the time being if you need to compile these dll's.
2023-01-25 02:26:52 +01:00

145 lines
4.5 KiB
C++

//=============================================================================//
//
// Purpose: Implement things from GameInterface.cpp. Mostly the engine interfaces.
//
// $NoKeywords: $
//=============================================================================//
#include "core/stdafx.h"
#include "tier1/cvar.h"
#include "public/server_class.h"
#include "public/eiface.h"
#include "public/const.h"
#include "engine/server/sv_main.h"
#include "gameinterface.h"
#include "entitylist.h"
#include "baseanimating.h"
//-----------------------------------------------------------------------------
// This is called when a new game is started. (restart, map)
//-----------------------------------------------------------------------------
void CServerGameDLL::GameInit(void)
{
const static int index = 1;
CallVFunc<void>(index, this);
}
//-----------------------------------------------------------------------------
// This is called when scripts are getting recompiled. (restart, map, changelevel)
//-----------------------------------------------------------------------------
void CServerGameDLL::PrecompileScriptsJob(void)
{
const static int index = 2;
CallVFunc<void>(index, this);
}
//-----------------------------------------------------------------------------
// Called when a level is shutdown (including changing levels)
//-----------------------------------------------------------------------------
void CServerGameDLL::LevelShutdown(void)
{
const static int index = 8;
CallVFunc<void>(index, this);
}
//-----------------------------------------------------------------------------
// This is called when a game ends (server disconnect, death, restart, load)
// NOT on level transitions within a game
//-----------------------------------------------------------------------------
void CServerGameDLL::GameShutdown(void)
{
// Game just calls a nullsub for GameShutdown lol.
const static int index = 9;
CallVFunc<void>(index, this);
}
//-----------------------------------------------------------------------------
// Purpose: Gets the simulation tick interval
// Output : float
//-----------------------------------------------------------------------------
float CServerGameDLL::GetTickInterval(void)
{
const static int index = 11;
return CallVFunc<float>(index, this);
}
//-----------------------------------------------------------------------------
// Purpose: get all server classes
// Output : ServerClass*
//-----------------------------------------------------------------------------
ServerClass* CServerGameDLL::GetAllServerClasses(void)
{
const static int index = 12;
return CallVFunc<ServerClass*>(index, this);
}
void __fastcall CServerGameDLL::OnReceivedSayTextMessage(void* thisptr, int senderId, const char* text, bool isTeamChat)
{
#if defined(GAMEDLL_S3)
// set isTeamChat to false so that we can let the convar sv_forceChatToTeamOnly decide whether team chat should be enforced
// this isn't a great way of doing it but it works so meh
CServerGameDLL__OnReceivedSayTextMessage(thisptr, senderId, text, false);
#endif
}
void DrawServerHitboxes(bool bRunOverlays)
{
int nVal = sv_showhitboxes->GetInt();
Assert(nVal < NUM_ENT_ENTRIES);
if (nVal == -1)
return;
std::function<void(int)> fnLookupAndDraw = [&](int iEntity)
{
IHandleEntity* pEntity = LookupEntityByIndex(iEntity);
CBaseAnimating* pAnimating = dynamic_cast<CBaseAnimating*>(pEntity);
if (pAnimating)
{
pAnimating->DrawServerHitboxes();
}
};
if (nVal == 0)
{
for (int i = 0; i < NUM_ENT_ENTRIES; i++)
{
fnLookupAndDraw(i);
}
}
else // Lookup entity manually by index from 'sv_showhitboxes'.
{
fnLookupAndDraw(nVal);
}
}
void RunFrameServer(double flFrameTime, bool bRunOverlays, bool bUniformUpdate)
{
DrawServerHitboxes(bRunOverlays);
v_RunFrameServer(flFrameTime, bRunOverlays, bUniformUpdate);
}
void VServerGameDLL::Attach() const
{
#if defined(GAMEDLL_S3)
DetourAttach((LPVOID*)&CServerGameDLL__OnReceivedSayTextMessage, &CServerGameDLL::OnReceivedSayTextMessage);
#endif
DetourAttach(&v_RunFrameServer, &RunFrameServer);
}
void VServerGameDLL::Detach() const
{
#if defined(GAMEDLL_S3)
DetourDetach((LPVOID*)&CServerGameDLL__OnReceivedSayTextMessage, &CServerGameDLL::OnReceivedSayTextMessage);
#endif
DetourDetach(&v_RunFrameServer, &RunFrameServer);
}
CServerGameDLL* g_pServerGameDLL = nullptr;
CServerGameClients* g_pServerGameClients = nullptr;
CServerGameEnts* g_pServerGameEntities = nullptr;
// Holds global variables shared between engine and game.
CGlobalVars** g_pGlobals = nullptr;