diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/ScriptEngine/Components/DotNetEngine/Scheduler/ScriptManager_ScriptLoadUnload.cs | 259 |
1 files changed, 259 insertions, 0 deletions
diff --git a/OpenSim/ScriptEngine/Components/DotNetEngine/Scheduler/ScriptManager_ScriptLoadUnload.cs b/OpenSim/ScriptEngine/Components/DotNetEngine/Scheduler/ScriptManager_ScriptLoadUnload.cs new file mode 100644 index 0000000..7d362f9 --- /dev/null +++ b/OpenSim/ScriptEngine/Components/DotNetEngine/Scheduler/ScriptManager_ScriptLoadUnload.cs | |||
@@ -0,0 +1,259 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Globalization; | ||
4 | using System.Reflection; | ||
5 | using System.Text; | ||
6 | using System.Threading; | ||
7 | using log4net; | ||
8 | using OpenMetaverse; | ||
9 | using OpenSim.Framework; | ||
10 | using OpenSim.Region.Environment.Scenes; | ||
11 | using OpenSim.Region.ScriptEngine.Interfaces; | ||
12 | using OpenSim.Region.ScriptEngine.Shared.ScriptBase; | ||
13 | using OpenSim.ScriptEngine.Shared; | ||
14 | |||
15 | namespace OpenSim.ScriptEngine.Components.DotNetEngine.Scheduler | ||
16 | { | ||
17 | public partial class ScriptManager | ||
18 | { | ||
19 | private Queue<LoadUnloadStructure> LUQueue = new Queue<LoadUnloadStructure>(); | ||
20 | private int LoadUnloadMaxQueueSize = 500; | ||
21 | private Object scriptLock = new Object(); | ||
22 | //private Dictionary<InstanceData, DetectParams[]> detparms = new Dictionary<InstanceData, DetectParams[]>(); | ||
23 | |||
24 | // Load/Unload structure | ||
25 | |||
26 | |||
27 | public void AddScript(ScriptStructure script) | ||
28 | { | ||
29 | lock (LUQueue) | ||
30 | { | ||
31 | if ((LUQueue.Count >= LoadUnloadMaxQueueSize)) | ||
32 | { | ||
33 | m_log.ErrorFormat("[{0}] ERROR: Load queue count is at {1} of max {2}. Ignoring load request for script LocalID: {3}, ItemID: {4}.", | ||
34 | Name, LUQueue.Count, LoadUnloadMaxQueueSize, script.LocalID, script.ItemID); | ||
35 | return; | ||
36 | } | ||
37 | |||
38 | LoadUnloadStructure ls = new LoadUnloadStructure(); | ||
39 | ls.Script = script; | ||
40 | ls.Action = LoadUnloadStructure.LUType.Load; | ||
41 | LUQueue.Enqueue(ls); | ||
42 | } | ||
43 | |||
44 | } | ||
45 | public void RemoveScript(uint localID, UUID itemID) | ||
46 | { | ||
47 | LoadUnloadStructure ls = new LoadUnloadStructure(); | ||
48 | |||
49 | // See if we can find script | ||
50 | if (!TryGetScript(localID, itemID, ref ls.Script)) | ||
51 | { | ||
52 | // Set manually | ||
53 | ls.Script.LocalID = localID; | ||
54 | ls.Script.ItemID = itemID; | ||
55 | } | ||
56 | ls.Script.StartParam = 0; | ||
57 | |||
58 | ls.Action = LoadUnloadStructure.LUType.Unload; | ||
59 | ls.PostOnRez = false; | ||
60 | |||
61 | lock (LUQueue) | ||
62 | { | ||
63 | LUQueue.Enqueue(ls); | ||
64 | } | ||
65 | } | ||
66 | |||
67 | internal bool DoScriptLoadUnload() | ||
68 | { | ||
69 | bool ret = false; | ||
70 | // if (!m_started) | ||
71 | // return; | ||
72 | |||
73 | lock (LUQueue) | ||
74 | { | ||
75 | if (LUQueue.Count > 0) | ||
76 | { | ||
77 | LoadUnloadStructure item = LUQueue.Dequeue(); | ||
78 | ret = true; | ||
79 | |||
80 | if (item.Action == LoadUnloadStructure.LUType.Unload) | ||
81 | { | ||
82 | m_log.DebugFormat("[{0}] Unloading script", Name); | ||
83 | _StopScript(item.Script.LocalID, item.Script.ItemID); | ||
84 | RemoveScript(item.Script.LocalID, item.Script.ItemID); | ||
85 | } | ||
86 | else if (item.Action == LoadUnloadStructure.LUType.Load) | ||
87 | { | ||
88 | m_log.DebugFormat("[{0}] Loading script", Name); | ||
89 | _StartScript(item); | ||
90 | } | ||
91 | } | ||
92 | } | ||
93 | return ret; | ||
94 | } | ||
95 | |||
96 | //public void _StartScript(uint localID, UUID itemID, string Script, int startParam, bool postOnRez) | ||
97 | private void _StartScript(LoadUnloadStructure ScriptObject) | ||
98 | { | ||
99 | m_log.DebugFormat( | ||
100 | "[{0}]: ScriptManager StartScript: localID: {1}, itemID: {2}", | ||
101 | Name, ScriptObject.Script.LocalID, ScriptObject.Script.ItemID); | ||
102 | |||
103 | // We will initialize and start the script. | ||
104 | // It will be up to the script itself to hook up the correct events. | ||
105 | |||
106 | SceneObjectPart m_host = ScriptObject.Script.RegionInfo.Scene.GetSceneObjectPart(ScriptObject.Script.LocalID); | ||
107 | |||
108 | if (null == m_host) | ||
109 | { | ||
110 | m_log.ErrorFormat( | ||
111 | "[{0}]: Could not find scene object part corresponding " + | ||
112 | "to localID {1} to start script", | ||
113 | Name, ScriptObject.Script.LocalID); | ||
114 | |||
115 | return; | ||
116 | } | ||
117 | |||
118 | UUID assetID = UUID.Zero; | ||
119 | TaskInventoryItem taskInventoryItem = new TaskInventoryItem(); | ||
120 | if (m_host.TaskInventory.TryGetValue(ScriptObject.Script.ItemID, out taskInventoryItem)) | ||
121 | assetID = taskInventoryItem.AssetID; | ||
122 | |||
123 | ScenePresence presence = | ||
124 | ScriptObject.Script.RegionInfo.Scene.GetScenePresence(taskInventoryItem.OwnerID); | ||
125 | |||
126 | CultureInfo USCulture = new CultureInfo("en-US"); | ||
127 | Thread.CurrentThread.CurrentCulture = USCulture; | ||
128 | |||
129 | try | ||
130 | { | ||
131 | // | ||
132 | // Compile script to an assembly | ||
133 | // | ||
134 | //TODO: DEBUG | ||
135 | BaseClassFactory.MakeBaseClass(ScriptObject.Script); | ||
136 | |||
137 | m_log.DebugFormat("[{0}] Compiling script {1}", Name, ScriptObject.Script.Name); | ||
138 | |||
139 | string fileName = ""; | ||
140 | try | ||
141 | { | ||
142 | IScriptCompiler compiler = | ||
143 | ScriptObject.Script.RegionInfo.FindCompiler(ScriptObject.Script.ScriptMetaData); | ||
144 | RegionInfoStructure currentRegionInfo = ScriptObject.Script.RegionInfo; | ||
145 | fileName = compiler.Compile(ScriptObject.Script.ScriptMetaData, | ||
146 | ref ScriptObject.Script.Source); | ||
147 | ScriptObject.Script.AssemblyFileName = fileName; | ||
148 | } | ||
149 | catch (Exception e) | ||
150 | { | ||
151 | m_log.ErrorFormat("[{0}] Internal error while compiling \"{1}\": {2}", Name, ScriptObject.Script.Name, e.ToString()); | ||
152 | } | ||
153 | m_log.DebugFormat("[{0}] Compiled \"{1}\" to assembly: \"{2}\".", Name, ScriptObject.Script.Name, fileName); | ||
154 | |||
155 | // Add it to our script memstruct | ||
156 | MemAddScript(ScriptObject.Script); | ||
157 | |||
158 | ScriptAssemblies.IScript CompiledScript; | ||
159 | CompiledScript = CurrentRegion.ScriptLoader.LoadScript(ScriptObject.Script); | ||
160 | ScriptObject.Script.State = "default"; | ||
161 | ScriptObject.Script.ScriptObject = CompiledScript; | ||
162 | ScriptObject.Script.Disabled = false; | ||
163 | ScriptObject.Script.Running = true; | ||
164 | //id.LineMap = LSLCompiler.LineMap(); | ||
165 | //id.Script = CompiledScript; | ||
166 | //id.Source = item.Script.Script; | ||
167 | //item.StartParam = startParam; | ||
168 | |||
169 | |||
170 | |||
171 | // TODO: Fire the first start-event | ||
172 | //int eventFlags = | ||
173 | // m_scriptEngine.m_ScriptManager.GetStateEventFlags( | ||
174 | // localID, itemID); | ||
175 | |||
176 | //m_host.SetScriptEvents(itemID, eventFlags); | ||
177 | ScriptObject.Script.RegionInfo.Executors_Execute(ScriptObject.Script, | ||
178 | new EventParams(ScriptObject.Script.LocalID, ScriptObject.Script.ItemID, "state_entry", new object[] { }, new Region.ScriptEngine.Shared.DetectParams[0]) | ||
179 | ); | ||
180 | |||
181 | if (ScriptObject.PostOnRez) | ||
182 | { | ||
183 | ScriptObject.Script.RegionInfo.Executors_Execute(ScriptObject.Script, | ||
184 | new EventParams(ScriptObject.Script.LocalID, "on_rez", new object[] | ||
185 | {new Region.ScriptEngine.Shared.LSL_Types.LSLInteger(ScriptObject.StartParam) | ||
186 | }, new Region.ScriptEngine.Shared.DetectParams[0])); | ||
187 | } | ||
188 | } | ||
189 | catch (Exception e) // LEGIT: User Scripting | ||
190 | { | ||
191 | if (presence != null && (!ScriptObject.PostOnRez)) | ||
192 | presence.ControllingClient.SendAgentAlertMessage( | ||
193 | "Script saved with errors, check debug window!", | ||
194 | false); | ||
195 | try | ||
196 | { | ||
197 | // DISPLAY ERROR INWORLD | ||
198 | string text = "Error compiling script:\n" + | ||
199 | e.Message.ToString(); | ||
200 | if (text.Length > 1100) | ||
201 | text = text.Substring(0, 1099); | ||
202 | |||
203 | ScriptObject.Script.RegionInfo.Scene.SimChat(Utils.StringToBytes(text), | ||
204 | ChatTypeEnum.DebugChannel, 2147483647, | ||
205 | m_host.AbsolutePosition, m_host.Name, m_host.UUID, | ||
206 | false); | ||
207 | } | ||
208 | catch (Exception e2) // LEGIT: User Scripting | ||
209 | { | ||
210 | m_log.Error("[" + | ||
211 | Name + | ||
212 | "]: Error displaying error in-world: " + | ||
213 | e2.ToString()); | ||
214 | m_log.Error("[" + | ||
215 | Name + "]: " + | ||
216 | "Errormessage: Error compiling script:\r\n" + | ||
217 | e2.Message.ToString()); | ||
218 | } | ||
219 | } | ||
220 | } | ||
221 | |||
222 | |||
223 | |||
224 | public void _StopScript(uint localID, UUID itemID) | ||
225 | { | ||
226 | ScriptStructure ss = new ScriptStructure(); | ||
227 | if (!TryGetScript(localID, itemID, ref ss)) | ||
228 | return; | ||
229 | |||
230 | // Stop long command on script | ||
231 | //AsyncCommandManager.RemoveScript(ss); | ||
232 | |||
233 | try | ||
234 | { | ||
235 | // Get AppDomain | ||
236 | // Tell script not to accept new requests | ||
237 | ss.Running = false; | ||
238 | ss.Disabled = true; | ||
239 | AppDomain ad = ss.AppDomain; | ||
240 | |||
241 | // Remove from internal structure | ||
242 | MemRemoveScript(localID, itemID); | ||
243 | |||
244 | // TODO: Tell AppDomain that we have stopped script | ||
245 | |||
246 | } | ||
247 | catch (Exception e) // LEGIT: User Scripting | ||
248 | { | ||
249 | m_log.Error("[" + | ||
250 | Name + | ||
251 | "]: Exception stopping script localID: " + | ||
252 | localID + " LLUID: " + itemID.ToString() + | ||
253 | ": " + e.ToString()); | ||
254 | } | ||
255 | } | ||
256 | |||
257 | |||
258 | } | ||
259 | } | ||