LandDataSerializer.cs 15 KB

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