ArchiveWriteRequestPreparation.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.IO.Compression;
  31. using System.Reflection;
  32. using System.Text.RegularExpressions;
  33. using System.Threading;
  34. using log4net;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Serialization;
  38. using OpenSim.Region.CoreModules.World.Terrain;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Framework.Scenes;
  41. namespace OpenSim.Region.CoreModules.World.Archiver
  42. {
  43. /// <summary>
  44. /// Prepare to write out an archive.
  45. /// </summary>
  46. public class ArchiveWriteRequestPreparation
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. protected Scene m_scene;
  50. protected Stream m_saveStream;
  51. protected Guid m_requestId;
  52. /// <summary>
  53. /// Constructor
  54. /// </summary>
  55. /// <param name="scene"></param>
  56. /// <param name="savePath">The path to which to save data.</param>
  57. /// <param name="requestId">The id associated with this request</param>
  58. /// <exception cref="System.IO.IOException">
  59. /// If there was a problem opening a stream for the file specified by the savePath
  60. /// </exception>
  61. public ArchiveWriteRequestPreparation(Scene scene, string savePath, Guid requestId)
  62. {
  63. m_scene = scene;
  64. try
  65. {
  66. m_saveStream = new GZipStream(new FileStream(savePath, FileMode.Create), CompressionMode.Compress);
  67. }
  68. catch (EntryPointNotFoundException e)
  69. {
  70. m_log.ErrorFormat(
  71. "[ARCHIVER]: Mismatch between Mono and zlib1g library version when trying to create compression stream."
  72. + "If you've manually installed Mono, have you appropriately updated zlib1g as well?");
  73. m_log.Error(e);
  74. }
  75. m_requestId = requestId;
  76. }
  77. /// <summary>
  78. /// Constructor.
  79. /// </summary>
  80. /// <param name="scene"></param>
  81. /// <param name="saveStream">The stream to which to save data.</param>
  82. /// <param name="requestId">The id associated with this request</param>
  83. public ArchiveWriteRequestPreparation(Scene scene, Stream saveStream, Guid requestId)
  84. {
  85. m_scene = scene;
  86. m_saveStream = saveStream;
  87. m_requestId = requestId;
  88. }
  89. /// <summary>
  90. /// Archive the region requested.
  91. /// </summary>
  92. /// <exception cref="System.IO.IOException">if there was an io problem with creating the file</exception>
  93. public void ArchiveRegion()
  94. {
  95. Dictionary<UUID, AssetType> assetUuids = new Dictionary<UUID, AssetType>();
  96. EntityBase[] entities = m_scene.GetEntities();
  97. List<SceneObjectGroup> sceneObjects = new List<SceneObjectGroup>();
  98. /*
  99. foreach (ILandObject lo in m_scene.LandChannel.AllParcels())
  100. {
  101. if (name == lo.LandData.Name)
  102. {
  103. // This is the parcel we want
  104. }
  105. }
  106. */
  107. // Filter entities so that we only have scene objects.
  108. // FIXME: Would be nicer to have this as a proper list in SceneGraph, since lots of methods
  109. // end up having to do this
  110. foreach (EntityBase entity in entities)
  111. {
  112. if (entity is SceneObjectGroup)
  113. {
  114. SceneObjectGroup sceneObject = (SceneObjectGroup)entity;
  115. if (!sceneObject.IsDeleted && !sceneObject.IsAttachment)
  116. sceneObjects.Add((SceneObjectGroup)entity);
  117. }
  118. }
  119. UuidGatherer assetGatherer = new UuidGatherer(m_scene.AssetService);
  120. foreach (SceneObjectGroup sceneObject in sceneObjects)
  121. {
  122. assetGatherer.GatherAssetUuids(sceneObject, assetUuids);
  123. }
  124. m_log.DebugFormat(
  125. "[ARCHIVER]: {0} scene objects to serialize requiring save of {1} assets",
  126. sceneObjects.Count, assetUuids.Count);
  127. // Make sure that we also request terrain texture assets
  128. RegionSettings regionSettings = m_scene.RegionInfo.RegionSettings;
  129. if (regionSettings.TerrainTexture1 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_1)
  130. assetUuids[regionSettings.TerrainTexture1] = AssetType.Texture;
  131. if (regionSettings.TerrainTexture2 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_2)
  132. assetUuids[regionSettings.TerrainTexture2] = AssetType.Texture;
  133. if (regionSettings.TerrainTexture3 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_3)
  134. assetUuids[regionSettings.TerrainTexture3] = AssetType.Texture;
  135. if (regionSettings.TerrainTexture4 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_4)
  136. assetUuids[regionSettings.TerrainTexture4] = AssetType.Texture;
  137. TarArchiveWriter archiveWriter = new TarArchiveWriter(m_saveStream);
  138. // Asynchronously request all the assets required to perform this archive operation
  139. ArchiveWriteRequestExecution awre
  140. = new ArchiveWriteRequestExecution(
  141. sceneObjects,
  142. m_scene.RequestModuleInterface<ITerrainModule>(),
  143. m_scene.RequestModuleInterface<IRegionSerialiserModule>(),
  144. m_scene,
  145. archiveWriter,
  146. m_requestId);
  147. new AssetsRequest(
  148. new AssetsArchiver(archiveWriter), assetUuids,
  149. m_scene.AssetService, awre.ReceivedAllAssets).Execute();
  150. }
  151. }
  152. }