UuidGatherer.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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;
  32. using log4net;
  33. using OpenMetaverse;
  34. using OpenMetaverse.Assets;
  35. using OpenMetaverse.StructuredData;
  36. using OpenSim.Framework;
  37. using OpenSim.Region.Framework.Scenes.Serialization;
  38. using OpenSim.Services.Interfaces;
  39. using OpenSimAssetType = OpenSim.Framework.SLUtil.OpenSimAssetType;
  40. namespace OpenSim.Region.Framework.Scenes
  41. {
  42. /// <summary>
  43. /// Gather uuids for a given entity.
  44. /// </summary>
  45. /// <remarks>
  46. /// This does a deep inspection of the entity to retrieve all the assets it uses (whether as textures, as scripts
  47. /// contained in inventory, as scripts contained in objects contained in another object's inventory, etc. Assets
  48. /// are only retrieved when they are necessary to carry out the inspection (i.e. a serialized object needs to be
  49. /// retrieved to work out which assets it references).
  50. /// </remarks>
  51. public class UuidGatherer
  52. {
  53. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  54. private static HashSet<UUID> ToSkip = new HashSet<UUID>()
  55. {
  56. new UUID("11111111-1111-0000-0000-000100bba000"),
  57. new UUID("5a9f4a74-30f2-821c-b88d-70499d3e7183"),
  58. new UUID("ae2de45c-d252-50b8-5c6e-19f39ce79317"),
  59. new UUID("24daea5f-0539-cfcf-047f-fbc40b2786ba"),
  60. new UUID("52cc6bb6-2ee5-e632-d3ad-50197b1dcb8a"),
  61. new UUID("43529ce8-7faa-ad92-165a-bc4078371687"),
  62. new UUID("09aac1fb-6bce-0bee-7d44-caac6dbb6c63"),
  63. new UUID("ff62763f-d60a-9855-890b-0c96f8f8cd98"),
  64. new UUID("8e915e25-31d1-cc95-ae08-d58a47488251"),
  65. new UUID("9742065b-19b5-297c-858a-29711d539043"),
  66. new UUID("03642e83-2bd1-4eb9-34b4-4c47ed586d2d"),
  67. new UUID("edd51b77-fc10-ce7a-4b3d-011dfc349e4f"),
  68. new UUID("44e87126-e794-4ded-05b3-7c42da3d5cdb"),
  69. new UUID("3d6181b0-6a4b-97ef-18d8-722652995cf1"),
  70. new UUID("b4ba225c-373f-446d-9f7e-6cb7b5cf9b3d"),
  71. new UUID("d2114404-dd59-4a4d-8e6c-49359e91bbf0"),
  72. new UUID("c228d1cf-4b5d-4ba8-84f4-899a0796aa97"),
  73. new UUID("e97cf410-8e61-7005-ec06-629eba4cd1fb"),
  74. new UUID("38b86f85-2575-52a9-a531-23108d8da837"),
  75. new UUID("8dcd4a48-2d37-4909-9f78-f7a9eb4ef903"),
  76. new UUID("3c59f7fe-9dc8-47f9-8aaf-a9dd1fbc3bef"),
  77. new UUID("0bc58228-74a0-7e83-89bc-5c23464bcec5"),
  78. new UUID("63338ede-0037-c4fd-855b-015d77112fc8"),
  79. new UUID("303cd381-8560-7579-23f1-f0a880799740"),
  80. new UUID("53a2f406-4895-1d13-d541-d2e3b86bc19c"),
  81. new UUID("822ded49-9a6c-f61c-cb89-6df54f42cdf4"),
  82. new UUID("6b61c8e8-4747-0d75-12d7-e49ff207a4ca"),
  83. new UUID("b5b4a67d-0aee-30d2-72cd-77b333e932ef"),
  84. new UUID("46bb4359-de38-4ed8-6a22-f1f52fe8f506"),
  85. new UUID("3147d815-6338-b932-f011-16b56d9ac18b"),
  86. new UUID("ea633413-8006-180a-c3ba-96dd1d756720"),
  87. new UUID("5747a48e-073e-c331-f6f3-7c2149613d3e"),
  88. new UUID("fd037134-85d4-f241-72c6-4f42164fedee"),
  89. new UUID("c4ca6188-9127-4f31-0158-23c4e2f93304"),
  90. new UUID("18b3a4b5-b463-bd48-e4b6-71eaac76c515"),
  91. new UUID("db84829b-462c-ee83-1e27-9bbee66bd624"),
  92. new UUID("b906c4ba-703b-1940-32a3-0c7f7d791510"),
  93. new UUID("82e99230-c906-1403-4d9c-3889dd98daba"),
  94. new UUID("349a3801-54f9-bf2c-3bd0-1ac89772af01"),
  95. new UUID("efcf670c-2d18-8128-973a-034ebc806b67"),
  96. new UUID("9b0c1c4e-8ac7-7969-1494-28c874c4f668"),
  97. new UUID("9ba1c942-08be-e43a-fb29-16ad440efc50"),
  98. new UUID("201f3fdf-cb1f-dbec-201f-7333e328ae7c"),
  99. new UUID("47f5f6fb-22e5-ae44-f871-73aaaf4a6022"),
  100. new UUID("92624d3e-1068-f1aa-a5ec-8244585193ed"),
  101. new UUID("038fcec9-5ebd-8a8e-0e2e-6e71a0a1ac53"),
  102. new UUID("6883a61a-b27b-5914-a61e-dda118a9ee2c"),
  103. new UUID("b68a3d7c-de9e-fc87-eec8-543d787e5b0d"),
  104. new UUID("928cae18-e31d-76fd-9cc9-2f55160ff818"),
  105. new UUID("30047778-10ea-1af7-6881-4db7a3a5a114"),
  106. new UUID("951469f4-c7b2-c818-9dee-ad7eea8c30b7"),
  107. new UUID("4bd69a1d-1114-a0b4-625f-84e0a5237155"),
  108. new UUID("cd28b69b-9c95-bb78-3f94-8d605ff1bb12"),
  109. new UUID("a54d8ee2-28bb-80a9-7f0c-7afbbe24a5d6"),
  110. new UUID("b0dc417c-1f11-af36-2e80-7e7489fa7cdc"),
  111. new UUID("57abaae6-1d17-7b1b-5f98-6d11a6411276"),
  112. new UUID("0f86e355-dd31-a61c-fdb0-3a96b9aad05f"),
  113. new UUID("514af488-9051-044a-b3fc-d4dbf76377c6"),
  114. new UUID("aa2df84d-cf8f-7218-527b-424a52de766e"),
  115. new UUID("1a03b575-9634-b62a-5767-3a679e81f4de"),
  116. new UUID("214aa6c1-ba6a-4578-f27c-ce7688f61d0d"),
  117. new UUID("d535471b-85bf-3b4d-a542-93bea4f59d33"),
  118. new UUID("d4416ff1-09d3-300f-4183-1b68a19b9fc1"),
  119. new UUID("0b8c8211-d78c-33e8-fa28-c51a9594e424"),
  120. new UUID("fee3df48-fa3d-1015-1e26-a205810e3001"),
  121. new UUID("1e8d90cc-a84e-e135-884c-7c82c8b03a14"),
  122. new UUID("62570842-0950-96f8-341c-809e65110823"),
  123. new UUID("d63bc1f9-fc81-9625-a0c6-007176d82eb7"),
  124. new UUID("f76cda94-41d4-a229-2872-e0296e58afe1"),
  125. new UUID("eb6ebfb2-a4b3-a19c-d388-4dd5c03823f7"),
  126. new UUID("a351b1bc-cc94-aac2-7bea-a7e6ebad15ef"),
  127. new UUID("b7c7c833-e3d3-c4e3-9fc0-131237446312"),
  128. new UUID("728646d9-cc79-08b2-32d6-937f0a835c24"),
  129. new UUID("835965c6-7f2f-bda2-5deb-2478737f91bf"),
  130. new UUID("b92ec1a5-e7ce-a76b-2b05-bcdb9311417e"),
  131. new UUID("da020525-4d94-59d6-23d7-81fdebf33148"),
  132. new UUID("9c05e5c7-6f07-6ca4-ed5a-b230390c3950"),
  133. new UUID("666307d9-a860-572d-6fd4-c3ab8865c094"),
  134. new UUID("85995026-eade-5d78-d364-94a64512cb66"),
  135. new UUID("f5fc7433-043d-e819-8298-f519a119b688"),
  136. new UUID("d60c41d2-7c24-7074-d3fa-6101cea22a51"),
  137. new UUID("c1bc7f36-3ba0-d844-f93c-93be945d644f"),
  138. new UUID("7db00ccd-f380-f3ee-439d-61968ec69c8a"),
  139. new UUID("aec4610c-757f-bc4e-c092-c6e9caf18daf"),
  140. new UUID("2b5a38b2-5e00-3a97-a495-4c826bc443e6"),
  141. new UUID("9b29cd61-c45b-5689-ded2-91756b8d76a9"),
  142. new UUID("ef62d355-c815-4816-2474-b1acc21094a6"),
  143. new UUID("8b102617-bcba-037b-86c1-b76219f90c88"),
  144. new UUID("efdc1727-8b8a-c800-4077-975fc27ee2f2"),
  145. new UUID("3d94bad0-c55b-7dcc-8763-033c59405d33"),
  146. new UUID("7570c7b5-1f22-56dd-56ef-a9168241bbb6"),
  147. new UUID("4ae8016b-31b9-03bb-c401-b1ea941db41d"),
  148. new UUID("20f063ea-8306-2562-0b07-5c853b37b31e"),
  149. new UUID("62c5de58-cb33-5743-3d07-9e4cd4352864"),
  150. new UUID("5ea3991f-c293-392e-6860-91dfa01278a3"),
  151. new UUID("2305bd75-1ca9-b03b-1faa-b176b8a8c49e"),
  152. new UUID("709ea28e-1573-c023-8bf8-520c8bc637fa"),
  153. new UUID("19999406-3a3a-d58c-a2ac-d72e555dcf51"),
  154. new UUID("7a17b059-12b2-41b1-570a-186368b6aa6f"),
  155. new UUID("ca5b3f14-3194-7a2b-c894-aa699b718d1f"),
  156. new UUID("f4f00d6e-b9fe-9292-f4cb-0ae06ea58d57"),
  157. new UUID("08464f78-3a8e-2944-cba5-0c94aff3af29"),
  158. new UUID("315c3a41-a5f3-0ba4-27da-f893f769e69b"),
  159. new UUID("5a977ed9-7f72-44e9-4c4c-6e913df8ae74"),
  160. new UUID("d83fa0e5-97ed-7eb2-e798-7bd006215cb4"),
  161. new UUID("f061723d-0a18-754f-66ee-29a44795a32f"),
  162. new UUID("eefc79be-daae-a239-8c04-890f5d23654a"),
  163. new UUID("b312b10e-65ab-a0a4-8b3c-1326ea8e3ed9"),
  164. new UUID("17c024cc-eef2-f6a0-3527-9869876d7752"),
  165. new UUID("ec952cca-61ef-aa3b-2789-4d1344f016de"),
  166. new UUID("7a4e87fe-de39-6fcb-6223-024b00893244"),
  167. new UUID("f3300ad9-3462-1d07-2044-0fef80062da0"),
  168. new UUID("c8e42d32-7310-6906-c903-cab5d4a34656"),
  169. new UUID("36f81a92-f076-5893-dc4b-7c3795e487cf"),
  170. new UUID("49aea43b-5ac3-8a44-b595-96100af0beda"),
  171. new UUID("35db4f7e-28c2-6679-cea9-3ee108f7fc7f"),
  172. new UUID("0836b67f-7f7b-f37b-c00a-460dc1521f5a"),
  173. new UUID("42dd95d5-0bc6-6392-f650-777304946c0f"),
  174. new UUID("16803a9f-5140-e042-4d7b-d28ba247c325"),
  175. new UUID("05ddbff8-aaa9-92a1-2b74-8fe77a29b445"),
  176. new UUID("1ab1b236-cd08-21e6-0cbc-0d923fc6eca2"),
  177. new UUID("0eb702e2-cc5a-9a88-56a5-661a55c0676a"),
  178. new UUID("cd7668a6-7011-d7e2-ead8-fc69eff1a104"),
  179. new UUID("e04d450d-fdb5-0432-fd68-818aaf5935f8"),
  180. new UUID("6bd01860-4ebd-127a-bb3d-d1427e8e0c42"),
  181. new UUID("70ea714f-3a97-d742-1b01-590a8fcd1db5"),
  182. new UUID("1a5fe8ac-a804-8a5d-7cbd-56bd83184568"),
  183. new UUID("b1709c8d-ecd3-54a1-4f28-d55ac0840782"),
  184. new UUID("245f3c54-f1c0-bf2e-811f-46d8eeb386e7"),
  185. new UUID("1c7600d6-661f-b87b-efe2-d7421eb93c86"),
  186. new UUID("1a2bd58e-87ff-0df8-0b4c-53e047b0bb6e"),
  187. new UUID("a8dee56f-2eae-9e7a-05a2-6fb92b97e21e"),
  188. new UUID("f2bed5f9-9d44-39af-b0cd-257b2a17fe40"),
  189. new UUID("d2f2ee58-8ad1-06c9-d8d3-3827ba31567a"),
  190. new UUID("6802d553-49da-0778-9f85-1599a2266526"),
  191. new UUID("0a9fb970-8b44-9114-d3a9-bf69cfe804d6"),
  192. new UUID("eae8905b-271a-99e2-4c0e-31106afd100c"),
  193. new UUID("2408fe9e-df1d-1d7d-f4ff-1384fa7b350f"),
  194. new UUID("3da1d753-028a-5446-24f3-9c9b856d9422"),
  195. new UUID("15468e00-3400-bb66-cecc-646d7c14458e"),
  196. new UUID("370f3a20-6ca6-9971-848c-9a01bc42ae3c"),
  197. new UUID("42b46214-4b44-79ae-deb8-0df61424ff4b"),
  198. new UUID("f22fed8b-a5ed-2c93-64d5-bdd8b93c889f"),
  199. new UUID("80700431-74ec-a008-14f8-77575e73693f"),
  200. new UUID("1cb562b0-ba21-2202-efb3-30f82cdf9595"),
  201. new UUID("41426836-7437-7e89-025d-0aa4d10f1d69"),
  202. new UUID("313b9881-4302-73c0-c7d0-0e7a36b6c224"),
  203. new UUID("85428680-6bf9-3e64-b489-6f81087c24bd"),
  204. new UUID("5c682a95-6da4-a463-0bf6-0f5b7be129d1"),
  205. new UUID("11000694-3f41-adc2-606b-eee1d66f3724"),
  206. new UUID("aa134404-7dac-7aca-2cba-435f9db875ca"),
  207. new UUID("83ff59fe-2346-f236-9009-4e3608af64c1"),
  208. new UUID("56e0ba0d-4a9f-7f27-6117-32f2ebbf6135"),
  209. new UUID("2d6daa51-3192-6794-8e2e-a15f8338ec30"),
  210. new UUID("c541c47f-e0c0-058b-ad1a-d6ae3a4584d9"),
  211. new UUID("6ed24bd8-91aa-4b12-ccc7-c97c857ab4e0"),
  212. new UUID("33339176-7ddc-9397-94a4-bf3403cbc8f5"),
  213. new UUID("7693f268-06c7-ea71-fa21-2b30d6533f8f"),
  214. new UUID("b1ed7982-c68e-a982-7561-52a88a5298c0"),
  215. new UUID("869ecdad-a44b-671e-3266-56aef2e3ac2e"),
  216. new UUID("c0c4030f-c02b-49de-24ba-2331f43fe41c"),
  217. new UUID("9f496bd2-589a-709f-16cc-69bf7df1d36c"),
  218. new UUID("15dd911d-be82-2856-26db-27659b142875"),
  219. new UUID("b8c8b2a3-9008-1771-3bfc-90924955ab2d"),
  220. new UUID("42ecd00b-9947-a97c-400a-bbc9174c7aeb")
  221. };
  222. /// <summary>
  223. /// Is gathering complete?
  224. /// </summary>
  225. public bool Complete { get { return m_assetUuidsToInspect.Count <= 0; } }
  226. /// <summary>
  227. /// The dictionary of UUIDs gathered so far. If Complete == true then this is all the reachable UUIDs.
  228. /// </summary>
  229. /// <value>The gathered uuids.</value>
  230. public IDictionary<UUID, sbyte> GatheredUuids { get; private set; }
  231. public HashSet<UUID> FailedUUIDs { get; private set; }
  232. public HashSet<UUID> UncertainAssetsUUIDs { get; private set; }
  233. public int possibleNotAssetCount { get; set; }
  234. public int ErrorCount { get; private set; }
  235. private bool verbose = true;
  236. /// <summary>
  237. /// Gets the next UUID to inspect.
  238. /// </summary>
  239. /// <value>If there is no next UUID then returns null</value>
  240. public UUID? NextUuidToInspect
  241. {
  242. get
  243. {
  244. if (Complete)
  245. return null;
  246. else
  247. return m_assetUuidsToInspect.Peek();
  248. }
  249. }
  250. protected IAssetService m_assetService;
  251. protected Queue<UUID> m_assetUuidsToInspect;
  252. /// <summary>
  253. /// Initializes a new instance of the <see cref="OpenSim.Region.Framework.Scenes.UuidGatherer"/> class.
  254. /// </summary>
  255. /// <remarks>In this case the collection of gathered assets will start out blank.</remarks>
  256. /// <param name="assetService">
  257. /// Asset service.
  258. /// </param>
  259. public UuidGatherer(IAssetService assetService) : this(assetService, new Dictionary<UUID, sbyte>(),
  260. new HashSet <UUID>(),new HashSet <UUID>()) {}
  261. public UuidGatherer(IAssetService assetService, IDictionary<UUID, sbyte> collector) : this(assetService, collector,
  262. new HashSet <UUID>(), new HashSet <UUID>()) {}
  263. /// <summary>
  264. /// Initializes a new instance of the <see cref="OpenSim.Region.Framework.Scenes.UuidGatherer"/> class.
  265. /// </summary>
  266. /// <param name="assetService">
  267. /// Asset service.
  268. /// </param>
  269. /// <param name="collector">
  270. /// Gathered UUIDs will be collected in this dictionary.
  271. /// It can be pre-populated if you want to stop the gatherer from analyzing assets that have already been fetched and inspected.
  272. /// </param>
  273. public UuidGatherer(IAssetService assetService, IDictionary<UUID, sbyte> collector, HashSet <UUID> failedIDs, HashSet <UUID> uncertainAssetsUUIDs)
  274. {
  275. m_assetService = assetService;
  276. GatheredUuids = collector;
  277. // FIXME: Not efficient for searching, can improve.
  278. m_assetUuidsToInspect = new Queue<UUID>();
  279. FailedUUIDs = failedIDs;
  280. UncertainAssetsUUIDs = uncertainAssetsUUIDs;
  281. ErrorCount = 0;
  282. possibleNotAssetCount = 0;
  283. }
  284. /// <summary>
  285. /// Adds the asset uuid for inspection during the gathering process.
  286. /// </summary>
  287. /// <returns><c>true</c>, if for inspection was added, <c>false</c> otherwise.</returns>
  288. /// <param name="uuid">UUID.</param>
  289. public bool AddForInspection(UUID uuid)
  290. {
  291. if(uuid == UUID.Zero)
  292. return false;
  293. if(ToSkip.Contains(uuid))
  294. return false;
  295. if(FailedUUIDs.Contains(uuid))
  296. {
  297. if(UncertainAssetsUUIDs.Contains(uuid))
  298. possibleNotAssetCount++;
  299. else
  300. ErrorCount++;
  301. return false;
  302. }
  303. if(GatheredUuids.ContainsKey(uuid))
  304. return false;
  305. if (m_assetUuidsToInspect.Contains(uuid))
  306. return false;
  307. // m_log.DebugFormat("[UUID GATHERER]: Adding asset {0} for inspection", uuid);
  308. m_assetUuidsToInspect.Enqueue(uuid);
  309. return true;
  310. }
  311. /// <summary>
  312. /// Gather all the asset uuids associated with a given object.
  313. /// </summary>
  314. /// <remarks>
  315. /// This includes both those directly associated with
  316. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  317. /// within this object).
  318. /// </remarks>
  319. /// <param name="sceneObject">The scene object for which to gather assets</param>
  320. public void AddForInspection(SceneObjectGroup sceneObject)
  321. {
  322. // m_log.DebugFormat(
  323. // "[UUID GATHERER]: Getting assets for object {0}, {1}", sceneObject.Name, sceneObject.UUID);
  324. if(sceneObject.IsDeleted)
  325. return;
  326. SceneObjectPart[] parts = sceneObject.Parts;
  327. for (int i = 0; i < parts.Length; i++)
  328. {
  329. SceneObjectPart part = parts[i];
  330. // m_log.DebugFormat(
  331. // "[UUID GATHERER]: Getting part {0}, {1} for object {2}", part.Name, part.UUID, sceneObject.UUID);
  332. try
  333. {
  334. Primitive.TextureEntry textureEntry = part.Shape.Textures;
  335. if (textureEntry != null)
  336. {
  337. // Get the prim's default texture. This will be used for faces which don't have their own texture
  338. if (textureEntry.DefaultTexture != null)
  339. RecordTextureEntryAssetUuids(textureEntry.DefaultTexture);
  340. if (textureEntry.FaceTextures != null)
  341. {
  342. // Loop through the rest of the texture faces (a non-null face means the face is different from DefaultTexture)
  343. foreach (Primitive.TextureEntryFace texture in textureEntry.FaceTextures)
  344. {
  345. if (texture != null)
  346. RecordTextureEntryAssetUuids(texture);
  347. }
  348. }
  349. }
  350. // If the prim is a sculpt then preserve this information too
  351. if (part.Shape.SculptTexture != UUID.Zero)
  352. GatheredUuids[part.Shape.SculptTexture] = (sbyte)AssetType.Texture;
  353. if (part.Shape.ProjectionTextureUUID != UUID.Zero)
  354. GatheredUuids[part.Shape.ProjectionTextureUUID] = (sbyte)AssetType.Texture;
  355. UUID collisionSound = part.CollisionSound;
  356. if ( collisionSound != UUID.Zero &&
  357. collisionSound != part.invalidCollisionSoundUUID)
  358. GatheredUuids[collisionSound] = (sbyte)AssetType.Sound;
  359. if (part.ParticleSystem.Length > 0)
  360. {
  361. try
  362. {
  363. Primitive.ParticleSystem ps = new Primitive.ParticleSystem(part.ParticleSystem, 0);
  364. if (ps.Texture != UUID.Zero)
  365. GatheredUuids[ps.Texture] = (sbyte)AssetType.Texture;
  366. }
  367. catch (Exception)
  368. {
  369. m_log.WarnFormat(
  370. "[UUID GATHERER]: Could not check particle system for part {0} {1} in object {2} {3} since it is corrupt. Continuing.",
  371. part.Name, part.UUID, sceneObject.Name, sceneObject.UUID);
  372. }
  373. }
  374. TaskInventoryDictionary taskDictionary = (TaskInventoryDictionary)part.TaskInventory.Clone();
  375. // Now analyze this prim's inventory items to preserve all the uuids that they reference
  376. foreach (TaskInventoryItem tii in taskDictionary.Values)
  377. {
  378. // m_log.DebugFormat(
  379. // "[ARCHIVER]: Analysing item {0} asset type {1} in {2} {3}",
  380. // tii.Name, tii.Type, part.Name, part.UUID);
  381. AddForInspection(tii.AssetID, (sbyte)tii.Type);
  382. }
  383. // FIXME: We need to make gathering modular but we cannot yet, since gatherers are not guaranteed
  384. // to be called with scene objects that are in a scene (e.g. in the case of hg asset mapping and
  385. // inventory transfer. There needs to be a way for a module to register a method without assuming a
  386. // Scene.EventManager is present.
  387. // part.ParentGroup.Scene.EventManager.TriggerGatherUuids(part, assetUuids);
  388. // still needed to retrieve textures used as materials for any parts containing legacy materials stored in DynAttrs
  389. RecordMaterialsUuids(part);
  390. }
  391. catch (Exception e)
  392. {
  393. m_log.ErrorFormat("[UUID GATHERER]: Failed to get part - {0}", e);
  394. }
  395. }
  396. }
  397. /// <summary>
  398. /// Gathers the next set of assets returned by the next uuid to get from the asset service.
  399. /// </summary>
  400. /// <returns>false if gathering is already complete, true otherwise</returns>
  401. public bool GatherNext()
  402. {
  403. if (Complete)
  404. return false;
  405. UUID nextToInspect = m_assetUuidsToInspect.Dequeue();
  406. // m_log.DebugFormat("[UUID GATHERER]: Inspecting asset {0}", nextToInspect);
  407. GetAssetUuids(nextToInspect);
  408. return m_assetUuidsToInspect.Count > 0;
  409. }
  410. /// <summary>
  411. /// Gathers all remaining asset UUIDS no matter how many calls are required to the asset service.
  412. /// </summary>
  413. /// <returns>false if gathering is already complete, true otherwise</returns>
  414. public bool GatherAll(bool report = false)
  415. {
  416. if (Complete)
  417. return false;
  418. if(report)
  419. verbose = false;
  420. while (GatherNext());
  421. if (report && FailedUUIDs.Count > 0)
  422. {
  423. StringBuilder sb = new StringBuilder(512);
  424. int i = FailedUUIDs.Count;
  425. sb.Append("[UUID GATHERER]: UUIDs that are not assets or really missing assets:\n\t");
  426. foreach (UUID id in FailedUUIDs)
  427. {
  428. sb.Append(id);
  429. if (--i > 0)
  430. sb.Append(',');
  431. }
  432. m_log.Debug(sb.ToString());
  433. }
  434. return true;
  435. }
  436. /// <summary>
  437. /// Gather all the asset uuids associated with the asset referenced by a given uuid
  438. /// </summary>
  439. /// <remarks>
  440. /// This includes both those directly associated with
  441. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  442. /// within this object).
  443. /// This method assumes that the asset type associated with this asset in persistent storage is correct (which
  444. /// should always be the case). So with this method we always need to retrieve asset data even if the asset
  445. /// is of a type which is known not to reference any other assets
  446. /// </remarks>
  447. /// <param name="assetUuid">The uuid of the asset for which to gather referenced assets</param>
  448. private void GetAssetUuids(UUID assetUuid)
  449. {
  450. if(assetUuid == UUID.Zero)
  451. return;
  452. if(FailedUUIDs.Contains(assetUuid))
  453. {
  454. if(UncertainAssetsUUIDs.Contains(assetUuid))
  455. possibleNotAssetCount++;
  456. else
  457. ErrorCount++;
  458. return;
  459. }
  460. // avoid infinite loops
  461. if (GatheredUuids.ContainsKey(assetUuid))
  462. return;
  463. AssetBase assetBase;
  464. try
  465. {
  466. assetBase = GetAsset(assetUuid);
  467. }
  468. catch (Exception e)
  469. {
  470. if(verbose)
  471. m_log.ErrorFormat("[UUID GATHERER]: Failed to get asset {0} : {1}", assetUuid, e.Message);
  472. ErrorCount++;
  473. FailedUUIDs.Add(assetUuid);
  474. return;
  475. }
  476. if(assetBase == null)
  477. {
  478. // m_log.ErrorFormat("[UUID GATHERER]: asset {0} not found", assetUuid);
  479. FailedUUIDs.Add(assetUuid);
  480. if(UncertainAssetsUUIDs.Contains(assetUuid))
  481. possibleNotAssetCount++;
  482. else
  483. ErrorCount++;
  484. return;
  485. }
  486. if(UncertainAssetsUUIDs.Contains(assetUuid))
  487. UncertainAssetsUUIDs.Remove(assetUuid);
  488. sbyte assetType = assetBase.Type;
  489. if(assetBase.Data == null || assetBase.Data.Length == 0)
  490. {
  491. // m_log.ErrorFormat("[UUID GATHERER]: asset {0}, type {1} has no data", assetUuid, assetType);
  492. ErrorCount++;
  493. FailedUUIDs.Add(assetUuid);
  494. return;
  495. }
  496. GatheredUuids[assetUuid] = assetType;
  497. try
  498. {
  499. if ((sbyte)AssetType.Bodypart == assetType || (sbyte)AssetType.Clothing == assetType)
  500. {
  501. RecordWearableAssetUuids(assetBase);
  502. }
  503. else if ((sbyte)AssetType.Gesture == assetType)
  504. {
  505. RecordGestureAssetUuids(assetBase);
  506. }
  507. else if ((sbyte)AssetType.Notecard == assetType)
  508. {
  509. RecordNoteCardEmbeddedAssetUuids(assetBase);
  510. }
  511. else if ((sbyte)AssetType.LSLText == assetType)
  512. {
  513. RecordEmbeddedAssetDataUuids(assetBase);
  514. }
  515. else if ((sbyte)OpenSimAssetType.Material == assetType)
  516. {
  517. RecordMaterialAssetUuids(assetBase);
  518. }
  519. else if ((sbyte)AssetType.Object == assetType)
  520. {
  521. RecordSceneObjectAssetUuids(assetBase);
  522. }
  523. }
  524. catch (Exception e)
  525. {
  526. if(verbose)
  527. m_log.ErrorFormat("[UUID GATHERER]: Failed to gather uuids for asset with id {0} type {1}: {2}", assetUuid, assetType, e.Message);
  528. GatheredUuids.Remove(assetUuid);
  529. ErrorCount++;
  530. FailedUUIDs.Add(assetUuid);
  531. }
  532. }
  533. private void AddForInspection(UUID assetUuid, sbyte assetType)
  534. {
  535. if(assetUuid == UUID.Zero)
  536. return;
  537. // Here, we want to collect uuids which require further asset fetches but mark the others as gathered
  538. if(FailedUUIDs.Contains(assetUuid))
  539. {
  540. if(UncertainAssetsUUIDs.Contains(assetUuid))
  541. possibleNotAssetCount++;
  542. else
  543. ErrorCount++;
  544. return;
  545. }
  546. if(GatheredUuids.ContainsKey(assetUuid))
  547. return;
  548. try
  549. {
  550. if ((sbyte)AssetType.Bodypart == assetType
  551. || (sbyte)AssetType.Clothing == assetType
  552. || (sbyte)AssetType.Gesture == assetType
  553. || (sbyte)AssetType.Notecard == assetType
  554. || (sbyte)AssetType.LSLText == assetType
  555. || (sbyte)OpenSimAssetType.Material == assetType
  556. || (sbyte)AssetType.Object == assetType
  557. || (sbyte)AssetType.Settings == assetType)
  558. {
  559. AddForInspection(assetUuid);
  560. }
  561. else
  562. {
  563. GatheredUuids[assetUuid] = assetType;
  564. }
  565. }
  566. catch (Exception)
  567. {
  568. m_log.ErrorFormat(
  569. "[UUID GATHERER]: Failed to gather uuids for asset id {0}, type {1}",
  570. assetUuid, assetType);
  571. throw;
  572. }
  573. }
  574. /// <summary>
  575. /// Collect all the asset uuids found in one face of a Texture Entry.
  576. /// </summary>
  577. private void RecordTextureEntryAssetUuids(Primitive.TextureEntryFace texture)
  578. {
  579. GatheredUuids[texture.TextureID] = (sbyte)AssetType.Texture;
  580. if (texture.MaterialID != UUID.Zero)
  581. AddForInspection(texture.MaterialID);
  582. }
  583. /// <summary>
  584. /// Gather all of the texture asset UUIDs used to reference "Materials" such as normal and specular maps
  585. /// stored in legacy format in part.DynAttrs
  586. /// </summary>
  587. /// <param name="part"></param>
  588. private void RecordMaterialsUuids(SceneObjectPart part)
  589. {
  590. // scan thru the dynAttrs map of this part for any textures used as materials
  591. OSD osdMaterials = null;
  592. if(part.DynAttrs == null)
  593. return;
  594. lock (part.DynAttrs)
  595. {
  596. if (part.DynAttrs.ContainsStore("OpenSim", "Materials"))
  597. {
  598. OSDMap materialsStore = part.DynAttrs.GetStore("OpenSim", "Materials");
  599. if (materialsStore == null)
  600. return;
  601. materialsStore.TryGetValue("Materials", out osdMaterials);
  602. }
  603. if (osdMaterials != null)
  604. {
  605. //m_log.Info("[UUID Gatherer]: found Materials: " + OSDParser.SerializeJsonString(osd));
  606. if (osdMaterials is OSDArray)
  607. {
  608. OSDArray matsArr = osdMaterials as OSDArray;
  609. foreach (OSDMap matMap in matsArr)
  610. {
  611. try
  612. {
  613. if (matMap.ContainsKey("Material"))
  614. {
  615. OSDMap mat = matMap["Material"] as OSDMap;
  616. if (mat.ContainsKey("NormMap"))
  617. {
  618. UUID normalMapId = mat["NormMap"].AsUUID();
  619. if (normalMapId != UUID.Zero)
  620. {
  621. GatheredUuids[normalMapId] = (sbyte)AssetType.Texture;
  622. //m_log.Info("[UUID Gatherer]: found normal map ID: " + normalMapId.ToString());
  623. }
  624. }
  625. if (mat.ContainsKey("SpecMap"))
  626. {
  627. UUID specularMapId = mat["SpecMap"].AsUUID();
  628. if (specularMapId != UUID.Zero)
  629. {
  630. GatheredUuids[specularMapId] = (sbyte)AssetType.Texture;
  631. //m_log.Info("[UUID Gatherer]: found specular map ID: " + specularMapId.ToString());
  632. }
  633. }
  634. }
  635. }
  636. catch (Exception e)
  637. {
  638. m_log.Warn("[UUID Gatherer]: exception getting materials: " + e.Message);
  639. }
  640. }
  641. }
  642. }
  643. }
  644. }
  645. /// <summary>
  646. /// Get an asset synchronously, potentially using an asynchronous callback. If the
  647. /// asynchronous callback is used, we will wait for it to complete.
  648. /// </summary>
  649. /// <param name="uuid"></param>
  650. /// <returns></returns>
  651. protected virtual AssetBase GetAsset(UUID uuid)
  652. {
  653. return m_assetService.Get(uuid.ToString());
  654. }
  655. /// <summary>
  656. /// Record the asset uuids embedded within the given text (e.g. a script).
  657. /// </summary>
  658. /// <param name="textAsset"></param>
  659. private void RecordEmbeddedAssetDataUuids(AssetBase textAsset)
  660. {
  661. // m_log.DebugFormat("[ASSET GATHERER]: Getting assets for uuid references in asset {0}", embeddingAssetId);
  662. if(textAsset.Data.Length < 36)
  663. return;
  664. List<UUID> ids = Util.GetUUIDsOnData(textAsset.Data, 0, textAsset.Data.Length);
  665. if (ids == null || ids.Count == 0)
  666. return;
  667. for (int i = 0; i < ids.Count; ++i)
  668. {
  669. if (ids[i] == UUID.Zero)
  670. continue;
  671. if (!UncertainAssetsUUIDs.Contains(ids[i]))
  672. UncertainAssetsUUIDs.Add(ids[i]);
  673. AddForInspection(ids[i]);
  674. }
  675. }
  676. private void RecordNoteCardEmbeddedAssetUuids(AssetBase textAsset)
  677. {
  678. List<UUID> ids = SLUtil.GetEmbeddedAssetIDs(textAsset.Data);
  679. if(ids == null || ids.Count == 0)
  680. return;
  681. for(int i = 0; i < ids.Count; ++i)
  682. {
  683. if (ids[i] == UUID.Zero)
  684. continue;
  685. if (!UncertainAssetsUUIDs.Contains(ids[i]))
  686. UncertainAssetsUUIDs.Add(ids[i]);
  687. AddForInspection(ids[i]);
  688. }
  689. }
  690. /// <summary>
  691. /// Record the uuids referenced by the given wearable asset
  692. /// </summary>
  693. /// <param name="assetBase"></param>
  694. private void RecordWearableAssetUuids(AssetBase assetBase)
  695. {
  696. //m_log.Debug(new System.Text.ASCIIEncoding().GetString(bodypartAsset.Data));
  697. AssetWearable wearableAsset = new AssetBodypart(assetBase.FullID, assetBase.Data);
  698. wearableAsset.Decode();
  699. //m_log.DebugFormat(
  700. // "[ARCHIVER]: Wearable asset {0} references {1} assets", wearableAssetUuid, wearableAsset.Textures.Count);
  701. foreach (UUID uuid in wearableAsset.Textures.Values)
  702. GatheredUuids[uuid] = (sbyte)AssetType.Texture;
  703. }
  704. /// <summary>
  705. /// Get all the asset uuids associated with a given object. This includes both those directly associated with
  706. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  707. /// within this object).
  708. /// </summary>
  709. /// <param name="sceneObjectAsset"></param>
  710. private void RecordSceneObjectAssetUuids(AssetBase sceneObjectAsset)
  711. {
  712. string xml = Utils.BytesToString(sceneObjectAsset.Data);
  713. CoalescedSceneObjects coa;
  714. if (CoalescedSceneObjectsSerializer.TryFromXml(xml, out coa))
  715. {
  716. foreach (SceneObjectGroup sog in coa.Objects)
  717. AddForInspection(sog);
  718. }
  719. else
  720. {
  721. SceneObjectGroup sog = SceneObjectSerializer.FromOriginalXmlFormat(xml);
  722. if (null != sog)
  723. AddForInspection(sog);
  724. }
  725. }
  726. /// <summary>
  727. /// Get the asset uuid associated with a gesture
  728. /// </summary>
  729. /// <param name="gestureAsset"></param>
  730. private void RecordGestureAssetUuids(AssetBase gestureAsset)
  731. {
  732. using (MemoryStream ms = new MemoryStream(gestureAsset.Data))
  733. using (StreamReader sr = new StreamReader(ms))
  734. {
  735. sr.ReadLine(); // Unknown (Version?)
  736. sr.ReadLine(); // Unknown
  737. sr.ReadLine(); // Unknown
  738. sr.ReadLine(); // Name
  739. sr.ReadLine(); // Comment ?
  740. int count = Convert.ToInt32(sr.ReadLine()); // Item count
  741. for (int i = 0 ; i < count ; i++)
  742. {
  743. string type = sr.ReadLine();
  744. if (type == null)
  745. break;
  746. string name = sr.ReadLine();
  747. if (name == null)
  748. break;
  749. string id = sr.ReadLine();
  750. if (id == null)
  751. break;
  752. string unknown = sr.ReadLine();
  753. if (unknown == null)
  754. break;
  755. // If it can be parsed as a UUID, it is an asset ID
  756. UUID uuid;
  757. if (UUID.TryParse(id, out uuid))
  758. GatheredUuids[uuid] = (sbyte)AssetType.Animation; // the asset is either an Animation or a Sound, but this distinction isn't important
  759. }
  760. }
  761. }
  762. /// <summary>
  763. /// Get the asset uuid's referenced in a material.
  764. /// </summary>
  765. private void RecordMaterialAssetUuids(AssetBase materialAsset)
  766. {
  767. OSDMap mat;
  768. try
  769. {
  770. mat = (OSDMap)OSDParser.DeserializeLLSDXml(materialAsset.Data);
  771. }
  772. catch (Exception e)
  773. {
  774. m_log.WarnFormat("[Materials]: cannot decode material asset {0}: {1}", materialAsset.ID, e.Message);
  775. return;
  776. }
  777. UUID normMap = mat["NormMap"].AsUUID();
  778. if (normMap != UUID.Zero)
  779. GatheredUuids[normMap] = (sbyte)AssetType.Texture;
  780. UUID specMap = mat["SpecMap"].AsUUID();
  781. if (specMap != UUID.Zero)
  782. GatheredUuids[specMap] = (sbyte)AssetType.Texture;
  783. }
  784. }
  785. public class HGUuidGatherer : UuidGatherer
  786. {
  787. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  788. protected string m_assetServerURL;
  789. public HGUuidGatherer(IAssetService assetService, string assetServerURL)
  790. : this(assetService, assetServerURL, new Dictionary<UUID, sbyte>()) {}
  791. public HGUuidGatherer(IAssetService assetService, string assetServerURL, IDictionary<UUID, sbyte> collector)
  792. : base(assetService, collector)
  793. {
  794. m_assetServerURL = assetServerURL;
  795. if (!String.IsNullOrWhiteSpace(assetServerURL) && !m_assetServerURL.EndsWith("/") && !m_assetServerURL.EndsWith("="))
  796. m_assetServerURL = m_assetServerURL + "/";
  797. }
  798. protected override AssetBase GetAsset(UUID uuid)
  799. {
  800. if (String.IsNullOrWhiteSpace(m_assetServerURL))
  801. return base.GetAsset(uuid);
  802. else
  803. return FetchAsset(uuid);
  804. }
  805. public AssetBase FetchAsset(UUID assetID)
  806. {
  807. // Test if it's already here
  808. AssetBase asset = m_assetService.Get(assetID.ToString());
  809. if (asset == null)
  810. {
  811. // It's not, so fetch it from abroad
  812. asset = m_assetService.Get(m_assetServerURL + assetID.ToString());
  813. if (asset != null)
  814. m_log.DebugFormat("[HGUUIDGatherer]: Copied asset {0} from {1} to local asset server", assetID, m_assetServerURL);
  815. else
  816. m_log.DebugFormat("[HGUUIDGatherer]: Failed to fetch asset {0} from {1}", assetID, m_assetServerURL);
  817. }
  818. //else
  819. // m_log.DebugFormat("[HGUUIDGatherer]: Asset {0} from {1} was already here", assetID, m_assetServerURL);
  820. return asset;
  821. }
  822. }
  823. }