ArchiveWriteRequestExecution.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.Reflection;
  31. using System.Xml;
  32. using log4net;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.CoreModules.World.Terrain;
  36. using OpenSim.Region.Framework.Interfaces;
  37. using OpenSim.Region.Framework.Scenes;
  38. namespace OpenSim.Region.CoreModules.World.Archiver
  39. {
  40. /// <summary>
  41. /// Method called when all the necessary assets for an archive request have been received.
  42. /// </summary>
  43. public delegate void AssetsRequestCallback(IDictionary<UUID, AssetBase> assetsFound, ICollection<UUID> assetsNotFoundUuids);
  44. /// <summary>
  45. /// Execute the write of an archive once we have received all the necessary data
  46. /// </summary>
  47. public class ArchiveWriteRequestExecution
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. protected ITerrainModule m_terrainModule;
  51. protected IRegionSerialiserModule m_serialiser;
  52. protected List<SceneObjectGroup> m_sceneObjects;
  53. protected Scene m_scene;
  54. protected Stream m_saveStream;
  55. public ArchiveWriteRequestExecution(
  56. List<SceneObjectGroup> sceneObjects,
  57. ITerrainModule terrainModule,
  58. IRegionSerialiserModule serialiser,
  59. Scene scene,
  60. Stream saveStream)
  61. {
  62. m_sceneObjects = sceneObjects;
  63. m_terrainModule = terrainModule;
  64. m_serialiser = serialiser;
  65. m_scene = scene;
  66. m_saveStream = saveStream;
  67. }
  68. protected internal void ReceivedAllAssets(
  69. IDictionary<UUID, AssetBase> assetsFound, ICollection<UUID> assetsNotFoundUuids)
  70. {
  71. foreach (UUID uuid in assetsNotFoundUuids)
  72. {
  73. m_log.DebugFormat("[ARCHIVER]: Could not find asset {0}", uuid);
  74. }
  75. m_log.InfoFormat(
  76. "[ARCHIVER]: Received {0} of {1} assets requested",
  77. assetsFound.Count, assetsFound.Count + assetsNotFoundUuids.Count);
  78. m_log.InfoFormat("[ARCHIVER]: Creating archive file. This may take some time.");
  79. TarArchiveWriter archive = new TarArchiveWriter(m_saveStream);
  80. // Write out control file
  81. archive.WriteFile(ArchiveConstants.CONTROL_FILE_PATH, Create0p2ControlFile());
  82. m_log.InfoFormat("[ARCHIVER]: Added control file to archive.");
  83. // Write out region settings
  84. string settingsPath
  85. = String.Format("{0}{1}.xml", ArchiveConstants.SETTINGS_PATH, m_scene.RegionInfo.RegionName);
  86. archive.WriteFile(settingsPath, RegionSettingsSerializer.Serialize(m_scene.RegionInfo.RegionSettings));
  87. m_log.InfoFormat("[ARCHIVER]: Added region settings to archive.");
  88. // Write out terrain
  89. string terrainPath
  90. = String.Format("{0}{1}.r32", ArchiveConstants.TERRAINS_PATH, m_scene.RegionInfo.RegionName);
  91. MemoryStream ms = new MemoryStream();
  92. m_terrainModule.SaveToStream(terrainPath, ms);
  93. archive.WriteFile(terrainPath, ms.ToArray());
  94. ms.Close();
  95. m_log.InfoFormat("[ARCHIVER]: Added terrain information to archive.");
  96. // Write out scene object metadata
  97. foreach (SceneObjectGroup sceneObject in m_sceneObjects)
  98. {
  99. //m_log.DebugFormat("[ARCHIVER]: Saving {0} {1}, {2}", entity.Name, entity.UUID, entity.GetType());
  100. Vector3 position = sceneObject.AbsolutePosition;
  101. string serializedObject = m_serialiser.SaveGroupToXml2(sceneObject);
  102. string filename
  103. = string.Format(
  104. "{0}{1}_{2:000}-{3:000}-{4:000}__{5}.xml",
  105. ArchiveConstants.OBJECTS_PATH, sceneObject.Name,
  106. Math.Round(position.X), Math.Round(position.Y), Math.Round(position.Z),
  107. sceneObject.UUID);
  108. archive.WriteFile(filename, serializedObject);
  109. }
  110. m_log.InfoFormat("[ARCHIVER]: Added scene objects to archive.");
  111. // Write out assets
  112. AssetsArchiver assetsArchiver = new AssetsArchiver(assetsFound);
  113. assetsArchiver.Archive(archive);
  114. archive.Close();
  115. m_log.InfoFormat("[ARCHIVER]: Wrote out OpenSimulator archive for {0}", m_scene.RegionInfo.RegionName);
  116. m_scene.EventManager.TriggerOarFileSaved(String.Empty);
  117. }
  118. /// <summary>
  119. /// Create the control file for a 0.2 version archive
  120. /// </summary>
  121. /// <returns></returns>
  122. public static string Create0p2ControlFile()
  123. {
  124. StringWriter sw = new StringWriter();
  125. XmlTextWriter xtw = new XmlTextWriter(sw);
  126. xtw.Formatting = Formatting.Indented;
  127. xtw.WriteStartDocument();
  128. xtw.WriteStartElement("archive");
  129. xtw.WriteAttributeString("major_version", "0");
  130. xtw.WriteAttributeString("minor_version", "2");
  131. xtw.WriteEndElement();
  132. xtw.Flush();
  133. xtw.Close();
  134. String s = sw.ToString();
  135. sw.Close();
  136. return s;
  137. }
  138. }
  139. }