ArchiveWriteRequestExecution.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.Xml;
  33. using libsecondlife;
  34. using log4net;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Environment.Interfaces;
  37. using OpenSim.Region.Environment.Modules.World.Serialiser;
  38. using OpenSim.Region.Environment.Modules.World.Terrain;
  39. using OpenSim.Region.Environment.Scenes;
  40. namespace OpenSim.Region.Environment.Modules.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(IDictionary<LLUUID, AssetBase> assets);
  46. /// <summary>
  47. /// Execute the write of an archive once we have received all the necessary data
  48. /// </summary>
  49. public class ArchiveWriteRequestExecution
  50. {
  51. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. protected ITerrainModule m_terrainModule;
  53. protected IRegionSerialiser m_serialiser;
  54. protected List<SceneObjectGroup> m_sceneObjects;
  55. protected string m_sceneName;
  56. protected string m_savePath;
  57. public ArchiveWriteRequestExecution(
  58. List<SceneObjectGroup> sceneObjects,
  59. ITerrainModule terrainModule,
  60. IRegionSerialiser serialiser,
  61. string sceneName,
  62. string savePath)
  63. {
  64. m_sceneObjects = sceneObjects;
  65. m_terrainModule = terrainModule;
  66. m_serialiser = serialiser;
  67. m_sceneName = sceneName;
  68. m_savePath = savePath;
  69. }
  70. protected internal void ReceivedAllAssets(IDictionary<LLUUID, AssetBase> assets)
  71. {
  72. m_log.DebugFormat("[ARCHIVER]: Received all {0} assets required", assets.Count);
  73. TarArchiveWriter archive = new TarArchiveWriter();
  74. // Write out control file
  75. archive.AddFile(ArchiveConstants.CONTROL_FILE_PATH, CreateControlFile());
  76. // Write out terrain
  77. string terrainPath = String.Format("{0}{1}.r32", ArchiveConstants.TERRAINS_PATH, m_sceneName);
  78. MemoryStream ms = new MemoryStream();
  79. m_terrainModule.SaveToStream(terrainPath, ms);
  80. archive.AddFile(terrainPath, ms.ToArray());
  81. ms.Close();
  82. // Write out scene object metadata
  83. foreach (SceneObjectGroup sceneObject in m_sceneObjects)
  84. {
  85. //m_log.DebugFormat("[ARCHIVER]: Saving {0} {1}, {2}", entity.Name, entity.UUID, entity.GetType());
  86. LLVector3 position = sceneObject.AbsolutePosition;
  87. string serializedObject = m_serialiser.SaveGroupToXml2(sceneObject);
  88. string filename
  89. = string.Format(
  90. "{0}{1}_{2:000}-{3:000}-{4:000}__{5}.xml",
  91. ArchiveConstants.OBJECTS_PATH, sceneObject.Name,
  92. Math.Round(position.X), Math.Round(position.Y), Math.Round(position.Z),
  93. sceneObject.UUID);
  94. archive.AddFile(filename, serializedObject);
  95. }
  96. // Write out assets
  97. AssetsArchiver assetsArchiver = new AssetsArchiver(assets);
  98. assetsArchiver.Archive(archive);
  99. archive.WriteTar(new GZipStream(new FileStream(m_savePath, FileMode.Create), CompressionMode.Compress));
  100. m_log.InfoFormat("[ARCHIVER]: Wrote out OpenSimulator archive {0}", m_savePath);
  101. }
  102. /// <summary>
  103. /// Create the control file for this archive
  104. /// </summary>
  105. /// <returns></returns>
  106. protected string CreateControlFile()
  107. {
  108. StringWriter sw = new StringWriter();
  109. XmlTextWriter xtw = new XmlTextWriter(sw);
  110. xtw.Formatting = Formatting.Indented;
  111. xtw.WriteStartDocument();
  112. xtw.WriteStartElement("archive");
  113. xtw.WriteAttributeString("major_version", "0");
  114. xtw.WriteAttributeString("minor_version", "1");
  115. xtw.WriteEndElement();
  116. xtw.Flush();
  117. xtw.Close();
  118. String s = sw.ToString();
  119. sw.Close();
  120. return s;
  121. }
  122. }
  123. }