ArchiveWriteRequestPreparation.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. public ArchiveWriteRequestPreparation(Scene scene, string savePath, Guid requestId)
  56. {
  57. m_scene = scene;
  58. m_saveStream = new GZipStream(new FileStream(savePath, FileMode.Create), CompressionMode.Compress);
  59. m_requestId = requestId;
  60. }
  61. /// <summary>
  62. /// Constructor.
  63. /// </summary>
  64. /// <param name="scene"></param>
  65. /// <param name="saveStream">The stream to which to save data.</param>
  66. /// <param name="requestId">The id associated with this request</param>
  67. public ArchiveWriteRequestPreparation(Scene scene, Stream saveStream, Guid requestId)
  68. {
  69. m_scene = scene;
  70. m_saveStream = saveStream;
  71. m_requestId = requestId;
  72. }
  73. /// <summary>
  74. /// Archive the region requested.
  75. /// </summary>
  76. /// <exception cref="System.IO.IOException">if there was an io problem with creating the file</exception>
  77. public void ArchiveRegion()
  78. {
  79. Dictionary<UUID, int> assetUuids = new Dictionary<UUID, int>();
  80. List<EntityBase> entities = m_scene.GetEntities();
  81. List<SceneObjectGroup> sceneObjects = new List<SceneObjectGroup>();
  82. // Filter entities so that we only have scene objects.
  83. // FIXME: Would be nicer to have this as a proper list in SceneGraph, since lots of methods
  84. // end up having to do this
  85. foreach (EntityBase entity in entities)
  86. {
  87. if (entity is SceneObjectGroup)
  88. {
  89. SceneObjectGroup sceneObject = (SceneObjectGroup)entity;
  90. if (!sceneObject.IsDeleted && !sceneObject.IsAttachment)
  91. sceneObjects.Add((SceneObjectGroup)entity);
  92. }
  93. }
  94. UuidGatherer assetGatherer = new UuidGatherer(m_scene.AssetService);
  95. foreach (SceneObjectGroup sceneObject in sceneObjects)
  96. {
  97. assetGatherer.GatherAssetUuids(sceneObject, assetUuids);
  98. }
  99. m_log.DebugFormat(
  100. "[ARCHIVER]: {0} scene objects to serialize requiring save of {1} assets",
  101. sceneObjects.Count, assetUuids.Count);
  102. // Make sure that we also request terrain texture assets
  103. RegionSettings regionSettings = m_scene.RegionInfo.RegionSettings;
  104. if (regionSettings.TerrainTexture1 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_1)
  105. assetUuids[regionSettings.TerrainTexture1] = 1;
  106. if (regionSettings.TerrainTexture2 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_2)
  107. assetUuids[regionSettings.TerrainTexture2] = 1;
  108. if (regionSettings.TerrainTexture3 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_3)
  109. assetUuids[regionSettings.TerrainTexture3] = 1;
  110. if (regionSettings.TerrainTexture4 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_4)
  111. assetUuids[regionSettings.TerrainTexture4] = 1;
  112. TarArchiveWriter archiveWriter = new TarArchiveWriter(m_saveStream);
  113. // Asynchronously request all the assets required to perform this archive operation
  114. ArchiveWriteRequestExecution awre
  115. = new ArchiveWriteRequestExecution(
  116. sceneObjects,
  117. m_scene.RequestModuleInterface<ITerrainModule>(),
  118. m_scene.RequestModuleInterface<IRegionSerialiserModule>(),
  119. m_scene,
  120. archiveWriter,
  121. m_requestId);
  122. new AssetsRequest(
  123. new AssetsArchiver(archiveWriter), assetUuids.Keys,
  124. m_scene.AssetService, awre.ReceivedAllAssets).Execute();
  125. }
  126. }
  127. }