diff options
author | UbitUmarov | 2018-02-23 14:52:34 +0000 |
---|---|---|
committer | UbitUmarov | 2018-02-23 14:52:34 +0000 |
commit | 2129d941acbc5f83aca4dc4c30a62d3226888136 (patch) | |
tree | e12f2391978c923ef780f558ee5a32d857ee9124 /OpenSim/Region/ScriptEngine/YEngine/XMRInstCtor.cs | |
parent | Merge branch 'master' into httptests (diff) | |
download | opensim-SC-2129d941acbc5f83aca4dc4c30a62d3226888136.zip opensim-SC-2129d941acbc5f83aca4dc4c30a62d3226888136.tar.gz opensim-SC-2129d941acbc5f83aca4dc4c30a62d3226888136.tar.bz2 opensim-SC-2129d941acbc5f83aca4dc4c30a62d3226888136.tar.xz |
rename XMREngine as Yengine (still not all done), big mess source formating changes, move state files to proper folder, fix a source file locking on errors, more changes for cross platform including from Mike,... yes yes i know a messy commit
Diffstat (limited to 'OpenSim/Region/ScriptEngine/YEngine/XMRInstCtor.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/YEngine/XMRInstCtor.cs | 724 |
1 files changed, 724 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/YEngine/XMRInstCtor.cs b/OpenSim/Region/ScriptEngine/YEngine/XMRInstCtor.cs new file mode 100644 index 0000000..68ec322 --- /dev/null +++ b/OpenSim/Region/ScriptEngine/YEngine/XMRInstCtor.cs | |||
@@ -0,0 +1,724 @@ | |||
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.Threading; | ||
30 | using System.Reflection; | ||
31 | using System.Collections; | ||
32 | using System.Collections.Generic; | ||
33 | using System.Runtime.Remoting.Lifetime; | ||
34 | using System.Security.Policy; | ||
35 | using System.IO; | ||
36 | using System.Xml; | ||
37 | using System.Text; | ||
38 | using OpenMetaverse; | ||
39 | using OpenSim.Framework; | ||
40 | using OpenSim.Region.ScriptEngine.Interfaces; | ||
41 | using OpenSim.Region.ScriptEngine.Shared; | ||
42 | using OpenSim.Region.ScriptEngine.Shared.Api; | ||
43 | using OpenSim.Region.ScriptEngine.Shared.ScriptBase; | ||
44 | using OpenSim.Region.ScriptEngine.Yengine; | ||
45 | using OpenSim.Region.Framework.Scenes; | ||
46 | using log4net; | ||
47 | |||
48 | using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat; | ||
49 | using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger; | ||
50 | using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString; | ||
51 | using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list; | ||
52 | using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion; | ||
53 | using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString; | ||
54 | using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3; | ||
55 | |||
56 | namespace OpenSim.Region.ScriptEngine.Yengine | ||
57 | { | ||
58 | public partial class XMRInstance | ||
59 | { | ||
60 | /****************************************************************************\ | ||
61 | * The only method of interest to outside this module is the Initializer. * | ||
62 | * * | ||
63 | * The rest of this module contains support routines for the Initializer. * | ||
64 | \****************************************************************************/ | ||
65 | |||
66 | /** | ||
67 | * @brief Initializer, loads script in memory and all ready for running. | ||
68 | * @param engine = YEngine instance this is part of | ||
69 | * @param scriptBasePath = directory name where files are | ||
70 | * @param stackSize = number of bytes to allocate for stacks | ||
71 | * @param errors = return compiler errors in this array | ||
72 | * @param forceRecomp = force recompile | ||
73 | * Throws exception if any error, so it was successful if it returns. | ||
74 | */ | ||
75 | public void Initialize(Yengine engine, string scriptBasePath, | ||
76 | int stackSize, int heapSize, ArrayList errors) | ||
77 | { | ||
78 | if(stackSize < 16384) | ||
79 | stackSize = 16384; | ||
80 | if(heapSize < 16384) | ||
81 | heapSize = 16384; | ||
82 | |||
83 | // Save all call parameters in instance vars for easy access. | ||
84 | m_Engine = engine; | ||
85 | m_ScriptBasePath = scriptBasePath; | ||
86 | m_StackSize = stackSize; | ||
87 | m_StackLeft = stackSize; | ||
88 | m_HeapSize = heapSize; | ||
89 | m_CompilerErrors = errors; | ||
90 | m_StateFileName = GetStateFileName(scriptBasePath, m_ItemID); | ||
91 | |||
92 | // Not in any XMRInstQueue. | ||
93 | m_NextInst = this; | ||
94 | m_PrevInst = this; | ||
95 | |||
96 | // Set up list of API calls it has available. | ||
97 | // This also gets the API modules ready to accept setup data, such as | ||
98 | // active listeners being restored. | ||
99 | IScriptApi scriptApi; | ||
100 | ApiManager am = new ApiManager(); | ||
101 | foreach(string api in am.GetApis()) | ||
102 | { | ||
103 | // Instantiate the API for this script instance. | ||
104 | if(api != "LSL") | ||
105 | scriptApi = am.CreateApi(api); | ||
106 | else | ||
107 | scriptApi = m_XMRLSLApi = new XMRLSL_Api(); | ||
108 | |||
109 | // Connect it up to the instance. | ||
110 | InitScriptApi(engine, api, scriptApi); | ||
111 | } | ||
112 | |||
113 | m_XMRLSLApi.InitXMRLSLApi(this); | ||
114 | |||
115 | // Get object loaded, compiling script and reading .state file as | ||
116 | // necessary to restore the state. | ||
117 | suspendOnCheckRunHold = true; | ||
118 | InstantiateScript(); | ||
119 | m_SourceCode = null; | ||
120 | if(m_ObjCode == null) | ||
121 | throw new ArgumentNullException("m_ObjCode"); | ||
122 | if(m_ObjCode.scriptEventHandlerTable == null) | ||
123 | throw new ArgumentNullException("m_ObjCode.scriptEventHandlerTable"); | ||
124 | |||
125 | suspendOnCheckRunHold = false; | ||
126 | suspendOnCheckRunTemp = false; | ||
127 | |||
128 | // Declare which events the script's current state can handle. | ||
129 | int eventMask = GetStateEventFlags(stateCode); | ||
130 | m_Part.SetScriptEvents(m_ItemID, eventMask); | ||
131 | } | ||
132 | |||
133 | private void InitScriptApi(Yengine engine, string api, IScriptApi scriptApi) | ||
134 | { | ||
135 | // Set up m_ApiManager_<APINAME> = instance pointer. | ||
136 | engine.m_XMRInstanceApiCtxFieldInfos[api].SetValue(this, scriptApi); | ||
137 | |||
138 | // Initialize the API instance. | ||
139 | scriptApi.Initialize(m_Engine, m_Part, m_Item); | ||
140 | this.InitApi(api, scriptApi); | ||
141 | } | ||
142 | |||
143 | |||
144 | /* | ||
145 | * Get script object code loaded in memory and all ready to run, | ||
146 | * ready to resume it from where the .state file says it was last | ||
147 | */ | ||
148 | private void InstantiateScript() | ||
149 | { | ||
150 | bool compiledIt = false; | ||
151 | ScriptObjCode objCode; | ||
152 | |||
153 | // If source code string is empty, use the asset ID as the object file name. | ||
154 | // Allow lines of // comments at the beginning (for such as engine selection). | ||
155 | int i, j, len; | ||
156 | if(m_SourceCode == null) | ||
157 | m_SourceCode = String.Empty; | ||
158 | for(len = m_SourceCode.Length; len > 0; --len) | ||
159 | { | ||
160 | if(m_SourceCode[len - 1] > ' ') | ||
161 | break; | ||
162 | } | ||
163 | for(i = 0; i < len; i++) | ||
164 | { | ||
165 | char c = m_SourceCode[i]; | ||
166 | if(c <= ' ') | ||
167 | continue; | ||
168 | if(c != '/') | ||
169 | break; | ||
170 | if((i + 1 >= len) || (m_SourceCode[i + 1] != '/')) | ||
171 | break; | ||
172 | i = m_SourceCode.IndexOf('\n', i); | ||
173 | if(i < 0) | ||
174 | i = len - 1; | ||
175 | } | ||
176 | if((i >= len) || !m_Engine.m_UseSourceHashCode) | ||
177 | { | ||
178 | // Source consists of nothing but // comments and whitespace, | ||
179 | // or we are being forced to use the asset-id as the key, to | ||
180 | // open an already existing object code file. | ||
181 | m_ScriptObjCodeKey = m_Item.AssetID.ToString(); | ||
182 | if(i >= len) | ||
183 | m_SourceCode = ""; | ||
184 | } | ||
185 | else | ||
186 | { | ||
187 | // Make up dictionary key for the object code. | ||
188 | // Use the same object code for identical source code | ||
189 | // regardless of asset ID, so we don't care if they | ||
190 | // copy scripts or not. | ||
191 | byte[] scbytes = System.Text.Encoding.UTF8.GetBytes(m_SourceCode); | ||
192 | StringBuilder sb = new StringBuilder((256 + 5) / 6); | ||
193 | ByteArrayToSixbitStr(sb, System.Security.Cryptography.SHA256.Create().ComputeHash(scbytes)); | ||
194 | m_ScriptObjCodeKey = sb.ToString(); | ||
195 | |||
196 | // But source code can be just a sixbit string itself | ||
197 | // that identifies an already existing object code file. | ||
198 | if(len - i == m_ScriptObjCodeKey.Length) | ||
199 | { | ||
200 | for(j = len; --j >= i;) | ||
201 | { | ||
202 | if(sixbit.IndexOf(m_SourceCode[j]) < 0) | ||
203 | break; | ||
204 | } | ||
205 | if(j < i) | ||
206 | { | ||
207 | m_ScriptObjCodeKey = m_SourceCode.Substring(i, len - i); | ||
208 | m_SourceCode = ""; | ||
209 | } | ||
210 | } | ||
211 | } | ||
212 | |||
213 | // There may already be an ScriptObjCode struct in memory that | ||
214 | // we can use. If not, try to compile it. | ||
215 | lock(m_CompileLock) | ||
216 | { | ||
217 | if(!m_CompiledScriptObjCode.TryGetValue(m_ScriptObjCodeKey, out objCode) || m_ForceRecomp) | ||
218 | { | ||
219 | objCode = TryToCompile(); | ||
220 | compiledIt = true; | ||
221 | } | ||
222 | |||
223 | // Loaded successfully, increment reference count. | ||
224 | // If we just compiled it though, reset count to 0 first as | ||
225 | // this is the one-and-only existance of this objCode struct, | ||
226 | // and we want any old ones for this source code to be garbage | ||
227 | // collected. | ||
228 | |||
229 | if(compiledIt) | ||
230 | { | ||
231 | m_CompiledScriptObjCode[m_ScriptObjCodeKey] = objCode; | ||
232 | objCode.refCount = 0; | ||
233 | } | ||
234 | objCode.refCount++; | ||
235 | |||
236 | // Now set up to decrement ref count on dispose. | ||
237 | m_ObjCode = objCode; | ||
238 | } | ||
239 | |||
240 | try | ||
241 | { | ||
242 | |||
243 | // Fill in script instance from object code | ||
244 | // Script instance is put in a "never-ever-has-run-before" state. | ||
245 | LoadObjCode(); | ||
246 | |||
247 | // Fill in script intial state | ||
248 | // - either as loaded from a .state file | ||
249 | // - or initial default state_entry() event | ||
250 | LoadInitialState(); | ||
251 | } | ||
252 | catch | ||
253 | { | ||
254 | |||
255 | // If any error loading, decrement object code reference count. | ||
256 | DecObjCodeRefCount(); | ||
257 | throw; | ||
258 | } | ||
259 | } | ||
260 | |||
261 | private const string sixbit = "0123456789_abcdefghijklmnopqrstuvwxyz@ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
262 | private static void ByteArrayToSixbitStr(StringBuilder sb, byte[] bytes) | ||
263 | { | ||
264 | int bit = 0; | ||
265 | int val = 0; | ||
266 | foreach(byte b in bytes) | ||
267 | { | ||
268 | val |= (int)((uint)b << bit); | ||
269 | bit += 8; | ||
270 | while(bit >= 6) | ||
271 | { | ||
272 | sb.Append(sixbit[val & 63]); | ||
273 | val >>= 6; | ||
274 | bit -= 6; | ||
275 | } | ||
276 | } | ||
277 | if(bit > 0) | ||
278 | sb.Append(sixbit[val & 63]); | ||
279 | } | ||
280 | |||
281 | // Try to create object code from source code | ||
282 | // If error, just throw exception | ||
283 | private ScriptObjCode TryToCompile() | ||
284 | { | ||
285 | m_CompilerErrors.Clear(); | ||
286 | |||
287 | // If object file exists, create ScriptObjCode directly from that. | ||
288 | // Otherwise, compile the source to create object file then create | ||
289 | // ScriptObjCode from that. | ||
290 | string assetID = m_Item.AssetID.ToString(); | ||
291 | m_CameFrom = "asset://" + assetID; | ||
292 | ScriptObjCode objCode = Compile(); | ||
293 | if(m_CompilerErrors.Count != 0) | ||
294 | throw new Exception("compilation errors"); | ||
295 | |||
296 | if(objCode == null) | ||
297 | throw new Exception("compilation failed"); | ||
298 | |||
299 | return objCode; | ||
300 | } | ||
301 | |||
302 | /* | ||
303 | * Retrieve source from asset server. | ||
304 | */ | ||
305 | private string FetchSource(string cameFrom) | ||
306 | { | ||
307 | m_log.Debug("[YEngine]: fetching source " + cameFrom); | ||
308 | if(!cameFrom.StartsWith("asset://")) | ||
309 | throw new Exception("unable to retrieve source from " + cameFrom); | ||
310 | |||
311 | string assetID = cameFrom.Substring(8); | ||
312 | AssetBase asset = m_Engine.World.AssetService.Get(assetID); | ||
313 | if(asset == null) | ||
314 | throw new Exception("source not found " + cameFrom); | ||
315 | |||
316 | string source = Encoding.UTF8.GetString(asset.Data); | ||
317 | if(EmptySource(source)) | ||
318 | throw new Exception("fetched source empty " + cameFrom); | ||
319 | |||
320 | return source; | ||
321 | } | ||
322 | |||
323 | /* | ||
324 | * Fill in script object initial contents. | ||
325 | * Set the initial state to "default". | ||
326 | */ | ||
327 | private void LoadObjCode() | ||
328 | { | ||
329 | // Script must leave this much stack remaining on calls to CheckRun(). | ||
330 | this.stackLimit = m_StackSize / 2; | ||
331 | |||
332 | // This is how many total heap bytes script is allowed to use. | ||
333 | this.heapLimit = m_HeapSize; | ||
334 | |||
335 | // Allocate global variable arrays. | ||
336 | this.glblVars.AllocVarArrays(m_ObjCode.glblSizes); | ||
337 | |||
338 | // Script can handle these event codes. | ||
339 | m_HaveEventHandlers = new bool[m_ObjCode.scriptEventHandlerTable.GetLength(1)]; | ||
340 | for(int i = m_ObjCode.scriptEventHandlerTable.GetLength(0); --i >= 0;) | ||
341 | { | ||
342 | for(int j = m_ObjCode.scriptEventHandlerTable.GetLength(1); --j >= 0;) | ||
343 | { | ||
344 | if(m_ObjCode.scriptEventHandlerTable[i, j] != null) | ||
345 | { | ||
346 | m_HaveEventHandlers[j] = true; | ||
347 | } | ||
348 | } | ||
349 | } | ||
350 | } | ||
351 | |||
352 | /* | ||
353 | * LoadInitialState() | ||
354 | * if no state XML file exists for the asset, | ||
355 | * post initial default state events | ||
356 | * else | ||
357 | * try to restore from .state file | ||
358 | * If any error, throw exception | ||
359 | */ | ||
360 | private void LoadInitialState() | ||
361 | { | ||
362 | // If no .state file exists, start from default state | ||
363 | // Otherwise, read initial state from the .state file | ||
364 | |||
365 | if(!File.Exists(m_StateFileName)) | ||
366 | { | ||
367 | m_Running = true; // event processing is enabled | ||
368 | eventCode = ScriptEventCode.None; // not processing any event | ||
369 | |||
370 | // default state_entry() must initialize global variables | ||
371 | doGblInit = true; | ||
372 | stateCode = 0; | ||
373 | |||
374 | PostEvent(new EventParams("state_entry", | ||
375 | zeroObjectArray, | ||
376 | zeroDetectParams)); | ||
377 | } | ||
378 | else | ||
379 | { | ||
380 | FileStream fs = File.Open(m_StateFileName, | ||
381 | FileMode.Open, | ||
382 | FileAccess.Read); | ||
383 | StreamReader ss = new StreamReader(fs); | ||
384 | string xml = ss.ReadToEnd(); | ||
385 | ss.Close(); | ||
386 | fs.Close(); | ||
387 | |||
388 | XmlDocument doc = new XmlDocument(); | ||
389 | doc.LoadXml(xml); | ||
390 | LoadScriptState(doc); | ||
391 | } | ||
392 | |||
393 | /* | ||
394 | * Post event(s) saying what caused the script to start. | ||
395 | */ | ||
396 | if(m_PostOnRez) | ||
397 | { | ||
398 | PostEvent(new EventParams("on_rez", | ||
399 | new Object[] { m_StartParam }, | ||
400 | zeroDetectParams)); | ||
401 | } | ||
402 | |||
403 | switch(m_StateSource) | ||
404 | { | ||
405 | case StateSource.AttachedRez: | ||
406 | // PostEvent(new EventParams("attach", | ||
407 | // new object[] { m_Part.ParentGroup.AttachedAvatar.ToString() }, | ||
408 | // zeroDetectParams)); | ||
409 | break; | ||
410 | |||
411 | case StateSource.PrimCrossing: | ||
412 | PostEvent(new EventParams("changed", | ||
413 | sbcCR, | ||
414 | zeroDetectParams)); | ||
415 | break; | ||
416 | |||
417 | |||
418 | case StateSource.Teleporting: | ||
419 | PostEvent(new EventParams("changed", | ||
420 | sbcCR, | ||
421 | zeroDetectParams)); | ||
422 | PostEvent(new EventParams("changed", | ||
423 | sbcCT, | ||
424 | zeroDetectParams)); | ||
425 | break; | ||
426 | |||
427 | case StateSource.RegionStart: | ||
428 | PostEvent(new EventParams("changed", | ||
429 | sbcCRS, | ||
430 | zeroDetectParams)); | ||
431 | break; | ||
432 | } | ||
433 | } | ||
434 | |||
435 | private static Object[] sbcCRS = new Object[] { ScriptBaseClass.CHANGED_REGION_START }; | ||
436 | private static Object[] sbcCR = new Object[] { ScriptBaseClass.CHANGED_REGION }; | ||
437 | private static Object[] sbcCT = new Object[] { ScriptBaseClass.CHANGED_TELEPORT }; | ||
438 | |||
439 | /** | ||
440 | * @brief Save compilation error messages for later retrieval | ||
441 | * via GetScriptErrors(). | ||
442 | */ | ||
443 | private void ErrorHandler(Token token, string message) | ||
444 | { | ||
445 | if(token != null) | ||
446 | { | ||
447 | string srcloc = token.SrcLoc; | ||
448 | if(srcloc.StartsWith(m_CameFrom)) | ||
449 | srcloc = srcloc.Substring(m_CameFrom.Length); | ||
450 | |||
451 | m_CompilerErrors.Add(srcloc + " Error: " + message); | ||
452 | } | ||
453 | else if(message != null) | ||
454 | m_CompilerErrors.Add("(0,0) Error: " + message); | ||
455 | else | ||
456 | m_CompilerErrors.Add("(0,0) Error compiling, see exception in log"); | ||
457 | } | ||
458 | |||
459 | /** | ||
460 | * @brief Load script state from the given XML doc into the script memory | ||
461 | * <ScriptState Engine="YEngine" Asset=...> | ||
462 | * <Running>...</Running> | ||
463 | * <DoGblInit>...</DoGblInit> | ||
464 | * <Permissions granted=... mask=... /> | ||
465 | * RestoreDetectParams() | ||
466 | * <Plugins> | ||
467 | * ExtractXMLObjectArray("plugin") | ||
468 | * </Plugins> | ||
469 | * <Snapshot> | ||
470 | * MigrateInEventHandler() | ||
471 | * </Snapshot> | ||
472 | * </ScriptState> | ||
473 | */ | ||
474 | private void LoadScriptState(XmlDocument doc) | ||
475 | { | ||
476 | DetectParams[] detParams; | ||
477 | LinkedList<EventParams> eventQueue; | ||
478 | |||
479 | // Everything we know is enclosed in <ScriptState>...</ScriptState> | ||
480 | XmlElement scriptStateN = (XmlElement)doc.SelectSingleNode("ScriptState"); | ||
481 | if(scriptStateN == null) | ||
482 | throw new Exception("no <ScriptState> tag"); | ||
483 | |||
484 | string sen = scriptStateN.GetAttribute("Engine"); | ||
485 | if((sen == null) || (sen != m_Engine.ScriptEngineName)) | ||
486 | throw new Exception("<ScriptState> missing Engine=\"YEngine\" attribute"); | ||
487 | |||
488 | // AssetID is unique for the script source text so make sure the | ||
489 | // state file was written for that source file | ||
490 | string assetID = scriptStateN.GetAttribute("Asset"); | ||
491 | if(assetID != m_Item.AssetID.ToString()) | ||
492 | throw new Exception("<ScriptState> assetID mismatch"); | ||
493 | |||
494 | // Also match the sourceHash in case script was | ||
495 | // loaded via 'xmroption fetchsource' and has changed | ||
496 | string sourceHash = scriptStateN.GetAttribute("SourceHash"); | ||
497 | if((sourceHash == null) || (sourceHash != m_ObjCode.sourceHash)) | ||
498 | throw new Exception("<ScriptState> SourceHash mismatch"); | ||
499 | |||
500 | // Get various attributes | ||
501 | XmlElement runningN = (XmlElement)scriptStateN.SelectSingleNode("Running"); | ||
502 | m_Running = bool.Parse(runningN.InnerText); | ||
503 | |||
504 | XmlElement doGblInitN = (XmlElement)scriptStateN.SelectSingleNode("DoGblInit"); | ||
505 | doGblInit = bool.Parse(doGblInitN.InnerText); | ||
506 | |||
507 | XmlElement permissionsN = (XmlElement)scriptStateN.SelectSingleNode("Permissions"); | ||
508 | m_Item.PermsGranter = new UUID(permissionsN.GetAttribute("granter")); | ||
509 | m_Item.PermsMask = Convert.ToInt32(permissionsN.GetAttribute("mask")); | ||
510 | m_Part.Inventory.UpdateInventoryItem(m_Item, false, false); | ||
511 | |||
512 | // get values used by stuff like llDetectedGrab, etc. | ||
513 | detParams = RestoreDetectParams(scriptStateN.SelectSingleNode("DetectArray")); | ||
514 | |||
515 | // Restore queued events | ||
516 | eventQueue = RestoreEventQueue(scriptStateN.SelectSingleNode("EventQueue")); | ||
517 | |||
518 | // Restore timers and listeners | ||
519 | XmlElement pluginN = (XmlElement)scriptStateN.SelectSingleNode("Plugins"); | ||
520 | Object[] pluginData = ExtractXMLObjectArray(pluginN, "plugin"); | ||
521 | |||
522 | // Script's global variables and stack contents | ||
523 | XmlElement snapshotN = | ||
524 | (XmlElement)scriptStateN.SelectSingleNode("Snapshot"); | ||
525 | |||
526 | Byte[] data = Convert.FromBase64String(snapshotN.InnerText); | ||
527 | MemoryStream ms = new MemoryStream(); | ||
528 | ms.Write(data, 0, data.Length); | ||
529 | ms.Seek(0, SeekOrigin.Begin); | ||
530 | MigrateInEventHandler(ms); | ||
531 | ms.Close(); | ||
532 | |||
533 | // Restore event queues, preserving any events that queued | ||
534 | // whilst we were restoring the state | ||
535 | lock(m_QueueLock) | ||
536 | { | ||
537 | m_DetectParams = detParams; | ||
538 | foreach(EventParams evt in m_EventQueue) | ||
539 | eventQueue.AddLast(evt); | ||
540 | |||
541 | m_EventQueue = eventQueue; | ||
542 | for(int i = m_EventCounts.Length; --i >= 0;) | ||
543 | m_EventCounts[i] = 0; | ||
544 | foreach(EventParams evt in m_EventQueue) | ||
545 | { | ||
546 | ScriptEventCode eventCode = (ScriptEventCode)Enum.Parse(typeof(ScriptEventCode), | ||
547 | evt.EventName); | ||
548 | m_EventCounts[(int)eventCode]++; | ||
549 | } | ||
550 | } | ||
551 | |||
552 | // Requeue timer and listeners (possibly queuing new events) | ||
553 | AsyncCommandManager.CreateFromData(m_Engine, | ||
554 | m_LocalID, m_ItemID, m_Part.UUID, | ||
555 | pluginData); | ||
556 | } | ||
557 | |||
558 | /** | ||
559 | * @brief Read llDetectedGrab, etc, values from XML | ||
560 | * <EventQueue> | ||
561 | * <DetectParams>...</DetectParams> | ||
562 | * . | ||
563 | * . | ||
564 | * . | ||
565 | * </EventQueue> | ||
566 | */ | ||
567 | private LinkedList<EventParams> RestoreEventQueue(XmlNode eventsN) | ||
568 | { | ||
569 | LinkedList<EventParams> eventQueue = new LinkedList<EventParams>(); | ||
570 | if(eventsN != null) | ||
571 | { | ||
572 | XmlNodeList eventL = eventsN.SelectNodes("Event"); | ||
573 | foreach(XmlNode evnt in eventL) | ||
574 | { | ||
575 | string name = ((XmlElement)evnt).GetAttribute("Name"); | ||
576 | object[] parms = ExtractXMLObjectArray(evnt, "param"); | ||
577 | DetectParams[] detects = RestoreDetectParams(evnt); | ||
578 | |||
579 | if(parms == null) | ||
580 | parms = zeroObjectArray; | ||
581 | if(detects == null) | ||
582 | detects = zeroDetectParams; | ||
583 | |||
584 | EventParams evt = new EventParams(name, parms, detects); | ||
585 | eventQueue.AddLast(evt); | ||
586 | } | ||
587 | } | ||
588 | return eventQueue; | ||
589 | } | ||
590 | |||
591 | /** | ||
592 | * @brief Read llDetectedGrab, etc, values from XML | ||
593 | * <DetectArray> | ||
594 | * <DetectParams>...</DetectParams> | ||
595 | * . | ||
596 | * . | ||
597 | * . | ||
598 | * </DetectArray> | ||
599 | */ | ||
600 | private DetectParams[] RestoreDetectParams(XmlNode detectedN) | ||
601 | { | ||
602 | if(detectedN == null) | ||
603 | return null; | ||
604 | |||
605 | List<DetectParams> detected = new List<DetectParams>(); | ||
606 | XmlNodeList detectL = detectedN.SelectNodes("DetectParams"); | ||
607 | |||
608 | DetectParams detprm = new DetectParams(); | ||
609 | foreach(XmlNode detxml in detectL) | ||
610 | { | ||
611 | try | ||
612 | { | ||
613 | detprm.Group = new UUID(detxml.Attributes.GetNamedItem("group").Value); | ||
614 | detprm.Key = new UUID(detxml.Attributes.GetNamedItem("key").Value); | ||
615 | detprm.Owner = new UUID(detxml.Attributes.GetNamedItem("owner").Value); | ||
616 | |||
617 | detprm.LinkNum = Int32.Parse(detxml.Attributes.GetNamedItem("linkNum").Value); | ||
618 | detprm.Type = Int32.Parse(detxml.Attributes.GetNamedItem("type").Value); | ||
619 | |||
620 | detprm.Name = detxml.Attributes.GetNamedItem("name").Value; | ||
621 | |||
622 | detprm.OffsetPos = new LSL_Types.Vector3(detxml.Attributes.GetNamedItem("pos").Value); | ||
623 | detprm.Position = new LSL_Types.Vector3(detxml.Attributes.GetNamedItem("position").Value); | ||
624 | detprm.Velocity = new LSL_Types.Vector3(detxml.Attributes.GetNamedItem("velocity").Value); | ||
625 | |||
626 | detprm.Rotation = new LSL_Types.Quaternion(detxml.Attributes.GetNamedItem("rotation").Value); | ||
627 | |||
628 | detected.Add(detprm); | ||
629 | detprm = new DetectParams(); | ||
630 | } | ||
631 | catch(Exception e) | ||
632 | { | ||
633 | m_log.Warn("[YEngine]: RestoreDetectParams bad XML: " + detxml.ToString()); | ||
634 | m_log.Warn("[YEngine]: ... " + e.ToString()); | ||
635 | } | ||
636 | } | ||
637 | |||
638 | return detected.ToArray(); | ||
639 | } | ||
640 | |||
641 | /** | ||
642 | * @brief Extract elements of an array of objects from an XML parent. | ||
643 | * Each element is of form <tag ...>...</tag> | ||
644 | * @param parent = XML parent to extract them from | ||
645 | * @param tag = what the value's tag is | ||
646 | * @returns object array of the values | ||
647 | */ | ||
648 | private static object[] ExtractXMLObjectArray(XmlNode parent, string tag) | ||
649 | { | ||
650 | List<Object> olist = new List<Object>(); | ||
651 | |||
652 | XmlNodeList itemL = parent.SelectNodes(tag); | ||
653 | foreach(XmlNode item in itemL) | ||
654 | { | ||
655 | olist.Add(ExtractXMLObjectValue(item)); | ||
656 | } | ||
657 | |||
658 | return olist.ToArray(); | ||
659 | } | ||
660 | |||
661 | private static object ExtractXMLObjectValue(XmlNode item) | ||
662 | { | ||
663 | string itemType = item.Attributes.GetNamedItem("type").Value; | ||
664 | |||
665 | if(itemType == "list") | ||
666 | { | ||
667 | return new LSL_List(ExtractXMLObjectArray(item, "item")); | ||
668 | } | ||
669 | |||
670 | if(itemType == "OpenMetaverse.UUID") | ||
671 | { | ||
672 | UUID val = new UUID(); | ||
673 | UUID.TryParse(item.InnerText, out val); | ||
674 | return val; | ||
675 | } | ||
676 | |||
677 | Type itemT = Type.GetType(itemType); | ||
678 | if(itemT == null) | ||
679 | { | ||
680 | Object[] args = new Object[] { item.InnerText }; | ||
681 | |||
682 | string assembly = itemType + ", OpenSim.Region.ScriptEngine.Shared"; | ||
683 | itemT = Type.GetType(assembly); | ||
684 | if(itemT == null) | ||
685 | { | ||
686 | return null; | ||
687 | } | ||
688 | return Activator.CreateInstance(itemT, args); | ||
689 | } | ||
690 | |||
691 | return Convert.ChangeType(item.InnerText, itemT); | ||
692 | } | ||
693 | |||
694 | /* | ||
695 | * Migrate an event handler in from a stream. | ||
696 | * | ||
697 | * Input: | ||
698 | * stream = as generated by MigrateOutEventHandler() | ||
699 | */ | ||
700 | private void MigrateInEventHandler(Stream stream) | ||
701 | { | ||
702 | int mv = stream.ReadByte(); | ||
703 | if(mv != migrationVersion) | ||
704 | throw new Exception("incoming migration version " + mv + " but accept only " + migrationVersion); | ||
705 | |||
706 | stream.ReadByte(); // ignored | ||
707 | |||
708 | /* | ||
709 | * Restore script variables and stack and other state from stream. | ||
710 | * And it also marks us busy (by setting this.eventCode) so we can't be | ||
711 | * started again and this event lost. If it restores this.eventCode = | ||
712 | * None, the the script was idle. | ||
713 | */ | ||
714 | lock(m_RunLock) | ||
715 | { | ||
716 | BinaryReader br = new BinaryReader(stream); | ||
717 | this.MigrateIn(br); | ||
718 | |||
719 | m_RunOnePhase = "MigrateInEventHandler finished"; | ||
720 | CheckRunLockInvariants(true); | ||
721 | } | ||
722 | } | ||
723 | } | ||
724 | } | ||