Initial commit

This commit is contained in:
Lauri Räsänen 2024-04-25 03:33:15 +03:00
commit e3a24cdd59
13 changed files with 1740 additions and 0 deletions

157
src/ServerPlugin.cpp Normal file
View file

@ -0,0 +1,157 @@
#include "ServerPlugin.h"
#include "tier1.h"
#include "tier2.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
ServerPlugin g_serverPlugin = ServerPlugin {};
EXPOSE_SINGLE_INTERFACE_GLOBALVAR(
ServerPlugin,
IServerPluginCallbacks,
INTERFACEVERSION_ISERVERPLUGINCALLBACKS,
g_serverPlugin
);
bool ServerPlugin::Load(CreateInterfaceFn engineFactory, CreateInterfaceFn serverFactory)
{
ConnectTier1Libraries(&engineFactory, 1);
// ConnectTier2Libraries(&engineFactory, 1);
m_engineInterfaces = EngineInterfaces(engineFactory, serverFactory);
if (!m_engineInterfaces.IsValid())
{
Msg("[ServerPlugin] Scientist: \"Do you suspect some alien subterfuge "
"behind this failure?\"\n");
Msg("[ServerPlugin] Failed to get interfaces from the engine\n");
m_engineInterfaces.Print();
return false;
}
MathLib_Init(2.2f, 2.2f, 0.0f, 2);
ConVar_Register(0);
Msg("[ServerPlugin] Scientist: \"Testing...testing... Everything seems to be in order.\"\n");
return true;
}
void ServerPlugin::Unload()
{
Msg("[ServerPlugin] Unload");
if (m_gameEventsHooked)
{
m_engineInterfaces.gameEventManager->RemoveListener(this);
}
ConVar_Unregister();
// DisconnectTier2Libraries();
DisconnectTier1Libraries();
}
void ServerPlugin::Pause()
{
Msg("[ServerPlugin] Pause");
}
void ServerPlugin::UnPause()
{
Msg("[ServerPlugin] UnPause");
}
const char* ServerPlugin::GetPluginDescription()
{
return "Server Plugin Example";
}
void ServerPlugin::LevelInit(const char* pMapName)
{
Msg("[ServerPlugin] LevelInit: \"%s\"", pMapName);
ListenToGameEvent("player_hurt");
}
void ServerPlugin::ServerActivate(edict_t* pEdictList, int edictCount, int clientMax)
{
}
void ServerPlugin::GameFrame(bool simulating)
{
}
void ServerPlugin::LevelShutdown()
{
Msg("[ServerPlugin] LevelShutdown");
}
void ServerPlugin::ClientPutInServer(edict_t* pEntity, const char* playername)
{
Msg("[ServerPlugin] ClientPutInServer: \"%s\"", playername);
}
void ServerPlugin::SetCommandClient(int index)
{
m_iClientCommandIndex = index;
}
void ServerPlugin::ClientActive(edict_t* pEntity)
{
}
void ServerPlugin::ClientDisconnect(edict_t* pEntity)
{
}
void ServerPlugin::ClientSettingsChanged(edict_t* pEdict)
{
}
PLUGIN_RESULT ServerPlugin::ClientConnect(
bool* bAllowConnect,
edict_t* pEntity,
const char* pszName,
const char* pszAddress,
char* reject,
int maxrejectlen
)
{
Msg("[ServerPlugin] ClientConnect: \"%s\" from \"%s\"", pszName, pszAddress);
return PLUGIN_CONTINUE;
}
PLUGIN_RESULT ServerPlugin::ClientCommand(edict_t* pEntity, const CCommand& args)
{
if (!pEntity || pEntity->IsFree())
{
return PLUGIN_CONTINUE;
}
Msg("[ServerPlugin] ClientCommand: \"%s\"", args.GetCommandString());
return PLUGIN_CONTINUE;
}
PLUGIN_RESULT ServerPlugin::NetworkIDValidated(const char* pszUserName, const char* pszNetworkID)
{
Msg("[ServerPlugin] NetworkIDValidated: \"%s\" with ID \"%s\"", pszUserName, pszNetworkID);
return PLUGIN_CONTINUE;
}
void ServerPlugin::OnQueryCvarValueFinished(
QueryCvarCookie_t iCookie,
edict_t* pPlayerEntity,
EQueryCvarValueStatus eStatus,
const char* pCvarName,
const char* pCvarValue
)
{
}
void ServerPlugin::FireGameEvent(IGameEvent* event)
{
Msg("[ServerPlugin] FireGameEvent: \"%s\"", event->GetName());
}
void ServerPlugin::ListenToGameEvent(const char* name)
{
m_engineInterfaces.gameEventManager->AddListener(this, name, true);
m_gameEventsHooked = true;
}

138
src/ServerPlugin.h Normal file
View file

@ -0,0 +1,138 @@
#pragma once
#include "eiface.h"
#include "icvar.h"
#include "IEngineTrace.h"
#include "igameevents.h"
#include "iplayerinfo.h"
#include "iserverplugin.h"
#include "vstdlib/random.h"
struct EngineInterfaces
{
EngineInterfaces() :
cvar(nullptr),
engineServer(nullptr),
engineTrace(nullptr),
gameEventManager(nullptr),
serverPluginHelpers(nullptr),
uniformRandomStream(nullptr),
botManager(nullptr),
playerInfoManager(nullptr),
globalVars(nullptr)
{
}
EngineInterfaces(CreateInterfaceFn engineFactory, CreateInterfaceFn serverFactory)
{
// Engine interfaces
cvar = (ICvar*)engineFactory(CVAR_QUERY_INTERFACE_VERSION, NULL);
engineServer = (IVEngineServer*)engineFactory(INTERFACEVERSION_VENGINESERVER, NULL);
engineTrace = (IEngineTrace*)engineFactory(INTERFACEVERSION_ENGINETRACE_SERVER, NULL);
gameEventManager =
(IGameEventManager2*)engineFactory(INTERFACEVERSION_GAMEEVENTSMANAGER, NULL);
serverPluginHelpers =
(IServerPluginHelpers*)engineFactory(INTERFACEVERSION_ISERVERPLUGINHELPERS, NULL);
uniformRandomStream =
(IUniformRandomStream*)engineFactory(VENGINE_SERVER_RANDOM_INTERFACE_VERSION, NULL);
// Server interfaces
botManager = (IBotManager*)serverFactory(INTERFACEVERSION_PLAYERBOTMANAGER, NULL);
playerInfoManager =
(IPlayerInfoManager*)serverFactory(INTERFACEVERSION_PLAYERINFOMANAGER, NULL);
if (playerInfoManager)
{
globalVars = playerInfoManager->GetGlobalVars();
}
}
bool IsValid()
{
return cvar && engineServer && engineTrace && gameEventManager && serverPluginHelpers
&& uniformRandomStream && botManager && playerInfoManager;
}
void Print()
{
Msg("Engine interfaces:\n"
" cvar: %p\n"
" engineServer: %p\n"
" engineTrace: %p\n"
" gameEventManager: %p\n"
" serverPluginHelpers: %p\n"
" uniformRandomStream: %p\n"
"Server interfaces:\n"
" botManager: %p\n"
" playerInfoManager: %p\n",
cvar,
engineServer,
engineTrace,
gameEventManager,
serverPluginHelpers,
uniformRandomStream,
botManager,
playerInfoManager);
}
ICvar* cvar;
IVEngineServer* engineServer;
IEngineTrace* engineTrace;
IGameEventManager2* gameEventManager;
IServerPluginHelpers* serverPluginHelpers;
IUniformRandomStream* uniformRandomStream;
IBotManager* botManager;
IPlayerInfoManager* playerInfoManager;
CGlobalVars* globalVars;
};
class ServerPlugin : public IServerPluginCallbacks, IGameEventListener2
{
public:
// IServerPluginCallbacks interface
bool Load(CreateInterfaceFn interfaceFactory, CreateInterfaceFn gameServerFactory) override;
void Unload() override;
void Pause() override;
void UnPause() override;
const char* GetPluginDescription() override;
void LevelInit(const char* pMapName) override;
;
void ServerActivate(edict_t* pEdictList, int edictCount, int clientMax) override;
void GameFrame(bool simulating) override;
void LevelShutdown() override;
void ClientPutInServer(edict_t* pEntity, const char* playername) override;
void SetCommandClient(int index) override;
void ClientActive(edict_t* pEntity) override;
void ClientDisconnect(edict_t* pEntity) override;
void ClientSettingsChanged(edict_t* pEdict) override;
PLUGIN_RESULT ClientConnect(
bool* bAllowConnect,
edict_t* pEntity,
const char* pszName,
const char* pszAddress,
char* reject,
int maxrejectlen
) override;
PLUGIN_RESULT ClientCommand(edict_t* pEntity, const CCommand& args) override;
PLUGIN_RESULT NetworkIDValidated(const char* pszUserName, const char* pszNetworkID) override;
void OnQueryCvarValueFinished(
QueryCvarCookie_t iCookie,
edict_t* pPlayerEntity,
EQueryCvarValueStatus eStatus,
const char* pCvarName,
const char* pCvarValue
) override;
// IGameEventListener2 interface
void FireGameEvent(IGameEvent* event) override;
void ListenToGameEvent(const char* name);
private:
EngineInterfaces m_engineInterfaces;
int m_iClientCommandIndex;
bool m_gameEventsHooked;
};
extern ServerPlugin g_ServerPlugin;

10
src/meson.build Normal file
View file

@ -0,0 +1,10 @@
plugin_src = files([
'ServerPlugin.cpp',
])
server_plugin = shared_library(
'server_plugin',
plugin_src,
include_directories: [sdk_includes],
name_prefix: '',
)