CoalescedSceneObjectsSerializer.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /// <remarks>
  44. /// Deserialization not yet here.
  45. /// </remarks>
  46. public class CoalescedSceneObjectsSerializer
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. /// <summary>
  50. /// Serialize coalesced objects to Xml
  51. /// </summary>
  52. /// <param name="coa"></param>
  53. /// <param name="doScriptStates">
  54. /// If true then serialize script states. This will halt any running scripts
  55. /// </param>
  56. /// <returns></returns>
  57. public static string ToXml(CoalescedSceneObjects coa)
  58. {
  59. return ToXml(coa, true);
  60. }
  61. /// <summary>
  62. /// Serialize coalesced objects to Xml
  63. /// </summary>
  64. /// <param name="coa"></param>
  65. /// <param name="doScriptStates">
  66. /// If true then serialize script states. This will halt any running scripts
  67. /// </param>
  68. /// <returns></returns>
  69. public static string ToXml(CoalescedSceneObjects coa, bool doScriptStates)
  70. {
  71. using (StringWriter sw = new StringWriter())
  72. {
  73. using (XmlTextWriter writer = new XmlTextWriter(sw))
  74. {
  75. Vector3 size;
  76. List<SceneObjectGroup> coaObjects = coa.Objects;
  77. // m_log.DebugFormat(
  78. // "[COALESCED SCENE OBJECTS SERIALIZER]: Writing {0} objects for coalesced object",
  79. // coaObjects.Count);
  80. // This is weak - we're relying on the set of coalesced objects still being identical
  81. Vector3[] offsets = coa.GetSizeAndOffsets(out size);
  82. writer.WriteStartElement("CoalescedObject");
  83. writer.WriteAttributeString("x", size.X.ToString());
  84. writer.WriteAttributeString("y", size.Y.ToString());
  85. writer.WriteAttributeString("z", size.Z.ToString());
  86. // Embed the offsets into the group XML
  87. for (int i = 0; i < coaObjects.Count; i++)
  88. {
  89. SceneObjectGroup obj = coaObjects[i];
  90. // m_log.DebugFormat(
  91. // "[COALESCED SCENE OBJECTS SERIALIZER]: Writing offset for object {0}, {1}",
  92. // i, obj.Name);
  93. writer.WriteStartElement("SceneObjectGroup");
  94. writer.WriteAttributeString("offsetx", offsets[i].X.ToString());
  95. writer.WriteAttributeString("offsety", offsets[i].Y.ToString());
  96. writer.WriteAttributeString("offsetz", offsets[i].Z.ToString());
  97. SceneObjectSerializer.ToOriginalXmlFormat(obj, writer, doScriptStates);
  98. writer.WriteEndElement(); // SceneObjectGroup
  99. }
  100. writer.WriteEndElement(); // CoalescedObject
  101. }
  102. string output = sw.ToString();
  103. // Console.WriteLine(output);
  104. return output;
  105. }
  106. }
  107. public static bool TryFromXml(string xml, out CoalescedSceneObjects coa)
  108. {
  109. // m_log.DebugFormat("[COALESCED SCENE OBJECTS SERIALIZER]: TryFromXml() deserializing {0}", xml);
  110. coa = null;
  111. using (StringReader sr = new StringReader(xml))
  112. {
  113. using (XmlTextReader reader = new XmlTextReader(sr))
  114. {
  115. try
  116. {
  117. reader.Read();
  118. if (reader.Name != "CoalescedObject")
  119. {
  120. // m_log.DebugFormat(
  121. // "[COALESCED SCENE OBJECTS SERIALIZER]: TryFromXml() root element was {0} so returning false",
  122. // reader.Name);
  123. return false;
  124. }
  125. coa = new CoalescedSceneObjects(UUID.Zero);
  126. reader.Read();
  127. while (reader.NodeType != XmlNodeType.EndElement && reader.Name != "CoalescedObject")
  128. {
  129. if (reader.Name == "SceneObjectGroup")
  130. {
  131. string soXml = reader.ReadOuterXml();
  132. coa.Add(SceneObjectSerializer.FromOriginalXmlFormat(soXml));
  133. }
  134. }
  135. reader.ReadEndElement(); // CoalescedObject
  136. }
  137. catch (Exception e)
  138. {
  139. m_log.ErrorFormat(
  140. "[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml failed with {0} {1}",
  141. e.Message, e.StackTrace);
  142. return false;
  143. }
  144. }
  145. }
  146. return true;
  147. }
  148. }
  149. }