ScriptSerializer.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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;
  28. using System.IO;
  29. using System.Threading;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Security.Policy;
  33. using System.Reflection;
  34. using System.Globalization;
  35. using System.Xml;
  36. using OpenMetaverse;
  37. using log4net;
  38. using Nini.Config;
  39. using Amib.Threading;
  40. using OpenSim.Framework;
  41. using OpenSim.Region.CoreModules;
  42. using OpenSim.Region.Framework.Scenes;
  43. using OpenSim.Region.Framework.Interfaces;
  44. using OpenSim.Region.ScriptEngine.Shared;
  45. using OpenSim.Region.ScriptEngine.Shared.Api;
  46. using OpenSim.Region.ScriptEngine.Shared.ScriptBase;
  47. using OpenSim.Region.ScriptEngine.Shared.CodeTools;
  48. using OpenSim.Region.ScriptEngine.Interfaces;
  49. namespace OpenSim.Region.ScriptEngine.Shared.Instance
  50. {
  51. public class ScriptSerializer
  52. {
  53. public static string Serialize(ScriptInstance instance)
  54. {
  55. bool running = instance.Running;
  56. XmlDocument xmldoc = new XmlDocument();
  57. XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration,
  58. "", "");
  59. xmldoc.AppendChild(xmlnode);
  60. XmlElement rootElement = xmldoc.CreateElement("", "ScriptState",
  61. "");
  62. xmldoc.AppendChild(rootElement);
  63. XmlElement state = xmldoc.CreateElement("", "State", "");
  64. state.AppendChild(xmldoc.CreateTextNode(instance.State));
  65. rootElement.AppendChild(state);
  66. XmlElement run = xmldoc.CreateElement("", "Running", "");
  67. run.AppendChild(xmldoc.CreateTextNode(
  68. running.ToString()));
  69. rootElement.AppendChild(run);
  70. Dictionary<string, Object> vars = instance.GetVars();
  71. XmlElement variables = xmldoc.CreateElement("", "Variables", "");
  72. foreach (KeyValuePair<string, Object> var in vars)
  73. WriteTypedValue(xmldoc, variables, "Variable", var.Key,
  74. var.Value);
  75. rootElement.AppendChild(variables);
  76. XmlElement queue = xmldoc.CreateElement("", "Queue", "");
  77. int count = instance.EventQueue.Count;
  78. while (count > 0)
  79. {
  80. EventParams ep = (EventParams)instance.EventQueue.Dequeue();
  81. instance.EventQueue.Enqueue(ep);
  82. count--;
  83. XmlElement item = xmldoc.CreateElement("", "Item", "");
  84. XmlAttribute itemEvent = xmldoc.CreateAttribute("", "event",
  85. "");
  86. itemEvent.Value = ep.EventName;
  87. item.Attributes.Append(itemEvent);
  88. XmlElement parms = xmldoc.CreateElement("", "Params", "");
  89. foreach (Object o in ep.Params)
  90. WriteTypedValue(xmldoc, parms, "Param", String.Empty, o);
  91. item.AppendChild(parms);
  92. XmlElement detect = xmldoc.CreateElement("", "Detected", "");
  93. foreach (DetectParams det in ep.DetectParams)
  94. {
  95. XmlElement objectElem = xmldoc.CreateElement("", "Object", "");
  96. XmlAttribute pos = xmldoc.CreateAttribute("", "pos", "");
  97. pos.Value = det.OffsetPos.ToString();
  98. objectElem.Attributes.Append(pos);
  99. XmlAttribute d_linkNum = xmldoc.CreateAttribute("", "linkNum", "");
  100. d_linkNum.Value = det.LinkNum.ToString();
  101. objectElem.Attributes.Append(d_linkNum);
  102. XmlAttribute d_group = xmldoc.CreateAttribute("", "group", "");
  103. d_group.Value = det.Group.ToString();
  104. objectElem.Attributes.Append(d_group);
  105. XmlAttribute d_name = xmldoc.CreateAttribute("", "name", "");
  106. d_name.Value = det.Name.ToString();
  107. objectElem.Attributes.Append(d_name);
  108. XmlAttribute d_owner = xmldoc.CreateAttribute("", "owner", "");
  109. d_owner.Value = det.Owner.ToString();
  110. objectElem.Attributes.Append(d_owner);
  111. XmlAttribute d_position = xmldoc.CreateAttribute("", "position", "");
  112. d_position.Value = det.Position.ToString();
  113. objectElem.Attributes.Append(d_position);
  114. XmlAttribute d_rotation = xmldoc.CreateAttribute("", "rotation", "");
  115. d_rotation.Value = det.Rotation.ToString();
  116. objectElem.Attributes.Append(d_rotation);
  117. XmlAttribute d_type = xmldoc.CreateAttribute("", "type", "");
  118. d_type.Value = det.Type.ToString();
  119. objectElem.Attributes.Append(d_type);
  120. XmlAttribute d_velocity = xmldoc.CreateAttribute("", "velocity", "");
  121. d_velocity.Value = det.Velocity.ToString();
  122. objectElem.Attributes.Append(d_velocity);
  123. objectElem.AppendChild(
  124. xmldoc.CreateTextNode(det.Key.ToString()));
  125. detect.AppendChild(objectElem);
  126. }
  127. item.AppendChild(detect);
  128. queue.AppendChild(item);
  129. }
  130. rootElement.AppendChild(queue);
  131. XmlNode plugins = xmldoc.CreateElement("", "Plugins", "");
  132. DumpList(xmldoc, plugins,
  133. new LSL_Types.list(instance.PluginData));
  134. rootElement.AppendChild(plugins);
  135. if (instance.ScriptTask != null)
  136. {
  137. if (instance.ScriptTask.PermsMask != 0 && instance.ScriptTask.PermsGranter != UUID.Zero)
  138. {
  139. XmlNode permissions = xmldoc.CreateElement("", "Permissions", "");
  140. XmlAttribute granter = xmldoc.CreateAttribute("", "granter", "");
  141. granter.Value = instance.ScriptTask.PermsGranter.ToString();
  142. permissions.Attributes.Append(granter);
  143. XmlAttribute mask = xmldoc.CreateAttribute("", "mask", "");
  144. mask.Value = instance.ScriptTask.PermsMask.ToString();
  145. permissions.Attributes.Append(mask);
  146. rootElement.AppendChild(permissions);
  147. }
  148. }
  149. if (instance.MinEventDelay > 0.0)
  150. {
  151. XmlElement eventDelay = xmldoc.CreateElement("", "MinEventDelay", "");
  152. eventDelay.AppendChild(xmldoc.CreateTextNode(instance.MinEventDelay.ToString()));
  153. rootElement.AppendChild(eventDelay);
  154. }
  155. return xmldoc.InnerXml;
  156. }
  157. public static void Deserialize(string xml, ScriptInstance instance)
  158. {
  159. XmlDocument doc = new XmlDocument();
  160. Dictionary<string, object> vars = instance.GetVars();
  161. instance.PluginData = new Object[0];
  162. doc.LoadXml(xml);
  163. XmlNodeList rootL = doc.GetElementsByTagName("ScriptState");
  164. if (rootL.Count != 1)
  165. {
  166. return;
  167. }
  168. XmlNode rootNode = rootL[0];
  169. if (rootNode != null)
  170. {
  171. object varValue;
  172. XmlNodeList partL = rootNode.ChildNodes;
  173. foreach (XmlNode part in partL)
  174. {
  175. switch (part.Name)
  176. {
  177. case "State":
  178. instance.State=part.InnerText;
  179. break;
  180. case "Running":
  181. instance.Running=bool.Parse(part.InnerText);
  182. break;
  183. case "Variables":
  184. XmlNodeList varL = part.ChildNodes;
  185. foreach (XmlNode var in varL)
  186. {
  187. string varName;
  188. varValue=ReadTypedValue(var, out varName);
  189. if (vars.ContainsKey(varName))
  190. vars[varName] = varValue;
  191. }
  192. instance.SetVars(vars);
  193. break;
  194. case "Queue":
  195. XmlNodeList itemL = part.ChildNodes;
  196. foreach (XmlNode item in itemL)
  197. {
  198. List<Object> parms = new List<Object>();
  199. List<DetectParams> detected =
  200. new List<DetectParams>();
  201. string eventName =
  202. item.Attributes.GetNamedItem("event").Value;
  203. XmlNodeList eventL = item.ChildNodes;
  204. foreach (XmlNode evt in eventL)
  205. {
  206. switch (evt.Name)
  207. {
  208. case "Params":
  209. XmlNodeList prms = evt.ChildNodes;
  210. foreach (XmlNode pm in prms)
  211. parms.Add(ReadTypedValue(pm));
  212. break;
  213. case "Detected":
  214. XmlNodeList detL = evt.ChildNodes;
  215. foreach (XmlNode det in detL)
  216. {
  217. string vect =
  218. det.Attributes.GetNamedItem(
  219. "pos").Value;
  220. LSL_Types.Vector3 v =
  221. new LSL_Types.Vector3(vect);
  222. int d_linkNum=0;
  223. UUID d_group = UUID.Zero;
  224. string d_name = String.Empty;
  225. UUID d_owner = UUID.Zero;
  226. LSL_Types.Vector3 d_position =
  227. new LSL_Types.Vector3();
  228. LSL_Types.Quaternion d_rotation =
  229. new LSL_Types.Quaternion();
  230. int d_type = 0;
  231. LSL_Types.Vector3 d_velocity =
  232. new LSL_Types.Vector3();
  233. try
  234. {
  235. string tmp;
  236. tmp = det.Attributes.GetNamedItem(
  237. "linkNum").Value;
  238. int.TryParse(tmp, out d_linkNum);
  239. tmp = det.Attributes.GetNamedItem(
  240. "group").Value;
  241. UUID.TryParse(tmp, out d_group);
  242. d_name = det.Attributes.GetNamedItem(
  243. "name").Value;
  244. tmp = det.Attributes.GetNamedItem(
  245. "owner").Value;
  246. UUID.TryParse(tmp, out d_owner);
  247. tmp = det.Attributes.GetNamedItem(
  248. "position").Value;
  249. d_position =
  250. new LSL_Types.Vector3(tmp);
  251. tmp = det.Attributes.GetNamedItem(
  252. "rotation").Value;
  253. d_rotation =
  254. new LSL_Types.Quaternion(tmp);
  255. tmp = det.Attributes.GetNamedItem(
  256. "type").Value;
  257. int.TryParse(tmp, out d_type);
  258. tmp = det.Attributes.GetNamedItem(
  259. "velocity").Value;
  260. d_velocity =
  261. new LSL_Types.Vector3(tmp);
  262. }
  263. catch (Exception) // Old version XML
  264. {
  265. }
  266. UUID uuid = new UUID();
  267. UUID.TryParse(det.InnerText,
  268. out uuid);
  269. DetectParams d = new DetectParams();
  270. d.Key = uuid;
  271. d.OffsetPos = v;
  272. d.LinkNum = d_linkNum;
  273. d.Group = d_group;
  274. d.Name = d_name;
  275. d.Owner = d_owner;
  276. d.Position = d_position;
  277. d.Rotation = d_rotation;
  278. d.Type = d_type;
  279. d.Velocity = d_velocity;
  280. detected.Add(d);
  281. }
  282. break;
  283. }
  284. }
  285. EventParams ep = new EventParams(
  286. eventName, parms.ToArray(),
  287. detected.ToArray());
  288. instance.EventQueue.Enqueue(ep);
  289. }
  290. break;
  291. case "Plugins":
  292. instance.PluginData = ReadList(part).Data;
  293. break;
  294. case "Permissions":
  295. string tmpPerm;
  296. int mask = 0;
  297. tmpPerm = part.Attributes.GetNamedItem("mask").Value;
  298. if (tmpPerm != null)
  299. {
  300. int.TryParse(tmpPerm, out mask);
  301. if (mask != 0)
  302. {
  303. tmpPerm = part.Attributes.GetNamedItem("granter").Value;
  304. if (tmpPerm != null)
  305. {
  306. UUID granter = new UUID();
  307. UUID.TryParse(tmpPerm, out granter);
  308. if (granter != UUID.Zero)
  309. {
  310. instance.ScriptTask.PermsMask = mask;
  311. instance.ScriptTask.PermsGranter = granter;
  312. }
  313. }
  314. }
  315. }
  316. break;
  317. case "MinEventDelay":
  318. double minEventDelay = 0.0;
  319. double.TryParse(part.InnerText, NumberStyles.Float, Culture.NumberFormatInfo, out minEventDelay);
  320. instance.MinEventDelay = minEventDelay;
  321. break;
  322. }
  323. }
  324. }
  325. }
  326. private static void DumpList(XmlDocument doc, XmlNode parent,
  327. LSL_Types.list l)
  328. {
  329. foreach (Object o in l.Data)
  330. WriteTypedValue(doc, parent, "ListItem", "", o);
  331. }
  332. private static LSL_Types.list ReadList(XmlNode parent)
  333. {
  334. List<Object> olist = new List<Object>();
  335. XmlNodeList itemL = parent.ChildNodes;
  336. foreach (XmlNode item in itemL)
  337. olist.Add(ReadTypedValue(item));
  338. return new LSL_Types.list(olist.ToArray());
  339. }
  340. private static void WriteTypedValue(XmlDocument doc, XmlNode parent,
  341. string tag, string name, object value)
  342. {
  343. Type t=value.GetType();
  344. XmlAttribute typ = doc.CreateAttribute("", "type", "");
  345. XmlNode n = doc.CreateElement("", tag, "");
  346. if (value is LSL_Types.list)
  347. {
  348. typ.Value = "list";
  349. n.Attributes.Append(typ);
  350. DumpList(doc, n, (LSL_Types.list) value);
  351. if (name != String.Empty)
  352. {
  353. XmlAttribute nam = doc.CreateAttribute("", "name", "");
  354. nam.Value = name;
  355. n.Attributes.Append(nam);
  356. }
  357. parent.AppendChild(n);
  358. return;
  359. }
  360. n.AppendChild(doc.CreateTextNode(value.ToString()));
  361. typ.Value = t.ToString();
  362. n.Attributes.Append(typ);
  363. if (name != String.Empty)
  364. {
  365. XmlAttribute nam = doc.CreateAttribute("", "name", "");
  366. nam.Value = name;
  367. n.Attributes.Append(nam);
  368. }
  369. parent.AppendChild(n);
  370. }
  371. private static object ReadTypedValue(XmlNode tag, out string name)
  372. {
  373. name = tag.Attributes.GetNamedItem("name").Value;
  374. return ReadTypedValue(tag);
  375. }
  376. private static object ReadTypedValue(XmlNode tag)
  377. {
  378. Object varValue;
  379. string assembly;
  380. string itemType = tag.Attributes.GetNamedItem("type").Value;
  381. if (itemType == "list")
  382. return ReadList(tag);
  383. if (itemType == "OpenMetaverse.UUID")
  384. {
  385. UUID val = new UUID();
  386. UUID.TryParse(tag.InnerText, out val);
  387. return val;
  388. }
  389. Type itemT = Type.GetType(itemType);
  390. if (itemT == null)
  391. {
  392. Object[] args =
  393. new Object[] { tag.InnerText };
  394. assembly = itemType+", OpenSim.Region.ScriptEngine.Shared";
  395. itemT = Type.GetType(assembly);
  396. if (itemT == null)
  397. return null;
  398. varValue = Activator.CreateInstance(itemT, args);
  399. if (varValue == null)
  400. return null;
  401. }
  402. else
  403. {
  404. varValue = Convert.ChangeType(tag.InnerText, itemT);
  405. }
  406. return varValue;
  407. }
  408. }
  409. }