Change parameter order for SendCustomEvent and SendNativecall

Use NativeCall to update entity
This commit is contained in:
Sardelka 2022-07-04 09:41:00 +08:00
parent 0a5bb67c54
commit 8ac5cc40c1
5 changed files with 90 additions and 38 deletions

View File

@ -134,7 +134,7 @@ namespace RageCoop.Server.Scripting
/// </summary>
public class API
{
private readonly Server Server;
internal readonly Server Server;
internal API(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="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(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);
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>
/// 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<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);
var p = new Packets.CustomEvent()
@ -284,6 +310,17 @@ namespace RageCoop.Server.Scripting
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>
/// Register an handler to the specifed event hash, one event can have multiple handlers.
/// </summary>

View File

@ -41,14 +41,14 @@ namespace RageCoop.Server.Scripting
{
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)
{
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)

View File

@ -596,7 +596,7 @@ namespace RageCoop.Server
// Add the player to Players
lock (Clients)
{
var player = new ServerPed
var player = new ServerPed(this)
{
ID= packet.PedID,
};

View File

@ -180,7 +180,7 @@ namespace RageCoop.Server
ServerPed 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._pos = p.Position;
@ -194,19 +194,19 @@ namespace RageCoop.Server
ServerVehicle 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._pos = p.Position;
veh.Owner=sender;
veh.Quaternion=p.Quaternion;
veh._quat=p.Quaternion;
}
internal void Update(Packets.VehicleStateSync p, Client sender)
{
ServerVehicle 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.Owner=sender;

View File

@ -9,6 +9,7 @@ using GTA.Native;
using GTA.Math;
using RageCoop.Core;
using RageCoop.Core.Scripting;
using RageCoop.Server.Scripting;
namespace RageCoop.Server
{
@ -18,7 +19,11 @@ namespace RageCoop.Server
/// </summary>
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>
/// 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
{
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;
@ -77,15 +82,15 @@ namespace RageCoop.Server
public virtual Vector3 Rotation
{
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;
/// <summary>
/// Send updated information to clients, would be called automatically.
/// </summary>
public virtual void Update() {
Owner.SendCustomEvent(CustomEvents.SetEntity, Handle, Position, Rotation);
internal virtual void Update() {
Owner?.SendCustomEvent(CustomEvents.SetEntity, Handle, Position, Rotation);
}
/// <summary>
@ -118,33 +123,44 @@ namespace RageCoop.Server
/// </summary>
public class ServerProp : ServerObject
{
private Server Server;
internal ServerProp(Server server)
{
Server= server;
}
internal ServerProp(Server server) : base(server) { }
/// <summary>
/// Delete this prop
/// </summary>
public override void Delete()
{
Server.API.SendCustomEvent(CustomEvents.DeleteServerProp, new() { ID });
Server.Entities.RemoveProp(ID);
Server.API.SendCustomEvent(CustomEvents.DeleteServerProp, null,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>
/// Send updated information to clients, would be called automatically.
/// </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>
public class ServerPed : ServerObject
{
internal ServerPed()
{
}
internal ServerPed(Server server) : base(server) { }
/// <summary>
/// Get the ped's last vehicle
@ -173,24 +186,26 @@ namespace RageCoop.Server
/// </summary>
public class ServerVehicle : ServerObject
{
internal ServerVehicle()
{
}
internal ServerVehicle(Server server) : base(server) { }
/// <summary>
/// Gets or sets vehicle rotation
/// </summary>
public override Vector3 Rotation
{
get { return Quaternion.ToEulerAngles().ToDegree(); }
set { Quaternion=value.ToQuaternion(); Update(); }
get { return _quat.ToEulerAngles().ToDegree(); }
set { Owner.SendNativeCall(Hash.SET_ENTITY_ROTATION, Handle ,value.X, value.Y ,value.Z); }
}
internal Quaternion _quat;
/// <summary>
/// Get this vehicle's quaternion
/// </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>
@ -273,7 +288,7 @@ namespace RageCoop.Server
/// </summary>
public void Delete()
{
Server.API.SendCustomEvent(CustomEvents.DeleteServerBlip, new() { ID });
Server.API.SendCustomEvent(CustomEvents.DeleteServerBlip, null,ID);
Server.Entities.RemoveServerBlip(ID);
}