UuidGatherer.cs 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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. protected IAssetService m_assetService;
  56. // /// <summary>
  57. // /// Used as a temporary store of an asset which represents an object. This can be a null if no appropriate
  58. // /// asset was found by the asset service.
  59. // /// </summary>
  60. // private AssetBase m_requestedObjectAsset;
  61. //
  62. // /// <summary>
  63. // /// Signal whether we are currently waiting for the asset service to deliver an asset.
  64. // /// </summary>
  65. // private bool m_waitingForObjectAsset;
  66. public UuidGatherer(IAssetService assetService)
  67. {
  68. m_assetService = assetService;
  69. }
  70. /// <summary>
  71. /// Gather all the asset uuids associated with the asset referenced by a given uuid
  72. /// </summary>
  73. /// <remarks>
  74. /// This includes both those directly associated with
  75. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  76. /// within this object).
  77. /// This method assumes that the asset type associated with this asset in persistent storage is correct (which
  78. /// should always be the case). So with this method we always need to retrieve asset data even if the asset
  79. /// is of a type which is known not to reference any other assets
  80. /// </remarks>
  81. /// <param name="assetUuid">The uuid of the asset for which to gather referenced assets</param>
  82. /// <param name="assetUuids">The assets gathered</param>
  83. public void GatherAssetUuids(UUID assetUuid, IDictionary<UUID, sbyte> assetUuids)
  84. {
  85. // avoid infinite loops
  86. if (assetUuids.ContainsKey(assetUuid))
  87. return;
  88. try
  89. {
  90. AssetBase assetBase = GetAsset(assetUuid);
  91. if (null != assetBase)
  92. {
  93. sbyte assetType = assetBase.Type;
  94. assetUuids[assetUuid] = assetType;
  95. if ((sbyte)AssetType.Bodypart == assetType || (sbyte)AssetType.Clothing == assetType)
  96. {
  97. GetWearableAssetUuids(assetBase, assetUuids);
  98. }
  99. else if ((sbyte)AssetType.Gesture == assetType)
  100. {
  101. GetGestureAssetUuids(assetBase, assetUuids);
  102. }
  103. else if ((sbyte)AssetType.Notecard == assetType)
  104. {
  105. GetTextEmbeddedAssetUuids(assetBase, assetUuids);
  106. }
  107. else if ((sbyte)AssetType.LSLText == assetType)
  108. {
  109. GetTextEmbeddedAssetUuids(assetBase, assetUuids);
  110. }
  111. else if ((sbyte)OpenSimAssetType.Material == assetType)
  112. {
  113. GetMaterialAssetUuids(assetBase, assetUuids);
  114. }
  115. else if ((sbyte)AssetType.Object == assetType)
  116. {
  117. GetSceneObjectAssetUuids(assetBase, assetUuids);
  118. }
  119. }
  120. }
  121. catch (Exception)
  122. {
  123. m_log.ErrorFormat("[UUID GATHERER]: Failed to gather uuids for asset id {0}", assetUuid);
  124. throw;
  125. }
  126. }
  127. /// <summary>
  128. /// Gather all the asset uuids associated with the asset referenced by a given uuid
  129. /// </summary>
  130. /// <remarks>
  131. /// This includes both those directly associated with
  132. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  133. /// within this object).
  134. /// </remarks>
  135. /// <param name="assetUuid">The uuid of the asset for which to gather referenced assets</param>
  136. /// <param name="assetType">The type of the asset for the uuid given</param>
  137. /// <param name="assetUuids">The assets gathered</param>
  138. public void GatherAssetUuids(UUID assetUuid, sbyte assetType, IDictionary<UUID, sbyte> assetUuids)
  139. {
  140. // avoid infinite loops
  141. if (assetUuids.ContainsKey(assetUuid))
  142. return;
  143. try
  144. {
  145. assetUuids[assetUuid] = assetType;
  146. if ((sbyte)AssetType.Bodypart == assetType || (sbyte)AssetType.Clothing == assetType)
  147. {
  148. GetWearableAssetUuids(assetUuid, assetUuids);
  149. }
  150. else if ((sbyte)AssetType.Gesture == assetType)
  151. {
  152. GetGestureAssetUuids(assetUuid, assetUuids);
  153. }
  154. else if ((sbyte)AssetType.Notecard == assetType)
  155. {
  156. GetTextEmbeddedAssetUuids(assetUuid, assetUuids);
  157. }
  158. else if ((sbyte)AssetType.LSLText == assetType)
  159. {
  160. GetTextEmbeddedAssetUuids(assetUuid, assetUuids);
  161. }
  162. else if ((sbyte)OpenSimAssetType.Material == assetType)
  163. {
  164. GetMaterialAssetUuids(assetUuid, assetUuids);
  165. }
  166. else if ((sbyte)AssetType.Object == assetType)
  167. {
  168. GetSceneObjectAssetUuids(assetUuid, assetUuids);
  169. }
  170. }
  171. catch (Exception)
  172. {
  173. m_log.ErrorFormat(
  174. "[UUID GATHERER]: Failed to gather uuids for asset id {0}, type {1}",
  175. assetUuid, assetType);
  176. throw;
  177. }
  178. }
  179. /// <summary>
  180. /// Gather all the asset uuids associated with a given object.
  181. /// </summary>
  182. /// <remarks>
  183. /// This includes both those directly associated with
  184. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  185. /// within this object).
  186. /// </remarks>
  187. /// <param name="sceneObject">The scene object for which to gather assets</param>
  188. /// <param name="assetUuids">
  189. /// A dictionary which is populated with the asset UUIDs gathered and the type of that asset.
  190. /// For assets where the type is not clear (e.g. UUIDs extracted from LSL and notecards), the type is Unknown.
  191. /// </param>
  192. public void GatherAssetUuids(SceneObjectGroup sceneObject, IDictionary<UUID, sbyte> assetUuids)
  193. {
  194. // m_log.DebugFormat(
  195. // "[ASSET GATHERER]: Getting assets for object {0}, {1}", sceneObject.Name, sceneObject.UUID);
  196. SceneObjectPart[] parts = sceneObject.Parts;
  197. for (int i = 0; i < parts.Length; i++)
  198. {
  199. SceneObjectPart part = parts[i];
  200. // m_log.DebugFormat(
  201. // "[ARCHIVER]: Getting part {0}, {1} for object {2}", part.Name, part.UUID, sceneObject.UUID);
  202. try
  203. {
  204. Primitive.TextureEntry textureEntry = part.Shape.Textures;
  205. if (textureEntry != null)
  206. {
  207. // Get the prim's default texture. This will be used for faces which don't have their own texture
  208. if (textureEntry.DefaultTexture != null)
  209. GatherTextureEntryAssets(textureEntry.DefaultTexture, assetUuids);
  210. if (textureEntry.FaceTextures != null)
  211. {
  212. // Loop through the rest of the texture faces (a non-null face means the face is different from DefaultTexture)
  213. foreach (Primitive.TextureEntryFace texture in textureEntry.FaceTextures)
  214. {
  215. if (texture != null)
  216. GatherTextureEntryAssets(texture, assetUuids);
  217. }
  218. }
  219. }
  220. // If the prim is a sculpt then preserve this information too
  221. if (part.Shape.SculptTexture != UUID.Zero)
  222. assetUuids[part.Shape.SculptTexture] = (sbyte)AssetType.Texture;
  223. if (part.Shape.ProjectionTextureUUID != UUID.Zero)
  224. assetUuids[part.Shape.ProjectionTextureUUID] = (sbyte)AssetType.Texture;
  225. if (part.CollisionSound != UUID.Zero)
  226. assetUuids[part.CollisionSound] = (sbyte)AssetType.Sound;
  227. if (part.ParticleSystem.Length > 0)
  228. {
  229. try
  230. {
  231. Primitive.ParticleSystem ps = new Primitive.ParticleSystem(part.ParticleSystem, 0);
  232. if (ps.Texture != UUID.Zero)
  233. assetUuids[ps.Texture] = (sbyte)AssetType.Texture;
  234. }
  235. catch (Exception)
  236. {
  237. m_log.WarnFormat(
  238. "[UUID GATHERER]: Could not check particle system for part {0} {1} in object {2} {3} since it is corrupt. Continuing.",
  239. part.Name, part.UUID, sceneObject.Name, sceneObject.UUID);
  240. }
  241. }
  242. TaskInventoryDictionary taskDictionary = (TaskInventoryDictionary)part.TaskInventory.Clone();
  243. // Now analyze this prim's inventory items to preserve all the uuids that they reference
  244. foreach (TaskInventoryItem tii in taskDictionary.Values)
  245. {
  246. // m_log.DebugFormat(
  247. // "[ARCHIVER]: Analysing item {0} asset type {1} in {2} {3}",
  248. // tii.Name, tii.Type, part.Name, part.UUID);
  249. if (!assetUuids.ContainsKey(tii.AssetID))
  250. GatherAssetUuids(tii.AssetID, (sbyte)tii.Type, assetUuids);
  251. }
  252. // FIXME: We need to make gathering modular but we cannot yet, since gatherers are not guaranteed
  253. // to be called with scene objects that are in a scene (e.g. in the case of hg asset mapping and
  254. // inventory transfer. There needs to be a way for a module to register a method without assuming a
  255. // Scene.EventManager is present.
  256. // part.ParentGroup.Scene.EventManager.TriggerGatherUuids(part, assetUuids);
  257. // still needed to retrieve textures used as materials for any parts containing legacy materials stored in DynAttrs
  258. GatherMaterialsUuids(part, assetUuids);
  259. }
  260. catch (Exception e)
  261. {
  262. m_log.ErrorFormat("[UUID GATHERER]: Failed to get part - {0}", e);
  263. m_log.DebugFormat(
  264. "[UUID GATHERER]: Texture entry length for prim was {0} (min is 46)",
  265. part.Shape.TextureEntry.Length);
  266. }
  267. }
  268. }
  269. /// <summary>
  270. /// Gather all the asset uuids found in one face of a Texture Entry.
  271. /// </summary>
  272. private void GatherTextureEntryAssets(Primitive.TextureEntryFace texture, IDictionary<UUID, sbyte> assetUuids)
  273. {
  274. assetUuids[texture.TextureID] = (sbyte)AssetType.Texture;
  275. if (texture.MaterialID != UUID.Zero)
  276. {
  277. GatherAssetUuids(texture.MaterialID, (sbyte)OpenSimAssetType.Material, assetUuids);
  278. }
  279. }
  280. /// <summary>
  281. /// Gather all of the texture asset UUIDs used to reference "Materials" such as normal and specular maps
  282. /// stored in legacy format in part.DynAttrs
  283. /// </summary>
  284. /// <param name="part"></param>
  285. /// <param name="assetUuids"></param>
  286. //public void GatherMaterialsUuids(SceneObjectPart part, IDictionary<UUID, AssetType> assetUuids)
  287. public void GatherMaterialsUuids(SceneObjectPart part, IDictionary<UUID, sbyte> assetUuids)
  288. {
  289. // scan thru the dynAttrs map of this part for any textures used as materials
  290. OSD osdMaterials = null;
  291. lock (part.DynAttrs)
  292. {
  293. if (part.DynAttrs.ContainsStore("OpenSim", "Materials"))
  294. {
  295. OSDMap materialsStore = part.DynAttrs.GetStore("OpenSim", "Materials");
  296. if (materialsStore == null)
  297. return;
  298. materialsStore.TryGetValue("Materials", out osdMaterials);
  299. }
  300. if (osdMaterials != null)
  301. {
  302. //m_log.Info("[UUID Gatherer]: found Materials: " + OSDParser.SerializeJsonString(osd));
  303. if (osdMaterials is OSDArray)
  304. {
  305. OSDArray matsArr = osdMaterials as OSDArray;
  306. foreach (OSDMap matMap in matsArr)
  307. {
  308. try
  309. {
  310. if (matMap.ContainsKey("Material"))
  311. {
  312. OSDMap mat = matMap["Material"] as OSDMap;
  313. if (mat.ContainsKey("NormMap"))
  314. {
  315. UUID normalMapId = mat["NormMap"].AsUUID();
  316. if (normalMapId != UUID.Zero)
  317. {
  318. assetUuids[normalMapId] = (sbyte)AssetType.Texture;
  319. //m_log.Info("[UUID Gatherer]: found normal map ID: " + normalMapId.ToString());
  320. }
  321. }
  322. if (mat.ContainsKey("SpecMap"))
  323. {
  324. UUID specularMapId = mat["SpecMap"].AsUUID();
  325. if (specularMapId != UUID.Zero)
  326. {
  327. assetUuids[specularMapId] = (sbyte)AssetType.Texture;
  328. //m_log.Info("[UUID Gatherer]: found specular map ID: " + specularMapId.ToString());
  329. }
  330. }
  331. }
  332. }
  333. catch (Exception e)
  334. {
  335. m_log.Warn("[UUID Gatherer]: exception getting materials: " + e.Message);
  336. }
  337. }
  338. }
  339. }
  340. }
  341. }
  342. /// <summary>
  343. /// Get an asset synchronously, potentially using an asynchronous callback. If the
  344. /// asynchronous callback is used, we will wait for it to complete.
  345. /// </summary>
  346. /// <param name="uuid"></param>
  347. /// <returns></returns>
  348. protected virtual AssetBase GetAsset(UUID uuid)
  349. {
  350. return m_assetService.Get(uuid.ToString());
  351. // XXX: Switching to do this synchronously where the call was async before but we always waited for it
  352. // to complete anyway!
  353. // m_waitingForObjectAsset = true;
  354. // m_assetCache.Get(uuid.ToString(), this, AssetReceived);
  355. //
  356. // // The asset cache callback can either
  357. // //
  358. // // 1. Complete on the same thread (if the asset is already in the cache) or
  359. // // 2. Come in via a different thread (if we need to go fetch it).
  360. // //
  361. // // The code below handles both these alternatives.
  362. // lock (this)
  363. // {
  364. // if (m_waitingForObjectAsset)
  365. // {
  366. // Monitor.Wait(this);
  367. // m_waitingForObjectAsset = false;
  368. // }
  369. // }
  370. //
  371. // return m_requestedObjectAsset;
  372. }
  373. /// <summary>
  374. /// Record the asset uuids embedded within the given text (e.g. a script).
  375. /// </summary>
  376. /// <param name="textAssetUuid"></param>
  377. /// <param name="assetUuids">Dictionary in which to record the references</param>
  378. private void GetTextEmbeddedAssetUuids(UUID textAssetUuid, IDictionary<UUID, sbyte> assetUuids)
  379. {
  380. // m_log.DebugFormat("[ASSET GATHERER]: Getting assets for uuid references in asset {0}", embeddingAssetId);
  381. AssetBase textAsset = GetAsset(textAssetUuid);
  382. if (null != textAsset)
  383. GetTextEmbeddedAssetUuids(textAsset, assetUuids);
  384. }
  385. /// <summary>
  386. /// Record the asset uuids embedded within the given text (e.g. a script).
  387. /// </summary>
  388. /// <param name="textAsset"></param>
  389. /// <param name="assetUuids">Dictionary in which to record the references</param>
  390. private void GetTextEmbeddedAssetUuids(AssetBase textAsset, IDictionary<UUID, sbyte> assetUuids)
  391. {
  392. // m_log.DebugFormat("[ASSET GATHERER]: Getting assets for uuid references in asset {0}", embeddingAssetId);
  393. string script = Utils.BytesToString(textAsset.Data);
  394. // m_log.DebugFormat("[ARCHIVER]: Script {0}", script);
  395. MatchCollection uuidMatches = Util.PermissiveUUIDPattern.Matches(script);
  396. // m_log.DebugFormat("[ARCHIVER]: Found {0} matches in text", uuidMatches.Count);
  397. foreach (Match uuidMatch in uuidMatches)
  398. {
  399. UUID uuid = new UUID(uuidMatch.Value);
  400. // m_log.DebugFormat("[ARCHIVER]: Recording {0} in text", uuid);
  401. // Embedded asset references (if not false positives) could be for many types of asset, so we will
  402. // label these as unknown.
  403. assetUuids[uuid] = (sbyte)AssetType.Unknown;
  404. }
  405. }
  406. /// <summary>
  407. /// Record the uuids referenced by the given wearable asset
  408. /// </summary>
  409. /// <param name="wearableAssetUuid"></param>
  410. /// <param name="assetUuids">Dictionary in which to record the references</param>
  411. private void GetWearableAssetUuids(UUID wearableAssetUuid, IDictionary<UUID, sbyte> assetUuids)
  412. {
  413. AssetBase assetBase = GetAsset(wearableAssetUuid);
  414. if (null != assetBase)
  415. GetWearableAssetUuids(assetBase, assetUuids);
  416. }
  417. /// <summary>
  418. /// Record the uuids referenced by the given wearable asset
  419. /// </summary>
  420. /// <param name="assetBase"></param>
  421. /// <param name="assetUuids">Dictionary in which to record the references</param>
  422. private void GetWearableAssetUuids(AssetBase assetBase, IDictionary<UUID, sbyte> assetUuids)
  423. {
  424. //m_log.Debug(new System.Text.ASCIIEncoding().GetString(bodypartAsset.Data));
  425. AssetWearable wearableAsset = new AssetBodypart(assetBase.FullID, assetBase.Data);
  426. wearableAsset.Decode();
  427. //m_log.DebugFormat(
  428. // "[ARCHIVER]: Wearable asset {0} references {1} assets", wearableAssetUuid, wearableAsset.Textures.Count);
  429. foreach (UUID uuid in wearableAsset.Textures.Values)
  430. {
  431. assetUuids[uuid] = (sbyte)AssetType.Texture;
  432. }
  433. }
  434. /// <summary>
  435. /// Get all the asset uuids associated with a given object. This includes both those directly associated with
  436. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  437. /// within this object).
  438. /// </summary>
  439. /// <param name="sceneObject"></param>
  440. /// <param name="assetUuids"></param>
  441. private void GetSceneObjectAssetUuids(UUID sceneObjectUuid, IDictionary<UUID, sbyte> assetUuids)
  442. {
  443. AssetBase sceneObjectAsset = GetAsset(sceneObjectUuid);
  444. if (null != sceneObjectAsset)
  445. GetSceneObjectAssetUuids(sceneObjectAsset, assetUuids);
  446. }
  447. /// <summary>
  448. /// Get all the asset uuids associated with a given object. This includes both those directly associated with
  449. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  450. /// within this object).
  451. /// </summary>
  452. /// <param name="sceneObjectAsset"></param>
  453. /// <param name="assetUuids"></param>
  454. private void GetSceneObjectAssetUuids(AssetBase sceneObjectAsset, IDictionary<UUID, sbyte> assetUuids)
  455. {
  456. string xml = Utils.BytesToString(sceneObjectAsset.Data);
  457. CoalescedSceneObjects coa;
  458. if (CoalescedSceneObjectsSerializer.TryFromXml(xml, out coa))
  459. {
  460. foreach (SceneObjectGroup sog in coa.Objects)
  461. GatherAssetUuids(sog, assetUuids);
  462. }
  463. else
  464. {
  465. SceneObjectGroup sog = SceneObjectSerializer.FromOriginalXmlFormat(xml);
  466. if (null != sog)
  467. GatherAssetUuids(sog, assetUuids);
  468. }
  469. }
  470. /// <summary>
  471. /// Get the asset uuid associated with a gesture
  472. /// </summary>
  473. /// <param name="gestureUuid"></param>
  474. /// <param name="assetUuids"></param>
  475. private void GetGestureAssetUuids(UUID gestureUuid, IDictionary<UUID, sbyte> assetUuids)
  476. {
  477. AssetBase gestureAsset = GetAsset(gestureUuid);
  478. if (null == gestureAsset)
  479. return;
  480. GetGestureAssetUuids(gestureAsset, assetUuids);
  481. }
  482. /// <summary>
  483. /// Get the asset uuid associated with a gesture
  484. /// </summary>
  485. /// <param name="gestureAsset"></param>
  486. /// <param name="assetUuids"></param>
  487. private void GetGestureAssetUuids(AssetBase gestureAsset, IDictionary<UUID, sbyte> assetUuids)
  488. {
  489. using (MemoryStream ms = new MemoryStream(gestureAsset.Data))
  490. using (StreamReader sr = new StreamReader(ms))
  491. {
  492. sr.ReadLine(); // Unknown (Version?)
  493. sr.ReadLine(); // Unknown
  494. sr.ReadLine(); // Unknown
  495. sr.ReadLine(); // Name
  496. sr.ReadLine(); // Comment ?
  497. int count = Convert.ToInt32(sr.ReadLine()); // Item count
  498. for (int i = 0 ; i < count ; i++)
  499. {
  500. string type = sr.ReadLine();
  501. if (type == null)
  502. break;
  503. string name = sr.ReadLine();
  504. if (name == null)
  505. break;
  506. string id = sr.ReadLine();
  507. if (id == null)
  508. break;
  509. string unknown = sr.ReadLine();
  510. if (unknown == null)
  511. break;
  512. // If it can be parsed as a UUID, it is an asset ID
  513. UUID uuid;
  514. if (UUID.TryParse(id, out uuid))
  515. assetUuids[uuid] = (sbyte)AssetType.Animation; // the asset is either an Animation or a Sound, but this distinction isn't important
  516. }
  517. }
  518. }
  519. /// <summary>
  520. /// Get the asset uuid's referenced in a material.
  521. /// </summary>
  522. private void GetMaterialAssetUuids(UUID materialUuid, IDictionary<UUID, sbyte> assetUuids)
  523. {
  524. AssetBase assetBase = GetAsset(materialUuid);
  525. if (null == assetBase)
  526. return;
  527. GetMaterialAssetUuids(assetBase, assetUuids);
  528. }
  529. /// <summary>
  530. /// Get the asset uuid's referenced in a material.
  531. /// </summary>
  532. private void GetMaterialAssetUuids(AssetBase materialAsset, IDictionary<UUID, sbyte> assetUuids)
  533. {
  534. OSDMap mat = (OSDMap)OSDParser.DeserializeLLSDXml(materialAsset.Data);
  535. UUID normMap = mat["NormMap"].AsUUID();
  536. if (normMap != UUID.Zero)
  537. assetUuids[normMap] = (sbyte)AssetType.Texture;
  538. UUID specMap = mat["SpecMap"].AsUUID();
  539. if (specMap != UUID.Zero)
  540. assetUuids[specMap] = (sbyte)AssetType.Texture;
  541. }
  542. }
  543. public class HGUuidGatherer : UuidGatherer
  544. {
  545. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  546. protected string m_assetServerURL;
  547. public HGUuidGatherer(IAssetService assetService, string assetServerURL)
  548. : base(assetService)
  549. {
  550. m_assetServerURL = assetServerURL;
  551. if (!m_assetServerURL.EndsWith("/") && !m_assetServerURL.EndsWith("="))
  552. m_assetServerURL = m_assetServerURL + "/";
  553. }
  554. protected override AssetBase GetAsset(UUID uuid)
  555. {
  556. if (string.Empty == m_assetServerURL)
  557. return base.GetAsset(uuid);
  558. else
  559. return FetchAsset(uuid);
  560. }
  561. public AssetBase FetchAsset(UUID assetID)
  562. {
  563. // Test if it's already here
  564. AssetBase asset = m_assetService.Get(assetID.ToString());
  565. if (asset == null)
  566. {
  567. // It's not, so fetch it from abroad
  568. asset = m_assetService.Get(m_assetServerURL + assetID.ToString());
  569. if (asset != null)
  570. m_log.DebugFormat("[HGUUIDGatherer]: Copied asset {0} from {1} to local asset server", assetID, m_assetServerURL);
  571. else
  572. m_log.DebugFormat("[HGUUIDGatherer]: Failed to fetch asset {0} from {1}", assetID, m_assetServerURL);
  573. }
  574. //else
  575. // m_log.DebugFormat("[HGUUIDGatherer]: Asset {0} from {1} was already here", assetID, m_assetServerURL);
  576. return asset;
  577. }
  578. }
  579. public class IteratingUuidGatherer
  580. {
  581. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  582. /// <summary>
  583. /// Is gathering complete?
  584. /// </summary>
  585. public bool Complete { get { return m_assetUuidsToInspect.Count <= 0; } }
  586. /// <summary>
  587. /// Gets the next UUID to inspect.
  588. /// </summary>
  589. /// <value>If there is no next UUID then returns null</value>
  590. public UUID? NextUuidToInspect
  591. {
  592. get
  593. {
  594. if (Complete)
  595. return null;
  596. else
  597. return m_assetUuidsToInspect.Peek();
  598. }
  599. }
  600. protected IAssetService m_assetService;
  601. protected IDictionary<UUID, sbyte> m_gatheredAssetUuids;
  602. protected Queue<UUID> m_assetUuidsToInspect;
  603. public IteratingUuidGatherer(IAssetService assetService)
  604. {
  605. m_assetService = assetService;
  606. m_gatheredAssetUuids = new Dictionary<UUID, sbyte>();
  607. // FIXME: Not efficient for searching, can improve.
  608. m_assetUuidsToInspect = new Queue<UUID>();
  609. }
  610. public IDictionary<UUID, sbyte> GetGatheredUuids()
  611. {
  612. return new Dictionary<UUID, sbyte>(m_gatheredAssetUuids);
  613. }
  614. public bool AddAssetUuidToInspect(UUID uuid)
  615. {
  616. if (m_assetUuidsToInspect.Contains(uuid))
  617. return false;
  618. m_assetUuidsToInspect.Enqueue(uuid);
  619. return true;
  620. }
  621. /// <summary>
  622. /// Gathers the next set of assets returned by the next uuid to get from the asset service.
  623. /// </summary>
  624. /// <returns>false if gathering is already complete, true otherwise</returns>
  625. public bool GatherNext()
  626. {
  627. if (Complete)
  628. return false;
  629. GetAssetUuids(m_assetUuidsToInspect.Dequeue());
  630. return true;
  631. }
  632. /// <summary>
  633. /// Gathers all remaining asset UUIDS no matter how many calls are required to the asset service.
  634. /// </summary>
  635. /// <returns>false if gathering is already complete, true otherwise</returns>
  636. public bool GatherAll()
  637. {
  638. if (Complete)
  639. return false;
  640. while (GatherNext());
  641. return true;
  642. }
  643. /// <summary>
  644. /// Gather all the asset uuids associated with the asset referenced by a given uuid
  645. /// </summary>
  646. /// <remarks>
  647. /// This includes both those directly associated with
  648. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  649. /// within this object).
  650. /// This method assumes that the asset type associated with this asset in persistent storage is correct (which
  651. /// should always be the case). So with this method we always need to retrieve asset data even if the asset
  652. /// is of a type which is known not to reference any other assets
  653. /// </remarks>
  654. /// <param name="assetUuid">The uuid of the asset for which to gather referenced assets</param>
  655. private void GetAssetUuids(UUID assetUuid)
  656. {
  657. // avoid infinite loops
  658. if (m_gatheredAssetUuids.ContainsKey(assetUuid))
  659. return;
  660. try
  661. {
  662. AssetBase assetBase = GetAsset(assetUuid);
  663. if (null != assetBase)
  664. {
  665. sbyte assetType = assetBase.Type;
  666. m_gatheredAssetUuids[assetUuid] = assetType;
  667. if ((sbyte)AssetType.Bodypart == assetType || (sbyte)AssetType.Clothing == assetType)
  668. {
  669. RecordWearableAssetUuids(assetBase);
  670. }
  671. else if ((sbyte)AssetType.Gesture == assetType)
  672. {
  673. RecordGestureAssetUuids(assetBase);
  674. }
  675. else if ((sbyte)AssetType.Notecard == assetType)
  676. {
  677. RecordTextEmbeddedAssetUuids(assetBase);
  678. }
  679. else if ((sbyte)AssetType.LSLText == assetType)
  680. {
  681. RecordTextEmbeddedAssetUuids(assetBase);
  682. }
  683. else if ((sbyte)OpenSimAssetType.Material == assetType)
  684. {
  685. RecordMaterialAssetUuids(assetBase);
  686. }
  687. else if ((sbyte)AssetType.Object == assetType)
  688. {
  689. RecordSceneObjectAssetUuids(assetBase);
  690. }
  691. }
  692. }
  693. catch (Exception)
  694. {
  695. m_log.ErrorFormat("[UUID GATHERER]: Failed to gather uuids for asset id {0}", assetUuid);
  696. throw;
  697. }
  698. }
  699. private void RecordAssetUuids(UUID assetUuid, sbyte assetType)
  700. {
  701. // Here, we want to collect uuids which require further asset fetches but mark the others as gathered
  702. try
  703. {
  704. m_gatheredAssetUuids[assetUuid] = assetType;
  705. if ((sbyte)AssetType.Bodypart == assetType || (sbyte)AssetType.Clothing == assetType)
  706. {
  707. AddAssetUuidToInspect(assetUuid);
  708. }
  709. else if ((sbyte)AssetType.Gesture == assetType)
  710. {
  711. AddAssetUuidToInspect(assetUuid);
  712. }
  713. else if ((sbyte)AssetType.Notecard == assetType)
  714. {
  715. AddAssetUuidToInspect(assetUuid);
  716. }
  717. else if ((sbyte)AssetType.LSLText == assetType)
  718. {
  719. AddAssetUuidToInspect(assetUuid);
  720. }
  721. else if ((sbyte)OpenSimAssetType.Material == assetType)
  722. {
  723. AddAssetUuidToInspect(assetUuid);
  724. }
  725. else if ((sbyte)AssetType.Object == assetType)
  726. {
  727. AddAssetUuidToInspect(assetUuid);
  728. }
  729. }
  730. catch (Exception)
  731. {
  732. m_log.ErrorFormat(
  733. "[ITERATABLE UUID GATHERER]: Failed to gather uuids for asset id {0}, type {1}",
  734. assetUuid, assetType);
  735. throw;
  736. }
  737. }
  738. /// <summary>
  739. /// Gather all the asset uuids associated with a given object.
  740. /// </summary>
  741. /// <remarks>
  742. /// This includes both those directly associated with
  743. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  744. /// within this object).
  745. /// </remarks>
  746. /// <param name="sceneObject">The scene object for which to gather assets</param>
  747. public void RecordAssetUuids(SceneObjectGroup sceneObject)
  748. {
  749. // m_log.DebugFormat(
  750. // "[ASSET GATHERER]: Getting assets for object {0}, {1}", sceneObject.Name, sceneObject.UUID);
  751. SceneObjectPart[] parts = sceneObject.Parts;
  752. for (int i = 0; i < parts.Length; i++)
  753. {
  754. SceneObjectPart part = parts[i];
  755. // m_log.DebugFormat(
  756. // "[ARCHIVER]: Getting part {0}, {1} for object {2}", part.Name, part.UUID, sceneObject.UUID);
  757. try
  758. {
  759. Primitive.TextureEntry textureEntry = part.Shape.Textures;
  760. if (textureEntry != null)
  761. {
  762. // Get the prim's default texture. This will be used for faces which don't have their own texture
  763. if (textureEntry.DefaultTexture != null)
  764. RecordTextureEntryAssetUuids(textureEntry.DefaultTexture);
  765. if (textureEntry.FaceTextures != null)
  766. {
  767. // Loop through the rest of the texture faces (a non-null face means the face is different from DefaultTexture)
  768. foreach (Primitive.TextureEntryFace texture in textureEntry.FaceTextures)
  769. {
  770. if (texture != null)
  771. RecordTextureEntryAssetUuids(texture);
  772. }
  773. }
  774. }
  775. // If the prim is a sculpt then preserve this information too
  776. if (part.Shape.SculptTexture != UUID.Zero)
  777. m_gatheredAssetUuids[part.Shape.SculptTexture] = (sbyte)AssetType.Texture;
  778. if (part.Shape.ProjectionTextureUUID != UUID.Zero)
  779. m_gatheredAssetUuids[part.Shape.ProjectionTextureUUID] = (sbyte)AssetType.Texture;
  780. if (part.CollisionSound != UUID.Zero)
  781. m_gatheredAssetUuids[part.CollisionSound] = (sbyte)AssetType.Sound;
  782. if (part.ParticleSystem.Length > 0)
  783. {
  784. try
  785. {
  786. Primitive.ParticleSystem ps = new Primitive.ParticleSystem(part.ParticleSystem, 0);
  787. if (ps.Texture != UUID.Zero)
  788. m_gatheredAssetUuids[ps.Texture] = (sbyte)AssetType.Texture;
  789. }
  790. catch (Exception)
  791. {
  792. m_log.WarnFormat(
  793. "[UUID GATHERER]: Could not check particle system for part {0} {1} in object {2} {3} since it is corrupt. Continuing.",
  794. part.Name, part.UUID, sceneObject.Name, sceneObject.UUID);
  795. }
  796. }
  797. TaskInventoryDictionary taskDictionary = (TaskInventoryDictionary)part.TaskInventory.Clone();
  798. // Now analyze this prim's inventory items to preserve all the uuids that they reference
  799. foreach (TaskInventoryItem tii in taskDictionary.Values)
  800. {
  801. // m_log.DebugFormat(
  802. // "[ARCHIVER]: Analysing item {0} asset type {1} in {2} {3}",
  803. // tii.Name, tii.Type, part.Name, part.UUID);
  804. if (!m_gatheredAssetUuids.ContainsKey(tii.AssetID))
  805. RecordAssetUuids(tii.AssetID, (sbyte)tii.Type);
  806. }
  807. // FIXME: We need to make gathering modular but we cannot yet, since gatherers are not guaranteed
  808. // to be called with scene objects that are in a scene (e.g. in the case of hg asset mapping and
  809. // inventory transfer. There needs to be a way for a module to register a method without assuming a
  810. // Scene.EventManager is present.
  811. // part.ParentGroup.Scene.EventManager.TriggerGatherUuids(part, assetUuids);
  812. // still needed to retrieve textures used as materials for any parts containing legacy materials stored in DynAttrs
  813. RecordMaterialsUuids(part);
  814. }
  815. catch (Exception e)
  816. {
  817. m_log.ErrorFormat("[UUID GATHERER]: Failed to get part - {0}", e);
  818. m_log.DebugFormat(
  819. "[UUID GATHERER]: Texture entry length for prim was {0} (min is 46)",
  820. part.Shape.TextureEntry.Length);
  821. }
  822. }
  823. }
  824. /// <summary>
  825. /// Collect all the asset uuids found in one face of a Texture Entry.
  826. /// </summary>
  827. private void RecordTextureEntryAssetUuids(Primitive.TextureEntryFace texture)
  828. {
  829. m_gatheredAssetUuids[texture.TextureID] = (sbyte)AssetType.Texture;
  830. if (texture.MaterialID != UUID.Zero)
  831. AddAssetUuidToInspect(texture.MaterialID);
  832. }
  833. /// <summary>
  834. /// Gather all of the texture asset UUIDs used to reference "Materials" such as normal and specular maps
  835. /// stored in legacy format in part.DynAttrs
  836. /// </summary>
  837. /// <param name="part"></param>
  838. public void RecordMaterialsUuids(SceneObjectPart part)
  839. {
  840. // scan thru the dynAttrs map of this part for any textures used as materials
  841. OSD osdMaterials = null;
  842. lock (part.DynAttrs)
  843. {
  844. if (part.DynAttrs.ContainsStore("OpenSim", "Materials"))
  845. {
  846. OSDMap materialsStore = part.DynAttrs.GetStore("OpenSim", "Materials");
  847. if (materialsStore == null)
  848. return;
  849. materialsStore.TryGetValue("Materials", out osdMaterials);
  850. }
  851. if (osdMaterials != null)
  852. {
  853. //m_log.Info("[UUID Gatherer]: found Materials: " + OSDParser.SerializeJsonString(osd));
  854. if (osdMaterials is OSDArray)
  855. {
  856. OSDArray matsArr = osdMaterials as OSDArray;
  857. foreach (OSDMap matMap in matsArr)
  858. {
  859. try
  860. {
  861. if (matMap.ContainsKey("Material"))
  862. {
  863. OSDMap mat = matMap["Material"] as OSDMap;
  864. if (mat.ContainsKey("NormMap"))
  865. {
  866. UUID normalMapId = mat["NormMap"].AsUUID();
  867. if (normalMapId != UUID.Zero)
  868. {
  869. m_gatheredAssetUuids[normalMapId] = (sbyte)AssetType.Texture;
  870. //m_log.Info("[UUID Gatherer]: found normal map ID: " + normalMapId.ToString());
  871. }
  872. }
  873. if (mat.ContainsKey("SpecMap"))
  874. {
  875. UUID specularMapId = mat["SpecMap"].AsUUID();
  876. if (specularMapId != UUID.Zero)
  877. {
  878. m_gatheredAssetUuids[specularMapId] = (sbyte)AssetType.Texture;
  879. //m_log.Info("[UUID Gatherer]: found specular map ID: " + specularMapId.ToString());
  880. }
  881. }
  882. }
  883. }
  884. catch (Exception e)
  885. {
  886. m_log.Warn("[UUID Gatherer]: exception getting materials: " + e.Message);
  887. }
  888. }
  889. }
  890. }
  891. }
  892. }
  893. /// <summary>
  894. /// Get an asset synchronously, potentially using an asynchronous callback. If the
  895. /// asynchronous callback is used, we will wait for it to complete.
  896. /// </summary>
  897. /// <param name="uuid"></param>
  898. /// <returns></returns>
  899. protected virtual AssetBase GetAsset(UUID uuid)
  900. {
  901. return m_assetService.Get(uuid.ToString());
  902. }
  903. /// <summary>
  904. /// Record the asset uuids embedded within the given text (e.g. a script).
  905. /// </summary>
  906. /// <param name="textAsset"></param>
  907. private void RecordTextEmbeddedAssetUuids(AssetBase textAsset)
  908. {
  909. // m_log.DebugFormat("[ASSET GATHERER]: Getting assets for uuid references in asset {0}", embeddingAssetId);
  910. string script = Utils.BytesToString(textAsset.Data);
  911. // m_log.DebugFormat("[ARCHIVER]: Script {0}", script);
  912. MatchCollection uuidMatches = Util.PermissiveUUIDPattern.Matches(script);
  913. // m_log.DebugFormat("[ARCHIVER]: Found {0} matches in text", uuidMatches.Count);
  914. foreach (Match uuidMatch in uuidMatches)
  915. {
  916. UUID uuid = new UUID(uuidMatch.Value);
  917. // m_log.DebugFormat("[ARCHIVER]: Recording {0} in text", uuid);
  918. AddAssetUuidToInspect(uuid);
  919. }
  920. }
  921. /// <summary>
  922. /// Record the uuids referenced by the given wearable asset
  923. /// </summary>
  924. /// <param name="assetBase"></param>
  925. private void RecordWearableAssetUuids(AssetBase assetBase)
  926. {
  927. //m_log.Debug(new System.Text.ASCIIEncoding().GetString(bodypartAsset.Data));
  928. AssetWearable wearableAsset = new AssetBodypart(assetBase.FullID, assetBase.Data);
  929. wearableAsset.Decode();
  930. //m_log.DebugFormat(
  931. // "[ARCHIVER]: Wearable asset {0} references {1} assets", wearableAssetUuid, wearableAsset.Textures.Count);
  932. foreach (UUID uuid in wearableAsset.Textures.Values)
  933. m_gatheredAssetUuids[uuid] = (sbyte)AssetType.Texture;
  934. }
  935. /// <summary>
  936. /// Get all the asset uuids associated with a given object. This includes both those directly associated with
  937. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  938. /// within this object).
  939. /// </summary>
  940. /// <param name="sceneObjectAsset"></param>
  941. private void RecordSceneObjectAssetUuids(AssetBase sceneObjectAsset)
  942. {
  943. string xml = Utils.BytesToString(sceneObjectAsset.Data);
  944. CoalescedSceneObjects coa;
  945. if (CoalescedSceneObjectsSerializer.TryFromXml(xml, out coa))
  946. {
  947. foreach (SceneObjectGroup sog in coa.Objects)
  948. RecordAssetUuids(sog);
  949. }
  950. else
  951. {
  952. SceneObjectGroup sog = SceneObjectSerializer.FromOriginalXmlFormat(xml);
  953. if (null != sog)
  954. RecordAssetUuids(sog);
  955. }
  956. }
  957. /// <summary>
  958. /// Get the asset uuid associated with a gesture
  959. /// </summary>
  960. /// <param name="gestureAsset"></param>
  961. private void RecordGestureAssetUuids(AssetBase gestureAsset)
  962. {
  963. using (MemoryStream ms = new MemoryStream(gestureAsset.Data))
  964. using (StreamReader sr = new StreamReader(ms))
  965. {
  966. sr.ReadLine(); // Unknown (Version?)
  967. sr.ReadLine(); // Unknown
  968. sr.ReadLine(); // Unknown
  969. sr.ReadLine(); // Name
  970. sr.ReadLine(); // Comment ?
  971. int count = Convert.ToInt32(sr.ReadLine()); // Item count
  972. for (int i = 0 ; i < count ; i++)
  973. {
  974. string type = sr.ReadLine();
  975. if (type == null)
  976. break;
  977. string name = sr.ReadLine();
  978. if (name == null)
  979. break;
  980. string id = sr.ReadLine();
  981. if (id == null)
  982. break;
  983. string unknown = sr.ReadLine();
  984. if (unknown == null)
  985. break;
  986. // If it can be parsed as a UUID, it is an asset ID
  987. UUID uuid;
  988. if (UUID.TryParse(id, out uuid))
  989. m_gatheredAssetUuids[uuid] = (sbyte)AssetType.Animation; // the asset is either an Animation or a Sound, but this distinction isn't important
  990. }
  991. }
  992. }
  993. /// <summary>
  994. /// Get the asset uuid's referenced in a material.
  995. /// </summary>
  996. private void RecordMaterialAssetUuids(AssetBase materialAsset)
  997. {
  998. OSDMap mat = (OSDMap)OSDParser.DeserializeLLSDXml(materialAsset.Data);
  999. UUID normMap = mat["NormMap"].AsUUID();
  1000. if (normMap != UUID.Zero)
  1001. m_gatheredAssetUuids[normMap] = (sbyte)AssetType.Texture;
  1002. UUID specMap = mat["SpecMap"].AsUUID();
  1003. if (specMap != UUID.Zero)
  1004. m_gatheredAssetUuids[specMap] = (sbyte)AssetType.Texture;
  1005. }
  1006. }
  1007. public class IteratingHGUuidGatherer : IteratingUuidGatherer
  1008. {
  1009. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  1010. protected string m_assetServerURL;
  1011. public IteratingHGUuidGatherer(IAssetService assetService, string assetServerURL)
  1012. : base(assetService)
  1013. {
  1014. m_assetServerURL = assetServerURL;
  1015. if (!m_assetServerURL.EndsWith("/") && !m_assetServerURL.EndsWith("="))
  1016. m_assetServerURL = m_assetServerURL + "/";
  1017. }
  1018. protected override AssetBase GetAsset(UUID uuid)
  1019. {
  1020. if (string.Empty == m_assetServerURL)
  1021. return base.GetAsset(uuid);
  1022. else
  1023. return FetchAsset(uuid);
  1024. }
  1025. public AssetBase FetchAsset(UUID assetID)
  1026. {
  1027. // Test if it's already here
  1028. AssetBase asset = m_assetService.Get(assetID.ToString());
  1029. if (asset == null)
  1030. {
  1031. // It's not, so fetch it from abroad
  1032. asset = m_assetService.Get(m_assetServerURL + assetID.ToString());
  1033. if (asset != null)
  1034. m_log.DebugFormat("[HGUUIDGatherer]: Copied asset {0} from {1} to local asset server", assetID, m_assetServerURL);
  1035. else
  1036. m_log.DebugFormat("[HGUUIDGatherer]: Failed to fetch asset {0} from {1}", assetID, m_assetServerURL);
  1037. }
  1038. //else
  1039. // m_log.DebugFormat("[HGUUIDGatherer]: Asset {0} from {1} was already here", assetID, m_assetServerURL);
  1040. return asset;
  1041. }
  1042. }
  1043. }