XMRInstMisc.cs 16 KB

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