1
0

SceneXmlLoader.cs 10 KB

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