CoalescedSceneObjectsSerializer.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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());
  81. writer.WriteAttributeString("y", size.Y.ToString());
  82. writer.WriteAttributeString("z", size.Z.ToString());
  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());
  92. writer.WriteAttributeString("offsety", offsets[i].Y.ToString());
  93. writer.WriteAttributeString("offsetz", offsets[i].Z.ToString());
  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. int i = 0;
  109. using (StringReader sr = new StringReader(xml))
  110. {
  111. using (XmlTextReader reader = new XmlTextReader(sr))
  112. {
  113. try
  114. {
  115. reader.Read();
  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. coa = new CoalescedSceneObjects(UUID.Zero);
  124. reader.Read();
  125. while (reader.NodeType != XmlNodeType.EndElement && reader.Name != "CoalescedObject")
  126. {
  127. if (reader.Name == "SceneObjectGroup")
  128. {
  129. string soXml = reader.ReadOuterXml();
  130. SceneObjectGroup so = SceneObjectSerializer.FromOriginalXmlFormat(soXml);
  131. if (so != null)
  132. {
  133. coa.Add(so);
  134. }
  135. else
  136. {
  137. // XXX: Possibly we should fail outright here rather than continuing if a particular component of the
  138. // coalesced object fails to load.
  139. m_log.WarnFormat(
  140. "[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml for component {0} failed. Continuing.",
  141. i);
  142. }
  143. i++;
  144. }
  145. }
  146. reader.ReadEndElement(); // CoalescedObject
  147. }
  148. catch (Exception e)
  149. {
  150. m_log.ErrorFormat(
  151. "[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml failed with {0} {1}",
  152. e.Message, e.StackTrace);
  153. return false;
  154. }
  155. }
  156. }
  157. return true;
  158. }
  159. }
  160. }