XMRInstMisc.cs 16 KB

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