OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
SerialiseObjects.cs
Go to the documentation of this file.
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 
28 using System.Collections.Generic;
29 using System.IO;
30 using System.IO.Compression;
31 using System.Text;
32 using System.Xml;
33 using OpenSim.Region.Framework.Scenes;
34 using OpenSim.Region.Framework.Scenes.Serialization;
35 
36 namespace OpenSim.Region.CoreModules.World.Serialiser
37 {
38  internal class SerialiseObjects : IFileSerialiser
39  {
40  #region IFileSerialiser Members
41 
42  public string WriteToFile(Scene scene, string dir)
43  {
44  string targetFileName = Path.Combine(dir, "objects.xml");
45 
46  SaveSerialisedToFile(targetFileName, scene);
47 
48  return "objects.xml";
49  }
50 
51  #endregion
52 
53  public void SaveSerialisedToFile(string fileName, Scene scene)
54  {
55  string xmlstream = GetObjectXml(scene);
56 
57  using (MemoryStream stream = ReformatXmlString(xmlstream))
58  {
59  stream.Seek(0, SeekOrigin.Begin);
60  CreateXmlFile(stream, fileName);
61 
62  stream.Seek(0, SeekOrigin.Begin);
63  CreateCompressedXmlFile(stream, fileName);
64  }
65  }
66 
67  private static MemoryStream ReformatXmlString(string xmlstream)
68  {
69  MemoryStream stream = new MemoryStream();
70  XmlTextWriter formatter = new XmlTextWriter(stream, Encoding.UTF8);
71  XmlDocument doc = new XmlDocument();
72 
73  doc.LoadXml(xmlstream);
74  formatter.Formatting = Formatting.Indented;
75  doc.WriteContentTo(formatter);
76  formatter.Flush();
77  return stream;
78  }
79 
80  private static string GetObjectXml(Scene scene)
81  {
82  string xmlstream = "<scene>";
83 
84  EntityBase[] EntityList = scene.GetEntities();
85  List<string> EntityXml = new List<string>();
86 
87  foreach (EntityBase ent in EntityList)
88  {
89  if (ent is SceneObjectGroup)
90  {
91  EntityXml.Add(SceneObjectSerializer.ToXml2Format((SceneObjectGroup)ent));
92  }
93  }
94  EntityXml.Sort();
95 
96  foreach (string xml in EntityXml)
97  xmlstream += xml;
98 
99  xmlstream += "</scene>";
100  return xmlstream;
101  }
102 
103  private static void CreateXmlFile(MemoryStream xmlStream, string fileName)
104  {
105  FileStream objectsFile = new FileStream(fileName, FileMode.Create);
106 
107  xmlStream.WriteTo(objectsFile);
108  objectsFile.Flush();
109  objectsFile.Close();
110  }
111 
112  private static void CreateCompressedXmlFile(MemoryStream xmlStream, string fileName)
113  {
114  #region GZip Compressed Version
115 
116  using (FileStream objectsFileCompressed = new FileStream(fileName + ".gzs", FileMode.Create))
117  using (MemoryStream gzipMSStream = new MemoryStream())
118  {
119  using (GZipStream gzipStream = new GZipStream(gzipMSStream, CompressionMode.Compress, true))
120  {
121  xmlStream.WriteTo(gzipStream);
122  }
123 
124  gzipMSStream.WriteTo(objectsFileCompressed);
125  }
126 
127  #endregion
128  }
129  }
130 }
A scene object group is conceptually an object in the scene. The object is constituted of SceneObject...
Ionic.Zlib.GZipStream GZipStream
Ionic.Zlib.CompressionMode CompressionMode