Executor.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. // this is not the right place for this
  41. // for now it must match similar enums duplicated on scritp engines
  42. public enum ScriptEventCode : int
  43. {
  44. None = -1,
  45. attach = 0,
  46. state_exit = 1,
  47. timer = 2,
  48. touch = 3,
  49. collision = 4,
  50. collision_end = 5,
  51. collision_start = 6,
  52. control = 7,
  53. dataserver = 8,
  54. email = 9,
  55. http_response = 10,
  56. land_collision = 11,
  57. land_collision_end = 12,
  58. land_collision_start = 13,
  59. at_target = 14,
  60. listen = 15,
  61. money = 16,
  62. moving_end = 17,
  63. moving_start = 18,
  64. not_at_rot_target = 19,
  65. not_at_target = 20,
  66. touch_start = 21,
  67. object_rez = 22,
  68. remote_data = 23,
  69. at_rot_target = 24,
  70. transaction_result = 25,
  71. //
  72. //
  73. run_time_permissions = 28,
  74. touch_end = 29,
  75. state_entry = 30,
  76. //
  77. //
  78. changed = 33,
  79. link_message = 34,
  80. no_sensor = 35,
  81. on_rez = 36,
  82. sensor = 37,
  83. http_request = 38,
  84. path_update = 40,
  85. // marks highest numbered event
  86. Size = 41
  87. }
  88. // this is not the right place for this
  89. // for now it must match similar enums duplicated on scritp engines
  90. [Flags]
  91. public enum scriptEvents : ulong
  92. {
  93. None = 0,
  94. attach = 1,
  95. state_exit = 1UL << 1,
  96. timer = 1UL << 2,
  97. touch = 1UL << 3,
  98. collision = 1UL << 4,
  99. collision_end = 1UL << 5,
  100. collision_start = 1UL << 6,
  101. control = 1UL << 7,
  102. dataserver = 1UL << 8,
  103. email = 1UL << 9,
  104. http_response = 1UL << 10,
  105. land_collision = 1UL << 11,
  106. land_collision_end = 1UL << 12,
  107. land_collision_start = 1UL << 13,
  108. at_target = 1UL << 14,
  109. listen = 1UL << 15,
  110. money = 1UL << 16,
  111. moving_end = 1UL << 17,
  112. moving_start = 1UL << 18,
  113. not_at_rot_target = 1UL << 19,
  114. not_at_target = 1UL << 20,
  115. touch_start = 1UL << 21,
  116. object_rez = 1UL << 22,
  117. remote_data = 1UL << 23,
  118. at_rot_target = 1UL << 24,
  119. transaction_result = 1UL << 25,
  120. //
  121. //
  122. run_time_permissions = 1UL << 28,
  123. touch_end = 1UL << 29,
  124. state_entry = 1UL << 30,
  125. //
  126. //
  127. changed = 1UL << 33,
  128. link_message = 1UL << 34,
  129. no_sensor = 1UL << 35,
  130. on_rez = 1UL << 36,
  131. sensor = 1UL << 37,
  132. http_request = 1UL << 38,
  133. path_update = 1UL << 40,
  134. anytouch = touch | touch_end | touch_start,
  135. anyTarget = at_target | not_at_target | at_rot_target | not_at_rot_target
  136. }
  137. // this is not the right place for this
  138. // for now it must match similar enums duplicated on scritp engines
  139. protected static readonly Dictionary<string, scriptEvents> m_eventFlagsMap = new Dictionary<string, scriptEvents>()
  140. {
  141. {"attach", scriptEvents.attach},
  142. {"at_rot_target", scriptEvents.at_rot_target},
  143. {"at_target", scriptEvents.at_target},
  144. {"collision", scriptEvents.collision},
  145. {"collision_end", scriptEvents.collision_end},
  146. {"collision_start", scriptEvents.collision_start},
  147. {"control", scriptEvents.control},
  148. {"dataserver", scriptEvents.dataserver},
  149. {"email", scriptEvents.email},
  150. {"http_response", scriptEvents.http_response},
  151. {"land_collision", scriptEvents.land_collision},
  152. {"land_collision_end", scriptEvents.land_collision_end},
  153. {"land_collision_start", scriptEvents.land_collision_start},
  154. {"listen", scriptEvents.listen},
  155. {"money", scriptEvents.money},
  156. {"moving_end", scriptEvents.moving_end},
  157. {"moving_start", scriptEvents.moving_start},
  158. {"not_at_rot_target", scriptEvents.not_at_rot_target},
  159. {"not_at_target", scriptEvents.not_at_target},
  160. {"remote_data", scriptEvents.remote_data},
  161. {"run_time_permissions", scriptEvents.run_time_permissions},
  162. {"state_entry", scriptEvents.state_entry},
  163. {"state_exit", scriptEvents.state_exit},
  164. {"timer", scriptEvents.timer},
  165. {"touch", scriptEvents.touch},
  166. {"touch_end", scriptEvents.touch_end},
  167. {"touch_start", scriptEvents.touch_start},
  168. {"transaction_result", scriptEvents.transaction_result},
  169. {"object_rez", scriptEvents.object_rez},
  170. {"changed", scriptEvents.changed},
  171. {"link_message", scriptEvents.link_message},
  172. {"no_sensor", scriptEvents.no_sensor},
  173. {"on_rez", scriptEvents.on_rez},
  174. {"sensor", scriptEvents.sensor},
  175. {"http_request", scriptEvents.http_request},
  176. {"path_update", scriptEvents.path_update},
  177. };
  178. /// <summary>
  179. /// Contains the script to execute functions in.
  180. /// </summary>
  181. protected IScript m_Script;
  182. // Cache functions by keeping a reference to them in a dictionary
  183. private Dictionary<string, MethodInfo> Events = new Dictionary<string, MethodInfo>();
  184. private Dictionary<string, scriptEvents> m_stateEvents = new Dictionary<string, scriptEvents>();
  185. public Executor(IScript script)
  186. {
  187. m_Script = script;
  188. }
  189. public scriptEvents GetStateEventFlags(string state)
  190. {
  191. //m_log.Debug("Get event flags for " + state);
  192. // Check to see if we've already computed the flags for this state
  193. scriptEvents eventFlags = scriptEvents.None;
  194. if (m_stateEvents.ContainsKey(state))
  195. {
  196. m_stateEvents.TryGetValue(state, out eventFlags);
  197. return eventFlags;
  198. }
  199. Type type=m_Script.GetType();
  200. // Fill in the events for this state, cache the results in the map
  201. foreach (KeyValuePair<string, scriptEvents> kvp in m_eventFlagsMap)
  202. {
  203. string evname = state + "_event_" + kvp.Key;
  204. //m_log.Debug("Trying event "+evname);
  205. try
  206. {
  207. MethodInfo mi = type.GetMethod(evname);
  208. if (mi != null)
  209. {
  210. //m_log.Debug("Found handler for " + kvp.Key);
  211. eventFlags |= kvp.Value;
  212. }
  213. }
  214. catch(Exception)
  215. {
  216. //m_log.Debug("Exeption in GetMethod:\n"+e.ToString());
  217. }
  218. }
  219. // Save the flags we just computed and return the result
  220. if (eventFlags != 0)
  221. m_stateEvents.Add(state, eventFlags);
  222. //m_log.Debug("Returning {0:x}", eventFlags);
  223. return (eventFlags);
  224. }
  225. [DebuggerNonUserCode]
  226. public void ExecuteEvent(string state, string FunctionName, object[] args)
  227. {
  228. // IMPORTANT: Types and MemberInfo-derived objects require a LOT of memory.
  229. // Instead use RuntimeTypeHandle, RuntimeFieldHandle and RunTimeHandle (IntPtr) instead!
  230. string EventName = state + "_event_" + FunctionName;
  231. //#if DEBUG
  232. //m_log.Debug("ScriptEngine: Script event function name: " + EventName);
  233. //#endif
  234. if (Events.ContainsKey(EventName) == false)
  235. {
  236. // Not found, create
  237. Type type = m_Script.GetType();
  238. try
  239. {
  240. MethodInfo mi = type.GetMethod(EventName);
  241. Events.Add(EventName, mi);
  242. }
  243. catch
  244. {
  245. // m_log.Error("Event "+EventName+" not found.");
  246. // Event name not found, cache it as not found
  247. Events.Add(EventName, null);
  248. }
  249. }
  250. // Get event
  251. MethodInfo ev = null;
  252. Events.TryGetValue(EventName, out ev);
  253. if (ev == null) // No event by that name!
  254. {
  255. //m_log.Debug("ScriptEngine Can not find any event named: \String.Empty + EventName + "\String.Empty);
  256. return;
  257. }
  258. //cfk 2-7-08 dont need this right now and the default Linux build has DEBUG defined
  259. #if DEBUG
  260. //m_log.Debug("ScriptEngine: Executing function name: " + EventName);
  261. #endif
  262. // Found
  263. try
  264. {
  265. ev.Invoke(m_Script, args);
  266. }
  267. catch (TargetInvocationException tie)
  268. {
  269. // Grab the inner exception and rethrow it, unless the inner
  270. // exception is an EventAbortException as this indicates event
  271. // invocation termination due to a state change.
  272. // DO NOT THROW JUST THE INNER EXCEPTION!
  273. // FriendlyErrors depends on getting the whole exception!
  274. //
  275. if (!(tie.InnerException is EventAbortException))
  276. {
  277. throw;
  278. }
  279. }
  280. }
  281. }
  282. }