diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/DotNetEngine/ScriptEngine.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/DotNetEngine/ScriptEngine.cs | 268 |
1 files changed, 260 insertions, 8 deletions
diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/ScriptEngine.cs b/OpenSim/Region/ScriptEngine/DotNetEngine/ScriptEngine.cs index e785cc0..7ec71c2 100644 --- a/OpenSim/Region/ScriptEngine/DotNetEngine/ScriptEngine.cs +++ b/OpenSim/Region/ScriptEngine/DotNetEngine/ScriptEngine.cs | |||
@@ -26,29 +26,281 @@ | |||
26 | */ | 26 | */ |
27 | 27 | ||
28 | using System; | 28 | using System; |
29 | using System.Collections.Generic; | ||
30 | using System.Reflection; | ||
31 | using log4net; | ||
29 | using Nini.Config; | 32 | using Nini.Config; |
33 | using OpenSim.Region.Interfaces; | ||
34 | using OpenSim.Region.Environment.Interfaces; | ||
30 | using OpenSim.Region.Environment.Scenes; | 35 | using OpenSim.Region.Environment.Scenes; |
36 | using OpenSim.Region.ScriptEngine.Interfaces; | ||
37 | using OpenMetaverse; | ||
38 | using OpenSim.Region.ScriptEngine.Shared; | ||
39 | using OpenSim.Region.ScriptEngine.Common; | ||
31 | 40 | ||
32 | namespace OpenSim.Region.ScriptEngine.DotNetEngine | 41 | namespace OpenSim.Region.ScriptEngine.DotNetEngine |
33 | { | 42 | { |
34 | [Serializable] | 43 | [Serializable] |
35 | public class ScriptEngine : Common.ScriptEngineBase.ScriptEngine | 44 | public class ScriptEngine : IRegionModule, IEventReceiver, IScriptModule |
36 | { | 45 | { |
37 | // We need to override a few things for our DotNetEngine | 46 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
38 | public override void Initialise(Scene scene, IConfigSource config) | 47 | |
48 | public static List<ScriptEngine> ScriptEngines = new List<ScriptEngine>(); | ||
49 | private Scene m_Scene; | ||
50 | public Scene World | ||
39 | { | 51 | { |
40 | ConfigSource = config; | 52 | get { return m_Scene; } |
41 | InitializeEngine(scene, config, true, GetScriptManager()); | ||
42 | } | 53 | } |
54 | public EventManager m_EventManager; // Handles and queues incoming events from OpenSim | ||
55 | public EventQueueManager m_EventQueueManager; // Executes events, handles script threads | ||
56 | public ScriptManager m_ScriptManager; // Load, unload and execute scripts | ||
57 | public AppDomainManager m_AppDomainManager; // Handles loading/unloading of scripts into AppDomains | ||
58 | public static MaintenanceThread m_MaintenanceThread; // Thread that does different kinds of maintenance, for example refreshing config and killing scripts that has been running too long | ||
59 | |||
60 | public IConfigSource ConfigSource; | ||
61 | public IConfig ScriptConfigSource; | ||
62 | private bool m_enabled = false; | ||
43 | 63 | ||
44 | public override Common.ScriptEngineBase.ScriptManager _GetScriptManager() | 64 | public IConfig Config |
45 | { | 65 | { |
46 | return new ScriptManager(this); | 66 | get { return ScriptConfigSource; } |
47 | } | 67 | } |
48 | 68 | ||
49 | public override string ScriptEngineName | 69 | /// <summary> |
70 | /// How many seconds between re-reading config-file. 0 = never. ScriptEngine will try to adjust to new config changes. | ||
71 | /// </summary> | ||
72 | public int RefreshConfigFileSeconds { | ||
73 | get { return (int)(RefreshConfigFilens / 10000000); } | ||
74 | set { RefreshConfigFilens = value * 10000000; } | ||
75 | } | ||
76 | public long RefreshConfigFilens; | ||
77 | |||
78 | public string ScriptEngineName | ||
50 | { | 79 | { |
51 | get { return "ScriptEngine.DotNetEngine"; } | 80 | get { return "ScriptEngine.DotNetEngine"; } |
52 | } | 81 | } |
82 | |||
83 | public ILog Log | ||
84 | { | ||
85 | get { return m_log; } | ||
86 | } | ||
87 | |||
88 | public ScriptEngine() | ||
89 | { | ||
90 | Common.mySE = this; // For logging, just need any instance, doesn't matter | ||
91 | lock (ScriptEngines) | ||
92 | { | ||
93 | ScriptEngines.Add(this); // Keep a list of ScriptEngines for shared threads to process all instances | ||
94 | } | ||
95 | } | ||
96 | |||
97 | public void Initialise(Scene Sceneworld, IConfigSource config) | ||
98 | { | ||
99 | m_log.Info("[" + ScriptEngineName + "]: ScriptEngine initializing"); | ||
100 | |||
101 | ConfigSource = config; | ||
102 | m_Scene = Sceneworld; | ||
103 | |||
104 | // Make sure we have config | ||
105 | if (ConfigSource.Configs[ScriptEngineName] == null) | ||
106 | ConfigSource.AddConfig(ScriptEngineName); | ||
107 | ScriptConfigSource = ConfigSource.Configs[ScriptEngineName]; | ||
108 | |||
109 | m_enabled = ScriptConfigSource.GetBoolean("Enabled", true); | ||
110 | if (!m_enabled) | ||
111 | return; | ||
112 | |||
113 | //m_log.Info("[" + ScriptEngineName + "]: InitializeEngine"); | ||
114 | |||
115 | // Create all objects we'll be using | ||
116 | m_EventQueueManager = new EventQueueManager(this); | ||
117 | m_EventManager = new EventManager(this, true); | ||
118 | // We need to start it | ||
119 | m_ScriptManager = new ScriptManager(this); | ||
120 | m_ScriptManager.Setup(); | ||
121 | m_AppDomainManager = new AppDomainManager(this); | ||
122 | if (m_MaintenanceThread == null) | ||
123 | m_MaintenanceThread = new MaintenanceThread(); | ||
124 | |||
125 | m_log.Info("[" + ScriptEngineName + "]: Reading configuration from config section \"" + ScriptEngineName + "\""); | ||
126 | ReadConfig(); | ||
127 | |||
128 | m_Scene.StackModuleInterface<IScriptModule>(this); | ||
129 | } | ||
130 | |||
131 | public void PostInitialise() | ||
132 | { | ||
133 | if (!m_enabled) | ||
134 | return; | ||
135 | |||
136 | m_EventManager.HookUpEvents(); | ||
137 | |||
138 | m_ScriptManager.Start(); | ||
139 | } | ||
140 | |||
141 | public void Shutdown() | ||
142 | { | ||
143 | // We are shutting down | ||
144 | lock (ScriptEngines) | ||
145 | { | ||
146 | ScriptEngines.Remove(this); | ||
147 | } | ||
148 | } | ||
149 | |||
150 | public void ReadConfig() | ||
151 | { | ||
152 | #if DEBUG | ||
153 | //m_log.Debug("[" + ScriptEngineName + "]: Refreshing configuration for all modules"); | ||
154 | #endif | ||
155 | RefreshConfigFileSeconds = ScriptConfigSource.GetInt("RefreshConfig", 30); | ||
156 | |||
157 | |||
158 | // Create a new object (probably not necessary?) | ||
159 | // ScriptConfigSource = ConfigSource.Configs[ScriptEngineName]; | ||
160 | |||
161 | if (m_EventQueueManager != null) m_EventQueueManager.ReadConfig(); | ||
162 | if (m_EventManager != null) m_EventManager.ReadConfig(); | ||
163 | if (m_ScriptManager != null) m_ScriptManager.ReadConfig(); | ||
164 | if (m_AppDomainManager != null) m_AppDomainManager.ReadConfig(); | ||
165 | if (m_MaintenanceThread != null) m_MaintenanceThread.ReadConfig(); | ||
166 | } | ||
167 | |||
168 | #region IRegionModule | ||
169 | |||
170 | public void Close() | ||
171 | { | ||
172 | } | ||
173 | |||
174 | public string Name | ||
175 | { | ||
176 | get { return "Common." + ScriptEngineName; } | ||
177 | } | ||
178 | |||
179 | public bool IsSharedModule | ||
180 | { | ||
181 | get { return false; } | ||
182 | } | ||
183 | |||
184 | public bool PostObjectEvent(uint localID, EventParams p) | ||
185 | { | ||
186 | return m_EventQueueManager.AddToObjectQueue(localID, p.EventName, p.DetectParams, p.Params); | ||
187 | } | ||
188 | |||
189 | public bool PostScriptEvent(UUID itemID, EventParams p) | ||
190 | { | ||
191 | uint localID = m_ScriptManager.GetLocalID(itemID); | ||
192 | return m_EventQueueManager.AddToScriptQueue(localID, itemID, p.EventName, p.DetectParams, p.Params); | ||
193 | } | ||
194 | |||
195 | public DetectParams GetDetectParams(UUID itemID, int number) | ||
196 | { | ||
197 | uint localID = m_ScriptManager.GetLocalID(itemID); | ||
198 | if (localID == 0) | ||
199 | return null; | ||
200 | |||
201 | IScript Script = m_ScriptManager.GetScript(localID, itemID); | ||
202 | |||
203 | if (Script == null) | ||
204 | return null; | ||
205 | |||
206 | DetectParams[] det = m_ScriptManager.GetDetectParams(Script); | ||
207 | |||
208 | if (number < 0 || number >= det.Length) | ||
209 | return null; | ||
210 | |||
211 | return det[number]; | ||
212 | } | ||
213 | |||
214 | public int GetStartParameter(UUID itemID) | ||
215 | { | ||
216 | return 0; | ||
217 | } | ||
218 | #endregion | ||
219 | |||
220 | public void SetState(UUID itemID, string state) | ||
221 | { | ||
222 | uint localID = m_ScriptManager.GetLocalID(itemID); | ||
223 | if (localID == 0) | ||
224 | return; | ||
225 | |||
226 | IScript Script = m_ScriptManager.GetScript(localID, itemID); | ||
227 | |||
228 | if (Script == null) | ||
229 | return; | ||
230 | |||
231 | string currentState = Script.State; | ||
232 | |||
233 | if (currentState != state) | ||
234 | { | ||
235 | try | ||
236 | { | ||
237 | m_EventManager.state_exit(localID); | ||
238 | |||
239 | } | ||
240 | catch (AppDomainUnloadedException) | ||
241 | { | ||
242 | Console.WriteLine("[SCRIPT]: state change called when script was unloaded. Nothing to worry about, but noting the occurance"); | ||
243 | } | ||
244 | |||
245 | Script.State = state; | ||
246 | |||
247 | try | ||
248 | { | ||
249 | int eventFlags = m_ScriptManager.GetStateEventFlags(localID, itemID); | ||
250 | SceneObjectPart part = m_Scene.GetSceneObjectPart(itemID); | ||
251 | if (part != null) | ||
252 | part.SetScriptEvents(itemID, eventFlags); | ||
253 | m_EventManager.state_entry(localID); | ||
254 | } | ||
255 | catch (AppDomainUnloadedException) | ||
256 | { | ||
257 | Console.WriteLine("[SCRIPT]: state change called when script was unloaded. Nothing to worry about, but noting the occurance"); | ||
258 | } | ||
259 | } | ||
260 | } | ||
261 | |||
262 | public bool GetScriptState(UUID itemID) | ||
263 | { | ||
264 | uint localID = m_ScriptManager.GetLocalID(itemID); | ||
265 | if (localID == 0) | ||
266 | return false; | ||
267 | |||
268 | IScript script = m_ScriptManager.GetScript(localID, itemID); | ||
269 | if (script == null) | ||
270 | return false; | ||
271 | |||
272 | return script.Exec.Running?true:false; | ||
273 | } | ||
274 | |||
275 | public void SetScriptState(UUID itemID, bool state) | ||
276 | { | ||
277 | uint localID = m_ScriptManager.GetLocalID(itemID); | ||
278 | if (localID == 0) | ||
279 | return; | ||
280 | |||
281 | IScript script = m_ScriptManager.GetScript(localID, itemID); | ||
282 | if (script == null) | ||
283 | return; | ||
284 | |||
285 | script.Exec.Running = state; | ||
286 | } | ||
287 | |||
288 | public void ApiResetScript(UUID itemID) | ||
289 | { | ||
290 | uint localID = m_ScriptManager.GetLocalID(itemID); | ||
291 | if (localID == 0) | ||
292 | return; | ||
293 | |||
294 | m_ScriptManager.ResetScript(localID, itemID); | ||
295 | } | ||
296 | |||
297 | public void ResetScript(UUID itemID) | ||
298 | { | ||
299 | uint localID = m_ScriptManager.GetLocalID(itemID); | ||
300 | if (localID == 0) | ||
301 | return; | ||
302 | |||
303 | m_ScriptManager.ResetScript(localID, itemID); | ||
304 | } | ||
53 | } | 305 | } |
54 | } | 306 | } |