Skip to content

Commit

Permalink
Implement API interface game message manager
Browse files Browse the repository at this point in the history
  • Loading branch information
s1lentq committed May 13, 2024
1 parent a761efa commit c9f9bbf
Show file tree
Hide file tree
Showing 13 changed files with 1,048 additions and 1 deletion.
1 change: 1 addition & 0 deletions rehlds/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ set(ENGINE_SRCS
rehlds/public_amalgamation.cpp
rehlds/rehlds_api_impl.cpp
rehlds/rehlds_interfaces_impl.cpp
rehlds/rehlds_messagemngr_impl.cpp
rehlds/rehlds_security.cpp
)

Expand Down
4 changes: 4 additions & 0 deletions rehlds/engine/sys_dll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,10 @@ void LoadThisDll(const char *szDllFilename)
goto IgnoreThisDLL;
}

#ifdef REHLDS_API
MessageManager().Init();
#endif

pfnGiveFnptrsToDll(&g_engfuncsExportedToDlls, &gGlobalVariables);
if (g_iextdllMac == MAX_EXTENSION_DLL)
{
Expand Down
3 changes: 3 additions & 0 deletions rehlds/msvc/ReHLDS.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<ClCompile Include="..\rehlds\FlightRecorderImpl.cpp" />
<ClCompile Include="..\rehlds\flight_recorder.cpp" />
<ClCompile Include="..\rehlds\main.cpp" />
<ClCompile Include="..\rehlds\rehlds_messagemngr_impl.cpp" />
<ClCompile Include="..\rehlds\rehlds_api_impl.cpp" />
<ClCompile Include="..\rehlds\rehlds_interfaces_impl.cpp" />
<ClCompile Include="..\rehlds\hookchains_impl.cpp" />
Expand Down Expand Up @@ -386,6 +387,7 @@
<ClInclude Include="..\public\rehlds\eiface.h" />
<ClInclude Include="..\public\rehlds\FlightRecorder.h" />
<ClInclude Include="..\public\rehlds\hookchains.h" />
<ClInclude Include="..\public\rehlds\IMessageManager.h" />
<ClInclude Include="..\public\rehlds\keydefs.h" />
<ClInclude Include="..\public\rehlds\maintypes.h" />
<ClInclude Include="..\public\rehlds\model.h" />
Expand Down Expand Up @@ -439,6 +441,7 @@
<ClInclude Include="..\rehlds\FlightRecorderImpl.h" />
<ClInclude Include="..\rehlds\flight_recorder.h" />
<ClInclude Include="..\rehlds\hookchains_impl.h" />
<ClInclude Include="..\rehlds\rehlds_messagemngr_impl.h" />
<ClInclude Include="..\rehlds\platform.h" />
<ClInclude Include="..\rehlds\precompiled.h" />
<ClInclude Include="..\rehlds\RehldsRuntimeConfig.h" />
Expand Down
9 changes: 9 additions & 0 deletions rehlds/msvc/ReHLDS.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@
<ClCompile Include="..\testsuite\memory.cpp">
<Filter>testsuite</Filter>
</ClCompile>
<ClCompile Include="..\rehlds\rehlds_messagemngr_impl.cpp">
<Filter>rehlds</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\version\version.h">
Expand Down Expand Up @@ -1060,5 +1063,11 @@
<ClInclude Include="..\testsuite\memory.h">
<Filter>testsuite</Filter>
</ClInclude>
<ClInclude Include="..\rehlds\rehlds_messagemngr_impl.h">
<Filter>rehlds</Filter>
</ClInclude>
<ClInclude Include="..\public\rehlds\IMessageManager.h">
<Filter>public\rehlds</Filter>
</ClInclude>
</ItemGroup>
</Project>
223 changes: 223 additions & 0 deletions rehlds/public/rehlds/IMessageManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/

#pragma once

/**
* @brief Interface for defining message parameters and behavior for a individual message object
*/
class IMessage
{
public:
/**
* @brief The parameter types for a message
*/
enum class ParamType : uint8
{
Byte,
Char,
Short,
Long,
Angle,
Coord,
String,
Entity,
};

/**
* @brief Blocking behavior types for messages
*/
enum class BlockType : uint8
{
Not, // Not a block
Once, // Block once
Set // Set block
};

/**
* @brief Message destinations
*/
enum class Dest : uint8
{
BROADCAST, // Unreliable to all
ONE, // Reliable to one (msg_entity)
ALL, // Reliable to all
INIT, // Write to the init string
PVS, // Ents in PVS of org
PAS, // Ents in PAS of org
PVS_R, // Reliable to PVS
PAS_R, // Reliable to PAS
ONE_UNRELIABLE, // Send to one client, but don't put in reliable stream, put in unreliable datagram
SPEC, // Sends to all spectator proxies
};

virtual ~IMessage() {};

/**
* @brief Returns the number of parameters in the message
* @return The number of parameters
*/
virtual int getParamCount() const = 0;

/**
* @brief Returns the type of the parameter at the given index
* @param index The index of the parameter
* @return The type of the parameter
*/
virtual ParamType getParamType(size_t index) const = 0;

/**
* @brief Returns the integer value of the parameter at the given index
* @param index The index of the parameter
* @return The integer value of the parameter
*/
virtual int getParamInt(size_t index) const = 0;

/**
* @brief Returns the float value of the parameter at the given index
* @param index The index of the parameter
* @return The float value of the parameter
*/
virtual float getParamFloat(size_t index) const = 0;

/**
* @brief Returns the string value of the parameter at the given index
* @param index The index of the parameter
* @return The string value of the parameter
*/
virtual const char* getParamString(size_t index) const = 0;

/**
* @brief Sets the integer value of the parameter at the given index
* @param index The index of the parameter
* @param value The integer value to set
*/
virtual void setParamInt(size_t index, int value) = 0;

/**
* @brief Sets the float value of the parameter at the given index
* @param index The index of the parameter
* @param value The float value to set
*/
virtual void setParamFloat(size_t index, float value) = 0;

/**
* @brief Sets the vector value of the parameter at the given index
* @param index The index of the parameter
* @param pos The vector value to set
*/
virtual void setParamVec(size_t index, const float *pos) = 0;

/**
* @brief Sets the string value of the parameter at the given index
* @param index The index of the parameter
* @param string The string value to set
*/
virtual void setParamString(size_t index, const char *string) = 0;

/**
* @brief Returns the destination of the message
* @return The destination of the message
*/
virtual Dest getDest() const = 0;

/**
* @brief Returns the type of the message
* @return The type of the message
*/
virtual int getType() const = 0;

/**
* @brief Returns the origin of the message
* @return The origin of the message
*/
virtual const float* getOrigin() const = 0;

/**
* @brief Returns the edict associated with the message
* @return The edict associated with the message
*/
virtual struct edict_s* getEdict() const = 0;

/**
* @brief Returns whether the message has been modified
* @return True if the message has been modified, false otherwise
*/
virtual bool isModified() const = 0;

// This must be the last virtual function in class
#ifdef REHLDS_SELF
// Set the copyback buffer for the message
virtual void setCopybackBuffer(struct sizebuf_s *pbuf) = 0;
#endif
};

#define MESSAGEMNGR_VERSION_MAJOR 1
#define MESSAGEMNGR_VERSION_MINOR 0

/**
* @brief Interface manages hooks and blocking behavior game messages
*/
class IMessageManager
{
public:
using hookfunc_t = void (*)(IVoidHookChain<IMessage *> *chain, IMessage *msg);

virtual ~IMessageManager() {};

/**
* @brief Returns the major version of the MessageManager
* @return The major version
*/
virtual int getMajorVersion() const = 0;

/**
* @brief Returns the minor version of the MessageManager
* @return The minor version
*/
virtual int getMinorVersion() const = 0;

/**
* @brief Returns the blocking behavior for the given message type
* @param msgType The message type
* @return The blocking behavior for the given message type
*/
virtual IMessage::BlockType getMessageBlock(int msgType) const = 0;

/**
* @brief Sets the blocking behavior for the given message type
* @param msgType The message type
* @param blockType The blocking behavior to set
*/
virtual void setMessageBlock(int msgType, IMessage::BlockType blockType) = 0;

/**
* @brief Registers a hook function for the given message type
* @param msgType The message type to register the hook for
* @param handler The hook function to register
* @param priority The priority of the hook function (see enum HookChainPriority)
*/
virtual void registerHook(int msgType, hookfunc_t handler, int priority = HC_PRIORITY_DEFAULT) = 0;

/**
* @brief Unregisters a hook function for the given message type
* @param msgType The message type to unregister the hook for
* @param handler The hook function to unregister
*/
virtual void unregisterHook(int msgType, hookfunc_t handler) = 0;
};
2 changes: 2 additions & 0 deletions rehlds/public/rehlds/rehlds_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "rehlds_interfaces.h"
#include "hookchains.h"
#include "FlightRecorder.h"
#include "IMessageManager.h"
#include "interface.h"
#include "model.h"
#include "ObjectList.h"
Expand Down Expand Up @@ -446,6 +447,7 @@ class IRehldsApi {
virtual IRehldsServerStatic* GetServerStatic() = 0;
virtual IRehldsServerData* GetServerData() = 0;
virtual IRehldsFlightRecorder* GetFlightRecorder() = 0;
virtual IMessageManager *GetMessageManager() = 0;
};

#define VREHLDS_HLDS_API_VERSION "VREHLDS_HLDS_API_VERSION001"
4 changes: 4 additions & 0 deletions rehlds/rehlds/hookchains_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,7 @@ void AbstractHookChainRegistry::removeHook(void* hookFunc) {
}
}
}

int AbstractHookChainRegistry::getCount() const {
return m_NumHooks;
}
12 changes: 11 additions & 1 deletion rehlds/rehlds/hookchains_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ class AbstractHookChainRegistry {

protected:
void addHook(void* hookFunc, int priority);
bool findHook(void* hookFunc) const;
void removeHook(void* hookFunc);

public:
int getCount() const;
bool findHook(void* hookFunc) const;
AbstractHookChainRegistry();
};

Expand All @@ -132,9 +133,14 @@ class IHookChainRegistryImpl : public IHookChainRegistry < t_ret, t_args...>, pu
EXT_FUNC virtual void registerHook(hookfunc_t hook, int priority) {
addHook((void*)hook, priority);
}

EXT_FUNC virtual void unregisterHook(hookfunc_t hook) {
removeHook((void*)hook);
}

bool isEmpty() const {
return getCount() == 0;
}
};

template<typename ...t_args>
Expand All @@ -157,4 +163,8 @@ class IVoidHookChainRegistryImpl : public IVoidHookChainRegistry <t_args...>, pu
EXT_FUNC virtual void unregisterHook(hookfunc_t hook) {
removeHook((void*)hook);
}

bool isEmpty() const {
return getCount() == 0;
}
};
1 change: 1 addition & 0 deletions rehlds/rehlds/precompiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "rehlds_api_impl.h"
#include "FlightRecorderImpl.h"
#include "flight_recorder.h"
#include "rehlds_messagemngr_impl.h"
#include "rehlds_security.h"

#include "dlls/cdll_dll.h"
Expand Down
4 changes: 4 additions & 0 deletions rehlds/rehlds/rehlds_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,10 @@ IRehldsServerData* EXT_FUNC CRehldsApi::GetServerData() {
return &g_RehldsServerData;
}

IMessageManager* EXT_FUNC CRehldsApi::GetMessageManager() {
return &MessageManager();
}

IRehldsFlightRecorder* EXT_FUNC CRehldsApi::GetFlightRecorder() {
return g_FlightRecorder;
}
Expand Down
1 change: 1 addition & 0 deletions rehlds/rehlds/rehlds_api_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ class CRehldsApi : public IRehldsApi {
virtual IRehldsServerStatic* GetServerStatic();
virtual IRehldsServerData* GetServerData();
virtual IRehldsFlightRecorder* GetFlightRecorder();
virtual IMessageManager* GetMessageManager();
};

extern sizebuf_t* GetNetMessage_api();
Expand Down
Loading

0 comments on commit c9f9bbf

Please sign in to comment.