ArchiveWriteRequestExecution.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. public ArchiveWriteRequestExecution(
  60. List<SceneObjectGroup> sceneObjects,
  61. ITerrainModule terrainModule,
  62. IRegionSerialiserModule serialiser,
  63. Scene scene,
  64. TarArchiveWriter archiveWriter,
  65. Guid requestId)
  66. {
  67. m_sceneObjects = sceneObjects;
  68. m_terrainModule = terrainModule;
  69. m_serialiser = serialiser;
  70. m_scene = scene;
  71. m_archiveWriter = archiveWriter;
  72. m_requestId = requestId;
  73. }
  74. protected internal void ReceivedAllAssets(
  75. ICollection<UUID> assetsFoundUuids, ICollection<UUID> assetsNotFoundUuids)
  76. {
  77. try
  78. {
  79. Save(assetsFoundUuids, assetsNotFoundUuids);
  80. }
  81. finally
  82. {
  83. m_archiveWriter.Close();
  84. }
  85. m_log.InfoFormat("[ARCHIVER]: Finished writing out OAR for {0}", m_scene.RegionInfo.RegionName);
  86. m_scene.EventManager.TriggerOarFileSaved(m_requestId, String.Empty);
  87. }
  88. protected internal void Save(ICollection<UUID> assetsFoundUuids, ICollection<UUID> assetsNotFoundUuids)
  89. {
  90. foreach (UUID uuid in assetsNotFoundUuids)
  91. {
  92. m_log.DebugFormat("[ARCHIVER]: Could not find asset {0}", uuid);
  93. }
  94. // m_log.InfoFormat(
  95. // "[ARCHIVER]: Received {0} of {1} assets requested",
  96. // assetsFoundUuids.Count, assetsFoundUuids.Count + assetsNotFoundUuids.Count);
  97. m_log.InfoFormat("[ARCHIVER]: Creating archive file. This may take some time.");
  98. // Write out control file
  99. m_archiveWriter.WriteFile(ArchiveConstants.CONTROL_FILE_PATH, Create0p2ControlFile());
  100. m_log.InfoFormat("[ARCHIVER]: Added control file to archive.");
  101. // Write out region settings
  102. string settingsPath
  103. = String.Format("{0}{1}.xml", ArchiveConstants.SETTINGS_PATH, m_scene.RegionInfo.RegionName);
  104. m_archiveWriter.WriteFile(settingsPath, RegionSettingsSerializer.Serialize(m_scene.RegionInfo.RegionSettings));
  105. m_log.InfoFormat("[ARCHIVER]: Added region settings to archive.");
  106. // Write out land data (aka parcel) settings
  107. List<ILandObject>landObjects = m_scene.LandChannel.AllParcels();
  108. foreach (ILandObject lo in landObjects)
  109. {
  110. LandData landData = lo.LandData;
  111. string landDataPath = String.Format("{0}{1}.xml", ArchiveConstants.LANDDATA_PATH,
  112. landData.GlobalID.ToString());
  113. m_archiveWriter.WriteFile(landDataPath, LandDataSerializer.Serialize(landData));
  114. }
  115. m_log.InfoFormat("[ARCHIVER]: Added parcel settings to archive.");
  116. // Write out terrain
  117. string terrainPath
  118. = String.Format("{0}{1}.r32", ArchiveConstants.TERRAINS_PATH, m_scene.RegionInfo.RegionName);
  119. MemoryStream ms = new MemoryStream();
  120. m_terrainModule.SaveToStream(terrainPath, ms);
  121. m_archiveWriter.WriteFile(terrainPath, ms.ToArray());
  122. ms.Close();
  123. m_log.InfoFormat("[ARCHIVER]: Added terrain information to archive.");
  124. // Write out scene object metadata
  125. foreach (SceneObjectGroup sceneObject in m_sceneObjects)
  126. {
  127. //m_log.DebugFormat("[ARCHIVER]: Saving {0} {1}, {2}", entity.Name, entity.UUID, entity.GetType());
  128. Vector3 position = sceneObject.AbsolutePosition;
  129. string serializedObject = m_serialiser.SerializeGroupToXml2(sceneObject);
  130. string filename
  131. = string.Format(
  132. "{0}{1}_{2:000}-{3:000}-{4:000}__{5}.xml",
  133. ArchiveConstants.OBJECTS_PATH, sceneObject.Name,
  134. Math.Round(position.X), Math.Round(position.Y), Math.Round(position.Z),
  135. sceneObject.UUID);
  136. m_archiveWriter.WriteFile(filename, serializedObject);
  137. }
  138. m_log.InfoFormat("[ARCHIVER]: Added scene objects to archive.");
  139. }
  140. /// <summary>
  141. /// Create the control file for a 0.2 version archive
  142. /// </summary>
  143. /// <returns></returns>
  144. public static string Create0p2ControlFile()
  145. {
  146. StringWriter sw = new StringWriter();
  147. XmlTextWriter xtw = new XmlTextWriter(sw);
  148. xtw.Formatting = Formatting.Indented;
  149. xtw.WriteStartDocument();
  150. xtw.WriteStartElement("archive");
  151. xtw.WriteAttributeString("major_version", "0");
  152. xtw.WriteAttributeString("minor_version", "3");
  153. xtw.WriteStartElement("creation_info");
  154. DateTime now = DateTime.UtcNow;
  155. TimeSpan t = now - new DateTime(1970, 1, 1);
  156. xtw.WriteElementString("datetime", ((int)t.TotalSeconds).ToString());
  157. xtw.WriteElementString("id", UUID.Random().ToString());
  158. xtw.WriteEndElement();
  159. xtw.WriteEndElement();
  160. xtw.Flush();
  161. xtw.Close();
  162. String s = sw.ToString();
  163. sw.Close();
  164. return s;
  165. }
  166. }
  167. }