IScriptInstance.cs 8.3 KB

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