XMRInstCtor.cs 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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.Threading;
  29. using System.Reflection;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Runtime.Remoting.Lifetime;
  33. using System.Security.Policy;
  34. using System.IO;
  35. using System.Xml;
  36. using System.Text;
  37. using OpenMetaverse;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.ScriptEngine.Interfaces;
  40. using OpenSim.Region.ScriptEngine.Shared;
  41. using OpenSim.Region.ScriptEngine.Shared.Api;
  42. using OpenSim.Region.ScriptEngine.Shared.ScriptBase;
  43. using OpenSim.Region.ScriptEngine.Yengine;
  44. using OpenSim.Region.Framework.Scenes;
  45. using log4net;
  46. using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat;
  47. using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger;
  48. using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
  49. using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
  50. using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
  51. using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
  52. using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;
  53. namespace OpenSim.Region.ScriptEngine.Yengine
  54. {
  55. public partial class XMRInstance
  56. {
  57. /****************************************************************************\
  58. * The only method of interest to outside this module is the Initializer. *
  59. * *
  60. * The rest of this module contains support routines for the Initializer. *
  61. \****************************************************************************/
  62. /**
  63. * @brief Initializer, loads script in memory and all ready for running.
  64. * @param engine = YEngine instance this is part of
  65. * @param scriptBasePath = directory name where files are
  66. * @param stackSize = number of bytes to allocate for stacks
  67. * @param errors = return compiler errors in this array
  68. * @param forceRecomp = force recompile
  69. * Throws exception if any error, so it was successful if it returns.
  70. */
  71. public void Initialize(Yengine engine, string scriptBasePath,
  72. int stackSize, int heapSize, ArrayList errors)
  73. {
  74. if(stackSize < 16384)
  75. stackSize = 16384;
  76. if(heapSize < 16384)
  77. heapSize = 16384;
  78. // Save all call parameters in instance vars for easy access.
  79. m_Engine = engine;
  80. m_ScriptBasePath = scriptBasePath;
  81. m_StackSize = stackSize;
  82. m_StackLeft = stackSize;
  83. m_HeapSize = heapSize;
  84. m_CompilerErrors = errors;
  85. m_StateFileName = GetStateFileName(scriptBasePath, m_ItemID);
  86. // Not in any XMRInstQueue.
  87. m_NextInst = this;
  88. m_PrevInst = this;
  89. // Set up list of API calls it has available.
  90. // This also gets the API modules ready to accept setup data, such as
  91. // active listeners being restored.
  92. IScriptApi scriptApi;
  93. ApiManager am = new ApiManager();
  94. foreach(string api in am.GetApis())
  95. {
  96. // Instantiate the API for this script instance.
  97. if(api != "LSL")
  98. scriptApi = am.CreateApi(api);
  99. else
  100. scriptApi = m_XMRLSLApi = new XMRLSL_Api();
  101. // Connect it up to the instance.
  102. InitScriptApi(engine, api, scriptApi);
  103. }
  104. m_XMRLSLApi.InitXMRLSLApi(this);
  105. // Get object loaded, compiling script and reading .state file as
  106. // necessary to restore the state.
  107. suspendOnCheckRunHold = true;
  108. InstantiateScript();
  109. m_SourceCode = null;
  110. if(m_ObjCode == null)
  111. throw new ArgumentNullException("m_ObjCode");
  112. if(m_ObjCode.scriptEventHandlerTable == null)
  113. throw new ArgumentNullException("m_ObjCode.scriptEventHandlerTable");
  114. suspendOnCheckRunHold = false;
  115. suspendOnCheckRunTemp = false;
  116. // Declare which events the script's current state can handle.
  117. int eventMask = GetStateEventFlags(stateCode);
  118. m_Part.SetScriptEvents(m_ItemID, eventMask);
  119. }
  120. private void InitScriptApi(Yengine engine, string api, IScriptApi scriptApi)
  121. {
  122. // Set up m_ApiManager_<APINAME> = instance pointer.
  123. engine.m_XMRInstanceApiCtxFieldInfos[api].SetValue(this, scriptApi);
  124. // Initialize the API instance.
  125. scriptApi.Initialize(m_Engine, m_Part, m_Item);
  126. this.InitApi(api, scriptApi);
  127. }
  128. /*
  129. * Get script object code loaded in memory and all ready to run,
  130. * ready to resume it from where the .state file says it was last
  131. */
  132. private void InstantiateScript()
  133. {
  134. bool compiledIt = false;
  135. ScriptObjCode objCode;
  136. // If source code string is empty, use the asset ID as the object file name.
  137. // Allow lines of // comments at the beginning (for such as engine selection).
  138. int i, j, len;
  139. if(m_SourceCode == null)
  140. m_SourceCode = String.Empty;
  141. for(len = m_SourceCode.Length; len > 0; --len)
  142. {
  143. if(m_SourceCode[len - 1] > ' ')
  144. break;
  145. }
  146. for(i = 0; i < len; i++)
  147. {
  148. char c = m_SourceCode[i];
  149. if(c <= ' ')
  150. continue;
  151. if(c != '/')
  152. break;
  153. if((i + 1 >= len) || (m_SourceCode[i + 1] != '/'))
  154. break;
  155. i = m_SourceCode.IndexOf('\n', i);
  156. if(i < 0)
  157. i = len - 1;
  158. }
  159. if((i >= len))
  160. {
  161. // Source consists of nothing but // comments and whitespace,
  162. // or we are being forced to use the asset-id as the key, to
  163. // open an already existing object code file.
  164. m_ScriptObjCodeKey = m_Item.AssetID.ToString();
  165. if(i >= len)
  166. m_SourceCode = "";
  167. }
  168. else
  169. {
  170. // Make up dictionary key for the object code.
  171. // Use the same object code for identical source code
  172. // regardless of asset ID, so we don't care if they
  173. // copy scripts or not.
  174. byte[] scbytes = System.Text.Encoding.UTF8.GetBytes(m_SourceCode);
  175. StringBuilder sb = new StringBuilder((256 + 5) / 6);
  176. using (System.Security.Cryptography.SHA256 sha = System.Security.Cryptography.SHA256.Create())
  177. ByteArrayToSixbitStr(sb, sha.ComputeHash(scbytes));
  178. m_ScriptObjCodeKey = sb.ToString();
  179. // But source code can be just a sixbit string itself
  180. // that identifies an already existing object code file.
  181. if(len - i == m_ScriptObjCodeKey.Length)
  182. {
  183. for(j = len; --j >= i;)
  184. {
  185. if(sixbit.IndexOf(m_SourceCode[j]) < 0)
  186. break;
  187. }
  188. if(j < i)
  189. {
  190. m_ScriptObjCodeKey = m_SourceCode.Substring(i, len - i);
  191. m_SourceCode = "";
  192. }
  193. }
  194. }
  195. // There may already be an ScriptObjCode struct in memory that
  196. // we can use. If not, try to compile it.
  197. lock(m_CompileLock)
  198. {
  199. if(!m_CompiledScriptObjCode.TryGetValue(m_ScriptObjCodeKey, out objCode) || m_ForceRecomp)
  200. {
  201. objCode = TryToCompile();
  202. compiledIt = true;
  203. }
  204. // Loaded successfully, increment reference count.
  205. // If we just compiled it though, reset count to 0 first as
  206. // this is the one-and-only existance of this objCode struct,
  207. // and we want any old ones for this source code to be garbage
  208. // collected.
  209. if(compiledIt)
  210. {
  211. m_CompiledScriptObjCode[m_ScriptObjCodeKey] = objCode;
  212. objCode.refCount = 0;
  213. }
  214. objCode.refCount++;
  215. // Now set up to decrement ref count on dispose.
  216. m_ObjCode = objCode;
  217. }
  218. try
  219. {
  220. // Fill in script instance from object code
  221. // Script instance is put in a "never-ever-has-run-before" state.
  222. LoadObjCode();
  223. // Fill in script intial state
  224. // - either as loaded from a .state file
  225. // - or initial default state_entry() event
  226. LoadInitialState();
  227. }
  228. catch
  229. {
  230. // If any error loading, decrement object code reference count.
  231. DecObjCodeRefCount();
  232. throw;
  233. }
  234. }
  235. private const string sixbit = "0123456789_abcdefghijklmnopqrstuvwxyz@ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  236. private static void ByteArrayToSixbitStr(StringBuilder sb, byte[] bytes)
  237. {
  238. int bit = 0;
  239. int val = 0;
  240. foreach(byte b in bytes)
  241. {
  242. val |= (int)((uint)b << bit);
  243. bit += 8;
  244. while(bit >= 6)
  245. {
  246. sb.Append(sixbit[val & 63]);
  247. val >>= 6;
  248. bit -= 6;
  249. }
  250. }
  251. if(bit > 0)
  252. sb.Append(sixbit[val & 63]);
  253. }
  254. // Try to create object code from source code
  255. // If error, just throw exception
  256. private ScriptObjCode TryToCompile()
  257. {
  258. m_CompilerErrors.Clear();
  259. // If object file exists, create ScriptObjCode directly from that.
  260. // Otherwise, compile the source to create object file then create
  261. // ScriptObjCode from that.
  262. string assetID = m_Item.AssetID.ToString();
  263. m_CameFrom = "asset://" + assetID;
  264. ScriptObjCode objCode = Compile();
  265. if(m_CompilerErrors.Count != 0)
  266. throw new Exception("compilation errors");
  267. if(objCode == null)
  268. throw new Exception("compilation failed");
  269. return objCode;
  270. }
  271. /*
  272. * Retrieve source from asset server.
  273. */
  274. private string FetchSource(string cameFrom)
  275. {
  276. m_log.Debug("[YEngine]: fetching source " + cameFrom);
  277. if(!cameFrom.StartsWith("asset://"))
  278. throw new Exception("unable to retrieve source from " + cameFrom);
  279. string assetID = cameFrom.Substring(8);
  280. AssetBase asset = m_Engine.World.AssetService.Get(assetID);
  281. if(asset == null)
  282. throw new Exception("source not found " + cameFrom);
  283. string source = Encoding.UTF8.GetString(asset.Data);
  284. if(EmptySource(source))
  285. throw new Exception("fetched source empty " + cameFrom);
  286. return source;
  287. }
  288. /*
  289. * Fill in script object initial contents.
  290. * Set the initial state to "default".
  291. */
  292. private void LoadObjCode()
  293. {
  294. // Script must leave this much stack remaining on calls to CheckRun().
  295. this.stackLimit = m_StackSize / 2;
  296. // This is how many total heap bytes script is allowed to use.
  297. this.heapLimit = m_HeapSize;
  298. // Allocate global variable arrays.
  299. this.glblVars.AllocVarArrays(m_ObjCode.glblSizes);
  300. // Script can handle these event codes.
  301. m_HaveEventHandlers = new bool[m_ObjCode.scriptEventHandlerTable.GetLength(1)];
  302. for(int i = m_ObjCode.scriptEventHandlerTable.GetLength(0); --i >= 0;)
  303. {
  304. for(int j = m_ObjCode.scriptEventHandlerTable.GetLength(1); --j >= 0;)
  305. {
  306. if(m_ObjCode.scriptEventHandlerTable[i, j] != null)
  307. {
  308. m_HaveEventHandlers[j] = true;
  309. }
  310. }
  311. }
  312. }
  313. /*
  314. * LoadInitialState()
  315. * if no state XML file exists for the asset,
  316. * post initial default state events
  317. * else
  318. * try to restore from .state file
  319. * If any error, throw exception
  320. */
  321. private void LoadInitialState()
  322. {
  323. // If no .state file exists, start from default state
  324. // Otherwise, read initial state from the .state file
  325. if(!File.Exists(m_StateFileName))
  326. {
  327. m_Running = true; // event processing is enabled
  328. eventCode = ScriptEventCode.None; // not processing any event
  329. // default state_entry() must initialize global variables
  330. doGblInit = true;
  331. stateCode = 0;
  332. PostEvent(new EventParams("state_entry",
  333. zeroObjectArray,
  334. zeroDetectParams));
  335. }
  336. else
  337. {
  338. try
  339. {
  340. string xml;
  341. using (FileStream fs = File.Open(m_StateFileName,
  342. FileMode.Open,
  343. FileAccess.Read))
  344. {
  345. using(StreamReader ss = new StreamReader(fs))
  346. xml = ss.ReadToEnd();
  347. }
  348. XmlDocument doc = new XmlDocument();
  349. doc.LoadXml(xml);
  350. LoadScriptState(doc);
  351. }
  352. catch
  353. {
  354. File.Delete(m_StateFileName);
  355. m_Running = true; // event processing is enabled
  356. eventCode = ScriptEventCode.None; // not processing any event
  357. // default state_entry() must initialize global variables
  358. glblVars.AllocVarArrays(m_ObjCode.glblSizes); // reset globals
  359. doGblInit = true;
  360. stateCode = 0;
  361. PostEvent(new EventParams("state_entry",
  362. zeroObjectArray,
  363. zeroDetectParams));
  364. }
  365. }
  366. // Post event(s) saying what caused the script to start.
  367. if(m_PostOnRez)
  368. {
  369. PostEvent(new EventParams("on_rez",
  370. new Object[] { m_StartParam },
  371. zeroDetectParams));
  372. }
  373. switch(m_StateSource)
  374. {
  375. case StateSource.AttachedRez:
  376. PostEvent(new EventParams("attach",
  377. new object[] { m_Part.ParentGroup.AttachedAvatar.ToString() },
  378. zeroDetectParams));
  379. break;
  380. case StateSource.PrimCrossing:
  381. PostEvent(new EventParams("changed",
  382. sbcCR,
  383. zeroDetectParams));
  384. break;
  385. case StateSource.Teleporting:
  386. PostEvent(new EventParams("changed",
  387. sbcCR,
  388. zeroDetectParams));
  389. PostEvent(new EventParams("changed",
  390. sbcCT,
  391. zeroDetectParams));
  392. break;
  393. case StateSource.RegionStart:
  394. PostEvent(new EventParams("changed",
  395. sbcCRS,
  396. zeroDetectParams));
  397. break;
  398. }
  399. }
  400. private static Object[] sbcCRS = new Object[] { ScriptBaseClass.CHANGED_REGION_START };
  401. private static Object[] sbcCR = new Object[] { ScriptBaseClass.CHANGED_REGION };
  402. private static Object[] sbcCT = new Object[] { ScriptBaseClass.CHANGED_TELEPORT };
  403. /**
  404. * @brief Save compilation error messages for later retrieval
  405. * via GetScriptErrors().
  406. */
  407. private void ErrorHandler(Token token, string message)
  408. {
  409. if(token != null)
  410. {
  411. string srcloc = token.SrcLoc;
  412. if(srcloc.StartsWith(m_CameFrom))
  413. srcloc = srcloc.Substring(m_CameFrom.Length);
  414. m_CompilerErrors.Add(srcloc + " Error: " + message);
  415. }
  416. else if(message != null)
  417. m_CompilerErrors.Add("(0,0) Error: " + message);
  418. else
  419. m_CompilerErrors.Add("(0,0) Error compiling, see exception in log");
  420. }
  421. /**
  422. * @brief Load script state from the given XML doc into the script memory
  423. * <ScriptState Engine="YEngine" Asset=...>
  424. * <Running>...</Running>
  425. * <DoGblInit>...</DoGblInit>
  426. * <Permissions granted=... mask=... />
  427. * RestoreDetectParams()
  428. * <Plugins>
  429. * ExtractXMLObjectArray("plugin")
  430. * </Plugins>
  431. * <Snapshot>
  432. * MigrateInEventHandler()
  433. * </Snapshot>
  434. * </ScriptState>
  435. */
  436. private void LoadScriptState(XmlDocument doc)
  437. {
  438. // Everything we know is enclosed in <ScriptState>...</ScriptState>
  439. XmlElement scriptStateN = (XmlElement)doc.SelectSingleNode("ScriptState");
  440. if(scriptStateN == null)
  441. throw new Exception("no <ScriptState> tag");
  442. XmlElement XvariablesN = null;
  443. string sen = scriptStateN.GetAttribute("Engine");
  444. if((sen == null) || (sen != m_Engine.ScriptEngineName))
  445. {
  446. XvariablesN = (XmlElement)scriptStateN.SelectSingleNode("Variables");
  447. if(XvariablesN == null)
  448. throw new Exception("<ScriptState> missing Engine=\"YEngine\" attribute");
  449. processXstate(doc);
  450. return;
  451. }
  452. // AssetID is unique for the script source text so make sure the
  453. // state file was written for that source file
  454. string assetID = scriptStateN.GetAttribute("Asset");
  455. if(assetID != m_Item.AssetID.ToString())
  456. throw new Exception("<ScriptState> assetID mismatch");
  457. // Also match the sourceHash in case script was
  458. // loaded via 'xmroption fetchsource' and has changed
  459. string sourceHash = scriptStateN.GetAttribute("SourceHash");
  460. if((sourceHash == null) || (sourceHash != m_ObjCode.sourceHash))
  461. throw new Exception("<ScriptState> SourceHash mismatch");
  462. // Get various attributes
  463. XmlElement runningN = (XmlElement)scriptStateN.SelectSingleNode("Running");
  464. m_Running = bool.Parse(runningN.InnerText);
  465. XmlElement doGblInitN = (XmlElement)scriptStateN.SelectSingleNode("DoGblInit");
  466. doGblInit = bool.Parse(doGblInitN.InnerText);
  467. double minEventDelay = 0.0;
  468. XmlElement minEventDelayN = (XmlElement)scriptStateN.SelectSingleNode("mEvtDly");
  469. if(minEventDelayN != null)
  470. minEventDelay = Double.Parse(minEventDelayN.InnerText);
  471. // get values used by stuff like llDetectedGrab, etc.
  472. DetectParams[] detParams = RestoreDetectParams(scriptStateN.SelectSingleNode("DetectArray"));
  473. // Restore queued events
  474. LinkedList<EventParams> eventQueue = RestoreEventQueue(scriptStateN.SelectSingleNode("EventQueue"));
  475. // Restore timers and listeners
  476. XmlElement pluginN = (XmlElement)scriptStateN.SelectSingleNode("Plugins");
  477. Object[] pluginData = ExtractXMLObjectArray(pluginN, "plugin");
  478. // Script's global variables and stack contents
  479. XmlElement snapshotN = (XmlElement)scriptStateN.SelectSingleNode("Snapshot");
  480. Byte[] data = Convert.FromBase64String(snapshotN.InnerText);
  481. using(MemoryStream ms = new MemoryStream())
  482. {
  483. ms.Write(data, 0, data.Length);
  484. ms.Seek(0, SeekOrigin.Begin);
  485. MigrateInEventHandler(ms);
  486. }
  487. XmlElement permissionsN = (XmlElement)scriptStateN.SelectSingleNode("Permissions");
  488. m_Item.PermsGranter = new UUID(permissionsN.GetAttribute("granter"));
  489. m_Item.PermsMask = Convert.ToInt32(permissionsN.GetAttribute("mask"));
  490. m_Part.Inventory.UpdateInventoryItem(m_Item, false, false);
  491. // Restore event queues, preserving any events that queued
  492. // whilst we were restoring the state
  493. lock (m_QueueLock)
  494. {
  495. m_DetectParams = detParams;
  496. foreach(EventParams evt in m_EventQueue)
  497. eventQueue.AddLast(evt);
  498. m_EventQueue = eventQueue;
  499. for(int i = m_EventCounts.Length; --i >= 0;)
  500. m_EventCounts[i] = 0;
  501. foreach(EventParams evt in m_EventQueue)
  502. {
  503. ScriptEventCode eventCode = (ScriptEventCode)Enum.Parse(typeof(ScriptEventCode),
  504. evt.EventName);
  505. m_EventCounts[(int)eventCode]++;
  506. }
  507. }
  508. // Requeue timer and listeners (possibly queuing new events)
  509. AsyncCommandManager.CreateFromData(m_Engine,
  510. m_LocalID, m_ItemID, m_Part.UUID,
  511. pluginData);
  512. MinEventDelay = minEventDelay;
  513. }
  514. private void processXstate(XmlDocument doc)
  515. {
  516. XmlNodeList rootL = doc.GetElementsByTagName("ScriptState");
  517. if (rootL.Count != 1)
  518. throw new Exception("Xstate <ScriptState> missing");
  519. XmlNode rootNode = rootL[0];
  520. if (rootNode == null)
  521. throw new Exception("Xstate root missing");
  522. string stateName = "";
  523. bool running = false;
  524. UUID permsGranter = UUID.Zero;
  525. int permsMask = 0;
  526. double minEventDelay = 0.0;
  527. Object[] pluginData = new Object[0];
  528. LinkedList<EventParams> eventQueue = new LinkedList<EventParams>();
  529. Dictionary<string, int> intNames = new Dictionary<string, int>();
  530. Dictionary<string, int> doubleNames = new Dictionary<string, int>();
  531. Dictionary<string, int> stringNames = new Dictionary<string, int>();
  532. Dictionary<string, int> vectorNames = new Dictionary<string, int>();
  533. Dictionary<string, int> rotationNames = new Dictionary<string, int>();
  534. Dictionary<string, int> listNames = new Dictionary<string, int>();
  535. int nn = m_ObjCode.globalVarNames.Count;
  536. int[] ints = null;
  537. double[] doubles = null;
  538. string[] strings = null;
  539. LSL_Vector[] vectors = null;
  540. LSL_Rotation[] rotations = null;
  541. LSL_List[] lists = null;
  542. if (nn > 0)
  543. {
  544. if (m_ObjCode.globalVarNames.ContainsKey("iarIntegers"))
  545. {
  546. getvarNames(m_ObjCode.globalVarNames["iarIntegers"], intNames);
  547. ints = new int[m_ObjCode.globalVarNames["iarIntegers"].Count];
  548. }
  549. if (m_ObjCode.globalVarNames.ContainsKey("iarFloats"))
  550. {
  551. getvarNames(m_ObjCode.globalVarNames["iarFloats"], doubleNames);
  552. doubles = new double[m_ObjCode.globalVarNames["iarFloats"].Count];
  553. }
  554. if (m_ObjCode.globalVarNames.ContainsKey("iarVectors"))
  555. {
  556. getvarNames(m_ObjCode.globalVarNames["iarVectors"], vectorNames);
  557. vectors = new LSL_Vector[m_ObjCode.globalVarNames["iarVectors"].Count];
  558. }
  559. if (m_ObjCode.globalVarNames.ContainsKey("iarRotations"))
  560. {
  561. getvarNames(m_ObjCode.globalVarNames["iarRotations"], rotationNames);
  562. rotations = new LSL_Rotation[m_ObjCode.globalVarNames["iarRotations"].Count];
  563. }
  564. if (m_ObjCode.globalVarNames.ContainsKey("iarStrings"))
  565. {
  566. getvarNames(m_ObjCode.globalVarNames["iarStrings"], stringNames);
  567. strings = new string[m_ObjCode.globalVarNames["iarStrings"].Count];
  568. }
  569. if (m_ObjCode.globalVarNames.ContainsKey("iarLists"))
  570. {
  571. getvarNames(m_ObjCode.globalVarNames["iarLists"], listNames);
  572. lists = new LSL_List[m_ObjCode.globalVarNames["iarLists"].Count];
  573. }
  574. }
  575. int heapsz = 0;
  576. try
  577. {
  578. XmlNodeList partL = rootNode.ChildNodes;
  579. foreach (XmlNode part in partL)
  580. {
  581. switch (part.Name)
  582. {
  583. case "State":
  584. stateName = part.InnerText;
  585. break;
  586. case "Running":
  587. running = bool.Parse(part.InnerText);
  588. break;
  589. case "Variables":
  590. int indx;
  591. XmlNodeList varL = part.ChildNodes;
  592. foreach (XmlNode var in varL)
  593. {
  594. string varName;
  595. object o = ReadXTypedValue(var, out varName);
  596. Type otype = o.GetType();
  597. if (otype == typeof(LSL_Integer))
  598. {
  599. if (intNames.TryGetValue(varName, out indx))
  600. ints[indx] = ((LSL_Integer)o);
  601. continue;
  602. }
  603. if (otype == typeof(LSL_Float))
  604. {
  605. if (doubleNames.TryGetValue(varName, out indx))
  606. doubles[indx] = ((LSL_Float)o);
  607. continue;
  608. }
  609. if (otype == typeof(LSL_String))
  610. {
  611. if (stringNames.TryGetValue(varName, out indx))
  612. {
  613. strings[indx] = ((LSL_String)o);
  614. heapsz += ((LSL_String)o).Length;
  615. }
  616. continue;
  617. }
  618. if (otype == typeof(LSL_Rotation))
  619. {
  620. if (rotationNames.TryGetValue(varName, out indx))
  621. rotations[indx] = ((LSL_Rotation)o);
  622. continue;
  623. }
  624. if (otype == typeof(LSL_Vector))
  625. {
  626. if (vectorNames.TryGetValue(varName, out indx))
  627. vectors[indx] = ((LSL_Vector)o);
  628. continue;
  629. }
  630. if (otype == typeof(LSL_Key))
  631. {
  632. if (stringNames.TryGetValue(varName, out indx))
  633. {
  634. strings[indx] = ((LSL_Key)o);
  635. heapsz += ((LSL_String)o).Length;
  636. }
  637. continue;
  638. }
  639. if (otype == typeof(UUID))
  640. {
  641. if (stringNames.TryGetValue(varName, out indx))
  642. {
  643. LSL_String id = ((UUID)o).ToString();
  644. strings[indx] = (id);
  645. heapsz += id.Length;
  646. }
  647. continue;
  648. }
  649. if (otype == typeof(LSL_List))
  650. {
  651. if (listNames.TryGetValue(varName, out indx))
  652. {
  653. LSL_List lo = (LSL_List)o;
  654. lists[indx] = (lo);
  655. heapsz += lo.Size;
  656. }
  657. continue;
  658. }
  659. }
  660. break;
  661. case "Queue":
  662. XmlNodeList itemL = part.ChildNodes;
  663. foreach (XmlNode item in itemL)
  664. {
  665. List<Object> parms = new List<Object>();
  666. List<DetectParams> detected = new List<DetectParams>();
  667. string eventName = item.Attributes.GetNamedItem("event").Value;
  668. XmlNodeList eventL = item.ChildNodes;
  669. foreach (XmlNode evt in eventL)
  670. {
  671. switch (evt.Name)
  672. {
  673. case "Params":
  674. XmlNodeList prms = evt.ChildNodes;
  675. foreach (XmlNode pm in prms)
  676. parms.Add(ReadXTypedValue(pm));
  677. break;
  678. case "Detected":
  679. XmlNodeList detL = evt.ChildNodes;
  680. foreach (XmlNode det in detL)
  681. {
  682. string vect = det.Attributes.GetNamedItem("pos").Value;
  683. LSL_Vector v = new LSL_Vector(vect);
  684. int d_linkNum = 0;
  685. UUID d_group = UUID.Zero;
  686. string d_name = String.Empty;
  687. UUID d_owner = UUID.Zero;
  688. LSL_Vector d_position = new LSL_Vector();
  689. LSL_Rotation d_rotation = new LSL_Rotation();
  690. int d_type = 0;
  691. LSL_Vector d_velocity = new LSL_Vector();
  692. try
  693. {
  694. string tmp;
  695. tmp = det.Attributes.GetNamedItem("linkNum").Value;
  696. int.TryParse(tmp, out d_linkNum);
  697. tmp = det.Attributes.GetNamedItem("group").Value;
  698. UUID.TryParse(tmp, out d_group);
  699. d_name = det.Attributes.GetNamedItem("name").Value;
  700. tmp = det.Attributes.GetNamedItem("owner").Value;
  701. UUID.TryParse(tmp, out d_owner);
  702. tmp = det.Attributes.GetNamedItem("position").Value;
  703. d_position = new LSL_Types.Vector3(tmp);
  704. tmp = det.Attributes.GetNamedItem("rotation").Value;
  705. d_rotation = new LSL_Rotation(tmp);
  706. tmp = det.Attributes.GetNamedItem("type").Value;
  707. int.TryParse(tmp, out d_type);
  708. tmp = det.Attributes.GetNamedItem("velocity").Value;
  709. d_velocity = new LSL_Vector(tmp);
  710. }
  711. catch (Exception) // Old version XML
  712. {
  713. }
  714. UUID uuid = new UUID();
  715. UUID.TryParse(det.InnerText, out uuid);
  716. DetectParams d = new DetectParams();
  717. d.Key = uuid;
  718. d.OffsetPos = v;
  719. d.LinkNum = d_linkNum;
  720. d.Group = d_group;
  721. d.Name = d_name;
  722. d.Owner = d_owner;
  723. d.Position = d_position;
  724. d.Rotation = d_rotation;
  725. d.Type = d_type;
  726. d.Velocity = d_velocity;
  727. detected.Add(d);
  728. }
  729. break;
  730. }
  731. }
  732. EventParams ep = new EventParams(
  733. eventName, parms.ToArray(),
  734. detected.ToArray());
  735. eventQueue.AddLast(ep);
  736. }
  737. break;
  738. case "Plugins":
  739. List<Object> olist = new List<Object>();
  740. XmlNodeList itemLP = part.ChildNodes;
  741. foreach (XmlNode item in itemLP)
  742. olist.Add(ReadXTypedValue(item));
  743. pluginData = olist.ToArray();
  744. break;
  745. case "Permissions":
  746. string tmpPerm;
  747. int mask = 0;
  748. tmpPerm = part.Attributes.GetNamedItem("mask").Value;
  749. if (tmpPerm != null)
  750. {
  751. int.TryParse(tmpPerm, out mask);
  752. if (mask != 0)
  753. {
  754. tmpPerm = part.Attributes.GetNamedItem("granter").Value;
  755. if (tmpPerm != null)
  756. {
  757. UUID granter = new UUID();
  758. UUID.TryParse(tmpPerm, out granter);
  759. if (granter != UUID.Zero)
  760. {
  761. permsMask = mask;
  762. permsGranter = granter;
  763. }
  764. }
  765. }
  766. }
  767. break;
  768. case "MinEventDelay":
  769. double.TryParse(part.InnerText, out minEventDelay);
  770. break;
  771. }
  772. }
  773. }
  774. catch
  775. {
  776. throw new Exception("Xstate fail decode");
  777. }
  778. int k = 0;
  779. stateCode = 0;
  780. foreach (string sn in m_ObjCode.stateNames)
  781. {
  782. if (stateName == sn)
  783. {
  784. stateCode = k;
  785. break;
  786. }
  787. k++;
  788. }
  789. eventCode = ScriptEventCode.None;
  790. m_Running = running;
  791. doGblInit = false;
  792. m_Item.PermsGranter = permsGranter;
  793. m_Item.PermsMask = permsMask;
  794. m_Part.Inventory.UpdateInventoryItem(m_Item, false, false);
  795. lock (m_RunLock)
  796. {
  797. glblVars.iarIntegers = ints;
  798. glblVars.iarFloats = doubles;
  799. glblVars.iarVectors = vectors;
  800. glblVars.iarRotations = rotations;
  801. glblVars.iarStrings = strings;
  802. glblVars.iarLists = lists;
  803. AddHeapUse(heapsz);
  804. CheckRunLockInvariants(true);
  805. }
  806. lock (m_QueueLock)
  807. {
  808. m_DetectParams = null;
  809. foreach (EventParams evt in m_EventQueue)
  810. eventQueue.AddLast(evt);
  811. m_EventQueue = eventQueue;
  812. for (int i = m_EventCounts.Length; --i >= 0;)
  813. m_EventCounts[i] = 0;
  814. foreach (EventParams evt in m_EventQueue)
  815. {
  816. ScriptEventCode evtCode = (ScriptEventCode)Enum.Parse(typeof(ScriptEventCode),
  817. evt.EventName);
  818. m_EventCounts[(int)evtCode]++;
  819. }
  820. }
  821. AsyncCommandManager.CreateFromData(m_Engine,
  822. m_LocalID, m_ItemID, m_Part.UUID, pluginData);
  823. MinEventDelay = minEventDelay;
  824. }
  825. private static void getvarNames(Dictionary<int, string> s, Dictionary<string, int> d)
  826. {
  827. foreach(KeyValuePair<int, string> kvp in s)
  828. d[kvp.Value] = kvp.Key;
  829. }
  830. private static LSL_Types.list ReadXList(XmlNode parent)
  831. {
  832. List<Object> olist = new List<Object>();
  833. XmlNodeList itemL = parent.ChildNodes;
  834. foreach (XmlNode item in itemL)
  835. olist.Add(ReadXTypedValue(item));
  836. return new LSL_Types.list(olist.ToArray());
  837. }
  838. private static object ReadXTypedValue(XmlNode tag, out string name)
  839. {
  840. name = tag.Attributes.GetNamedItem("name").Value;
  841. return ReadXTypedValue(tag);
  842. }
  843. private static object ReadXTypedValue(XmlNode tag)
  844. {
  845. Object varValue;
  846. string assembly;
  847. string itemType = tag.Attributes.GetNamedItem("type").Value;
  848. if (itemType == "list")
  849. return ReadXList(tag);
  850. if (itemType == "OpenMetaverse.UUID")
  851. {
  852. UUID val = new UUID();
  853. UUID.TryParse(tag.InnerText, out val);
  854. return val;
  855. }
  856. Type itemT = Type.GetType(itemType);
  857. if (itemT == null)
  858. {
  859. Object[] args =
  860. new Object[] { tag.InnerText };
  861. assembly = itemType + ", OpenSim.Region.ScriptEngine.Shared";
  862. itemT = Type.GetType(assembly);
  863. if (itemT == null)
  864. return null;
  865. varValue = Activator.CreateInstance(itemT, args);
  866. if (varValue == null)
  867. return null;
  868. }
  869. else
  870. {
  871. varValue = Convert.ChangeType(tag.InnerText, itemT);
  872. }
  873. return varValue;
  874. }
  875. /**
  876. * @brief Read llDetectedGrab, etc, values from XML
  877. * <EventQueue>
  878. * <DetectParams>...</DetectParams>
  879. * .
  880. * .
  881. * .
  882. * </EventQueue>
  883. */
  884. private LinkedList<EventParams> RestoreEventQueue(XmlNode eventsN)
  885. {
  886. LinkedList<EventParams> eventQueue = new LinkedList<EventParams>();
  887. if(eventsN != null)
  888. {
  889. XmlNodeList eventL = eventsN.SelectNodes("Event");
  890. foreach(XmlNode evnt in eventL)
  891. {
  892. string name = ((XmlElement)evnt).GetAttribute("Name");
  893. object[] parms = ExtractXMLObjectArray(evnt, "param");
  894. DetectParams[] detects = RestoreDetectParams(evnt);
  895. if(parms == null)
  896. parms = zeroObjectArray;
  897. if(detects == null)
  898. detects = zeroDetectParams;
  899. EventParams evt = new EventParams(name, parms, detects);
  900. eventQueue.AddLast(evt);
  901. }
  902. }
  903. return eventQueue;
  904. }
  905. /**
  906. * @brief Read llDetectedGrab, etc, values from XML
  907. * <DetectArray>
  908. * <DetectParams>...</DetectParams>
  909. * .
  910. * .
  911. * .
  912. * </DetectArray>
  913. */
  914. private DetectParams[] RestoreDetectParams(XmlNode detectedN)
  915. {
  916. if(detectedN == null)
  917. return null;
  918. List<DetectParams> detected = new List<DetectParams>();
  919. XmlNodeList detectL = detectedN.SelectNodes("DetectParams");
  920. DetectParams detprm = new DetectParams();
  921. foreach(XmlNode detxml in detectL)
  922. {
  923. try
  924. {
  925. detprm.Group = new UUID(detxml.Attributes.GetNamedItem("group").Value);
  926. detprm.Key = new UUID(detxml.Attributes.GetNamedItem("key").Value);
  927. detprm.Owner = new UUID(detxml.Attributes.GetNamedItem("owner").Value);
  928. detprm.LinkNum = Int32.Parse(detxml.Attributes.GetNamedItem("linkNum").Value);
  929. detprm.Type = Int32.Parse(detxml.Attributes.GetNamedItem("type").Value);
  930. detprm.Name = detxml.Attributes.GetNamedItem("name").Value;
  931. detprm.OffsetPos = new LSL_Types.Vector3(detxml.Attributes.GetNamedItem("pos").Value);
  932. detprm.Position = new LSL_Types.Vector3(detxml.Attributes.GetNamedItem("position").Value);
  933. detprm.Velocity = new LSL_Types.Vector3(detxml.Attributes.GetNamedItem("velocity").Value);
  934. detprm.Rotation = new LSL_Types.Quaternion(detxml.Attributes.GetNamedItem("rotation").Value);
  935. detected.Add(detprm);
  936. detprm = new DetectParams();
  937. }
  938. catch(Exception e)
  939. {
  940. m_log.Warn("[YEngine]: RestoreDetectParams bad XML: " + detxml.ToString());
  941. m_log.Warn("[YEngine]: ... " + e.ToString());
  942. }
  943. }
  944. return detected.ToArray();
  945. }
  946. /**
  947. * @brief Extract elements of an array of objects from an XML parent.
  948. * Each element is of form <tag ...>...</tag>
  949. * @param parent = XML parent to extract them from
  950. * @param tag = what the value's tag is
  951. * @returns object array of the values
  952. */
  953. private static object[] ExtractXMLObjectArray(XmlNode parent, string tag)
  954. {
  955. List<Object> olist = new List<Object>();
  956. XmlNodeList itemL = parent.SelectNodes(tag);
  957. foreach(XmlNode item in itemL)
  958. {
  959. olist.Add(ExtractXMLObjectValue(item));
  960. }
  961. return olist.ToArray();
  962. }
  963. private static object ExtractXMLObjectValue(XmlNode item)
  964. {
  965. string itemType = item.Attributes.GetNamedItem("type").Value;
  966. if(itemType == "list")
  967. {
  968. return new LSL_List(ExtractXMLObjectArray(item, "item"));
  969. }
  970. if(itemType == "OpenMetaverse.UUID")
  971. {
  972. UUID val = new UUID();
  973. UUID.TryParse(item.InnerText, out val);
  974. return val;
  975. }
  976. Type itemT = Type.GetType(itemType);
  977. if(itemT == null)
  978. {
  979. Object[] args = new Object[] { item.InnerText };
  980. string assembly = itemType + ", OpenSim.Region.ScriptEngine.Shared";
  981. itemT = Type.GetType(assembly);
  982. if(itemT == null)
  983. {
  984. return null;
  985. }
  986. return Activator.CreateInstance(itemT, args);
  987. }
  988. return Convert.ChangeType(item.InnerText, itemT);
  989. }
  990. /*
  991. * Migrate an event handler in from a stream.
  992. *
  993. * Input:
  994. * stream = as generated by MigrateOutEventHandler()
  995. */
  996. private void MigrateInEventHandler(Stream stream)
  997. {
  998. int mv = stream.ReadByte();
  999. if(mv != migrationVersion)
  1000. throw new Exception("incoming migration version " + mv + " but accept only " + migrationVersion);
  1001. stream.ReadByte(); // ignored
  1002. /*
  1003. * Restore script variables and stack and other state from stream.
  1004. * And it also marks us busy (by setting this.eventCode) so we can't be
  1005. * started again and this event lost. If it restores this.eventCode =
  1006. * None, the the script was idle.
  1007. */
  1008. lock(m_RunLock)
  1009. {
  1010. BinaryReader br = new BinaryReader(stream);
  1011. this.MigrateIn(br);
  1012. m_RunOnePhase = "MigrateInEventHandler finished";
  1013. CheckRunLockInvariants(true);
  1014. }
  1015. }
  1016. }
  1017. }