1
0

RegionSettingsSerializer.cs 14 KB

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