From 149487ea0f74a46a70c98b3a31259b667f4d29b2 Mon Sep 17 00:00:00 2001
From: Justin Clark-Casey (justincc)
Date: Thu, 27 Jun 2013 23:42:35 +0100
Subject: refactor: Move code for gathering textures referenced by materials
 into MaterialsDemoModule from UuidGatherer

This code is now triggered via EventManager.OnGatherUuids which modules can subscribe to.
---
 OpenSim/Region/Framework/Scenes/EventManager.cs    | 31 ++++++++++
 OpenSim/Region/Framework/Scenes/UuidGatherer.cs    | 69 +--------------------
 .../Materials/MaterialsDemoModule.cs               | 72 +++++++++++++++++++++-
 3 files changed, 103 insertions(+), 69 deletions(-)

diff --git a/OpenSim/Region/Framework/Scenes/EventManager.cs b/OpenSim/Region/Framework/Scenes/EventManager.cs
index a246319..720bfa9 100644
--- a/OpenSim/Region/Framework/Scenes/EventManager.cs
+++ b/OpenSim/Region/Framework/Scenes/EventManager.cs
@@ -1021,6 +1021,16 @@ namespace OpenSim.Region.Framework.Scenes
         /// </remarks>
         public event TeleportFail OnTeleportFail;
 
+        public delegate void GatherUuids(SceneObjectPart sop, IDictionary<UUID, AssetType> assetUuids);
+
+        /// <summary>
+        /// Triggered when UUIDs referenced by a scene object are being gathered for archiving, hg transfer, etc.
+        /// </summary>
+        /// <remarks>
+        /// The listener should add references to the IDictionary<UUID, AssetType> as appropriate.
+        /// </remarks>
+        public event GatherUuids OnGatherUuids;
+
         public class MoneyTransferArgs : EventArgs
         {
             public UUID sender;
@@ -3237,5 +3247,26 @@ namespace OpenSim.Region.Framework.Scenes
                 }
             }
         }
+
+        public void TriggerGatherUuids(SceneObjectPart sop, IDictionary<UUID, AssetType> assetUuids)
+        {
+            GatherUuids handler = OnGatherUuids;
+
+            if (handler != null)
+            {
+                foreach (GatherUuids d in handler.GetInvocationList())
+                {
+                    try
+                    {
+                        d(sop, assetUuids);
+                    }
+                    catch (Exception e)
+                    {
+                        m_log.ErrorFormat("[EVENT MANAGER]: Delegate for TriggerUuidGather failed - continuing {0} - {1}",
+                            e.Message, e.StackTrace);
+                    }
+                }
+            }
+        }
     }
 }
diff --git a/OpenSim/Region/Framework/Scenes/UuidGatherer.cs b/OpenSim/Region/Framework/Scenes/UuidGatherer.cs
index 586b59d..3492813 100644
--- a/OpenSim/Region/Framework/Scenes/UuidGatherer.cs
+++ b/OpenSim/Region/Framework/Scenes/UuidGatherer.cs
@@ -186,8 +186,7 @@ namespace OpenSim.Region.Framework.Scenes
                             GatherAssetUuids(tii.AssetID, (AssetType)tii.Type, assetUuids);
                     }
 
-                    // get any texture UUIDs used for materials such as normal and specular maps
-                    GatherMaterialsUuids(part, assetUuids);
+                    part.ParentGroup.Scene.EventManager.TriggerGatherUuids(part, assetUuids);
                 }
                 catch (Exception e)
                 {
@@ -211,71 +210,7 @@ namespace OpenSim.Region.Framework.Scenes
 //                Monitor.Pulse(this);
 //            }
 //        }
-
-        /// <summary>
-        /// Gather all of the texture asset UUIDs used to reference "Materials" such as normal and specular maps
-        /// </summary>
-        /// <param name="part"></param>
-        /// <param name="assetUuids"></param>
-        public void GatherMaterialsUuids(SceneObjectPart part, IDictionary<UUID, AssetType> assetUuids)
-        {
-            // scan thru the dynAttrs map of this part for any textures used as materials
-            OSD osdMaterials = null;
-
-            lock (part.DynAttrs)
-            {
-                if (part.DynAttrs.ContainsStore("OpenSim", "Materials"))
-                {
-                    OSDMap materialsStore = part.DynAttrs.GetStore("OpenSim", "Materials");
-                    materialsStore.TryGetValue("Materials", out osdMaterials);
-                }
-
-                if (osdMaterials != null)
-                {
-                    //m_log.Info("[UUID Gatherer]: found Materials: " + OSDParser.SerializeJsonString(osd));
-
-                    if (osdMaterials is OSDArray)
-                    {
-                        OSDArray matsArr = osdMaterials as OSDArray;
-                        foreach (OSDMap matMap in matsArr)
-                        {
-                            try
-                            {
-                                if (matMap.ContainsKey("Material"))
-                                {
-                                    OSDMap mat = matMap["Material"] as OSDMap;
-                                    if (mat.ContainsKey("NormMap"))
-                                    {
-                                        UUID normalMapId = mat["NormMap"].AsUUID();
-                                        if (normalMapId != UUID.Zero)
-                                        {
-                                            assetUuids[normalMapId] = AssetType.Texture;
-                                            //m_log.Info("[UUID Gatherer]: found normal map ID: " + normalMapId.ToString());
-                                        }
-                                    }
-                                    if (mat.ContainsKey("SpecMap"))
-                                    {
-                                        UUID specularMapId = mat["SpecMap"].AsUUID();
-                                        if (specularMapId != UUID.Zero)
-                                        {
-                                            assetUuids[specularMapId] = AssetType.Texture;
-                                            //m_log.Info("[UUID Gatherer]: found specular map ID: " + specularMapId.ToString());
-                                        }
-                                    }
-                                }
-
-                            }
-                            catch (Exception e)
-                            {
-                                m_log.Warn("[UUID Gatherer]: exception getting materials: " + e.Message);
-                            }
-                        }
-                    }
-                }
-            }
-        }
-
-
+       
         /// <summary>
         /// Get an asset synchronously, potentially using an asynchronous callback.  If the
         /// asynchronous callback is used, we will wait for it to complete.
diff --git a/OpenSim/Region/OptionalModules/Materials/MaterialsDemoModule.cs b/OpenSim/Region/OptionalModules/Materials/MaterialsDemoModule.cs
index 5b15a73..e7b8928 100644
--- a/OpenSim/Region/OptionalModules/Materials/MaterialsDemoModule.cs
+++ b/OpenSim/Region/OptionalModules/Materials/MaterialsDemoModule.cs
@@ -121,9 +121,11 @@ namespace OpenSim.Region.OptionalModules.MaterialsDemoModule
                 return;
 
             m_log.DebugFormat("[MaterialsDemoModule]: REGION {0} ADDED", scene.RegionInfo.RegionName);
+
             m_scene = scene;
-            m_scene.EventManager.OnRegisterCaps += new EventManager.RegisterCapsEvent(OnRegisterCaps);
-            m_scene.EventManager.OnObjectAddedToScene += new Action<SceneObjectGroup>(EventManager_OnObjectAddedToScene);
+            m_scene.EventManager.OnRegisterCaps += OnRegisterCaps;
+            m_scene.EventManager.OnObjectAddedToScene += EventManager_OnObjectAddedToScene;
+            m_scene.EventManager.OnGatherUuids += GatherMaterialsUuids;           
         }
 
         void EventManager_OnObjectAddedToScene(SceneObjectGroup obj)
@@ -157,6 +159,10 @@ namespace OpenSim.Region.OptionalModules.MaterialsDemoModule
             if (!m_enabled)
                 return;
 
+            m_scene.EventManager.OnRegisterCaps -= OnRegisterCaps;
+            m_scene.EventManager.OnObjectAddedToScene -= EventManager_OnObjectAddedToScene;
+            m_scene.EventManager.OnGatherUuids -= GatherMaterialsUuids; 
+
             m_log.DebugFormat("[MaterialsDemoModule]: REGION {0} REMOVED", scene.RegionInfo.RegionName);
         }        
         
@@ -569,5 +575,67 @@ namespace OpenSim.Region.OptionalModules.MaterialsDemoModule
             output.Flush();
         }
 
+        /// <summary>
+        /// Gather all of the texture asset UUIDs used to reference "Materials" such as normal and specular maps
+        /// </summary>
+        /// <param name="part"></param>
+        /// <param name="assetUuids"></param>
+        private void GatherMaterialsUuids(SceneObjectPart part, IDictionary<UUID, AssetType> assetUuids)
+        {
+            // scan thru the dynAttrs map of this part for any textures used as materials
+            OSD osdMaterials = null;
+
+            lock (part.DynAttrs)
+            {
+                if (part.DynAttrs.ContainsStore("OpenSim", "Materials"))
+                {
+                    OSDMap materialsStore = part.DynAttrs.GetStore("OpenSim", "Materials");
+                    materialsStore.TryGetValue("Materials", out osdMaterials);
+                }
+
+                if (osdMaterials != null)
+                {
+                    //m_log.Info("[UUID Gatherer]: found Materials: " + OSDParser.SerializeJsonString(osd));
+
+                    if (osdMaterials is OSDArray)
+                    {
+                        OSDArray matsArr = osdMaterials as OSDArray;
+                        foreach (OSDMap matMap in matsArr)
+                        {
+                            try
+                            {
+                                if (matMap.ContainsKey("Material"))
+                                {
+                                    OSDMap mat = matMap["Material"] as OSDMap;
+                                    if (mat.ContainsKey("NormMap"))
+                                    {
+                                        UUID normalMapId = mat["NormMap"].AsUUID();
+                                        if (normalMapId != UUID.Zero)
+                                        {
+                                            assetUuids[normalMapId] = AssetType.Texture;
+                                            //m_log.Info("[UUID Gatherer]: found normal map ID: " + normalMapId.ToString());
+                                        }
+                                    }
+                                    if (mat.ContainsKey("SpecMap"))
+                                    {
+                                        UUID specularMapId = mat["SpecMap"].AsUUID();
+                                        if (specularMapId != UUID.Zero)
+                                        {
+                                            assetUuids[specularMapId] = AssetType.Texture;
+                                            //m_log.Info("[UUID Gatherer]: found specular map ID: " + specularMapId.ToString());
+                                        }
+                                    }
+                                }
+
+                            }
+                            catch (Exception e)
+                            {
+                                m_log.Warn("[MaterialsDemoModule]: exception getting materials: " + e.Message);
+                            }
+                        }
+                    }
+                }
+            }
+        }
     }
 }
\ No newline at end of file
-- 
cgit v1.1