LandDataSerializer.cs 14 KB

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