SceneXmlLoader.cs 11 KB

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