Executor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.Reflection;
  30. using System.Runtime.Remoting.Lifetime;
  31. using OpenSim.Region.ScriptEngine.Shared;
  32. using OpenSim.Region.ScriptEngine.Shared.ScriptBase;
  33. namespace OpenSim.Region.ScriptEngine.Shared.ScriptBase
  34. {
  35. public class Executor : MarshalByRefObject
  36. {
  37. /// <summary>
  38. /// Contains the script to execute functions in.
  39. /// </summary>
  40. protected IScript m_Script;
  41. protected Dictionary<string, scriptEvents> m_eventFlagsMap = new Dictionary<string, scriptEvents>();
  42. [Flags]
  43. public enum scriptEvents : int
  44. {
  45. None = 0,
  46. attach = 1,
  47. collision = 16,
  48. collision_end = 32,
  49. collision_start = 64,
  50. control = 128,
  51. dataserver = 256,
  52. email = 512,
  53. http_response = 1024,
  54. land_collision = 2048,
  55. land_collision_end = 4096,
  56. land_collision_start = 8192,
  57. at_target = 16384,
  58. listen = 32768,
  59. money = 65536,
  60. moving_end = 131072,
  61. moving_start = 262144,
  62. not_at_rot_target = 524288,
  63. not_at_target = 1048576,
  64. remote_data = 8388608,
  65. run_time_permissions = 268435456,
  66. state_entry = 1073741824,
  67. state_exit = 2,
  68. timer = 4,
  69. touch = 8,
  70. touch_end = 536870912,
  71. touch_start = 2097152,
  72. object_rez = 4194304
  73. }
  74. // Cache functions by keeping a reference to them in a dictionary
  75. private Dictionary<string, MethodInfo> Events = new Dictionary<string, MethodInfo>();
  76. private Dictionary<string, scriptEvents> m_stateEvents = new Dictionary<string, scriptEvents>();
  77. public Executor(IScript script)
  78. {
  79. m_Script = script;
  80. initEventFlags();
  81. }
  82. /// <summary>
  83. /// Make sure our object does not timeout when in AppDomain. (Called by ILease base class)
  84. /// </summary>
  85. /// <returns></returns>
  86. public override Object InitializeLifetimeService()
  87. {
  88. //Console.WriteLine("Executor: InitializeLifetimeService()");
  89. // return null;
  90. ILease lease = (ILease)base.InitializeLifetimeService();
  91. if (lease.CurrentState == LeaseState.Initial)
  92. {
  93. lease.InitialLeaseTime = TimeSpan.Zero; // TimeSpan.FromMinutes(1);
  94. // lease.SponsorshipTimeout = TimeSpan.FromMinutes(2);
  95. // lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
  96. }
  97. return lease;
  98. }
  99. public scriptEvents GetStateEventFlags(string state)
  100. {
  101. //Console.WriteLine("Get event flags for " + state);
  102. // Check to see if we've already computed the flags for this state
  103. scriptEvents eventFlags = scriptEvents.None;
  104. if (m_stateEvents.ContainsKey(state))
  105. {
  106. m_stateEvents.TryGetValue(state, out eventFlags);
  107. return eventFlags;
  108. }
  109. Type type=m_Script.GetType();
  110. // Fill in the events for this state, cache the results in the map
  111. foreach (KeyValuePair<string, scriptEvents> kvp in m_eventFlagsMap)
  112. {
  113. string evname = state + "_event_" + kvp.Key;
  114. //Console.WriteLine("Trying event "+evname);
  115. try
  116. {
  117. MethodInfo mi = type.GetMethod(evname);
  118. if (mi != null)
  119. {
  120. //Console.WriteLine("Found handler for " + kvp.Key);
  121. eventFlags |= kvp.Value;
  122. }
  123. }
  124. catch(Exception)
  125. {
  126. //Console.WriteLine("Exeption in GetMethod:\n"+e.ToString());
  127. }
  128. }
  129. // Save the flags we just computed and return the result
  130. if (eventFlags != 0)
  131. m_stateEvents.Add(state, eventFlags);
  132. //Console.WriteLine("Returning {0:x}", eventFlags);
  133. return (eventFlags);
  134. }
  135. public void ExecuteEvent(string state, string FunctionName, object[] args)
  136. {
  137. // IMPORTANT: Types and MemberInfo-derived objects require a LOT of memory.
  138. // Instead use RuntimeTypeHandle, RuntimeFieldHandle and RunTimeHandle (IntPtr) instead!
  139. string EventName = state + "_event_" + FunctionName;
  140. //#if DEBUG
  141. //Console.WriteLine("ScriptEngine: Script event function name: " + EventName);
  142. //#endif
  143. if (Events.ContainsKey(EventName) == false)
  144. {
  145. // Not found, create
  146. Type type = m_Script.GetType();
  147. try
  148. {
  149. MethodInfo mi = type.GetMethod(EventName);
  150. Events.Add(EventName, mi);
  151. }
  152. catch
  153. {
  154. Console.WriteLine("Event {0}not found", EventName);
  155. // Event name not found, cache it as not found
  156. Events.Add(EventName, null);
  157. }
  158. }
  159. // Get event
  160. MethodInfo ev = null;
  161. Events.TryGetValue(EventName, out ev);
  162. if (ev == null) // No event by that name!
  163. {
  164. //Console.WriteLine("ScriptEngine Can not find any event named: \String.Empty + EventName + "\String.Empty);
  165. return;
  166. }
  167. //cfk 2-7-08 dont need this right now and the default Linux build has DEBUG defined
  168. #if DEBUG
  169. //Console.WriteLine("ScriptEngine: Executing function name: " + EventName);
  170. #endif
  171. // Found
  172. try
  173. {
  174. ev.Invoke(m_Script, args);
  175. }
  176. catch (TargetInvocationException tie)
  177. {
  178. // Grab the inner exception and rethrow it, unless the inner
  179. // exception is an EventAbortException as this indicates event
  180. // invocation termination due to a state change.
  181. // DO NOT THROW JUST THE INNER EXCEPTION!
  182. // FriendlyErrors depends on getting the whole exception!
  183. //
  184. if ( !(tie.InnerException is EventAbortException) )
  185. {
  186. throw;
  187. }
  188. }
  189. }
  190. protected void initEventFlags()
  191. {
  192. // Initialize the table if it hasn't already been done
  193. if (m_eventFlagsMap.Count > 0)
  194. {
  195. return;
  196. }
  197. m_eventFlagsMap.Add("attach", scriptEvents.attach);
  198. // m_eventFlagsMap.Add("at_rot_target",(long)scriptEvents.at_rot_target);
  199. m_eventFlagsMap.Add("at_target", scriptEvents.at_target);
  200. // m_eventFlagsMap.Add("changed",(long)scriptEvents.changed);
  201. m_eventFlagsMap.Add("collision", scriptEvents.collision);
  202. m_eventFlagsMap.Add("collision_end", scriptEvents.collision_end);
  203. m_eventFlagsMap.Add("collision_start", scriptEvents.collision_start);
  204. m_eventFlagsMap.Add("control", scriptEvents.control);
  205. m_eventFlagsMap.Add("dataserver", scriptEvents.dataserver);
  206. m_eventFlagsMap.Add("email", scriptEvents.email);
  207. m_eventFlagsMap.Add("http_response", scriptEvents.http_response);
  208. m_eventFlagsMap.Add("land_collision", scriptEvents.land_collision);
  209. m_eventFlagsMap.Add("land_collision_end", scriptEvents.land_collision_end);
  210. m_eventFlagsMap.Add("land_collision_start", scriptEvents.land_collision_start);
  211. // m_eventFlagsMap.Add("link_message",scriptEvents.link_message);
  212. m_eventFlagsMap.Add("listen", scriptEvents.listen);
  213. m_eventFlagsMap.Add("money", scriptEvents.money);
  214. m_eventFlagsMap.Add("moving_end", scriptEvents.moving_end);
  215. m_eventFlagsMap.Add("moving_start", scriptEvents.moving_start);
  216. m_eventFlagsMap.Add("not_at_rot_target", scriptEvents.not_at_rot_target);
  217. m_eventFlagsMap.Add("not_at_target", scriptEvents.not_at_target);
  218. // m_eventFlagsMap.Add("no_sensor",(long)scriptEvents.no_sensor);
  219. // m_eventFlagsMap.Add("on_rez",(long)scriptEvents.on_rez);
  220. m_eventFlagsMap.Add("remote_data", scriptEvents.remote_data);
  221. m_eventFlagsMap.Add("run_time_permissions", scriptEvents.run_time_permissions);
  222. // m_eventFlagsMap.Add("sensor",(long)scriptEvents.sensor);
  223. m_eventFlagsMap.Add("state_entry", scriptEvents.state_entry);
  224. m_eventFlagsMap.Add("state_exit", scriptEvents.state_exit);
  225. m_eventFlagsMap.Add("timer", scriptEvents.timer);
  226. m_eventFlagsMap.Add("touch", scriptEvents.touch);
  227. m_eventFlagsMap.Add("touch_end", scriptEvents.touch_end);
  228. m_eventFlagsMap.Add("touch_start", scriptEvents.touch_start);
  229. m_eventFlagsMap.Add("object_rez", scriptEvents.object_rez);
  230. }
  231. }
  232. }