From e70cdbc5accc5025ed4e6262ff01ce50bf553141 Mon Sep 17 00:00:00 2001 From: Tedd Hansen Date: Sun, 19 Aug 2007 10:28:27 +0000 Subject: Added event method invoke cache to Executor. "Bind once, Invoke multiple times". Will speed up script event execution considerable. But at the cost of some memory (will be optimized later with RuntimeXHandle). --- OpenSim/Region/ScriptEngine/Common/Executor.cs | 44 ++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 7 deletions(-) (limited to 'OpenSim/Region/ScriptEngine/Common/Executor.cs') diff --git a/OpenSim/Region/ScriptEngine/Common/Executor.cs b/OpenSim/Region/ScriptEngine/Common/Executor.cs index ca6459b..dedf00d 100644 --- a/OpenSim/Region/ScriptEngine/Common/Executor.cs +++ b/OpenSim/Region/ScriptEngine/Common/Executor.cs @@ -5,7 +5,7 @@ using System.Reflection; namespace OpenSim.Region.ScriptEngine.Common { - public class Executor: MarshalByRefObject + public class Executor : MarshalByRefObject { /* TODO: * @@ -16,10 +16,12 @@ namespace OpenSim.Region.ScriptEngine.Common */ private IScript m_Script; + private Dictionary Events = new Dictionary(); public Executor(IScript Script) { m_Script = Script; + } public void ExecuteEvent(string FunctionName, object[] args) { @@ -34,23 +36,51 @@ namespace OpenSim.Region.ScriptEngine.Common //} //} - Type type = m_Script.GetType(); - Console.WriteLine("ScriptEngine Executor.ExecuteEvent: \"" + m_Script.State() + "_event_" + FunctionName + "\""); + string EventName = m_Script.State() + "_event_" + FunctionName; + + //type.InvokeMember(EventName, BindingFlags.InvokeMethod, null, m_Script, args); + + Console.WriteLine("ScriptEngine Executor.ExecuteEvent: \"" + EventName + "\""); + + if (Events.ContainsKey(EventName) == false) + { + // Not found, create + Type type = m_Script.GetType(); + try + { + MethodInfo mi = type.GetMethod(EventName); + Events.Add(EventName, mi); + } + catch (Exception e) + { + // Event name not found, cache it as not found + Events.Add(EventName, null); + } + } + + // Get event + MethodInfo ev = null; + Events.TryGetValue(EventName, out ev); + + if (ev == null) // No event by that name! + return; + + // Found try { - type.InvokeMember(m_Script.State() + "_event_" + FunctionName, BindingFlags.InvokeMethod, null, m_Script, args); + // Invoke it + ev.Invoke(m_Script, args); + } catch (Exception e) { // TODO: Send to correct place Console.WriteLine("ScriptEngine Exception attempting to executing script function: " + e.ToString()); } - - } - } + } -- cgit v1.1