UuidGatherer.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. lock (part.DynAttrs)
  405. {
  406. if (part.DynAttrs.ContainsStore("OpenSim", "Materials"))
  407. {
  408. OSDMap materialsStore = part.DynAttrs.GetStore("OpenSim", "Materials");
  409. if (materialsStore == null)
  410. return;
  411. materialsStore.TryGetValue("Materials", out osdMaterials);
  412. }
  413. if (osdMaterials != null)
  414. {
  415. //m_log.Info("[UUID Gatherer]: found Materials: " + OSDParser.SerializeJsonString(osd));
  416. if (osdMaterials is OSDArray)
  417. {
  418. OSDArray matsArr = osdMaterials as OSDArray;
  419. foreach (OSDMap matMap in matsArr)
  420. {
  421. try
  422. {
  423. if (matMap.ContainsKey("Material"))
  424. {
  425. OSDMap mat = matMap["Material"] as OSDMap;
  426. if (mat.ContainsKey("NormMap"))
  427. {
  428. UUID normalMapId = mat["NormMap"].AsUUID();
  429. if (normalMapId != UUID.Zero)
  430. {
  431. GatheredUuids[normalMapId] = (sbyte)AssetType.Texture;
  432. //m_log.Info("[UUID Gatherer]: found normal map ID: " + normalMapId.ToString());
  433. }
  434. }
  435. if (mat.ContainsKey("SpecMap"))
  436. {
  437. UUID specularMapId = mat["SpecMap"].AsUUID();
  438. if (specularMapId != UUID.Zero)
  439. {
  440. GatheredUuids[specularMapId] = (sbyte)AssetType.Texture;
  441. //m_log.Info("[UUID Gatherer]: found specular map ID: " + specularMapId.ToString());
  442. }
  443. }
  444. }
  445. }
  446. catch (Exception e)
  447. {
  448. m_log.Warn("[UUID Gatherer]: exception getting materials: " + e.Message);
  449. }
  450. }
  451. }
  452. }
  453. }
  454. }
  455. /// <summary>
  456. /// Get an asset synchronously, potentially using an asynchronous callback. If the
  457. /// asynchronous callback is used, we will wait for it to complete.
  458. /// </summary>
  459. /// <param name="uuid"></param>
  460. /// <returns></returns>
  461. protected virtual AssetBase GetAsset(UUID uuid)
  462. {
  463. return m_assetService.Get(uuid.ToString());
  464. }
  465. /// <summary>
  466. /// Record the asset uuids embedded within the given text (e.g. a script).
  467. /// </summary>
  468. /// <param name="textAsset"></param>
  469. private void RecordTextEmbeddedAssetUuids(AssetBase textAsset)
  470. {
  471. // m_log.DebugFormat("[ASSET GATHERER]: Getting assets for uuid references in asset {0}", embeddingAssetId);
  472. string text = Utils.BytesToString(textAsset.Data);
  473. // m_log.DebugFormat("[UUID GATHERER]: Text {0}", text);
  474. MatchCollection uuidMatches = Util.PermissiveUUIDPattern.Matches(text);
  475. // m_log.DebugFormat("[UUID GATHERER]: Found {0} matches in text", uuidMatches.Count);
  476. foreach (Match uuidMatch in uuidMatches)
  477. {
  478. UUID uuid = new UUID(uuidMatch.Value);
  479. if(uuid == UUID.Zero)
  480. continue;
  481. // m_log.DebugFormat("[UUID GATHERER]: Recording {0} in text", uuid);
  482. if(!UncertainAssetsUUIDs.Contains(uuid))
  483. UncertainAssetsUUIDs.Add(uuid);
  484. AddForInspection(uuid);
  485. }
  486. }
  487. /// <summary>
  488. /// Record the uuids referenced by the given wearable asset
  489. /// </summary>
  490. /// <param name="assetBase"></param>
  491. private void RecordWearableAssetUuids(AssetBase assetBase)
  492. {
  493. //m_log.Debug(new System.Text.ASCIIEncoding().GetString(bodypartAsset.Data));
  494. AssetWearable wearableAsset = new AssetBodypart(assetBase.FullID, assetBase.Data);
  495. wearableAsset.Decode();
  496. //m_log.DebugFormat(
  497. // "[ARCHIVER]: Wearable asset {0} references {1} assets", wearableAssetUuid, wearableAsset.Textures.Count);
  498. foreach (UUID uuid in wearableAsset.Textures.Values)
  499. GatheredUuids[uuid] = (sbyte)AssetType.Texture;
  500. }
  501. /// <summary>
  502. /// Get all the asset uuids associated with a given object. This includes both those directly associated with
  503. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  504. /// within this object).
  505. /// </summary>
  506. /// <param name="sceneObjectAsset"></param>
  507. private void RecordSceneObjectAssetUuids(AssetBase sceneObjectAsset)
  508. {
  509. string xml = Utils.BytesToString(sceneObjectAsset.Data);
  510. CoalescedSceneObjects coa;
  511. if (CoalescedSceneObjectsSerializer.TryFromXml(xml, out coa))
  512. {
  513. foreach (SceneObjectGroup sog in coa.Objects)
  514. AddForInspection(sog);
  515. }
  516. else
  517. {
  518. SceneObjectGroup sog = SceneObjectSerializer.FromOriginalXmlFormat(xml);
  519. if (null != sog)
  520. AddForInspection(sog);
  521. }
  522. }
  523. /// <summary>
  524. /// Get the asset uuid associated with a gesture
  525. /// </summary>
  526. /// <param name="gestureAsset"></param>
  527. private void RecordGestureAssetUuids(AssetBase gestureAsset)
  528. {
  529. using (MemoryStream ms = new MemoryStream(gestureAsset.Data))
  530. using (StreamReader sr = new StreamReader(ms))
  531. {
  532. sr.ReadLine(); // Unknown (Version?)
  533. sr.ReadLine(); // Unknown
  534. sr.ReadLine(); // Unknown
  535. sr.ReadLine(); // Name
  536. sr.ReadLine(); // Comment ?
  537. int count = Convert.ToInt32(sr.ReadLine()); // Item count
  538. for (int i = 0 ; i < count ; i++)
  539. {
  540. string type = sr.ReadLine();
  541. if (type == null)
  542. break;
  543. string name = sr.ReadLine();
  544. if (name == null)
  545. break;
  546. string id = sr.ReadLine();
  547. if (id == null)
  548. break;
  549. string unknown = sr.ReadLine();
  550. if (unknown == null)
  551. break;
  552. // If it can be parsed as a UUID, it is an asset ID
  553. UUID uuid;
  554. if (UUID.TryParse(id, out uuid))
  555. GatheredUuids[uuid] = (sbyte)AssetType.Animation; // the asset is either an Animation or a Sound, but this distinction isn't important
  556. }
  557. }
  558. }
  559. /// <summary>
  560. /// Get the asset uuid's referenced in a material.
  561. /// </summary>
  562. private void RecordMaterialAssetUuids(AssetBase materialAsset)
  563. {
  564. OSDMap mat;
  565. try
  566. {
  567. mat = (OSDMap)OSDParser.DeserializeLLSDXml(materialAsset.Data);
  568. }
  569. catch (Exception e)
  570. {
  571. m_log.WarnFormat("[Materials]: cannot decode material asset {0}: {1}", materialAsset.ID, e.Message);
  572. return;
  573. }
  574. UUID normMap = mat["NormMap"].AsUUID();
  575. if (normMap != UUID.Zero)
  576. GatheredUuids[normMap] = (sbyte)AssetType.Texture;
  577. UUID specMap = mat["SpecMap"].AsUUID();
  578. if (specMap != UUID.Zero)
  579. GatheredUuids[specMap] = (sbyte)AssetType.Texture;
  580. }
  581. }
  582. public class HGUuidGatherer : UuidGatherer
  583. {
  584. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  585. protected string m_assetServerURL;
  586. public HGUuidGatherer(IAssetService assetService, string assetServerURL)
  587. : this(assetService, assetServerURL, new Dictionary<UUID, sbyte>()) {}
  588. public HGUuidGatherer(IAssetService assetService, string assetServerURL, IDictionary<UUID, sbyte> collector)
  589. : base(assetService, collector)
  590. {
  591. m_assetServerURL = assetServerURL;
  592. if (!m_assetServerURL.EndsWith("/") && !m_assetServerURL.EndsWith("="))
  593. m_assetServerURL = m_assetServerURL + "/";
  594. }
  595. protected override AssetBase GetAsset(UUID uuid)
  596. {
  597. if (string.Empty == m_assetServerURL)
  598. return base.GetAsset(uuid);
  599. else
  600. return FetchAsset(uuid);
  601. }
  602. public AssetBase FetchAsset(UUID assetID)
  603. {
  604. // Test if it's already here
  605. AssetBase asset = m_assetService.Get(assetID.ToString());
  606. if (asset == null)
  607. {
  608. // It's not, so fetch it from abroad
  609. asset = m_assetService.Get(m_assetServerURL + assetID.ToString());
  610. if (asset != null)
  611. m_log.DebugFormat("[HGUUIDGatherer]: Copied asset {0} from {1} to local asset server", assetID, m_assetServerURL);
  612. else
  613. m_log.DebugFormat("[HGUUIDGatherer]: Failed to fetch asset {0} from {1}", assetID, m_assetServerURL);
  614. }
  615. //else
  616. // m_log.DebugFormat("[HGUUIDGatherer]: Asset {0} from {1} was already here", assetID, m_assetServerURL);
  617. return asset;
  618. }
  619. }
  620. }