RegionSettingsSerializer.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. /// <param name="regionEnv"></param>
  46. /// <param name="estateSettings">The Estate Settings stored in the archive will be merged into this object</param>
  47. /// <returns></returns>
  48. /// <exception cref="System.Xml.XmlException"></exception>
  49. public static RegionSettings Deserialize(byte[] serializedSettings, out ViewerEnvironment regionEnv, EstateSettings estateSettings)
  50. {
  51. // encoding is wrong. old oars seem to be on utf-16
  52. return Deserialize(Encoding.ASCII.GetString(serializedSettings, 0, serializedSettings.Length), out regionEnv, estateSettings);
  53. }
  54. /// <summary>
  55. /// Deserialize settings
  56. /// </summary>
  57. /// <param name="serializedSettings"></param>
  58. /// <param name="regionEnv"></param>
  59. /// <param name="estateSettings">The Estate Settings stored in the archive will be merged into this object</param>
  60. /// <returns></returns>
  61. /// <exception cref="System.Xml.XmlException"></exception>
  62. public static RegionSettings Deserialize(string serializedSettings, out ViewerEnvironment regionEnv, EstateSettings estateSettings)
  63. {
  64. RegionSettings settings = new RegionSettings();
  65. regionEnv = null;
  66. StringReader sr = new StringReader(serializedSettings);
  67. XmlTextReader xtr = new XmlTextReader(sr);
  68. xtr.DtdProcessing = DtdProcessing.Ignore;
  69. xtr.ReadStartElement("RegionSettings");
  70. xtr.ReadStartElement("General");
  71. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  72. {
  73. switch (xtr.Name)
  74. {
  75. case "AllowDamage":
  76. settings.AllowDamage = bool.Parse(xtr.ReadElementContentAsString());
  77. break;
  78. case "AllowLandResell":
  79. settings.AllowLandResell = bool.Parse(xtr.ReadElementContentAsString());
  80. break;
  81. case "AllowLandJoinDivide":
  82. settings.AllowLandJoinDivide = bool.Parse(xtr.ReadElementContentAsString());
  83. break;
  84. case "BlockFly":
  85. settings.BlockFly = bool.Parse(xtr.ReadElementContentAsString());
  86. break;
  87. case "BlockLandShowInSearch":
  88. settings.BlockShowInSearch = bool.Parse(xtr.ReadElementContentAsString());
  89. break;
  90. case "BlockTerraform":
  91. settings.BlockTerraform = bool.Parse(xtr.ReadElementContentAsString());
  92. break;
  93. case "DisableCollisions":
  94. settings.DisableCollisions = bool.Parse(xtr.ReadElementContentAsString());
  95. break;
  96. case "DisablePhysics":
  97. settings.DisablePhysics = bool.Parse(xtr.ReadElementContentAsString());
  98. break;
  99. case "DisableScripts":
  100. settings.DisableScripts = bool.Parse(xtr.ReadElementContentAsString());
  101. break;
  102. case "MaturityRating":
  103. settings.Maturity = int.Parse(xtr.ReadElementContentAsString());
  104. break;
  105. case "RestrictPushing":
  106. settings.RestrictPushing = bool.Parse(xtr.ReadElementContentAsString());
  107. break;
  108. case "AgentLimit":
  109. settings.AgentLimit = int.Parse(xtr.ReadElementContentAsString());
  110. break;
  111. case "ObjectBonus":
  112. settings.ObjectBonus = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  113. break;
  114. }
  115. }
  116. xtr.ReadEndElement();
  117. xtr.ReadStartElement("GroundTextures");
  118. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  119. {
  120. switch (xtr.Name)
  121. {
  122. case "Texture1":
  123. settings.TerrainTexture1 = UUID.Parse(xtr.ReadElementContentAsString());
  124. break;
  125. case "Texture2":
  126. settings.TerrainTexture2 = UUID.Parse(xtr.ReadElementContentAsString());
  127. break;
  128. case "Texture3":
  129. settings.TerrainTexture3 = UUID.Parse(xtr.ReadElementContentAsString());
  130. break;
  131. case "Texture4":
  132. settings.TerrainTexture4 = UUID.Parse(xtr.ReadElementContentAsString());
  133. break;
  134. case "ElevationLowSW":
  135. settings.Elevation1SW = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  136. break;
  137. case "ElevationLowNW":
  138. settings.Elevation1NW = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  139. break;
  140. case "ElevationLowSE":
  141. settings.Elevation1SE = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  142. break;
  143. case "ElevationLowNE":
  144. settings.Elevation1NE = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  145. break;
  146. case "ElevationHighSW":
  147. settings.Elevation2SW = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  148. break;
  149. case "ElevationHighNW":
  150. settings.Elevation2NW = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  151. break;
  152. case "ElevationHighSE":
  153. settings.Elevation2SE = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  154. break;
  155. case "ElevationHighNE":
  156. settings.Elevation2NE = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  157. break;
  158. }
  159. }
  160. xtr.ReadEndElement();
  161. xtr.ReadStartElement("Terrain");
  162. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  163. {
  164. switch (xtr.Name)
  165. {
  166. case "WaterHeight":
  167. settings.WaterHeight = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  168. break;
  169. case "TerrainRaiseLimit":
  170. settings.TerrainRaiseLimit = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  171. break;
  172. case "TerrainLowerLimit":
  173. settings.TerrainLowerLimit = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  174. break;
  175. case "UseEstateSun":
  176. settings.UseEstateSun = bool.Parse(xtr.ReadElementContentAsString());
  177. break;
  178. case "FixedSun":
  179. settings.FixedSun = bool.Parse(xtr.ReadElementContentAsString());
  180. break;
  181. case "SunPosition":
  182. settings.SunPosition = double.Parse(xtr.ReadElementContentAsString());
  183. break;
  184. }
  185. }
  186. xtr.ReadEndElement();
  187. if (xtr.IsStartElement("Telehub"))
  188. {
  189. if (xtr.IsEmptyElement)
  190. xtr.Read();
  191. else
  192. {
  193. xtr.ReadStartElement("Telehub");
  194. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  195. {
  196. switch (xtr.Name)
  197. {
  198. case "TelehubObject":
  199. settings.TelehubObject = UUID.Parse(xtr.ReadElementContentAsString());
  200. break;
  201. case "SpawnPoint":
  202. string str = xtr.ReadElementContentAsString();
  203. SpawnPoint sp = SpawnPoint.Parse(str);
  204. settings.AddSpawnPoint(sp);
  205. break;
  206. }
  207. }
  208. xtr.ReadEndElement();
  209. }
  210. }
  211. if (xtr.IsStartElement("Environment"))
  212. {
  213. if (xtr.IsEmptyElement)
  214. xtr.Read();
  215. else
  216. {
  217. xtr.ReadStartElement("Environment");
  218. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  219. {
  220. switch (xtr.Name)
  221. {
  222. case "data":
  223. regionEnv = ViewerEnvironment.FromOSDString(xtr.ReadElementContentAsString());
  224. break;
  225. }
  226. }
  227. xtr.ReadEndElement();
  228. }
  229. }
  230. if (xtr.IsStartElement("Estate"))
  231. {
  232. if (xtr.IsEmptyElement)
  233. xtr.Read();
  234. else
  235. {
  236. xtr.ReadStartElement("Estate");
  237. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  238. {
  239. switch (xtr.Name)
  240. {
  241. case "AllowDirectTeleport":
  242. estateSettings.AllowDirectTeleport = bool.Parse(xtr.ReadElementContentAsString());
  243. break;
  244. case "AllowEnvironmentOverride":
  245. estateSettings.AllowEnvironmentOverride = bool.Parse(xtr.ReadElementContentAsString());
  246. break;
  247. }
  248. }
  249. xtr.ReadEndElement();
  250. }
  251. }
  252. xtr.Close();
  253. sr.Close();
  254. return settings;
  255. }
  256. public static string Serialize(RegionSettings settings, ViewerEnvironment RegionEnv, EstateSettings estateSettings)
  257. {
  258. StringWriter sw = new StringWriter();
  259. XmlTextWriter xtw = new XmlTextWriter(sw);
  260. xtw.Formatting = Formatting.Indented;
  261. xtw.WriteStartDocument();
  262. xtw.WriteStartElement("RegionSettings");
  263. xtw.WriteStartElement("General");
  264. xtw.WriteElementString("AllowDamage", settings.AllowDamage.ToString());
  265. xtw.WriteElementString("AllowLandResell", settings.AllowLandResell.ToString());
  266. xtw.WriteElementString("AllowLandJoinDivide", settings.AllowLandJoinDivide.ToString());
  267. xtw.WriteElementString("BlockFly", settings.BlockFly.ToString());
  268. xtw.WriteElementString("BlockLandShowInSearch", settings.BlockShowInSearch.ToString());
  269. xtw.WriteElementString("BlockTerraform", settings.BlockTerraform.ToString());
  270. xtw.WriteElementString("DisableCollisions", settings.DisableCollisions.ToString());
  271. xtw.WriteElementString("DisablePhysics", settings.DisablePhysics.ToString());
  272. xtw.WriteElementString("DisableScripts", settings.DisableScripts.ToString());
  273. xtw.WriteElementString("MaturityRating", settings.Maturity.ToString());
  274. xtw.WriteElementString("RestrictPushing", settings.RestrictPushing.ToString());
  275. xtw.WriteElementString("AgentLimit", settings.AgentLimit.ToString());
  276. xtw.WriteElementString("ObjectBonus", settings.ObjectBonus.ToString());
  277. xtw.WriteEndElement();
  278. xtw.WriteStartElement("GroundTextures");
  279. xtw.WriteElementString("Texture1", settings.TerrainTexture1.ToString());
  280. xtw.WriteElementString("Texture2", settings.TerrainTexture2.ToString());
  281. xtw.WriteElementString("Texture3", settings.TerrainTexture3.ToString());
  282. xtw.WriteElementString("Texture4", settings.TerrainTexture4.ToString());
  283. xtw.WriteElementString("ElevationLowSW", settings.Elevation1SW.ToString());
  284. xtw.WriteElementString("ElevationLowNW", settings.Elevation1NW.ToString());
  285. xtw.WriteElementString("ElevationLowSE", settings.Elevation1SE.ToString());
  286. xtw.WriteElementString("ElevationLowNE", settings.Elevation1NE.ToString());
  287. xtw.WriteElementString("ElevationHighSW", settings.Elevation2SW.ToString());
  288. xtw.WriteElementString("ElevationHighNW", settings.Elevation2NW.ToString());
  289. xtw.WriteElementString("ElevationHighSE", settings.Elevation2SE.ToString());
  290. xtw.WriteElementString("ElevationHighNE", settings.Elevation2NE.ToString());
  291. xtw.WriteEndElement();
  292. xtw.WriteStartElement("Terrain");
  293. xtw.WriteElementString("WaterHeight", settings.WaterHeight.ToString());
  294. xtw.WriteElementString("TerrainRaiseLimit", settings.TerrainRaiseLimit.ToString());
  295. xtw.WriteElementString("TerrainLowerLimit", settings.TerrainLowerLimit.ToString());
  296. xtw.WriteElementString("UseEstateSun", settings.UseEstateSun.ToString());
  297. xtw.WriteElementString("FixedSun", settings.FixedSun.ToString());
  298. xtw.WriteElementString("SunPosition", settings.SunPosition.ToString());
  299. xtw.WriteEndElement();
  300. xtw.WriteStartElement("Telehub");
  301. if (!settings.TelehubObject.IsZero())
  302. {
  303. xtw.WriteElementString("TelehubObject", settings.TelehubObject.ToString());
  304. foreach (SpawnPoint sp in settings.SpawnPoints())
  305. xtw.WriteElementString("SpawnPoint", sp.ToString());
  306. }
  307. xtw.WriteEndElement();
  308. if (RegionEnv != null)
  309. {
  310. xtw.WriteStartElement("Environment");
  311. xtw.WriteElementString("data", ViewerEnvironment.ToOSDString(RegionEnv));
  312. xtw.WriteEndElement();
  313. }
  314. xtw.WriteStartElement("Estate");
  315. xtw.WriteElementString("AllowDirectTeleport", estateSettings.AllowDirectTeleport.ToString());
  316. xtw.WriteElementString("AllowEnvironmentOverride", estateSettings.AllowEnvironmentOverride.ToString());
  317. xtw.WriteEndElement();
  318. xtw.WriteEndElement();
  319. xtw.Close();
  320. sw.Close();
  321. return sw.ToString();
  322. }
  323. }
  324. }