RegionSettingsSerializer.cs 14 KB

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