SceneXmlLoader.cs 10.0 KB

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