diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs | 177 |
1 files changed, 137 insertions, 40 deletions
diff --git a/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs b/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs index 771db0c..a869a6a 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs | |||
@@ -94,6 +94,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
94 | private UUID m_CurrentStateHash; | 94 | private UUID m_CurrentStateHash; |
95 | private UUID m_RegionID; | 95 | private UUID m_RegionID; |
96 | 96 | ||
97 | public int DebugLevel { get; set; } | ||
98 | |||
97 | public Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> LineMap { get; set; } | 99 | public Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> LineMap { get; set; } |
98 | 100 | ||
99 | private Dictionary<string,IScriptApi> m_Apis = new Dictionary<string,IScriptApi>(); | 101 | private Dictionary<string,IScriptApi> m_Apis = new Dictionary<string,IScriptApi>(); |
@@ -156,6 +158,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
156 | 158 | ||
157 | public UUID AppDomain { get; set; } | 159 | public UUID AppDomain { get; set; } |
158 | 160 | ||
161 | public SceneObjectPart Part { get; private set; } | ||
162 | |||
159 | public string PrimName { get; private set; } | 163 | public string PrimName { get; private set; } |
160 | 164 | ||
161 | public string ScriptName { get; private set; } | 165 | public string ScriptName { get; private set; } |
@@ -174,6 +178,17 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
174 | 178 | ||
175 | public Queue EventQueue { get; private set; } | 179 | public Queue EventQueue { get; private set; } |
176 | 180 | ||
181 | public long EventsQueued | ||
182 | { | ||
183 | get | ||
184 | { | ||
185 | lock (EventQueue) | ||
186 | return EventQueue.Count; | ||
187 | } | ||
188 | } | ||
189 | |||
190 | public long EventsProcessed { get; private set; } | ||
191 | |||
177 | public int StartParam { get; set; } | 192 | public int StartParam { get; set; } |
178 | 193 | ||
179 | public TaskInventoryItem ScriptTask { get; private set; } | 194 | public TaskInventoryItem ScriptTask { get; private set; } |
@@ -186,54 +201,68 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
186 | 201 | ||
187 | public static readonly long MaxMeasurementPeriod = 30 * TimeSpan.TicksPerMinute; | 202 | public static readonly long MaxMeasurementPeriod = 30 * TimeSpan.TicksPerMinute; |
188 | 203 | ||
204 | private bool m_coopTermination; | ||
205 | |||
206 | private EventWaitHandle m_coopSleepHandle; | ||
207 | |||
189 | public void ClearQueue() | 208 | public void ClearQueue() |
190 | { | 209 | { |
191 | m_TimerQueued = false; | 210 | m_TimerQueued = false; |
192 | EventQueue.Clear(); | 211 | EventQueue.Clear(); |
193 | } | 212 | } |
194 | 213 | ||
195 | public ScriptInstance(IScriptEngine engine, SceneObjectPart part, | 214 | public ScriptInstance( |
196 | UUID itemID, UUID assetID, string assembly, | 215 | IScriptEngine engine, SceneObjectPart part, TaskInventoryItem item, |
197 | AppDomain dom, string primName, string scriptName, | 216 | int startParam, bool postOnRez, |
198 | int startParam, bool postOnRez, StateSource stateSource, | 217 | int maxScriptQueue) |
199 | int maxScriptQueue) | ||
200 | { | 218 | { |
201 | State = "default"; | 219 | State = "default"; |
202 | EventQueue = new Queue(32); | 220 | EventQueue = new Queue(32); |
203 | 221 | ||
204 | Engine = engine; | 222 | Engine = engine; |
205 | LocalID = part.LocalId; | 223 | Part = part; |
206 | ObjectID = part.UUID; | 224 | ScriptTask = item; |
207 | RootLocalID = part.ParentGroup.LocalId; | 225 | |
208 | RootObjectID = part.ParentGroup.UUID; | 226 | // This is currently only here to allow regression tests to get away without specifying any inventory |
209 | ItemID = itemID; | 227 | // item when they are testing script logic that doesn't require an item. |
210 | AssetID = assetID; | 228 | if (ScriptTask != null) |
211 | PrimName = primName; | 229 | { |
212 | ScriptName = scriptName; | 230 | ScriptName = ScriptTask.Name; |
213 | m_Assembly = assembly; | 231 | ItemID = ScriptTask.ItemID; |
232 | AssetID = ScriptTask.AssetID; | ||
233 | } | ||
234 | |||
235 | PrimName = part.ParentGroup.Name; | ||
214 | StartParam = startParam; | 236 | StartParam = startParam; |
215 | m_MaxScriptQueue = maxScriptQueue; | 237 | m_MaxScriptQueue = maxScriptQueue; |
216 | m_stateSource = stateSource; | ||
217 | m_postOnRez = postOnRez; | 238 | m_postOnRez = postOnRez; |
218 | m_AttachedAvatar = part.ParentGroup.AttachedAvatar; | 239 | m_AttachedAvatar = part.ParentGroup.AttachedAvatar; |
219 | m_RegionID = part.ParentGroup.Scene.RegionInfo.RegionID; | 240 | m_RegionID = part.ParentGroup.Scene.RegionInfo.RegionID; |
220 | 241 | ||
221 | if (part != null) | 242 | if (Engine.Config.GetString("ScriptStopStrategy", "abort") == "co-op") |
222 | { | 243 | { |
223 | part.TaskInventory.LockItemsForRead(true); | 244 | m_coopTermination = true; |
224 | if (part.TaskInventory.ContainsKey(ItemID)) | 245 | m_coopSleepHandle = new AutoResetEvent(false); |
225 | { | ||
226 | ScriptTask = part.TaskInventory[ItemID]; | ||
227 | } | ||
228 | part.TaskInventory.LockItemsForRead(false); | ||
229 | } | 246 | } |
247 | } | ||
248 | |||
249 | /// <summary> | ||
250 | /// Load the script from an assembly into an AppDomain. | ||
251 | /// </summary> | ||
252 | /// <param name='dom'></param> | ||
253 | /// <param name='assembly'></param> | ||
254 | /// <param name='stateSource'></param> | ||
255 | public void Load(AppDomain dom, string assembly, StateSource stateSource) | ||
256 | { | ||
257 | m_Assembly = assembly; | ||
258 | m_stateSource = stateSource; | ||
230 | 259 | ||
231 | ApiManager am = new ApiManager(); | 260 | ApiManager am = new ApiManager(); |
232 | 261 | ||
233 | foreach (string api in am.GetApis()) | 262 | foreach (string api in am.GetApis()) |
234 | { | 263 | { |
235 | m_Apis[api] = am.CreateApi(api); | 264 | m_Apis[api] = am.CreateApi(api); |
236 | m_Apis[api].Initialize(engine, part, ScriptTask); | 265 | m_Apis[api].Initialize(Engine, Part, ScriptTask, m_coopSleepHandle); |
237 | } | 266 | } |
238 | 267 | ||
239 | try | 268 | try |
@@ -267,7 +296,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
267 | 296 | ||
268 | // // m_log.Debug("[Script] Script instance created"); | 297 | // // m_log.Debug("[Script] Script instance created"); |
269 | 298 | ||
270 | part.SetScriptEvents(ItemID, | 299 | Part.SetScriptEvents(ItemID, |
271 | (int)m_Script.GetStateEventFlags(State)); | 300 | (int)m_Script.GetStateEventFlags(State)); |
272 | } | 301 | } |
273 | catch (Exception e) | 302 | catch (Exception e) |
@@ -309,7 +338,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
309 | 338 | ||
310 | // m_log.DebugFormat("[Script] Successfully retrieved state for script {0}.{1}", PrimName, m_ScriptName); | 339 | // m_log.DebugFormat("[Script] Successfully retrieved state for script {0}.{1}", PrimName, m_ScriptName); |
311 | 340 | ||
312 | part.SetScriptEvents(ItemID, | 341 | Part.SetScriptEvents(ItemID, |
313 | (int)m_Script.GetStateEventFlags(State)); | 342 | (int)m_Script.GetStateEventFlags(State)); |
314 | 343 | ||
315 | if (!Running) | 344 | if (!Running) |
@@ -521,9 +550,34 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
521 | } | 550 | } |
522 | 551 | ||
523 | // Wait for the current event to complete. | 552 | // Wait for the current event to complete. |
524 | if (!m_InSelfDelete && workItem.Wait(new TimeSpan((long)timeout * 100000))) | 553 | if (!m_InSelfDelete) |
525 | { | 554 | { |
526 | return true; | 555 | if (!m_coopTermination) |
556 | { | ||
557 | // If we're not co-operative terminating then try and wait for the event to complete before stopping | ||
558 | if (workItem.Wait(new TimeSpan((long)timeout * 100000))) | ||
559 | return true; | ||
560 | } | ||
561 | else | ||
562 | { | ||
563 | m_log.DebugFormat( | ||
564 | "[SCRIPT INSTANCE]: Co-operatively stopping script {0} {1} in {2} {3}", | ||
565 | ScriptName, ItemID, PrimName, ObjectID); | ||
566 | |||
567 | // This will terminate the event on next handle check by the script. | ||
568 | m_coopSleepHandle.Set(); | ||
569 | |||
570 | // For now, we will wait forever since the event should always cleanly terminate once LSL loop | ||
571 | // checking is implemented. May want to allow a shorter timeout option later. | ||
572 | if (workItem.Wait(TimeSpan.MaxValue)) | ||
573 | { | ||
574 | m_log.DebugFormat( | ||
575 | "[SCRIPT INSTANCE]: Co-operatively stopped script {0} {1} in {2} {3}", | ||
576 | ScriptName, ItemID, PrimName, ObjectID); | ||
577 | |||
578 | return true; | ||
579 | } | ||
580 | } | ||
527 | } | 581 | } |
528 | 582 | ||
529 | lock (EventQueue) | 583 | lock (EventQueue) |
@@ -536,11 +590,12 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
536 | 590 | ||
537 | // If the event still hasn't stopped and we the stop isn't the result of script or object removal, then | 591 | // If the event still hasn't stopped and we the stop isn't the result of script or object removal, then |
538 | // forcibly abort the work item (this aborts the underlying thread). | 592 | // forcibly abort the work item (this aborts the underlying thread). |
593 | // Co-operative termination should never reach this point. | ||
539 | if (!m_InSelfDelete) | 594 | if (!m_InSelfDelete) |
540 | { | 595 | { |
541 | // m_log.ErrorFormat( | 596 | m_log.DebugFormat( |
542 | // "[SCRIPT INSTANCE]: Aborting script {0} {1} in prim {2} {3} {4} {5}", | 597 | "[SCRIPT INSTANCE]: Aborting unstopped script {0} {1} in prim {2}, localID {3}, timeout was {4} ms", |
543 | // ScriptName, ItemID, PrimName, ObjectID, m_InSelfDelete, DateTime.Now.Ticks); | 598 | ScriptName, ItemID, PrimName, LocalID, timeout); |
544 | 599 | ||
545 | workItem.Abort(); | 600 | workItem.Abort(); |
546 | } | 601 | } |
@@ -696,19 +751,41 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
696 | { | 751 | { |
697 | 752 | ||
698 | // m_log.DebugFormat("[XEngine]: Processing event {0} for {1}", data.EventName, this); | 753 | // m_log.DebugFormat("[XEngine]: Processing event {0} for {1}", data.EventName, this); |
754 | SceneObjectPart part = Engine.World.GetSceneObjectPart(LocalID); | ||
755 | |||
756 | if (DebugLevel >= 2) | ||
757 | m_log.DebugFormat( | ||
758 | "[SCRIPT INSTANCE]: Processing event {0} for {1}/{2}({3})/{4}({5}) @ {6}/{7}", | ||
759 | data.EventName, | ||
760 | ScriptName, | ||
761 | part.Name, | ||
762 | part.LocalId, | ||
763 | part.ParentGroup.Name, | ||
764 | part.ParentGroup.UUID, | ||
765 | part.AbsolutePosition, | ||
766 | part.ParentGroup.Scene.Name); | ||
699 | 767 | ||
700 | m_DetectParams = data.DetectParams; | 768 | m_DetectParams = data.DetectParams; |
701 | 769 | ||
702 | if (data.EventName == "state") // Hardcoded state change | 770 | if (data.EventName == "state") // Hardcoded state change |
703 | { | 771 | { |
704 | // m_log.DebugFormat("[Script] Script {0}.{1} state set to {2}", | ||
705 | // PrimName, ScriptName, data.Params[0].ToString()); | ||
706 | State = data.Params[0].ToString(); | 772 | State = data.Params[0].ToString(); |
773 | |||
774 | if (DebugLevel >= 1) | ||
775 | m_log.DebugFormat( | ||
776 | "[SCRIPT INSTANCE]: Changing state to {0} for {1}/{2}({3})/{4}({5}) @ {6}/{7}", | ||
777 | State, | ||
778 | ScriptName, | ||
779 | part.Name, | ||
780 | part.LocalId, | ||
781 | part.ParentGroup.Name, | ||
782 | part.ParentGroup.UUID, | ||
783 | part.AbsolutePosition, | ||
784 | part.ParentGroup.Scene.Name); | ||
785 | |||
707 | AsyncCommandManager.RemoveScript(Engine, | 786 | AsyncCommandManager.RemoveScript(Engine, |
708 | LocalID, ItemID); | 787 | LocalID, ItemID); |
709 | 788 | ||
710 | SceneObjectPart part = Engine.World.GetSceneObjectPart( | ||
711 | LocalID); | ||
712 | if (part != null) | 789 | if (part != null) |
713 | { | 790 | { |
714 | part.SetScriptEvents(ItemID, | 791 | part.SetScriptEvents(ItemID, |
@@ -720,8 +797,6 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
720 | if (Engine.World.PipeEventsForScript(LocalID) || | 797 | if (Engine.World.PipeEventsForScript(LocalID) || |
721 | data.EventName == "control") // Don't freeze avies! | 798 | data.EventName == "control") // Don't freeze avies! |
722 | { | 799 | { |
723 | SceneObjectPart part = Engine.World.GetSceneObjectPart( | ||
724 | LocalID); | ||
725 | // m_log.DebugFormat("[Script] Delivered event {2} in state {3} to {0}.{1}", | 800 | // m_log.DebugFormat("[Script] Delivered event {2} in state {3} to {0}.{1}", |
726 | // PrimName, ScriptName, data.EventName, State); | 801 | // PrimName, ScriptName, data.EventName, State); |
727 | 802 | ||
@@ -763,7 +838,11 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
763 | m_InEvent = false; | 838 | m_InEvent = false; |
764 | m_CurrentEvent = String.Empty; | 839 | m_CurrentEvent = String.Empty; |
765 | 840 | ||
766 | if ((!(e is TargetInvocationException) || (!(e.InnerException is SelfDeleteException) && !(e.InnerException is ScriptDeleteException))) && !(e is ThreadAbortException)) | 841 | if ((!(e is TargetInvocationException) |
842 | || (!(e.InnerException is SelfDeleteException) | ||
843 | && !(e.InnerException is ScriptDeleteException) | ||
844 | && !(e.InnerException is ScriptCoopStopException))) | ||
845 | && !(e is ThreadAbortException)) | ||
767 | { | 846 | { |
768 | try | 847 | try |
769 | { | 848 | { |
@@ -776,6 +855,17 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
776 | ChatTypeEnum.DebugChannel, 2147483647, | 855 | ChatTypeEnum.DebugChannel, 2147483647, |
777 | part.AbsolutePosition, | 856 | part.AbsolutePosition, |
778 | part.Name, part.UUID, false); | 857 | part.Name, part.UUID, false); |
858 | |||
859 | |||
860 | m_log.DebugFormat( | ||
861 | "[SCRIPT INSTANCE]: Runtime error in script {0}, part {1} {2} at {3} in {4}, displayed error {5}, actual exception {6}", | ||
862 | ScriptName, | ||
863 | PrimName, | ||
864 | part.UUID, | ||
865 | part.AbsolutePosition, | ||
866 | part.ParentGroup.Scene.Name, | ||
867 | text.Replace("\n", "\\n"), | ||
868 | e.InnerException); | ||
779 | } | 869 | } |
780 | catch (Exception) | 870 | catch (Exception) |
781 | { | 871 | { |
@@ -802,6 +892,12 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
802 | if (part != null) | 892 | if (part != null) |
803 | part.Inventory.RemoveInventoryItem(ItemID); | 893 | part.Inventory.RemoveInventoryItem(ItemID); |
804 | } | 894 | } |
895 | else if ((e is TargetInvocationException) && (e.InnerException is ScriptCoopStopException)) | ||
896 | { | ||
897 | m_log.DebugFormat( | ||
898 | "[SCRIPT INSTANCE]: Script {0}.{1} in event {2}, state {3} stopped co-operatively.", | ||
899 | PrimName, ScriptName, data.EventName, State); | ||
900 | } | ||
805 | } | 901 | } |
806 | } | 902 | } |
807 | } | 903 | } |
@@ -810,6 +906,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
810 | // script engine to run the next event. | 906 | // script engine to run the next event. |
811 | lock (EventQueue) | 907 | lock (EventQueue) |
812 | { | 908 | { |
909 | EventsProcessed++; | ||
910 | |||
813 | if (EventQueue.Count > 0 && Running && !ShuttingDown) | 911 | if (EventQueue.Count > 0 && Running && !ShuttingDown) |
814 | { | 912 | { |
815 | m_CurrentWorkItem = Engine.QueueEventHandler(this); | 913 | m_CurrentWorkItem = Engine.QueueEventHandler(this); |
@@ -834,7 +932,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
834 | return (DateTime.Now - m_EventStart).Seconds; | 932 | return (DateTime.Now - m_EventStart).Seconds; |
835 | } | 933 | } |
836 | 934 | ||
837 | public void ResetScript() | 935 | public void ResetScript(int timeout) |
838 | { | 936 | { |
839 | if (m_Script == null) | 937 | if (m_Script == null) |
840 | return; | 938 | return; |
@@ -844,7 +942,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
844 | RemoveState(); | 942 | RemoveState(); |
845 | ReleaseControls(); | 943 | ReleaseControls(); |
846 | 944 | ||
847 | Stop(0); | 945 | Stop(timeout); |
848 | SceneObjectPart part = Engine.World.GetSceneObjectPart(LocalID); | 946 | SceneObjectPart part = Engine.World.GetSceneObjectPart(LocalID); |
849 | part.Inventory.GetInventoryItem(ItemID).PermsMask = 0; | 947 | part.Inventory.GetInventoryItem(ItemID).PermsMask = 0; |
850 | part.Inventory.GetInventoryItem(ItemID).PermsGranter = UUID.Zero; | 948 | part.Inventory.GetInventoryItem(ItemID).PermsGranter = UUID.Zero; |
@@ -1015,7 +1113,6 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance | |||
1015 | "({0}): {1}", scriptLine - 1, | 1113 | "({0}): {1}", scriptLine - 1, |
1016 | e.InnerException.Message); | 1114 | e.InnerException.Message); |
1017 | 1115 | ||
1018 | System.Console.WriteLine(e.ToString()+"\n"); | ||
1019 | return message; | 1116 | return message; |
1020 | } | 1117 | } |
1021 | } | 1118 | } |