SceneObjectSerializer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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.IO;
  29. using System.Reflection;
  30. using System.Xml;
  31. using log4net;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Framework.Scenes;
  35. namespace OpenSim.Region.Framework.Scenes.Serialization
  36. {
  37. /// <summary>
  38. /// Serialize and deserialize scene objects.
  39. /// </summary>
  40. /// This should really be in OpenSim.Framework.Serialization but this would mean circular dependency problems
  41. /// right now - hopefully this isn't forever.
  42. public class SceneObjectSerializer
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. /// <summary>
  46. /// Deserialize a scene object from the original xml format
  47. /// </summary>
  48. /// <param name="serialization"></param>
  49. /// <returns></returns>
  50. public static SceneObjectGroup FromOriginalXmlFormat(string serialization)
  51. {
  52. return FromOriginalXmlFormat(UUID.Zero, serialization);
  53. }
  54. /// <summary>
  55. /// Deserialize a scene object from the original xml format
  56. /// </summary>
  57. /// <param name="serialization"></param>
  58. /// <returns></returns>
  59. public static SceneObjectGroup FromOriginalXmlFormat(UUID fromUserInventoryItemID, string xmlData)
  60. {
  61. //m_log.DebugFormat("[SOG]: Starting deserialization of SOG");
  62. //int time = System.Environment.TickCount;
  63. SceneObjectGroup sceneObject = new SceneObjectGroup();
  64. // libomv.types changes UUID to Guid
  65. xmlData = xmlData.Replace("<UUID>", "<Guid>");
  66. xmlData = xmlData.Replace("</UUID>", "</Guid>");
  67. // Handle Nested <UUID><UUID> property
  68. xmlData = xmlData.Replace("<Guid><Guid>", "<UUID><Guid>");
  69. xmlData = xmlData.Replace("</Guid></Guid>", "</Guid></UUID>");
  70. try
  71. {
  72. StringReader sr;
  73. XmlTextReader reader;
  74. XmlNodeList parts;
  75. XmlDocument doc;
  76. int linkNum;
  77. doc = new XmlDocument();
  78. doc.LoadXml(xmlData);
  79. parts = doc.GetElementsByTagName("RootPart");
  80. if (parts.Count == 0)
  81. {
  82. throw new Exception("Invalid Xml format - no root part");
  83. }
  84. else
  85. {
  86. sr = new StringReader(parts[0].InnerXml);
  87. reader = new XmlTextReader(sr);
  88. sceneObject.SetRootPart(SceneObjectPart.FromXml(fromUserInventoryItemID, reader));
  89. reader.Close();
  90. sr.Close();
  91. }
  92. parts = doc.GetElementsByTagName("Part");
  93. for (int i = 0; i < parts.Count; i++)
  94. {
  95. sr = new StringReader(parts[i].InnerXml);
  96. reader = new XmlTextReader(sr);
  97. SceneObjectPart part = SceneObjectPart.FromXml(reader);
  98. linkNum = part.LinkNum;
  99. sceneObject.AddPart(part);
  100. part.LinkNum = linkNum;
  101. part.TrimPermissions();
  102. part.StoreUndoState();
  103. reader.Close();
  104. sr.Close();
  105. }
  106. // Script state may, or may not, exist. Not having any, is NOT
  107. // ever a problem.
  108. sceneObject.LoadScriptState(doc);
  109. }
  110. catch (Exception e)
  111. {
  112. m_log.ErrorFormat(
  113. "[SERIALIZER]: Deserialization of xml failed with {0}. xml was {1}", e, xmlData);
  114. }
  115. //m_log.DebugFormat("[SERIALIZER]: Finished deserialization of SOG {0}, {1}ms", Name, System.Environment.TickCount - time);
  116. return sceneObject;
  117. }
  118. /// <summary>
  119. /// Serialize a scene object to the original xml format
  120. /// </summary>
  121. /// <param name="sceneObject"></param>
  122. /// <returns></returns>
  123. public static string ToOriginalXmlFormat(SceneObjectGroup sceneObject)
  124. {
  125. using (StringWriter sw = new StringWriter())
  126. {
  127. using (XmlTextWriter writer = new XmlTextWriter(sw))
  128. {
  129. ToOriginalXmlFormat(sceneObject, writer);
  130. }
  131. return sw.ToString();
  132. }
  133. }
  134. /// <summary>
  135. /// Serialize a scene object to the original xml format
  136. /// </summary>
  137. /// <param name="sceneObject"></param>
  138. /// <returns></returns>
  139. public static void ToOriginalXmlFormat(SceneObjectGroup sceneObject, XmlTextWriter writer)
  140. {
  141. //m_log.DebugFormat("[SERIALIZER]: Starting serialization of {0}", Name);
  142. //int time = System.Environment.TickCount;
  143. writer.WriteStartElement(String.Empty, "SceneObjectGroup", String.Empty);
  144. writer.WriteStartElement(String.Empty, "RootPart", String.Empty);
  145. ToOriginalXmlFormat(sceneObject.RootPart, writer);
  146. writer.WriteEndElement();
  147. writer.WriteStartElement(String.Empty, "OtherParts", String.Empty);
  148. lock (sceneObject.Children)
  149. {
  150. foreach (SceneObjectPart part in sceneObject.Children.Values)
  151. {
  152. if (part.UUID != sceneObject.RootPart.UUID)
  153. {
  154. writer.WriteStartElement(String.Empty, "Part", String.Empty);
  155. ToOriginalXmlFormat(part, writer);
  156. writer.WriteEndElement();
  157. }
  158. }
  159. }
  160. writer.WriteEndElement(); // OtherParts
  161. sceneObject.SaveScriptedState(writer);
  162. writer.WriteEndElement(); // SceneObjectGroup
  163. //m_log.DebugFormat("[SERIALIZER]: Finished serialization of SOG {0}, {1}ms", Name, System.Environment.TickCount - time);
  164. }
  165. protected static void ToOriginalXmlFormat(SceneObjectPart part, XmlTextWriter writer)
  166. {
  167. part.ToXml(writer);
  168. }
  169. public static SceneObjectGroup FromXml2Format(string xmlData)
  170. {
  171. //m_log.DebugFormat("[SOG]: Starting deserialization of SOG");
  172. //int time = System.Environment.TickCount;
  173. SceneObjectGroup sceneObject = new SceneObjectGroup();
  174. // libomv.types changes UUID to Guid
  175. xmlData = xmlData.Replace("<UUID>", "<Guid>");
  176. xmlData = xmlData.Replace("</UUID>", "</Guid>");
  177. // Handle Nested <UUID><UUID> property
  178. xmlData = xmlData.Replace("<Guid><Guid>", "<UUID><Guid>");
  179. xmlData = xmlData.Replace("</Guid></Guid>", "</Guid></UUID>");
  180. try
  181. {
  182. XmlDocument doc = new XmlDocument();
  183. doc.LoadXml(xmlData);
  184. XmlNodeList parts = doc.GetElementsByTagName("SceneObjectPart");
  185. // Process the root part first
  186. if (parts.Count > 0)
  187. {
  188. StringReader sr = new StringReader(parts[0].OuterXml);
  189. XmlTextReader reader = new XmlTextReader(sr);
  190. sceneObject.SetRootPart(SceneObjectPart.FromXml(reader));
  191. reader.Close();
  192. sr.Close();
  193. }
  194. // Then deal with the rest
  195. for (int i = 1; i < parts.Count; i++)
  196. {
  197. StringReader sr = new StringReader(parts[i].OuterXml);
  198. XmlTextReader reader = new XmlTextReader(sr);
  199. SceneObjectPart part = SceneObjectPart.FromXml(reader);
  200. sceneObject.AddPart(part);
  201. part.StoreUndoState();
  202. reader.Close();
  203. sr.Close();
  204. }
  205. // Script state may, or may not, exist. Not having any, is NOT
  206. // ever a problem.
  207. sceneObject.LoadScriptState(doc);
  208. }
  209. catch (Exception e)
  210. {
  211. m_log.ErrorFormat("[SERIALIZER]: Deserialization of xml failed with {0}. xml was {1}", e, xmlData);
  212. }
  213. //m_log.DebugFormat("[SERIALIZER]: Finished deserialization of SOG {0}, {1}ms", Name, System.Environment.TickCount - time);
  214. return sceneObject;
  215. }
  216. /// <summary>
  217. /// Serialize a scene object to the 'xml2' format.
  218. /// </summary>
  219. /// <param name="sceneObject"></param>
  220. /// <returns></returns>
  221. public static string ToXml2Format(SceneObjectGroup sceneObject)
  222. {
  223. using (StringWriter sw = new StringWriter())
  224. {
  225. using (XmlTextWriter writer = new XmlTextWriter(sw))
  226. {
  227. ToXml2Format(sceneObject, writer);
  228. }
  229. return sw.ToString();
  230. }
  231. }
  232. /// <summary>
  233. /// Serialize a scene object to the 'xml2' format.
  234. /// </summary>
  235. /// <param name="sceneObject"></param>
  236. /// <returns></returns>
  237. public static void ToXml2Format(SceneObjectGroup sceneObject, XmlTextWriter writer)
  238. {
  239. //m_log.DebugFormat("[SERIALIZER]: Starting serialization of SOG {0} to XML2", Name);
  240. //int time = System.Environment.TickCount;
  241. writer.WriteStartElement(String.Empty, "SceneObjectGroup", String.Empty);
  242. sceneObject.RootPart.ToXml(writer);
  243. writer.WriteStartElement(String.Empty, "OtherParts", String.Empty);
  244. lock (sceneObject.Children)
  245. {
  246. foreach (SceneObjectPart part in sceneObject.Children.Values)
  247. {
  248. if (part.UUID != sceneObject.RootPart.UUID)
  249. {
  250. part.ToXml(writer);
  251. }
  252. }
  253. }
  254. writer.WriteEndElement(); // End of OtherParts
  255. sceneObject.SaveScriptedState(writer);
  256. writer.WriteEndElement(); // End of SceneObjectGroup
  257. //m_log.DebugFormat("[SERIALIZER]: Finished serialization of SOG {0} to XML2, {1}ms", Name, System.Environment.TickCount - time);
  258. }
  259. }
  260. }