Hooking UserMessages (CSGO)

Please post any questions about developing your plugin here. Please use the search function before posting!
roflmuffin
Junior Member
Posts: 19
Joined: Fri Nov 06, 2015 11:49 pm

Hooking UserMessages (CSGO)

Postby roflmuffin » Sun Nov 08, 2015 12:01 am

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
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Sun Nov 08, 2015 12:08 am

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?
roflmuffin
Junior Member
Posts: 19
Joined: Fri Nov 06, 2015 11:49 pm

Postby roflmuffin » Sun Nov 08, 2015 12:13 am

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?
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Sun Nov 08, 2015 1:31 pm

Since we don't have a something like this built-in, it's a bit hacky/clunky, but possible! :grin:

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.
roflmuffin
Junior Member
Posts: 19
Joined: Fri Nov 06, 2015 11:49 pm

Postby roflmuffin » Mon Nov 09, 2015 8:47 am

Hi Ayuto,

This is exactly what I was looking for, amazing. Thanks for this!

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 154 guests