aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/OptionalModules/Materials/MaterialsDemoModule.cs
blob: e7b89281ffb24e1c9645013a2a5d90240c9d1f54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
 * Copyright (c) Contributors, http://opensimulator.org/
 * See CONTRIBUTORS.TXT for a full list of copyright holders.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSimulator Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Security.Cryptography; // for computing md5 hash
using log4net;
using Mono.Addins;
using Nini.Config;

using OpenMetaverse;
using OpenMetaverse.StructuredData;

using OpenSim.Framework;
using OpenSim.Framework.Servers;
using OpenSim.Framework.Servers.HttpServer;
using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Framework.Scenes;

using Ionic.Zlib;

// You will need to uncomment these lines if you are adding a region module to some other assembly which does not already
// specify its assembly.  Otherwise, the region modules in the assembly will not be picked up when OpenSimulator scans
// the available DLLs
//[assembly: Addin("MaterialsDemoModule", "1.0")]
//[assembly: AddinDependency("OpenSim", "0.5")]

namespace OpenSim.Region.OptionalModules.MaterialsDemoModule
{
    /// <summary>
    ///
    //   #    #    ##    #####   #    #     #    #    #   ####
    //   #    #   #  #   #    #  ##   #     #    ##   #  #    #
    //   #    #  #    #  #    #  # #  #     #    # #  #  #
    //   # ## #  ######  #####   #  # #     #    #  # #  #  ###
    //   ##  ##  #    #  #   #   #   ##     #    #   ##  #    #
    //   #    #  #    #  #    #  #    #     #    #    #   ####
    //
    //   THIS MODULE IS FOR EXPERIMENTAL USE ONLY AND MAY CAUSE REGION OR ASSET CORRUPTION!
    //
    //////////////  WARNING  //////////////////////////////////////////////////////////////////
    /// This is an *Experimental* module for developing support for materials-capable viewers
    /// This module should NOT be used in a production environment! It may cause data corruption and
    /// viewer crashes. It should be only used to evaluate implementations of materials.
    /// 
    /// Materials are persisted via SceneObjectPart.dynattrs. This is a relatively new feature
    /// of OpenSimulator and is not field proven at the time this module was written. Persistence
    /// may fail or become corrupt and this could cause viewer crashes due to erroneous materials
    /// data being sent to viewers. Materials descriptions might survive IAR, OAR, or other means
    /// of archiving however the texture resources used by these materials probably will not as they
    /// may not be adequately referenced to ensure proper archiving.
    /// 
    /// 
    /// 
    /// To enable this module, add this string at the bottom of OpenSim.ini:
    /// [MaterialsDemoModule]
    /// 
    /// </summary>
    /// 

    [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "MaterialsDemoModule")]
    public class MaterialsDemoModule : INonSharedRegionModule
    {
        private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        
        public string Name { get { return "MaterialsDemoModule"; } }        
        
        public Type ReplaceableInterface { get { return null; } }

        private Scene m_scene = null;
        private bool m_enabled = false;

        public Dictionary<UUID, OSDMap> m_knownMaterials = new Dictionary<UUID, OSDMap>();
        
        public void Initialise(IConfigSource source)
        {
            m_enabled = (source.Configs["MaterialsDemoModule"] != null);
            if (!m_enabled)
                return;

            m_log.DebugFormat("[MaterialsDemoModule]: INITIALIZED MODULE");
        }
        
        public void Close()
        {
            if (!m_enabled)
                return;

            m_log.DebugFormat("[MaterialsDemoModule]: CLOSED MODULE");
        }
        
        public void AddRegion(Scene scene)
        {
            if (!m_enabled)
                return;

            m_log.DebugFormat("[MaterialsDemoModule]: REGION {0} ADDED", scene.RegionInfo.RegionName);

            m_scene = scene;
            m_scene.EventManager.OnRegisterCaps += OnRegisterCaps;
            m_scene.EventManager.OnObjectAddedToScene += EventManager_OnObjectAddedToScene;
            m_scene.EventManager.OnGatherUuids += GatherMaterialsUuids;           
        }

        void EventManager_OnObjectAddedToScene(SceneObjectGroup obj)
        {
            foreach (var part in obj.Parts)
                if (part != null)
                    GetStoredMaterialsForPart(part);
        }

        void OnRegisterCaps(OpenMetaverse.UUID agentID, OpenSim.Framework.Capabilities.Caps caps)
        {
            string capsBase = "/CAPS/" + caps.CapsObjectPath;

            IRequestHandler renderMaterialsPostHandler = new RestStreamHandler("POST", capsBase + "/", RenderMaterialsPostCap);
            caps.RegisterHandler("RenderMaterials", renderMaterialsPostHandler);

            // OpenSimulator CAPs infrastructure seems to be somewhat hostile towards any CAP that requires both GET
            // and POST handlers, (at least at the time this was originally written), so we first set up a POST
            // handler normally and then add a GET handler via MainServer

            IRequestHandler renderMaterialsGetHandler = new RestStreamHandler("GET", capsBase + "/", RenderMaterialsGetCap);
            MainServer.Instance.AddStreamHandler(renderMaterialsGetHandler);

            // materials viewer seems to use either POST or PUT, so assign POST handler for PUT as well
            IRequestHandler renderMaterialsPutHandler = new RestStreamHandler("PUT", capsBase + "/", RenderMaterialsPostCap);
            MainServer.Instance.AddStreamHandler(renderMaterialsPutHandler);
        }
        
        public void RemoveRegion(Scene scene)
        {
            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);
        }        
        
        public void RegionLoaded(Scene scene)
        {
        }

        OSDMap GetMaterial(UUID id)
        {
            OSDMap map = null;
            if (m_knownMaterials.ContainsKey(id))
            {
                map = new OSDMap();
                map["ID"] = OSD.FromBinary(id.GetBytes());
                map["Material"] = m_knownMaterials[id];
            }
            return map;
        }

        void GetStoredMaterialsForPart(SceneObjectPart part)
        { 
            OSD OSMaterials = null;
            OSDArray matsArr = null;

            if (part.DynAttrs == null)
            {
                m_log.Warn("[MaterialsDemoModule]: NULL DYNATTRS :( ");
            }

            lock (part.DynAttrs)
            {
                if (part.DynAttrs.ContainsStore("OpenSim", "Materials"))
                {
                    OSDMap materialsStore = part.DynAttrs.GetStore("OpenSim", "Materials");
                    materialsStore.TryGetValue("Materials", out OSMaterials);
                }

                if (OSMaterials != null && OSMaterials is OSDArray)
                    matsArr = OSMaterials as OSDArray;
                else
                    return;
            }

            m_log.Info("[MaterialsDemoModule]: OSMaterials: " + OSDParser.SerializeJsonString(OSMaterials));

            if (matsArr == null)
            {
                m_log.Info("[MaterialsDemoModule]: matsArr is null :( ");
                return;
            }

            foreach (OSD elemOsd in matsArr)
            {
                if (elemOsd != null && elemOsd is OSDMap)
                {
                    OSDMap matMap = elemOsd as OSDMap;
                    if (matMap.ContainsKey("ID") && matMap.ContainsKey("Material"))
                    {
                        try
                        {
                            m_knownMaterials[matMap["ID"].AsUUID()] = (OSDMap)matMap["Material"];
                        }
                        catch (Exception e)
                        {
                            m_log.Warn("[MaterialsDemoModule]: exception decoding persisted material: " + e.ToString());
                        }
                    }
                }
            }
        }

        void StoreMaterialsForPart(SceneObjectPart part)
        {
            try
            {
                if (part == null || part.Shape == null)
                    return;

                Dictionary<UUID, OSDMap> mats = new Dictionary<UUID, OSDMap>();

                Primitive.TextureEntry te = part.Shape.Textures;

                if (te.DefaultTexture != null)
                {
                    if (m_knownMaterials.ContainsKey(te.DefaultTexture.MaterialID))
                        mats[te.DefaultTexture.MaterialID] = m_knownMaterials[te.DefaultTexture.MaterialID];
                }

                if (te.FaceTextures != null)
                {
                    foreach (var face in te.FaceTextures)
                    {
                        if (face != null)
                        {
                            if (m_knownMaterials.ContainsKey(face.MaterialID))
                                mats[face.MaterialID] = m_knownMaterials[face.MaterialID];
                        }
                    }
                }
                if (mats.Count == 0)
                    return;

                OSDArray matsArr = new OSDArray();
                foreach (KeyValuePair<UUID, OSDMap> kvp in mats)
                {
                    OSDMap matOsd = new OSDMap();
                    matOsd["ID"] = OSD.FromUUID(kvp.Key);
                    matOsd["Material"] = kvp.Value;
                    matsArr.Add(matOsd);
                }

                OSDMap OSMaterials = new OSDMap();
                OSMaterials["Materials"] = matsArr;

                lock (part.DynAttrs)
                    part.DynAttrs.SetStore("OpenSim", "Materials", OSMaterials);
            }
            catch (Exception e)
            {
                m_log.Warn("[MaterialsDemoModule]: exception in StoreMaterialsForPart(): " + e.ToString());
            }
        }

        public string RenderMaterialsPostCap(string request, string path,
                string param, IOSHttpRequest httpRequest,
                IOSHttpResponse httpResponse)
        {
            m_log.Debug("[MaterialsDemoModule]: POST cap handler");

            OSDMap req = (OSDMap)OSDParser.DeserializeLLSDXml(request);
            OSDMap resp = new OSDMap();

            OSDMap materialsFromViewer = null;

            OSDArray respArr = new OSDArray();

            if (req.ContainsKey("Zipped"))
            {
                OSD osd = null;

                byte[] inBytes = req["Zipped"].AsBinary();

                try 
                {
                    osd = ZDecompressBytesToOsd(inBytes);

                    if (osd != null)
                    {
                        if (osd is OSDArray) // assume array of MaterialIDs designating requested material entries
                        {
                            foreach (OSD elem in (OSDArray)osd)
                            {

                                try
                                {
                                    UUID id = new UUID(elem.AsBinary(), 0);
                                    
                                    if (m_knownMaterials.ContainsKey(id))
                                    {
                                        m_log.Info("[MaterialsDemoModule]: request for known material ID: " + id.ToString());
                                        OSDMap matMap = new OSDMap();
                                        matMap["ID"] = OSD.FromBinary(id.GetBytes());

                                        matMap["Material"] = m_knownMaterials[id];
                                        respArr.Add(matMap);
                                    }
                                    else
                                        m_log.Info("[MaterialsDemoModule]: request for UNKNOWN material ID: " + id.ToString());
                                }
                                catch (Exception e)
                                {
                                    // report something here?
                                    continue;
                                }
                            }
                        }
                        else if (osd is OSDMap) // reqest to assign a material
                        {
                            materialsFromViewer = osd as OSDMap;

                            if (materialsFromViewer.ContainsKey("FullMaterialsPerFace"))
                            {
                                OSD matsOsd = materialsFromViewer["FullMaterialsPerFace"];
                                if (matsOsd is OSDArray)
                                {
                                    OSDArray matsArr = matsOsd as OSDArray;

                                    try
                                    {
                                        foreach (OSDMap matsMap in matsArr)
                                        {
                                            m_log.Debug("[MaterialsDemoModule]: processing matsMap: " + OSDParser.SerializeJsonString(matsMap));

                                            uint matLocalID = 0;
                                            try { matLocalID = matsMap["ID"].AsUInteger(); }
                                            catch (Exception e) { m_log.Warn("[MaterialsDemoModule]: cannot decode \"ID\" from matsMap: " + e.Message); }
                                            m_log.Debug("[MaterialsDemoModule]: matLocalId: " + matLocalID.ToString());


                                            OSDMap mat = null;
                                            try { mat = matsMap["Material"] as OSDMap; }
                                            catch (Exception e) { m_log.Warn("[MaterialsDemoModule]: cannot decode \"Material\" from matsMap: " + e.Message); }
                                            m_log.Debug("[MaterialsDemoModule]: mat: " + OSDParser.SerializeJsonString(mat));
                                        
                                            UUID id = HashOsd(mat);
                                            m_knownMaterials[id] = mat;
                                        

                                            var sop = m_scene.GetSceneObjectPart(matLocalID);
                                            if (sop == null)
                                                m_log.Debug("[MaterialsDemoModule]: null SOP for localId: " + matLocalID.ToString());
                                            else
                                            {
                                                //var te = sop.Shape.Textures;
                                                var te = new Primitive.TextureEntry(sop.Shape.TextureEntry, 0, sop.Shape.TextureEntry.Length);

                                                if (te == null)
                                                {
                                                    m_log.Debug("[MaterialsDemoModule]: null TextureEntry for localId: " + matLocalID.ToString());
                                                }
                                                else
                                                {
                                                    int face = -1;

                                                    if (matsMap.ContainsKey("Face"))
                                                    {
                                                        face = matsMap["Face"].AsInteger();
                                                        if (te.FaceTextures == null) // && face == 0)
                                                        {
                                                            if (te.DefaultTexture == null)
                                                                m_log.Debug("[MaterialsDemoModule]: te.DefaultTexture is null");
                                                            else
                                                            {
                                                                if (te.DefaultTexture.MaterialID == null)
                                                                    m_log.Debug("[MaterialsDemoModule]: te.DefaultTexture.MaterialID is null");
                                                                else
                                                                {
                                                                    te.DefaultTexture.MaterialID = id;
                                                                }
                                                            }
                                                        }
                                                        else
                                                        {
                                                            if (te.FaceTextures.Length >= face - 1)
                                                            {
                                                                if (te.FaceTextures[face] == null)
                                                                    te.DefaultTexture.MaterialID = id;
                                                                else
                                                                    te.FaceTextures[face].MaterialID = id;
                                                            }
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (te.DefaultTexture != null)
                                                            te.DefaultTexture.MaterialID = id;
                                                    }

                                                    m_log.Debug("[MaterialsDemoModule]: setting material ID for face " + face.ToString() + " to " + id.ToString());

                                                    //we cant use sop.UpdateTextureEntry(te); because it filters so do it manually

                                                    if (sop.ParentGroup != null)
                                                    {
                                                        sop.Shape.TextureEntry = te.GetBytes();
                                                        sop.TriggerScriptChangedEvent(Changed.TEXTURE);
                                                        sop.UpdateFlag = UpdateRequired.FULL;
                                                        sop.ParentGroup.HasGroupChanged = true;

                                                        sop.ScheduleFullUpdate();

                                                        StoreMaterialsForPart(sop);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        m_log.Warn("[MaterialsDemoModule]: exception processing received material: " + e.Message);
                                    }
                                }
                            }
                        }
                    }

                }
                catch (Exception e)
                {
                    m_log.Warn("[MaterialsDemoModule]: exception decoding zipped CAP payload: " + e.Message);
                    //return "";
                }
                m_log.Debug("[MaterialsDemoModule]: knownMaterials.Count: " + m_knownMaterials.Count.ToString());
            }

            
            resp["Zipped"] = ZCompressOSD(respArr, false);
            string response = OSDParser.SerializeLLSDXmlString(resp);

            //m_log.Debug("[MaterialsDemoModule]: cap request: " + request);
            m_log.Debug("[MaterialsDemoModule]: cap request (zipped portion): " + ZippedOsdBytesToString(req["Zipped"].AsBinary()));
            m_log.Debug("[MaterialsDemoModule]: cap response: " + response);
            return response;
        }


        public string RenderMaterialsGetCap(string request, string path,
                string param, IOSHttpRequest httpRequest,
                IOSHttpResponse httpResponse)
        {
            m_log.Debug("[MaterialsDemoModule]: GET cap handler");

            OSDMap resp = new OSDMap();

 
            int matsCount = 0;

            OSDArray allOsd = new OSDArray();

            foreach (KeyValuePair<UUID, OSDMap> kvp in m_knownMaterials)
            {
                OSDMap matMap = new OSDMap();

                matMap["ID"] = OSD.FromBinary(kvp.Key.GetBytes());

                matMap["Material"] = kvp.Value;
                allOsd.Add(matMap);
                matsCount++;
            }


            resp["Zipped"] = ZCompressOSD(allOsd, false);
            m_log.Debug("[MaterialsDemoModule]: matsCount: " + matsCount.ToString());

            return OSDParser.SerializeLLSDXmlString(resp);
        }

        static string ZippedOsdBytesToString(byte[] bytes)
        {
            try
            {
                return OSDParser.SerializeJsonString(ZDecompressBytesToOsd(bytes));
            }
            catch (Exception e)
            {
                return "ZippedOsdBytesToString caught an exception: " + e.ToString();
            }
        }

        /// <summary>
        /// computes a UUID by hashing a OSD object
        /// </summary>
        /// <param name="osd"></param>
        /// <returns></returns>
        private static UUID HashOsd(OSD osd)
        {
            using (var md5 = MD5.Create())
                using (MemoryStream ms = new MemoryStream(OSDParser.SerializeLLSDBinary(osd, false)))
                    return new UUID(md5.ComputeHash(ms), 0);
        }

        public static OSD ZCompressOSD(OSD inOsd, bool useHeader)
        {
            OSD osd = null;

            using (MemoryStream msSinkCompressed = new MemoryStream())
            {
                using (Ionic.Zlib.ZlibStream zOut = new Ionic.Zlib.ZlibStream(msSinkCompressed, 
                    Ionic.Zlib.CompressionMode.Compress, CompressionLevel.BestCompression, true))
                {
                    CopyStream(new MemoryStream(OSDParser.SerializeLLSDBinary(inOsd, useHeader)), zOut);
                    zOut.Close();
                }

                msSinkCompressed.Seek(0L, SeekOrigin.Begin);
                osd = OSD.FromBinary( msSinkCompressed.ToArray());
            }

            return osd;
        }


        public static OSD ZDecompressBytesToOsd(byte[] input)
        {
            OSD osd = null;

            using (MemoryStream msSinkUnCompressed = new MemoryStream())
            {
                using (Ionic.Zlib.ZlibStream zOut = new Ionic.Zlib.ZlibStream(msSinkUnCompressed, CompressionMode.Decompress, true))
                {
                    CopyStream(new MemoryStream(input), zOut);
                    zOut.Close();
                }
                msSinkUnCompressed.Seek(0L, SeekOrigin.Begin);
                osd = OSDParser.DeserializeLLSDBinary(msSinkUnCompressed.ToArray());
            }

            return osd;
        }

        static void CopyStream(System.IO.Stream input, System.IO.Stream output)
        {
            byte[] buffer = new byte[2048];
            int len;
            while ((len = input.Read(buffer, 0, 2048)) > 0)
            {
                output.Write(buffer, 0, len);
            }

            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);
                            }
                        }
                    }
                }
            }
        }
    }
}