UuidGatherer.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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 OpenMetaverse.StructuredData;
  37. using OpenSim.Framework;
  38. using OpenSim.Region.Framework.Scenes.Serialization;
  39. using OpenSim.Services.Interfaces;
  40. using OpenSimAssetType = OpenSim.Framework.SLUtil.OpenSimAssetType;
  41. namespace OpenSim.Region.Framework.Scenes
  42. {
  43. /// <summary>
  44. /// Gather uuids for a given entity.
  45. /// </summary>
  46. /// <remarks>
  47. /// This does a deep inspection of the entity to retrieve all the assets it uses (whether as textures, as scripts
  48. /// contained in inventory, as scripts contained in objects contained in another object's inventory, etc. Assets
  49. /// are only retrieved when they are necessary to carry out the inspection (i.e. a serialized object needs to be
  50. /// retrieved to work out which assets it references).
  51. /// </remarks>
  52. public class UuidGatherer
  53. {
  54. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  55. /// <summary>
  56. /// Is gathering complete?
  57. /// </summary>
  58. public bool Complete { get { return m_assetUuidsToInspect.Count <= 0; } }
  59. /// <summary>
  60. /// The dictionary of UUIDs gathered so far. If Complete == true then this is all the reachable UUIDs.
  61. /// </summary>
  62. /// <value>The gathered uuids.</value>
  63. public IDictionary<UUID, sbyte> GatheredUuids { get; private set; }
  64. public HashSet<UUID> FailedUUIDs { get; private set; }
  65. public HashSet<UUID> UncertainAssetsUUIDs { get; private set; }
  66. public int possibleNotAssetCount { get; set; }
  67. public int ErrorCount { get; private set; }
  68. /// <summary>
  69. /// Gets the next UUID to inspect.
  70. /// </summary>
  71. /// <value>If there is no next UUID then returns null</value>
  72. public UUID? NextUuidToInspect
  73. {
  74. get
  75. {
  76. if (Complete)
  77. return null;
  78. else
  79. return m_assetUuidsToInspect.Peek();
  80. }
  81. }
  82. protected IAssetService m_assetService;
  83. protected Queue<UUID> m_assetUuidsToInspect;
  84. /// <summary>
  85. /// Initializes a new instance of the <see cref="OpenSim.Region.Framework.Scenes.UuidGatherer"/> class.
  86. /// </summary>
  87. /// <remarks>In this case the collection of gathered assets will start out blank.</remarks>
  88. /// <param name="assetService">
  89. /// Asset service.
  90. /// </param>
  91. public UuidGatherer(IAssetService assetService) : this(assetService, new Dictionary<UUID, sbyte>(),
  92. new HashSet <UUID>(),new HashSet <UUID>()) {}
  93. public UuidGatherer(IAssetService assetService, IDictionary<UUID, sbyte> collector) : this(assetService, collector,
  94. new HashSet <UUID>(), new HashSet <UUID>()) {}
  95. /// <summary>
  96. /// Initializes a new instance of the <see cref="OpenSim.Region.Framework.Scenes.UuidGatherer"/> class.
  97. /// </summary>
  98. /// <param name="assetService">
  99. /// Asset service.
  100. /// </param>
  101. /// <param name="collector">
  102. /// Gathered UUIDs will be collected in this dictionary.
  103. /// It can be pre-populated if you want to stop the gatherer from analyzing assets that have already been fetched and inspected.
  104. /// </param>
  105. public UuidGatherer(IAssetService assetService, IDictionary<UUID, sbyte> collector, HashSet <UUID> failedIDs, HashSet <UUID> uncertainAssetsUUIDs)
  106. {
  107. m_assetService = assetService;
  108. GatheredUuids = collector;
  109. // FIXME: Not efficient for searching, can improve.
  110. m_assetUuidsToInspect = new Queue<UUID>();
  111. FailedUUIDs = failedIDs;
  112. UncertainAssetsUUIDs = uncertainAssetsUUIDs;
  113. ErrorCount = 0;
  114. possibleNotAssetCount = 0;
  115. }
  116. /// <summary>
  117. /// Adds the asset uuid for inspection during the gathering process.
  118. /// </summary>
  119. /// <returns><c>true</c>, if for inspection was added, <c>false</c> otherwise.</returns>
  120. /// <param name="uuid">UUID.</param>
  121. public bool AddForInspection(UUID uuid)
  122. {
  123. if(uuid == UUID.Zero)
  124. return false;
  125. if(FailedUUIDs.Contains(uuid))
  126. {
  127. if(UncertainAssetsUUIDs.Contains(uuid))
  128. possibleNotAssetCount++;
  129. else
  130. ErrorCount++;
  131. return false;
  132. }
  133. if(GatheredUuids.ContainsKey(uuid))
  134. return false;
  135. if (m_assetUuidsToInspect.Contains(uuid))
  136. return false;
  137. // m_log.DebugFormat("[UUID GATHERER]: Adding asset {0} for inspection", uuid);
  138. m_assetUuidsToInspect.Enqueue(uuid);
  139. return true;
  140. }
  141. /// <summary>
  142. /// Gather all the asset uuids associated with a given object.
  143. /// </summary>
  144. /// <remarks>
  145. /// This includes both those directly associated with
  146. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  147. /// within this object).
  148. /// </remarks>
  149. /// <param name="sceneObject">The scene object for which to gather assets</param>
  150. public void AddForInspection(SceneObjectGroup sceneObject)
  151. {
  152. // m_log.DebugFormat(
  153. // "[UUID GATHERER]: Getting assets for object {0}, {1}", sceneObject.Name, sceneObject.UUID);
  154. if(sceneObject.IsDeleted)
  155. return;
  156. SceneObjectPart[] parts = sceneObject.Parts;
  157. for (int i = 0; i < parts.Length; i++)
  158. {
  159. SceneObjectPart part = parts[i];
  160. // m_log.DebugFormat(
  161. // "[UUID GATHERER]: Getting part {0}, {1} for object {2}", part.Name, part.UUID, sceneObject.UUID);
  162. try
  163. {
  164. Primitive.TextureEntry textureEntry = part.Shape.Textures;
  165. if (textureEntry != null)
  166. {
  167. // Get the prim's default texture. This will be used for faces which don't have their own texture
  168. if (textureEntry.DefaultTexture != null)
  169. RecordTextureEntryAssetUuids(textureEntry.DefaultTexture);
  170. if (textureEntry.FaceTextures != null)
  171. {
  172. // Loop through the rest of the texture faces (a non-null face means the face is different from DefaultTexture)
  173. foreach (Primitive.TextureEntryFace texture in textureEntry.FaceTextures)
  174. {
  175. if (texture != null)
  176. RecordTextureEntryAssetUuids(texture);
  177. }
  178. }
  179. }
  180. // If the prim is a sculpt then preserve this information too
  181. if (part.Shape.SculptTexture != UUID.Zero)
  182. GatheredUuids[part.Shape.SculptTexture] = (sbyte)AssetType.Texture;
  183. if (part.Shape.ProjectionTextureUUID != UUID.Zero)
  184. GatheredUuids[part.Shape.ProjectionTextureUUID] = (sbyte)AssetType.Texture;
  185. UUID collisionSound = part.CollisionSound;
  186. if ( collisionSound != UUID.Zero &&
  187. collisionSound != part.invalidCollisionSoundUUID)
  188. GatheredUuids[collisionSound] = (sbyte)AssetType.Sound;
  189. if (part.ParticleSystem.Length > 0)
  190. {
  191. try
  192. {
  193. Primitive.ParticleSystem ps = new Primitive.ParticleSystem(part.ParticleSystem, 0);
  194. if (ps.Texture != UUID.Zero)
  195. GatheredUuids[ps.Texture] = (sbyte)AssetType.Texture;
  196. }
  197. catch (Exception)
  198. {
  199. m_log.WarnFormat(
  200. "[UUID GATHERER]: Could not check particle system for part {0} {1} in object {2} {3} since it is corrupt. Continuing.",
  201. part.Name, part.UUID, sceneObject.Name, sceneObject.UUID);
  202. }
  203. }
  204. TaskInventoryDictionary taskDictionary = (TaskInventoryDictionary)part.TaskInventory.Clone();
  205. // Now analyze this prim's inventory items to preserve all the uuids that they reference
  206. foreach (TaskInventoryItem tii in taskDictionary.Values)
  207. {
  208. // m_log.DebugFormat(
  209. // "[ARCHIVER]: Analysing item {0} asset type {1} in {2} {3}",
  210. // tii.Name, tii.Type, part.Name, part.UUID);
  211. AddForInspection(tii.AssetID, (sbyte)tii.Type);
  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. // still needed to retrieve textures used as materials for any parts containing legacy materials stored in DynAttrs
  219. RecordMaterialsUuids(part);
  220. }
  221. catch (Exception e)
  222. {
  223. m_log.ErrorFormat("[UUID GATHERER]: Failed to get part - {0}", e);
  224. }
  225. }
  226. }
  227. /// <summary>
  228. /// Gathers the next set of assets returned by the next uuid to get from the asset service.
  229. /// </summary>
  230. /// <returns>false if gathering is already complete, true otherwise</returns>
  231. public bool GatherNext()
  232. {
  233. if (Complete)
  234. return false;
  235. UUID nextToInspect = m_assetUuidsToInspect.Dequeue();
  236. // m_log.DebugFormat("[UUID GATHERER]: Inspecting asset {0}", nextToInspect);
  237. GetAssetUuids(nextToInspect);
  238. return true;
  239. }
  240. /// <summary>
  241. /// Gathers all remaining asset UUIDS no matter how many calls are required to the asset service.
  242. /// </summary>
  243. /// <returns>false if gathering is already complete, true otherwise</returns>
  244. public bool GatherAll()
  245. {
  246. if (Complete)
  247. return false;
  248. while (GatherNext());
  249. return true;
  250. }
  251. /// <summary>
  252. /// Gather all the asset uuids associated with the asset referenced by a given uuid
  253. /// </summary>
  254. /// <remarks>
  255. /// This includes both those directly associated with
  256. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  257. /// within this object).
  258. /// This method assumes that the asset type associated with this asset in persistent storage is correct (which
  259. /// should always be the case). So with this method we always need to retrieve asset data even if the asset
  260. /// is of a type which is known not to reference any other assets
  261. /// </remarks>
  262. /// <param name="assetUuid">The uuid of the asset for which to gather referenced assets</param>
  263. private void GetAssetUuids(UUID assetUuid)
  264. {
  265. if(assetUuid == UUID.Zero)
  266. return;
  267. if(FailedUUIDs.Contains(assetUuid))
  268. {
  269. if(UncertainAssetsUUIDs.Contains(assetUuid))
  270. possibleNotAssetCount++;
  271. else
  272. ErrorCount++;
  273. return;
  274. }
  275. // avoid infinite loops
  276. if (GatheredUuids.ContainsKey(assetUuid))
  277. return;
  278. AssetBase assetBase;
  279. try
  280. {
  281. assetBase = GetAsset(assetUuid);
  282. }
  283. catch (Exception e)
  284. {
  285. m_log.ErrorFormat("[UUID GATHERER]: Failed to get asset {0} : {1}", assetUuid, e.Message);
  286. ErrorCount++;
  287. FailedUUIDs.Add(assetUuid);
  288. return;
  289. }
  290. if(assetBase == null)
  291. {
  292. // m_log.ErrorFormat("[UUID GATHERER]: asset {0} not found", assetUuid);
  293. FailedUUIDs.Add(assetUuid);
  294. if(UncertainAssetsUUIDs.Contains(assetUuid))
  295. possibleNotAssetCount++;
  296. else
  297. ErrorCount++;
  298. return;
  299. }
  300. if(UncertainAssetsUUIDs.Contains(assetUuid))
  301. UncertainAssetsUUIDs.Remove(assetUuid);
  302. sbyte assetType = assetBase.Type;
  303. if(assetBase.Data == null || assetBase.Data.Length == 0)
  304. {
  305. // m_log.ErrorFormat("[UUID GATHERER]: asset {0}, type {1} has no data", assetUuid, assetType);
  306. ErrorCount++;
  307. FailedUUIDs.Add(assetUuid);
  308. return;
  309. }
  310. GatheredUuids[assetUuid] = assetType;
  311. try
  312. {
  313. if ((sbyte)AssetType.Bodypart == assetType || (sbyte)AssetType.Clothing == assetType)
  314. {
  315. RecordWearableAssetUuids(assetBase);
  316. }
  317. else if ((sbyte)AssetType.Gesture == assetType)
  318. {
  319. RecordGestureAssetUuids(assetBase);
  320. }
  321. else if ((sbyte)AssetType.Notecard == assetType)
  322. {
  323. RecordTextEmbeddedAssetUuids(assetBase);
  324. }
  325. else if ((sbyte)AssetType.LSLText == assetType)
  326. {
  327. RecordTextEmbeddedAssetUuids(assetBase);
  328. }
  329. else if ((sbyte)OpenSimAssetType.Material == assetType)
  330. {
  331. RecordMaterialAssetUuids(assetBase);
  332. }
  333. else if ((sbyte)AssetType.Object == assetType)
  334. {
  335. RecordSceneObjectAssetUuids(assetBase);
  336. }
  337. }
  338. catch (Exception e)
  339. {
  340. m_log.ErrorFormat("[UUID GATHERER]: Failed to gather uuids for asset with id {0} type {1}: {2}", assetUuid, assetType, e.Message);
  341. GatheredUuids.Remove(assetUuid);
  342. ErrorCount++;
  343. FailedUUIDs.Add(assetUuid);
  344. }
  345. }
  346. private void AddForInspection(UUID assetUuid, sbyte assetType)
  347. {
  348. if(assetUuid == UUID.Zero)
  349. return;
  350. // Here, we want to collect uuids which require further asset fetches but mark the others as gathered
  351. if(FailedUUIDs.Contains(assetUuid))
  352. {
  353. if(UncertainAssetsUUIDs.Contains(assetUuid))
  354. possibleNotAssetCount++;
  355. else
  356. ErrorCount++;
  357. return;
  358. }
  359. if(GatheredUuids.ContainsKey(assetUuid))
  360. return;
  361. try
  362. {
  363. if ((sbyte)AssetType.Bodypart == assetType
  364. || (sbyte)AssetType.Clothing == assetType
  365. || (sbyte)AssetType.Gesture == assetType
  366. || (sbyte)AssetType.Notecard == assetType
  367. || (sbyte)AssetType.LSLText == assetType
  368. || (sbyte)OpenSimAssetType.Material == assetType
  369. || (sbyte)AssetType.Object == assetType)
  370. {
  371. AddForInspection(assetUuid);
  372. }
  373. else
  374. {
  375. GatheredUuids[assetUuid] = assetType;
  376. }
  377. }
  378. catch (Exception)
  379. {
  380. m_log.ErrorFormat(
  381. "[UUID GATHERER]: Failed to gather uuids for asset id {0}, type {1}",
  382. assetUuid, assetType);
  383. throw;
  384. }
  385. }
  386. /// <summary>
  387. /// Collect all the asset uuids found in one face of a Texture Entry.
  388. /// </summary>
  389. private void RecordTextureEntryAssetUuids(Primitive.TextureEntryFace texture)
  390. {
  391. GatheredUuids[texture.TextureID] = (sbyte)AssetType.Texture;
  392. if (texture.MaterialID != UUID.Zero)
  393. AddForInspection(texture.MaterialID);
  394. }
  395. /// <summary>
  396. /// Gather all of the texture asset UUIDs used to reference "Materials" such as normal and specular maps
  397. /// stored in legacy format in part.DynAttrs
  398. /// </summary>
  399. /// <param name="part"></param>
  400. private void RecordMaterialsUuids(SceneObjectPart part)
  401. {
  402. // scan thru the dynAttrs map of this part for any textures used as materials
  403. OSD osdMaterials = null;
  404. if(part.DynAttrs == null)
  405. return;
  406. lock (part.DynAttrs)
  407. {
  408. if (part.DynAttrs.ContainsStore("OpenSim", "Materials"))
  409. {
  410. OSDMap materialsStore = part.DynAttrs.GetStore("OpenSim", "Materials");
  411. if (materialsStore == null)
  412. return;
  413. materialsStore.TryGetValue("Materials", out osdMaterials);
  414. }
  415. if (osdMaterials != null)
  416. {
  417. //m_log.Info("[UUID Gatherer]: found Materials: " + OSDParser.SerializeJsonString(osd));
  418. if (osdMaterials is OSDArray)
  419. {
  420. OSDArray matsArr = osdMaterials as OSDArray;
  421. foreach (OSDMap matMap in matsArr)
  422. {
  423. try
  424. {
  425. if (matMap.ContainsKey("Material"))
  426. {
  427. OSDMap mat = matMap["Material"] as OSDMap;
  428. if (mat.ContainsKey("NormMap"))
  429. {
  430. UUID normalMapId = mat["NormMap"].AsUUID();
  431. if (normalMapId != UUID.Zero)
  432. {
  433. GatheredUuids[normalMapId] = (sbyte)AssetType.Texture;
  434. //m_log.Info("[UUID Gatherer]: found normal map ID: " + normalMapId.ToString());
  435. }
  436. }
  437. if (mat.ContainsKey("SpecMap"))
  438. {
  439. UUID specularMapId = mat["SpecMap"].AsUUID();
  440. if (specularMapId != UUID.Zero)
  441. {
  442. GatheredUuids[specularMapId] = (sbyte)AssetType.Texture;
  443. //m_log.Info("[UUID Gatherer]: found specular map ID: " + specularMapId.ToString());
  444. }
  445. }
  446. }
  447. }
  448. catch (Exception e)
  449. {
  450. m_log.Warn("[UUID Gatherer]: exception getting materials: " + e.Message);
  451. }
  452. }
  453. }
  454. }
  455. }
  456. }
  457. /// <summary>
  458. /// Get an asset synchronously, potentially using an asynchronous callback. If the
  459. /// asynchronous callback is used, we will wait for it to complete.
  460. /// </summary>
  461. /// <param name="uuid"></param>
  462. /// <returns></returns>
  463. protected virtual AssetBase GetAsset(UUID uuid)
  464. {
  465. return m_assetService.Get(uuid.ToString());
  466. }
  467. /// <summary>
  468. /// Record the asset uuids embedded within the given text (e.g. a script).
  469. /// </summary>
  470. /// <param name="textAsset"></param>
  471. private void RecordTextEmbeddedAssetUuids(AssetBase textAsset)
  472. {
  473. // m_log.DebugFormat("[ASSET GATHERER]: Getting assets for uuid references in asset {0}", embeddingAssetId);
  474. string text = Utils.BytesToString(textAsset.Data);
  475. // m_log.DebugFormat("[UUID GATHERER]: Text {0}", text);
  476. MatchCollection uuidMatches = Util.PermissiveUUIDPattern.Matches(text);
  477. // m_log.DebugFormat("[UUID GATHERER]: Found {0} matches in text", uuidMatches.Count);
  478. foreach (Match uuidMatch in uuidMatches)
  479. {
  480. UUID uuid = new UUID(uuidMatch.Value);
  481. if(uuid == UUID.Zero)
  482. continue;
  483. // m_log.DebugFormat("[UUID GATHERER]: Recording {0} in text", uuid);
  484. if(!UncertainAssetsUUIDs.Contains(uuid))
  485. UncertainAssetsUUIDs.Add(uuid);
  486. AddForInspection(uuid);
  487. }
  488. }
  489. /// <summary>
  490. /// Record the uuids referenced by the given wearable asset
  491. /// </summary>
  492. /// <param name="assetBase"></param>
  493. private void RecordWearableAssetUuids(AssetBase assetBase)
  494. {
  495. //m_log.Debug(new System.Text.ASCIIEncoding().GetString(bodypartAsset.Data));
  496. AssetWearable wearableAsset = new AssetBodypart(assetBase.FullID, assetBase.Data);
  497. wearableAsset.Decode();
  498. //m_log.DebugFormat(
  499. // "[ARCHIVER]: Wearable asset {0} references {1} assets", wearableAssetUuid, wearableAsset.Textures.Count);
  500. foreach (UUID uuid in wearableAsset.Textures.Values)
  501. GatheredUuids[uuid] = (sbyte)AssetType.Texture;
  502. }
  503. /// <summary>
  504. /// Get all the asset uuids associated with a given object. This includes both those directly associated with
  505. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  506. /// within this object).
  507. /// </summary>
  508. /// <param name="sceneObjectAsset"></param>
  509. private void RecordSceneObjectAssetUuids(AssetBase sceneObjectAsset)
  510. {
  511. string xml = Utils.BytesToString(sceneObjectAsset.Data);
  512. CoalescedSceneObjects coa;
  513. if (CoalescedSceneObjectsSerializer.TryFromXml(xml, out coa))
  514. {
  515. foreach (SceneObjectGroup sog in coa.Objects)
  516. AddForInspection(sog);
  517. }
  518. else
  519. {
  520. SceneObjectGroup sog = SceneObjectSerializer.FromOriginalXmlFormat(xml);
  521. if (null != sog)
  522. AddForInspection(sog);
  523. }
  524. }
  525. /// <summary>
  526. /// Get the asset uuid associated with a gesture
  527. /// </summary>
  528. /// <param name="gestureAsset"></param>
  529. private void RecordGestureAssetUuids(AssetBase gestureAsset)
  530. {
  531. using (MemoryStream ms = new MemoryStream(gestureAsset.Data))
  532. using (StreamReader sr = new StreamReader(ms))
  533. {
  534. sr.ReadLine(); // Unknown (Version?)
  535. sr.ReadLine(); // Unknown
  536. sr.ReadLine(); // Unknown
  537. sr.ReadLine(); // Name
  538. sr.ReadLine(); // Comment ?
  539. int count = Convert.ToInt32(sr.ReadLine()); // Item count
  540. for (int i = 0 ; i < count ; i++)
  541. {
  542. string type = sr.ReadLine();
  543. if (type == null)
  544. break;
  545. string name = sr.ReadLine();
  546. if (name == null)
  547. break;
  548. string id = sr.ReadLine();
  549. if (id == null)
  550. break;
  551. string unknown = sr.ReadLine();
  552. if (unknown == null)
  553. break;
  554. // If it can be parsed as a UUID, it is an asset ID
  555. UUID uuid;
  556. if (UUID.TryParse(id, out uuid))
  557. GatheredUuids[uuid] = (sbyte)AssetType.Animation; // the asset is either an Animation or a Sound, but this distinction isn't important
  558. }
  559. }
  560. }
  561. /// <summary>
  562. /// Get the asset uuid's referenced in a material.
  563. /// </summary>
  564. private void RecordMaterialAssetUuids(AssetBase materialAsset)
  565. {
  566. OSDMap mat;
  567. try
  568. {
  569. mat = (OSDMap)OSDParser.DeserializeLLSDXml(materialAsset.Data);
  570. }
  571. catch (Exception e)
  572. {
  573. m_log.WarnFormat("[Materials]: cannot decode material asset {0}: {1}", materialAsset.ID, e.Message);
  574. return;
  575. }
  576. UUID normMap = mat["NormMap"].AsUUID();
  577. if (normMap != UUID.Zero)
  578. GatheredUuids[normMap] = (sbyte)AssetType.Texture;
  579. UUID specMap = mat["SpecMap"].AsUUID();
  580. if (specMap != UUID.Zero)
  581. GatheredUuids[specMap] = (sbyte)AssetType.Texture;
  582. }
  583. }
  584. public class HGUuidGatherer : UuidGatherer
  585. {
  586. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  587. protected string m_assetServerURL;
  588. public HGUuidGatherer(IAssetService assetService, string assetServerURL)
  589. : this(assetService, assetServerURL, new Dictionary<UUID, sbyte>()) {}
  590. public HGUuidGatherer(IAssetService assetService, string assetServerURL, IDictionary<UUID, sbyte> collector)
  591. : base(assetService, collector)
  592. {
  593. m_assetServerURL = assetServerURL;
  594. if (!m_assetServerURL.EndsWith("/") && !m_assetServerURL.EndsWith("="))
  595. m_assetServerURL = m_assetServerURL + "/";
  596. }
  597. protected override AssetBase GetAsset(UUID uuid)
  598. {
  599. if (string.Empty == m_assetServerURL)
  600. return base.GetAsset(uuid);
  601. else
  602. return FetchAsset(uuid);
  603. }
  604. public AssetBase FetchAsset(UUID assetID)
  605. {
  606. // Test if it's already here
  607. AssetBase asset = m_assetService.Get(assetID.ToString());
  608. if (asset == null)
  609. {
  610. // It's not, so fetch it from abroad
  611. asset = m_assetService.Get(m_assetServerURL + assetID.ToString());
  612. if (asset != null)
  613. m_log.DebugFormat("[HGUUIDGatherer]: Copied asset {0} from {1} to local asset server", assetID, m_assetServerURL);
  614. else
  615. m_log.DebugFormat("[HGUUIDGatherer]: Failed to fetch asset {0} from {1}", assetID, m_assetServerURL);
  616. }
  617. //else
  618. // m_log.DebugFormat("[HGUUIDGatherer]: Asset {0} from {1} was already here", assetID, m_assetServerURL);
  619. return asset;
  620. }
  621. }
  622. }