SceneXmlLoader.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.Physics.Manager;
  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. }
  68. }
  69. else
  70. {
  71. throw new Exception("Could not open file " + fileName + " for reading");
  72. }
  73. }
  74. public static void SavePrimsToXml(Scene scene, string fileName)
  75. {
  76. FileStream file = new FileStream(fileName, FileMode.Create);
  77. StreamWriter stream = new StreamWriter(file);
  78. int primCount = 0;
  79. stream.WriteLine("<scene>\n");
  80. EntityBase[] entityList = scene.GetEntities();
  81. foreach (EntityBase ent in entityList)
  82. {
  83. if (ent is SceneObjectGroup)
  84. {
  85. stream.WriteLine(SceneObjectSerializer.ToOriginalXmlFormat((SceneObjectGroup)ent));
  86. primCount++;
  87. }
  88. }
  89. stream.WriteLine("</scene>\n");
  90. stream.Close();
  91. file.Close();
  92. }
  93. #endregion
  94. #region XML2 serialization
  95. // Called by archives (save oar)
  96. public static string SaveGroupToXml2(SceneObjectGroup grp, Dictionary<string, object> options)
  97. {
  98. //return SceneObjectSerializer.ToXml2Format(grp);
  99. using (MemoryStream mem = new MemoryStream())
  100. {
  101. using (XmlTextWriter writer = new XmlTextWriter(mem, System.Text.Encoding.UTF8))
  102. {
  103. SceneObjectSerializer.SOGToXml2(writer, grp, options);
  104. writer.Flush();
  105. using (StreamReader reader = new StreamReader(mem))
  106. {
  107. mem.Seek(0, SeekOrigin.Begin);
  108. return reader.ReadToEnd();
  109. }
  110. }
  111. }
  112. }
  113. // Called by scene serializer (save xml2)
  114. public static void SavePrimsToXml2(Scene scene, string fileName)
  115. {
  116. EntityBase[] entityList = scene.GetEntities();
  117. SavePrimListToXml2(entityList, fileName);
  118. }
  119. // Called by scene serializer (save xml2)
  120. public static void SaveNamedPrimsToXml2(Scene scene, string primName, string fileName)
  121. {
  122. m_log.InfoFormat(
  123. "[SERIALISER]: Saving prims with name {0} in xml2 format for region {1} to {2}",
  124. primName, scene.RegionInfo.RegionName, fileName);
  125. EntityBase[] entityList = scene.GetEntities();
  126. List<EntityBase> primList = new List<EntityBase>();
  127. foreach (EntityBase ent in entityList)
  128. {
  129. if (ent is SceneObjectGroup)
  130. {
  131. if (ent.Name == primName)
  132. {
  133. primList.Add(ent);
  134. }
  135. }
  136. }
  137. SavePrimListToXml2(primList.ToArray(), fileName);
  138. }
  139. // Called by REST Application plugin
  140. public static void SavePrimsToXml2(Scene scene, TextWriter stream, Vector3 min, Vector3 max)
  141. {
  142. EntityBase[] entityList = scene.GetEntities();
  143. SavePrimListToXml2(entityList, stream, min, max);
  144. }
  145. // Called here only. Should be private?
  146. public static void SavePrimListToXml2(EntityBase[] entityList, string fileName)
  147. {
  148. FileStream file = new FileStream(fileName, FileMode.Create);
  149. try
  150. {
  151. StreamWriter stream = new StreamWriter(file);
  152. try
  153. {
  154. SavePrimListToXml2(entityList, stream, Vector3.Zero, Vector3.Zero);
  155. }
  156. finally
  157. {
  158. stream.Close();
  159. }
  160. }
  161. finally
  162. {
  163. file.Close();
  164. }
  165. }
  166. // Called here only. Should be private?
  167. public static void SavePrimListToXml2(EntityBase[] entityList, TextWriter stream, Vector3 min, Vector3 max)
  168. {
  169. XmlTextWriter writer = new XmlTextWriter(stream);
  170. int primCount = 0;
  171. stream.WriteLine("<scene>\n");
  172. foreach (EntityBase ent in entityList)
  173. {
  174. if (ent is SceneObjectGroup)
  175. {
  176. SceneObjectGroup g = (SceneObjectGroup)ent;
  177. if (!min.Equals(Vector3.Zero) || !max.Equals(Vector3.Zero))
  178. {
  179. Vector3 pos = g.RootPart.GetWorldPosition();
  180. if (min.X > pos.X || min.Y > pos.Y || min.Z > pos.Z)
  181. continue;
  182. if (max.X < pos.X || max.Y < pos.Y || max.Z < pos.Z)
  183. continue;
  184. }
  185. //stream.WriteLine(SceneObjectSerializer.ToXml2Format(g));
  186. SceneObjectSerializer.SOGToXml2(writer, (SceneObjectGroup)ent, new Dictionary<string,object>());
  187. stream.WriteLine();
  188. primCount++;
  189. }
  190. }
  191. stream.WriteLine("</scene>\n");
  192. stream.Flush();
  193. }
  194. #endregion
  195. #region XML2 deserialization
  196. public static SceneObjectGroup DeserializeGroupFromXml2(string xmlString)
  197. {
  198. XmlDocument doc = new XmlDocument();
  199. XmlNode rootNode;
  200. XmlTextReader reader = new XmlTextReader(new StringReader(xmlString));
  201. reader.WhitespaceHandling = WhitespaceHandling.None;
  202. doc.Load(reader);
  203. reader.Close();
  204. rootNode = doc.FirstChild;
  205. // This is to deal with neighbouring regions that are still surrounding the group xml with the <scene>
  206. // tag. It should be possible to remove the first part of this if statement once we go past 0.5.9 (or
  207. // when some other changes forces all regions to upgrade).
  208. // This might seem rather pointless since prim crossing from this revision to an earlier revision remains
  209. // broken. But it isn't much work to accomodate the old format here.
  210. if (rootNode.LocalName.Equals("scene"))
  211. {
  212. foreach (XmlNode aPrimNode in rootNode.ChildNodes)
  213. {
  214. // There is only ever one prim. This oddity should be removeable post 0.5.9
  215. //return SceneObjectSerializer.FromXml2Format(aPrimNode.OuterXml);
  216. using (reader = new XmlTextReader(new StringReader(aPrimNode.OuterXml)))
  217. {
  218. SceneObjectGroup obj = new SceneObjectGroup();
  219. if (SceneObjectSerializer.Xml2ToSOG(reader, obj))
  220. return obj;
  221. return null;
  222. }
  223. }
  224. return null;
  225. }
  226. else
  227. {
  228. //return SceneObjectSerializer.FromXml2Format(rootNode.OuterXml);
  229. using (reader = new XmlTextReader(new StringReader(rootNode.OuterXml)))
  230. {
  231. SceneObjectGroup obj = new SceneObjectGroup();
  232. if (SceneObjectSerializer.Xml2ToSOG(reader, obj))
  233. return obj;
  234. return null;
  235. }
  236. }
  237. }
  238. /// <summary>
  239. /// Load prims from the xml2 format
  240. /// </summary>
  241. /// <param name="scene"></param>
  242. /// <param name="fileName"></param>
  243. public static void LoadPrimsFromXml2(Scene scene, string fileName)
  244. {
  245. LoadPrimsFromXml2(scene, new XmlTextReader(fileName), false);
  246. }
  247. /// <summary>
  248. /// Load prims from the xml2 format
  249. /// </summary>
  250. /// <param name="scene"></param>
  251. /// <param name="reader"></param>
  252. /// <param name="startScripts"></param>
  253. public static void LoadPrimsFromXml2(Scene scene, TextReader reader, bool startScripts)
  254. {
  255. LoadPrimsFromXml2(scene, new XmlTextReader(reader), startScripts);
  256. }
  257. /// <summary>
  258. /// Load prims from the xml2 format. This method will close the reader
  259. /// </summary>
  260. /// <param name="scene"></param>
  261. /// <param name="reader"></param>
  262. /// <param name="startScripts"></param>
  263. protected static void LoadPrimsFromXml2(Scene scene, XmlTextReader reader, bool startScripts)
  264. {
  265. XmlDocument doc = new XmlDocument();
  266. reader.WhitespaceHandling = WhitespaceHandling.None;
  267. doc.Load(reader);
  268. reader.Close();
  269. XmlNode rootNode = doc.FirstChild;
  270. ICollection<SceneObjectGroup> sceneObjects = new List<SceneObjectGroup>();
  271. foreach (XmlNode aPrimNode in rootNode.ChildNodes)
  272. {
  273. SceneObjectGroup obj = CreatePrimFromXml2(scene, aPrimNode.OuterXml);
  274. if (obj != null && startScripts)
  275. sceneObjects.Add(obj);
  276. }
  277. foreach (SceneObjectGroup sceneObject in sceneObjects)
  278. {
  279. sceneObject.CreateScriptInstances(0, true, scene.DefaultScriptEngine, 0);
  280. sceneObject.ResumeScripts();
  281. }
  282. }
  283. /// <summary>
  284. /// Create a prim from the xml2 representation.
  285. /// </summary>
  286. /// <param name="scene"></param>
  287. /// <param name="xmlData"></param>
  288. /// <returns>The scene object created. null if the scene object already existed</returns>
  289. protected static SceneObjectGroup CreatePrimFromXml2(Scene scene, string xmlData)
  290. {
  291. //SceneObjectGroup obj = SceneObjectSerializer.FromXml2Format(xmlData);
  292. using (XmlTextReader reader = new XmlTextReader(new StringReader(xmlData)))
  293. {
  294. SceneObjectGroup obj = new SceneObjectGroup();
  295. SceneObjectSerializer.Xml2ToSOG(reader, obj);
  296. if (scene.AddRestoredSceneObject(obj, true, false))
  297. return obj;
  298. else
  299. return null;
  300. }
  301. }
  302. #endregion
  303. }
  304. }