aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Framework/Scenes/Serialization
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Framework/Scenes/Serialization')
-rw-r--r--OpenSim/Region/Framework/Scenes/Serialization/SceneXmlLoader.cs290
1 files changed, 290 insertions, 0 deletions
diff --git a/OpenSim/Region/Framework/Scenes/Serialization/SceneXmlLoader.cs b/OpenSim/Region/Framework/Scenes/Serialization/SceneXmlLoader.cs
new file mode 100644
index 0000000..24f1be1
--- /dev/null
+++ b/OpenSim/Region/Framework/Scenes/Serialization/SceneXmlLoader.cs
@@ -0,0 +1,290 @@
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.Scenes;
37using OpenSim.Region.Physics.Manager;
38
39namespace OpenSim.Region.Framework.Scenes.Serialization
40{
41 /// <summary>
42 /// Static methods to serialize and deserialize scene objects to and from XML
43 /// </summary>
44 public class SceneXmlLoader
45 {
46 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
47
48 public static void LoadPrimsFromXml(Scene scene, string fileName, bool newIDS, Vector3 loadOffset)
49 {
50 XmlDocument doc = new XmlDocument();
51 XmlNode rootNode;
52
53 if (fileName.StartsWith("http:") || File.Exists(fileName))
54 {
55 XmlTextReader reader = new XmlTextReader(fileName);
56 reader.WhitespaceHandling = WhitespaceHandling.None;
57 doc.Load(reader);
58 reader.Close();
59 rootNode = doc.FirstChild;
60 foreach (XmlNode aPrimNode in rootNode.ChildNodes)
61 {
62 SceneObjectGroup obj = SceneObjectSerializer.FromOriginalXmlFormat(aPrimNode.OuterXml);
63
64 if (newIDS)
65 {
66 obj.ResetIDs();
67 }
68 //if we want this to be a import method then we need new uuids for the object to avoid any clashes
69 //obj.RegenerateFullIDs();
70
71 scene.AddNewSceneObject(obj, true);
72 }
73 }
74 else
75 {
76 throw new Exception("Could not open file " + fileName + " for reading");
77 }
78 }
79
80 public static void SavePrimsToXml(Scene scene, string fileName)
81 {
82 FileStream file = new FileStream(fileName, FileMode.Create);
83 StreamWriter stream = new StreamWriter(file);
84 int primCount = 0;
85 stream.WriteLine("<scene>\n");
86
87 List<EntityBase> EntityList = scene.GetEntities();
88
89 foreach (EntityBase ent in EntityList)
90 {
91 if (ent is SceneObjectGroup)
92 {
93 stream.WriteLine(SceneObjectSerializer.ToOriginalXmlFormat((SceneObjectGroup)ent));
94 primCount++;
95 }
96 }
97 stream.WriteLine("</scene>\n");
98 stream.Close();
99 file.Close();
100 }
101
102 public static string SaveGroupToXml2(SceneObjectGroup grp)
103 {
104 return SceneObjectSerializer.ToXml2Format(grp);
105 }
106
107 public static SceneObjectGroup DeserializeGroupFromXml2(string xmlString)
108 {
109 XmlDocument doc = new XmlDocument();
110 XmlNode rootNode;
111
112 XmlTextReader reader = new XmlTextReader(new StringReader(xmlString));
113 reader.WhitespaceHandling = WhitespaceHandling.None;
114 doc.Load(reader);
115 reader.Close();
116 rootNode = doc.FirstChild;
117
118 // This is to deal with neighbouring regions that are still surrounding the group xml with the <scene>
119 // tag. It should be possible to remove the first part of this if statement once we go past 0.5.9 (or
120 // when some other changes forces all regions to upgrade).
121 // This might seem rather pointless since prim crossing from this revision to an earlier revision remains
122 // broken. But it isn't much work to accomodate the old format here.
123 if (rootNode.LocalName.Equals("scene"))
124 {
125 foreach (XmlNode aPrimNode in rootNode.ChildNodes)
126 {
127 // There is only ever one prim. This oddity should be removeable post 0.5.9
128 return SceneObjectSerializer.FromXml2Format(aPrimNode.OuterXml);
129 }
130
131 return null;
132 }
133 else
134 {
135 return SceneObjectSerializer.FromXml2Format(rootNode.OuterXml);
136 }
137 }
138
139 /// <summary>
140 /// Load prims from the xml2 format
141 /// </summary>
142 /// <param name="scene"></param>
143 /// <param name="fileName"></param>
144 public static void LoadPrimsFromXml2(Scene scene, string fileName)
145 {
146 LoadPrimsFromXml2(scene, new XmlTextReader(fileName), false);
147 }
148
149 /// <summary>
150 /// Load prims from the xml2 format
151 /// </summary>
152 /// <param name="scene"></param>
153 /// <param name="reader"></param>
154 /// <param name="startScripts"></param>
155 public static void LoadPrimsFromXml2(Scene scene, TextReader reader, bool startScripts)
156 {
157 LoadPrimsFromXml2(scene, new XmlTextReader(reader), startScripts);
158 }
159
160 /// <summary>
161 /// Load prims from the xml2 format. This method will close the reader
162 /// </summary>
163 /// <param name="scene"></param>
164 /// <param name="reader"></param>
165 /// <param name="startScripts"></param>
166 protected static void LoadPrimsFromXml2(Scene scene, XmlTextReader reader, bool startScripts)
167 {
168 XmlDocument doc = new XmlDocument();
169 reader.WhitespaceHandling = WhitespaceHandling.None;
170 doc.Load(reader);
171 reader.Close();
172 XmlNode rootNode = doc.FirstChild;
173
174 ICollection<SceneObjectGroup> sceneObjects = new List<SceneObjectGroup>();
175 foreach (XmlNode aPrimNode in rootNode.ChildNodes)
176 {
177 SceneObjectGroup obj = CreatePrimFromXml2(scene, aPrimNode.OuterXml);
178 if (obj != null && startScripts)
179 sceneObjects.Add(obj);
180 }
181
182 foreach (SceneObjectGroup sceneObject in sceneObjects)
183 {
184 sceneObject.CreateScriptInstances(0, true, scene.DefaultScriptEngine, 0);
185 }
186 }
187
188 /// <summary>
189 /// Create a prim from the xml2 representation.
190 /// </summary>
191 /// <param name="scene"></param>
192 /// <param name="xmlData"></param>
193 /// <returns>The scene object created. null if the scene object already existed</returns>
194 protected static SceneObjectGroup CreatePrimFromXml2(Scene scene, string xmlData)
195 {
196 SceneObjectGroup obj = SceneObjectSerializer.FromXml2Format(xmlData);
197
198 if (scene.AddRestoredSceneObject(obj, true, false))
199 return obj;
200 else
201 return null;
202 }
203
204 public static void SavePrimsToXml2(Scene scene, string fileName)
205 {
206 List<EntityBase> EntityList = scene.GetEntities();
207
208 SavePrimListToXml2(EntityList, fileName);
209 }
210
211 public static void SavePrimsToXml2(Scene scene, TextWriter stream, Vector3 min, Vector3 max)
212 {
213 List<EntityBase> EntityList = scene.GetEntities();
214
215 SavePrimListToXml2(EntityList, stream, min, max);
216 }
217
218 public static void SaveNamedPrimsToXml2(Scene scene, string primName, string fileName)
219 {
220 m_log.InfoFormat(
221 "[SERIALISER]: Saving prims with name {0} in xml2 format for region {1} to {2}",
222 primName, scene.RegionInfo.RegionName, fileName);
223
224 List<EntityBase> entityList = scene.GetEntities();
225 List<EntityBase> primList = new List<EntityBase>();
226
227 foreach (EntityBase ent in entityList)
228 {
229 if (ent is SceneObjectGroup)
230 {
231 if (ent.Name == primName)
232 {
233 primList.Add(ent);
234 }
235 }
236 }
237
238 SavePrimListToXml2(primList, fileName);
239 }
240
241 public static void SavePrimListToXml2(List<EntityBase> entityList, string fileName)
242 {
243 FileStream file = new FileStream(fileName, FileMode.Create);
244 try
245 {
246 StreamWriter stream = new StreamWriter(file);
247 try
248 {
249 SavePrimListToXml2(entityList, stream, Vector3.Zero, Vector3.Zero);
250 }
251 finally
252 {
253 stream.Close();
254 }
255 }
256 finally
257 {
258 file.Close();
259 }
260 }
261
262 public static void SavePrimListToXml2(List<EntityBase> entityList, TextWriter stream, Vector3 min, Vector3 max)
263 {
264 int primCount = 0;
265 stream.WriteLine("<scene>\n");
266
267 foreach (EntityBase ent in entityList)
268 {
269 if (ent is SceneObjectGroup)
270 {
271 SceneObjectGroup g = (SceneObjectGroup)ent;
272 if (!min.Equals(Vector3.Zero) || !max.Equals(Vector3.Zero))
273 {
274 Vector3 pos = g.RootPart.GetWorldPosition();
275 if (min.X > pos.X || min.Y > pos.Y || min.Z > pos.Z)
276 continue;
277 if (max.X < pos.X || max.Y < pos.Y || max.Z < pos.Z)
278 continue;
279 }
280
281 stream.WriteLine(SceneObjectSerializer.ToXml2Format(g));
282 primCount++;
283 }
284 }
285 stream.WriteLine("</scene>\n");
286 stream.Flush();
287 }
288
289 }
290}