Skip to content

Commit

Permalink
fix(server): rpc string as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Doxoh committed Feb 17, 2024
1 parent 8500450 commit 5744452
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
8 changes: 4 additions & 4 deletions api/AltV.Net.Async/AsyncCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -968,13 +968,13 @@ public override void OnPlayerStopTalkingEvent(IPlayer player)
});
}

public override void OnScriptRPCEvent(IntPtr eventpointer, IPlayer target, string name, IntPtr[] args, ushort answerId, bool async)
public override void OnScriptRPCEvent(IntPtr eventpointer, IPlayer target, string name, object[] objects, ushort answerId, bool async)
{
if (!UnansweredServerRpcRequest.Contains(answerId))
{
UnansweredServerRpcRequest.Add(answerId);
}
base.OnScriptRPCEvent(eventpointer, target, name, args, answerId, true);
base.OnScriptRPCEvent(eventpointer, target, name, objects, answerId, true);

if (UnansweredServerRpcRequest.Contains(answerId))
{
Expand All @@ -985,11 +985,11 @@ public override void OnScriptRPCEvent(IntPtr eventpointer, IPlayer target, strin
}

if (!ScriptRpcAsyncEventHandler.HasEvents()) return;

Task.Run(async () =>
{
var mValues = MValueConst.CreateFrom(this, args);
var clientScriptRPCEvent = new AsyncScriptRpcEvent(target, answerId);
await ScriptRpcAsyncEventHandler.CallAsync(@delegate => @delegate(clientScriptRPCEvent, target, name, mValues.Select(x => x.ToObject()).ToArray(), answerId));
await ScriptRpcAsyncEventHandler.CallAsync(@delegate => @delegate(clientScriptRPCEvent, target, name, objects, answerId));
});

if (UnansweredServerRpcRequest.Contains(answerId))
Expand Down
26 changes: 18 additions & 8 deletions api/AltV.Net/Core.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2633,7 +2633,7 @@ public virtual void OnPlayerStopTalkingEvent(IPlayer player)
}
}

public void OnScriptRPC(IntPtr eventpointer, IntPtr targetpointer, string name, IntPtr pointer, ulong size, ushort answerId)
public void OnScriptRPC(IntPtr eventpointer, IntPtr targetpointer, string name, IntPtr[] args, ushort answerId)
{
var target = PoolManager.Player.Get(targetpointer);
if (target == null)
Expand All @@ -2642,28 +2642,38 @@ public void OnScriptRPC(IntPtr eventpointer, IntPtr targetpointer, string name,
return;
}

var args = new IntPtr[size];
if (pointer != IntPtr.Zero)
var length = args.Length;
var mValues = new MValueConst[length];
for (var i = 0; i < length; i++)
{
Marshal.Copy(pointer, args, 0, (int) size);
mValues[i] = new MValueConst(this, args[i]);
}

OnScriptRPCEvent(eventpointer, target, name, args, answerId, false);
var objects = new object[length];
for (var i = 0; i < length; i++)
{
objects[i] = mValues[i].ToObject();
}

OnScriptRPCEvent(eventpointer, target, name, objects, answerId, false);
}

public virtual void OnScriptRPCEvent(IntPtr eventpointer, IPlayer target, string name, IntPtr[] args, ushort answerId, bool async)
public virtual void OnScriptRPCEvent(IntPtr eventpointer, IPlayer target, string name, object[] objects, ushort answerId, bool async)
{
if (!UnansweredServerRpcRequest.Contains(answerId))
{
UnansweredServerRpcRequest.Add(answerId);
}
var mValues = MValueConst.CreateFrom(this, args);

if (!ScriptRpcHandler.HasEvents()) return;

var clientScriptRPCEvent = new ScriptRpcEvent(this, eventpointer, answerId, false);

foreach (var @delegate in ScriptRpcHandler.GetEvents())
{
try
{
@delegate(clientScriptRPCEvent, target, name, mValues.Select(x => x.ToObject()).ToArray(), answerId);
@delegate(clientScriptRPCEvent, target, name, objects, answerId);
}
catch (TargetInvocationException exception)
{
Expand Down
9 changes: 7 additions & 2 deletions api/AltV.Net/ModuleWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,14 @@ public static void OnPlayerStopTalking(IntPtr playerpointer)
_core.OnPlayerStopTalking(playerpointer);
}

public static void OnScriptRPC(IntPtr eventpointer, IntPtr targetpointer, string name, IntPtr args, ulong size, ushort answerId)
public static void OnScriptRPC(IntPtr eventpointer, IntPtr targetpointer, string name, IntPtr pointer, ulong size, ushort answerId)
{
_core.OnScriptRPC(eventpointer, targetpointer, name, args, size, answerId);
var args = new IntPtr[size];
if (pointer != IntPtr.Zero)
{
Marshal.Copy(pointer, args, 0, (int) size);
}
_core.OnScriptRPC(eventpointer, targetpointer, name, args, answerId);
}

public static void OnScriptRPCAnswer(IntPtr targetpointer, ushort answerid, IntPtr answer, string answererror)
Expand Down
2 changes: 1 addition & 1 deletion runtime

0 comments on commit 5744452

Please sign in to comment.