OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
UuidGatherer.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 using System;
29 using System.Collections.Generic;
30 using System.IO;
31 using System.Reflection;
32 using System.Text.RegularExpressions;
33 using System.Threading;
34 using log4net;
35 using OpenMetaverse;
36 using OpenMetaverse.Assets;
37 using OpenMetaverse.StructuredData;
38 using OpenSim.Framework;
39 using OpenSim.Region.Framework.Scenes.Serialization;
40 using OpenSim.Services.Interfaces;
42 
43 namespace OpenSim.Region.Framework.Scenes
44 {
54  public class UuidGatherer
55  {
56  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
57 
61  public bool Complete { get { return m_assetUuidsToInspect.Count <= 0; } }
62 
67  public IDictionary<UUID, sbyte> GatheredUuids { get; private set; }
68 
73  public UUID? NextUuidToInspect
74  {
75  get
76  {
77  if (Complete)
78  return null;
79  else
80  return m_assetUuidsToInspect.Peek();
81  }
82  }
83 
85 
86  protected Queue<UUID> m_assetUuidsToInspect;
87 
95  public UuidGatherer(IAssetService assetService) : this(assetService, new Dictionary<UUID, sbyte>()) {}
96 
107  public UuidGatherer(IAssetService assetService, IDictionary<UUID, sbyte> collector)
108  {
109  m_assetService = assetService;
110  GatheredUuids = collector;
111 
112  // FIXME: Not efficient for searching, can improve.
113  m_assetUuidsToInspect = new Queue<UUID>();
114  }
115 
121  public bool AddForInspection(UUID uuid)
122  {
123  if (m_assetUuidsToInspect.Contains(uuid))
124  return false;
125 
126 // m_log.DebugFormat("[UUID GATHERER]: Adding asset {0} for inspection", uuid);
127 
128  m_assetUuidsToInspect.Enqueue(uuid);
129  return true;
130  }
131 
141  public void AddForInspection(SceneObjectGroup sceneObject)
142  {
143  // m_log.DebugFormat(
144  // "[ASSET GATHERER]: Getting assets for object {0}, {1}", sceneObject.Name, sceneObject.UUID);
145 
146  SceneObjectPart[] parts = sceneObject.Parts;
147  for (int i = 0; i < parts.Length; i++)
148  {
149  SceneObjectPart part = parts[i];
150 
151  // m_log.DebugFormat(
152  // "[ARCHIVER]: Getting part {0}, {1} for object {2}", part.Name, part.UUID, sceneObject.UUID);
153 
154  try
155  {
156  Primitive.TextureEntry textureEntry = part.Shape.Textures;
157  if (textureEntry != null)
158  {
159  // Get the prim's default texture. This will be used for faces which don't have their own texture
160  if (textureEntry.DefaultTexture != null)
161  RecordTextureEntryAssetUuids(textureEntry.DefaultTexture);
162 
163  if (textureEntry.FaceTextures != null)
164  {
165  // Loop through the rest of the texture faces (a non-null face means the face is different from DefaultTexture)
166  foreach (Primitive.TextureEntryFace texture in textureEntry.FaceTextures)
167  {
168  if (texture != null)
169  RecordTextureEntryAssetUuids(texture);
170  }
171  }
172  }
173 
174  // If the prim is a sculpt then preserve this information too
175  if (part.Shape.SculptTexture != UUID.Zero)
176  GatheredUuids[part.Shape.SculptTexture] = (sbyte)AssetType.Texture;
177 
178  if (part.Shape.ProjectionTextureUUID != UUID.Zero)
179  GatheredUuids[part.Shape.ProjectionTextureUUID] = (sbyte)AssetType.Texture;
180 
181  if (part.CollisionSound != UUID.Zero)
182  GatheredUuids[part.CollisionSound] = (sbyte)AssetType.Sound;
183 
184  if (part.ParticleSystem.Length > 0)
185  {
186  try
187  {
188  Primitive.ParticleSystem ps = new Primitive.ParticleSystem(part.ParticleSystem, 0);
189  if (ps.Texture != UUID.Zero)
190  GatheredUuids[ps.Texture] = (sbyte)AssetType.Texture;
191  }
192  catch (Exception)
193  {
194  m_log.WarnFormat(
195  "[UUID GATHERER]: Could not check particle system for part {0} {1} in object {2} {3} since it is corrupt. Continuing.",
196  part.Name, part.UUID, sceneObject.Name, sceneObject.UUID);
197  }
198  }
199 
201 
202  // Now analyze this prim's inventory items to preserve all the uuids that they reference
203  foreach (TaskInventoryItem tii in taskDictionary.Values)
204  {
205  // m_log.DebugFormat(
206  // "[ARCHIVER]: Analysing item {0} asset type {1} in {2} {3}",
207  // tii.Name, tii.Type, part.Name, part.UUID);
208 
209  if (!GatheredUuids.ContainsKey(tii.AssetID))
210  AddForInspection(tii.AssetID, (sbyte)tii.Type);
211  }
212 
213  // FIXME: We need to make gathering modular but we cannot yet, since gatherers are not guaranteed
214  // to be called with scene objects that are in a scene (e.g. in the case of hg asset mapping and
215  // inventory transfer. There needs to be a way for a module to register a method without assuming a
216  // Scene.EventManager is present.
217  // part.ParentGroup.Scene.EventManager.TriggerGatherUuids(part, assetUuids);
218 
219 
220  // still needed to retrieve textures used as materials for any parts containing legacy materials stored in DynAttrs
221  RecordMaterialsUuids(part);
222  }
223  catch (Exception e)
224  {
225  m_log.ErrorFormat("[UUID GATHERER]: Failed to get part - {0}", e);
226  m_log.DebugFormat(
227  "[UUID GATHERER]: Texture entry length for prim was {0} (min is 46)",
228  part.Shape.TextureEntry.Length);
229  }
230  }
231  }
232 
237  public bool GatherNext()
238  {
239  if (Complete)
240  return false;
241 
242  UUID nextToInspect = m_assetUuidsToInspect.Dequeue();
243 
244 // m_log.DebugFormat("[UUID GATHERER]: Inspecting asset {0}", nextToInspect);
245 
246  GetAssetUuids(nextToInspect);
247 
248  return true;
249  }
250 
255  public bool GatherAll()
256  {
257  if (Complete)
258  return false;
259 
260  while (GatherNext());
261 
262  return true;
263  }
264 
277  private void GetAssetUuids(UUID assetUuid)
278  {
279  // avoid infinite loops
280  if (GatheredUuids.ContainsKey(assetUuid))
281  return;
282 
283  try
284  {
285  AssetBase assetBase = GetAsset(assetUuid);
286 
287  if (null != assetBase)
288  {
289  sbyte assetType = assetBase.Type;
290  GatheredUuids[assetUuid] = assetType;
291 
292  if ((sbyte)AssetType.Bodypart == assetType || (sbyte)AssetType.Clothing == assetType)
293  {
294  RecordWearableAssetUuids(assetBase);
295  }
296  else if ((sbyte)AssetType.Gesture == assetType)
297  {
298  RecordGestureAssetUuids(assetBase);
299  }
300  else if ((sbyte)AssetType.Notecard == assetType)
301  {
302  RecordTextEmbeddedAssetUuids(assetBase);
303  }
304  else if ((sbyte)AssetType.LSLText == assetType)
305  {
306  RecordTextEmbeddedAssetUuids(assetBase);
307  }
308  else if ((sbyte)OpenSimAssetType.Material == assetType)
309  {
310  RecordMaterialAssetUuids(assetBase);
311  }
312  else if ((sbyte)AssetType.Object == assetType)
313  {
314  RecordSceneObjectAssetUuids(assetBase);
315  }
316  }
317  }
318  catch (Exception)
319  {
320  m_log.ErrorFormat("[UUID GATHERER]: Failed to gather uuids for asset id {0}", assetUuid);
321  throw;
322  }
323  }
324 
325  private void AddForInspection(UUID assetUuid, sbyte assetType)
326  {
327  // Here, we want to collect uuids which require further asset fetches but mark the others as gathered
328  try
329  {
330  if ((sbyte)AssetType.Bodypart == assetType
331  || (sbyte)AssetType.Clothing == assetType
332  || (sbyte)AssetType.Gesture == assetType
333  || (sbyte)AssetType.Notecard == assetType
334  || (sbyte)AssetType.LSLText == assetType
335  || (sbyte)OpenSimAssetType.Material == assetType
336  || (sbyte)AssetType.Object == assetType)
337  {
338  AddForInspection(assetUuid);
339  }
340  else
341  {
342  GatheredUuids[assetUuid] = assetType;
343  }
344  }
345  catch (Exception)
346  {
347  m_log.ErrorFormat(
348  "[UUID GATHERER]: Failed to gather uuids for asset id {0}, type {1}",
349  assetUuid, assetType);
350  throw;
351  }
352  }
353 
357  private void RecordTextureEntryAssetUuids(Primitive.TextureEntryFace texture)
358  {
359  GatheredUuids[texture.TextureID] = (sbyte)AssetType.Texture;
360 
361  if (texture.MaterialID != UUID.Zero)
362  AddForInspection(texture.MaterialID);
363  }
364 
370  private void RecordMaterialsUuids(SceneObjectPart part)
371  {
372  // scan thru the dynAttrs map of this part for any textures used as materials
373  OSD osdMaterials = null;
374 
375  lock (part.DynAttrs)
376  {
377  if (part.DynAttrs.ContainsStore("OpenSim", "Materials"))
378  {
379  OSDMap materialsStore = part.DynAttrs.GetStore("OpenSim", "Materials");
380 
381  if (materialsStore == null)
382  return;
383 
384  materialsStore.TryGetValue("Materials", out osdMaterials);
385  }
386 
387  if (osdMaterials != null)
388  {
389  //m_log.Info("[UUID Gatherer]: found Materials: " + OSDParser.SerializeJsonString(osd));
390 
391  if (osdMaterials is OSDArray)
392  {
393  OSDArray matsArr = osdMaterials as OSDArray;
394  foreach (OSDMap matMap in matsArr)
395  {
396  try
397  {
398  if (matMap.ContainsKey("Material"))
399  {
400  OSDMap mat = matMap["Material"] as OSDMap;
401  if (mat.ContainsKey("NormMap"))
402  {
403  UUID normalMapId = mat["NormMap"].AsUUID();
404  if (normalMapId != UUID.Zero)
405  {
406  GatheredUuids[normalMapId] = (sbyte)AssetType.Texture;
407  //m_log.Info("[UUID Gatherer]: found normal map ID: " + normalMapId.ToString());
408  }
409  }
410  if (mat.ContainsKey("SpecMap"))
411  {
412  UUID specularMapId = mat["SpecMap"].AsUUID();
413  if (specularMapId != UUID.Zero)
414  {
415  GatheredUuids[specularMapId] = (sbyte)AssetType.Texture;
416  //m_log.Info("[UUID Gatherer]: found specular map ID: " + specularMapId.ToString());
417  }
418  }
419  }
420 
421  }
422  catch (Exception e)
423  {
424  m_log.Warn("[UUID Gatherer]: exception getting materials: " + e.Message);
425  }
426  }
427  }
428  }
429  }
430  }
431 
438  protected virtual AssetBase GetAsset(UUID uuid)
439  {
440  return m_assetService.Get(uuid.ToString());
441  }
442 
447  private void RecordTextEmbeddedAssetUuids(AssetBase textAsset)
448  {
449  // m_log.DebugFormat("[ASSET GATHERER]: Getting assets for uuid references in asset {0}", embeddingAssetId);
450 
451  string text = Utils.BytesToString(textAsset.Data);
452 // m_log.DebugFormat("[UUID GATHERER]: Text {0}", text);
453  MatchCollection uuidMatches = Util.PermissiveUUIDPattern.Matches(text);
454 // m_log.DebugFormat("[UUID GATHERER]: Found {0} matches in text", uuidMatches.Count);
455 
456  foreach (Match uuidMatch in uuidMatches)
457  {
458  UUID uuid = new UUID(uuidMatch.Value);
459 // m_log.DebugFormat("[UUID GATHERER]: Recording {0} in text", uuid);
460 
461  AddForInspection(uuid);
462  }
463  }
464 
469  private void RecordWearableAssetUuids(AssetBase assetBase)
470  {
471  //m_log.Debug(new System.Text.ASCIIEncoding().GetString(bodypartAsset.Data));
472  AssetWearable wearableAsset = new AssetBodypart(assetBase.FullID, assetBase.Data);
473  wearableAsset.Decode();
474 
475  //m_log.DebugFormat(
476  // "[ARCHIVER]: Wearable asset {0} references {1} assets", wearableAssetUuid, wearableAsset.Textures.Count);
477 
478  foreach (UUID uuid in wearableAsset.Textures.Values)
479  GatheredUuids[uuid] = (sbyte)AssetType.Texture;
480  }
481 
488  private void RecordSceneObjectAssetUuids(AssetBase sceneObjectAsset)
489  {
490  string xml = Utils.BytesToString(sceneObjectAsset.Data);
491 
492  CoalescedSceneObjects coa;
494  {
495  foreach (SceneObjectGroup sog in coa.Objects)
496  AddForInspection(sog);
497  }
498  else
499  {
500  SceneObjectGroup sog = SceneObjectSerializer.FromOriginalXmlFormat(xml);
501 
502  if (null != sog)
503  AddForInspection(sog);
504  }
505  }
506 
511  private void RecordGestureAssetUuids(AssetBase gestureAsset)
512  {
513  using (MemoryStream ms = new MemoryStream(gestureAsset.Data))
514  using (StreamReader sr = new StreamReader(ms))
515  {
516  sr.ReadLine(); // Unknown (Version?)
517  sr.ReadLine(); // Unknown
518  sr.ReadLine(); // Unknown
519  sr.ReadLine(); // Name
520  sr.ReadLine(); // Comment ?
521  int count = Convert.ToInt32(sr.ReadLine()); // Item count
522 
523  for (int i = 0 ; i < count ; i++)
524  {
525  string type = sr.ReadLine();
526  if (type == null)
527  break;
528  string name = sr.ReadLine();
529  if (name == null)
530  break;
531  string id = sr.ReadLine();
532  if (id == null)
533  break;
534  string unknown = sr.ReadLine();
535  if (unknown == null)
536  break;
537 
538  // If it can be parsed as a UUID, it is an asset ID
539  UUID uuid;
540  if (UUID.TryParse(id, out uuid))
541  GatheredUuids[uuid] = (sbyte)AssetType.Animation; // the asset is either an Animation or a Sound, but this distinction isn't important
542  }
543  }
544  }
545 
549  private void RecordMaterialAssetUuids(AssetBase materialAsset)
550  {
551  OSDMap mat = (OSDMap)OSDParser.DeserializeLLSDXml(materialAsset.Data);
552 
553  UUID normMap = mat["NormMap"].AsUUID();
554  if (normMap != UUID.Zero)
555  GatheredUuids[normMap] = (sbyte)AssetType.Texture;
556 
557  UUID specMap = mat["SpecMap"].AsUUID();
558  if (specMap != UUID.Zero)
559  GatheredUuids[specMap] = (sbyte)AssetType.Texture;
560  }
561  }
562 
564  {
565  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
566 
567  protected string m_assetServerURL;
568 
569  public HGUuidGatherer(IAssetService assetService, string assetServerURL)
570  : this(assetService, assetServerURL, new Dictionary<UUID, sbyte>()) {}
571 
572  public HGUuidGatherer(IAssetService assetService, string assetServerURL, IDictionary<UUID, sbyte> collector)
573  : base(assetService, collector)
574  {
575  m_assetServerURL = assetServerURL;
576  if (!m_assetServerURL.EndsWith("/") && !m_assetServerURL.EndsWith("="))
577  m_assetServerURL = m_assetServerURL + "/";
578  }
579 
580  protected override AssetBase GetAsset(UUID uuid)
581  {
582  if (string.Empty == m_assetServerURL)
583  return base.GetAsset(uuid);
584  else
585  return FetchAsset(uuid);
586  }
587 
588  public AssetBase FetchAsset(UUID assetID)
589  {
590  // Test if it's already here
591  AssetBase asset = m_assetService.Get(assetID.ToString());
592  if (asset == null)
593  {
594  // It's not, so fetch it from abroad
595  asset = m_assetService.Get(m_assetServerURL + assetID.ToString());
596  if (asset != null)
597  m_log.DebugFormat("[HGUUIDGatherer]: Copied asset {0} from {1} to local asset server", assetID, m_assetServerURL);
598  else
599  m_log.DebugFormat("[HGUUIDGatherer]: Failed to fetch asset {0} from {1}", assetID, m_assetServerURL);
600  }
601  //else
602  // m_log.DebugFormat("[HGUUIDGatherer]: Asset {0} from {1} was already here", assetID, m_assetServerURL);
603 
604  return asset;
605  }
606  }
607 }
Gather uuids for a given entity.
Definition: UuidGatherer.cs:54
OpenSim.Framework.SLUtil.OpenSimAssetType OpenSimAssetType
Definition: UuidGatherer.cs:41
OpenMetaverse.StructuredData.OSDArray OSDArray
OpenSim.Framework.SLUtil.OpenSimAssetType OpenSimAssetType
A dictionary containing task inventory items. Indexed by item UUID.
UuidGatherer(IAssetService assetService)
Initializes a new instance of the OpenSim.Region.Framework.Scenes.UuidGatherer class.
Definition: UuidGatherer.cs:95
HGUuidGatherer(IAssetService assetService, string assetServerURL)
OpenMetaverse.StructuredData.OSDMap OSDMap
A scene object group is conceptually an object in the scene. The object is constituted of SceneObject...
Represents an item in a task inventory
bool GatherAll()
Gathers all remaining asset UUIDS no matter how many calls are required to the asset service...
Asset class. All Assets are reference by this class or a class derived from this class ...
Definition: AssetBase.cs:49
void AddForInspection(SceneObjectGroup sceneObject)
Gather all the asset uuids associated with a given object.
bool GatherNext()
Gathers the next set of assets returned by the next uuid to get from the asset service.
OpenMetaverse.StructuredData.OSD OSD
OpenSim.Framework.Animation Animation
HGUuidGatherer(IAssetService assetService, string assetServerURL, IDictionary< UUID, sbyte > collector)
bool AddForInspection(UUID uuid)
Adds the asset uuid for inspection during the gathering process.
UuidGatherer(IAssetService assetService, IDictionary< UUID, sbyte > collector)
Initializes a new instance of the OpenSim.Region.Framework.Scenes.UuidGatherer class.
override AssetBase GetAsset(UUID uuid)
Get an asset synchronously, potentially using an asynchronous callback. If the asynchronous callback ...
UUID FullID
Asset UUID
Definition: AssetBase.cs:168
virtual AssetBase GetAsset(UUID uuid)
Get an asset synchronously, potentially using an asynchronous callback. If the asynchronous callback ...