Change object list to array for CustomEvent
This commit is contained in:
@ -267,6 +267,7 @@ namespace RageCoop.Client
|
|||||||
{
|
{
|
||||||
Packets.CustomEvent packet = new Packets.CustomEvent(_resolveHandle);
|
Packets.CustomEvent packet = new Packets.CustomEvent(_resolveHandle);
|
||||||
packet.Unpack(data);
|
packet.Unpack(data);
|
||||||
|
Main.Logger.Debug(packet.Args.DumpWithType());
|
||||||
Scripting.API.Events.InvokeCustomEventReceived(packet);
|
Scripting.API.Events.InvokeCustomEventReceived(packet);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -19,7 +19,7 @@ namespace RageCoop.Client.Scripting
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Supported types: byte, short, ushort, int, uint, long, ulong, float, bool, string, Vector3, Quaternion
|
/// Supported types: byte, short, ushort, int, uint, long, ulong, float, bool, string, Vector3, Quaternion
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<object> Args { get; set; }
|
public object[] Args { get; set; }
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides vital functionality to interact with RAGECOOP
|
/// Provides vital functionality to interact with RAGECOOP
|
||||||
@ -241,25 +241,11 @@ namespace RageCoop.Client.Scripting
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="eventHash">An unique identifier of the event</param>
|
/// <param name="eventHash">An unique identifier of the event</param>
|
||||||
/// <param name="args">The objects conataing your data, see <see cref="CustomEventReceivedArgs"/> for a list of supported types</param>
|
/// <param name="args">The objects conataing your data, see <see cref="CustomEventReceivedArgs"/> for a list of supported types</param>
|
||||||
public static void SendCustomEvent(int eventHash, List<object> args)
|
|
||||||
{
|
|
||||||
var p = new Packets.CustomEvent()
|
|
||||||
{
|
|
||||||
Args=args,
|
|
||||||
Hash=eventHash
|
|
||||||
};
|
|
||||||
Networking.Send(p, ConnectionChannel.Event, Lidgren.Network.NetDeliveryMethod.ReliableOrdered);
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Send an event and data to the server.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="eventHash"></param>
|
|
||||||
/// <param name="args"></param>
|
|
||||||
public static void SendCustomEvent(int eventHash, params object[] args)
|
public static void SendCustomEvent(int eventHash, params object[] args)
|
||||||
{
|
{
|
||||||
var p = new Packets.CustomEvent()
|
var p = new Packets.CustomEvent()
|
||||||
{
|
{
|
||||||
Args=new List<object>(args),
|
Args=args,
|
||||||
Hash=eventHash
|
Hash=eventHash
|
||||||
};
|
};
|
||||||
Networking.Send(p, ConnectionChannel.Event, Lidgren.Network.NetDeliveryMethod.ReliableOrdered);
|
Networking.Send(p, ConnectionChannel.Event, Lidgren.Network.NetDeliveryMethod.ReliableOrdered);
|
||||||
|
@ -168,7 +168,7 @@ namespace RageCoop.Client.Scripting
|
|||||||
TypeCode returnType=(TypeCode)ty;
|
TypeCode returnType=(TypeCode)ty;
|
||||||
i = returnType==TypeCode.Empty ? 1 : 2;
|
i = returnType==TypeCode.Empty ? 1 : 2;
|
||||||
var hash = (Hash)e.Args[i++];
|
var hash = (Hash)e.Args[i++];
|
||||||
for(; i<e.Args.Count;i++)
|
for(; i<e.Args.Length;i++)
|
||||||
{
|
{
|
||||||
arguments.Add(GetInputArgument(e.Args[i]));
|
arguments.Add(GetInputArgument(e.Args[i]));
|
||||||
}
|
}
|
||||||
|
@ -15,16 +15,17 @@ namespace RageCoop.Core
|
|||||||
}
|
}
|
||||||
private Func<byte, BitReader, object> _resolve { get; set; }
|
private Func<byte, BitReader, object> _resolve { get; set; }
|
||||||
public int Hash { get; set; }
|
public int Hash { get; set; }
|
||||||
public List<object> Args { get; set; }
|
public bool IsStaged { get; set; }=false;
|
||||||
|
public object[] Args { get; set; }
|
||||||
|
|
||||||
public override void Pack(NetOutgoingMessage message)
|
public override void Pack(NetOutgoingMessage message)
|
||||||
{
|
{
|
||||||
Args= Args ?? new List<object>(0);
|
Args= Args ?? new object[] { };
|
||||||
message.Write((byte)PacketType.CustomEvent);
|
message.Write((byte)PacketType.CustomEvent);
|
||||||
|
|
||||||
List<byte> result = new List<byte>();
|
List<byte> result = new List<byte>();
|
||||||
result.AddInt(Hash);
|
result.AddInt(Hash);
|
||||||
result.AddInt(Args.Count);
|
result.AddInt(Args.Length);
|
||||||
(byte, byte[]) tup;
|
(byte, byte[]) tup;
|
||||||
foreach (var arg in Args)
|
foreach (var arg in Args)
|
||||||
{
|
{
|
||||||
@ -47,40 +48,40 @@ namespace RageCoop.Core
|
|||||||
|
|
||||||
Hash = reader.ReadInt();
|
Hash = reader.ReadInt();
|
||||||
var len=reader.ReadInt();
|
var len=reader.ReadInt();
|
||||||
Args=new List<object>(len);
|
Args=new object[len];
|
||||||
for (int i = 0; i < len; i++)
|
for (int i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
byte type = reader.ReadByte();
|
byte type = reader.ReadByte();
|
||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case 0x01:
|
case 0x01:
|
||||||
Args.Add(reader.ReadByte()); break;
|
Args[i]=reader.ReadByte(); break;
|
||||||
case 0x02:
|
case 0x02:
|
||||||
Args.Add(reader.ReadShort()); break;
|
Args[i]=reader.ReadShort(); break;
|
||||||
case 0x03:
|
case 0x03:
|
||||||
Args.Add(reader.ReadUShort()); break;
|
Args[i]=reader.ReadUShort(); break;
|
||||||
case 0x04:
|
case 0x04:
|
||||||
Args.Add(reader.ReadInt()); break;
|
Args[i]=reader.ReadInt(); break;
|
||||||
case 0x05:
|
case 0x05:
|
||||||
Args.Add(reader.ReadUInt()); break;
|
Args[i]=reader.ReadUInt(); break;
|
||||||
case 0x06:
|
case 0x06:
|
||||||
Args.Add(reader.ReadLong()); break;
|
Args[i]=reader.ReadLong(); break;
|
||||||
case 0x07:
|
case 0x07:
|
||||||
Args.Add(reader.ReadULong()); break;
|
Args[i]=reader.ReadULong(); break;
|
||||||
case 0x08:
|
case 0x08:
|
||||||
Args.Add(reader.ReadFloat()); break;
|
Args[i]=reader.ReadFloat(); break;
|
||||||
case 0x09:
|
case 0x09:
|
||||||
Args.Add(reader.ReadBool()); break;
|
Args[i]=reader.ReadBool(); break;
|
||||||
case 0x10:
|
case 0x10:
|
||||||
Args.Add(reader.ReadString()); break;
|
Args[i]=reader.ReadString(); break;
|
||||||
case 0x11:
|
case 0x11:
|
||||||
Args.Add(reader.ReadVector3()); break;
|
Args[i]=reader.ReadVector3(); break;
|
||||||
case 0x12:
|
case 0x12:
|
||||||
Args.Add(reader.ReadQuaternion()); break;
|
Args[i]=reader.ReadQuaternion(); break;
|
||||||
case 0x13:
|
case 0x13:
|
||||||
Args.Add((GTA.Model)reader.ReadInt()); break;
|
Args[i]=(GTA.Model)reader.ReadInt(); break;
|
||||||
case 0x14:
|
case 0x14:
|
||||||
Args.Add(reader.ReadVector2()); break;
|
Args[i]=reader.ReadVector2(); break;
|
||||||
default:
|
default:
|
||||||
if (_resolve==null)
|
if (_resolve==null)
|
||||||
{
|
{
|
||||||
@ -88,7 +89,7 @@ namespace RageCoop.Core
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Args.Add(_resolve(type, reader)); break;
|
Args[i]=_resolve(type, reader); break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -159,7 +159,7 @@ namespace RageCoop.Server
|
|||||||
var argsList= new List<object>(args);
|
var argsList= new List<object>(args);
|
||||||
argsList.InsertRange(0, new object[] { (byte)Type.GetTypeCode(typeof(T)), RequestNativeCallID<T>(callBack), (ulong)hash });
|
argsList.InsertRange(0, new object[] { (byte)Type.GetTypeCode(typeof(T)), RequestNativeCallID<T>(callBack), (ulong)hash });
|
||||||
|
|
||||||
SendCustomEvent(CustomEvents.NativeCall, argsList);
|
SendCustomEvent(CustomEvents.NativeCall, argsList.ToArray());
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Send a native call to client and ignore it's response.
|
/// Send a native call to client and ignore it's response.
|
||||||
@ -171,7 +171,7 @@ namespace RageCoop.Server
|
|||||||
var argsList = new List<object>(args);
|
var argsList = new List<object>(args);
|
||||||
argsList.InsertRange(0, new object[] { (byte)TypeCode.Empty,(ulong)hash });
|
argsList.InsertRange(0, new object[] { (byte)TypeCode.Empty,(ulong)hash });
|
||||||
// Server.Logger?.Debug(argsList.DumpWithType());
|
// Server.Logger?.Debug(argsList.DumpWithType());
|
||||||
SendCustomEvent(CustomEvents.NativeCall, argsList);
|
SendCustomEvent(CustomEvents.NativeCall, argsList.ToArray());
|
||||||
}
|
}
|
||||||
private int RequestNativeCallID<T>(Action<object> callback)
|
private int RequestNativeCallID<T>(Action<object> callback)
|
||||||
{
|
{
|
||||||
@ -197,7 +197,7 @@ namespace RageCoop.Server
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="hash">An unique identifier of the event, you can use <see cref="CustomEvents.Hash(string)"/> to get it from a string</param>
|
/// <param name="hash">An unique identifier of the event, you can use <see cref="CustomEvents.Hash(string)"/> to get it from a string</param>
|
||||||
/// <param name="args">Arguments</param>
|
/// <param name="args">Arguments</param>
|
||||||
public void SendCustomEvent(int hash,List<object> args)
|
public void SendCustomEvent(int hash,params object[] args)
|
||||||
{
|
{
|
||||||
if (!IsReady)
|
if (!IsReady)
|
||||||
{
|
{
|
||||||
@ -221,35 +221,6 @@ namespace RageCoop.Server
|
|||||||
Server.Logger?.Error(ex);
|
Server.Logger?.Error(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
|
||||||
/// Trigger a CustomEvent for this client
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="hash">An unique identifier of the event, you can use <see cref="CustomEvents.Hash(string)"/> to get it from a string</param>
|
|
||||||
/// <param name="args">Arguments</param>
|
|
||||||
public void SendCustomEvent(int hash,params object[] args)
|
|
||||||
{
|
|
||||||
if (!IsReady)
|
|
||||||
{
|
|
||||||
Server.Logger?.Warning($"Player \"{Username}\" is not ready!");
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
|
|
||||||
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
|
|
||||||
new Packets.CustomEvent()
|
|
||||||
{
|
|
||||||
Hash=hash,
|
|
||||||
Args=new(args)
|
|
||||||
}.Pack(outgoingMessage);
|
|
||||||
Server.MainNetServer.SendMessage(outgoingMessage, Connection, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.Event);
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Server.Logger?.Error(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
@ -245,38 +245,6 @@ namespace RageCoop.Server.Scripting
|
|||||||
(ctx) => { method.Invoke(obj, new object[] { ctx }); });
|
(ctx) => { method.Invoke(obj, new object[] { ctx }); });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// <summary>
|
|
||||||
/// Send an event and data to the specified clients. Use <see cref="Client.SendCustomEvent(int, List{object})"/> if you want to send event to individual client.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="name">The name of the event, will be hashed to an int. For optimal performence, you should hash it in a static contructor inside the shared library, then call <see cref="SendCustomEvent(int, List{object}, List{Client})"/>.</param>
|
|
||||||
/// <param name="args">See <see cref="CustomEventReceivedArgs"/> for a list of supported types.</param>
|
|
||||||
/// <param name="targets">The target clients to send. Leave it null to send to all clients</param>
|
|
||||||
public void SendCustomEvent(List<Client> targets, string name, List<object> args = null)
|
|
||||||
{
|
|
||||||
targets ??= new(Server.Clients.Values);
|
|
||||||
var p = new Packets.CustomEvent()
|
|
||||||
{
|
|
||||||
Args=args,
|
|
||||||
Hash=CustomEvents.Hash(name)
|
|
||||||
};
|
|
||||||
foreach (var c in targets)
|
|
||||||
{
|
|
||||||
Server.Send(p, c, ConnectionChannel.Event, NetDeliveryMethod.ReliableOrdered);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Send native call specified clients.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="hash"></param>
|
|
||||||
/// <param name="args"></param>
|
|
||||||
/// /// <param name="clients">Clients to send, null for all clients</param>
|
|
||||||
public void SendNativeCall(List<Client> clients, GTA.Native.Hash hash, List<object> args)
|
|
||||||
{
|
|
||||||
var argsList = new List<object>(args);
|
|
||||||
argsList.InsertRange(0, new object[] { (byte)TypeCode.Empty, (ulong)hash });
|
|
||||||
SendCustomEvent(clients, CustomEvents.NativeCall, argsList);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Send native call specified clients.
|
/// Send native call specified clients.
|
||||||
@ -288,17 +256,19 @@ namespace RageCoop.Server.Scripting
|
|||||||
{
|
{
|
||||||
var argsList = new List<object>(args);
|
var argsList = new List<object>(args);
|
||||||
argsList.InsertRange(0, new object[] { (byte)TypeCode.Empty, (ulong)hash });
|
argsList.InsertRange(0, new object[] { (byte)TypeCode.Empty, (ulong)hash });
|
||||||
SendCustomEvent(clients, CustomEvents.NativeCall, argsList);
|
SendCustomEvent(clients, CustomEvents.NativeCall, argsList.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Send an event and data to the specified clients. Use <see cref="Client.SendCustomEvent(int, List{object})"/> if you want to send event to individual client.
|
/// Send an event and data to the specified clients. Use <see cref="Client.SendCustomEvent(int, List{object})"/> if you want to send event to individual client.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="eventHash">An unique identifier of the event, you can use <see cref="CustomEvents.Hash(string)"/> to get it from a string</param>
|
/// <param name="eventHash">An unique identifier of the event, you can use <see cref="CustomEvents.Hash(string)"/> to get it from a string</param>
|
||||||
/// <param name="args">The objects conataing your data, see <see cref="Scripting.CustomEventReceivedArgs.Args"/> for supported types.</param>
|
/// <param name="args">The objects conataing your data, see <see cref="Scripting.CustomEventReceivedArgs.Args"/> for supported types.</param>
|
||||||
/// <param name="targets">The target clients to send. Leave it null to send to all clients</param>
|
/// <param name="targets">The target clients to send. Leave it null to send to all clients</param>
|
||||||
public void SendCustomEvent(List<Client> targets , int eventHash, List<object> args=null)
|
public void SendCustomEvent(List<Client> targets, int eventHash, params object[] args)
|
||||||
{
|
{
|
||||||
|
|
||||||
targets ??= new(Server.Clients.Values);
|
targets ??= new(Server.Clients.Values);
|
||||||
var p = new Packets.CustomEvent()
|
var p = new Packets.CustomEvent()
|
||||||
{
|
{
|
||||||
@ -310,17 +280,6 @@ namespace RageCoop.Server.Scripting
|
|||||||
Server.Send(p, c, ConnectionChannel.Event, NetDeliveryMethod.ReliableOrdered);
|
Server.Send(p, c, ConnectionChannel.Event, NetDeliveryMethod.ReliableOrdered);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Send an event and data to the specified clients. Use <see cref="Client.SendCustomEvent(int, List{object})"/> if you want to send event to individual client.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="eventHash">An unique identifier of the event, you can use <see cref="CustomEvents.Hash(string)"/> to get it from a string</param>
|
|
||||||
/// <param name="args">The objects conataing your data, see <see cref="Scripting.CustomEventReceivedArgs.Args"/> for supported types.</param>
|
|
||||||
/// <param name="targets">The target clients to send. Leave it null to send to all clients</param>
|
|
||||||
public void SendCustomEvent(List<Client> targets, int eventHash, params object[] args)
|
|
||||||
{
|
|
||||||
SendCustomEvent(targets, eventHash,new List<object>(args));
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Register an handler to the specifed event hash, one event can have multiple handlers.
|
/// Register an handler to the specifed event hash, one event can have multiple handlers.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -39,7 +39,7 @@ namespace RageCoop.Server.Scripting
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Supported types: byte, short, ushort, int, uint, long, ulong, float, bool, string, Vector3, Quaternion, Vector2 <see cref="ServerObject.Handle"/>
|
/// Supported types: byte, short, ushort, int, uint, long, ulong, float, bool, string, Vector3, Quaternion, Vector2 <see cref="ServerObject.Handle"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<object> Args { get; set; }
|
public object[] Args { get; set; }
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
|
Reference in New Issue
Block a user