diff --git a/RageCoop.Server/Scripting/API.cs b/RageCoop.Server/Scripting/API.cs index c98db48..c9c24f4 100644 --- a/RageCoop.Server/Scripting/API.cs +++ b/RageCoop.Server/Scripting/API.cs @@ -134,7 +134,7 @@ namespace RageCoop.Server.Scripting /// 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 /// 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 for a list of supported types. /// The target clients to send. Leave it null to send to all clients - public void SendCustomEvent(string name, List args = null, List targets = null) + public void SendCustomEvent(string name, List targets = null, List args = null) { targets ??= new(Server.Clients.Values); var p = new Packets.CustomEvent() @@ -265,13 +265,39 @@ namespace RageCoop.Server.Scripting } } + /// + /// Send native call specified clients. + /// + /// + /// + /// /// Clients to send, null for all clients + public void SendNativeCall(GTA.Native.Hash hash, List clients, List args) + { + var argsList = new List(args); + argsList.InsertRange(0, new object[] { (byte)TypeCode.Empty, (ulong)hash }); + SendCustomEvent(CustomEvents.NativeCall,clients, argsList); + } + + /// + /// Send native call specified clients. + /// + /// + /// + /// /// Clients to send, null for all clients + public void SendNativeCall(GTA.Native.Hash hash, List clients = null, params object[] args) + { + var argsList = new List(args); + argsList.InsertRange(0, new object[] { (byte)TypeCode.Empty, (ulong)hash }); + SendCustomEvent(CustomEvents.NativeCall, clients, argsList); + } + /// /// Send an event and data to the specified clients. Use if you want to send event to individual client. /// /// An unique identifier of the event, you can use to get it from a string /// The objects conataing your data, see for supported types. /// The target clients to send. Leave it null to send to all clients - public void SendCustomEvent(int eventHash,List args=null,List targets=null) + public void SendCustomEvent(int eventHash, List targets = null, List 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); } } + + /// + /// Send an event and data to the specified clients. Use if you want to send event to individual client. + /// + /// An unique identifier of the event, you can use to get it from a string + /// The objects conataing your data, see for supported types. + /// The target clients to send. Leave it null to send to all clients + public void SendCustomEvent(int eventHash, List targets, params object[] args) + { + SendCustomEvent(eventHash,targets,new List(args)); + } /// /// Register an handler to the specifed event hash, one event can have multiple handlers. /// diff --git a/RageCoop.Server/Scripting/BaseScript.cs b/RageCoop.Server/Scripting/BaseScript.cs index bbb9d88..60862ea 100644 --- a/RageCoop.Server/Scripting/BaseScript.cs +++ b/RageCoop.Server/Scripting/BaseScript.cs @@ -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 objects, List 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) diff --git a/RageCoop.Server/Server.cs b/RageCoop.Server/Server.cs index a461742..8c90604 100644 --- a/RageCoop.Server/Server.cs +++ b/RageCoop.Server/Server.cs @@ -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, }; diff --git a/RageCoop.Server/ServerEntities.cs b/RageCoop.Server/ServerEntities.cs index 89045aa..cbdd1fa 100644 --- a/RageCoop.Server/ServerEntities.cs +++ b/RageCoop.Server/ServerEntities.cs @@ -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; diff --git a/RageCoop.Server/ServerObject.cs b/RageCoop.Server/ServerObject.cs index 01792d4..6a12ada 100644 --- a/RageCoop.Server/ServerObject.cs +++ b/RageCoop.Server/ServerObject.cs @@ -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 /// public abstract class ServerObject { - internal ServerObject() { } + /// + /// Server that this object belongs to + /// + protected readonly Server Server; + internal ServerObject(Server server) { Server=server; } /// /// 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; /// /// Send updated information to clients, would be called automatically. /// - public virtual void Update() { - Owner.SendCustomEvent(CustomEvents.SetEntity, Handle, Position, Rotation); + internal virtual void Update() { + Owner?.SendCustomEvent(CustomEvents.SetEntity, Handle, Position, Rotation); } /// @@ -118,33 +123,44 @@ namespace RageCoop.Server /// public class ServerProp : ServerObject { - private Server Server; - internal ServerProp(Server server) - { - Server= server; - } + internal ServerProp(Server server) : base(server) { } - /// /// Delete this prop /// 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); } - + /// + /// Gets or sets this object's position + /// + 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); } + } + + /// + /// Gets or sets this object's rotation + /// + 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); } + } /// /// Send updated information to clients, would be called automatically. /// - 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 /// public class ServerPed : ServerObject { - internal ServerPed() - { - - } + internal ServerPed(Server server) : base(server) { } /// /// Get the ped's last vehicle @@ -173,24 +186,26 @@ namespace RageCoop.Server /// public class ServerVehicle : ServerObject { - internal ServerVehicle() - { - - } + internal ServerVehicle(Server server) : base(server) { } /// /// Gets or sets vehicle rotation /// 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; /// /// Get this vehicle's quaternion /// - 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); } + } } /// @@ -273,7 +288,7 @@ namespace RageCoop.Server /// public void Delete() { - Server.API.SendCustomEvent(CustomEvents.DeleteServerBlip, new() { ID }); + Server.API.SendCustomEvent(CustomEvents.DeleteServerBlip, null,ID); Server.Entities.RemoveServerBlip(ID); }