RegionSettingsSerializer.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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, out ViewerEnvironment regionEnv)
  48. {
  49. // encoding is wrong. old oars seem to be on utf-16
  50. return Deserialize(Encoding.ASCII.GetString(serializedSettings, 0, serializedSettings.Length), out regionEnv);
  51. }
  52. /// <summary>
  53. /// Deserialize settings
  54. /// </summary>
  55. /// <param name="serializedSettings"></param>
  56. /// <returns></returns>
  57. /// <exception cref="System.Xml.XmlException"></exception>
  58. public static RegionSettings Deserialize(string serializedSettings, out ViewerEnvironment regionEnv)
  59. {
  60. RegionSettings settings = new RegionSettings();
  61. regionEnv = null;
  62. StringReader sr = new StringReader(serializedSettings);
  63. XmlTextReader xtr = new XmlTextReader(sr);
  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. if (xtr.IsEmptyElement)
  185. xtr.Read();
  186. else
  187. {
  188. xtr.ReadStartElement("Telehub");
  189. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  190. {
  191. switch (xtr.Name)
  192. {
  193. case "TelehubObject":
  194. settings.TelehubObject = UUID.Parse(xtr.ReadElementContentAsString());
  195. break;
  196. case "SpawnPoint":
  197. string str = xtr.ReadElementContentAsString();
  198. SpawnPoint sp = SpawnPoint.Parse(str);
  199. settings.AddSpawnPoint(sp);
  200. break;
  201. }
  202. }
  203. xtr.ReadEndElement();
  204. }
  205. }
  206. if (xtr.IsStartElement("Environment"))
  207. {
  208. if (xtr.IsEmptyElement)
  209. xtr.Read();
  210. else
  211. {
  212. xtr.ReadStartElement("Environment");
  213. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  214. {
  215. switch (xtr.Name)
  216. {
  217. case "data":
  218. regionEnv = ViewerEnvironment.FromOSDString(xtr.ReadElementContentAsString());
  219. break;
  220. }
  221. }
  222. xtr.ReadEndElement();
  223. }
  224. }
  225. xtr.Close();
  226. sr.Close();
  227. return settings;
  228. }
  229. public static string Serialize(RegionSettings settings, ViewerEnvironment RegionEnv)
  230. {
  231. StringWriter sw = new StringWriter();
  232. XmlTextWriter xtw = new XmlTextWriter(sw);
  233. xtw.Formatting = Formatting.Indented;
  234. xtw.WriteStartDocument();
  235. xtw.WriteStartElement("RegionSettings");
  236. xtw.WriteStartElement("General");
  237. xtw.WriteElementString("AllowDamage", settings.AllowDamage.ToString());
  238. xtw.WriteElementString("AllowLandResell", settings.AllowLandResell.ToString());
  239. xtw.WriteElementString("AllowLandJoinDivide", settings.AllowLandJoinDivide.ToString());
  240. xtw.WriteElementString("BlockFly", settings.BlockFly.ToString());
  241. xtw.WriteElementString("BlockLandShowInSearch", settings.BlockShowInSearch.ToString());
  242. xtw.WriteElementString("BlockTerraform", settings.BlockTerraform.ToString());
  243. xtw.WriteElementString("DisableCollisions", settings.DisableCollisions.ToString());
  244. xtw.WriteElementString("DisablePhysics", settings.DisablePhysics.ToString());
  245. xtw.WriteElementString("DisableScripts", settings.DisableScripts.ToString());
  246. xtw.WriteElementString("MaturityRating", settings.Maturity.ToString());
  247. xtw.WriteElementString("RestrictPushing", settings.RestrictPushing.ToString());
  248. xtw.WriteElementString("AgentLimit", settings.AgentLimit.ToString());
  249. xtw.WriteElementString("ObjectBonus", settings.ObjectBonus.ToString());
  250. xtw.WriteEndElement();
  251. xtw.WriteStartElement("GroundTextures");
  252. xtw.WriteElementString("Texture1", settings.TerrainTexture1.ToString());
  253. xtw.WriteElementString("Texture2", settings.TerrainTexture2.ToString());
  254. xtw.WriteElementString("Texture3", settings.TerrainTexture3.ToString());
  255. xtw.WriteElementString("Texture4", settings.TerrainTexture4.ToString());
  256. xtw.WriteElementString("ElevationLowSW", settings.Elevation1SW.ToString());
  257. xtw.WriteElementString("ElevationLowNW", settings.Elevation1NW.ToString());
  258. xtw.WriteElementString("ElevationLowSE", settings.Elevation1SE.ToString());
  259. xtw.WriteElementString("ElevationLowNE", settings.Elevation1NE.ToString());
  260. xtw.WriteElementString("ElevationHighSW", settings.Elevation2SW.ToString());
  261. xtw.WriteElementString("ElevationHighNW", settings.Elevation2NW.ToString());
  262. xtw.WriteElementString("ElevationHighSE", settings.Elevation2SE.ToString());
  263. xtw.WriteElementString("ElevationHighNE", settings.Elevation2NE.ToString());
  264. xtw.WriteEndElement();
  265. xtw.WriteStartElement("Terrain");
  266. xtw.WriteElementString("WaterHeight", settings.WaterHeight.ToString());
  267. xtw.WriteElementString("TerrainRaiseLimit", settings.TerrainRaiseLimit.ToString());
  268. xtw.WriteElementString("TerrainLowerLimit", settings.TerrainLowerLimit.ToString());
  269. xtw.WriteElementString("UseEstateSun", settings.UseEstateSun.ToString());
  270. xtw.WriteElementString("FixedSun", settings.FixedSun.ToString());
  271. xtw.WriteElementString("SunPosition", settings.SunPosition.ToString());
  272. xtw.WriteEndElement();
  273. xtw.WriteStartElement("Telehub");
  274. if (settings.TelehubObject != UUID.Zero)
  275. {
  276. xtw.WriteElementString("TelehubObject", settings.TelehubObject.ToString());
  277. foreach (SpawnPoint sp in settings.SpawnPoints())
  278. xtw.WriteElementString("SpawnPoint", sp.ToString());
  279. }
  280. xtw.WriteEndElement();
  281. if (RegionEnv != null)
  282. {
  283. xtw.WriteStartElement("Environment");
  284. xtw.WriteElementString("data", ViewerEnvironment.ToOSDString(RegionEnv));
  285. xtw.WriteEndElement();
  286. }
  287. xtw.WriteEndElement();
  288. xtw.Close();
  289. sw.Close();
  290. return sw.ToString();
  291. }
  292. }
  293. }