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