From e31eb21fa2e09e249be4095d8a3cecd566973c9a Mon Sep 17 00:00:00 2001 From: asmodai Date: Sun, 13 Dec 2015 22:46:24 +0300 Subject: [PATCH] Added API registration tool for plugins Fixed voice stream API --- rehlds/engine/host.cpp | 20 ++++++------ rehlds/public/rehlds/rehlds_api.h | 4 ++- rehlds/public/rehlds/rehlds_interfaces.h | 2 +- rehlds/rehlds/rehlds_api_impl.cpp | 41 +++++++++++++++++++++++- rehlds/rehlds/rehlds_interfaces_impl.cpp | 6 ++-- rehlds/rehlds/rehlds_interfaces_impl.h | 2 +- 6 files changed, 59 insertions(+), 16 deletions(-) diff --git a/rehlds/engine/host.cpp b/rehlds/engine/host.cpp index 50c7e26..3a885b3 100644 --- a/rehlds/engine/host.cpp +++ b/rehlds/engine/host.cpp @@ -459,16 +459,16 @@ void EXT_FUNC SV_DropClient_api(IGameClient* cl, bool crash, const char* fmt, .. g_RehldsHookchains.m_SV_DropClient.callChain(SV_DropClient_hook, cl, crash, buf); } -void SV_DropClient(client_t *cl, qboolean crash, const char *fmt, ...) -{ - char buf[1024]; - va_list argptr; - - va_start(argptr, fmt); - Q_vsnprintf(buf, ARRAYSIZE(buf) - 1, fmt, argptr); - va_end(argptr); - - g_RehldsHookchains.m_SV_DropClient.callChain(SV_DropClient_hook, GetRehldsApiClient(cl), crash != FALSE, buf); +void SV_DropClient(client_t *cl, qboolean crash, const char *fmt, ...) +{ + char buf[1024]; + va_list argptr; + + va_start(argptr, fmt); + Q_vsnprintf(buf, ARRAYSIZE(buf) - 1, fmt, argptr); + va_end(argptr); + + g_RehldsHookchains.m_SV_DropClient.callChain(SV_DropClient_hook, GetRehldsApiClient(cl), crash != FALSE, buf); } /* <35f4e> ../engine/host.c:673 */ diff --git a/rehlds/public/rehlds/rehlds_api.h b/rehlds/public/rehlds/rehlds_api.h index 2857c5b..b5b14de 100644 --- a/rehlds/public/rehlds/rehlds_api.h +++ b/rehlds/public/rehlds/rehlds_api.h @@ -35,7 +35,7 @@ #include "model.h" #define REHLDS_API_VERSION_MAJOR 2 -#define REHLDS_API_VERSION_MINOR 3 +#define REHLDS_API_VERSION_MINOR 4 //Steam_NotifyClientConnect hook typedef IHookChain IRehldsHook_Steam_NotifyClientConnect; @@ -233,6 +233,8 @@ struct RehldsFuncs_t { void(*MSG_WriteByte)(sizebuf_t *sb, int c); void(*MSG_WriteShort)(sizebuf_t *sb, int c); void(*MSG_WriteString)(sizebuf_t *sb, const char *s); + void*(*GetPluginApi)(const char *name); + void(*RegisterPluginApi)(const char *name, void *impl); }; class IRehldsApi { diff --git a/rehlds/public/rehlds/rehlds_interfaces.h b/rehlds/public/rehlds/rehlds_interfaces.h index e1dc430..d30af0f 100644 --- a/rehlds/public/rehlds/rehlds_interfaces.h +++ b/rehlds/public/rehlds/rehlds_interfaces.h @@ -67,7 +67,7 @@ public: virtual bool IsConnected() = 0; virtual void SetConnected(bool connected) = 0; - virtual uint32 GetVoiceStreams(int id) = 0; + virtual uint32 GetVoiceStream(int stream_id) = 0; virtual void SetLastVoiceTime(double time) = 0; virtual double GetLastVoiceTime() = 0; virtual bool GetLoopback() = 0; diff --git a/rehlds/rehlds/rehlds_api_impl.cpp b/rehlds/rehlds/rehlds_api_impl.cpp index 8bb82e7..50f6531 100644 --- a/rehlds/rehlds/rehlds_api_impl.cpp +++ b/rehlds/rehlds/rehlds_api_impl.cpp @@ -27,6 +27,26 @@ */ #include "precompiled.h" +struct plugin_api_t +{ + char name[32]; + void *impl; +}; + +std::vector g_PluginApis; + +plugin_api_t* FindPluginApiByName(const char *name) { + for (auto it = g_PluginApis.begin(), end = g_PluginApis.end(); it != end; ++it) { + auto api = *it; + + if (!strcmp(api->name, name)) { + return api; + } + } + + return NULL; +} + char* EXT_FUNC GetClientFallback_api() { return com_clientfallback; } @@ -109,6 +129,23 @@ cvar_t* EXT_FUNC GetCvarVars_api() { return cvar_vars; } +void* EXT_FUNC Rehlds_GetPluginApi(const char *name) { + auto api = FindPluginApiByName(name); + return api ? api->impl : NULL; +} + +void EXT_FUNC Rehlds_RegisterPluginApi(const char *name, void *impl) { + auto api = FindPluginApiByName(name); + + if (!api) { + api = new plugin_api_t; + strncpy(api->name, name, sizeof api->name - 1); + g_PluginApis.push_back(api); + } + + api->impl = impl; +} + CRehldsServerStatic g_RehldsServerStatic; CRehldsServerData g_RehldsServerData; CRehldsHookchains g_RehldsHookchains; @@ -152,7 +189,9 @@ RehldsFuncs_t g_RehldsApiFuncs = &MSG_WriteBuf_api, &MSG_WriteByte_api, &MSG_WriteShort_api, - &MSG_WriteString_api + &MSG_WriteString_api, + &Rehlds_GetPluginApi, + &Rehlds_RegisterPluginApi }; sizebuf_t* EXT_FUNC GetNetMessage_api() diff --git a/rehlds/rehlds/rehlds_interfaces_impl.cpp b/rehlds/rehlds/rehlds_interfaces_impl.cpp index 33e3d64..a88ab3a 100644 --- a/rehlds/rehlds/rehlds_interfaces_impl.cpp +++ b/rehlds/rehlds/rehlds_interfaces_impl.cpp @@ -70,8 +70,10 @@ void EXT_FUNC CGameClient::SetConnected(bool connected) { m_pClient->connected = connected ? 1 : 0; } -uint32 EXT_FUNC CGameClient::GetVoiceStreams(int id) { - return m_pClient->m_VoiceStreams[id >> 5]; +uint32 EXT_FUNC CGameClient::GetVoiceStream(int stream_id) { + if (stream_id >= 0 && stream_id < ARRAYSIZE(m_pClient->m_VoiceStreams)) + return m_pClient->m_VoiceStreams[stream_id]; + return 0; } void EXT_FUNC CGameClient::SetLastVoiceTime(double time) { diff --git a/rehlds/rehlds/rehlds_interfaces_impl.h b/rehlds/rehlds/rehlds_interfaces_impl.h index 2713ebd..a0d0f3e 100644 --- a/rehlds/rehlds/rehlds_interfaces_impl.h +++ b/rehlds/rehlds/rehlds_interfaces_impl.h @@ -76,7 +76,7 @@ public: virtual bool IsConnected(); virtual void SetConnected(bool connected); - virtual uint32 GetVoiceStreams(int id); + virtual uint32 GetVoiceStream(int stream_id); virtual void SetLastVoiceTime(double time); virtual double GetLastVoiceTime(); virtual bool GetLoopback();