LandDataSerializer.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. // 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 ProcessParcelEnvironment(LandData ld, XmlReader xtr)
  127. {
  128. string senv = xtr.ReadElementString("Environment");
  129. ld.Environment = ViewerEnvironment.FromOSDString(senv);
  130. ld.EnvironmentVersion = ld.Environment.version;
  131. }
  132. public static void ProcessParcelAccessList(LandData ld, XmlReader xtr)
  133. {
  134. if (!xtr.IsEmptyElement)
  135. {
  136. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  137. {
  138. LandAccessEntry lae = new LandAccessEntry();
  139. xtr.ReadStartElement("ParcelAccessEntry");
  140. ExternalRepresentationUtils.ExecuteReadProcessors<LandAccessEntry>(lae, m_laeProcessors, xtr);
  141. xtr.ReadEndElement();
  142. ld.ParcelAccessList.Add(lae);
  143. }
  144. }
  145. xtr.Read();
  146. }
  147. /// <summary>
  148. /// Reify/deserialize landData
  149. /// </summary>
  150. /// <param name="serializedLandData"></param>
  151. /// <returns></returns>
  152. /// <exception cref="System.Xml.XmlException"></exception>
  153. public static LandData Deserialize(byte[] serializedLandData)
  154. {
  155. return Deserialize(Encoding.UTF8.GetString(serializedLandData, 0, serializedLandData.Length));
  156. }
  157. /// <summary>
  158. /// Reify/deserialize landData
  159. /// </summary>
  160. /// <param name="serializedLandData"></param>
  161. /// <returns></returns>
  162. /// <exception cref="System.Xml.XmlException"></exception>
  163. public static LandData Deserialize(string serializedLandData)
  164. {
  165. LandData landData = new LandData();
  166. using (XmlTextReader reader = new XmlTextReader(new StringReader(serializedLandData)))
  167. {
  168. reader.ReadStartElement("LandData");
  169. ExternalRepresentationUtils.ExecuteReadProcessors<LandData>(landData, m_ldProcessors, reader);
  170. reader.ReadEndElement();
  171. }
  172. return landData;
  173. }
  174. /// <summary>
  175. /// Serialize land data
  176. /// </summary>
  177. /// <param name='landData'></param>
  178. /// <param name='options'>
  179. /// Serialization options.
  180. /// Can be null if there are no options.
  181. /// "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
  182. /// </param>
  183. public static string Serialize(LandData landData, Dictionary<string, object> options)
  184. {
  185. StringWriter sw = new StringWriter();
  186. XmlTextWriter xtw = new XmlTextWriter(sw);
  187. xtw.Formatting = Formatting.Indented;
  188. xtw.WriteStartDocument();
  189. xtw.WriteStartElement("LandData");
  190. xtw.WriteElementString("Area", Convert.ToString(landData.Area));
  191. xtw.WriteElementString("AuctionID", Convert.ToString(landData.AuctionID));
  192. xtw.WriteElementString("AuthBuyerID", landData.AuthBuyerID.ToString());
  193. xtw.WriteElementString("Category", Convert.ToString((sbyte)landData.Category));
  194. xtw.WriteElementString("ClaimDate", Convert.ToString(landData.ClaimDate));
  195. xtw.WriteElementString("ClaimPrice", Convert.ToString(landData.ClaimPrice));
  196. xtw.WriteElementString("GlobalID", landData.GlobalID.ToString());
  197. UUID groupID = options.ContainsKey("wipe-owners") ? UUID.Zero : landData.GroupID;
  198. xtw.WriteElementString("GroupID", groupID.ToString());
  199. bool isGroupOwned = options.ContainsKey("wipe-owners") ? false : landData.IsGroupOwned;
  200. xtw.WriteElementString("IsGroupOwned", Convert.ToString(isGroupOwned));
  201. xtw.WriteElementString("Bitmap", Convert.ToBase64String(landData.Bitmap));
  202. xtw.WriteElementString("Description", landData.Description);
  203. xtw.WriteElementString("Flags", Convert.ToString((uint)landData.Flags));
  204. xtw.WriteElementString("LandingType", Convert.ToString((byte)landData.LandingType));
  205. xtw.WriteElementString("Name", landData.Name);
  206. xtw.WriteElementString("Status", Convert.ToString((sbyte)landData.Status));
  207. xtw.WriteElementString("LocalID", landData.LocalID.ToString());
  208. xtw.WriteElementString("MediaAutoScale", Convert.ToString(landData.MediaAutoScale));
  209. xtw.WriteElementString("MediaID", landData.MediaID.ToString());
  210. xtw.WriteElementString("MediaURL", landData.MediaURL);
  211. xtw.WriteElementString("MusicURL", landData.MusicURL);
  212. UUID ownerID = options.ContainsKey("wipe-owners") ? UUID.Zero : landData.OwnerID;
  213. xtw.WriteElementString("OwnerID", ownerID.ToString());
  214. xtw.WriteStartElement("ParcelAccessList");
  215. foreach (LandAccessEntry pal in landData.ParcelAccessList)
  216. {
  217. xtw.WriteStartElement("ParcelAccessEntry");
  218. xtw.WriteElementString("AgentID", pal.AgentID.ToString());
  219. xtw.WriteElementString("Time", pal.Expires.ToString());
  220. xtw.WriteElementString("AccessList", Convert.ToString((uint)pal.Flags));
  221. xtw.WriteEndElement();
  222. }
  223. xtw.WriteEndElement();
  224. xtw.WriteElementString("PassHours", Convert.ToString(landData.PassHours));
  225. xtw.WriteElementString("PassPrice", Convert.ToString(landData.PassPrice));
  226. xtw.WriteElementString("SalePrice", Convert.ToString(landData.SalePrice));
  227. xtw.WriteElementString("SnapshotID", landData.SnapshotID.ToString());
  228. xtw.WriteElementString("UserLocation", landData.UserLocation.ToString());
  229. xtw.WriteElementString("UserLookAt", landData.UserLookAt.ToString());
  230. xtw.WriteElementString("Dwell", "0");
  231. xtw.WriteElementString("OtherCleanTime", Convert.ToString(landData.OtherCleanTime));
  232. if(landData.Environment != null)
  233. {
  234. try
  235. {
  236. string senv = ViewerEnvironment.ToOSDString(landData.Environment);
  237. xtw.WriteElementString("Environment", senv);
  238. }
  239. catch { }
  240. }
  241. xtw.WriteEndElement();
  242. xtw.Close();
  243. sw.Close();
  244. return sw.ToString();
  245. }
  246. }
  247. }