OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
SceneXmlLoader.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;
29 using System.Collections.Generic;
30 using System.IO;
31 using System.Reflection;
32 using System.Xml;
33 using OpenMetaverse;
34 using log4net;
35 using OpenSim.Framework;
36 using OpenSim.Region.Framework.Scenes;
37 using OpenSim.Region.PhysicsModules.SharedBase;
38 
39 namespace OpenSim.Region.Framework.Scenes.Serialization
40 {
44  public class SceneXmlLoader
45  {
46  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
47 
48  #region old xml format
49  public static void LoadPrimsFromXml(Scene scene, string fileName, bool newIDS, Vector3 loadOffset)
50  {
51  XmlDocument doc = new XmlDocument();
52  XmlNode rootNode;
53 
54  if (fileName.StartsWith("http:") || File.Exists(fileName))
55  {
56  XmlTextReader reader = new XmlTextReader(fileName);
57  reader.WhitespaceHandling = WhitespaceHandling.None;
58  doc.Load(reader);
59  reader.Close();
60  rootNode = doc.FirstChild;
61  foreach (XmlNode aPrimNode in rootNode.ChildNodes)
62  {
63  SceneObjectGroup obj = SceneObjectSerializer.FromOriginalXmlFormat(aPrimNode.OuterXml);
64 
65  if (newIDS)
66  {
67  obj.ResetIDs();
68  }
69  //if we want this to be a import method then we need new uuids for the object to avoid any clashes
70  //obj.RegenerateFullIDs();
71 
72  scene.AddNewSceneObject(obj, true);
73  }
74  }
75  else
76  {
77  throw new Exception("Could not open file " + fileName + " for reading");
78  }
79  }
80 
81  public static void SavePrimsToXml(Scene scene, string fileName)
82  {
83  FileStream file = new FileStream(fileName, FileMode.Create);
84  StreamWriter stream = new StreamWriter(file);
85  int primCount = 0;
86  stream.WriteLine("<scene>\n");
87 
88  EntityBase[] entityList = scene.GetEntities();
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  #endregion
103 
104  #region XML2 serialization
105 
106  // Called by archives (save oar)
107  public static string SaveGroupToXml2(SceneObjectGroup grp, Dictionary<string, object> options)
108  {
109  //return SceneObjectSerializer.ToXml2Format(grp);
110  using (MemoryStream mem = new MemoryStream())
111  {
112  using (XmlTextWriter writer = new XmlTextWriter(mem, System.Text.Encoding.UTF8))
113  {
114  SceneObjectSerializer.SOGToXml2(writer, grp, options);
115  writer.Flush();
116 
117  using (StreamReader reader = new StreamReader(mem))
118  {
119  mem.Seek(0, SeekOrigin.Begin);
120  return reader.ReadToEnd();
121  }
122  }
123  }
124  }
125 
126  // Called by scene serializer (save xml2)
127  public static void SavePrimsToXml2(Scene scene, string fileName)
128  {
129  EntityBase[] entityList = scene.GetEntities();
130  SavePrimListToXml2(entityList, fileName);
131  }
132 
133  // Called by scene serializer (save xml2)
134  public static void SaveNamedPrimsToXml2(Scene scene, string primName, string fileName)
135  {
136  m_log.InfoFormat(
137  "[SERIALISER]: Saving prims with name {0} in xml2 format for region {1} to {2}",
138  primName, scene.RegionInfo.RegionName, fileName);
139 
140  EntityBase[] entityList = scene.GetEntities();
141  List<EntityBase> primList = new List<EntityBase>();
142 
143  foreach (EntityBase ent in entityList)
144  {
145  if (ent is SceneObjectGroup)
146  {
147  if (ent.Name == primName)
148  {
149  primList.Add(ent);
150  }
151  }
152  }
153 
154  SavePrimListToXml2(primList.ToArray(), fileName);
155  }
156 
157  // Called by REST Application plugin
158  public static void SavePrimsToXml2(Scene scene, TextWriter stream, Vector3 min, Vector3 max)
159  {
160  EntityBase[] entityList = scene.GetEntities();
161  SavePrimListToXml2(entityList, stream, min, max);
162  }
163 
164  // Called here only. Should be private?
165  public static void SavePrimListToXml2(EntityBase[] entityList, string fileName)
166  {
167  FileStream file = new FileStream(fileName, FileMode.Create);
168  try
169  {
170  StreamWriter stream = new StreamWriter(file);
171  try
172  {
173  SavePrimListToXml2(entityList, stream, Vector3.Zero, Vector3.Zero);
174  }
175  finally
176  {
177  stream.Close();
178  }
179  }
180  finally
181  {
182  file.Close();
183  }
184  }
185 
186  // Called here only. Should be private?
187  public static void SavePrimListToXml2(EntityBase[] entityList, TextWriter stream, Vector3 min, Vector3 max)
188  {
189  XmlTextWriter writer = new XmlTextWriter(stream);
190 
191  int primCount = 0;
192  stream.WriteLine("<scene>\n");
193 
194  foreach (EntityBase ent in entityList)
195  {
196  if (ent is SceneObjectGroup)
197  {
198  SceneObjectGroup g = (SceneObjectGroup)ent;
199  if (!min.Equals(Vector3.Zero) || !max.Equals(Vector3.Zero))
200  {
201  Vector3 pos = g.RootPart.GetWorldPosition();
202  if (min.X > pos.X || min.Y > pos.Y || min.Z > pos.Z)
203  continue;
204  if (max.X < pos.X || max.Y < pos.Y || max.Z < pos.Z)
205  continue;
206  }
207 
208  //stream.WriteLine(SceneObjectSerializer.ToXml2Format(g));
209  SceneObjectSerializer.SOGToXml2(writer, (SceneObjectGroup)ent, new Dictionary<string,object>());
210  stream.WriteLine();
211 
212  primCount++;
213  }
214  }
215 
216  stream.WriteLine("</scene>\n");
217  stream.Flush();
218  }
219 
220  #endregion
221 
222  #region XML2 deserialization
223 
224  public static SceneObjectGroup DeserializeGroupFromXml2(string xmlString)
225  {
226  return SceneObjectSerializer.FromXml2Format(xmlString);
227  }
228 
234  public static void LoadPrimsFromXml2(Scene scene, string fileName)
235  {
236  LoadPrimsFromXml2(scene, new XmlTextReader(fileName), false);
237  }
238 
245  public static void LoadPrimsFromXml2(Scene scene, TextReader reader, bool startScripts)
246  {
247  LoadPrimsFromXml2(scene, new XmlTextReader(reader), startScripts);
248  }
249 
256  protected static void LoadPrimsFromXml2(Scene scene, XmlTextReader reader, bool startScripts)
257  {
258  XmlDocument doc = new XmlDocument();
259  reader.WhitespaceHandling = WhitespaceHandling.None;
260  doc.Load(reader);
261  reader.Close();
262  XmlNode rootNode = doc.FirstChild;
263 
264  ICollection<SceneObjectGroup> sceneObjects = new List<SceneObjectGroup>();
265  foreach (XmlNode aPrimNode in rootNode.ChildNodes)
266  {
267  SceneObjectGroup obj = DeserializeGroupFromXml2(aPrimNode.OuterXml);
268  if (startScripts)
269  sceneObjects.Add(obj);
270  }
271 
272  foreach (SceneObjectGroup sceneObject in sceneObjects)
273  {
274  sceneObject.CreateScriptInstances(0, true, scene.DefaultScriptEngine, 0);
275  sceneObject.ResumeScripts();
276  }
277  }
278 
279  #endregion
280  }
281 }
static void SavePrimListToXml2(EntityBase[] entityList, string fileName)
static void LoadPrimsFromXml2(Scene scene, string fileName)
Load prims from the xml2 format
static void LoadPrimsFromXml2(Scene scene, TextReader reader, bool startScripts)
Load prims from the xml2 format
A scene object group is conceptually an object in the scene. The object is constituted of SceneObject...
Static methods to serialize and deserialize scene objects to and from XML
static void SaveNamedPrimsToXml2(Scene scene, string primName, string fileName)
static void LoadPrimsFromXml(Scene scene, string fileName, bool newIDS, Vector3 loadOffset)
static void SavePrimsToXml2(Scene scene, TextWriter stream, Vector3 min, Vector3 max)
static SceneObjectGroup DeserializeGroupFromXml2(string xmlString)
static void SavePrimsToXml2(Scene scene, string fileName)
static string SaveGroupToXml2(SceneObjectGroup grp, Dictionary< string, object > options)
virtual string Name
The name of this entity
Definition: EntityBase.cs:65
static void SavePrimListToXml2(EntityBase[] entityList, TextWriter stream, Vector3 min, Vector3 max)
static void LoadPrimsFromXml2(Scene scene, XmlTextReader reader, bool startScripts)
Load prims from the xml2 format. This method will close the reader
static void SavePrimsToXml(Scene scene, string fileName)