SceneXmlLoader.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Xml;
  32. using Axiom.Math;
  33. using libsecondlife;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Physics.Manager;
  36. namespace OpenSim.Region.Environment.Scenes
  37. {
  38. public class SceneXmlLoader // can move to a module?
  39. {
  40. protected InnerScene m_innerScene;
  41. protected RegionInfo m_regInfo;
  42. protected Scene m_parentScene;
  43. public SceneXmlLoader(Scene parentScene, InnerScene innerScene, RegionInfo regionInfo)
  44. {
  45. m_parentScene = parentScene;
  46. m_innerScene = innerScene;
  47. m_regInfo = regionInfo;
  48. }
  49. public void LoadPrimsFromXml(string fileName, bool newIDS, LLVector3 loadOffset)
  50. {
  51. XmlDocument doc = new XmlDocument();
  52. XmlNode rootNode;
  53. int primCount = 0;
  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 = new SceneObjectGroup(m_parentScene,
  64. m_regInfo.RegionHandle, aPrimNode.OuterXml);
  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. m_innerScene.AddEntity(obj);
  72. SceneObjectPart rootPart = obj.GetChildPart(obj.UUID);
  73. // Apply loadOffsets for load/import and move combinations
  74. rootPart.GroupPosition = rootPart.AbsolutePosition + loadOffset;
  75. bool UsePhysics = (((rootPart.ObjectFlags & (uint) LLObject.ObjectFlags.Physics) > 0) &&
  76. m_parentScene.m_physicalPrim);
  77. if ((rootPart.ObjectFlags & (uint) LLObject.ObjectFlags.Phantom) == 0)
  78. {
  79. rootPart.PhysActor = m_innerScene.PhysicsScene.AddPrimShape(
  80. rootPart.Name,
  81. rootPart.Shape,
  82. new PhysicsVector(rootPart.AbsolutePosition.X + loadOffset.X,
  83. rootPart.AbsolutePosition.Y + loadOffset.Y,
  84. rootPart.AbsolutePosition.Z + loadOffset.Z),
  85. new PhysicsVector(rootPart.Scale.X, rootPart.Scale.Y, rootPart.Scale.Z),
  86. new Quaternion(rootPart.RotationOffset.W, rootPart.RotationOffset.X,
  87. rootPart.RotationOffset.Y, rootPart.RotationOffset.Z), UsePhysics);
  88. rootPart.DoPhysicsPropertyUpdate(UsePhysics, true);
  89. }
  90. primCount++;
  91. }
  92. }
  93. else
  94. {
  95. throw new Exception("Could not open file " + fileName + " for reading");
  96. }
  97. }
  98. public void SavePrimsToXml(string fileName)
  99. {
  100. FileStream file = new FileStream(fileName, FileMode.Create);
  101. StreamWriter stream = new StreamWriter(file);
  102. int primCount = 0;
  103. stream.WriteLine("<scene>\n");
  104. List<EntityBase> EntityList = m_innerScene.GetEntities();
  105. foreach (EntityBase ent in EntityList)
  106. {
  107. if (ent is SceneObjectGroup)
  108. {
  109. stream.WriteLine(((SceneObjectGroup) ent).ToXmlString());
  110. primCount++;
  111. }
  112. }
  113. stream.WriteLine("</scene>\n");
  114. stream.Close();
  115. file.Close();
  116. }
  117. public string SavePrimGroupToXML2String(SceneObjectGroup grp)
  118. {
  119. string returnstring = "";
  120. returnstring += "<scene>\n";
  121. returnstring += grp.ToXmlString2();
  122. returnstring += "</scene>\n";
  123. return returnstring;
  124. }
  125. public void LoadGroupFromXml2String(string xmlString)
  126. {
  127. XmlDocument doc = new XmlDocument();
  128. XmlNode rootNode;
  129. XmlTextReader reader = new XmlTextReader(new StringReader(xmlString));
  130. reader.WhitespaceHandling = WhitespaceHandling.None;
  131. doc.Load(reader);
  132. reader.Close();
  133. rootNode = doc.FirstChild;
  134. foreach (XmlNode aPrimNode in rootNode.ChildNodes)
  135. {
  136. CreatePrimFromXml(aPrimNode.OuterXml);
  137. }
  138. }
  139. public void LoadPrimsFromXml2(string fileName)
  140. {
  141. XmlDocument doc = new XmlDocument();
  142. XmlNode rootNode;
  143. if (fileName.StartsWith("http:") || File.Exists(fileName))
  144. {
  145. XmlTextReader reader = new XmlTextReader(fileName);
  146. reader.WhitespaceHandling = WhitespaceHandling.None;
  147. doc.Load(reader);
  148. reader.Close();
  149. rootNode = doc.FirstChild;
  150. foreach (XmlNode aPrimNode in rootNode.ChildNodes)
  151. {
  152. CreatePrimFromXml(aPrimNode.OuterXml);
  153. }
  154. }
  155. else
  156. {
  157. throw new Exception("Could not open file " + fileName + " for reading");
  158. }
  159. }
  160. public void CreatePrimFromXml(string xmlData)
  161. {
  162. SceneObjectGroup obj = new SceneObjectGroup(xmlData);
  163. LLVector3 receivedVelocity = obj.RootPart.Velocity;
  164. //System.Console.WriteLine(obj.RootPart.Velocity.ToString());
  165. m_innerScene.AddEntityFromStorage(obj);
  166. SceneObjectPart rootPart = obj.GetChildPart(obj.UUID);
  167. bool UsePhysics = (((rootPart.ObjectFlags & (uint) LLObject.ObjectFlags.Physics) > 0) &&
  168. m_parentScene.m_physicalPrim);
  169. if ((rootPart.ObjectFlags & (uint) LLObject.ObjectFlags.Phantom) == 0)
  170. {
  171. rootPart.PhysActor = m_innerScene.PhysicsScene.AddPrimShape(
  172. rootPart.Name,
  173. rootPart.Shape,
  174. new PhysicsVector(rootPart.AbsolutePosition.X, rootPart.AbsolutePosition.Y,
  175. rootPart.AbsolutePosition.Z),
  176. new PhysicsVector(rootPart.Scale.X, rootPart.Scale.Y, rootPart.Scale.Z),
  177. new Quaternion(rootPart.RotationOffset.W, rootPart.RotationOffset.X,
  178. rootPart.RotationOffset.Y, rootPart.RotationOffset.Z), UsePhysics);
  179. rootPart.DoPhysicsPropertyUpdate(UsePhysics, true);
  180. rootPart.Velocity = receivedVelocity;
  181. }
  182. }
  183. public void SavePrimsToXml2(string fileName)
  184. {
  185. FileStream file = new FileStream(fileName, FileMode.Create);
  186. StreamWriter stream = new StreamWriter(file);
  187. int primCount = 0;
  188. stream.WriteLine("<scene>\n");
  189. List<EntityBase> EntityList = m_innerScene.GetEntities();
  190. foreach (EntityBase ent in EntityList)
  191. {
  192. if (ent is SceneObjectGroup)
  193. {
  194. stream.WriteLine(((SceneObjectGroup) ent).ToXmlString2());
  195. primCount++;
  196. }
  197. }
  198. stream.WriteLine("</scene>\n");
  199. stream.Close();
  200. file.Close();
  201. }
  202. }
  203. }