LandDataSerializer.cs 11 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.IO;
  30. using System.Text;
  31. using System.Xml;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. namespace OpenSim.Framework.Serialization.External
  35. {
  36. /// <summary>
  37. /// Serialize and deserialize LandData as an external format.
  38. /// </summary>
  39. public class LandDataSerializer
  40. {
  41. protected static UTF8Encoding m_utf8Encoding = new UTF8Encoding();
  42. /// <summary>
  43. /// Reify/deserialize landData
  44. /// </summary>
  45. /// <param name="serializedLandData"></param>
  46. /// <returns></returns>
  47. /// <exception cref="System.Xml.XmlException"></exception>
  48. public static LandData Deserialize(byte[] serializedLandData)
  49. {
  50. return Deserialize(m_utf8Encoding.GetString(serializedLandData, 0, serializedLandData.Length));
  51. }
  52. /// <summary>
  53. /// Reify/deserialize landData
  54. /// </summary>
  55. /// <param name="serializedLandData"></param>
  56. /// <returns></returns>
  57. /// <exception cref="System.Xml.XmlException"></exception>
  58. public static LandData Deserialize(string serializedLandData)
  59. {
  60. LandData landData = new LandData();
  61. StringReader sr = new StringReader(serializedLandData);
  62. XmlTextReader xtr = new XmlTextReader(sr);
  63. xtr.ReadStartElement("LandData");
  64. landData.Area = Convert.ToInt32( xtr.ReadElementString("Area"));
  65. landData.AuctionID = Convert.ToUInt32( xtr.ReadElementString("AuctionID"));
  66. landData.AuthBuyerID = UUID.Parse( xtr.ReadElementString("AuthBuyerID"));
  67. landData.Category = (ParcelCategory)Convert.ToSByte( xtr.ReadElementString("Category"));
  68. landData.ClaimDate = Convert.ToInt32( xtr.ReadElementString("ClaimDate"));
  69. landData.ClaimPrice = Convert.ToInt32( xtr.ReadElementString("ClaimPrice"));
  70. landData.GlobalID = UUID.Parse( xtr.ReadElementString("GlobalID"));
  71. landData.GroupID = UUID.Parse( xtr.ReadElementString("GroupID"));
  72. landData.IsGroupOwned = Convert.ToBoolean( xtr.ReadElementString("IsGroupOwned"));
  73. landData.Bitmap = Convert.FromBase64String( xtr.ReadElementString("Bitmap"));
  74. landData.Description = xtr.ReadElementString("Description");
  75. landData.Flags = Convert.ToUInt32( xtr.ReadElementString("Flags"));
  76. landData.LandingType = Convert.ToByte( xtr.ReadElementString("LandingType"));
  77. landData.Name = xtr.ReadElementString("Name");
  78. landData.Status = (ParcelStatus)Convert.ToSByte( xtr.ReadElementString("Status"));
  79. landData.LocalID = Convert.ToInt32( xtr.ReadElementString("LocalID"));
  80. landData.MediaAutoScale = Convert.ToByte( xtr.ReadElementString("MediaAutoScale"));
  81. landData.MediaID = UUID.Parse( xtr.ReadElementString("MediaID"));
  82. landData.MediaURL = xtr.ReadElementString("MediaURL");
  83. landData.MusicURL = xtr.ReadElementString("MusicURL");
  84. landData.OwnerID = UUID.Parse( xtr.ReadElementString("OwnerID"));
  85. landData.ParcelAccessList = new List<ParcelManager.ParcelAccessEntry>();
  86. xtr.Read();
  87. if (xtr.Name != "ParcelAccessList")
  88. throw new XmlException(String.Format("Expected \"ParcelAccessList\" element but got \"{0}\"", xtr.Name));
  89. if (!xtr.IsEmptyElement)
  90. {
  91. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  92. {
  93. ParcelManager.ParcelAccessEntry pae = new ParcelManager.ParcelAccessEntry();
  94. xtr.ReadStartElement("ParcelAccessEntry");
  95. pae.AgentID = UUID.Parse( xtr.ReadElementString("AgentID"));
  96. pae.Time = Convert.ToDateTime( xtr.ReadElementString("Time"));
  97. pae.Flags = (AccessList)Convert.ToUInt32( xtr.ReadElementString("AccessList"));
  98. xtr.ReadEndElement();
  99. landData.ParcelAccessList.Add(pae);
  100. }
  101. }
  102. xtr.Read();
  103. landData.PassHours = Convert.ToSingle( xtr.ReadElementString("PassHours"));
  104. landData.PassPrice = Convert.ToInt32( xtr.ReadElementString("PassPrice"));
  105. landData.SalePrice = Convert.ToInt32( xtr.ReadElementString("SalePrice"));
  106. landData.SnapshotID = UUID.Parse( xtr.ReadElementString("SnapshotID"));
  107. landData.UserLocation = Vector3.Parse( xtr.ReadElementString("UserLocation"));
  108. landData.UserLookAt = Vector3.Parse( xtr.ReadElementString("UserLookAt"));
  109. // No longer used here
  110. xtr.ReadElementString("Dwell");
  111. landData.OtherCleanTime = Convert.ToInt32( xtr.ReadElementString("OtherCleanTime"));
  112. xtr.ReadEndElement();
  113. xtr.Close();
  114. sr.Close();
  115. return landData;
  116. }
  117. public static string Serialize(LandData landData)
  118. {
  119. StringWriter sw = new StringWriter();
  120. XmlTextWriter xtw = new XmlTextWriter(sw);
  121. xtw.Formatting = Formatting.Indented;
  122. xtw.WriteStartDocument();
  123. xtw.WriteStartElement("LandData");
  124. xtw.WriteElementString("Area", Convert.ToString(landData.Area));
  125. xtw.WriteElementString("AuctionID", Convert.ToString(landData.AuctionID));
  126. xtw.WriteElementString("AuthBuyerID", landData.AuthBuyerID.ToString());
  127. xtw.WriteElementString("Category", Convert.ToString((sbyte)landData.Category));
  128. xtw.WriteElementString("ClaimDate", Convert.ToString(landData.ClaimDate));
  129. xtw.WriteElementString("ClaimPrice", Convert.ToString(landData.ClaimPrice));
  130. xtw.WriteElementString("GlobalID", landData.GlobalID.ToString());
  131. xtw.WriteElementString("GroupID", landData.GroupID.ToString());
  132. xtw.WriteElementString("IsGroupOwned", Convert.ToString(landData.IsGroupOwned));
  133. xtw.WriteElementString("Bitmap", Convert.ToBase64String(landData.Bitmap));
  134. xtw.WriteElementString("Description", landData.Description);
  135. xtw.WriteElementString("Flags", Convert.ToString((uint)landData.Flags));
  136. xtw.WriteElementString("LandingType", Convert.ToString((byte)landData.LandingType));
  137. xtw.WriteElementString("Name", landData.Name);
  138. xtw.WriteElementString("Status", Convert.ToString((sbyte)landData.Status));
  139. xtw.WriteElementString("LocalID", landData.LocalID.ToString());
  140. xtw.WriteElementString("MediaAutoScale", Convert.ToString(landData.MediaAutoScale));
  141. xtw.WriteElementString("MediaID", landData.MediaID.ToString());
  142. xtw.WriteElementString("MediaURL", landData.MediaURL);
  143. xtw.WriteElementString("MusicURL", landData.MusicURL);
  144. xtw.WriteElementString("OwnerID", landData.OwnerID.ToString());
  145. xtw.WriteStartElement("ParcelAccessList");
  146. foreach (ParcelManager.ParcelAccessEntry pal in landData.ParcelAccessList)
  147. {
  148. xtw.WriteStartElement("ParcelAccessEntry");
  149. xtw.WriteElementString("AgentID", pal.AgentID.ToString());
  150. xtw.WriteElementString("Time", pal.Time.ToString("s"));
  151. xtw.WriteElementString("AccessList", Convert.ToString((uint)pal.Flags));
  152. xtw.WriteEndElement();
  153. }
  154. xtw.WriteEndElement();
  155. xtw.WriteElementString("PassHours", Convert.ToString(landData.PassHours));
  156. xtw.WriteElementString("PassPrice", Convert.ToString(landData.PassPrice));
  157. xtw.WriteElementString("SalePrice", Convert.ToString(landData.SalePrice));
  158. xtw.WriteElementString("SnapshotID", landData.SnapshotID.ToString());
  159. xtw.WriteElementString("UserLocation", landData.UserLocation.ToString());
  160. xtw.WriteElementString("UserLookAt", landData.UserLookAt.ToString());
  161. xtw.WriteElementString("Dwell", "0");
  162. xtw.WriteElementString("OtherCleanTime", Convert.ToString(landData.OtherCleanTime));
  163. xtw.WriteEndElement();
  164. xtw.Close();
  165. sw.Close();
  166. return sw.ToString();
  167. }
  168. }
  169. }