Skip to content

Commit

Permalink
Implement message hook using ReHLDS API
Browse files Browse the repository at this point in the history
  • Loading branch information
s1lentq committed May 13, 2024
1 parent 92c13f9 commit b757be5
Show file tree
Hide file tree
Showing 15 changed files with 897 additions and 1 deletion.
2 changes: 2 additions & 0 deletions reapi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,15 @@ set(REAPI_SRCS
"src/hook_callback.cpp"
"src/hook_list.cpp"
"src/hook_manager.cpp"
"src/hook_message_manager.cpp"
"src/api_config.cpp"
"src/member_list.cpp"
"src/meta_api.cpp"
"src/reapi_utils.cpp"
"src/sdk_util.cpp"
"src/natives/natives_common.cpp"
"src/natives/natives_hookchains.cpp"
"src/natives/natives_message.cpp"
"src/natives/natives_members.cpp"
"src/natives/natives_misc.cpp"
"src/natives/natives_rechecker.cpp"
Expand Down
121 changes: 121 additions & 0 deletions reapi/extra/amxmodx/scripting/include/reapi_engine.inc
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,124 @@ native rh_get_net_from(output[], len);
* @return Netchan connection time in seconds or 0 if client index is invalid or client is not connected
*/
native rh_get_client_connect_time(const index);


enum MessageHook
{
INVALID_MESSAGEHOOK = 0
};

/**
* Registers a callback function to be called when a game message with the specified ID is received.
*
* @param msg_id The ID of the message to register the callback for.
* @param callback The name of the callback function.
* @param post Whether the callback should be invoked before or after processing the message. (optional)
*
* @note You can modify the message content using SetMessageParam native before the original function is invoked.
* Also can reading the message content using GetMessageParam native.
*
* In the callback function, use the return values from Hookchain return types, such as HC_CONTINUE, HC_SUPERCEDE, etc.
* to control the flow of message processing.
*
* @return Returns a handle to the registered message hook.
*/
native MessageHook:RegisterMessage(const msg_id, const callback[], post = 0);

/**
* Unregisters a game message hook identified by the specified handle.
*
* @param handle The handle of the message hook to unregister.
*
* @return Returns true if the message hook is successfully unregistered, otherwise false.
*/
native bool:UnregisterMessage(const MessageHook:handle);

/**
* Enables a game message hook identified by the specified handle.
*
* @param handle The handle of the message hook to enable.
*
* @return Returns true if the message hook is successfully enabled, otherwise false.
*/
native bool:EnableHookMessage(const MessageHook:handle);

/**
* Disables a game message hook identified by the specified handle.
*
* @param handle The handle of the message hook to disable.
*
* @return Returns true if the message hook is successfully disabled, otherwise false.
*/
native bool:DisableHookMessage(const MessageHook:handle);

/**
* Sets the parameter value for the specified index in the current game message.
*
* @param index The index of the parameter to set.
* @param value The value to set for the parameter.
*
* @return Returns true if the parameter value is successfully set, otherwise false.
*/
native bool:SetMessageParam(const number, any:...);

/**
* Retrieves the parameter value for the specified index in the current game message.
*
* @param index The index of the parameter to retrieve.
* @param ... Additional parameters depending on the type of the parameter being retrieved.
*
* @return Returns the retrieved parameter value.
*/
native any:GetMessageParam(const number, any:...);

/**
* Retrieves the type of the parameter at the specified index in the current game message.
*
* @param index The index of the parameter to retrieve the type for.
*
* @return Returns the type of the parameter, look at the enum MsgParamType
*/
native MsgParamType:GetMessageParamType(const number);

/**
* Retrieves the number of parameters in the current game message.
*
* @return Returns the number of parameters in the current game message.
*/
native GetMessageParamCount();

/**
* Retrieves the origin of the current game message.
*
* @param origin An array to store the origin coordinates of the game message.
*
* @return Returns true if the origin is successfully retrieved, otherwise false.
*/
native bool:GetMessageOrigin(Float:origin[3]);

/**
* Retrieves the destination of the current message.
*
* @return Returns the destination of the current message.
*/
native GetMessageDest();

/**
* Sets the block type for the specified message ID.
*
* @param msgid The ID of the message to set the block type for.
* @param type The type of block to set for the message, look at the enum MsgBlockType
*
* @return Returns true if the block type is successfully set, otherwise false.
*/
native bool:SetMessageBlock(const msgid, MsgBlockType:type);

/**
* Retrieves the block type for the specified message ID.
*
* @param msgid The ID of the message to retrieve the block type for.
*
* @return Returns the block type of the specified message, look at the enum MsgBlockType
*/
native MsgBlockType:GetMessageBlock(const msgid);
28 changes: 27 additions & 1 deletion reapi/extra/amxmodx/scripting/include/reapi_engine_const.inc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum CheckVisibilityType
VisibilityInPAS // Check in Potentially Audible Set (PAS)
};

/*
/**
* For RH_SV_AddResource hook
*/
enum ResourceType_t
Expand Down Expand Up @@ -1321,3 +1321,29 @@ enum NetAdrVars
*/
netadr_port
};

/**
* Message param types used with GetMessageParamType()
*/
enum MsgParamType
{
ParamByte,
ParamChar,
ParamShort,
ParamLong,
ParamAngle,
ParamCoord,
ParamString,
ParamEntity,
};

/**
* Blocking behavior types for messages
* Flags for natives SetMessageBlock()/GetMessageBlock()
*/
enum MsgBlockType
{
MSG_BLOCK_NOT, // Not a block
MSG_BLOCK_ONCE, // Block once
MSG_BLOCK_SET // Set block
};
5 changes: 5 additions & 0 deletions reapi/msvc/reapi.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
<ClInclude Include="..\include\cssdk\engine\hookchains.h">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
</ClInclude>
<ClInclude Include="..\include\cssdk\engine\IMessageManager.h" />
<ClInclude Include="..\include\cssdk\engine\keydefs.h" />
<ClInclude Include="..\include\cssdk\engine\maintypes.h" />
<ClInclude Include="..\include\cssdk\engine\model.h" />
Expand Down Expand Up @@ -217,6 +218,7 @@
<ClInclude Include="..\src\hook_list.h" />
<ClInclude Include="..\src\main.h" />
<ClInclude Include="..\src\member_list.h" />
<ClInclude Include="..\src\hook_message_manager.h" />
<ClInclude Include="..\src\mods\mod_rechecker_api.h" />
<ClInclude Include="..\src\mods\mod_regamedll_api.h" />
<ClInclude Include="..\src\mods\mod_rehlds_api.h" />
Expand All @@ -227,6 +229,7 @@
<ClInclude Include="..\src\natives\natives_helper.h" />
<ClInclude Include="..\src\natives\natives_hookchains.h" />
<ClInclude Include="..\src\natives\natives_members.h" />
<ClInclude Include="..\src\natives\natives_hookmessage.h" />
<ClInclude Include="..\src\natives\natives_misc.h" />
<ClInclude Include="..\src\natives\natives_rechecker.h" />
<ClInclude Include="..\src\natives\natives_reunion.h" />
Expand Down Expand Up @@ -266,6 +269,7 @@
</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\src\main.cpp" />
<ClCompile Include="..\src\hook_message_manager.cpp" />
<ClCompile Include="..\src\meta_api.cpp" />
<ClCompile Include="..\src\mods\mod_rechecker_api.cpp" />
<ClCompile Include="..\src\mods\mod_regamedll_api.cpp" />
Expand All @@ -276,6 +280,7 @@
<ClCompile Include="..\src\natives\natives_common.cpp" />
<ClCompile Include="..\src\natives\natives_hookchains.cpp" />
<ClCompile Include="..\src\natives\natives_members.cpp" />
<ClCompile Include="..\src\natives\natives_hookmessage.cpp" />
<ClCompile Include="..\src\natives\natives_misc.cpp" />
<ClCompile Include="..\src\natives\natives_rechecker.cpp" />
<ClCompile Include="..\src\natives\natives_reunion.cpp" />
Expand Down
15 changes: 15 additions & 0 deletions reapi/msvc/reapi.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,15 @@
<ClInclude Include="..\include\cssdk\dlls\gib.h">
<Filter>include\cssdk\dlls</Filter>
</ClInclude>
<ClInclude Include="..\include\cssdk\engine\IMessageManager.h">
<Filter>include\cssdk\engine</Filter>
</ClInclude>
<ClInclude Include="..\src\hook_message_manager.h">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="..\src\natives\natives_hookmessage.h">
<Filter>src\natives</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\include\cssdk\common\parsemsg.cpp">
Expand Down Expand Up @@ -830,6 +839,12 @@
<ClCompile Include="..\src\amx_hook.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\src\hook_message_manager.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\src\natives\natives_hookmessage.cpp">
<Filter>src\natives</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\extra\amxmodx\scripting\include\reapi.inc">
Expand Down
1 change: 1 addition & 0 deletions reapi/src/amxxmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ C_DLLEXPORT int AMXX_Attach(PFN_REQ_FNPTR reqFnptrFunc)
OnAmxxAttach();

RegisterNatives_HookChains();
RegisterNatives_HookMessage();
RegisterNatives_Members();
RegisterNatives_Misc();
RegisterNatives_VTC();
Expand Down
Loading

0 comments on commit b757be5

Please sign in to comment.