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