Change parameter order for SendCustomEvent and SendNativecall
Use NativeCall to update entity
This commit is contained in:
@ -134,7 +134,7 @@ namespace RageCoop.Server.Scripting
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class API
|
public class API
|
||||||
{
|
{
|
||||||
private readonly Server Server;
|
internal readonly Server Server;
|
||||||
internal API(Server server)
|
internal API(Server server)
|
||||||
{
|
{
|
||||||
Server=server;
|
Server=server;
|
||||||
@ -251,7 +251,7 @@ namespace RageCoop.Server.Scripting
|
|||||||
/// <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="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="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>
|
/// <param name="targets">The target clients to send. Leave it null to send to all clients</param>
|
||||||
public void SendCustomEvent(string name, List<object> args = null, List<Client> targets = null)
|
public void SendCustomEvent(string name, List<Client> targets = null, List<object> args = null)
|
||||||
{
|
{
|
||||||
targets ??= new(Server.Clients.Values);
|
targets ??= new(Server.Clients.Values);
|
||||||
var p = new Packets.CustomEvent()
|
var p = new Packets.CustomEvent()
|
||||||
@ -265,13 +265,39 @@ namespace RageCoop.Server.Scripting
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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(GTA.Native.Hash hash, List<Client> clients, List<object> args)
|
||||||
|
{
|
||||||
|
var argsList = new List<object>(args);
|
||||||
|
argsList.InsertRange(0, new object[] { (byte)TypeCode.Empty, (ulong)hash });
|
||||||
|
SendCustomEvent(CustomEvents.NativeCall,clients, argsList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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(GTA.Native.Hash hash, List<Client> clients = null, params object[] args)
|
||||||
|
{
|
||||||
|
var argsList = new List<object>(args);
|
||||||
|
argsList.InsertRange(0, new object[] { (byte)TypeCode.Empty, (ulong)hash });
|
||||||
|
SendCustomEvent(CustomEvents.NativeCall, clients, argsList);
|
||||||
|
}
|
||||||
|
|
||||||
/// <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(int eventHash,List<object> args=null,List<Client> targets=null)
|
public void SendCustomEvent(int eventHash, List<Client> targets = null, List<object> args=null)
|
||||||
{
|
{
|
||||||
targets ??= new(Server.Clients.Values);
|
targets ??= new(Server.Clients.Values);
|
||||||
var p = new Packets.CustomEvent()
|
var p = new Packets.CustomEvent()
|
||||||
@ -284,6 +310,17 @@ 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(int eventHash, List<Client> targets, params object[] args)
|
||||||
|
{
|
||||||
|
SendCustomEvent(eventHash,targets,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>
|
||||||
|
@ -41,14 +41,14 @@ namespace RageCoop.Server.Scripting
|
|||||||
{
|
{
|
||||||
foreach(var obj in objects)
|
foreach(var obj in objects)
|
||||||
{
|
{
|
||||||
API.SendCustomEvent(CustomEvents.ServerPropSync, new() { obj.ID, obj.Model ,obj.Position,obj.Rotation },clients);
|
API.SendCustomEvent(CustomEvents.ServerPropSync, clients,obj.ID, obj.Model ,obj.Position,obj.Rotation );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void SendServerBlipsTo(List<ServerBlip> objects, List<Client> clients = null)
|
public void SendServerBlipsTo(List<ServerBlip> objects, List<Client> clients = null)
|
||||||
{
|
{
|
||||||
foreach (var obj in objects)
|
foreach (var obj in objects)
|
||||||
{
|
{
|
||||||
API.SendCustomEvent(CustomEvents.ServerBlipSync, new() { obj.ID, (short)obj.Sprite, (byte)obj.Color, obj.Scale,obj.Position,obj.Rotation,obj.Name }, clients);
|
API.SendCustomEvent(CustomEvents.ServerBlipSync, clients, obj.ID, (short)obj.Sprite, (byte)obj.Color, obj.Scale,obj.Position,obj.Rotation,obj.Name );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void NativeResponse(CustomEventReceivedArgs e)
|
void NativeResponse(CustomEventReceivedArgs e)
|
||||||
|
@ -596,7 +596,7 @@ namespace RageCoop.Server
|
|||||||
// Add the player to Players
|
// Add the player to Players
|
||||||
lock (Clients)
|
lock (Clients)
|
||||||
{
|
{
|
||||||
var player = new ServerPed
|
var player = new ServerPed(this)
|
||||||
{
|
{
|
||||||
ID= packet.PedID,
|
ID= packet.PedID,
|
||||||
};
|
};
|
||||||
|
@ -180,7 +180,7 @@ namespace RageCoop.Server
|
|||||||
ServerPed ped;
|
ServerPed ped;
|
||||||
if(!Peds.TryGetValue(p.ID,out ped))
|
if(!Peds.TryGetValue(p.ID,out ped))
|
||||||
{
|
{
|
||||||
Peds.Add(p.ID,ped=new ServerPed());
|
Peds.Add(p.ID,ped=new ServerPed(Server));
|
||||||
ped.ID=p.ID;
|
ped.ID=p.ID;
|
||||||
}
|
}
|
||||||
ped._pos = p.Position;
|
ped._pos = p.Position;
|
||||||
@ -194,19 +194,19 @@ namespace RageCoop.Server
|
|||||||
ServerVehicle veh;
|
ServerVehicle veh;
|
||||||
if (!Vehicles.TryGetValue(p.ID, out veh))
|
if (!Vehicles.TryGetValue(p.ID, out veh))
|
||||||
{
|
{
|
||||||
Vehicles.Add(p.ID, veh=new ServerVehicle());
|
Vehicles.Add(p.ID, veh=new ServerVehicle(Server));
|
||||||
veh.ID=p.ID;
|
veh.ID=p.ID;
|
||||||
}
|
}
|
||||||
veh._pos = p.Position;
|
veh._pos = p.Position;
|
||||||
veh.Owner=sender;
|
veh.Owner=sender;
|
||||||
veh.Quaternion=p.Quaternion;
|
veh._quat=p.Quaternion;
|
||||||
}
|
}
|
||||||
internal void Update(Packets.VehicleStateSync p, Client sender)
|
internal void Update(Packets.VehicleStateSync p, Client sender)
|
||||||
{
|
{
|
||||||
ServerVehicle veh;
|
ServerVehicle veh;
|
||||||
if (!Vehicles.TryGetValue(p.ID, out veh))
|
if (!Vehicles.TryGetValue(p.ID, out veh))
|
||||||
{
|
{
|
||||||
Vehicles.Add(p.ID, veh=new ServerVehicle());
|
Vehicles.Add(p.ID, veh=new ServerVehicle(Server));
|
||||||
}
|
}
|
||||||
veh.ID=p.ID;
|
veh.ID=p.ID;
|
||||||
veh.Owner=sender;
|
veh.Owner=sender;
|
||||||
|
@ -9,6 +9,7 @@ using GTA.Native;
|
|||||||
using GTA.Math;
|
using GTA.Math;
|
||||||
using RageCoop.Core;
|
using RageCoop.Core;
|
||||||
using RageCoop.Core.Scripting;
|
using RageCoop.Core.Scripting;
|
||||||
|
using RageCoop.Server.Scripting;
|
||||||
|
|
||||||
namespace RageCoop.Server
|
namespace RageCoop.Server
|
||||||
{
|
{
|
||||||
@ -18,7 +19,11 @@ namespace RageCoop.Server
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class ServerObject
|
public abstract class ServerObject
|
||||||
{
|
{
|
||||||
internal ServerObject() { }
|
/// <summary>
|
||||||
|
/// Server that this object belongs to
|
||||||
|
/// </summary>
|
||||||
|
protected readonly Server Server;
|
||||||
|
internal ServerObject(Server server) { Server=server; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Pass this as an argument in CustomEvent or NativeCall to convert this object to handle at client side.
|
/// Pass this as an argument in CustomEvent or NativeCall to convert this object to handle at client side.
|
||||||
@ -67,7 +72,7 @@ namespace RageCoop.Server
|
|||||||
public virtual Vector3 Position
|
public virtual Vector3 Position
|
||||||
{
|
{
|
||||||
get { return _pos; }
|
get { return _pos; }
|
||||||
set { _pos=value; Update(); }
|
set { _pos=value; Owner.SendNativeCall(Hash.SET_ENTITY_COORDS_NO_OFFSET, Handle, value.X, value.Y, value.Z,1, 1,1 ); }
|
||||||
}
|
}
|
||||||
internal Vector3 _pos;
|
internal Vector3 _pos;
|
||||||
|
|
||||||
@ -77,15 +82,15 @@ namespace RageCoop.Server
|
|||||||
public virtual Vector3 Rotation
|
public virtual Vector3 Rotation
|
||||||
{
|
{
|
||||||
get { return _rot; }
|
get { return _rot; }
|
||||||
set { _rot=value; Update(); }
|
set { _rot=value; Owner.SendNativeCall(Hash.SET_ENTITY_ROTATION, Handle, value.X, value.Y, value.Z ,2,1); }
|
||||||
}
|
}
|
||||||
internal Vector3 _rot;
|
internal Vector3 _rot;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Send updated information to clients, would be called automatically.
|
/// Send updated information to clients, would be called automatically.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual void Update() {
|
internal virtual void Update() {
|
||||||
Owner.SendCustomEvent(CustomEvents.SetEntity, Handle, Position, Rotation);
|
Owner?.SendCustomEvent(CustomEvents.SetEntity, Handle, Position, Rotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -118,33 +123,44 @@ namespace RageCoop.Server
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class ServerProp : ServerObject
|
public class ServerProp : ServerObject
|
||||||
{
|
{
|
||||||
private Server Server;
|
|
||||||
internal ServerProp(Server server)
|
|
||||||
{
|
|
||||||
Server= server;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
internal ServerProp(Server server) : base(server) { }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Delete this prop
|
/// Delete this prop
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public override void Delete()
|
public override void Delete()
|
||||||
{
|
{
|
||||||
Server.API.SendCustomEvent(CustomEvents.DeleteServerProp, new() { ID });
|
Server.API.SendCustomEvent(CustomEvents.DeleteServerProp, null,ID);
|
||||||
Server.Entities.RemoveProp(ID);
|
Server.API.Entities.RemoveProp(ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets this object's position
|
||||||
|
/// </summary>
|
||||||
|
public override Vector3 Position
|
||||||
|
{
|
||||||
|
get { return _pos; }
|
||||||
|
set { _pos=value; Server.API.SendNativeCall(Hash.SET_ENTITY_COORDS_NO_OFFSET, clients:null ,Handle, value.X, value.Y, value.Z, 1, 1, 1); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets this object's rotation
|
||||||
|
/// </summary>
|
||||||
|
public override Vector3 Rotation
|
||||||
|
{
|
||||||
|
get { return _rot; }
|
||||||
|
set { _rot=value; Server.API.SendNativeCall(Hash.SET_ENTITY_ROTATION,null, Handle, value.X, value.Y, value.Z, 2, 1); }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Send updated information to clients, would be called automatically.
|
/// Send updated information to clients, would be called automatically.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public override void Update()
|
internal override void Update()
|
||||||
{
|
{
|
||||||
Server.BaseScript.SendServerPropsTo(new() { this });
|
Server.API.Server.BaseScript.SendServerPropsTo(new() { this });
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -153,10 +169,7 @@ namespace RageCoop.Server
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class ServerPed : ServerObject
|
public class ServerPed : ServerObject
|
||||||
{
|
{
|
||||||
internal ServerPed()
|
internal ServerPed(Server server) : base(server) { }
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the ped's last vehicle
|
/// Get the ped's last vehicle
|
||||||
@ -173,24 +186,26 @@ namespace RageCoop.Server
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class ServerVehicle : ServerObject
|
public class ServerVehicle : ServerObject
|
||||||
{
|
{
|
||||||
internal ServerVehicle()
|
internal ServerVehicle(Server server) : base(server) { }
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets vehicle rotation
|
/// Gets or sets vehicle rotation
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public override Vector3 Rotation
|
public override Vector3 Rotation
|
||||||
{
|
{
|
||||||
get { return Quaternion.ToEulerAngles().ToDegree(); }
|
get { return _quat.ToEulerAngles().ToDegree(); }
|
||||||
set { Quaternion=value.ToQuaternion(); Update(); }
|
set { Owner.SendNativeCall(Hash.SET_ENTITY_ROTATION, Handle ,value.X, value.Y ,value.Z); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal Quaternion _quat;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get this vehicle's quaternion
|
/// Get this vehicle's quaternion
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Quaternion Quaternion { get; internal set; }
|
public Quaternion Quaternion
|
||||||
|
{
|
||||||
|
get { return _quat; }
|
||||||
|
set { _quat = value ;Owner.SendNativeCall(Hash.SET_ENTITY_QUATERNION, Handle, value.X, value.Y, value.Z, value.W); }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -273,7 +288,7 @@ namespace RageCoop.Server
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void Delete()
|
public void Delete()
|
||||||
{
|
{
|
||||||
Server.API.SendCustomEvent(CustomEvents.DeleteServerBlip, new() { ID });
|
Server.API.SendCustomEvent(CustomEvents.DeleteServerBlip, null,ID);
|
||||||
Server.Entities.RemoveServerBlip(ID);
|
Server.Entities.RemoveServerBlip(ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user