CoalescedSceneObjectsSerializer.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.Collections.Generic;
  29. using System.Drawing;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Text;
  33. using System.Xml;
  34. using log4net;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. namespace OpenSim.Region.Framework.Scenes.Serialization
  40. {
  41. /// <summary>
  42. /// Serialize and deserialize coalesced scene objects.
  43. /// </summary>
  44. public class CoalescedSceneObjectsSerializer
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. /// <summary>
  48. /// Serialize coalesced objects to Xml
  49. /// </summary>
  50. /// <param name="coa"></param>
  51. /// <param name="doScriptStates">
  52. /// If true then serialize script states. This will halt any running scripts
  53. /// </param>
  54. /// <returns></returns>
  55. public static string ToXml(CoalescedSceneObjects coa)
  56. {
  57. return ToXml(coa, true);
  58. }
  59. /// <summary>
  60. /// Serialize coalesced objects to Xml
  61. /// </summary>
  62. /// <param name="coa"></param>
  63. /// <param name="doScriptStates">
  64. /// If true then serialize script states. This will halt any running scripts
  65. /// </param>
  66. /// <returns></returns>
  67. public static string ToXml(CoalescedSceneObjects coa, bool doScriptStates)
  68. {
  69. using (StringWriter sw = new StringWriter())
  70. {
  71. using (XmlTextWriter writer = new XmlTextWriter(sw))
  72. {
  73. Vector3 size;
  74. List<SceneObjectGroup> coaObjects = coa.Objects;
  75. // m_log.DebugFormat(
  76. // "[COALESCED SCENE OBJECTS SERIALIZER]: Writing {0} objects for coalesced object",
  77. // coaObjects.Count);
  78. // This is weak - we're relying on the set of coalesced objects still being identical
  79. Vector3[] offsets = coa.GetSizeAndOffsets(out size);
  80. writer.WriteStartElement("CoalescedObject");
  81. writer.WriteAttributeString("x", size.X.ToString(Culture.FormatProvider));
  82. writer.WriteAttributeString("y", size.Y.ToString(Culture.FormatProvider));
  83. writer.WriteAttributeString("z", size.Z.ToString(Culture.FormatProvider));
  84. // Embed the offsets into the group XML
  85. for (int i = 0; i < coaObjects.Count; i++)
  86. {
  87. SceneObjectGroup obj = coaObjects[i];
  88. // m_log.DebugFormat(
  89. // "[COALESCED SCENE OBJECTS SERIALIZER]: Writing offset for object {0}, {1}",
  90. // i, obj.Name);
  91. writer.WriteStartElement("SceneObjectGroup");
  92. writer.WriteAttributeString("offsetx", offsets[i].X.ToString(Culture.FormatProvider));
  93. writer.WriteAttributeString("offsety", offsets[i].Y.ToString(Culture.FormatProvider));
  94. writer.WriteAttributeString("offsetz", offsets[i].Z.ToString(Culture.FormatProvider));
  95. SceneObjectSerializer.ToOriginalXmlFormat(obj, writer, doScriptStates);
  96. writer.WriteEndElement(); // SceneObjectGroup
  97. }
  98. writer.WriteEndElement(); // CoalescedObject
  99. }
  100. string output = sw.ToString();
  101. // Console.WriteLine(output);
  102. return output;
  103. }
  104. }
  105. public static bool TryFromXml(string xml, out CoalescedSceneObjects coa)
  106. {
  107. // m_log.DebugFormat("[COALESCED SCENE OBJECTS SERIALIZER]: TryFromXml() deserializing {0}", xml);
  108. coa = null;
  109. try
  110. {
  111. // Quickly check if this is a coalesced object, without fully parsing the XML
  112. using (XmlTextReader reader = new XmlTextReader(new StringReader(xml)))
  113. {
  114. reader.DtdProcessing = DtdProcessing.Ignore;
  115. reader.MoveToContent(); // skip possible xml declaration
  116. if (reader.Name != "CoalescedObject")
  117. {
  118. // m_log.DebugFormat(
  119. // "[COALESCED SCENE OBJECTS SERIALIZER]: TryFromXml() root element was {0} so returning false",
  120. // reader.Name);
  121. return false;
  122. }
  123. }
  124. XmlDocument doc = new XmlDocument();
  125. doc.LoadXml(xml);
  126. XmlElement e = (XmlElement)doc.SelectSingleNode("/CoalescedObject");
  127. if (e == null)
  128. return false;
  129. coa = new CoalescedSceneObjects(UUID.Zero);
  130. XmlNodeList groups = e.SelectNodes("SceneObjectGroup");
  131. int i = 0;
  132. foreach (XmlNode n in groups)
  133. {
  134. SceneObjectGroup so = SceneObjectSerializer.FromOriginalXmlFormat(n.OuterXml);
  135. if (so != null)
  136. {
  137. coa.Add(so);
  138. }
  139. else
  140. {
  141. // XXX: Possibly we should fail outright here rather than continuing if a particular component of the
  142. // coalesced object fails to load.
  143. m_log.WarnFormat(
  144. "[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml for component {0} failed. Continuing.",
  145. i);
  146. }
  147. i++;
  148. }
  149. }
  150. catch (Exception e)
  151. {
  152. m_log.Error("[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml failed ", e);
  153. Util.LogFailedXML("[COALESCED SCENE OBJECTS SERIALIZER]:", xml);
  154. return false;
  155. }
  156. return true;
  157. }
  158. public static bool TryFromXmlData(byte[] data, out CoalescedSceneObjects coa)
  159. {
  160. // m_log.DebugFormat("[COALESCED SCENE OBJECTS SERIALIZER]: TryFromXml() deserializing {0}", xml);
  161. coa = null;
  162. try
  163. {
  164. int len = data.Length;
  165. if (len < 32)
  166. return false;
  167. if (data[len - 1] == 0)
  168. --len;
  169. MemoryStream ms;
  170. XmlDocument doc;
  171. // Quickly check if this is a coalesced object, without fully parsing the XML
  172. using (ms = new MemoryStream(data, 0, len, false))
  173. {
  174. using(StreamReader sr = new StreamReader(ms, Encoding.UTF8))
  175. {
  176. using (XmlTextReader reader = new XmlTextReader(sr))
  177. {
  178. reader.DtdProcessing = DtdProcessing.Ignore;
  179. reader.MoveToContent(); // skip possible xml declaration
  180. if (reader.Name != "CoalescedObject")
  181. return false;
  182. }
  183. }
  184. ms.Seek(0, SeekOrigin.Begin);
  185. doc = new XmlDocument();
  186. doc.Load(ms);
  187. }
  188. XmlElement e = (XmlElement)doc.SelectSingleNode("/CoalescedObject");
  189. if (e == null)
  190. return false;
  191. coa = new CoalescedSceneObjects(UUID.Zero);
  192. XmlNodeList groups = e.SelectNodes("SceneObjectGroup");
  193. int i = 0;
  194. foreach (XmlNode n in groups)
  195. {
  196. SceneObjectGroup so = SceneObjectSerializer.FromOriginalXmlFormat(n.OuterXml);
  197. if (so != null)
  198. {
  199. coa.Add(so);
  200. }
  201. else
  202. {
  203. // XXX: Possibly we should fail outright here rather than continuing if a particular component of the
  204. // coalesced object fails to load.
  205. m_log.WarnFormat(
  206. "[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml for component {0} failed. Continuing.",
  207. i);
  208. }
  209. i++;
  210. }
  211. }
  212. catch (Exception e)
  213. {
  214. m_log.Error("[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of binary xml failed ", e);
  215. return false;
  216. }
  217. return true;
  218. }
  219. }
  220. }