CoalescedSceneObjectsSerializer.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.MoveToContent(); // skip possible xml declaration
  115. if (reader.Name != "CoalescedObject")
  116. {
  117. // m_log.DebugFormat(
  118. // "[COALESCED SCENE OBJECTS SERIALIZER]: TryFromXml() root element was {0} so returning false",
  119. // reader.Name);
  120. return false;
  121. }
  122. }
  123. XmlDocument doc = new XmlDocument();
  124. doc.LoadXml(xml);
  125. XmlElement e = (XmlElement)doc.SelectSingleNode("/CoalescedObject");
  126. if (e == null)
  127. return false;
  128. coa = new CoalescedSceneObjects(UUID.Zero);
  129. XmlNodeList groups = e.SelectNodes("SceneObjectGroup");
  130. int i = 0;
  131. foreach (XmlNode n in groups)
  132. {
  133. SceneObjectGroup so = SceneObjectSerializer.FromOriginalXmlFormat(n.OuterXml);
  134. if (so != null)
  135. {
  136. coa.Add(so);
  137. }
  138. else
  139. {
  140. // XXX: Possibly we should fail outright here rather than continuing if a particular component of the
  141. // coalesced object fails to load.
  142. m_log.WarnFormat(
  143. "[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml for component {0} failed. Continuing.",
  144. i);
  145. }
  146. i++;
  147. }
  148. }
  149. catch (Exception e)
  150. {
  151. m_log.Error("[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml failed ", e);
  152. Util.LogFailedXML("[COALESCED SCENE OBJECTS SERIALIZER]:", xml);
  153. return false;
  154. }
  155. return true;
  156. }
  157. public static bool TryFromXmlData(byte[] data, out CoalescedSceneObjects coa)
  158. {
  159. // m_log.DebugFormat("[COALESCED SCENE OBJECTS SERIALIZER]: TryFromXml() deserializing {0}", xml);
  160. coa = null;
  161. try
  162. {
  163. int len = data.Length;
  164. if (len < 32)
  165. return false;
  166. if (data[len - 1] == 0)
  167. --len;
  168. // Quickly check if this is a coalesced object, without fully parsing the XML
  169. MemoryStream ms = new MemoryStream(data, 0, len, false);
  170. StreamReader sr = new StreamReader(ms, Encoding.UTF8);
  171. using (XmlTextReader reader = new XmlTextReader(sr))
  172. {
  173. reader.MoveToContent(); // skip possible xml declaration
  174. if (reader.Name != "CoalescedObject")
  175. {
  176. return false;
  177. }
  178. }
  179. XmlDocument doc = new XmlDocument();
  180. using (ms = new MemoryStream(data, 0, len, false))
  181. doc.Load(ms);
  182. XmlElement e = (XmlElement)doc.SelectSingleNode("/CoalescedObject");
  183. if (e == null)
  184. return false;
  185. coa = new CoalescedSceneObjects(UUID.Zero);
  186. XmlNodeList groups = e.SelectNodes("SceneObjectGroup");
  187. int i = 0;
  188. foreach (XmlNode n in groups)
  189. {
  190. SceneObjectGroup so = SceneObjectSerializer.FromOriginalXmlFormat(n.OuterXml);
  191. if (so != null)
  192. {
  193. coa.Add(so);
  194. }
  195. else
  196. {
  197. // XXX: Possibly we should fail outright here rather than continuing if a particular component of the
  198. // coalesced object fails to load.
  199. m_log.WarnFormat(
  200. "[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml for component {0} failed. Continuing.",
  201. i);
  202. }
  203. i++;
  204. }
  205. }
  206. catch (Exception e)
  207. {
  208. m_log.Error("[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of binary xml failed ", e);
  209. return false;
  210. }
  211. return true;
  212. }
  213. }
  214. }