RegionSettingsSerializer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 OpenSim 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.IO;
  28. using System.Text;
  29. using System.Xml;
  30. using OpenMetaverse;
  31. using OpenSim.Framework;
  32. namespace OpenSim.Region.CoreModules.World.Archiver
  33. {
  34. /// <summary>
  35. /// Serialize and deserialize region settings for an archive file format.
  36. /// </summary>
  37. /// We didn't simply use automatic .NET serializagion for OpenSim.Framework.RegionSettings since this is really
  38. /// a file format rather than an object serialization.
  39. /// TODO: However, we could still have used separate non-framework classes here to read and write the xml
  40. /// automatically rather than laboriously doing it by hand using XmlTextReader and Writer. Should switch to this
  41. /// in the future.
  42. public class RegionSettingsSerializer
  43. {
  44. protected static ASCIIEncoding m_asciiEncoding = new ASCIIEncoding();
  45. /// <summary>
  46. /// Deserialize region settings
  47. /// </summary>
  48. /// <param name="serializedSettings"></param>
  49. /// <returns></returns>
  50. /// <exception cref="System.Xml.XmlException"></exception>
  51. public static RegionSettings Deserialize(byte[] serializedSettings)
  52. {
  53. return Deserialize(m_asciiEncoding.GetString(serializedSettings, 0, serializedSettings.Length));
  54. }
  55. /// <summary>
  56. /// Deserialize region settings
  57. /// </summary>
  58. /// <param name="serializedSettings"></param>
  59. /// <returns></returns>
  60. /// <exception cref="System.Xml.XmlException"></exception>
  61. public static RegionSettings Deserialize(string serializedSettings)
  62. {
  63. RegionSettings settings = new RegionSettings();
  64. StringReader sr = new StringReader(serializedSettings);
  65. XmlTextReader xtr = new XmlTextReader(sr);
  66. xtr.ReadStartElement("RegionSettings");
  67. xtr.ReadStartElement("General");
  68. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  69. {
  70. switch (xtr.Name)
  71. {
  72. case "AllowDamage":
  73. settings.AllowDamage = bool.Parse(xtr.ReadElementContentAsString());
  74. break;
  75. case "AllowLandResell":
  76. settings.AllowLandResell = bool.Parse(xtr.ReadElementContentAsString());
  77. break;
  78. case "AllowLandJoinDivide":
  79. settings.AllowLandJoinDivide = bool.Parse(xtr.ReadElementContentAsString());
  80. break;
  81. case "BlockFly":
  82. settings.BlockFly = bool.Parse(xtr.ReadElementContentAsString());
  83. break;
  84. case "BlockLandShowInSearch":
  85. settings.BlockShowInSearch = bool.Parse(xtr.ReadElementContentAsString());
  86. break;
  87. case "BlockTerraform":
  88. settings.BlockTerraform = bool.Parse(xtr.ReadElementContentAsString());
  89. break;
  90. case "DisableCollisions":
  91. settings.DisableCollisions = bool.Parse(xtr.ReadElementContentAsString());
  92. break;
  93. case "DisablePhysics":
  94. settings.DisablePhysics = bool.Parse(xtr.ReadElementContentAsString());
  95. break;
  96. case "DisableScripts":
  97. settings.DisableScripts = bool.Parse(xtr.ReadElementContentAsString());
  98. break;
  99. case "MaturityRating":
  100. settings.Maturity = int.Parse(xtr.ReadElementContentAsString());
  101. break;
  102. case "RestrictPushing":
  103. settings.RestrictPushing = bool.Parse(xtr.ReadElementContentAsString());
  104. break;
  105. case "AgentLimit":
  106. settings.AgentLimit = int.Parse(xtr.ReadElementContentAsString());
  107. break;
  108. case "ObjectBonus":
  109. settings.ObjectBonus = double.Parse(xtr.ReadElementContentAsString());
  110. break;
  111. }
  112. }
  113. xtr.ReadEndElement();
  114. xtr.ReadStartElement("GroundTextures");
  115. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  116. {
  117. switch (xtr.Name)
  118. {
  119. case "Texture1":
  120. settings.TerrainTexture1 = UUID.Parse(xtr.ReadElementContentAsString());
  121. break;
  122. case "Texture2":
  123. settings.TerrainTexture2 = UUID.Parse(xtr.ReadElementContentAsString());
  124. break;
  125. case "Texture3":
  126. settings.TerrainTexture3 = UUID.Parse(xtr.ReadElementContentAsString());
  127. break;
  128. case "Texture4":
  129. settings.TerrainTexture4 = UUID.Parse(xtr.ReadElementContentAsString());
  130. break;
  131. case "ElevationLowSW":
  132. settings.Elevation1SW = double.Parse(xtr.ReadElementContentAsString());
  133. break;
  134. case "ElevationLowNW":
  135. settings.Elevation1NW = double.Parse(xtr.ReadElementContentAsString());
  136. break;
  137. case "ElevationLowSE":
  138. settings.Elevation1SE = double.Parse(xtr.ReadElementContentAsString());
  139. break;
  140. case "ElevationLowNE":
  141. settings.Elevation1NE = double.Parse(xtr.ReadElementContentAsString());
  142. break;
  143. case "ElevationHighSW":
  144. settings.Elevation2SW = double.Parse(xtr.ReadElementContentAsString());
  145. break;
  146. case "ElevationHighNW":
  147. settings.Elevation2NW = double.Parse(xtr.ReadElementContentAsString());
  148. break;
  149. case "ElevationHighSE":
  150. settings.Elevation2SE = double.Parse(xtr.ReadElementContentAsString());
  151. break;
  152. case "ElevationHighNE":
  153. settings.Elevation2NE = double.Parse(xtr.ReadElementContentAsString());
  154. break;
  155. }
  156. }
  157. xtr.ReadEndElement();
  158. xtr.ReadStartElement("Terrain");
  159. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  160. {
  161. switch (xtr.Name)
  162. {
  163. case "WaterHeight":
  164. settings.WaterHeight = double.Parse(xtr.ReadElementContentAsString());
  165. break;
  166. case "TerrainRaiseLimit":
  167. settings.TerrainRaiseLimit = double.Parse(xtr.ReadElementContentAsString());
  168. break;
  169. case "TerrainLowerLimit":
  170. settings.TerrainLowerLimit = double.Parse(xtr.ReadElementContentAsString());
  171. break;
  172. case "UseEstateSun":
  173. settings.UseEstateSun = bool.Parse(xtr.ReadElementContentAsString());
  174. break;
  175. case "FixedSun":
  176. settings.FixedSun = bool.Parse(xtr.ReadElementContentAsString());
  177. break;
  178. }
  179. }
  180. xtr.Close();
  181. sr.Close();
  182. return settings;
  183. }
  184. public static string Serialize(RegionSettings settings)
  185. {
  186. StringWriter sw = new StringWriter();
  187. XmlTextWriter xtw = new XmlTextWriter(sw);
  188. xtw.Formatting = Formatting.Indented;
  189. xtw.WriteStartDocument();
  190. xtw.WriteStartElement("RegionSettings");
  191. xtw.WriteStartElement("General");
  192. xtw.WriteElementString("AllowDamage", settings.AllowDamage.ToString());
  193. xtw.WriteElementString("AllowLandResell", settings.AllowLandResell.ToString());
  194. xtw.WriteElementString("AllowLandJoinDivide", settings.AllowLandJoinDivide.ToString());
  195. xtw.WriteElementString("BlockFly", settings.BlockFly.ToString());
  196. xtw.WriteElementString("BlockLandShowInSearch", settings.BlockShowInSearch.ToString());
  197. xtw.WriteElementString("BlockTerraform", settings.BlockTerraform.ToString());
  198. xtw.WriteElementString("DisableCollisions", settings.DisableCollisions.ToString());
  199. xtw.WriteElementString("DisablePhysics", settings.DisablePhysics.ToString());
  200. xtw.WriteElementString("DisableScripts", settings.DisableScripts.ToString());
  201. xtw.WriteElementString("MaturityRating", settings.Maturity.ToString());
  202. xtw.WriteElementString("RestrictPushing", settings.RestrictPushing.ToString());
  203. xtw.WriteElementString("AgentLimit", settings.AgentLimit.ToString());
  204. xtw.WriteElementString("ObjectBonus", settings.ObjectBonus.ToString());
  205. xtw.WriteEndElement();
  206. xtw.WriteStartElement("GroundTextures");
  207. xtw.WriteElementString("Texture1", settings.TerrainTexture1.ToString());
  208. xtw.WriteElementString("Texture2", settings.TerrainTexture2.ToString());
  209. xtw.WriteElementString("Texture3", settings.TerrainTexture3.ToString());
  210. xtw.WriteElementString("Texture4", settings.TerrainTexture4.ToString());
  211. xtw.WriteElementString("ElevationLowSW", settings.Elevation1SW.ToString());
  212. xtw.WriteElementString("ElevationLowNW", settings.Elevation1NW.ToString());
  213. xtw.WriteElementString("ElevationLowSE", settings.Elevation1SE.ToString());
  214. xtw.WriteElementString("ElevationLowNE", settings.Elevation1NE.ToString());
  215. xtw.WriteElementString("ElevationHighSW", settings.Elevation2SW.ToString());
  216. xtw.WriteElementString("ElevationHighNW", settings.Elevation2NW.ToString());
  217. xtw.WriteElementString("ElevationHighSE", settings.Elevation2SE.ToString());
  218. xtw.WriteElementString("ElevationHighNE", settings.Elevation2NE.ToString());
  219. xtw.WriteEndElement();
  220. xtw.WriteStartElement("Terrain");
  221. xtw.WriteElementString("WaterHeight", settings.WaterHeight.ToString());
  222. xtw.WriteElementString("TerrainRaiseLimit", settings.TerrainRaiseLimit.ToString());
  223. xtw.WriteElementString("TerrainLowerLimit", settings.TerrainLowerLimit.ToString());
  224. xtw.WriteElementString("UseEstateSun", settings.UseEstateSun.ToString());
  225. xtw.WriteElementString("FixedSun", settings.FixedSun.ToString());
  226. // XXX: Need to expose interface to get sun phase information from sun module
  227. // xtw.WriteStartElement("SunPhase",
  228. xtw.WriteEndElement();
  229. xtw.WriteEndElement();
  230. xtw.Close();
  231. sw.Close();
  232. return sw.ToString();
  233. }
  234. }
  235. }