Executor.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 OpenSim.Region.ScriptEngine.XEngine.Script;
  31. namespace OpenSim.Region.ScriptEngine.XEngine
  32. {
  33. public class Executor : ExecutorBase
  34. {
  35. // Cache functions by keeping a reference to them in a dictionary
  36. private Dictionary<string, MethodInfo> Events = new Dictionary<string, MethodInfo>();
  37. private Dictionary<string, scriptEvents> m_stateEvents = new Dictionary<string, scriptEvents>();
  38. public Executor(IScript script) : base(script)
  39. {
  40. initEventFlags();
  41. }
  42. protected override scriptEvents DoGetStateEventFlags()
  43. {
  44. //Console.WriteLine("Get event flags for " + m_Script.State);
  45. // Check to see if we've already computed the flags for this state
  46. scriptEvents eventFlags = scriptEvents.None;
  47. if (m_stateEvents.ContainsKey(m_Script.State))
  48. {
  49. m_stateEvents.TryGetValue(m_Script.State, out eventFlags);
  50. return eventFlags;
  51. }
  52. Type type=m_Script.GetType();
  53. // Fill in the events for this state, cache the results in the map
  54. foreach (KeyValuePair<string, scriptEvents> kvp in m_eventFlagsMap)
  55. {
  56. string evname = m_Script.State + "_event_" + kvp.Key;
  57. //Console.WriteLine("Trying event "+evname);
  58. try
  59. {
  60. MethodInfo mi = type.GetMethod(evname);
  61. if (mi != null)
  62. {
  63. //Console.WriteLine("Found handler for " + kvp.Key);
  64. eventFlags |= kvp.Value;
  65. }
  66. }
  67. catch(Exception e)
  68. {
  69. //Console.WriteLine("Exeption in GetMethod:\n"+e.ToString());
  70. }
  71. }
  72. // Save the flags we just computed and return the result
  73. if(eventFlags != 0)
  74. m_stateEvents.Add(m_Script.State, eventFlags);
  75. //Console.WriteLine("Returning {0:x}", eventFlags);
  76. return (eventFlags);
  77. }
  78. protected override void DoExecuteEvent(string FunctionName, object[] args)
  79. {
  80. // IMPORTANT: Types and MemberInfo-derived objects require a LOT of memory.
  81. // Instead use RuntimeTypeHandle, RuntimeFieldHandle and RunTimeHandle (IntPtr) instead!
  82. string EventName = m_Script.State + "_event_" + FunctionName;
  83. //#if DEBUG
  84. // Console.WriteLine("ScriptEngine: Script event function name: " + EventName);
  85. //#endif
  86. if (Events.ContainsKey(EventName) == false)
  87. {
  88. // Not found, create
  89. Type type = m_Script.GetType();
  90. try
  91. {
  92. MethodInfo mi = type.GetMethod(EventName);
  93. Events.Add(EventName, mi);
  94. }
  95. catch
  96. {
  97. Console.WriteLine("Event {0}not found", EventName);
  98. // Event name not found, cache it as not found
  99. Events.Add(EventName, null);
  100. }
  101. }
  102. // Get event
  103. MethodInfo ev = null;
  104. Events.TryGetValue(EventName, out ev);
  105. if (ev == null) // No event by that name!
  106. {
  107. //Console.WriteLine("ScriptEngine Can not find any event named: \String.Empty + EventName + "\String.Empty);
  108. return;
  109. }
  110. //cfk 2-7-08 dont need this right now and the default Linux build has DEBUG defined
  111. #if DEBUG
  112. //Console.WriteLine("ScriptEngine: Executing function name: " + EventName);
  113. #endif
  114. // Found
  115. ev.Invoke(m_Script, args);
  116. }
  117. }
  118. }