UuidGatherer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Text.RegularExpressions;
  31. using System.Threading;
  32. using log4net;
  33. using OpenMetaverse;
  34. using OpenMetaverse.Assets;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Framework.Scenes.Serialization;
  37. using OpenSim.Services.Interfaces;
  38. namespace OpenSim.Region.Framework.Scenes
  39. {
  40. /// <summary>
  41. /// Gather uuids for a given entity.
  42. /// </summary>
  43. ///
  44. /// This does a deep inspection of the entity to retrieve all the assets it uses (whether as textures, as scripts
  45. /// contained in inventory, as scripts contained in objects contained in another object's inventory, etc. Assets
  46. /// are only retrieved when they are necessary to carry out the inspection (i.e. a serialized object needs to be
  47. /// retrieved to work out which assets it references).
  48. public class UuidGatherer
  49. {
  50. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. /// <summary>
  52. /// Asset cache used for gathering assets
  53. /// </summary>
  54. protected IAssetService m_assetCache;
  55. /// <summary>
  56. /// Used as a temporary store of an asset which represents an object. This can be a null if no appropriate
  57. /// asset was found by the asset service.
  58. /// </summary>
  59. protected AssetBase m_requestedObjectAsset;
  60. /// <summary>
  61. /// Signal whether we are currently waiting for the asset service to deliver an asset.
  62. /// </summary>
  63. protected bool m_waitingForObjectAsset;
  64. public UuidGatherer(IAssetService assetCache)
  65. {
  66. m_assetCache = assetCache;
  67. }
  68. /// <summary>
  69. /// Gather all the asset uuids associated with the asset referenced by a given uuid
  70. /// </summary>
  71. ///
  72. /// This includes both those directly associated with
  73. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  74. /// within this object).
  75. ///
  76. /// <param name="assetUuid">The uuid of the asset for which to gather referenced assets</param>
  77. /// <param name="assetType">The type of the asset for the uuid given</param>
  78. /// <param name="assetUuids">The assets gathered</param>
  79. public void GatherAssetUuids(UUID assetUuid, AssetType assetType, IDictionary<UUID, int> assetUuids)
  80. {
  81. assetUuids[assetUuid] = 1;
  82. if (AssetType.Bodypart == assetType || AssetType.Clothing == assetType)
  83. {
  84. GetWearableAssetUuids(assetUuid, assetUuids);
  85. }
  86. else if (AssetType.LSLText == assetType)
  87. {
  88. GetScriptAssetUuids(assetUuid, assetUuids);
  89. }
  90. else if (AssetType.Object == assetType)
  91. {
  92. GetSceneObjectAssetUuids(assetUuid, assetUuids);
  93. }
  94. }
  95. /// <summary>
  96. /// Gather all the asset uuids associated with a given object.
  97. /// </summary>
  98. ///
  99. /// This includes both those directly associated with
  100. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  101. /// within this object).
  102. ///
  103. /// <param name="sceneObject">The scene object for which to gather assets</param>
  104. /// <param name="assetUuids">The assets gathered</param>
  105. public void GatherAssetUuids(SceneObjectGroup sceneObject, IDictionary<UUID, int> assetUuids)
  106. {
  107. // m_log.DebugFormat(
  108. // "[ASSET GATHERER]: Getting assets for object {0}, {1}", sceneObject.Name, sceneObject.UUID);
  109. foreach (SceneObjectPart part in sceneObject.GetParts())
  110. {
  111. //m_log.DebugFormat(
  112. // "[ARCHIVER]: Getting part {0}, {1} for object {2}", part.Name, part.UUID, sceneObject.UUID);
  113. try
  114. {
  115. Primitive.TextureEntry textureEntry = part.Shape.Textures;
  116. // Get the prim's default texture. This will be used for faces which don't have their own texture
  117. assetUuids[textureEntry.DefaultTexture.TextureID] = 1;
  118. // XXX: Not a great way to iterate through face textures, but there's no
  119. // other method available to tell how many faces there actually are
  120. //int i = 0;
  121. foreach (Primitive.TextureEntryFace texture in textureEntry.FaceTextures)
  122. {
  123. if (texture != null)
  124. {
  125. //m_log.DebugFormat("[ARCHIVER]: Got face {0}", i++);
  126. assetUuids[texture.TextureID] = 1;
  127. }
  128. }
  129. // If the prim is a sculpt then preserve this information too
  130. if (part.Shape.SculptTexture != UUID.Zero)
  131. assetUuids[part.Shape.SculptTexture] = 1;
  132. TaskInventoryDictionary taskDictionary = (TaskInventoryDictionary)part.TaskInventory.Clone();
  133. // Now analyze this prim's inventory items to preserve all the uuids that they reference
  134. foreach (TaskInventoryItem tii in taskDictionary.Values)
  135. {
  136. //m_log.DebugFormat("[ARCHIVER]: Analysing item asset type {0}", tii.Type);
  137. if (!assetUuids.ContainsKey(tii.AssetID))
  138. GatherAssetUuids(tii.AssetID, (AssetType)tii.Type, assetUuids);
  139. }
  140. }
  141. catch (Exception e)
  142. {
  143. m_log.ErrorFormat("[UUID GATHERER]: Failed to get part - {0}", e);
  144. m_log.DebugFormat(
  145. "[UUID GATHERER]: Texture entry length for prim was {0} (min is 46)",
  146. part.Shape.TextureEntry.Length);
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// The callback made when we request the asset for an object from the asset service.
  152. /// </summary>
  153. protected void AssetReceived(string id, Object sender, AssetBase asset)
  154. {
  155. lock (this)
  156. {
  157. m_requestedObjectAsset = asset;
  158. m_waitingForObjectAsset = false;
  159. Monitor.Pulse(this);
  160. }
  161. }
  162. /// <summary>
  163. /// Get an asset synchronously, potentially using an asynchronous callback. If the
  164. /// asynchronous callback is used, we will wait for it to complete.
  165. /// </summary>
  166. /// <param name="uuid"></param>
  167. /// <returns></returns>
  168. protected virtual AssetBase GetAsset(UUID uuid)
  169. {
  170. m_waitingForObjectAsset = true;
  171. m_assetCache.Get(uuid.ToString(), this, AssetReceived);
  172. // The asset cache callback can either
  173. //
  174. // 1. Complete on the same thread (if the asset is already in the cache) or
  175. // 2. Come in via a different thread (if we need to go fetch it).
  176. //
  177. // The code below handles both these alternatives.
  178. lock (this)
  179. {
  180. if (m_waitingForObjectAsset)
  181. {
  182. Monitor.Wait(this);
  183. m_waitingForObjectAsset = false;
  184. }
  185. }
  186. return m_requestedObjectAsset;
  187. }
  188. /// <summary>
  189. /// Record the asset uuids embedded within the given script.
  190. /// </summary>
  191. /// <param name="scriptUuid"></param>
  192. /// <param name="assetUuids">Dictionary in which to record the references</param>
  193. protected void GetScriptAssetUuids(UUID scriptUuid, IDictionary<UUID, int> assetUuids)
  194. {
  195. AssetBase scriptAsset = GetAsset(scriptUuid);
  196. if (null != scriptAsset)
  197. {
  198. string script = Utils.BytesToString(scriptAsset.Data);
  199. //m_log.DebugFormat("[ARCHIVER]: Script {0}", script);
  200. MatchCollection uuidMatches = Util.UUIDPattern.Matches(script);
  201. //m_log.DebugFormat("[ARCHIVER]: Found {0} matches in script", uuidMatches.Count);
  202. foreach (Match uuidMatch in uuidMatches)
  203. {
  204. UUID uuid = new UUID(uuidMatch.Value);
  205. //m_log.DebugFormat("[ARCHIVER]: Recording {0} in script", uuid);
  206. assetUuids[uuid] = 1;
  207. }
  208. }
  209. }
  210. /// <summary>
  211. /// Record the uuids referenced by the given wearable asset
  212. /// </summary>
  213. /// <param name="wearableAssetUuid"></param>
  214. /// <param name="assetUuids">Dictionary in which to record the references</param>
  215. protected void GetWearableAssetUuids(UUID wearableAssetUuid, IDictionary<UUID, int> assetUuids)
  216. {
  217. AssetBase assetBase = GetAsset(wearableAssetUuid);
  218. if (null != assetBase)
  219. {
  220. //m_log.Debug(new System.Text.ASCIIEncoding().GetString(bodypartAsset.Data));
  221. AssetWearable wearableAsset = new AssetBodypart(wearableAssetUuid, assetBase.Data);
  222. wearableAsset.Decode();
  223. //m_log.DebugFormat(
  224. // "[ARCHIVER]: Wearable asset {0} references {1} assets", wearableAssetUuid, wearableAsset.Textures.Count);
  225. foreach (UUID uuid in wearableAsset.Textures.Values)
  226. {
  227. //m_log.DebugFormat("[ARCHIVER]: Got bodypart uuid {0}", uuid);
  228. assetUuids[uuid] = 1;
  229. }
  230. }
  231. }
  232. /// <summary>
  233. /// Get all the asset uuids associated with a given object. This includes both those directly associated with
  234. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  235. /// within this object).
  236. /// </summary>
  237. /// <param name="sceneObject"></param>
  238. /// <param name="assetUuids"></param>
  239. protected void GetSceneObjectAssetUuids(UUID sceneObjectUuid, IDictionary<UUID, int> assetUuids)
  240. {
  241. AssetBase objectAsset = GetAsset(sceneObjectUuid);
  242. if (null != objectAsset)
  243. {
  244. string xml = Utils.BytesToString(objectAsset.Data);
  245. SceneObjectGroup sog = SceneObjectSerializer.FromOriginalXmlFormat(xml);
  246. GatherAssetUuids(sog, assetUuids);
  247. }
  248. }
  249. }
  250. }