Skip to content

Commit f72e6d5

Browse files
authored
feat: add ReplicateConVar (#751)
1 parent c8bccb0 commit f72e6d5

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

managed/CounterStrikeSharp.API/Core/API.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,18 @@ public static void SetFakeClientConvarValue(int clientindex, string convarname,
206206
}
207207
}
208208

209+
public static void ReplicateConvar(int clientslot, string convarname, string convarvalue){
210+
lock (ScriptContext.GlobalScriptContext.Lock) {
211+
ScriptContext.GlobalScriptContext.Reset();
212+
ScriptContext.GlobalScriptContext.Push(clientslot);
213+
ScriptContext.GlobalScriptContext.Push(convarname);
214+
ScriptContext.GlobalScriptContext.Push(convarvalue);
215+
ScriptContext.GlobalScriptContext.SetIdentifier(0xC8728BEC);
216+
ScriptContext.GlobalScriptContext.Invoke();
217+
ScriptContext.GlobalScriptContext.CheckErrors();
218+
}
219+
}
220+
209221
public static T DynamicHookGetReturn<T>(IntPtr hook, int datatype){
210222
lock (ScriptContext.GlobalScriptContext.Lock) {
211223
ScriptContext.GlobalScriptContext.Reset();

managed/CounterStrikeSharp.API/Core/Model/CCSPlayerController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,9 @@ public VoiceFlags VoiceFlags
347347
{
348348
base.Teleport(position, angles, velocity);
349349
}
350+
351+
public void ReplicateConVar(string conVar, string value)
352+
{
353+
NativeAPI.ReplicateConvar(Slot, conVar, value);
354+
}
350355
}

src/scripting/natives/natives_commands.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
*/
1616

1717
#include <eiface.h>
18+
#include <networksystem/inetworkmessages.h>
1819

1920
#include "scripting/autonative.h"
2021
#include "scripting/callback_manager.h"
2122
#include "core/managers/con_command_manager.h"
2223
#include "core/managers/player_manager.h"
24+
#include "core/recipientfilters.h"
25+
#include "igameeventsystem.h"
2326
#include "scripting/script_engine.h"
2427
#include "core/log.h"
28+
#include <networkbasetypes.pb.h>
2529

2630
namespace counterstrikesharp {
2731

@@ -191,6 +195,25 @@ void SetConVarStringValue(ScriptContext& script_context)
191195
pCvar->values = reinterpret_cast<CVValue_t**>((char*)value);
192196
}
193197

198+
void ReplicateConVar(ScriptContext& script_context)
199+
{
200+
auto slot = script_context.GetArgument<int>(0);
201+
auto name = script_context.GetArgument<const char*>(1);
202+
auto value = script_context.GetArgument<const char*>(2);
203+
204+
INetworkMessageInternal* pNetMsg = globals::networkMessages->FindNetworkMessagePartial("SetConVar");
205+
auto msg = pNetMsg->AllocateMessage()->ToPB<CNETMsg_SetConVar>();
206+
207+
CMsg_CVars_CVar* cvarMsg = msg->mutable_convars()->add_cvars();
208+
cvarMsg->set_name(name);
209+
cvarMsg->set_value(value);
210+
211+
CSingleRecipientFilter filter(slot);
212+
globals::gameEventSystem->PostEventAbstract(-1, false, &filter, pNetMsg, msg, 0);
213+
214+
delete msg;
215+
}
216+
194217
REGISTER_NATIVES(commands, {
195218
ScriptEngine::RegisterNativeHandler("ADD_COMMAND", AddCommand);
196219
ScriptEngine::RegisterNativeHandler("REMOVE_COMMAND", RemoveCommand);
@@ -211,5 +234,6 @@ REGISTER_NATIVES(commands, {
211234
IssueClientCommandFromServer);
212235
ScriptEngine::RegisterNativeHandler("GET_CLIENT_CONVAR_VALUE", GetClientConVarValue);
213236
ScriptEngine::RegisterNativeHandler("SET_FAKE_CLIENT_CONVAR_VALUE", SetFakeClientConVarValue);
237+
ScriptEngine::RegisterNativeHandler("REPLICATE_CONVAR", ReplicateConVar);
214238
})
215239
} // namespace counterstrikesharp

src/scripting/natives/natives_commands.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ ISSUE_CLIENT_COMMAND_FROM_SERVER: slot:int,command:string -> void
1212
FIND_CONVAR: name:string -> pointer
1313
SET_CONVAR_STRING_VALUE: convar:pointer,value:string -> void
1414
GET_CLIENT_CONVAR_VALUE: clientIndex:int,convarName:string -> string
15-
SET_FAKE_CLIENT_CONVAR_VALUE: clientIndex:int,convarName:string,convarValue:string -> void
15+
SET_FAKE_CLIENT_CONVAR_VALUE: clientIndex:int,convarName:string,convarValue:string -> void
16+
REPLICATE_CONVAR: clientSlot:int,convarName:string,convarValue:string -> void

0 commit comments

Comments
 (0)