opensim-development – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 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 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 private static MemoryStream ReformatXmlString(string xmlstream)
67 {
68 MemoryStream stream = new MemoryStream();
69 XmlTextWriter formatter = new XmlTextWriter(stream, Encoding.UTF8);
70 XmlDocument doc = new XmlDocument();
71  
72 doc.LoadXml(xmlstream);
73 formatter.Formatting = Formatting.Indented;
74 doc.WriteContentTo(formatter);
75 formatter.Flush();
76 return stream;
77 }
78  
79 private static string GetObjectXml(Scene scene)
80 {
81 string xmlstream = "<scene>";
82  
83 EntityBase[] EntityList = scene.GetEntities();
84 List<string> EntityXml = new List<string>();
85  
86 foreach (EntityBase ent in EntityList)
87 {
88 if (ent is SceneObjectGroup)
89 {
90 EntityXml.Add(SceneObjectSerializer.ToXml2Format((SceneObjectGroup)ent));
91 }
92 }
93 EntityXml.Sort();
94  
95 foreach (string xml in EntityXml)
96 xmlstream += xml;
97  
98 xmlstream += "</scene>";
99 return xmlstream;
100 }
101  
102 private static void CreateXmlFile(MemoryStream xmlStream, string fileName)
103 {
104 FileStream objectsFile = new FileStream(fileName, FileMode.Create);
105  
106 xmlStream.WriteTo(objectsFile);
107 objectsFile.Flush();
108 objectsFile.Close();
109 }
110  
111 private static void CreateCompressedXmlFile(MemoryStream xmlStream, string fileName)
112 {
113 #region GZip Compressed Version
114  
115 FileStream objectsFileCompressed = new FileStream(fileName + ".gzs", FileMode.Create);
116 MemoryStream gzipMSStream = new MemoryStream();
117 GZipStream gzipStream = new GZipStream(gzipMSStream, CompressionMode.Compress);
118 xmlStream.WriteTo(gzipStream);
119 gzipMSStream.WriteTo(objectsFileCompressed);
120 objectsFileCompressed.Flush();
121 objectsFileCompressed.Close();
122  
123 #endregion
124 }
125 }
126 }