1
0

ExecutorBase.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 OpenSim 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.Generic;
  29. using System.Runtime.Remoting.Lifetime;
  30. namespace OpenSim.Region.ScriptEngine.Common
  31. {
  32. public abstract class ExecutorBase : MarshalByRefObject
  33. {
  34. /// <summary>
  35. /// Contains the script to execute functions in.
  36. /// </summary>
  37. protected IScript m_Script;
  38. /// <summary>
  39. /// If set to False events will not be executed.
  40. /// </summary>
  41. protected bool m_Running = true;
  42. /// <summary>
  43. /// True indicates that the ScriptManager has stopped
  44. /// this script. This prevents a script that has been
  45. /// stopped as part of deactivation from being
  46. /// resumed by a pending llSetScriptState request.
  47. /// </summary>
  48. protected bool m_Disable = false;
  49. /// <summary>
  50. /// Indicate the scripts current running status.
  51. /// </summary>
  52. public bool Running
  53. {
  54. get { return m_Running; }
  55. set
  56. {
  57. if (!m_Disable)
  58. m_Running = value;
  59. }
  60. }
  61. protected Dictionary<string, scriptEvents> m_eventFlagsMap = new Dictionary<string, scriptEvents>();
  62. [Flags]
  63. public enum scriptEvents : int
  64. {
  65. None = 0,
  66. attach = 1,
  67. collision = 15,
  68. collision_end = 32,
  69. collision_start = 64,
  70. control = 128,
  71. dataserver = 256,
  72. email = 512,
  73. http_response = 1024,
  74. land_collision = 2048,
  75. land_collision_end = 4096,
  76. land_collision_start = 8192,
  77. at_target = 16384,
  78. listen = 32768,
  79. money = 65536,
  80. moving_end = 131072,
  81. moving_start = 262144,
  82. not_at_rot_target = 524288,
  83. not_at_target = 1048576,
  84. remote_data = 8388608,
  85. run_time_permissions = 268435456,
  86. state_entry = 1073741824,
  87. state_exit = 2,
  88. timer = 4,
  89. touch = 8,
  90. touch_end = 536870912,
  91. touch_start = 2097152,
  92. object_rez = 4194304
  93. }
  94. /// <summary>
  95. /// Create a new instance of ExecutorBase
  96. /// </summary>
  97. /// <param name="Script"></param>
  98. public ExecutorBase(IScript Script)
  99. {
  100. m_Script = Script;
  101. initEventFlags();
  102. }
  103. /// <summary>
  104. /// Make sure our object does not timeout when in AppDomain. (Called by ILease base class)
  105. /// </summary>
  106. /// <returns></returns>
  107. public override Object InitializeLifetimeService()
  108. {
  109. //Console.WriteLine("Executor: InitializeLifetimeService()");
  110. // return null;
  111. ILease lease = (ILease)base.InitializeLifetimeService();
  112. if (lease.CurrentState == LeaseState.Initial)
  113. {
  114. lease.InitialLeaseTime = TimeSpan.Zero; // TimeSpan.FromMinutes(1);
  115. // lease.SponsorshipTimeout = TimeSpan.FromMinutes(2);
  116. // lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
  117. }
  118. return lease;
  119. }
  120. /// <summary>
  121. /// Get current AppDomain
  122. /// </summary>
  123. /// <returns>Current AppDomain</returns>
  124. public AppDomain GetAppDomain()
  125. {
  126. return AppDomain.CurrentDomain;
  127. }
  128. /// <summary>
  129. /// Execute a specific function/event in script.
  130. /// </summary>
  131. /// <param name="FunctionName">Name of function to execute</param>
  132. /// <param name="args">Arguments to pass to function</param>
  133. public void ExecuteEvent(string FunctionName, object[] args)
  134. {
  135. if (m_Running == false)
  136. {
  137. // Script is inactive, do not execute!
  138. return;
  139. }
  140. DoExecuteEvent(FunctionName, args);
  141. }
  142. protected abstract void DoExecuteEvent(string FunctionName, object[] args);
  143. /// <summary>
  144. /// Compute the events handled by the current state of the script
  145. /// </summary>
  146. /// <returns>state mask</returns>
  147. public scriptEvents GetStateEventFlags()
  148. {
  149. return DoGetStateEventFlags();
  150. }
  151. protected abstract scriptEvents DoGetStateEventFlags();
  152. /// <summary>
  153. /// Stop script from running. Event execution will be ignored.
  154. /// </summary>
  155. public void StopScript()
  156. {
  157. m_Running = false;
  158. m_Disable = true;
  159. }
  160. protected void initEventFlags()
  161. {
  162. // Initialize the table if it hasn't already been done
  163. if (m_eventFlagsMap.Count > 0)
  164. {
  165. return;
  166. }
  167. m_eventFlagsMap.Add("attach", scriptEvents.attach);
  168. // m_eventFlagsMap.Add("at_rot_target",(long)scriptEvents.at_rot_target);
  169. m_eventFlagsMap.Add("at_target", scriptEvents.at_target);
  170. // m_eventFlagsMap.Add("changed",(long)scriptEvents.changed);
  171. m_eventFlagsMap.Add("collision", scriptEvents.collision);
  172. m_eventFlagsMap.Add("collision_end", scriptEvents.collision_end);
  173. m_eventFlagsMap.Add("collision_start", scriptEvents.collision_start);
  174. m_eventFlagsMap.Add("control", scriptEvents.control);
  175. m_eventFlagsMap.Add("dataserver", scriptEvents.dataserver);
  176. m_eventFlagsMap.Add("email", scriptEvents.email);
  177. m_eventFlagsMap.Add("http_response", scriptEvents.http_response);
  178. m_eventFlagsMap.Add("land_collision", scriptEvents.land_collision);
  179. m_eventFlagsMap.Add("land_collision_end", scriptEvents.land_collision_end);
  180. m_eventFlagsMap.Add("land_collision_start", scriptEvents.land_collision_start);
  181. // m_eventFlagsMap.Add("link_message",scriptEvents.link_message);
  182. m_eventFlagsMap.Add("listen", scriptEvents.listen);
  183. m_eventFlagsMap.Add("money", scriptEvents.money);
  184. m_eventFlagsMap.Add("moving_end", scriptEvents.moving_end);
  185. m_eventFlagsMap.Add("moving_start", scriptEvents.moving_start);
  186. m_eventFlagsMap.Add("not_at_rot_target", scriptEvents.not_at_rot_target);
  187. m_eventFlagsMap.Add("not_at_target", scriptEvents.not_at_target);
  188. // m_eventFlagsMap.Add("no_sensor",(long)scriptEvents.no_sensor);
  189. // m_eventFlagsMap.Add("on_rez",(long)scriptEvents.on_rez);
  190. m_eventFlagsMap.Add("remote_data", scriptEvents.remote_data);
  191. m_eventFlagsMap.Add("run_time_permissions", scriptEvents.run_time_permissions);
  192. // m_eventFlagsMap.Add("sensor",(long)scriptEvents.sensor);
  193. m_eventFlagsMap.Add("state_entry", scriptEvents.state_entry);
  194. m_eventFlagsMap.Add("state_exit", scriptEvents.state_exit);
  195. m_eventFlagsMap.Add("timer", scriptEvents.timer);
  196. m_eventFlagsMap.Add("touch", scriptEvents.touch);
  197. m_eventFlagsMap.Add("touch_end", scriptEvents.touch_end);
  198. m_eventFlagsMap.Add("touch_start", scriptEvents.touch_start);
  199. m_eventFlagsMap.Add("object_rez", scriptEvents.object_rez);
  200. }
  201. }
  202. }