Page 1 of 1
Hooking UserMessages (CSGO)
Posted: Sun Nov 08, 2015 12:01 am
by roflmuffin
Hi,
I noticed it is possible to create your own UserMessages through the use of the messages package, but is there anyway to hook a particular message (or all of them) that is being sent to players?
Something like what you would do in SourceMod:
Syntax: Select all
HookUserMessage(GetUserMessageId("TextMsg"), TextMsg, true);
Thanks
Posted: Sun Nov 08, 2015 12:08 am
by Ayuto
We currently don't have a usermessage hook built-in. You can of course use the memory module to hook them on your own.
However, what exactly do you want to do?
Posted: Sun Nov 08, 2015 12:13 am
by roflmuffin
Hi Ayuto,
There is a particular SourceMod plugin that will post things in individual players chat, and this information is catered to them based on that plugin. I am looking to find this information and associate it with that player. Does that make any sense?
Posted: Sun Nov 08, 2015 1:31 pm
by Ayuto
Since we don't have a something like this built-in, it's a bit hacky/clunky, but possible!
You have to update to version 105 (it's currently compiling) to use this sample code, because I forgot
two memory tools definitions. Those are required to use a few memory function like memory.make_object() on a pointer.
Syntax: Select all
# =============================================================================
# >> IMPORTS
# =============================================================================
from messages import SayText2
import memory
from memory import Convention
from memory import DataType
from memory.hooks import PreHook
from engines.server import engine_server
from filters.recipients import RecipientFilter
from messages import UserMessage
from _messages import ProtobufMessage
# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# Not a nice way to get the user message index...
saytext2_index = UserMessage(RecipientFilter(), 'SayText2').message_index
# virtual void SendUserMessage( IRecipientFilter &filter, int message, const google: :p rotobuf::Message &msg ) = 0;
SendUserMessage = memory.get_object_pointer(engine_server).make_virtual_function(
45,
Convention.THISCALL,
[DataType.POINTER, DataType.POINTER, DataType.INT, DataType.POINTER],
DataType.VOID
)
# =============================================================================
# >> IMPORTS
# =============================================================================
@PreHook(SendUserMessage)
def pre_user_message_send(args):
message_index = args[2]
# Check if a SayText2 user message has been sent
if message_index != saytext2_index:
return
# Wrap the IRecipientFilter pointer using the RecipientFilter class
recipients = memory.make_object(RecipientFilter, args[1])
# Allocate space for a ProtobufMessage object. It should allocate 4 bytes
ptr = memory.alloc(memory.get_size(ProtobufMessage), False)
# The ProtobufMessage class only has one attribute, which is a
# google: :p rotobuf::Message pointer. Maybe we should extend it and instead
# of wrapping it.
# https://github.com/Source-Python-Dev-Te ... ges.h#L287
ptr.set_pointer(args[3])
# Wrap the pointer using the ProtobufMessage class
buffer = memory.make_object(ProtobufMessage, ptr)
# We can now use all of these methods:
# https://github.com/Source-Python-Dev-Te ... p.cpp#L104
print(buffer.get_string('msg_name'))
# Don't forget to deallocate the allocated space!
ptr.dealloc()
# Send a test message to everyone
SayText2('Hello, world!').send()
There are plans for a built-in hook, so this will be much easier in the future.
Posted: Mon Nov 09, 2015 8:47 am
by roflmuffin
Hi Ayuto,
This is exactly what I was looking for, amazing. Thanks for this!