CoalescedSceneObjectsSerializer.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /// <returns></returns>
  54. public static string ToXml(CoalescedSceneObjects coa)
  55. {
  56. using (StringWriter sw = new StringWriter())
  57. {
  58. using (XmlTextWriter writer = new XmlTextWriter(sw))
  59. {
  60. Vector3 size;
  61. List<SceneObjectGroup> coaObjects = coa.Objects;
  62. // m_log.DebugFormat(
  63. // "[COALESCED SCENE OBJECTS SERIALIZER]: Writing {0} objects for coalesced object",
  64. // coaObjects.Count);
  65. // This is weak - we're relying on the set of coalesced objects still being identical
  66. Vector3[] offsets = coa.GetSizeAndOffsets(out size);
  67. writer.WriteStartElement("CoalescedObject");
  68. writer.WriteAttributeString("x", size.X.ToString());
  69. writer.WriteAttributeString("y", size.Y.ToString());
  70. writer.WriteAttributeString("z", size.Z.ToString());
  71. // Embed the offsets into the group XML
  72. for (int i = 0; i < coaObjects.Count; i++)
  73. {
  74. SceneObjectGroup obj = coaObjects[i];
  75. // m_log.DebugFormat(
  76. // "[COALESCED SCENE OBJECTS SERIALIZER]: Writing offset for object {0}, {1}",
  77. // i, obj.Name);
  78. writer.WriteStartElement("SceneObjectGroup");
  79. writer.WriteAttributeString("offsetx", offsets[i].X.ToString());
  80. writer.WriteAttributeString("offsety", offsets[i].Y.ToString());
  81. writer.WriteAttributeString("offsetz", offsets[i].Z.ToString());
  82. SceneObjectSerializer.ToOriginalXmlFormat(obj, writer, true);
  83. writer.WriteEndElement(); // SceneObjectGroup
  84. }
  85. writer.WriteEndElement(); // CoalescedObject
  86. }
  87. string output = sw.ToString();
  88. // Console.WriteLine(output);
  89. return output;
  90. }
  91. }
  92. public static bool TryFromXml(string xml, out CoalescedSceneObjects coa)
  93. {
  94. // m_log.DebugFormat("[COALESCED SCENE OBJECTS SERIALIZER]: TryFromXml() deserializing {0}", xml);
  95. coa = null;
  96. using (StringReader sr = new StringReader(xml))
  97. {
  98. using (XmlTextReader reader = new XmlTextReader(sr))
  99. {
  100. try
  101. {
  102. reader.Read();
  103. if (reader.Name != "CoalescedObject")
  104. {
  105. // m_log.DebugFormat(
  106. // "[COALESCED SCENE OBJECTS SERIALIZER]: TryFromXml() root element was {0} so returning false",
  107. // reader.Name);
  108. return false;
  109. }
  110. coa = new CoalescedSceneObjects(UUID.Zero);
  111. reader.Read();
  112. while (reader.NodeType != XmlNodeType.EndElement && reader.Name != "CoalescedObject")
  113. {
  114. if (reader.Name == "SceneObjectGroup")
  115. {
  116. string soXml = reader.ReadOuterXml();
  117. coa.Add(SceneObjectSerializer.FromOriginalXmlFormat(soXml));
  118. }
  119. }
  120. reader.ReadEndElement(); // CoalescedObject
  121. }
  122. catch (Exception e)
  123. {
  124. m_log.ErrorFormat(
  125. "[COALESCED SCENE OBJECTS SERIALIZER]: Deserialization of xml failed with {0} {1}",
  126. e.Message, e.StackTrace);
  127. return false;
  128. }
  129. }
  130. }
  131. return true;
  132. }
  133. }
  134. }