SceneXmlLoader.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.ObjectFlags & (uint) LLObject.ObjectFlags.Physics) > 0) &&
  75. m_parentScene.m_physicalPrim);
  76. if ((rootPart.ObjectFlags & (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. rootPart.PhysActor.LocalID = rootPart.LocalId;
  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.PhysActor.LocalID = rootPart.LocalId;
  180. rootPart.DoPhysicsPropertyUpdate(UsePhysics, true);
  181. rootPart.Velocity = receivedVelocity;
  182. }
  183. obj.ScheduleGroupForFullUpdate();
  184. }
  185. public void SavePrimsToXml2(string fileName)
  186. {
  187. FileStream file = new FileStream(fileName, FileMode.Create);
  188. StreamWriter stream = new StreamWriter(file);
  189. int primCount = 0;
  190. stream.WriteLine("<scene>\n");
  191. List<EntityBase> EntityList = m_innerScene.GetEntities();
  192. foreach (EntityBase ent in EntityList)
  193. {
  194. if (ent is SceneObjectGroup)
  195. {
  196. stream.WriteLine(((SceneObjectGroup) ent).ToXmlString2());
  197. primCount++;
  198. }
  199. }
  200. stream.WriteLine("</scene>\n");
  201. stream.Close();
  202. file.Close();
  203. }
  204. }
  205. }