IScriptInstance.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.Collections;
  29. using System.Collections.Generic;
  30. using System.Threading;
  31. using System.Diagnostics;
  32. using OpenMetaverse;
  33. using log4net;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Scenes;
  36. using OpenSim.Region.ScriptEngine.Shared;
  37. using OpenSim.Region.ScriptEngine.Interfaces;
  38. namespace OpenSim.Region.ScriptEngine.Interfaces
  39. {
  40. public enum StateSource
  41. {
  42. RegionStart = 0,
  43. NewRez = 1,
  44. PrimCrossing = 2,
  45. ScriptedRez = 3,
  46. AttachedRez = 4,
  47. Teleporting = 5
  48. }
  49. public interface IScriptWorkItem
  50. {
  51. bool Cancel();
  52. bool Abort();
  53. /// <summary>
  54. /// Wait for the work item to complete.
  55. /// </summary>
  56. /// <param name='t'>The number of milliseconds to wait. Must be >= -1 (Timeout.Infinite).</param>
  57. bool Wait(int t);
  58. }
  59. /// <summary>
  60. /// Interface for interaction with a particular script instance
  61. /// </summary>
  62. public interface IScriptInstance
  63. {
  64. /// <summary>
  65. /// Debug level for this script instance.
  66. /// </summary>
  67. /// <remarks>
  68. /// Level == 0, no extra data is logged.
  69. /// Level >= 1, state changes are logged.
  70. /// Level >= 2, event firing is logged.
  71. /// <value>
  72. /// The debug level.
  73. /// </value>
  74. int DebugLevel { get; set; }
  75. /// <summary>
  76. /// Is the script currently running?
  77. /// </summary>
  78. bool Running { get; set; }
  79. /// <summary>
  80. /// Is the script suspended?
  81. /// </summary>
  82. bool Suspended { get; set; }
  83. /// <summary>
  84. /// Is the script shutting down?
  85. /// </summary>
  86. bool ShuttingDown { get; set; }
  87. /// <summary>
  88. /// When stopping the script: should it remain stopped permanently (i.e., save !Running in its state)?
  89. /// </summary>
  90. bool StayStopped { get; set; }
  91. /// <summary>
  92. /// Script state
  93. /// </summary>
  94. string State { get; set; }
  95. /// <summary>
  96. /// If true then the engine is responsible for persisted state. If false then some other component may
  97. /// persist state (e.g. attachments persisting in assets).
  98. /// </summary>
  99. bool StatePersistedHere { get; }
  100. /// <summary>
  101. /// Time the script was last started
  102. /// </summary>
  103. DateTime TimeStarted { get; }
  104. /// <summary>
  105. /// Collects information about how long the script was executed.
  106. /// </summary>
  107. MetricsCollectorTime ExecutionTime { get; }
  108. /// <summary>
  109. /// Scene part in which this script instance is contained.
  110. /// </summary>
  111. SceneObjectPart Part { get; }
  112. IScriptEngine Engine { get; }
  113. UUID AppDomain { get; set; }
  114. string PrimName { get; }
  115. string ScriptName { get; }
  116. UUID ItemID { get; }
  117. UUID ObjectID { get; }
  118. /// <summary>
  119. /// UUID of the root object for the linkset that the script is in.
  120. /// </summary>
  121. UUID RootObjectID { get; }
  122. /// <summary>
  123. /// Local id of the root object for the linkset that the script is in.
  124. /// </summary>
  125. uint RootLocalID { get; }
  126. uint LocalID { get; }
  127. UUID AssetID { get; }
  128. /// <summary>
  129. /// Inventory item containing the script used.
  130. /// </summary>
  131. TaskInventoryItem ScriptTask { get; }
  132. Queue EventQueue { get; }
  133. /// <summary>
  134. /// Number of events queued for processing.
  135. /// </summary>
  136. long EventsQueued { get; }
  137. /// <summary>
  138. /// Number of events processed by this script instance.
  139. /// </summary>
  140. long EventsProcessed { get; }
  141. void ClearQueue();
  142. int StartParam { get; set; }
  143. WaitHandle CoopWaitHandle { get; }
  144. Stopwatch ExecutionTimer { get; }
  145. void RemoveState();
  146. void Init();
  147. void Start();
  148. /// <summary>
  149. /// Stop the script instance.
  150. /// </summary>
  151. /// <remarks>
  152. /// This must not be called by a thread that is in the process of handling an event for this script. Otherwise
  153. /// there is a danger that it will self-abort and not complete the reset.
  154. /// </remarks>
  155. /// <param name="timeout"></param>
  156. /// How many milliseconds we will wait for an existing script event to finish before
  157. /// forcibly aborting that event.
  158. /// <param name="clearEventQueue">If true then the event queue is also cleared</param>
  159. /// <returns>true if the script was successfully stopped, false otherwise</returns>
  160. bool Stop(int timeout, bool clearEventQueue = false);
  161. void SetState(string state);
  162. /// <summary>
  163. /// Post an event to this script instance.
  164. /// </summary>
  165. /// <param name="data"></param>
  166. void PostEvent(EventParams data);
  167. void Suspend();
  168. void Resume();
  169. /// <summary>
  170. /// Process the next event queued for this script instance.
  171. /// </summary>
  172. /// <returns></returns>
  173. object EventProcessor();
  174. int EventTime();
  175. /// <summary>
  176. /// Reset the script.
  177. /// </summary>
  178. /// <remarks>
  179. /// This must not be called by a thread that is in the process of handling an event for this script. Otherwise
  180. /// there is a danger that it will self-abort and not complete the reset. Such a thread must call
  181. /// ApiResetScript() instead.
  182. /// </remarks>
  183. /// <param name='timeout'>
  184. /// How many milliseconds we will wait for an existing script event to finish before
  185. /// forcibly aborting that event prior to script reset.
  186. /// </param>
  187. void ResetScript(int timeout);
  188. /// <summary>
  189. /// Reset the script.
  190. /// </summary>
  191. /// <remarks>
  192. /// This must not be called by any thread other than the one executing the scripts current event. This is
  193. /// because there is no wait or abort logic if another thread is in the middle of processing a script event.
  194. /// Such an external thread should use ResetScript() instead.
  195. /// </remarks>
  196. void ApiResetScript();
  197. Dictionary<string, object> GetVars();
  198. void SetVars(Dictionary<string, object> vars);
  199. DetectParams GetDetectParams(int idx);
  200. UUID GetDetectID(int idx);
  201. void SaveState();
  202. void DestroyScriptInstance();
  203. IScriptApi GetApi(string name);
  204. Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> LineMap
  205. { get; set; }
  206. string GetAssemblyName();
  207. string GetXMLState();
  208. double MinEventDelay { set; }
  209. UUID RegionID { get; }
  210. }
  211. }