UuidGatherer.cs 14 KB

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