ArchiveWriteRequestExecution.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.Xml;
  32. using log4net;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Serialization;
  36. using OpenSim.Framework.Serialization.External;
  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. /// Method called when all the necessary assets for an archive request have been received.
  44. /// </summary>
  45. public delegate void AssetsRequestCallback(
  46. ICollection<UUID> assetsFoundUuids, ICollection<UUID> assetsNotFoundUuids);
  47. /// <summary>
  48. /// Execute the write of an archive once we have received all the necessary data
  49. /// </summary>
  50. public class ArchiveWriteRequestExecution
  51. {
  52. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. protected ITerrainModule m_terrainModule;
  54. protected IRegionSerialiserModule m_serialiser;
  55. protected List<SceneObjectGroup> m_sceneObjects;
  56. protected Scene m_scene;
  57. protected TarArchiveWriter m_archiveWriter;
  58. protected Guid m_requestId;
  59. protected Dictionary<string, object> m_options;
  60. public ArchiveWriteRequestExecution(
  61. List<SceneObjectGroup> sceneObjects,
  62. ITerrainModule terrainModule,
  63. IRegionSerialiserModule serialiser,
  64. Scene scene,
  65. TarArchiveWriter archiveWriter,
  66. Guid requestId,
  67. Dictionary<string, object> options)
  68. {
  69. m_sceneObjects = sceneObjects;
  70. m_terrainModule = terrainModule;
  71. m_serialiser = serialiser;
  72. m_scene = scene;
  73. m_archiveWriter = archiveWriter;
  74. m_requestId = requestId;
  75. m_options = options;
  76. }
  77. protected internal void ReceivedAllAssets(
  78. ICollection<UUID> assetsFoundUuids, ICollection<UUID> assetsNotFoundUuids)
  79. {
  80. try
  81. {
  82. Save(assetsFoundUuids, assetsNotFoundUuids);
  83. }
  84. finally
  85. {
  86. m_archiveWriter.Close();
  87. }
  88. m_log.InfoFormat("[ARCHIVER]: Finished writing out OAR for {0}", m_scene.RegionInfo.RegionName);
  89. m_scene.EventManager.TriggerOarFileSaved(m_requestId, String.Empty);
  90. }
  91. protected internal void Save(ICollection<UUID> assetsFoundUuids, ICollection<UUID> assetsNotFoundUuids)
  92. {
  93. foreach (UUID uuid in assetsNotFoundUuids)
  94. {
  95. m_log.DebugFormat("[ARCHIVER]: Could not find asset {0}", uuid);
  96. }
  97. // m_log.InfoFormat(
  98. // "[ARCHIVER]: Received {0} of {1} assets requested",
  99. // assetsFoundUuids.Count, assetsFoundUuids.Count + assetsNotFoundUuids.Count);
  100. // Write out region settings
  101. string settingsPath
  102. = String.Format("{0}{1}.xml", ArchiveConstants.SETTINGS_PATH, m_scene.RegionInfo.RegionName);
  103. m_archiveWriter.WriteFile(settingsPath, RegionSettingsSerializer.Serialize(m_scene.RegionInfo.RegionSettings));
  104. m_log.InfoFormat("[ARCHIVER]: Added region settings to archive.");
  105. // Write out land data (aka parcel) settings
  106. List<ILandObject>landObjects = m_scene.LandChannel.AllParcels();
  107. foreach (ILandObject lo in landObjects)
  108. {
  109. LandData landData = lo.LandData;
  110. string landDataPath = String.Format("{0}{1}.xml", ArchiveConstants.LANDDATA_PATH,
  111. landData.GlobalID.ToString());
  112. m_archiveWriter.WriteFile(landDataPath, LandDataSerializer.Serialize(landData));
  113. }
  114. m_log.InfoFormat("[ARCHIVER]: Added parcel settings to archive.");
  115. // Write out terrain
  116. string terrainPath
  117. = String.Format("{0}{1}.r32", ArchiveConstants.TERRAINS_PATH, m_scene.RegionInfo.RegionName);
  118. MemoryStream ms = new MemoryStream();
  119. m_terrainModule.SaveToStream(terrainPath, ms);
  120. m_archiveWriter.WriteFile(terrainPath, ms.ToArray());
  121. ms.Close();
  122. m_log.InfoFormat("[ARCHIVER]: Added terrain information to archive.");
  123. // Write out scene object metadata
  124. foreach (SceneObjectGroup sceneObject in m_sceneObjects)
  125. {
  126. //m_log.DebugFormat("[ARCHIVER]: Saving {0} {1}, {2}", entity.Name, entity.UUID, entity.GetType());
  127. string serializedObject = m_serialiser.SerializeGroupToXml2(sceneObject, m_options);
  128. m_archiveWriter.WriteFile(ArchiveHelpers.CreateObjectPath(sceneObject), serializedObject);
  129. }
  130. m_log.InfoFormat("[ARCHIVER]: Added scene objects to archive.");
  131. }
  132. }
  133. }