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. // libomv.types changes UUID to Guid
  64. xmlData = xmlData.Replace("<UUID>", "<Guid>");
  65. xmlData = xmlData.Replace("</UUID>", "</Guid>");
  66. // Handle Nested <UUID><UUID> property
  67. xmlData = xmlData.Replace("<Guid><Guid>", "<UUID><Guid>");
  68. xmlData = xmlData.Replace("</Guid></Guid>", "</Guid></UUID>");
  69. try
  70. {
  71. StringReader sr;
  72. XmlTextReader reader;
  73. XmlNodeList parts;
  74. XmlDocument doc;
  75. int linkNum;
  76. doc = new XmlDocument();
  77. doc.LoadXml(xmlData);
  78. parts = doc.GetElementsByTagName("RootPart");
  79. if (parts.Count == 0)
  80. throw new Exception("Invalid Xml format - no root part");
  81. sr = new StringReader(parts[0].InnerXml);
  82. reader = new XmlTextReader(sr);
  83. SceneObjectGroup sceneObject = new SceneObjectGroup(SceneObjectPart.FromXml(fromUserInventoryItemID, reader));
  84. reader.Close();
  85. sr.Close();
  86. parts = doc.GetElementsByTagName("Part");
  87. for (int i = 0; i < parts.Count; i++)
  88. {
  89. sr = new StringReader(parts[i].InnerXml);
  90. reader = new XmlTextReader(sr);
  91. SceneObjectPart part = SceneObjectPart.FromXml(reader);
  92. linkNum = part.LinkNum;
  93. sceneObject.AddPart(part);
  94. part.LinkNum = linkNum;
  95. part.TrimPermissions();
  96. part.StoreUndoState();
  97. reader.Close();
  98. sr.Close();
  99. }
  100. // Script state may, or may not, exist. Not having any, is NOT
  101. // ever a problem.
  102. sceneObject.LoadScriptState(doc);
  103. return sceneObject;
  104. }
  105. catch (Exception e)
  106. {
  107. m_log.ErrorFormat(
  108. "[SERIALIZER]: Deserialization of xml failed with {0}. xml was {1}", e, xmlData);
  109. return null;
  110. }
  111. }
  112. /// <summary>
  113. /// Serialize a scene object to the original xml format
  114. /// </summary>
  115. /// <param name="sceneObject"></param>
  116. /// <returns></returns>
  117. public static string ToOriginalXmlFormat(SceneObjectGroup sceneObject)
  118. {
  119. using (StringWriter sw = new StringWriter())
  120. {
  121. using (XmlTextWriter writer = new XmlTextWriter(sw))
  122. {
  123. ToOriginalXmlFormat(sceneObject, writer);
  124. }
  125. return sw.ToString();
  126. }
  127. }
  128. /// <summary>
  129. /// Serialize a scene object to the original xml format
  130. /// </summary>
  131. /// <param name="sceneObject"></param>
  132. /// <returns></returns>
  133. public static void ToOriginalXmlFormat(SceneObjectGroup sceneObject, XmlTextWriter writer)
  134. {
  135. //m_log.DebugFormat("[SERIALIZER]: Starting serialization of {0}", Name);
  136. //int time = System.Environment.TickCount;
  137. writer.WriteStartElement(String.Empty, "SceneObjectGroup", String.Empty);
  138. writer.WriteStartElement(String.Empty, "RootPart", String.Empty);
  139. ToOriginalXmlFormat(sceneObject.RootPart, writer);
  140. writer.WriteEndElement();
  141. writer.WriteStartElement(String.Empty, "OtherParts", String.Empty);
  142. lock (sceneObject.Children)
  143. {
  144. foreach (SceneObjectPart part in sceneObject.Children.Values)
  145. {
  146. if (part.UUID != sceneObject.RootPart.UUID)
  147. {
  148. writer.WriteStartElement(String.Empty, "Part", String.Empty);
  149. ToOriginalXmlFormat(part, writer);
  150. writer.WriteEndElement();
  151. }
  152. }
  153. }
  154. writer.WriteEndElement(); // OtherParts
  155. sceneObject.SaveScriptedState(writer);
  156. writer.WriteEndElement(); // SceneObjectGroup
  157. //m_log.DebugFormat("[SERIALIZER]: Finished serialization of SOG {0}, {1}ms", Name, System.Environment.TickCount - time);
  158. }
  159. protected static void ToOriginalXmlFormat(SceneObjectPart part, XmlTextWriter writer)
  160. {
  161. part.ToXml(writer);
  162. }
  163. public static SceneObjectGroup FromXml2Format(string xmlData)
  164. {
  165. //m_log.DebugFormat("[SOG]: Starting deserialization of SOG");
  166. //int time = System.Environment.TickCount;
  167. // libomv.types changes UUID to Guid
  168. xmlData = xmlData.Replace("<UUID>", "<Guid>");
  169. xmlData = xmlData.Replace("</UUID>", "</Guid>");
  170. // Handle Nested <UUID><UUID> property
  171. xmlData = xmlData.Replace("<Guid><Guid>", "<UUID><Guid>");
  172. xmlData = xmlData.Replace("</Guid></Guid>", "</Guid></UUID>");
  173. try
  174. {
  175. XmlDocument doc = new XmlDocument();
  176. doc.LoadXml(xmlData);
  177. XmlNodeList parts = doc.GetElementsByTagName("SceneObjectPart");
  178. if (parts.Count == 0)
  179. {
  180. m_log.ErrorFormat("[SERIALIZER]: Deserialization of xml failed: No SceneObjectPart nodes. xml was " + xmlData);
  181. return null;
  182. }
  183. StringReader sr = new StringReader(parts[0].OuterXml);
  184. XmlTextReader reader = new XmlTextReader(sr);
  185. SceneObjectGroup sceneObject = new SceneObjectGroup(SceneObjectPart.FromXml(reader));
  186. reader.Close();
  187. sr.Close();
  188. // Then deal with the rest
  189. for (int i = 1; i < parts.Count; i++)
  190. {
  191. sr = new StringReader(parts[i].OuterXml);
  192. reader = new XmlTextReader(sr);
  193. SceneObjectPart part = SceneObjectPart.FromXml(reader);
  194. int originalLinkNum = part.LinkNum;
  195. sceneObject.AddPart(part);
  196. // SceneObjectGroup.AddPart() tries to be smart and automatically set the LinkNum.
  197. // We override that here
  198. if (originalLinkNum != 0)
  199. part.LinkNum = originalLinkNum;
  200. part.StoreUndoState();
  201. reader.Close();
  202. sr.Close();
  203. }
  204. // Script state may, or may not, exist. Not having any, is NOT
  205. // ever a problem.
  206. sceneObject.LoadScriptState(doc);
  207. return sceneObject;
  208. }
  209. catch (Exception e)
  210. {
  211. m_log.ErrorFormat("[SERIALIZER]: Deserialization of xml failed with {0}. xml was {1}", e, xmlData);
  212. return null;
  213. }
  214. }
  215. /// <summary>
  216. /// Serialize a scene object to the 'xml2' format.
  217. /// </summary>
  218. /// <param name="sceneObject"></param>
  219. /// <returns></returns>
  220. public static string ToXml2Format(SceneObjectGroup sceneObject)
  221. {
  222. using (StringWriter sw = new StringWriter())
  223. {
  224. using (XmlTextWriter writer = new XmlTextWriter(sw))
  225. {
  226. ToXml2Format(sceneObject, writer);
  227. }
  228. return sw.ToString();
  229. }
  230. }
  231. /// <summary>
  232. /// Serialize a scene object to the 'xml2' format.
  233. /// </summary>
  234. /// <param name="sceneObject"></param>
  235. /// <returns></returns>
  236. public static void ToXml2Format(SceneObjectGroup sceneObject, XmlTextWriter writer)
  237. {
  238. //m_log.DebugFormat("[SERIALIZER]: Starting serialization of SOG {0} to XML2", Name);
  239. //int time = System.Environment.TickCount;
  240. writer.WriteStartElement(String.Empty, "SceneObjectGroup", String.Empty);
  241. sceneObject.RootPart.ToXml(writer);
  242. writer.WriteStartElement(String.Empty, "OtherParts", String.Empty);
  243. lock (sceneObject.Children)
  244. {
  245. foreach (SceneObjectPart part in sceneObject.Children.Values)
  246. {
  247. if (part.UUID != sceneObject.RootPart.UUID)
  248. {
  249. part.ToXml(writer);
  250. }
  251. }
  252. }
  253. writer.WriteEndElement(); // End of OtherParts
  254. sceneObject.SaveScriptedState(writer);
  255. writer.WriteEndElement(); // End of SceneObjectGroup
  256. //m_log.DebugFormat("[SERIALIZER]: Finished serialization of SOG {0} to XML2, {1}ms", Name, System.Environment.TickCount - time);
  257. }
  258. }
  259. }