UuidGatherer.cs 17 KB

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