XMRInstMisc.cs 16 KB

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