SceneXmlLoader.cs 11 KB

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