diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Region/ScriptEngine/YEngine/XMRInstMisc.cs | 399 |
1 files changed, 399 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/YEngine/XMRInstMisc.cs b/OpenSim/Region/ScriptEngine/YEngine/XMRInstMisc.cs new file mode 100644 index 0000000..0af3d37 --- /dev/null +++ b/OpenSim/Region/ScriptEngine/YEngine/XMRInstMisc.cs | |||
@@ -0,0 +1,399 @@ | |||
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.Reflection.Emit; | ||
34 | using System.Runtime.Remoting.Lifetime; | ||
35 | using System.Security.Policy; | ||
36 | using System.IO; | ||
37 | using System.Xml; | ||
38 | using System.Text; | ||
39 | using OpenMetaverse; | ||
40 | using OpenSim.Framework; | ||
41 | using OpenSim.Region.ScriptEngine.Interfaces; | ||
42 | using OpenSim.Region.ScriptEngine.Shared; | ||
43 | using OpenSim.Region.ScriptEngine.Shared.Api; | ||
44 | using OpenSim.Region.ScriptEngine.Shared.ScriptBase; | ||
45 | using OpenSim.Region.ScriptEngine.Yengine; | ||
46 | using OpenSim.Region.Framework.Scenes; | ||
47 | using log4net; | ||
48 | |||
49 | using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat; | ||
50 | using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger; | ||
51 | using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString; | ||
52 | using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list; | ||
53 | using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion; | ||
54 | using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString; | ||
55 | using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3; | ||
56 | |||
57 | // This class exists in the main app domain | ||
58 | // | ||
59 | namespace OpenSim.Region.ScriptEngine.Yengine | ||
60 | { | ||
61 | public partial class XMRInstance | ||
62 | { | ||
63 | |||
64 | // In case Dispose() doesn't get called, we want to be sure to clean | ||
65 | // up. This makes sure we decrement m_CompiledScriptRefCount. | ||
66 | ~XMRInstance() | ||
67 | { | ||
68 | Dispose(); | ||
69 | } | ||
70 | |||
71 | /** | ||
72 | * @brief Clean up stuff. | ||
73 | * We specifically leave m_DescName intact for 'xmr ls' command. | ||
74 | */ | ||
75 | public void Dispose() | ||
76 | { | ||
77 | // Tell script stop executing next time it calls CheckRun(). | ||
78 | suspendOnCheckRunHold = true; | ||
79 | |||
80 | // Don't send us any more events. | ||
81 | lock(m_RunLock) | ||
82 | { | ||
83 | if(m_Part != null) | ||
84 | { | ||
85 | m_Part.RemoveScriptEvents(m_ItemID); | ||
86 | AsyncCommandManager.RemoveScript(m_Engine, m_LocalID, m_ItemID); | ||
87 | m_Part = null; | ||
88 | } | ||
89 | } | ||
90 | |||
91 | // Let script methods get garbage collected if no one else is using | ||
92 | // them. | ||
93 | DecObjCodeRefCount(); | ||
94 | } | ||
95 | |||
96 | private void DecObjCodeRefCount() | ||
97 | { | ||
98 | if(m_ObjCode != null) | ||
99 | { | ||
100 | lock(m_CompileLock) | ||
101 | { | ||
102 | ScriptObjCode objCode; | ||
103 | |||
104 | if(m_CompiledScriptObjCode.TryGetValue(m_ScriptObjCodeKey, out objCode) && | ||
105 | (objCode == m_ObjCode) && | ||
106 | (--objCode.refCount == 0)) | ||
107 | { | ||
108 | m_CompiledScriptObjCode.Remove(m_ScriptObjCodeKey); | ||
109 | } | ||
110 | } | ||
111 | m_ObjCode = null; | ||
112 | } | ||
113 | } | ||
114 | |||
115 | public void Verbose(string format, params object[] args) | ||
116 | { | ||
117 | if(m_Engine.m_Verbose) | ||
118 | m_log.DebugFormat(format, args); | ||
119 | } | ||
120 | |||
121 | // Called by 'xmr top' console command | ||
122 | // to dump this script's state to console | ||
123 | // Sacha | ||
124 | public void RunTestTop() | ||
125 | { | ||
126 | if(m_InstEHSlice > 0) | ||
127 | { | ||
128 | Console.WriteLine(m_DescName); | ||
129 | Console.WriteLine(" m_LocalID = " + m_LocalID); | ||
130 | Console.WriteLine(" m_ItemID = " + m_ItemID); | ||
131 | Console.WriteLine(" m_Item.AssetID = " + m_Item.AssetID); | ||
132 | Console.WriteLine(" m_StartParam = " + m_StartParam); | ||
133 | Console.WriteLine(" m_PostOnRez = " + m_PostOnRez); | ||
134 | Console.WriteLine(" m_StateSource = " + m_StateSource); | ||
135 | Console.WriteLine(" m_SuspendCount = " + m_SuspendCount); | ||
136 | Console.WriteLine(" m_SleepUntil = " + m_SleepUntil); | ||
137 | Console.WriteLine(" m_IState = " + m_IState.ToString()); | ||
138 | Console.WriteLine(" m_StateCode = " + GetStateName(stateCode)); | ||
139 | Console.WriteLine(" eventCode = " + eventCode.ToString()); | ||
140 | Console.WriteLine(" m_LastRanAt = " + m_LastRanAt.ToString()); | ||
141 | Console.WriteLine(" heapUsed/Limit = " + xmrHeapUsed() + "/" + heapLimit); | ||
142 | Console.WriteLine(" m_InstEHEvent = " + m_InstEHEvent.ToString()); | ||
143 | Console.WriteLine(" m_InstEHSlice = " + m_InstEHSlice.ToString()); | ||
144 | } | ||
145 | } | ||
146 | |||
147 | // Called by 'xmr ls' console command | ||
148 | // to dump this script's state to console | ||
149 | public string RunTestLs(bool flagFull) | ||
150 | { | ||
151 | if(flagFull) | ||
152 | { | ||
153 | StringBuilder sb = new StringBuilder(); | ||
154 | sb.AppendLine(m_DescName); | ||
155 | sb.AppendLine(" m_LocalID = " + m_LocalID); | ||
156 | sb.AppendLine(" m_ItemID = " + m_ItemID + " (.state file)"); | ||
157 | sb.AppendLine(" m_Item.AssetID = " + m_Item.AssetID); | ||
158 | sb.AppendLine(" m_Part.WorldPosition = " + m_Part.GetWorldPosition()); | ||
159 | sb.AppendLine(" m_ScriptObjCodeKey = " + m_ScriptObjCodeKey + " (source text)"); | ||
160 | sb.AppendLine(" m_StartParam = " + m_StartParam); | ||
161 | sb.AppendLine(" m_PostOnRez = " + m_PostOnRez); | ||
162 | sb.AppendLine(" m_StateSource = " + m_StateSource); | ||
163 | sb.AppendLine(" m_SuspendCount = " + m_SuspendCount); | ||
164 | sb.AppendLine(" m_SleepUntil = " + m_SleepUntil); | ||
165 | sb.AppendLine(" m_SleepEvMask1 = 0x" + m_SleepEventMask1.ToString("X")); | ||
166 | sb.AppendLine(" m_SleepEvMask2 = 0x" + m_SleepEventMask2.ToString("X")); | ||
167 | sb.AppendLine(" m_IState = " + m_IState.ToString()); | ||
168 | sb.AppendLine(" m_StateCode = " + GetStateName(stateCode)); | ||
169 | sb.AppendLine(" eventCode = " + eventCode.ToString()); | ||
170 | sb.AppendLine(" m_LastRanAt = " + m_LastRanAt.ToString()); | ||
171 | sb.AppendLine(" m_RunOnePhase = " + m_RunOnePhase); | ||
172 | sb.AppendLine(" suspOnCkRunHold = " + suspendOnCheckRunHold); | ||
173 | sb.AppendLine(" suspOnCkRunTemp = " + suspendOnCheckRunTemp); | ||
174 | sb.AppendLine(" m_CheckRunPhase = " + m_CheckRunPhase); | ||
175 | sb.AppendLine(" heapUsed/Limit = " + xmrHeapUsed() + "/" + heapLimit); | ||
176 | sb.AppendLine(" m_InstEHEvent = " + m_InstEHEvent.ToString()); | ||
177 | sb.AppendLine(" m_InstEHSlice = " + m_InstEHSlice.ToString()); | ||
178 | sb.AppendLine(" m_CPUTime = " + m_CPUTime); | ||
179 | sb.AppendLine(" callMode = " + callMode); | ||
180 | lock(m_QueueLock) | ||
181 | { | ||
182 | sb.AppendLine(" m_Running = " + m_Running); | ||
183 | foreach(EventParams evt in m_EventQueue) | ||
184 | { | ||
185 | sb.AppendLine(" evt.EventName = " + evt.EventName); | ||
186 | } | ||
187 | } | ||
188 | return sb.ToString(); | ||
189 | } | ||
190 | else | ||
191 | { | ||
192 | return String.Format("{0} {1} {2} {3} {4} {5}", | ||
193 | m_ItemID, | ||
194 | m_CPUTime.ToString("F3").PadLeft(9), | ||
195 | m_InstEHEvent.ToString().PadLeft(9), | ||
196 | m_IState.ToString().PadRight(10), | ||
197 | m_Part.GetWorldPosition().ToString().PadRight(32), | ||
198 | m_DescName); | ||
199 | } | ||
200 | } | ||
201 | |||
202 | /** | ||
203 | * @brief For a given stateCode, get a mask of the low 32 event codes | ||
204 | * that the state has handlers defined for. | ||
205 | */ | ||
206 | public int GetStateEventFlags(int stateCode) | ||
207 | { | ||
208 | if((stateCode < 0) || | ||
209 | (stateCode >= m_ObjCode.scriptEventHandlerTable.GetLength(0))) | ||
210 | { | ||
211 | return 0; | ||
212 | } | ||
213 | |||
214 | int code = 0; | ||
215 | for(int i = 0; i < 32; i++) | ||
216 | { | ||
217 | if(m_ObjCode.scriptEventHandlerTable[stateCode, i] != null) | ||
218 | { | ||
219 | code |= 1 << i; | ||
220 | } | ||
221 | } | ||
222 | |||
223 | return code; | ||
224 | } | ||
225 | |||
226 | /** | ||
227 | * @brief Get the .state file name. | ||
228 | */ | ||
229 | public static string GetStateFileName(string scriptBasePath, UUID itemID) | ||
230 | { | ||
231 | return GetScriptFileName(scriptBasePath, itemID.ToString() + ".state"); | ||
232 | } | ||
233 | |||
234 | public string GetScriptFileName(string filename) | ||
235 | { | ||
236 | return GetScriptFileName(m_ScriptBasePath, filename); | ||
237 | } | ||
238 | |||
239 | public static string GetScriptFileName(string scriptBasePath, string filename) | ||
240 | { | ||
241 | // Get old path, ie, all files lumped in a single huge directory. | ||
242 | string oldPath = Path.Combine(scriptBasePath, filename); | ||
243 | |||
244 | // Get new path, ie, files split up based on first 2 chars of name. | ||
245 | // string subdir = filename.Substring (0, 2); | ||
246 | // filename = filename.Substring (2); | ||
247 | string subdir = filename.Substring(0, 1); | ||
248 | filename = filename.Substring(1); | ||
249 | scriptBasePath = Path.Combine(scriptBasePath, subdir); | ||
250 | Directory.CreateDirectory(scriptBasePath); | ||
251 | string newPath = Path.Combine(scriptBasePath, filename); | ||
252 | |||
253 | // If file exists only in old location, move to new location. | ||
254 | // If file exists in both locations, delete old location. | ||
255 | if(File.Exists(oldPath)) | ||
256 | { | ||
257 | if(File.Exists(newPath)) | ||
258 | { | ||
259 | File.Delete(oldPath); | ||
260 | } | ||
261 | else | ||
262 | { | ||
263 | File.Move(oldPath, newPath); | ||
264 | } | ||
265 | } | ||
266 | |||
267 | // Always return new location. | ||
268 | return newPath; | ||
269 | } | ||
270 | |||
271 | /** | ||
272 | * @brief Decode state code (int) to state name (string). | ||
273 | */ | ||
274 | public string GetStateName(int stateCode) | ||
275 | { | ||
276 | try | ||
277 | { | ||
278 | return m_ObjCode.stateNames[stateCode]; | ||
279 | } | ||
280 | catch | ||
281 | { | ||
282 | return stateCode.ToString(); | ||
283 | } | ||
284 | } | ||
285 | |||
286 | /** | ||
287 | * @brief various gets & sets. | ||
288 | */ | ||
289 | public int StartParam | ||
290 | { | ||
291 | get | ||
292 | { | ||
293 | return m_StartParam; | ||
294 | } | ||
295 | set | ||
296 | { | ||
297 | m_StartParam = value; | ||
298 | } | ||
299 | } | ||
300 | |||
301 | public SceneObjectPart SceneObject | ||
302 | { | ||
303 | get | ||
304 | { | ||
305 | return m_Part; | ||
306 | } | ||
307 | } | ||
308 | |||
309 | public DetectParams[] DetectParams | ||
310 | { | ||
311 | get | ||
312 | { | ||
313 | return m_DetectParams; | ||
314 | } | ||
315 | set | ||
316 | { | ||
317 | m_DetectParams = value; | ||
318 | } | ||
319 | } | ||
320 | |||
321 | public UUID ItemID | ||
322 | { | ||
323 | get | ||
324 | { | ||
325 | return m_ItemID; | ||
326 | } | ||
327 | } | ||
328 | |||
329 | public UUID AssetID | ||
330 | { | ||
331 | get | ||
332 | { | ||
333 | return m_Item.AssetID; | ||
334 | } | ||
335 | } | ||
336 | |||
337 | public bool Running | ||
338 | { | ||
339 | get | ||
340 | { | ||
341 | return m_Running; | ||
342 | } | ||
343 | set | ||
344 | { | ||
345 | lock(m_QueueLock) | ||
346 | { | ||
347 | m_Running = value; | ||
348 | if(!value) | ||
349 | { | ||
350 | EmptyEventQueues(); | ||
351 | } | ||
352 | } | ||
353 | } | ||
354 | } | ||
355 | |||
356 | /** | ||
357 | * @brief Empty out the event queues. | ||
358 | * Assumes caller has the m_QueueLock locked. | ||
359 | */ | ||
360 | public void EmptyEventQueues() | ||
361 | { | ||
362 | m_EventQueue.Clear(); | ||
363 | for(int i = m_EventCounts.Length; --i >= 0;) | ||
364 | m_EventCounts[i] = 0; | ||
365 | } | ||
366 | |||
367 | /** | ||
368 | * @brief Convert an LSL vector to an Openmetaverse vector. | ||
369 | */ | ||
370 | public static OpenMetaverse.Vector3 LSLVec2OMVec(LSL_Vector lslVec) | ||
371 | { | ||
372 | return new OpenMetaverse.Vector3((float)lslVec.x, (float)lslVec.y, (float)lslVec.z); | ||
373 | } | ||
374 | |||
375 | /** | ||
376 | * @brief Extract an integer from an element of an LSL_List. | ||
377 | */ | ||
378 | public static int ListInt(object element) | ||
379 | { | ||
380 | if(element is LSL_Integer) | ||
381 | { | ||
382 | return (int)(LSL_Integer)element; | ||
383 | } | ||
384 | return (int)element; | ||
385 | } | ||
386 | |||
387 | /** | ||
388 | * @brief Extract a string from an element of an LSL_List. | ||
389 | */ | ||
390 | public static string ListStr(object element) | ||
391 | { | ||
392 | if(element is LSL_String) | ||
393 | { | ||
394 | return (string)(LSL_String)element; | ||
395 | } | ||
396 | return (string)element; | ||
397 | } | ||
398 | } | ||
399 | } | ||