RegionSettingsSerializer.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 "PBR1":
  135. settings.TerrainPBR1 = UUID.Parse(xtr.ReadElementContentAsString());
  136. break;
  137. case "PBR2":
  138. settings.TerrainPBR2 = UUID.Parse(xtr.ReadElementContentAsString());
  139. break;
  140. case "PBR3":
  141. settings.TerrainPBR3 = UUID.Parse(xtr.ReadElementContentAsString());
  142. break;
  143. case "PBR4":
  144. settings.TerrainPBR4 = UUID.Parse(xtr.ReadElementContentAsString());
  145. break;
  146. case "ElevationLowSW":
  147. settings.Elevation1SW = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  148. break;
  149. case "ElevationLowNW":
  150. settings.Elevation1NW = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  151. break;
  152. case "ElevationLowSE":
  153. settings.Elevation1SE = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  154. break;
  155. case "ElevationLowNE":
  156. settings.Elevation1NE = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  157. break;
  158. case "ElevationHighSW":
  159. settings.Elevation2SW = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  160. break;
  161. case "ElevationHighNW":
  162. settings.Elevation2NW = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  163. break;
  164. case "ElevationHighSE":
  165. settings.Elevation2SE = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  166. break;
  167. case "ElevationHighNE":
  168. settings.Elevation2NE = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  169. break;
  170. }
  171. }
  172. xtr.ReadEndElement();
  173. xtr.ReadStartElement("Terrain");
  174. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  175. {
  176. switch (xtr.Name)
  177. {
  178. case "WaterHeight":
  179. settings.WaterHeight = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  180. break;
  181. case "TerrainRaiseLimit":
  182. settings.TerrainRaiseLimit = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  183. break;
  184. case "TerrainLowerLimit":
  185. settings.TerrainLowerLimit = double.Parse(xtr.ReadElementContentAsString(), Culture.NumberFormatInfo);
  186. break;
  187. case "UseEstateSun":
  188. settings.UseEstateSun = bool.Parse(xtr.ReadElementContentAsString());
  189. break;
  190. case "FixedSun":
  191. settings.FixedSun = bool.Parse(xtr.ReadElementContentAsString());
  192. break;
  193. case "SunPosition":
  194. settings.SunPosition = double.Parse(xtr.ReadElementContentAsString());
  195. break;
  196. }
  197. }
  198. xtr.ReadEndElement();
  199. if (xtr.IsStartElement("Telehub"))
  200. {
  201. if (xtr.IsEmptyElement)
  202. xtr.Read();
  203. else
  204. {
  205. xtr.ReadStartElement("Telehub");
  206. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  207. {
  208. switch (xtr.Name)
  209. {
  210. case "TelehubObject":
  211. settings.TelehubObject = UUID.Parse(xtr.ReadElementContentAsString());
  212. break;
  213. case "SpawnPoint":
  214. string str = xtr.ReadElementContentAsString();
  215. SpawnPoint sp = SpawnPoint.Parse(str);
  216. settings.AddSpawnPoint(sp);
  217. break;
  218. }
  219. }
  220. xtr.ReadEndElement();
  221. }
  222. }
  223. if (xtr.IsStartElement("Environment"))
  224. {
  225. if (xtr.IsEmptyElement)
  226. xtr.Read();
  227. else
  228. {
  229. xtr.ReadStartElement("Environment");
  230. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  231. {
  232. switch (xtr.Name)
  233. {
  234. case "data":
  235. regionEnv = ViewerEnvironment.FromOSDString(xtr.ReadElementContentAsString());
  236. break;
  237. }
  238. }
  239. xtr.ReadEndElement();
  240. }
  241. }
  242. if (xtr.IsStartElement("Estate"))
  243. {
  244. if (xtr.IsEmptyElement)
  245. xtr.Read();
  246. else
  247. {
  248. xtr.ReadStartElement("Estate");
  249. while (xtr.Read() && xtr.NodeType != XmlNodeType.EndElement)
  250. {
  251. switch (xtr.Name)
  252. {
  253. case "AllowDirectTeleport":
  254. estateSettings.AllowDirectTeleport = bool.Parse(xtr.ReadElementContentAsString());
  255. break;
  256. case "AllowEnvironmentOverride":
  257. estateSettings.AllowEnvironmentOverride = bool.Parse(xtr.ReadElementContentAsString());
  258. break;
  259. }
  260. }
  261. xtr.ReadEndElement();
  262. }
  263. }
  264. xtr.Close();
  265. sr.Close();
  266. return settings;
  267. }
  268. public static string Serialize(RegionSettings settings, ViewerEnvironment RegionEnv, EstateSettings estateSettings)
  269. {
  270. StringWriter sw = new StringWriter();
  271. XmlTextWriter xtw = new XmlTextWriter(sw);
  272. xtw.Formatting = Formatting.Indented;
  273. xtw.WriteStartDocument();
  274. xtw.WriteStartElement("RegionSettings");
  275. xtw.WriteStartElement("General");
  276. xtw.WriteElementString("AllowDamage", settings.AllowDamage.ToString());
  277. xtw.WriteElementString("AllowLandResell", settings.AllowLandResell.ToString());
  278. xtw.WriteElementString("AllowLandJoinDivide", settings.AllowLandJoinDivide.ToString());
  279. xtw.WriteElementString("BlockFly", settings.BlockFly.ToString());
  280. xtw.WriteElementString("BlockLandShowInSearch", settings.BlockShowInSearch.ToString());
  281. xtw.WriteElementString("BlockTerraform", settings.BlockTerraform.ToString());
  282. xtw.WriteElementString("DisableCollisions", settings.DisableCollisions.ToString());
  283. xtw.WriteElementString("DisablePhysics", settings.DisablePhysics.ToString());
  284. xtw.WriteElementString("DisableScripts", settings.DisableScripts.ToString());
  285. xtw.WriteElementString("MaturityRating", settings.Maturity.ToString());
  286. xtw.WriteElementString("RestrictPushing", settings.RestrictPushing.ToString());
  287. xtw.WriteElementString("AgentLimit", settings.AgentLimit.ToString());
  288. xtw.WriteElementString("ObjectBonus", settings.ObjectBonus.ToString());
  289. xtw.WriteEndElement();
  290. xtw.WriteStartElement("GroundTextures");
  291. xtw.WriteElementString("Texture1", settings.TerrainTexture1.ToString());
  292. xtw.WriteElementString("Texture2", settings.TerrainTexture2.ToString());
  293. xtw.WriteElementString("Texture3", settings.TerrainTexture3.ToString());
  294. xtw.WriteElementString("Texture4", settings.TerrainTexture4.ToString());
  295. xtw.WriteElementString("PBR1", settings.TerrainPBR1.ToString());
  296. xtw.WriteElementString("PBR2", settings.TerrainPBR2.ToString());
  297. xtw.WriteElementString("PBR3", settings.TerrainPBR3.ToString());
  298. xtw.WriteElementString("PBR4", settings.TerrainPBR4.ToString());
  299. xtw.WriteElementString("ElevationLowSW", settings.Elevation1SW.ToString());
  300. xtw.WriteElementString("ElevationLowNW", settings.Elevation1NW.ToString());
  301. xtw.WriteElementString("ElevationLowSE", settings.Elevation1SE.ToString());
  302. xtw.WriteElementString("ElevationLowNE", settings.Elevation1NE.ToString());
  303. xtw.WriteElementString("ElevationHighSW", settings.Elevation2SW.ToString());
  304. xtw.WriteElementString("ElevationHighNW", settings.Elevation2NW.ToString());
  305. xtw.WriteElementString("ElevationHighSE", settings.Elevation2SE.ToString());
  306. xtw.WriteElementString("ElevationHighNE", settings.Elevation2NE.ToString());
  307. xtw.WriteEndElement();
  308. xtw.WriteStartElement("Terrain");
  309. xtw.WriteElementString("WaterHeight", settings.WaterHeight.ToString());
  310. xtw.WriteElementString("TerrainRaiseLimit", settings.TerrainRaiseLimit.ToString());
  311. xtw.WriteElementString("TerrainLowerLimit", settings.TerrainLowerLimit.ToString());
  312. xtw.WriteElementString("UseEstateSun", settings.UseEstateSun.ToString());
  313. xtw.WriteElementString("FixedSun", settings.FixedSun.ToString());
  314. xtw.WriteElementString("SunPosition", settings.SunPosition.ToString());
  315. xtw.WriteEndElement();
  316. xtw.WriteStartElement("Telehub");
  317. if (!settings.TelehubObject.IsZero())
  318. {
  319. xtw.WriteElementString("TelehubObject", settings.TelehubObject.ToString());
  320. foreach (SpawnPoint sp in settings.SpawnPoints())
  321. xtw.WriteElementString("SpawnPoint", sp.ToString());
  322. }
  323. xtw.WriteEndElement();
  324. if (RegionEnv != null)
  325. {
  326. xtw.WriteStartElement("Environment");
  327. xtw.WriteElementString("data", ViewerEnvironment.ToOSDString(RegionEnv));
  328. xtw.WriteEndElement();
  329. }
  330. xtw.WriteStartElement("Estate");
  331. xtw.WriteElementString("AllowDirectTeleport", estateSettings.AllowDirectTeleport.ToString());
  332. xtw.WriteElementString("AllowEnvironmentOverride", estateSettings.AllowEnvironmentOverride.ToString());
  333. xtw.WriteEndElement();
  334. xtw.WriteEndElement();
  335. xtw.Close();
  336. sw.Close();
  337. return sw.ToString();
  338. }
  339. }
  340. }