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