Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions managed/CounterStrikeSharp.API/Core/API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,34 @@ public static string PbReadstring(UserMessage message, string name, int index){
}
}

public static int PbReadbytes(UserMessage message, string name, IntPtr buffer, int size, int index){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(message);
ScriptContext.GlobalScriptContext.Push(name);
ScriptContext.GlobalScriptContext.Push(buffer);
ScriptContext.GlobalScriptContext.Push(size);
ScriptContext.GlobalScriptContext.Push(index);
ScriptContext.GlobalScriptContext.SetIdentifier(0xECD23703);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
return (int)ScriptContext.GlobalScriptContext.GetResult(typeof(int));
}
}

public static int PbReadbyteslength(UserMessage message, string name, int index){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(message);
ScriptContext.GlobalScriptContext.Push(name);
ScriptContext.GlobalScriptContext.Push(index);
ScriptContext.GlobalScriptContext.SetIdentifier(0xF74C465F);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
return (int)ScriptContext.GlobalScriptContext.GetResult(typeof(int));
}
}

public static int PbGetrepeatedfieldcount(UserMessage message, string name){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
Expand Down Expand Up @@ -1614,6 +1642,20 @@ public static void PbSetstring(UserMessage message, string name, string value, i
}
}

public static void PbSetbytes(UserMessage message, string name, IntPtr buffer, int size, int index){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(message);
ScriptContext.GlobalScriptContext.Push(name);
ScriptContext.GlobalScriptContext.Push(buffer);
ScriptContext.GlobalScriptContext.Push(size);
ScriptContext.GlobalScriptContext.Push(index);
ScriptContext.GlobalScriptContext.SetIdentifier(0xF7C09993);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
}
}

public static void PbAddint(UserMessage message, string name, int value){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
Expand Down Expand Up @@ -1674,6 +1716,19 @@ public static void PbAddstring(UserMessage message, string name, string value){
}
}

public static void PbAddbytes(UserMessage message, string name, IntPtr buffer, int size){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
ScriptContext.GlobalScriptContext.Push(message);
ScriptContext.GlobalScriptContext.Push(name);
ScriptContext.GlobalScriptContext.Push(buffer);
ScriptContext.GlobalScriptContext.Push(size);
ScriptContext.GlobalScriptContext.SetIdentifier(0x50DB8210);
ScriptContext.GlobalScriptContext.Invoke();
ScriptContext.GlobalScriptContext.CheckErrors();
}
}

public static void PbRemoverepeatedfieldvalue(UserMessage message, string name, int index){
lock (ScriptContext.GlobalScriptContext.Lock) {
ScriptContext.GlobalScriptContext.Reset();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using CounterStrikeSharp.API.Modules.Utils;

namespace CounterStrikeSharp.API.Modules.UserMessages;
Expand All @@ -9,7 +10,7 @@

public delegate HookResult UserMessageHandler(UserMessage native);

public UserMessage(IntPtr pointer) : base(pointer)

Check warning on line 13 in managed/CounterStrikeSharp.API/Modules/UserMessages/UserMessage.cs

View workflow job for this annotation

GitHub Actions / build_managed

Non-nullable field '_recipients' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
{
Recipients = new RecipientFilter(NativeAPI.UsermessageGetrecipients(this));
Recipients.CollectionChanged = () => NativeAPI.UsermessageSetrecipients(this, Recipients.GetRecipientMask());
Expand Down Expand Up @@ -49,6 +50,22 @@
public string ReadString(string fieldName, int? index = null) => NativeAPI.PbReadstring(this, fieldName, index ?? -1);
public bool ReadBool(string fieldName, int? index = null) => NativeAPI.PbReadbool(this, fieldName, index ?? -1);

public byte[] ReadBytes(string fieldName, int? index = null)
{
var size = NativeAPI.PbReadbyteslength(this, fieldName, index ?? -1);
if (size == 0) return Array.Empty<byte>();
var bytes = new byte[size];
unsafe
{
fixed (byte* ptr = bytes)
{
var length = NativeAPI.PbReadbytes(this, fieldName, (IntPtr)ptr, size, index ?? -1);
return bytes;
}
}
}


public void SetInt(string fieldName, int value, int? index = null) => NativeAPI.PbSetint(this, fieldName, value, index ?? -1);
public void SetUInt(string fieldName, uint value, int? index = null) => NativeAPI.PbSetint(this, fieldName, (int)value, index ?? -1);
public void SetInt64(string fieldName, long value, int? index = null) => NativeAPI.PbSetint64(this, fieldName, value, index ?? -1);
Expand All @@ -64,6 +81,17 @@
public void SetString(string fieldName, string value, int? index = null) => NativeAPI.PbSetstring(this, fieldName, value, index ?? -1);
public void SetBool(string fieldName, bool value, int? index = null) => NativeAPI.PbSetbool(this, fieldName, value, index ?? -1);

public void SetBytes(string fieldName, byte[] value, int? index = null)
{
unsafe
{
fixed (byte* ptr = value)
{
NativeAPI.PbSetbytes(this, fieldName, (IntPtr)ptr, value.Length, index ?? -1);
}
}
}

public int GetRepeatedFieldCount(string fieldName) => NativeAPI.PbGetrepeatedfieldcount(this, fieldName);

public void RemoveRepeatedField(string fieldName, int index) => NativeAPI.PbRemoverepeatedfieldvalue(this, fieldName, index);
Expand All @@ -77,6 +105,17 @@
public void AddString(string fieldName, string value) => NativeAPI.PbAddstring(this, fieldName, value);
public void AddBool(string fieldName, bool value) => NativeAPI.PbAddbool(this, fieldName, value);

public void AddBytes(string fieldName, byte[] value)
{
unsafe
{
fixed (byte* ptr = value)
{
NativeAPI.PbAddbytes(this, fieldName, (IntPtr)ptr, value.Length);
}
}
}

// public UserMessage ReadMessage(string fieldName) => NativeAPI.PbReadmessage(this, fieldName);
// public UserMessage ReadRepeatedMessage(string fieldName, int index ) => NativeAPI.PbReadrepeatedmessage(this, fieldName, index);
// public UserMessage AddMessage(string fieldName) => NativeAPI.PbAddmessage(this, fieldName);
Expand Down
125 changes: 125 additions & 0 deletions src/scripting/natives/natives_usermessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,81 @@ static void PbReadString(ScriptContext& scriptContext)
scriptContext.SetResult(returnValue.c_str());
}

static void PbReadBytes(ScriptContext& scriptContext)
{
GET_MESSAGE_OR_ERR();
GET_FIELD_NAME_OR_ERR();

std::string returnValue;
auto ptr = scriptContext.GetArgument<void*>(2);
auto size = scriptContext.GetArgument<int>(3);
auto index = scriptContext.GetArgument<int>(4);

if (ptr == nullptr)
{
scriptContext.ThrowNativeError("Invalid buffer pointer for reading field \"%s\"", fieldName);
return;
}

if (index < 0)
{
if (!message->GetString(fieldName, returnValue))
{
scriptContext.ThrowNativeError("Invalid field \"%s\" for message \"%s\"", fieldName,
message->GetProtobufMessage()->GetTypeName().c_str());
return;
}
}
else
{
if (!message->GetRepeatedString(fieldName, index, returnValue))
{
scriptContext.ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", fieldName, index,
message->GetProtobufMessage()->GetTypeName().c_str());
return;
}
}

if (returnValue.size() > size)
{
scriptContext.ThrowNativeError("Buffer size is too small for field \"%s\" for message \"%s\"", fieldName,
message->GetProtobufMessage()->GetTypeName().c_str());
return;
}

memcpy(ptr, returnValue.c_str(), returnValue.size());
scriptContext.SetResult(returnValue.size());
}

static void PbReadBytesLength(ScriptContext& scriptContext)
{
GET_MESSAGE_OR_ERR();
GET_FIELD_NAME_OR_ERR();

auto index = scriptContext.GetArgument<int>(2);

std::string returnValue;

if (index < 0)
{
if (!message->GetString(fieldName, returnValue))
{
scriptContext.ThrowNativeError("Invalid field \"%s\" for message \"%s\"", fieldName,
message->GetProtobufMessage()->GetTypeName().c_str());
}
}
else
{
if (!message->GetRepeatedString(fieldName, index, returnValue))
{
scriptContext.ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", fieldName, index,
message->GetProtobufMessage()->GetTypeName().c_str());
}
}

scriptContext.SetResult(returnValue.size());
}

static void PbGetRepeatedFieldCount(ScriptContext& scriptContext)
{
GET_MESSAGE_OR_ERR();
Expand Down Expand Up @@ -392,6 +467,35 @@ static void PbSetString(ScriptContext& scriptContext)
}
}

static void PbSetBytes(ScriptContext& scriptContext)
{
GET_MESSAGE_OR_ERR();
GET_FIELD_NAME_OR_ERR();

auto ptr = scriptContext.GetArgument<const char*>(2);
auto size = scriptContext.GetArgument<int>(3);
auto index = scriptContext.GetArgument<int>(4);

std::string value(ptr, size);

if (index < 0)
{
if (!message->SetString(fieldName, value))
{
scriptContext.ThrowNativeError("Invalid field \"%s\" for message \"%s\"", fieldName,
message->GetProtobufMessage()->GetTypeName().c_str());
}
}
else
{
if (!message->SetRepeatedString(fieldName, index, value))
{
scriptContext.ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", fieldName, index,
message->GetProtobufMessage()->GetTypeName().c_str());
}
}
}

static void PbAddInt(ScriptContext& scriptContext)
{
GET_MESSAGE_OR_ERR();
Expand Down Expand Up @@ -462,6 +566,23 @@ static void PbAddString(ScriptContext& scriptContext)
}
}

static void PbAddBytes(ScriptContext& scriptContext)
{
GET_MESSAGE_OR_ERR();
GET_FIELD_NAME_OR_ERR();

auto ptr = scriptContext.GetArgument<const char*>(2);
auto size = scriptContext.GetArgument<int>(3);

std::string value(ptr, size);

if (!message->AddString(fieldName, value.c_str()))
{
scriptContext.ThrowNativeError("Invalid field \"%s\" for message \"%s\"", fieldName,
message->GetProtobufMessage()->GetTypeName().c_str());
}
}

static void PbRemoveRepeatedFieldValue(ScriptContext& scriptContext)
{
GET_MESSAGE_OR_ERR();
Expand Down Expand Up @@ -688,17 +809,21 @@ REGISTER_NATIVES(usermessages, {
ScriptEngine::RegisterNativeHandler("PB_READFLOAT", PbReadFloat);
ScriptEngine::RegisterNativeHandler("PB_READBOOL", PbReadBool);
ScriptEngine::RegisterNativeHandler("PB_READSTRING", PbReadString);
ScriptEngine::RegisterNativeHandler("PB_READBYTES", PbReadBytes);
ScriptEngine::RegisterNativeHandler("PB_READBYTESLENGTH", PbReadBytesLength);
ScriptEngine::RegisterNativeHandler("PB_GETREPEATEDFIELDCOUNT", PbGetRepeatedFieldCount);
ScriptEngine::RegisterNativeHandler("PB_SETINT", PbSetInt);
ScriptEngine::RegisterNativeHandler("PB_SETINT64", PbSetInt64);
ScriptEngine::RegisterNativeHandler("PB_SETFLOAT", PbSetFloat);
ScriptEngine::RegisterNativeHandler("PB_SETBOOL", PbSetBool);
ScriptEngine::RegisterNativeHandler("PB_SETSTRING", PbSetString);
ScriptEngine::RegisterNativeHandler("PB_SETBYTES", PbSetBytes);
ScriptEngine::RegisterNativeHandler("PB_ADDINT", PbAddInt);
ScriptEngine::RegisterNativeHandler("PB_ADDINT64", PbAddInt64);
ScriptEngine::RegisterNativeHandler("PB_ADDFLOAT", PbAddFloat);
ScriptEngine::RegisterNativeHandler("PB_ADDBOOL", PbAddBool);
ScriptEngine::RegisterNativeHandler("PB_ADDSTRING", PbAddString);
ScriptEngine::RegisterNativeHandler("PB_ADDBYTES", PbAddBytes);
ScriptEngine::RegisterNativeHandler("PB_REMOVEREPEATEDFIELDVALUE", PbRemoveRepeatedFieldValue);
// ScriptEngine::RegisterNativeHandler("PB_READMESSAGE", PbReadMessage);
// ScriptEngine::RegisterNativeHandler("PB_READREPEATEDMESSAGE", PbReadRepeatedMessage);
Expand Down
4 changes: 4 additions & 0 deletions src/scripting/natives/natives_usermessages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ PB_READINT64: message:UserMessage, name:string, index:int -> long
PB_READFLOAT: message:UserMessage, name:string, index:int -> float
PB_READBOOL: message:UserMessage, name:string, index:int -> bool
PB_READSTRING: message:UserMessage, name:string, index:int -> string
PB_READBYTES: message:UserMessage, name:string, buffer:pointer, size:int, index:int -> int
PB_READBYTESLENGTH: message:UserMessage, name:string, index:int -> int
PB_GETREPEATEDFIELDCOUNT: message:UserMessage, name:string -> int
PB_SETINT: message:UserMessage, name:string, value:int, index:int -> void
PB_SETINT64: message:UserMessage, name:string, value:long, index:int -> void
PB_SETFLOAT: message:UserMessage, name:string, value:float, index:int -> void
PB_SETBOOL: message:UserMessage, name:string, value:bool, index:int -> void
PB_SETSTRING: message:UserMessage, name:string, value:string, index:int -> void
PB_SETBYTES: message:UserMessage, name:string, buffer:pointer, size:int, index:int -> void
PB_ADDINT: message:UserMessage, name:string, value:int -> void
PB_ADDINT64: message:UserMessage, name:string, value:int64 -> void
PB_ADDFLOAT: message:UserMessage, name:string, value:float -> void
PB_ADDBOOL: message:UserMessage, name:string, value:bool -> void
PB_ADDSTRING: message:UserMessage, name:string, value:string -> void
PB_ADDBYTES: message:UserMessage, name:string, buffer:pointer, size:int -> void
PB_REMOVEREPEATEDFIELDVALUE: message:UserMessage, name:string, index:int -> void
#PB_READMESSAGE: message:UserMessage, name:string -> UserMessage
#PB_READREPEATEDMESSAGE: message:UserMessage, name:string, index:int -> UserMessage
Expand Down
Loading