From f86a65f14be58d6458efb2384a72180a0af8306d Mon Sep 17 00:00:00 2001
From: Jeff Ames
Date: Wed, 7 Nov 2007 02:42:18 +0000
Subject: refactored some duplicate SceneObjectGroup searching code in Scene

---
 OpenSim/Region/Environment/Scenes/InnerScene.cs    | 264 ++++++---------------
 .../Region/Environment/Scenes/Scene.Inventory.cs   |  70 +++---
 2 files changed, 98 insertions(+), 236 deletions(-)

diff --git a/OpenSim/Region/Environment/Scenes/InnerScene.cs b/OpenSim/Region/Environment/Scenes/InnerScene.cs
index 8ed2352..90478c6 100644
--- a/OpenSim/Region/Environment/Scenes/InnerScene.cs
+++ b/OpenSim/Region/Environment/Scenes/InnerScene.cs
@@ -226,40 +226,50 @@ namespace OpenSim.Region.Environment.Scenes
             return null;
         }
 
-        public SceneObjectPart GetSceneObjectPart(uint localID)
+        private SceneObjectGroup GetGroupByPrim(uint localID)
         {
-            bool hasPrim = false;
             foreach (EntityBase ent in Entities.Values)
             {
                 if (ent is SceneObjectGroup)
                 {
-                    hasPrim = ((SceneObjectGroup)ent).HasChildPrim(localID);
-                    if (hasPrim != false)
-                    {
-                        return ((SceneObjectGroup)ent).GetChildPart(localID);
-                    }
+                    if (((SceneObjectGroup)ent).HasChildPrim(localID))
+                        return (SceneObjectGroup)ent;
                 }
             }
             return null;
         }
 
-        public SceneObjectPart GetSceneObjectPart(LLUUID fullID)
+        private SceneObjectGroup GetGroupByPrim(LLUUID fullID)
         {
-            bool hasPrim = false;
             foreach (EntityBase ent in Entities.Values)
             {
                 if (ent is SceneObjectGroup)
                 {
-                    hasPrim = ((SceneObjectGroup)ent).HasChildPrim(fullID);
-                    if (hasPrim != false)
-                    {
-                        return ((SceneObjectGroup)ent).GetChildPart(fullID);
-                    }
+                    if (((SceneObjectGroup)ent).HasChildPrim(fullID))
+                        return (SceneObjectGroup)ent;
                 }
             }
             return null;
         }
 
+        public SceneObjectPart GetSceneObjectPart(uint localID)
+        {
+            SceneObjectGroup group = GetGroupByPrim(localID);
+            if (group != null)
+                return group.GetChildPart(localID);
+            else
+                return null;
+        }
+
+        public SceneObjectPart GetSceneObjectPart(LLUUID fullID)
+        {
+            SceneObjectGroup group = GetGroupByPrim(fullID);
+            if (group != null)
+                return group.GetChildPart(fullID);
+            else
+                return null;
+        }
+
         internal bool TryGetAvatar(LLUUID avatarId, out ScenePresence avatar)
         {
             ScenePresence presence;
@@ -302,19 +312,11 @@ namespace OpenSim.Region.Environment.Scenes
 
         public LLUUID ConvertLocalIDToFullID(uint localID)
         {
-            bool hasPrim = false;
-            foreach (EntityBase ent in Entities.Values)
-            {
-                if (ent is SceneObjectGroup)
-                {
-                    hasPrim = ((SceneObjectGroup)ent).HasChildPrim(localID);
-                    if (hasPrim != false)
-                    {
-                        return ((SceneObjectGroup)ent).GetPartsFullID(localID);
-                    }
-                }
-            }
-            return LLUUID.Zero;
+            SceneObjectGroup group = GetGroupByPrim(localID);
+            if (group != null)
+                return group.GetPartsFullID(localID);
+            else
+                return LLUUID.Zero;
         }
 
         public void SendAllSceneObjectsToClient(ScenePresence presence)
@@ -346,19 +348,9 @@ namespace OpenSim.Region.Environment.Scenes
         /// <param name="remoteClient"></param>
         public void UpdatePrimScale(uint localID, LLVector3 scale, IClientAPI remoteClient)
         {
-            bool hasPrim = false;
-            foreach (EntityBase ent in Entities.Values)
-            {
-                if (ent is SceneObjectGroup)
-                {
-                    hasPrim = ((SceneObjectGroup)ent).HasChildPrim(localID);
-                    if (hasPrim != false)
-                    {
-                        ((SceneObjectGroup)ent).Resize(scale, localID);
-                        break;
-                    }
-                }
-            }
+            SceneObjectGroup group = GetGroupByPrim(localID);
+            if (group != null)
+                group.Resize(scale, localID);
         }
 
         /// <summary>
@@ -369,19 +361,9 @@ namespace OpenSim.Region.Environment.Scenes
         /// <param name="remoteClient"></param>
         public void UpdatePrimSingleRotation(uint localID, LLQuaternion rot, IClientAPI remoteClient)
         {
-            bool hasPrim = false;
-            foreach (EntityBase ent in Entities.Values)
-            {
-                if (ent is SceneObjectGroup)
-                {
-                    hasPrim = ((SceneObjectGroup)ent).HasChildPrim(localID);
-                    if (hasPrim != false)
-                    {
-                        ((SceneObjectGroup)ent).UpdateSingleRotation(rot, localID);
-                        break;
-                    }
-                }
-            }
+            SceneObjectGroup group = GetGroupByPrim(localID);
+            if (group != null)
+                group.UpdateSingleRotation(rot, localID);
         }
 
         /// <summary>
@@ -392,19 +374,9 @@ namespace OpenSim.Region.Environment.Scenes
         /// <param name="remoteClient"></param>
         public void UpdatePrimRotation(uint localID, LLQuaternion rot, IClientAPI remoteClient)
         {
-            bool hasPrim = false;
-            foreach (EntityBase ent in Entities.Values)
-            {
-                if (ent is SceneObjectGroup)
-                {
-                    hasPrim = ((SceneObjectGroup)ent).HasChildPrim(localID);
-                    if (hasPrim != false)
-                    {
-                        ((SceneObjectGroup)ent).UpdateGroupRotation(rot);
-                        break;
-                    }
-                }
-            }
+            SceneObjectGroup group = GetGroupByPrim(localID);
+            if (group != null)
+                group.UpdateGroupRotation(rot);
         }
 
         /// <summary>
@@ -416,36 +388,16 @@ namespace OpenSim.Region.Environment.Scenes
         /// <param name="remoteClient"></param>
         public void UpdatePrimRotation(uint localID, LLVector3 pos, LLQuaternion rot, IClientAPI remoteClient)
         {
-            bool hasPrim = false;
-            foreach (EntityBase ent in Entities.Values)
-            {
-                if (ent is SceneObjectGroup)
-                {
-                    hasPrim = ((SceneObjectGroup)ent).HasChildPrim(localID);
-                    if (hasPrim != false)
-                    {
-                        ((SceneObjectGroup)ent).UpdateGroupRotation(pos, rot);
-                        break;
-                    }
-                }
-            }
+            SceneObjectGroup group = GetGroupByPrim(localID);
+            if (group != null)
+                group.UpdateGroupRotation(pos, rot);
         }
 
         public void UpdatePrimSinglePosition(uint localID, LLVector3 pos, IClientAPI remoteClient)
         {
-            bool hasPrim = false;
-            foreach (EntityBase ent in Entities.Values)
-            {
-                if (ent is SceneObjectGroup)
-                {
-                    hasPrim = ((SceneObjectGroup)ent).HasChildPrim(localID);
-                    if (hasPrim != false)
-                    {
-                        ((SceneObjectGroup)ent).UpdateSinglePosition(pos, localID);
-                        break;
-                    }
-                }
-            }
+            SceneObjectGroup group = GetGroupByPrim(localID);
+            if (group != null)
+                group.UpdateSinglePosition(pos, localID);
         }
 
         /// <summary>
@@ -456,19 +408,9 @@ namespace OpenSim.Region.Environment.Scenes
         /// <param name="remoteClient"></param>
         public void UpdatePrimPosition(uint localID, LLVector3 pos, IClientAPI remoteClient)
         {
-            bool hasPrim = false;
-            foreach (EntityBase ent in Entities.Values)
-            {
-                if (ent is SceneObjectGroup)
-                {
-                    hasPrim = ((SceneObjectGroup)ent).HasChildPrim(localID);
-                    if (hasPrim != false)
-                    {
-                        ((SceneObjectGroup)ent).UpdateGroupPosition(pos);
-                        break;
-                    }
-                }
-            }
+            SceneObjectGroup group = GetGroupByPrim(localID);
+            if (group != null)
+                group.UpdateGroupPosition(pos);
         }
 
         /// <summary>
@@ -479,19 +421,9 @@ namespace OpenSim.Region.Environment.Scenes
         /// <param name="remoteClient"></param>
         public void UpdatePrimTexture(uint localID, byte[] texture, IClientAPI remoteClient)
         {
-            bool hasPrim = false;
-            foreach (EntityBase ent in Entities.Values)
-            {
-                if (ent is SceneObjectGroup)
-                {
-                    hasPrim = ((SceneObjectGroup)ent).HasChildPrim(localID);
-                    if (hasPrim != false)
-                    {
-                        ((SceneObjectGroup)ent).UpdateTextureEntry(localID, texture);
-                        break;
-                    }
-                }
-            }
+            SceneObjectGroup group = GetGroupByPrim(localID);
+            if (group != null)
+                group.UpdateTextureEntry(localID, texture);
         }
 
         /// <summary>
@@ -502,19 +434,9 @@ namespace OpenSim.Region.Environment.Scenes
         /// <param name="remoteClient"></param>
         public void UpdatePrimFlags(uint localID, Packet packet, IClientAPI remoteClient)
         {
-            bool hasprim = false;
-            foreach (EntityBase ent in Entities.Values)
-            {
-                if (ent is SceneObjectGroup)
-                {
-                    hasprim = ((SceneObjectGroup)ent).HasChildPrim(localID);
-                    if (hasprim != false)
-                    {
-                        ((SceneObjectGroup)ent).UpdatePrimFlags(localID, (ushort)packet.Type, true, packet.ToBytes());
-                    }
-                }
-            }
-
+            SceneObjectGroup group = GetGroupByPrim(localID);
+            if (group != null)
+                group.UpdatePrimFlags(localID, (ushort)packet.Type, true, packet.ToBytes());
             //System.Console.WriteLine("Got primupdate packet: " + packet.UsePhysics.ToString());
         }
 
@@ -522,19 +444,9 @@ namespace OpenSim.Region.Environment.Scenes
         {
             if (PermissionsMngr.CanEditObject(remoteClient.AgentId, objectID))
             {
-                bool hasPrim = false;
-                foreach (EntityBase ent in Entities.Values)
-                {
-                    if (ent is SceneObjectGroup)
-                    {
-                        hasPrim = ((SceneObjectGroup)ent).HasChildPrim(objectID);
-                        if (hasPrim != false)
-                        {
-                            ((SceneObjectGroup)ent).GrabMovement(offset, pos, remoteClient);
-                            break;
-                        }
-                    }
-                }
+                SceneObjectGroup group = GetGroupByPrim(objectID);
+                if (group != null)
+                    group.GrabMovement(offset, pos, remoteClient);
             }
         }
 
@@ -545,19 +457,9 @@ namespace OpenSim.Region.Environment.Scenes
         /// <param name="description"></param>
         public void PrimName(uint primLocalID, string name)
         {
-            bool hasPrim = false;
-            foreach (EntityBase ent in Entities.Values)
-            {
-                if (ent is SceneObjectGroup)
-                {
-                    hasPrim = ((SceneObjectGroup)ent).HasChildPrim(primLocalID);
-                    if (hasPrim != false)
-                    {
-                        ((SceneObjectGroup)ent).SetPartName(name, primLocalID);
-                        break;
-                    }
-                }
-            }
+            SceneObjectGroup group = GetGroupByPrim(primLocalID);
+            if (group != null)
+                group.SetPartName(name, primLocalID);
         }
 
         /// <summary>
@@ -567,36 +469,16 @@ namespace OpenSim.Region.Environment.Scenes
         /// <param name="description"></param>
         public void PrimDescription(uint primLocalID, string description)
         {
-            bool hasPrim = false;
-            foreach (EntityBase ent in Entities.Values)
-            {
-                if (ent is SceneObjectGroup)
-                {
-                    hasPrim = ((SceneObjectGroup)ent).HasChildPrim(primLocalID);
-                    if (hasPrim != false)
-                    {
-                        ((SceneObjectGroup)ent).SetPartDescription(description, primLocalID);
-                        break;
-                    }
-                }
-            }
+            SceneObjectGroup group = GetGroupByPrim(primLocalID);
+            if (group != null)
+                group.SetPartDescription(description, primLocalID);
         }
 
         public void UpdateExtraParam(uint primLocalID, ushort type, bool inUse, byte[] data)
         {
-            bool hasPrim = false;
-            foreach (EntityBase ent in Entities.Values)
-            {
-                if (ent is SceneObjectGroup)
-                {
-                    hasPrim = ((SceneObjectGroup)ent).HasChildPrim(primLocalID);
-                    if (hasPrim != false)
-                    {
-                        ((SceneObjectGroup)ent).UpdateExtraParam(primLocalID, type, inUse, data);
-                        break;
-                    }
-                }
-            }
+            SceneObjectGroup group = GetGroupByPrim(primLocalID);
+            if (group != null)
+                group.UpdateExtraParam(primLocalID, type, inUse, data);
         }
 
         /// <summary>
@@ -606,19 +488,9 @@ namespace OpenSim.Region.Environment.Scenes
         /// <param name="shapeBlock"></param>
         public void UpdatePrimShape(uint primLocalID, ObjectShapePacket.ObjectDataBlock shapeBlock)
         {
-            bool hasPrim = false;
-            foreach (EntityBase ent in Entities.Values)
-            {
-                if (ent is SceneObjectGroup)
-                {
-                    hasPrim = ((SceneObjectGroup)ent).HasChildPrim(primLocalID);
-                    if (hasPrim != false)
-                    {
-                        ((SceneObjectGroup)ent).UpdateShape(shapeBlock, primLocalID);
-                        break;
-                    }
-                }
-            }
+            SceneObjectGroup group = GetGroupByPrim(primLocalID);
+            if (group != null)
+                group.UpdateShape(shapeBlock, primLocalID);
         }
 
         /// <summary>
diff --git a/OpenSim/Region/Environment/Scenes/Scene.Inventory.cs b/OpenSim/Region/Environment/Scenes/Scene.Inventory.cs
index 138cb20..f5e6f63 100644
--- a/OpenSim/Region/Environment/Scenes/Scene.Inventory.cs
+++ b/OpenSim/Region/Environment/Scenes/Scene.Inventory.cs
@@ -217,6 +217,19 @@ namespace OpenSim.Region.Environment.Scenes
             }
         }
 
+        private SceneObjectGroup GetGroupByPrim(uint localID)
+        {
+            foreach (EntityBase ent in Entities.Values)
+            {
+                if (ent is SceneObjectGroup)
+                {
+                    if (((SceneObjectGroup)ent).HasChildPrim(localID))
+                        return (SceneObjectGroup)ent;
+                }
+            }
+            return null;
+        }
+
         /// <summary>
         /// 
         /// </summary>
@@ -224,23 +237,15 @@ namespace OpenSim.Region.Environment.Scenes
         /// <param name="primLocalID"></param>
         public void RequestTaskInventory(IClientAPI remoteClient, uint primLocalID)
         {
-            bool hasPrim = false;
-            foreach (EntityBase ent in Entities.Values)
+            SceneObjectGroup group = GetGroupByPrim(primLocalID);
+            if (group != null)
             {
-                if (ent is SceneObjectGroup)
+                bool fileChange = group.GetPartInventoryFileName(remoteClient, primLocalID);
+                if (fileChange)
                 {
-                    hasPrim = ((SceneObjectGroup) ent).HasChildPrim(primLocalID);
-                    if (hasPrim != false)
+                    if (XferManager != null)
                     {
-                        bool fileChange = ((SceneObjectGroup) ent).GetPartInventoryFileName(remoteClient, primLocalID);
-                        if (fileChange)
-                        {
-                            if (XferManager != null)
-                            {
-                                ((SceneObjectGroup) ent).RequestInventoryFile(primLocalID, XferManager);
-                            }
-                        }
-                        break;
+                        group.RequestInventoryFile(primLocalID, XferManager);
                     }
                 }
             }
@@ -248,21 +253,14 @@ namespace OpenSim.Region.Environment.Scenes
 
         public void RemoveTaskInventory(IClientAPI remoteClient, LLUUID itemID, uint localID)
         {
-            bool hasPrim = false;
-            foreach (EntityBase ent in Entities.Values)
+            SceneObjectGroup group = GetGroupByPrim(localID);
+            if (group != null)
             {
-                if (ent is SceneObjectGroup)
+                int type = group.RemoveInventoryItem(remoteClient, localID, itemID);
+                group.GetProperites(remoteClient);
+                if (type == 10)
                 {
-                    hasPrim = ((SceneObjectGroup) ent).HasChildPrim(localID);
-                    if (hasPrim != false)
-                    {
-                        int type = ((SceneObjectGroup) ent).RemoveInventoryItem(remoteClient, localID, itemID);
-                        ((SceneObjectGroup) ent).GetProperites(remoteClient);
-                        if (type == 10)
-                        {
-                            EventManager.TriggerRemoveScript(localID, itemID);
-                        }
-                    }
+                    EventManager.TriggerRemoveScript(localID, itemID);
                 }
             }
         }
@@ -307,20 +305,12 @@ namespace OpenSim.Region.Environment.Scenes
 
                         if (rezzed)
                         {
-                            bool hasPrim = false;
-                            foreach (EntityBase ent in Entities.Values)
+                            SceneObjectGroup group = GetGroupByPrim(localID);
+                            if (group != null)
                             {
-                                if (ent is SceneObjectGroup)
-                                {
-                                    hasPrim = ((SceneObjectGroup) ent).HasChildPrim(localID);
-                                    if (hasPrim != false)
-                                    {
-                                        bool added =
-                                            ((SceneObjectGroup) ent).AddInventoryItem(remoteClient, localID, item,
-                                                                                      copyID);
-                                        ((SceneObjectGroup) ent).GetProperites(remoteClient);
-                                    }
-                                }
+                                // TODO: do we care about the value of this bool?
+                                bool added = group.AddInventoryItem(remoteClient, localID, item, copyID);
+                                group.GetProperites(remoteClient);
                             }
                         }
                     }
-- 
cgit v1.1