1
0

LandDataSerializer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. "OwnerID", (ld, xtr) => ld.OwnerID = UUID.Parse(xtr.ReadElementString("OwnerID")));
  93. m_ldProcessors.Add(
  94. "ParcelAccessList", ProcessParcelAccessList);
  95. m_ldProcessors.Add(
  96. "PassHours", (ld, xtr) => ld.PassHours = Convert.ToSingle(xtr.ReadElementString("PassHours")));
  97. m_ldProcessors.Add(
  98. "PassPrice", (ld, xtr) => ld.PassPrice = Convert.ToInt32(xtr.ReadElementString("PassPrice")));
  99. m_ldProcessors.Add(
  100. "SalePrice", (ld, xtr) => ld.SalePrice = Convert.ToInt32(xtr.ReadElementString("SalePrice")));
  101. m_ldProcessors.Add(
  102. "SnapshotID", (ld, xtr) => ld.SnapshotID = UUID.Parse(xtr.ReadElementString("SnapshotID")));
  103. m_ldProcessors.Add(
  104. "UserLocation", (ld, xtr) => ld.UserLocation = Vector3.Parse(xtr.ReadElementString("UserLocation")));
  105. m_ldProcessors.Add(
  106. "UserLookAt", (ld, xtr) => ld.UserLookAt = Vector3.Parse(xtr.ReadElementString("UserLookAt")));
  107. // No longer used here //
  108. // m_ldProcessors.Add("Dwell", (landData, xtr) => return);
  109. m_ldProcessors.Add(
  110. "OtherCleanTime", (ld, xtr) => ld.OtherCleanTime = Convert.ToInt32(xtr.ReadElementString("OtherCleanTime")));
  111. // LandAccessEntryProcessors
  112. m_laeProcessors.Add(
  113. "AgentID", (lae, xtr) => lae.AgentID = UUID.Parse(xtr.ReadElementString("AgentID")));
  114. m_laeProcessors.Add(
  115. "Time", (lae, xtr) =>
  116. {
  117. // We really don't care about temp vs perm here and this
  118. // would break on old oars. Assume all bans are perm
  119. xtr.ReadElementString("Time");
  120. lae.Expires = 0; // Convert.ToUint( xtr.ReadElementString("Time"));
  121. }
  122. );
  123. m_laeProcessors.Add(
  124. "AccessList", (lae, xtr) => lae.Flags = (AccessList)Convert.ToUInt32(xtr.ReadElementString("AccessList")));
  125. }
  126. public static void ProcessParcelAccessList(LandData ld, XmlTextReader xtr)
  127. {
  128. if (!xtr.IsEmptyElement)
  129. {
  130. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  131. {
  132. LandAccessEntry lae = new LandAccessEntry();
  133. xtr.ReadStartElement("ParcelAccessEntry");
  134. ExternalRepresentationUtils.ExecuteReadProcessors<LandAccessEntry>(lae, m_laeProcessors, xtr);
  135. xtr.ReadEndElement();
  136. ld.ParcelAccessList.Add(lae);
  137. }
  138. }
  139. xtr.Read();
  140. }
  141. /// <summary>
  142. /// Reify/deserialize landData
  143. /// </summary>
  144. /// <param name="serializedLandData"></param>
  145. /// <returns></returns>
  146. /// <exception cref="System.Xml.XmlException"></exception>
  147. public static LandData Deserialize(byte[] serializedLandData)
  148. {
  149. return Deserialize(Encoding.UTF8.GetString(serializedLandData, 0, serializedLandData.Length));
  150. }
  151. /// <summary>
  152. /// Reify/deserialize landData
  153. /// </summary>
  154. /// <param name="serializedLandData"></param>
  155. /// <returns></returns>
  156. /// <exception cref="System.Xml.XmlException"></exception>
  157. public static LandData Deserialize(string serializedLandData)
  158. {
  159. LandData landData = new LandData();
  160. using (XmlTextReader reader = new XmlTextReader(new StringReader(serializedLandData)))
  161. {
  162. reader.ReadStartElement("LandData");
  163. ExternalRepresentationUtils.ExecuteReadProcessors<LandData>(landData, m_ldProcessors, reader);
  164. reader.ReadEndElement();
  165. }
  166. return landData;
  167. }
  168. /// <summary>
  169. /// Serialize land data
  170. /// </summary>
  171. /// <param name='landData'></param>
  172. /// <param name='options'>
  173. /// Serialization options.
  174. /// Can be null if there are no options.
  175. /// "wipe-owners" will write UUID.Zero rather than the ownerID so that a later reload loads all parcels with the estate owner as the owner
  176. /// </param>
  177. public static string Serialize(LandData landData, Dictionary<string, object> options)
  178. {
  179. StringWriter sw = new StringWriter();
  180. XmlTextWriter xtw = new XmlTextWriter(sw);
  181. xtw.Formatting = Formatting.Indented;
  182. xtw.WriteStartDocument();
  183. xtw.WriteStartElement("LandData");
  184. xtw.WriteElementString("Area", Convert.ToString(landData.Area));
  185. xtw.WriteElementString("AuctionID", Convert.ToString(landData.AuctionID));
  186. xtw.WriteElementString("AuthBuyerID", landData.AuthBuyerID.ToString());
  187. xtw.WriteElementString("Category", Convert.ToString((sbyte)landData.Category));
  188. xtw.WriteElementString("ClaimDate", Convert.ToString(landData.ClaimDate));
  189. xtw.WriteElementString("ClaimPrice", Convert.ToString(landData.ClaimPrice));
  190. xtw.WriteElementString("GlobalID", landData.GlobalID.ToString());
  191. xtw.WriteElementString("GroupID", landData.GroupID.ToString());
  192. xtw.WriteElementString("IsGroupOwned", Convert.ToString(landData.IsGroupOwned));
  193. xtw.WriteElementString("Bitmap", Convert.ToBase64String(landData.Bitmap));
  194. xtw.WriteElementString("Description", landData.Description);
  195. xtw.WriteElementString("Flags", Convert.ToString((uint)landData.Flags));
  196. xtw.WriteElementString("LandingType", Convert.ToString((byte)landData.LandingType));
  197. xtw.WriteElementString("Name", landData.Name);
  198. xtw.WriteElementString("Status", Convert.ToString((sbyte)landData.Status));
  199. xtw.WriteElementString("LocalID", landData.LocalID.ToString());
  200. xtw.WriteElementString("MediaAutoScale", Convert.ToString(landData.MediaAutoScale));
  201. xtw.WriteElementString("MediaID", landData.MediaID.ToString());
  202. xtw.WriteElementString("MediaURL", landData.MediaURL);
  203. xtw.WriteElementString("MusicURL", landData.MusicURL);
  204. UUID ownerIdToWrite;
  205. if (options != null && options.ContainsKey("wipe-owners"))
  206. ownerIdToWrite = UUID.Zero;
  207. else
  208. ownerIdToWrite = landData.OwnerID;
  209. xtw.WriteElementString("OwnerID", ownerIdToWrite.ToString());
  210. xtw.WriteStartElement("ParcelAccessList");
  211. foreach (LandAccessEntry pal in landData.ParcelAccessList)
  212. {
  213. xtw.WriteStartElement("ParcelAccessEntry");
  214. xtw.WriteElementString("AgentID", pal.AgentID.ToString());
  215. xtw.WriteElementString("Time", pal.Expires.ToString());
  216. xtw.WriteElementString("AccessList", Convert.ToString((uint)pal.Flags));
  217. xtw.WriteEndElement();
  218. }
  219. xtw.WriteEndElement();
  220. xtw.WriteElementString("PassHours", Convert.ToString(landData.PassHours));
  221. xtw.WriteElementString("PassPrice", Convert.ToString(landData.PassPrice));
  222. xtw.WriteElementString("SalePrice", Convert.ToString(landData.SalePrice));
  223. xtw.WriteElementString("SnapshotID", landData.SnapshotID.ToString());
  224. xtw.WriteElementString("UserLocation", landData.UserLocation.ToString());
  225. xtw.WriteElementString("UserLookAt", landData.UserLookAt.ToString());
  226. xtw.WriteElementString("Dwell", "0");
  227. xtw.WriteElementString("OtherCleanTime", Convert.ToString(landData.OtherCleanTime));
  228. xtw.WriteEndElement();
  229. xtw.Close();
  230. sw.Close();
  231. return sw.ToString();
  232. }
  233. }
  234. }