LandDataSerializer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.Reflection;
  31. using System.Text;
  32. using System.Xml;
  33. using log4net;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. namespace OpenSim.Framework.Serialization.External
  37. {
  38. /// <summary>
  39. /// Serialize and deserialize LandData as an external format.
  40. /// </summary>
  41. public class LandDataSerializer
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private static Dictionary<string, Action<LandData, XmlTextReader>> m_ldProcessors
  45. = new Dictionary<string, Action<LandData, XmlTextReader>>();
  46. private static Dictionary<string, Action<LandAccessEntry, XmlTextReader>> m_laeProcessors
  47. = new Dictionary<string, Action<LandAccessEntry, XmlTextReader>>();
  48. static LandDataSerializer()
  49. {
  50. // LandData processors
  51. m_ldProcessors.Add(
  52. "Area", (ld, xtr) => ld.Area = Convert.ToInt32(xtr.ReadElementString("Area")));
  53. m_ldProcessors.Add(
  54. "AuctionID", (ld, xtr) => ld.AuctionID = Convert.ToUInt32(xtr.ReadElementString("AuctionID")));
  55. m_ldProcessors.Add(
  56. "AuthBuyerID", (ld, xtr) => ld.AuthBuyerID = UUID.Parse(xtr.ReadElementString("AuthBuyerID")));
  57. m_ldProcessors.Add(
  58. "Category", (ld, xtr) => ld.Category = (ParcelCategory)Convert.ToSByte(xtr.ReadElementString("Category")));
  59. m_ldProcessors.Add(
  60. "ClaimDate", (ld, xtr) => ld.ClaimDate = Convert.ToInt32(xtr.ReadElementString("ClaimDate")));
  61. m_ldProcessors.Add(
  62. "ClaimPrice", (ld, xtr) => ld.ClaimPrice = Convert.ToInt32(xtr.ReadElementString("ClaimPrice")));
  63. m_ldProcessors.Add(
  64. "GlobalID", (ld, xtr) => ld.GlobalID = UUID.Parse(xtr.ReadElementString("GlobalID")));
  65. m_ldProcessors.Add(
  66. "GroupID", (ld, xtr) => ld.GroupID = UUID.Parse(xtr.ReadElementString("GroupID")));
  67. m_ldProcessors.Add(
  68. "IsGroupOwned", (ld, xtr) => ld.IsGroupOwned = Convert.ToBoolean(xtr.ReadElementString("IsGroupOwned")));
  69. m_ldProcessors.Add(
  70. "Bitmap", (ld, xtr) => ld.Bitmap = Convert.FromBase64String(xtr.ReadElementString("Bitmap")));
  71. m_ldProcessors.Add(
  72. "Description", (ld, xtr) => ld.Description = xtr.ReadElementString("Description"));
  73. m_ldProcessors.Add(
  74. "Flags", (ld, xtr) => ld.Flags = Convert.ToUInt32(xtr.ReadElementString("Flags")));
  75. m_ldProcessors.Add(
  76. "LandingType", (ld, xtr) => ld.LandingType = Convert.ToByte(xtr.ReadElementString("LandingType")));
  77. m_ldProcessors.Add(
  78. "Name", (ld, xtr) => ld.Name = xtr.ReadElementString("Name"));
  79. m_ldProcessors.Add(
  80. "Status", (ld, xtr) => ld.Status = (ParcelStatus)Convert.ToSByte(xtr.ReadElementString("Status")));
  81. m_ldProcessors.Add(
  82. "LocalID", (ld, xtr) => ld.LocalID = Convert.ToInt32(xtr.ReadElementString("LocalID")));
  83. m_ldProcessors.Add(
  84. "MediaAutoScale", (ld, xtr) => ld.MediaAutoScale = Convert.ToByte(xtr.ReadElementString("MediaAutoScale")));
  85. m_ldProcessors.Add(
  86. "MediaID", (ld, xtr) => ld.MediaID = UUID.Parse(xtr.ReadElementString("MediaID")));
  87. m_ldProcessors.Add(
  88. "MediaURL", (ld, xtr) => ld.MediaURL = xtr.ReadElementString("MediaURL"));
  89. m_ldProcessors.Add(
  90. "MusicURL", (ld, xtr) => ld.MusicURL = xtr.ReadElementString("MusicURL"));
  91. m_ldProcessors.Add(
  92. "ParcelAccessList", ProcessParcelAccessList);
  93. m_ldProcessors.Add(
  94. "PassHours", (ld, xtr) => ld.PassHours = Convert.ToSingle(xtr.ReadElementString("PassHours")));
  95. m_ldProcessors.Add(
  96. "PassPrice", (ld, xtr) => ld.PassPrice = Convert.ToInt32(xtr.ReadElementString("PassPrice")));
  97. m_ldProcessors.Add(
  98. "SalePrice", (ld, xtr) => ld.SalePrice = Convert.ToInt32(xtr.ReadElementString("SalePrice")));
  99. m_ldProcessors.Add(
  100. "SnapshotID", (ld, xtr) => ld.SnapshotID = UUID.Parse(xtr.ReadElementString("SnapshotID")));
  101. m_ldProcessors.Add(
  102. "UserLocation", (ld, xtr) => ld.UserLocation = Vector3.Parse(xtr.ReadElementString("UserLocation")));
  103. m_ldProcessors.Add(
  104. "UserLookAt", (ld, xtr) => ld.UserLookAt = Vector3.Parse(xtr.ReadElementString("UserLookAt")));
  105. // No longer used here //
  106. // m_ldProcessors.Add("Dwell", (landData, xtr) => return);
  107. m_ldProcessors.Add(
  108. "OtherCleanTime", (ld, xtr) => ld.OtherCleanTime = Convert.ToInt32(xtr.ReadElementString("OtherCleanTime")));
  109. // LandAccessEntryProcessors
  110. m_laeProcessors.Add(
  111. "AgentID", (lae, xtr) => lae.AgentID = UUID.Parse(xtr.ReadElementString("AgentID")));
  112. m_laeProcessors.Add(
  113. "Time", (lae, xtr) =>
  114. {
  115. // We really don't care about temp vs perm here and this
  116. // would break on old oars. Assume all bans are perm
  117. xtr.ReadElementString("Time");
  118. lae.Expires = 0; // Convert.ToUint( xtr.ReadElementString("Time"));
  119. }
  120. );
  121. m_laeProcessors.Add(
  122. "AccessList", (lae, xtr) => lae.Flags = (AccessList)Convert.ToUInt32(xtr.ReadElementString("AccessList")));
  123. }
  124. public static void ProcessParcelAccessList(LandData ld, XmlTextReader xtr)
  125. {
  126. if (!xtr.IsEmptyElement)
  127. {
  128. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  129. {
  130. LandAccessEntry lae = new LandAccessEntry();
  131. xtr.ReadStartElement("ParcelAccessEntry");
  132. ExternalRepresentationUtils.ExecuteReadProcessors<LandAccessEntry>(lae, m_laeProcessors, xtr);
  133. xtr.ReadEndElement();
  134. ld.ParcelAccessList.Add(lae);
  135. }
  136. }
  137. xtr.Read();
  138. }
  139. /// <summary>
  140. /// Reify/deserialize landData
  141. /// </summary>
  142. /// <param name="serializedLandData"></param>
  143. /// <returns></returns>
  144. /// <exception cref="System.Xml.XmlException"></exception>
  145. public static LandData Deserialize(byte[] serializedLandData)
  146. {
  147. return Deserialize(Encoding.UTF8.GetString(serializedLandData, 0, serializedLandData.Length));
  148. }
  149. /// <summary>
  150. /// Reify/deserialize landData
  151. /// </summary>
  152. /// <param name="serializedLandData"></param>
  153. /// <returns></returns>
  154. /// <exception cref="System.Xml.XmlException"></exception>
  155. public static LandData Deserialize(string serializedLandData)
  156. {
  157. LandData landData = new LandData();
  158. using (XmlTextReader reader = new XmlTextReader(new StringReader(serializedLandData)))
  159. {
  160. reader.ReadStartElement("LandData");
  161. ExternalRepresentationUtils.ExecuteReadProcessors<LandData>(landData, m_ldProcessors, reader);
  162. reader.ReadEndElement();
  163. }
  164. return landData;
  165. }
  166. public static string Serialize(LandData landData)
  167. {
  168. StringWriter sw = new StringWriter();
  169. XmlTextWriter xtw = new XmlTextWriter(sw);
  170. xtw.Formatting = Formatting.Indented;
  171. xtw.WriteStartDocument();
  172. xtw.WriteStartElement("LandData");
  173. xtw.WriteElementString("Area", Convert.ToString(landData.Area));
  174. xtw.WriteElementString("AuctionID", Convert.ToString(landData.AuctionID));
  175. xtw.WriteElementString("AuthBuyerID", landData.AuthBuyerID.ToString());
  176. xtw.WriteElementString("Category", Convert.ToString((sbyte)landData.Category));
  177. xtw.WriteElementString("ClaimDate", Convert.ToString(landData.ClaimDate));
  178. xtw.WriteElementString("ClaimPrice", Convert.ToString(landData.ClaimPrice));
  179. xtw.WriteElementString("GlobalID", landData.GlobalID.ToString());
  180. xtw.WriteElementString("GroupID", landData.GroupID.ToString());
  181. xtw.WriteElementString("IsGroupOwned", Convert.ToString(landData.IsGroupOwned));
  182. xtw.WriteElementString("Bitmap", Convert.ToBase64String(landData.Bitmap));
  183. xtw.WriteElementString("Description", landData.Description);
  184. xtw.WriteElementString("Flags", Convert.ToString((uint)landData.Flags));
  185. xtw.WriteElementString("LandingType", Convert.ToString((byte)landData.LandingType));
  186. xtw.WriteElementString("Name", landData.Name);
  187. xtw.WriteElementString("Status", Convert.ToString((sbyte)landData.Status));
  188. xtw.WriteElementString("LocalID", landData.LocalID.ToString());
  189. xtw.WriteElementString("MediaAutoScale", Convert.ToString(landData.MediaAutoScale));
  190. xtw.WriteElementString("MediaID", landData.MediaID.ToString());
  191. xtw.WriteElementString("MediaURL", landData.MediaURL);
  192. xtw.WriteElementString("MusicURL", landData.MusicURL);
  193. xtw.WriteElementString("OwnerID", landData.OwnerID.ToString());
  194. xtw.WriteStartElement("ParcelAccessList");
  195. foreach (LandAccessEntry pal in landData.ParcelAccessList)
  196. {
  197. xtw.WriteStartElement("ParcelAccessEntry");
  198. xtw.WriteElementString("AgentID", pal.AgentID.ToString());
  199. xtw.WriteElementString("Time", pal.Expires.ToString());
  200. xtw.WriteElementString("AccessList", Convert.ToString((uint)pal.Flags));
  201. xtw.WriteEndElement();
  202. }
  203. xtw.WriteEndElement();
  204. xtw.WriteElementString("PassHours", Convert.ToString(landData.PassHours));
  205. xtw.WriteElementString("PassPrice", Convert.ToString(landData.PassPrice));
  206. xtw.WriteElementString("SalePrice", Convert.ToString(landData.SalePrice));
  207. xtw.WriteElementString("SnapshotID", landData.SnapshotID.ToString());
  208. xtw.WriteElementString("UserLocation", landData.UserLocation.ToString());
  209. xtw.WriteElementString("UserLookAt", landData.UserLookAt.ToString());
  210. xtw.WriteElementString("Dwell", "0");
  211. xtw.WriteElementString("OtherCleanTime", Convert.ToString(landData.OtherCleanTime));
  212. xtw.WriteEndElement();
  213. xtw.Close();
  214. sw.Close();
  215. return sw.ToString();
  216. }
  217. }
  218. }