1
0

XMRInstMisc.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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.Reflection.Emit;
  33. using System.Runtime.Remoting.Lifetime;
  34. using System.Security.Policy;
  35. using System.IO;
  36. using System.Xml;
  37. using System.Text;
  38. using OpenMetaverse;
  39. using OpenSim.Framework;
  40. using OpenSim.Region.ScriptEngine.Interfaces;
  41. using OpenSim.Region.ScriptEngine.Shared;
  42. using OpenSim.Region.ScriptEngine.Shared.Api;
  43. using OpenSim.Region.ScriptEngine.Shared.ScriptBase;
  44. using OpenSim.Region.ScriptEngine.Yengine;
  45. using OpenSim.Region.Framework.Scenes;
  46. using log4net;
  47. using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat;
  48. using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger;
  49. using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
  50. using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
  51. using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
  52. using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
  53. using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;
  54. // This class exists in the main app domain
  55. //
  56. namespace OpenSim.Region.ScriptEngine.Yengine
  57. {
  58. public partial class XMRInstance
  59. {
  60. private bool m_disposed;
  61. // In case Dispose() doesn't get called, we want to be sure to clean
  62. // up. This makes sure we decrement m_CompiledScriptRefCount.
  63. ~XMRInstance()
  64. {
  65. Dispose(false);
  66. }
  67. /**
  68. * @brief Clean up stuff.
  69. * We specifically leave m_DescName intact for 'xmr ls' command.
  70. */
  71. public void Dispose()
  72. {
  73. Dispose(true);
  74. //GC.SuppressFinalize(this);
  75. }
  76. public void Dispose(bool fromdispose)
  77. {
  78. if (m_disposed)
  79. return;
  80. // Tell script stop executing next time it calls CheckRun().
  81. suspendOnCheckRunHold = true;
  82. // Don't send us any more events.
  83. lock (m_RunLock)
  84. {
  85. if(m_Part != null)
  86. {
  87. AsyncCommandManager.RemoveScript(m_Engine, m_LocalID, m_ItemID);
  88. m_Part = null;
  89. }
  90. }
  91. // Let script methods get garbage collected if no one else is using
  92. // them.
  93. DecObjCodeRefCount();
  94. m_disposed = true;
  95. }
  96. private void DecObjCodeRefCount()
  97. {
  98. if(m_ObjCode != null)
  99. {
  100. lock(m_CompileLock)
  101. {
  102. ScriptObjCode objCode;
  103. if(m_CompiledScriptObjCode.TryGetValue(m_ScriptObjCodeKey, out objCode) &&
  104. (objCode == m_ObjCode) &&
  105. (--objCode.refCount == 0))
  106. {
  107. m_CompiledScriptObjCode.Remove(m_ScriptObjCodeKey);
  108. }
  109. }
  110. m_ObjCode = null;
  111. }
  112. }
  113. public void Verbose(string format, params object[] args)
  114. {
  115. if(m_Engine.m_Verbose)
  116. m_log.DebugFormat(format, args);
  117. }
  118. // Called by 'xmr top' console command
  119. // to dump this script's state to console
  120. // Sacha
  121. public void RunTestTop()
  122. {
  123. if(m_InstEHSlice > 0)
  124. {
  125. Console.WriteLine(m_DescName);
  126. Console.WriteLine(" m_LocalID = " + m_LocalID);
  127. Console.WriteLine(" m_ItemID = " + m_ItemID);
  128. Console.WriteLine(" m_Item.AssetID = " + m_Item.AssetID);
  129. Console.WriteLine(" m_StartParam = " + m_StartParam);
  130. Console.WriteLine(" m_PostOnRez = " + m_PostOnRez);
  131. Console.WriteLine(" m_StateSource = " + m_StateSource);
  132. Console.WriteLine(" m_SuspendCount = " + m_SuspendCount);
  133. Console.WriteLine(" m_SleepUntil = " + m_SleepUntil);
  134. Console.WriteLine(" m_IState = " + m_IState.ToString());
  135. Console.WriteLine(" m_StateCode = " + GetStateName(stateCode));
  136. Console.WriteLine(" eventCode = " + eventCode.ToString());
  137. Console.WriteLine(" m_LastRanAt = " + m_LastRanAt.ToString());
  138. Console.WriteLine(" heapUsed/Limit = " + xmrHeapUsed() + "/" + heapLimit);
  139. Console.WriteLine(" m_InstEHEvent = " + m_InstEHEvent.ToString());
  140. Console.WriteLine(" m_InstEHSlice = " + m_InstEHSlice.ToString());
  141. }
  142. }
  143. // Called by 'xmr ls' console command
  144. // to dump this script's state to console
  145. public string RunTestLs(bool flagFull)
  146. {
  147. if(flagFull)
  148. {
  149. StringBuilder sb = new StringBuilder();
  150. sb.AppendLine(m_DescName);
  151. sb.AppendLine(" m_LocalID = " + m_LocalID);
  152. sb.AppendLine(" m_ItemID = " + m_ItemID + " (.state file)");
  153. sb.AppendLine(" m_Item.AssetID = " + m_Item.AssetID);
  154. sb.AppendLine(" m_Part.WorldPosition = " + m_Part.GetWorldPosition());
  155. sb.AppendLine(" m_ScriptObjCodeKey = " + m_ScriptObjCodeKey + " (source text)");
  156. sb.AppendLine(" m_StartParam = " + m_StartParam);
  157. sb.AppendLine(" m_PostOnRez = " + m_PostOnRez);
  158. sb.AppendLine(" m_StateSource = " + m_StateSource);
  159. sb.AppendLine(" m_SuspendCount = " + m_SuspendCount);
  160. sb.AppendLine(" m_SleepUntil = " + m_SleepUntil);
  161. sb.AppendLine(" m_SleepEvMask1 = 0x" + m_SleepEventMask1.ToString("X"));
  162. sb.AppendLine(" m_SleepEvMask2 = 0x" + m_SleepEventMask2.ToString("X"));
  163. sb.AppendLine(" m_IState = " + m_IState.ToString());
  164. sb.AppendLine(" m_StateCode = " + GetStateName(stateCode));
  165. sb.AppendLine(" eventCode = " + eventCode.ToString());
  166. sb.AppendLine(" m_LastRanAt = " + m_LastRanAt.ToString());
  167. sb.AppendLine(" m_RunOnePhase = " + m_RunOnePhase);
  168. sb.AppendLine(" suspOnCkRunHold = " + suspendOnCheckRunHold);
  169. sb.AppendLine(" suspOnCkRunTemp = " + suspendOnCheckRunTemp);
  170. sb.AppendLine(" m_CheckRunPhase = " + m_CheckRunPhase);
  171. sb.AppendLine(" heapUsed/Limit = " + xmrHeapUsed() + "/" + heapLimit);
  172. sb.AppendLine(" m_InstEHEvent = " + m_InstEHEvent.ToString());
  173. sb.AppendLine(" m_InstEHSlice = " + m_InstEHSlice.ToString());
  174. sb.AppendLine(" m_CPUTime = " + m_CPUTime);
  175. sb.AppendLine(" callMode = " + callMode);
  176. lock(m_QueueLock)
  177. {
  178. sb.AppendLine(" m_Running = " + m_Running);
  179. foreach(EventParams evt in m_EventQueue)
  180. {
  181. sb.AppendLine(" evt.EventName = " + evt.EventName);
  182. }
  183. }
  184. return sb.ToString();
  185. }
  186. else
  187. {
  188. return String.Format("{0} {1} {2} {3} {4} {5}",
  189. m_ItemID,
  190. m_CPUTime.ToString("F3").PadLeft(9),
  191. m_InstEHEvent.ToString().PadLeft(9),
  192. m_IState.ToString().PadRight(10),
  193. m_Part.GetWorldPosition().ToString().PadRight(32),
  194. m_DescName);
  195. }
  196. }
  197. /**
  198. * @brief For a given stateCode, get a mask of the low 32 event codes
  199. * that the state has handlers defined for.
  200. */
  201. public ulong GetStateEventFlags(int state)
  202. {
  203. if((state < 0) ||
  204. (state >= m_ObjCode.scriptEventHandlerTable.GetLength(0)))
  205. {
  206. return 0;
  207. }
  208. ulong flags = 0;
  209. for(int i = 0; i <(int)ScriptEventCode.Size; i++)
  210. {
  211. if(m_ObjCode.scriptEventHandlerTable[state, i] != null)
  212. {
  213. flags |= 1ul << i;
  214. }
  215. }
  216. return flags;
  217. }
  218. /**
  219. * @brief Get the .state file name.
  220. */
  221. public static string GetStateFileName(string scriptBasePath, UUID itemID)
  222. {
  223. return GetScriptFileName(scriptBasePath, itemID.ToString() + ".state");
  224. }
  225. public string GetScriptFileName(string filename)
  226. {
  227. return GetScriptFileName(m_ScriptBasePath, filename);
  228. }
  229. public string GetScriptILFileName(string filename)
  230. {
  231. string path = Path.Combine(m_ScriptBasePath, "DebugIL");
  232. Directory.CreateDirectory(path);
  233. return Path.Combine(path, filename);
  234. }
  235. public static string GetScriptFileName(string scriptBasePath, string filename)
  236. {
  237. // Get old path, ie, all files lumped in a single huge directory.
  238. string oldPath = Path.Combine(scriptBasePath, filename);
  239. // Get new path, ie, files split up based on first 2 chars of name.
  240. // string subdir = filename.Substring (0, 2);
  241. // filename = filename.Substring (2);
  242. string subdir = filename.Substring(0, 1);
  243. filename = filename.Substring(1);
  244. scriptBasePath = Path.Combine(scriptBasePath, subdir);
  245. Directory.CreateDirectory(scriptBasePath);
  246. string newPath = Path.Combine(scriptBasePath, filename);
  247. // If file exists only in old location, move to new location.
  248. // If file exists in both locations, delete old location.
  249. if(File.Exists(oldPath))
  250. {
  251. if(File.Exists(newPath))
  252. {
  253. File.Delete(oldPath);
  254. }
  255. else
  256. {
  257. File.Move(oldPath, newPath);
  258. }
  259. }
  260. // Always return new location.
  261. return newPath;
  262. }
  263. /**
  264. * @brief Decode state code (int) to state name (string).
  265. */
  266. public string GetStateName(int stateCode)
  267. {
  268. try
  269. {
  270. return m_ObjCode.stateNames[stateCode];
  271. }
  272. catch
  273. {
  274. return stateCode.ToString();
  275. }
  276. }
  277. /**
  278. * @brief various gets & sets.
  279. */
  280. public int StartParam
  281. {
  282. get
  283. {
  284. return m_StartParam;
  285. }
  286. set
  287. {
  288. m_StartParam = value;
  289. }
  290. }
  291. public double MinEventDelay
  292. {
  293. get
  294. {
  295. return m_minEventDelay;
  296. }
  297. set
  298. {
  299. if (value > 0.001)
  300. m_minEventDelay = value;
  301. else
  302. m_minEventDelay = 0.0;
  303. m_nextEventTime = 0.0; // reset it
  304. }
  305. }
  306. public SceneObjectPart SceneObject
  307. {
  308. get
  309. {
  310. return m_Part;
  311. }
  312. }
  313. public DetectParams[] DetectParams
  314. {
  315. get
  316. {
  317. return m_DetectParams;
  318. }
  319. set
  320. {
  321. m_DetectParams = value;
  322. }
  323. }
  324. public UUID ItemID
  325. {
  326. get
  327. {
  328. return m_ItemID;
  329. }
  330. }
  331. public UUID AssetID
  332. {
  333. get
  334. {
  335. return m_Item.AssetID;
  336. }
  337. }
  338. public bool Running
  339. {
  340. get
  341. {
  342. return m_Running;
  343. }
  344. set
  345. {
  346. lock(m_QueueLock)
  347. {
  348. m_Running = value;
  349. if(value)
  350. {
  351. if (m_IState == XMRInstState.SUSPENDED && m_SuspendCount == 0)
  352. {
  353. if(eventCode != ScriptEventCode.None)
  354. {
  355. m_IState = XMRInstState.ONYIELDQ;
  356. m_Engine.QueueToYield(this);
  357. }
  358. else if ((m_EventQueue != null) && (m_EventQueue.First != null))
  359. {
  360. m_IState = XMRInstState.ONSTARTQ;
  361. m_Engine.QueueToStart(this);
  362. }
  363. else
  364. m_IState = XMRInstState.IDLE;
  365. }
  366. //else if(m_SuspendCount != 0)
  367. // m_IState = XMRInstState.IDLE;
  368. }
  369. else
  370. {
  371. if(m_IState == XMRInstState.ONSLEEPQ)
  372. {
  373. m_Engine.RemoveFromSleep(this);
  374. m_IState = XMRInstState.SUSPENDED;
  375. }
  376. EmptyEventQueues();
  377. }
  378. }
  379. }
  380. }
  381. /**
  382. * @brief Empty out the event queues.
  383. * Assumes caller has the m_QueueLock locked.
  384. */
  385. public void EmptyEventQueues()
  386. {
  387. m_EventQueue.Clear();
  388. for(int i = m_EventCounts.Length; --i >= 0;)
  389. m_EventCounts[i] = 0;
  390. }
  391. /**
  392. * @brief Convert an LSL vector to an Openmetaverse vector.
  393. */
  394. public static OpenMetaverse.Vector3 LSLVec2OMVec(LSL_Vector lslVec)
  395. {
  396. return new OpenMetaverse.Vector3((float)lslVec.x, (float)lslVec.y, (float)lslVec.z);
  397. }
  398. /**
  399. * @brief Extract an integer from an element of an LSL_List.
  400. */
  401. public static int ListInt(object element)
  402. {
  403. if(element is LSL_Integer)
  404. {
  405. return (int)(LSL_Integer)element;
  406. }
  407. return (int)element;
  408. }
  409. /**
  410. * @brief Extract a string from an element of an LSL_List.
  411. */
  412. public static string ListStr(object element)
  413. {
  414. if(element is LSL_String)
  415. {
  416. return (string)(LSL_String)element;
  417. }
  418. return (string)element;
  419. }
  420. }
  421. }