UuidGatherer.cs 26 KB

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