Executor.cs 10 KB

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