UuidGatherer.cs 15 KB

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