1
0

ScriptSerializer.cs 20 KB

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