From bce31bda7d627f34e3b6ac51b965a7fb09c53847 Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Sun, 16 Oct 2022 19:21:54 +0100 Subject: [PATCH] c# sugar --- OpenSim/Framework/AgentCircuitData.cs | 22 ++++----- OpenSim/Framework/AgentCircuitManager.cs | 44 +++++++++--------- OpenSim/Framework/ClientManager.cs | 10 ++--- OpenSim/Region/Framework/Scenes/SceneGraph.cs | 45 +++++++++---------- 4 files changed, 61 insertions(+), 60 deletions(-) diff --git a/OpenSim/Framework/AgentCircuitData.cs b/OpenSim/Framework/AgentCircuitData.cs index 5c64839a3f..7242993a45 100644 --- a/OpenSim/Framework/AgentCircuitData.cs +++ b/OpenSim/Framework/AgentCircuitData.cs @@ -97,7 +97,7 @@ namespace OpenSim.Framework /// /// Agent's full name. /// - public string Name { get { return string.Format("{0} {1}", firstname, lastname); } } + public string Name { get { return $"{firstname} {lastname}"; } } /// /// Random Unique GUID for this session. Client gets this at login and it's @@ -139,13 +139,13 @@ namespace OpenSim.Framework get { // Old style version string contains viewer name followed by a space followed by a version number - if (m_viewerInternal == null || m_viewerInternal.Contains(" ")) + if (m_viewerInternal is null || m_viewerInternal.Contains(' ')) { return m_viewerInternal; } else // New style version contains no spaces, just version number { - return Channel + " " + m_viewerInternal; + return $"{Channel} {m_viewerInternal}"; } } } @@ -183,14 +183,16 @@ namespace OpenSim.Framework /// map of the agent circuit data public OSDMap PackAgentCircuitData(EntityTransferContext ctx) { - OSDMap args = new OSDMap(); - args["agent_id"] = OSD.FromUUID(AgentID); - args["base_folder"] = OSD.FromUUID(BaseFolder); - args["caps_path"] = OSD.FromString(CapsPath); - - if (ChildrenCapSeeds != null) + OSDMap args = new() { - OSDArray childrenSeeds = new OSDArray(ChildrenCapSeeds.Count); + ["agent_id"] = OSD.FromUUID(AgentID), + ["base_folder"] = OSD.FromUUID(BaseFolder), + ["caps_path"] = OSD.FromString(CapsPath) + }; + + if (ChildrenCapSeeds is not null) + { + OSDArray childrenSeeds = new(ChildrenCapSeeds.Count); foreach (KeyValuePair kvp in ChildrenCapSeeds) { OSDMap pair = new OSDMap(); diff --git a/OpenSim/Framework/AgentCircuitManager.cs b/OpenSim/Framework/AgentCircuitManager.cs index 38fdcf83ef..060ce9542f 100644 --- a/OpenSim/Framework/AgentCircuitManager.cs +++ b/OpenSim/Framework/AgentCircuitManager.cs @@ -42,36 +42,38 @@ namespace OpenSim.Framework /// /// We lock this for operations both on this dictionary and on m_agentCircuitsByUUID /// - private ConcurrentDictionary m_agentCircuits = new ConcurrentDictionary(); + private readonly ConcurrentDictionary m_agentCircuits = new(); /// /// Agent circuits indexed by agent UUID. /// - private ConcurrentDictionary m_agentCircuitsByUUID = new ConcurrentDictionary(); + private readonly ConcurrentDictionary m_agentCircuitsByUUID = new(); public virtual AuthenticateResponse AuthenticateSession(UUID sessionID, UUID agentID, uint circuitcode) { - AuthenticateResponse user = new AuthenticateResponse(); - if (!m_agentCircuits.TryGetValue(circuitcode, out AgentCircuitData validcircuit) || validcircuit == null) + AuthenticateResponse user = new(); + if (!m_agentCircuits.TryGetValue(circuitcode, out AgentCircuitData validcircuit) || validcircuit is null) { //don't have this circuit code in our list user.Authorised = false; return user; } - if ((sessionID == validcircuit.SessionID) && (agentID == validcircuit.AgentID)) + if (sessionID.Equals(validcircuit.SessionID) && agentID.Equals(validcircuit.AgentID)) { user.Authorised = true; - user.LoginInfo = new Login(); - user.LoginInfo.Agent = agentID; - user.LoginInfo.Session = sessionID; - user.LoginInfo.SecureSession = validcircuit.SecureSessionID; - user.LoginInfo.First = validcircuit.firstname; - user.LoginInfo.Last = validcircuit.lastname; - user.LoginInfo.InventoryFolder = validcircuit.InventoryFolder; - user.LoginInfo.BaseFolder = validcircuit.BaseFolder; - user.LoginInfo.StartPos = validcircuit.startpos; - user.LoginInfo.StartFar = (float)validcircuit.startfar; + user.LoginInfo = new Login + { + Agent = agentID, + Session = sessionID, + SecureSession = validcircuit.SecureSessionID, + First = validcircuit.firstname, + Last = validcircuit.lastname, + InventoryFolder = validcircuit.InventoryFolder, + BaseFolder = validcircuit.BaseFolder, + StartPos = validcircuit.startpos, + StartFar = validcircuit.startfar + }; } else { @@ -106,7 +108,7 @@ namespace OpenSim.Framework { if (m_agentCircuits.TryRemove(circuitCode, out AgentCircuitData ac)) { - m_agentCircuitsByUUID.TryRemove(ac.AgentID, out AgentCircuitData dummy); + m_agentCircuitsByUUID.TryRemove(ac.AgentID, out AgentCircuitData _); } } @@ -114,16 +116,16 @@ namespace OpenSim.Framework { if (m_agentCircuitsByUUID.TryRemove(agentID, out AgentCircuitData ac)) { - m_agentCircuits.TryRemove(ac.circuitcode, out AgentCircuitData dummy); + m_agentCircuits.TryRemove(ac.circuitcode, out AgentCircuitData _); } } public virtual void RemoveCircuit(AgentCircuitData ac) { - m_agentCircuitsByUUID.TryRemove(ac.AgentID, out AgentCircuitData dummy); - m_agentCircuits.TryRemove(ac.circuitcode, out AgentCircuitData dummyb); - if (dummy!= null && dummy.circuitcode != ac.circuitcode) //?? - m_agentCircuits.TryRemove(dummy.circuitcode, out AgentCircuitData dummyc); + m_agentCircuitsByUUID.TryRemove(ac.AgentID, out AgentCircuitData byuuid); + m_agentCircuits.TryRemove(ac.circuitcode, out AgentCircuitData _); + if (byuuid is not null && byuuid.circuitcode != ac.circuitcode) //?? + m_agentCircuits.TryRemove(byuuid.circuitcode, out AgentCircuitData _); } public AgentCircuitData GetAgentCircuitData(uint circuitCode) diff --git a/OpenSim/Framework/ClientManager.cs b/OpenSim/Framework/ClientManager.cs index c444fcffc5..07fcc0d6d2 100644 --- a/OpenSim/Framework/ClientManager.cs +++ b/OpenSim/Framework/ClientManager.cs @@ -40,15 +40,15 @@ namespace OpenSim.Framework { /// A dictionary mapping from /// to references - private readonly Dictionary m_dictbyUUID = new Dictionary(); + private readonly Dictionary m_dictbyUUID = new(); /// A dictionary mapping from /// to references - private readonly Dictionary m_dictbyIPe= new Dictionary(); + private readonly Dictionary m_dictbyIPe= new(); /// snapshot collection of current /// references private IClientAPI[] m_array = null; /// Synchronization object for writing to the collections - private readonly object m_syncRoot = new object(); + private readonly object m_syncRoot = new(); /// Number of clients in the collection public int Count @@ -96,10 +96,8 @@ namespace OpenSim.Framework { lock (m_syncRoot) { - IClientAPI value; - if (m_dictbyUUID.TryGetValue(key, out value)) + if (m_dictbyUUID.Remove(key, out IClientAPI value)) { - m_dictbyUUID.Remove(key); m_dictbyIPe.Remove(value.RemoteEndPoint); m_array = null; return true; diff --git a/OpenSim/Region/Framework/Scenes/SceneGraph.cs b/OpenSim/Region/Framework/Scenes/SceneGraph.cs index cf998659ce..d196ce8ffd 100755 --- a/OpenSim/Region/Framework/Scenes/SceneGraph.cs +++ b/OpenSim/Region/Framework/Scenes/SceneGraph.cs @@ -37,6 +37,7 @@ using OpenSim.Framework; using OpenSim.Region.Framework.Scenes.Types; using OpenSim.Region.PhysicsModules.SharedBase; using OpenSim.Region.Framework.Interfaces; +using System.Runtime.InteropServices; namespace OpenSim.Region.Framework.Scenes { @@ -251,7 +252,7 @@ namespace OpenSim.Region.Framework.Scenes scaleY = 1.0f / scaleY; List presences = GetScenePresences(); - foreach (ScenePresence sp in presences) + foreach (ScenePresence sp in CollectionsMarshal.AsSpan(presences)) { // If this presence is a child agent, we don't want its coarse locations if (sp.IsChildAgent) @@ -804,7 +805,7 @@ namespace OpenSim.Region.Framework.Scenes int rootnpccount = 0; List presences = GetScenePresences(); - foreach(ScenePresence sp in presences) + foreach(ScenePresence sp in CollectionsMarshal.AsSpan(presences)) { if (sp.IsChildAgent) ++childcount; @@ -986,7 +987,7 @@ namespace OpenSim.Region.Framework.Scenes protected internal ScenePresence GetScenePresence(in string firstName, in string lastName) { List presences = GetScenePresences(); - foreach (ScenePresence presence in presences) + foreach (ScenePresence presence in CollectionsMarshal.AsSpan(presences)) { if (string.Equals(presence.Firstname, firstName, StringComparison.CurrentCultureIgnoreCase) && string.Equals(presence.Lastname, lastName, StringComparison.CurrentCultureIgnoreCase)) @@ -1050,7 +1051,7 @@ namespace OpenSim.Region.Framework.Scenes protected internal bool TryGetAvatarByName(in string name, out ScenePresence avatar) { List presences = GetScenePresences(); - foreach (ScenePresence presence in presences) + foreach (ScenePresence presence in CollectionsMarshal.AsSpan(presences)) { if (string.Equals(name, presence.ControllingClient.Name, StringComparison.CurrentCultureIgnoreCase)) { @@ -1122,11 +1123,10 @@ namespace OpenSim.Region.Framework.Scenes float closestDistance = 280f; EntityIntersection result = new(); EntityBase[] EntityList = GetEntities(); - foreach (EntityBase ent in EntityList) + foreach (EntityBase ent in EntityList.AsSpan()) { - if (ent is SceneObjectGroup) + if (ent is SceneObjectGroup reportingG) { - SceneObjectGroup reportingG = ent as SceneObjectGroup; EntityIntersection inter = reportingG.TestIntersection(hray, frontFacesOnly, faceCenters); if (inter.HitTF && inter.distance < closestDistance) { @@ -1149,7 +1149,7 @@ namespace OpenSim.Region.Framework.Scenes EntityBase[] entities = Entities.GetEntities(); List ret = new(entities.Length); - foreach(EntityBase et in entities) + foreach(EntityBase et in entities.AsSpan()) { if(et is SceneObjectGroup sog) ret.Add(sog); @@ -1188,7 +1188,7 @@ namespace OpenSim.Region.Framework.Scenes /// null if the part was not found protected internal SceneObjectGroup GetSceneObjectGroup(in string name) { - foreach(EntityBase entity in Entities.GetEntities()) + foreach(EntityBase entity in Entities.GetEntities().AsSpan()) { if (entity is SceneObjectGroup sog && sog.Name.Equals(name)) return sog; @@ -1355,7 +1355,7 @@ namespace OpenSim.Region.Framework.Scenes protected internal void ForEachSOG(Action action) { EntityBase[] entities = Entities.GetEntities(); - foreach (EntityBase entity in entities) + foreach (EntityBase entity in entities.AsSpan()) { if (entity is SceneObjectGroup sog) { @@ -1379,7 +1379,7 @@ namespace OpenSim.Region.Framework.Scenes public void ForEachRootScenePresence(Action action) { List presences = GetScenePresences(); - foreach (ScenePresence sp in presences) + foreach (ScenePresence sp in CollectionsMarshal.AsSpan(presences)) { if(sp.IsChildAgent || sp.IsDeleted) continue; @@ -1402,7 +1402,7 @@ namespace OpenSim.Region.Framework.Scenes public void ForEachScenePresence(Action action) { List presences = GetScenePresences(); - foreach (ScenePresence sp in presences) + foreach (ScenePresence sp in CollectionsMarshal.AsSpan(presences)) { if (sp.IsDeleted) continue; @@ -1890,10 +1890,9 @@ namespace OpenSim.Region.Framework.Scenes List childGroups = new(); // We do this in reverse to get the link order of the prims correct - for (int i = 0; i < children.Count; i++) + foreach (SceneObjectPart childpart in CollectionsMarshal.AsSpan(children)) { - SceneObjectGroup child = children[i].ParentGroup; - + SceneObjectGroup child = childpart.ParentGroup; // Don't try and add a group to itself - this will only cause severe problems later on. if (child == parentGroup) continue; @@ -1910,7 +1909,7 @@ namespace OpenSim.Region.Framework.Scenes } } - foreach (SceneObjectGroup child in childGroups) + foreach (SceneObjectGroup child in CollectionsMarshal.AsSpan(childGroups)) { if (parentGroup.OwnerID == child.OwnerID) { @@ -1962,7 +1961,7 @@ namespace OpenSim.Region.Framework.Scenes Monitor.Enter(m_linkLock); try { - foreach (SceneObjectPart part in prims) + foreach (SceneObjectPart part in CollectionsMarshal.AsSpan(prims)) { if(part is null) continue; @@ -1997,7 +1996,7 @@ namespace OpenSim.Region.Framework.Scenes if (childParts.Count > 0) { - foreach (SceneObjectPart child in childParts) + foreach (SceneObjectPart child in CollectionsMarshal.AsSpan(childParts)) { // Unlink all child parts from their groups child.ParentGroup.DelinkFromGroup(child, true); @@ -2007,7 +2006,7 @@ namespace OpenSim.Region.Framework.Scenes } } - foreach (SceneObjectPart root in rootParts) + foreach (SceneObjectPart root in CollectionsMarshal.AsSpan(rootParts)) { // In most cases, this will run only one time, and the prim // will be a solo prim @@ -2035,7 +2034,7 @@ namespace OpenSim.Region.Framework.Scenes // Determine new root // newSet.RemoveAt(0); - foreach (SceneObjectPart newChild in newSet) + foreach (SceneObjectPart newChild in CollectionsMarshal.AsSpan(newSet)) newChild.ClearUpdateSchedule(); LinkObjects(newRoot, newSet); @@ -2051,7 +2050,7 @@ namespace OpenSim.Region.Framework.Scenes // trigger events in the roots // - foreach (SceneObjectGroup g in affectedGroups) + foreach (SceneObjectGroup g in CollectionsMarshal.AsSpan(affectedGroups)) { if(g.RootPart.PhysActor is not null) g.RootPart.PhysActor.Building = false; @@ -2143,7 +2142,7 @@ namespace OpenSim.Region.Framework.Scenes if (m_parentScene.Permissions.PropagatePermissions()) { - foreach (SceneObjectPart child in parts) + foreach (SceneObjectPart child in parts.AsSpan()) { child.Inventory.ChangeInventoryOwner(AgentID); child.TriggerScriptChangedEvent(Changed.OWNER); @@ -2165,7 +2164,7 @@ namespace OpenSim.Region.Framework.Scenes Entities.Add(copy); m_scenePartsArray = null; - foreach (SceneObjectPart part in parts) + foreach (SceneObjectPart part in parts.AsSpan()) { if (part.GetPrimType() == PrimType.SCULPT) m_numMesh++;