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.