CoalescedSceneObjectsSerializer.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.Xml;
  33. using log4net;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Framework.Interfaces;
  37. using OpenSim.Region.Framework.Scenes;
  38. namespace OpenSim.Region.Framework.Scenes.Serialization
  39. {
  40. /// <summary>
  41. /// Serialize and deserialize coalesced scene objects.
  42. /// </summary>
  43. public class CoalescedSceneObjectsSerializer
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. /// <summary>
  47. /// Serialize coalesced objects to Xml
  48. /// </summary>
  49. /// <param name="coa"></param>
  50. /// <param name="doScriptStates">
  51. /// If true then serialize script states. This will halt any running scripts
  52. /// </param>
  53. /// <returns></returns>
  54. public static string ToXml(CoalescedSceneObjects coa)
  55. {
  56. return ToXml(coa, true);
  57. }
  58. /// <summary>
  59. /// Serialize coalesced objects to Xml
  60. /// </summary>
  61. /// <param name="coa"></param>
  62. /// <param name="doScriptStates">
  63. /// If true then serialize script states. This will halt any running scripts
  64. /// </param>
  65. /// <returns></returns>
  66. public static string ToXml(CoalescedSceneObjects coa, bool doScriptStates)
  67. {
  68. using (StringWriter sw = new StringWriter())
  69. {
  70. using (XmlTextWriter writer = new XmlTextWriter(sw))
  71. {
  72. Vector3 size;
  73. List<SceneObjectGroup> coaObjects = coa.Objects;
  74. // m_log.DebugFormat(
  75. // "[COALESCED SCENE OBJECTS SERIALIZER]: Writing {0} objects for coalesced object",
  76. // coaObjects.Count);
  77. // This is weak - we're relying on the set of coalesced objects still being identical
  78. Vector3[] offsets = coa.GetSizeAndOffsets(out size);
  79. writer.WriteStartElement("CoalescedObject");
  80. writer.WriteAttributeString("x", size.X.ToString(Culture.FormatProvider));
  81. writer.WriteAttributeString("y", size.Y.ToString(Culture.FormatProvider));
  82. writer.WriteAttributeString("z", size.Z.ToString(Culture.FormatProvider));
  83. // Embed the offsets into the group XML
  84. for (int i = 0; i < coaObjects.Count; i++)
  85. {
  86. SceneObjectGroup obj = coaObjects[i];
  87. // m_log.DebugFormat(
  88. // "[COALESCED SCENE OBJECTS SERIALIZER]: Writing offset for object {0}, {1}",
  89. // i, obj.Name);
  90. writer.WriteStartElement("SceneObjectGroup");
  91. writer.WriteAttributeString("offsetx", offsets[i].X.ToString(Culture.FormatProvider));
  92. writer.WriteAttributeString("offsety", offsets[i].Y.ToString(Culture.FormatProvider));
  93. writer.WriteAttributeString("offsetz", offsets[i].Z.ToString(Culture.FormatProvider));
  94. SceneObjectSerializer.ToOriginalXmlFormat(obj, writer, doScriptStates);
  95. writer.WriteEndElement(); // SceneObjectGroup
  96. }
  97. writer.WriteEndElement(); // CoalescedObject
  98. }
  99. string output = sw.ToString();
  100. // Console.WriteLine(output);
  101. return output;
  102. }
  103. }
  104. public static bool TryFromXml(string xml, out CoalescedSceneObjects coa)
  105. {
  106. // m_log.DebugFormat("[COALESCED SCENE OBJECTS SERIALIZER]: TryFromXml() deserializing {0}", xml);
  107. coa = null;
  108. try
  109. {
  110. // Quickly check if this is a coalesced object, without fully parsing the XML
  111. using (StringReader sr = new StringReader(xml))
  112. {
  113. using (XmlTextReader reader = new XmlTextReader(sr))
  114. {
  115. reader.ProhibitDtd = true;
  116. reader.MoveToContent(); // skip possible xml declaration
  117. if (reader.Name != "CoalescedObject")
  118. {
  119. // m_log.DebugFormat(
  120. // "[COALESCED SCENE OBJECTS SERIALIZER]: TryFromXml() root element was {0} so returning false",
  121. // reader.Name);
  122. return false;
  123. }
  124. }
  125. }
  126. XmlDocument doc = new XmlDocument();
  127. doc.XmlResolver=null;
  128. doc.LoadXml(xml);
  129. XmlElement e = (XmlElement)doc.SelectSingleNode("/CoalescedObject");
  130. if (e == null)
  131. return false;
  132. coa = new CoalescedSceneObjects(UUID.Zero);
  133. XmlNodeList groups = e.SelectNodes("SceneObjectGroup");
  134. int i = 0;
  135. foreach (XmlNode n in groups)
  136. {
  137. SceneObjectGroup so = SceneObjectSerializer.FromOriginalXmlFormat(n.OuterXml);
  138. if (so != null)
  139. {
  140. coa.Add(so);
  141. }
  142. else
  143. {
  144. // XXX: Possibly we should fail outright here rather than continuing if a particular component of the
  145. // coalesced object fails to load.
  146. m_log.WarnFormat(
  147. "[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml for component {0} failed. Continuing.",
  148. i);
  149. }
  150. i++;
  151. }
  152. }
  153. catch (Exception e)
  154. {
  155. m_log.Error("[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml failed ", e);
  156. Util.LogFailedXML("[COALESCED SCENE OBJECTS SERIALIZER]:", xml);
  157. return false;
  158. }
  159. return true;
  160. }
  161. }
  162. }