ArchiveWriteRequestPreparation.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 OpenSim 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 OpenSim.Framework;
  28. using OpenSim.Framework.Communications.Cache;
  29. using OpenSim.Region.Environment.Interfaces;
  30. using OpenSim.Region.Environment.Modules.World.Serialiser;
  31. using OpenSim.Region.Environment.Modules.World.Terrain;
  32. using OpenSim.Region.Environment.Scenes;
  33. using System;
  34. using System.Collections.Generic;
  35. using System.IO;
  36. using System.IO.Compression;
  37. using System.Reflection;
  38. using System.Text.RegularExpressions;
  39. using System.Threading;
  40. using OpenMetaverse;
  41. using log4net;
  42. using Nini.Config;
  43. namespace OpenSim.Region.Environment.Modules.World.Archiver
  44. {
  45. /// <summary>
  46. /// Prepare to write out an archive.
  47. /// </summary>
  48. public class ArchiveWriteRequestPreparation
  49. {
  50. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. protected Scene m_scene;
  52. protected Stream m_saveStream;
  53. /// <summary>
  54. /// Used as a temporary store of an asset which represents an object. This can be a null if no appropriate
  55. /// asset was found by the asset service.
  56. /// </summary>
  57. protected AssetBase m_requestedObjectAsset;
  58. /// <summary>
  59. /// Signal whether we are currently waiting for the asset service to deliver an asset.
  60. /// </summary>
  61. protected bool m_waitingForObjectAsset;
  62. /// <summary>
  63. /// Constructor
  64. /// </summary>
  65. public ArchiveWriteRequestPreparation(Scene scene, string savePath)
  66. {
  67. m_scene = scene;
  68. m_saveStream = new GZipStream(new FileStream(savePath, FileMode.Create), CompressionMode.Compress);
  69. }
  70. /// <summary>
  71. /// Constructor.
  72. /// </summary>
  73. /// <param name="scene"></param>
  74. /// <param name="saveStream">The stream to which to save data.</param>
  75. public ArchiveWriteRequestPreparation(Scene scene, Stream saveStream)
  76. {
  77. m_scene = scene;
  78. m_saveStream = saveStream;
  79. }
  80. /// <summary>
  81. /// The callback made when we request the asset for an object from the asset service.
  82. /// </summary>
  83. public void AssetRequestCallback(UUID assetID, AssetBase asset)
  84. {
  85. lock (this)
  86. {
  87. m_requestedObjectAsset = asset;
  88. m_waitingForObjectAsset = false;
  89. Monitor.Pulse(this);
  90. }
  91. }
  92. /// <summary>
  93. /// Get an asset synchronously, potentially using an asynchronous callback. If the
  94. /// asynchronous callback is used, we will wait for it to complete.
  95. /// </summary>
  96. /// <param name="uuid"></param>
  97. /// <returns></returns>
  98. protected AssetBase GetAsset(UUID uuid)
  99. {
  100. m_waitingForObjectAsset = true;
  101. m_scene.AssetCache.GetAsset(uuid, AssetRequestCallback, true);
  102. // The asset cache callback can either
  103. //
  104. // 1. Complete on the same thread (if the asset is already in the cache) or
  105. // 2. Come in via a different thread (if we need to go fetch it).
  106. //
  107. // The code below handles both these alternatives.
  108. lock (this)
  109. {
  110. if (m_waitingForObjectAsset)
  111. {
  112. Monitor.Wait(this);
  113. m_waitingForObjectAsset = false;
  114. }
  115. }
  116. return m_requestedObjectAsset;
  117. }
  118. /// <summary>
  119. /// Record the asset uuids embedded within the given script.
  120. /// </summary>
  121. /// <param name="scriptUuid"></param>
  122. /// <param name="assetUuids">Dictionary in which to record the references</param>
  123. protected void GetScriptAssetUuids(UUID scriptUuid, IDictionary<UUID, int> assetUuids)
  124. {
  125. AssetBase scriptAsset = GetAsset(scriptUuid);
  126. if (null != scriptAsset)
  127. {
  128. string script = Utils.BytesToString(scriptAsset.Data);
  129. //m_log.DebugFormat("[ARCHIVER]: Script {0}", script);
  130. MatchCollection uuidMatches = Util.UUIDPattern.Matches(script);
  131. //m_log.DebugFormat("[ARCHIVER]: Found {0} matches in script", uuidMatches.Count);
  132. foreach (Match uuidMatch in uuidMatches)
  133. {
  134. UUID uuid = new UUID(uuidMatch.Value);
  135. //m_log.DebugFormat("[ARCHIVER]: Recording {0} in script", uuid);
  136. assetUuids[uuid] = 1;
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// Record the uuids referenced by the given wearable asset
  142. /// </summary>
  143. /// <param name="wearableAssetUuid"></param>
  144. /// <param name="assetUuids">Dictionary in which to record the references</param>
  145. protected void GetWearableAssetUuids(UUID wearableAssetUuid, IDictionary<UUID, int> assetUuids)
  146. {
  147. AssetBase assetBase = GetAsset(wearableAssetUuid);
  148. //m_log.Debug(new System.Text.ASCIIEncoding().GetString(bodypartAsset.Data));
  149. AssetWearable wearableAsset = new AssetBodypart(wearableAssetUuid, assetBase.Data);
  150. wearableAsset.Decode();
  151. //m_log.DebugFormat(
  152. // "[ARCHIVER]: Wearable asset {0} references {1} assets", wearableAssetUuid, wearableAsset.Textures.Count);
  153. foreach (UUID uuid in wearableAsset.Textures.Values)
  154. {
  155. //m_log.DebugFormat("[ARCHIVER]: Got bodypart uuid {0}", uuid);
  156. assetUuids[uuid] = 1;
  157. }
  158. }
  159. /// <summary>
  160. /// Get all the asset uuids associated with a given object. This includes both those directly associated with
  161. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  162. /// within this object).
  163. /// </summary>
  164. /// <param name="sceneObject"></param>
  165. /// <param name="assetUuids"></param>
  166. protected void GetSceneObjectAssetUuids(UUID sceneObjectUuid, IDictionary<UUID, int> assetUuids)
  167. {
  168. AssetBase objectAsset = GetAsset(sceneObjectUuid);
  169. if (null != objectAsset)
  170. {
  171. string xml = Utils.BytesToString(objectAsset.Data);
  172. SceneObjectGroup sog = new SceneObjectGroup(xml, true);
  173. GetSceneObjectAssetUuids(sog, assetUuids);
  174. }
  175. }
  176. /// <summary>
  177. /// Get all the asset uuids associated with a given object. This includes both those directly associated with
  178. /// it (e.g. face textures) and recursively, those of items within it's inventory (e.g. objects contained
  179. /// within this object).
  180. /// </summary>
  181. /// <param name="sceneObject"></param>
  182. /// <param name="assetUuids"></param>
  183. protected void GetSceneObjectAssetUuids(SceneObjectGroup sceneObject, IDictionary<UUID, int> assetUuids)
  184. {
  185. m_log.DebugFormat(
  186. "[ARCHIVER]: Getting assets for object {0}, {1}", sceneObject.Name, sceneObject.UUID);
  187. foreach (SceneObjectPart part in sceneObject.GetParts())
  188. {
  189. //m_log.DebugFormat(
  190. // "[ARCHIVER]: Getting part {0}, {1} for object {2}", part.Name, part.UUID, sceneObject.UUID);
  191. try
  192. {
  193. Primitive.TextureEntry textureEntry = part.Shape.Textures;
  194. // Get the prim's default texture. This will be used for faces which don't have their own texture
  195. assetUuids[textureEntry.DefaultTexture.TextureID] = 1;
  196. // XXX: Not a great way to iterate through face textures, but there's no
  197. // other method available to tell how many faces there actually are
  198. //int i = 0;
  199. foreach (Primitive.TextureEntryFace texture in textureEntry.FaceTextures)
  200. {
  201. if (texture != null)
  202. {
  203. //m_log.DebugFormat("[ARCHIVER]: Got face {0}", i++);
  204. assetUuids[texture.TextureID] = 1;
  205. }
  206. }
  207. // If the prim is a sculpt then preserve this information too
  208. if (part.Shape.SculptTexture != UUID.Zero)
  209. assetUuids[part.Shape.SculptTexture] = 1;
  210. // Now analyze this prim's inventory items to preserve all the uuids that they reference
  211. foreach (TaskInventoryItem tii in part.TaskInventory.Values)
  212. {
  213. //m_log.DebugFormat("[ARCHIVER]: Analysing item asset type {0}", tii.Type);
  214. if (!assetUuids.ContainsKey(tii.AssetID))
  215. {
  216. assetUuids[tii.AssetID] = 1;
  217. if ((int)AssetType.Bodypart == tii.Type || ((int)AssetType.Clothing == tii.Type))
  218. {
  219. GetWearableAssetUuids(tii.AssetID, assetUuids);
  220. }
  221. else if ((int)AssetType.LSLText == tii.Type)
  222. {
  223. GetScriptAssetUuids(tii.AssetID, assetUuids);
  224. }
  225. else if ((int)AssetType.Object == tii.Type)
  226. {
  227. GetSceneObjectAssetUuids(tii.AssetID, assetUuids);
  228. }
  229. //else
  230. //{
  231. //m_log.DebugFormat("[ARCHIVER]: Recording asset {0} in object {1}", tii.AssetID, part.UUID);
  232. //}
  233. }
  234. }
  235. }
  236. catch (Exception e)
  237. {
  238. m_log.ErrorFormat("[ARCHIVER]: Failed to get part - {0}", e);
  239. m_log.DebugFormat("[ARCHIVER]: Texture entry length for prim was {0} (min is 46)", part.Shape.TextureEntry.Length);
  240. }
  241. }
  242. }
  243. /// <summary>
  244. /// Archive the region requested.
  245. /// </summary>
  246. /// <exception cref="System.IO.IOException">if there was an io problem with creating the file</exception>
  247. public void ArchiveRegion()
  248. {
  249. Dictionary<UUID, int> assetUuids = new Dictionary<UUID, int>();
  250. List<EntityBase> entities = m_scene.GetEntities();
  251. List<SceneObjectGroup> sceneObjects = new List<SceneObjectGroup>();
  252. // Filter entities so that we only have scene objects.
  253. // FIXME: Would be nicer to have this as a proper list in SceneGraph, since lots of methods
  254. // end up having to do this
  255. foreach (EntityBase entity in entities)
  256. {
  257. if (entity is SceneObjectGroup)
  258. {
  259. SceneObjectGroup sceneObject = (SceneObjectGroup)entity;
  260. if (!sceneObject.IsDeleted && !sceneObject.IsAttachment)
  261. sceneObjects.Add((SceneObjectGroup)entity);
  262. }
  263. }
  264. foreach (SceneObjectGroup sceneObject in sceneObjects)
  265. {
  266. GetSceneObjectAssetUuids(sceneObject, assetUuids);
  267. }
  268. m_log.DebugFormat(
  269. "[ARCHIVER]: {0} scene objects to serialize requiring save of {1} assets",
  270. sceneObjects.Count, assetUuids.Count);
  271. // Make sure that we also request terrain texture assets
  272. RegionSettings regionSettings = m_scene.RegionInfo.RegionSettings;
  273. if (regionSettings.TerrainTexture1 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_1)
  274. assetUuids[regionSettings.TerrainTexture1] = 1;
  275. if (regionSettings.TerrainTexture2 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_2)
  276. assetUuids[regionSettings.TerrainTexture2] = 1;
  277. if (regionSettings.TerrainTexture3 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_3)
  278. assetUuids[regionSettings.TerrainTexture3] = 1;
  279. if (regionSettings.TerrainTexture4 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_4)
  280. assetUuids[regionSettings.TerrainTexture4] = 1;
  281. // Asynchronously request all the assets required to perform this archive operation
  282. ArchiveWriteRequestExecution awre
  283. = new ArchiveWriteRequestExecution(
  284. sceneObjects,
  285. m_scene.RequestModuleInterface<ITerrainModule>(),
  286. m_scene.RequestModuleInterface<IRegionSerialiserModule>(),
  287. m_scene.RegionInfo,
  288. m_saveStream);
  289. new AssetsRequest(assetUuids.Keys, m_scene.AssetCache, awre.ReceivedAllAssets).Execute();
  290. }
  291. }
  292. }