1
0

RegionSettingsSerializer.cs 13 KB

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