XMRInstMisc.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 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.Substring(0, 1);
  229. filename = filename.Substring(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. EmptyEventQueues();
  338. }
  339. }
  340. }
  341. }
  342. /**
  343. * @brief Empty out the event queues.
  344. * Assumes caller has the m_QueueLock locked.
  345. */
  346. public void EmptyEventQueues()
  347. {
  348. m_EventQueue.Clear();
  349. for(int i = m_EventCounts.Length; --i >= 0;)
  350. m_EventCounts[i] = 0;
  351. }
  352. /**
  353. * @brief Convert an LSL vector to an Openmetaverse vector.
  354. */
  355. public static OpenMetaverse.Vector3 LSLVec2OMVec(LSL_Vector lslVec)
  356. {
  357. return new OpenMetaverse.Vector3((float)lslVec.x, (float)lslVec.y, (float)lslVec.z);
  358. }
  359. /**
  360. * @brief Extract an integer from an element of an LSL_List.
  361. */
  362. public static int ListInt(object element)
  363. {
  364. if(element is LSL_Integer)
  365. {
  366. return (int)(LSL_Integer)element;
  367. }
  368. return (int)element;
  369. }
  370. /**
  371. * @brief Extract a string from an element of an LSL_List.
  372. */
  373. public static string ListStr(object element)
  374. {
  375. if(element is LSL_String)
  376. {
  377. return (string)(LSL_String)element;
  378. }
  379. return (string)element;
  380. }
  381. }
  382. }