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/XEngine | |
parent | * Updates OGP module to define a default starting point (diff) | |
download | opensim-SC_OLD-a2985b5655d336b6da3ae48dcf80b9cbb16b90fb.zip opensim-SC_OLD-a2985b5655d336b6da3ae48dcf80b9cbb16b90fb.tar.gz opensim-SC_OLD-a2985b5655d336b6da3ae48dcf80b9cbb16b90fb.tar.bz2 opensim-SC_OLD-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/XEngine')
-rw-r--r-- | OpenSim/Region/ScriptEngine/XEngine/Executor.cs | 136 | ||||
-rw-r--r-- | OpenSim/Region/ScriptEngine/XEngine/ExecutorBase.cs | 183 | ||||
-rw-r--r-- | OpenSim/Region/ScriptEngine/XEngine/XEngine.cs | 9 |
3 files changed, 3 insertions, 325 deletions
diff --git a/OpenSim/Region/ScriptEngine/XEngine/Executor.cs b/OpenSim/Region/ScriptEngine/XEngine/Executor.cs deleted file mode 100644 index b848a02..0000000 --- a/OpenSim/Region/ScriptEngine/XEngine/Executor.cs +++ /dev/null | |||
@@ -1,136 +0,0 @@ | |||
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.Shared; | ||
32 | using OpenSim.Region.ScriptEngine.Shared.ScriptBase; | ||
33 | |||
34 | namespace OpenSim.Region.ScriptEngine.XEngine | ||
35 | { | ||
36 | public class Executor : ExecutorBase | ||
37 | { | ||
38 | // Cache functions by keeping a reference to them in a dictionary | ||
39 | private Dictionary<string, MethodInfo> Events = new Dictionary<string, MethodInfo>(); | ||
40 | private Dictionary<string, scriptEvents> m_stateEvents = new Dictionary<string, scriptEvents>(); | ||
41 | |||
42 | public Executor(IScript script) : base(script) | ||
43 | { | ||
44 | initEventFlags(); | ||
45 | } | ||
46 | |||
47 | |||
48 | protected override scriptEvents DoGetStateEventFlags(string state) | ||
49 | { | ||
50 | //Console.WriteLine("Get event flags for " + state); | ||
51 | |||
52 | // Check to see if we've already computed the flags for this state | ||
53 | scriptEvents eventFlags = scriptEvents.None; | ||
54 | if (m_stateEvents.ContainsKey(state)) | ||
55 | { | ||
56 | m_stateEvents.TryGetValue(state, out eventFlags); | ||
57 | return eventFlags; | ||
58 | } | ||
59 | |||
60 | Type type=m_Script.GetType(); | ||
61 | |||
62 | // Fill in the events for this state, cache the results in the map | ||
63 | foreach (KeyValuePair<string, scriptEvents> kvp in m_eventFlagsMap) | ||
64 | { | ||
65 | string evname = state + "_event_" + kvp.Key; | ||
66 | //Console.WriteLine("Trying event "+evname); | ||
67 | try | ||
68 | { | ||
69 | MethodInfo mi = type.GetMethod(evname); | ||
70 | if (mi != null) | ||
71 | { | ||
72 | //Console.WriteLine("Found handler for " + kvp.Key); | ||
73 | eventFlags |= kvp.Value; | ||
74 | } | ||
75 | } | ||
76 | catch(Exception) | ||
77 | { | ||
78 | //Console.WriteLine("Exeption in GetMethod:\n"+e.ToString()); | ||
79 | } | ||
80 | } | ||
81 | |||
82 | // Save the flags we just computed and return the result | ||
83 | if (eventFlags != 0) | ||
84 | m_stateEvents.Add(state, eventFlags); | ||
85 | |||
86 | //Console.WriteLine("Returning {0:x}", eventFlags); | ||
87 | return (eventFlags); | ||
88 | } | ||
89 | |||
90 | protected override void DoExecuteEvent(string state, string FunctionName, object[] args) | ||
91 | { | ||
92 | // IMPORTANT: Types and MemberInfo-derived objects require a LOT of memory. | ||
93 | // Instead use RuntimeTypeHandle, RuntimeFieldHandle and RunTimeHandle (IntPtr) instead! | ||
94 | |||
95 | string EventName = state + "_event_" + FunctionName; | ||
96 | |||
97 | //#if DEBUG | ||
98 | // Console.WriteLine("ScriptEngine: Script event function name: " + EventName); | ||
99 | //#endif | ||
100 | |||
101 | if (Events.ContainsKey(EventName) == false) | ||
102 | { | ||
103 | // Not found, create | ||
104 | Type type = m_Script.GetType(); | ||
105 | try | ||
106 | { | ||
107 | MethodInfo mi = type.GetMethod(EventName); | ||
108 | Events.Add(EventName, mi); | ||
109 | } | ||
110 | catch | ||
111 | { | ||
112 | Console.WriteLine("Event {0}not found", EventName); | ||
113 | // Event name not found, cache it as not found | ||
114 | Events.Add(EventName, null); | ||
115 | } | ||
116 | } | ||
117 | |||
118 | // Get event | ||
119 | MethodInfo ev = null; | ||
120 | Events.TryGetValue(EventName, out ev); | ||
121 | |||
122 | if (ev == null) // No event by that name! | ||
123 | { | ||
124 | //Console.WriteLine("ScriptEngine Can not find any event named: \String.Empty + EventName + "\String.Empty); | ||
125 | return; | ||
126 | } | ||
127 | |||
128 | //cfk 2-7-08 dont need this right now and the default Linux build has DEBUG defined | ||
129 | #if DEBUG | ||
130 | //Console.WriteLine("ScriptEngine: Executing function name: " + EventName); | ||
131 | #endif | ||
132 | // Found | ||
133 | ev.Invoke(m_Script, args); | ||
134 | } | ||
135 | } | ||
136 | } | ||
diff --git a/OpenSim/Region/ScriptEngine/XEngine/ExecutorBase.cs b/OpenSim/Region/ScriptEngine/XEngine/ExecutorBase.cs deleted file mode 100644 index 79c3cc9..0000000 --- a/OpenSim/Region/ScriptEngine/XEngine/ExecutorBase.cs +++ /dev/null | |||
@@ -1,183 +0,0 @@ | |||
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.Runtime.Remoting.Lifetime; | ||
31 | using OpenSim.Region.ScriptEngine.Shared; | ||
32 | using OpenSim.Region.ScriptEngine.Shared.ScriptBase; | ||
33 | using OpenSim.Region.ScriptEngine.Interfaces; | ||
34 | |||
35 | namespace OpenSim.Region.ScriptEngine.XEngine | ||
36 | { | ||
37 | public abstract class ExecutorBase : 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 | /// <summary> | ||
80 | /// Create a new instance of ExecutorBase | ||
81 | /// </summary> | ||
82 | /// <param name="Script"></param> | ||
83 | public ExecutorBase(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 override 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 | /// <summary> | ||
109 | /// Get current AppDomain | ||
110 | /// </summary> | ||
111 | /// <returns>Current AppDomain</returns> | ||
112 | public AppDomain GetAppDomain() | ||
113 | { | ||
114 | return AppDomain.CurrentDomain; | ||
115 | } | ||
116 | |||
117 | /// <summary> | ||
118 | /// Execute a specific function/event in script. | ||
119 | /// </summary> | ||
120 | /// <param name="FunctionName">Name of function to execute</param> | ||
121 | /// <param name="args">Arguments to pass to function</param> | ||
122 | public void ExecuteEvent(string state, string FunctionName, object[] args) | ||
123 | { | ||
124 | DoExecuteEvent(state, FunctionName, args); | ||
125 | } | ||
126 | |||
127 | protected abstract void DoExecuteEvent(string state, string FunctionName, object[] args); | ||
128 | |||
129 | /// <summary> | ||
130 | /// Compute the events handled by the current state of the script | ||
131 | /// </summary> | ||
132 | /// <returns>state mask</returns> | ||
133 | public scriptEvents GetStateEventFlags(string state) | ||
134 | { | ||
135 | return DoGetStateEventFlags(state); | ||
136 | } | ||
137 | |||
138 | protected abstract scriptEvents DoGetStateEventFlags(string state); | ||
139 | |||
140 | protected void initEventFlags() | ||
141 | { | ||
142 | // Initialize the table if it hasn't already been done | ||
143 | if (m_eventFlagsMap.Count > 0) | ||
144 | { | ||
145 | return; | ||
146 | } | ||
147 | |||
148 | m_eventFlagsMap.Add("attach", scriptEvents.attach); | ||
149 | // m_eventFlagsMap.Add("at_rot_target",(long)scriptEvents.at_rot_target); | ||
150 | m_eventFlagsMap.Add("at_target", scriptEvents.at_target); | ||
151 | // m_eventFlagsMap.Add("changed",(long)scriptEvents.changed); | ||
152 | m_eventFlagsMap.Add("collision", scriptEvents.collision); | ||
153 | m_eventFlagsMap.Add("collision_end", scriptEvents.collision_end); | ||
154 | m_eventFlagsMap.Add("collision_start", scriptEvents.collision_start); | ||
155 | m_eventFlagsMap.Add("control", scriptEvents.control); | ||
156 | m_eventFlagsMap.Add("dataserver", scriptEvents.dataserver); | ||
157 | m_eventFlagsMap.Add("email", scriptEvents.email); | ||
158 | m_eventFlagsMap.Add("http_response", scriptEvents.http_response); | ||
159 | m_eventFlagsMap.Add("land_collision", scriptEvents.land_collision); | ||
160 | m_eventFlagsMap.Add("land_collision_end", scriptEvents.land_collision_end); | ||
161 | m_eventFlagsMap.Add("land_collision_start", scriptEvents.land_collision_start); | ||
162 | // m_eventFlagsMap.Add("link_message",scriptEvents.link_message); | ||
163 | m_eventFlagsMap.Add("listen", scriptEvents.listen); | ||
164 | m_eventFlagsMap.Add("money", scriptEvents.money); | ||
165 | m_eventFlagsMap.Add("moving_end", scriptEvents.moving_end); | ||
166 | m_eventFlagsMap.Add("moving_start", scriptEvents.moving_start); | ||
167 | m_eventFlagsMap.Add("not_at_rot_target", scriptEvents.not_at_rot_target); | ||
168 | m_eventFlagsMap.Add("not_at_target", scriptEvents.not_at_target); | ||
169 | // m_eventFlagsMap.Add("no_sensor",(long)scriptEvents.no_sensor); | ||
170 | // m_eventFlagsMap.Add("on_rez",(long)scriptEvents.on_rez); | ||
171 | m_eventFlagsMap.Add("remote_data", scriptEvents.remote_data); | ||
172 | m_eventFlagsMap.Add("run_time_permissions", scriptEvents.run_time_permissions); | ||
173 | // m_eventFlagsMap.Add("sensor",(long)scriptEvents.sensor); | ||
174 | m_eventFlagsMap.Add("state_entry", scriptEvents.state_entry); | ||
175 | m_eventFlagsMap.Add("state_exit", scriptEvents.state_exit); | ||
176 | m_eventFlagsMap.Add("timer", scriptEvents.timer); | ||
177 | m_eventFlagsMap.Add("touch", scriptEvents.touch); | ||
178 | m_eventFlagsMap.Add("touch_end", scriptEvents.touch_end); | ||
179 | m_eventFlagsMap.Add("touch_start", scriptEvents.touch_start); | ||
180 | m_eventFlagsMap.Add("object_rez", scriptEvents.object_rez); | ||
181 | } | ||
182 | } | ||
183 | } | ||
diff --git a/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs b/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs index c528c02..cfe0599 100644 --- a/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs +++ b/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs | |||
@@ -863,7 +863,6 @@ namespace OpenSim.Region.ScriptEngine.XEngine | |||
863 | private LLUUID m_ObjectID; | 863 | private LLUUID m_ObjectID; |
864 | private LLUUID m_AssetID; | 864 | private LLUUID m_AssetID; |
865 | private IScript m_Script; | 865 | private IScript m_Script; |
866 | private Executor m_Executor; | ||
867 | private LLUUID m_AppDomain; | 866 | private LLUUID m_AppDomain; |
868 | private DetectParams[] m_DetectParams; | 867 | private DetectParams[] m_DetectParams; |
869 | private bool m_TimerQueued; | 868 | private bool m_TimerQueued; |
@@ -1009,12 +1008,10 @@ namespace OpenSim.Region.ScriptEngine.XEngine | |||
1009 | m_Script.InitApi(kv.Key, kv.Value); | 1008 | m_Script.InitApi(kv.Key, kv.Value); |
1010 | } | 1009 | } |
1011 | 1010 | ||
1012 | m_Executor = new Executor(m_Script); | ||
1013 | |||
1014 | // m_Engine.Log.Debug("[XEngine] Script instance created"); | 1011 | // m_Engine.Log.Debug("[XEngine] Script instance created"); |
1015 | 1012 | ||
1016 | part.SetScriptEvents(m_ItemID, | 1013 | part.SetScriptEvents(m_ItemID, |
1017 | (int)m_Executor.GetStateEventFlags(State)); | 1014 | (int)m_Script.GetStateEventFlags(State)); |
1018 | } | 1015 | } |
1019 | catch (Exception e) | 1016 | catch (Exception e) |
1020 | { | 1017 | { |
@@ -1274,7 +1271,7 @@ namespace OpenSim.Region.ScriptEngine.XEngine | |||
1274 | if (part != null) | 1271 | if (part != null) |
1275 | { | 1272 | { |
1276 | part.SetScriptEvents(m_ItemID, | 1273 | part.SetScriptEvents(m_ItemID, |
1277 | (int)m_Executor.GetStateEventFlags(State)); | 1274 | (int)m_Script.GetStateEventFlags(State)); |
1278 | } | 1275 | } |
1279 | } | 1276 | } |
1280 | else | 1277 | else |
@@ -1290,7 +1287,7 @@ namespace OpenSim.Region.ScriptEngine.XEngine | |||
1290 | m_EventStart = DateTime.Now; | 1287 | m_EventStart = DateTime.Now; |
1291 | m_InEvent = true; | 1288 | m_InEvent = true; |
1292 | 1289 | ||
1293 | m_Executor.ExecuteEvent(State, data.EventName, data.Params); | 1290 | m_Script.ExecuteEvent(State, data.EventName, data.Params); |
1294 | 1291 | ||
1295 | m_InEvent = false; | 1292 | m_InEvent = false; |
1296 | m_CurrentEvent = String.Empty; | 1293 | m_CurrentEvent = String.Empty; |