ArchiveWriteRequestPreparation.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 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.Region.CoreModules.World.Terrain;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  40. namespace OpenSim.Region.CoreModules.World.Archiver
  41. {
  42. /// <summary>
  43. /// Prepare to write out an archive.
  44. /// </summary>
  45. public class ArchiveWriteRequestPreparation
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. protected Scene m_scene;
  49. protected Stream m_saveStream;
  50. /// <summary>
  51. /// Constructor
  52. /// </summary>
  53. public ArchiveWriteRequestPreparation(Scene scene, string savePath)
  54. {
  55. m_scene = scene;
  56. m_saveStream = new GZipStream(new FileStream(savePath, FileMode.Create), CompressionMode.Compress);
  57. }
  58. /// <summary>
  59. /// Constructor.
  60. /// </summary>
  61. /// <param name="scene"></param>
  62. /// <param name="saveStream">The stream to which to save data.</param>
  63. public ArchiveWriteRequestPreparation(Scene scene, Stream saveStream)
  64. {
  65. m_scene = scene;
  66. m_saveStream = saveStream;
  67. }
  68. /// <summary>
  69. /// Archive the region requested.
  70. /// </summary>
  71. /// <exception cref="System.IO.IOException">if there was an io problem with creating the file</exception>
  72. public void ArchiveRegion()
  73. {
  74. Dictionary<UUID, int> assetUuids = new Dictionary<UUID, int>();
  75. List<EntityBase> entities = m_scene.GetEntities();
  76. List<SceneObjectGroup> sceneObjects = new List<SceneObjectGroup>();
  77. // Filter entities so that we only have scene objects.
  78. // FIXME: Would be nicer to have this as a proper list in SceneGraph, since lots of methods
  79. // end up having to do this
  80. foreach (EntityBase entity in entities)
  81. {
  82. if (entity is SceneObjectGroup)
  83. {
  84. SceneObjectGroup sceneObject = (SceneObjectGroup)entity;
  85. if (!sceneObject.IsDeleted && !sceneObject.IsAttachment)
  86. sceneObjects.Add((SceneObjectGroup)entity);
  87. }
  88. }
  89. UuidGatherer assetGatherer = new UuidGatherer(m_scene.CommsManager.AssetCache);
  90. foreach (SceneObjectGroup sceneObject in sceneObjects)
  91. {
  92. assetGatherer.GatherAssetUuids(sceneObject, assetUuids);
  93. }
  94. m_log.DebugFormat(
  95. "[ARCHIVER]: {0} scene objects to serialize requiring save of {1} assets",
  96. sceneObjects.Count, assetUuids.Count);
  97. // Make sure that we also request terrain texture assets
  98. RegionSettings regionSettings = m_scene.RegionInfo.RegionSettings;
  99. if (regionSettings.TerrainTexture1 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_1)
  100. assetUuids[regionSettings.TerrainTexture1] = 1;
  101. if (regionSettings.TerrainTexture2 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_2)
  102. assetUuids[regionSettings.TerrainTexture2] = 1;
  103. if (regionSettings.TerrainTexture3 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_3)
  104. assetUuids[regionSettings.TerrainTexture3] = 1;
  105. if (regionSettings.TerrainTexture4 != RegionSettings.DEFAULT_TERRAIN_TEXTURE_4)
  106. assetUuids[regionSettings.TerrainTexture4] = 1;
  107. // Asynchronously request all the assets required to perform this archive operation
  108. ArchiveWriteRequestExecution awre
  109. = new ArchiveWriteRequestExecution(
  110. sceneObjects,
  111. m_scene.RequestModuleInterface<ITerrainModule>(),
  112. m_scene.RequestModuleInterface<IRegionSerialiserModule>(),
  113. m_scene,
  114. m_saveStream);
  115. new AssetsRequest(assetUuids.Keys, m_scene.CommsManager.AssetCache, awre.ReceivedAllAssets).Execute();
  116. }
  117. }
  118. }