diff options
author | Melanie Thielker | 2008-08-27 21:36:03 +0000 |
---|---|---|
committer | Melanie Thielker | 2008-08-27 21:36:03 +0000 |
commit | a2985b5655d336b6da3ae48dcf80b9cbb16b90fb (patch) | |
tree | 1b02f9b907f75d4085ca6050ed481d82b9c2cfc0 /OpenSim/Region/ScriptEngine/Shared/Api/Runtime/Executor.cs | |
parent | * Updates OGP module to define a default starting point (diff) | |
download | opensim-SC-a2985b5655d336b6da3ae48dcf80b9cbb16b90fb.zip opensim-SC-a2985b5655d336b6da3ae48dcf80b9cbb16b90fb.tar.gz opensim-SC-a2985b5655d336b6da3ae48dcf80b9cbb16b90fb.tar.bz2 opensim-SC-a2985b5655d336b6da3ae48dcf80b9cbb16b90fb.tar.xz |
Refactor Executor into the script app domain and IScript. This changes
an implicit reference into a proxied one and further reduces memory
consumption of XEngine
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Shared/Api/Runtime/Executor.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/Shared/Api/Runtime/Executor.cs | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/Executor.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/Executor.cs new file mode 100644 index 0000000..0241da5 --- /dev/null +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/Executor.cs | |||
@@ -0,0 +1,240 @@ | |||
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 System.Runtime.Remoting.Lifetime; | ||
32 | using OpenSim.Region.ScriptEngine.Shared; | ||
33 | using OpenSim.Region.ScriptEngine.Shared.ScriptBase; | ||
34 | |||
35 | namespace OpenSim.Region.ScriptEngine.Shared.ScriptBase | ||
36 | { | ||
37 | public class Executor : MarshalByRefObject | ||
38 | { | ||
39 | /// <summary> | ||
40 | /// Contains the script to execute functions in. | ||
41 | /// </summary> | ||
42 | protected IScript m_Script; | ||
43 | |||
44 | protected Dictionary<string, scriptEvents> m_eventFlagsMap = new Dictionary<string, scriptEvents>(); | ||
45 | |||
46 | [Flags] | ||
47 | public enum scriptEvents : int | ||
48 | { | ||
49 | None = 0, | ||
50 | attach = 1, | ||
51 | collision = 16, | ||
52 | collision_end = 32, | ||
53 | collision_start = 64, | ||
54 | control = 128, | ||
55 | dataserver = 256, | ||
56 | email = 512, | ||
57 | http_response = 1024, | ||
58 | land_collision = 2048, | ||
59 | land_collision_end = 4096, | ||
60 | land_collision_start = 8192, | ||
61 | at_target = 16384, | ||
62 | listen = 32768, | ||
63 | money = 65536, | ||
64 | moving_end = 131072, | ||
65 | moving_start = 262144, | ||
66 | not_at_rot_target = 524288, | ||
67 | not_at_target = 1048576, | ||
68 | remote_data = 8388608, | ||
69 | run_time_permissions = 268435456, | ||
70 | state_entry = 1073741824, | ||
71 | state_exit = 2, | ||
72 | timer = 4, | ||
73 | touch = 8, | ||
74 | touch_end = 536870912, | ||
75 | touch_start = 2097152, | ||
76 | object_rez = 4194304 | ||
77 | } | ||
78 | |||
79 | // Cache functions by keeping a reference to them in a dictionary | ||
80 | private Dictionary<string, MethodInfo> Events = new Dictionary<string, MethodInfo>(); | ||
81 | private Dictionary<string, scriptEvents> m_stateEvents = new Dictionary<string, scriptEvents>(); | ||
82 | |||
83 | public Executor(IScript script) | ||
84 | { | ||
85 | m_Script = script; | ||
86 | initEventFlags(); | ||
87 | } | ||
88 | |||
89 | /// <summary> | ||
90 | /// Make sure our object does not timeout when in AppDomain. (Called by ILease base class) | ||
91 | /// </summary> | ||
92 | /// <returns></returns> | ||
93 | public Object InitializeLifetimeService() | ||
94 | { | ||
95 | //Console.WriteLine("Executor: InitializeLifetimeService()"); | ||
96 | // return null; | ||
97 | ILease lease = (ILease)base.InitializeLifetimeService(); | ||
98 | |||
99 | if (lease.CurrentState == LeaseState.Initial) | ||
100 | { | ||
101 | lease.InitialLeaseTime = TimeSpan.Zero; // TimeSpan.FromMinutes(1); | ||
102 | // lease.SponsorshipTimeout = TimeSpan.FromMinutes(2); | ||
103 | // lease.RenewOnCallTime = TimeSpan.FromSeconds(2); | ||
104 | } | ||
105 | return lease; | ||
106 | } | ||
107 | |||
108 | |||
109 | public scriptEvents GetStateEventFlags(string state) | ||
110 | { | ||
111 | //Console.WriteLine("Get event flags for " + state); | ||
112 | |||
113 | // Check to see if we've already computed the flags for this state | ||
114 | scriptEvents eventFlags = scriptEvents.None; | ||
115 | if (m_stateEvents.ContainsKey(state)) | ||
116 | { | ||
117 | m_stateEvents.TryGetValue(state, out eventFlags); | ||
118 | return eventFlags; | ||
119 | } | ||
120 | |||
121 | Type type=m_Script.GetType(); | ||
122 | |||
123 | // Fill in the events for this state, cache the results in the map | ||
124 | foreach (KeyValuePair<string, scriptEvents> kvp in m_eventFlagsMap) | ||
125 | { | ||
126 | string evname = state + "_event_" + kvp.Key; | ||
127 | //Console.WriteLine("Trying event "+evname); | ||
128 | try | ||
129 | { | ||
130 | MethodInfo mi = type.GetMethod(evname); | ||
131 | if (mi != null) | ||
132 | { | ||
133 | //Console.WriteLine("Found handler for " + kvp.Key); | ||
134 | eventFlags |= kvp.Value; | ||
135 | } | ||
136 | } | ||
137 | catch(Exception) | ||
138 | { | ||
139 | //Console.WriteLine("Exeption in GetMethod:\n"+e.ToString()); | ||
140 | } | ||
141 | } | ||
142 | |||
143 | // Save the flags we just computed and return the result | ||
144 | if (eventFlags != 0) | ||
145 | m_stateEvents.Add(state, eventFlags); | ||
146 | |||
147 | //Console.WriteLine("Returning {0:x}", eventFlags); | ||
148 | return (eventFlags); | ||
149 | } | ||
150 | |||
151 | public void ExecuteEvent(string state, string FunctionName, object[] args) | ||
152 | { | ||
153 | // IMPORTANT: Types and MemberInfo-derived objects require a LOT of memory. | ||
154 | // Instead use RuntimeTypeHandle, RuntimeFieldHandle and RunTimeHandle (IntPtr) instead! | ||
155 | |||
156 | string EventName = state + "_event_" + FunctionName; | ||
157 | |||
158 | //#if DEBUG | ||
159 | // Console.WriteLine("ScriptEngine: Script event function name: " + EventName); | ||
160 | //#endif | ||
161 | |||
162 | if (Events.ContainsKey(EventName) == false) | ||
163 | { | ||
164 | // Not found, create | ||
165 | Type type = m_Script.GetType(); | ||
166 | try | ||
167 | { | ||
168 | MethodInfo mi = type.GetMethod(EventName); | ||
169 | Events.Add(EventName, mi); | ||
170 | } | ||
171 | catch | ||
172 | { | ||
173 | Console.WriteLine("Event {0}not found", EventName); | ||
174 | // Event name not found, cache it as not found | ||
175 | Events.Add(EventName, null); | ||
176 | } | ||
177 | } | ||
178 | |||
179 | // Get event | ||
180 | MethodInfo ev = null; | ||
181 | Events.TryGetValue(EventName, out ev); | ||
182 | |||
183 | if (ev == null) // No event by that name! | ||
184 | { | ||
185 | //Console.WriteLine("ScriptEngine Can not find any event named: \String.Empty + EventName + "\String.Empty); | ||
186 | return; | ||
187 | } | ||
188 | |||
189 | //cfk 2-7-08 dont need this right now and the default Linux build has DEBUG defined | ||
190 | #if DEBUG | ||
191 | //Console.WriteLine("ScriptEngine: Executing function name: " + EventName); | ||
192 | #endif | ||
193 | // Found | ||
194 | ev.Invoke(m_Script, args); | ||
195 | } | ||
196 | |||
197 | protected void initEventFlags() | ||
198 | { | ||
199 | // Initialize the table if it hasn't already been done | ||
200 | if (m_eventFlagsMap.Count > 0) | ||
201 | { | ||
202 | return; | ||
203 | } | ||
204 | |||
205 | m_eventFlagsMap.Add("attach", scriptEvents.attach); | ||
206 | // m_eventFlagsMap.Add("at_rot_target",(long)scriptEvents.at_rot_target); | ||
207 | m_eventFlagsMap.Add("at_target", scriptEvents.at_target); | ||
208 | // m_eventFlagsMap.Add("changed",(long)scriptEvents.changed); | ||
209 | m_eventFlagsMap.Add("collision", scriptEvents.collision); | ||
210 | m_eventFlagsMap.Add("collision_end", scriptEvents.collision_end); | ||
211 | m_eventFlagsMap.Add("collision_start", scriptEvents.collision_start); | ||
212 | m_eventFlagsMap.Add("control", scriptEvents.control); | ||
213 | m_eventFlagsMap.Add("dataserver", scriptEvents.dataserver); | ||
214 | m_eventFlagsMap.Add("email", scriptEvents.email); | ||
215 | m_eventFlagsMap.Add("http_response", scriptEvents.http_response); | ||
216 | m_eventFlagsMap.Add("land_collision", scriptEvents.land_collision); | ||
217 | m_eventFlagsMap.Add("land_collision_end", scriptEvents.land_collision_end); | ||
218 | m_eventFlagsMap.Add("land_collision_start", scriptEvents.land_collision_start); | ||
219 | // m_eventFlagsMap.Add("link_message",scriptEvents.link_message); | ||
220 | m_eventFlagsMap.Add("listen", scriptEvents.listen); | ||
221 | m_eventFlagsMap.Add("money", scriptEvents.money); | ||
222 | m_eventFlagsMap.Add("moving_end", scriptEvents.moving_end); | ||
223 | m_eventFlagsMap.Add("moving_start", scriptEvents.moving_start); | ||
224 | m_eventFlagsMap.Add("not_at_rot_target", scriptEvents.not_at_rot_target); | ||
225 | m_eventFlagsMap.Add("not_at_target", scriptEvents.not_at_target); | ||
226 | // m_eventFlagsMap.Add("no_sensor",(long)scriptEvents.no_sensor); | ||
227 | // m_eventFlagsMap.Add("on_rez",(long)scriptEvents.on_rez); | ||
228 | m_eventFlagsMap.Add("remote_data", scriptEvents.remote_data); | ||
229 | m_eventFlagsMap.Add("run_time_permissions", scriptEvents.run_time_permissions); | ||
230 | // m_eventFlagsMap.Add("sensor",(long)scriptEvents.sensor); | ||
231 | m_eventFlagsMap.Add("state_entry", scriptEvents.state_entry); | ||
232 | m_eventFlagsMap.Add("state_exit", scriptEvents.state_exit); | ||
233 | m_eventFlagsMap.Add("timer", scriptEvents.timer); | ||
234 | m_eventFlagsMap.Add("touch", scriptEvents.touch); | ||
235 | m_eventFlagsMap.Add("touch_end", scriptEvents.touch_end); | ||
236 | m_eventFlagsMap.Add("touch_start", scriptEvents.touch_start); | ||
237 | m_eventFlagsMap.Add("object_rez", scriptEvents.object_rez); | ||
238 | } | ||
239 | } | ||
240 | } | ||