RegionSettingsSerializer.cs 13 KB

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