From 55649f26aea65300f27e9422c4f64ab19d6d1e69 Mon Sep 17 00:00:00 2001 From: Sardelka Date: Sat, 13 Aug 2022 09:47:13 +0800 Subject: [PATCH] Use ConcurrentDictionary for ServerEntities --- .../Sync/Entities/SyncedVehicle.cs | 2 +- RageCoop.Core/Packets/Packets.cs | 5 ++- RageCoop.Server/Scripting/ServerEntities.cs | 37 +++++++++---------- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/RageCoop.Client/Sync/Entities/SyncedVehicle.cs b/RageCoop.Client/Sync/Entities/SyncedVehicle.cs index 7066721..9cd77f6 100644 --- a/RageCoop.Client/Sync/Entities/SyncedVehicle.cs +++ b/RageCoop.Client/Sync/Entities/SyncedVehicle.cs @@ -110,7 +110,7 @@ namespace RageCoop.Client #region -- INITIAL CHECK -- // Check if all data avalible - if (!IsReady) { return; } + if (!IsReady || Owner==null ) { return; } #endregion #region -- CHECK EXISTENCE -- if ((MainVehicle == null) || (!MainVehicle.Exists()) || (MainVehicle.Model.Hash != Model)) diff --git a/RageCoop.Core/Packets/Packets.cs b/RageCoop.Core/Packets/Packets.cs index 8c9e45f..3a26ebe 100644 --- a/RageCoop.Core/Packets/Packets.cs +++ b/RageCoop.Core/Packets/Packets.cs @@ -95,9 +95,10 @@ namespace RageCoop.Core IsVaulting = 1 << 9, IsInCover = 1 << 10, IsInVehicle = 1 << 11, - IsFullSync = 1<<12 , + IsEnteringVehicle = 1 << 12, + IsLeavingVehicle = 1 << 13, + IsFullSync = 1<<15 , } - #region ===== VEHICLE DATA ===== internal enum VehicleDataFlags:ushort { diff --git a/RageCoop.Server/Scripting/ServerEntities.cs b/RageCoop.Server/Scripting/ServerEntities.cs index 9c83465..3b71518 100644 --- a/RageCoop.Server/Scripting/ServerEntities.cs +++ b/RageCoop.Server/Scripting/ServerEntities.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Concurrent; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -22,10 +23,10 @@ namespace RageCoop.Server.Scripting { Server = server; } - internal Dictionary Peds { get; set; } = new(); - internal Dictionary Vehicles { get; set; } = new(); - internal Dictionary ServerProps { get; set; }=new(); - internal Dictionary Blips { get; set; } = new(); + internal ConcurrentDictionary Peds { get; set; } = new(); + internal ConcurrentDictionary Vehicles { get; set; } = new(); + internal ConcurrentDictionary ServerProps { get; set; }=new(); + internal ConcurrentDictionary Blips { get; set; } = new(); /// /// Get a by it's id @@ -106,7 +107,7 @@ namespace RageCoop.Server.Scripting { int id = RequestNetworkID(); ServerProp prop; - ServerProps.Add(id,prop=new ServerProp(Server) + ServerProps.TryAdd(id,prop=new ServerProp(Server) { ID=id, Model=model, @@ -136,7 +137,7 @@ namespace RageCoop.Server.Scripting _pos= pos, }; owner.SendCustomEventQueued(CustomEvents.CreateVehicle,veh.ID, model, pos, heading); - Vehicles.Add(veh.ID, veh); + Vehicles.TryAdd(veh.ID, veh); return veh; } @@ -154,7 +155,7 @@ namespace RageCoop.Server.Scripting Position=pos, Rotation=rotation }; - Blips.Add(b.ID,b); + Blips.TryAdd(b.ID,b); b.Update(); return b; } @@ -203,7 +204,7 @@ namespace RageCoop.Server.Scripting ServerPed ped; if(!Peds.TryGetValue(p.ID,out ped)) { - Peds.Add(p.ID,ped=new ServerPed(Server)); + Peds.TryAdd(p.ID,ped=new ServerPed(Server)); ped.ID=p.ID; } ped._pos = p.Position; @@ -217,7 +218,7 @@ namespace RageCoop.Server.Scripting ServerVehicle veh; if (!Vehicles.TryGetValue(p.ID, out veh)) { - Vehicles.Add(p.ID, veh=new ServerVehicle(Server)); + Vehicles.TryAdd(p.ID, veh=new ServerVehicle(Server)); veh.ID=p.ID; } veh._pos = p.Position; @@ -242,14 +243,14 @@ namespace RageCoop.Server.Scripting { if (pair.Value.Owner==left) { - Server.QueueJob(()=>Peds.Remove(pair.Key)); + Server.QueueJob(()=>Peds.TryRemove(pair.Key,out _)); } } foreach (var pair in Vehicles) { if (pair.Value.Owner==left) { - Server.QueueJob(() => Vehicles.Remove(pair.Key)); + Server.QueueJob(() => Vehicles.TryRemove(pair.Key, out _)); } } // Server.QueueJob(() => @@ -257,24 +258,20 @@ namespace RageCoop.Server.Scripting } internal void RemoveVehicle(int id) { - // Server.Logger?.Trace($"Removing vehicle:{id}"); - if (Vehicles.ContainsKey(id)) { Vehicles.Remove(id); } + Vehicles.TryRemove(id, out _); } internal void RemoveProp(int id) { - // Server.Logger?.Trace($"Removing vehicle:{id}"); - if (ServerProps.ContainsKey(id)) { ServerProps.Remove(id); } + ServerProps.TryRemove(id, out _); } internal void RemoveServerBlip(int id) { - // Server.Logger?.Trace($"Removing vehicle:{id}"); - if (Blips.ContainsKey(id)) { Blips.Remove(id); } + Blips.TryRemove(id, out _); } internal void RemovePed(int id) { - // Server.Logger?.Trace($"Removing ped:{id}"); - if (Peds.ContainsKey(id)) { Peds.Remove(id); } + Peds.TryRemove(id, out _); } internal void Add(ServerPed ped) @@ -285,7 +282,7 @@ namespace RageCoop.Server.Scripting } else { - Peds.Add(ped.ID, ped); + Peds.TryAdd(ped.ID, ped); } } internal int RequestNetworkID()