diff --git a/Razor/Properties/AssemblyInfo.cs b/Razor/Properties/AssemblyInfo.cs index 2ae1f097..da99dd4c 100644 --- a/Razor/Properties/AssemblyInfo.cs +++ b/Razor/Properties/AssemblyInfo.cs @@ -26,7 +26,7 @@ // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: -[assembly: AssemblyVersion("0.8.2.101")] +[assembly: AssemblyVersion("0.8.2.102")] // // In order to sign your assembly you must specify a key to use. Refer to the diff --git a/Razor/RazorEnhanced/EnhancedScript.cs b/Razor/RazorEnhanced/EnhancedScript.cs index f172c5f0..4d4fe2a9 100644 --- a/Razor/RazorEnhanced/EnhancedScript.cs +++ b/Razor/RazorEnhanced/EnhancedScript.cs @@ -1,28 +1,25 @@ using Assistant; using IronPython.Hosting; -using IronPython.Runtime.Exceptions; using IronPython.Runtime; -using Microsoft.Scripting.Hosting.Providers; +using IronPython.Runtime.Exceptions; +using Microsoft.Scripting; using Microsoft.Scripting.Hosting; +using Microsoft.Scripting.Hosting.Providers; +using Microsoft.Scripting.Utils; +using RazorEnhanced.UOScript; using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Reflection; +using System.Runtime.InteropServices; using System.Text; +using System.Text.RegularExpressions; using System.Threading; - -using Microsoft.Scripting; +using System.Windows.Forms; using static RazorEnhanced.Scripts; using UOSScript = RazorEnhanced.UOScript.Script; -using System.Text.RegularExpressions; -using System.Collections.Concurrent; -using System.Linq; -using RazorEnhanced.UOScript; -using System.Windows.Forms; -using System.Threading.Tasks; -using System.Management.Instrumentation; -using Microsoft.Scripting.Utils; -using System.Diagnostics.Tracing; namespace RazorEnhanced { @@ -155,6 +152,8 @@ public class EnhancedScript public event Action OnStop; public event Action OnError; public event Action OnOutput; + + public static ScriptLanguage ExtToLanguage(string extenstion) { @@ -639,6 +638,22 @@ internal bool IsRunning } } + public bool Suspended + { + get { return ScriptEngine.Suspended; } + set { ScriptEngine.Suspended = value; } + } + public void Suspend() + { + ScriptEngine.Suspend(); + } + + public void Reseume() + { + ScriptEngine.Reseume(); + } + + } // --------------------------------------------- ENHANCED SCRIPT ENGINE ---------------------------------------------------- @@ -655,16 +670,18 @@ public class EnhancedScriptEngine public Assembly csProgram; public UOSScript uosProgram; - public TracebackDelegate pyTraceback; - public Action m_StdoutWriter; - public Action m_StderrWriter; - + private TracebackDelegate m_pyTraceback; + private Action m_stdoutWriter; + private Action m_stderrWriter; + private bool m_Suspended = false; + private ManualResetEvent m_SuspendedMutex; - public EnhancedScriptEngine(EnhancedScript script, bool autoLoad = true) { + m_SuspendedMutex = new ManualResetEvent(!m_Suspended); + m_Script = script; var lang = script.SetLanguage(); if (autoLoad && lang != ScriptLanguage.UNKNOWN) @@ -673,18 +690,49 @@ public EnhancedScriptEngine(EnhancedScript script, bool autoLoad = true) } } + public bool Suspended + { + get { return m_Suspended; } + set { + if (value) { Suspend();} + else { Reseume(); } + } + } + public void Suspend() + { + if (m_Script.Language != ScriptLanguage.PYTHON) { + Misc.SendMessage("WARNING: Script Suspend is supported only by python scripts."); + return; + } + m_Suspended = true; + m_SuspendedMutex.Reset(); + } + + public void Reseume() + { + if (m_Script.Language != ScriptLanguage.PYTHON) + { + Misc.SendMessage("WARNING: Script Resume is supported only by python scripts."); + return; + } + m_Suspended = false; + m_SuspendedMutex.Set(); + } + + + public void SetTracebackPython(TracebackDelegate traceFunc) { - pyTraceback = traceFunc; + m_pyTraceback = traceFunc; } public void SetStdout(Action stdoutWriter) { - m_StdoutWriter = stdoutWriter; + m_stdoutWriter = stdoutWriter; } public void SetStderr(Action stderrWriter) { - m_StderrWriter = stderrWriter; + m_stderrWriter = stderrWriter; } @@ -692,17 +740,17 @@ public void SendOutput(string message) { //Misc.SendMessage(message); SendMessageScriptError(message); - if (m_StdoutWriter != null) { - m_StdoutWriter(message); + if (m_stdoutWriter != null) { + m_stdoutWriter(message); } } public void SendError(string message) { SendMessageScriptError(message, 138); - if (m_StderrWriter != null) { - m_StderrWriter(message); - } else if (m_StdoutWriter != null) { - m_StdoutWriter(message); + if (m_stderrWriter != null) { + m_stderrWriter(message); + } else if (m_stdoutWriter != null) { + m_stdoutWriter(message); } } @@ -797,6 +845,20 @@ private bool LoadPython() return true; } + private TracebackDelegate TracebackPython(TraceBackFrame frame, string result, object payload) { + if (m_pyTraceback != null) + { + m_pyTraceback = m_pyTraceback.Invoke(frame, result, payload); + } + + if (Suspended) + { + m_SuspendedMutex.WaitOne(); + } + return TracebackPython; + } + + private bool RunPython() @@ -811,24 +873,21 @@ private bool RunPython() m_Script.LastModified = lastModified; } - if (pyTraceback != null) - { - pyEngine.Engine.SetTrace(pyTraceback); - } - + pyEngine.SetTrace(TracebackPython); + pyEngine.SetStderr( (string message) => { Misc.SendMessage(message,178); - if (m_StderrWriter == null) return; - m_StderrWriter.Invoke(message); + if (m_stderrWriter == null) return; + m_stderrWriter.Invoke(message); } ); pyEngine.SetStdout( (string message) => { Misc.SendMessage(message); - if (m_StdoutWriter == null) return; - m_StdoutWriter.Invoke(message); + if (m_stdoutWriter == null) return; + m_stdoutWriter.Invoke(message); } ); diff --git a/Razor/RazorEnhanced/Misc.cs b/Razor/RazorEnhanced/Misc.cs index aff159e3..ea0c94be 100644 --- a/Razor/RazorEnhanced/Misc.cs +++ b/Razor/RazorEnhanced/Misc.cs @@ -1062,9 +1062,10 @@ public static void ScriptRun(string scriptfile) { script.Start(); } - else + else { Scripts.SendMessageScriptError("ScriptRun: Script not exist"); - } + } + } /// /// Stop a script by file name, Script must be present in script grid. @@ -1087,18 +1088,73 @@ public static void ScriptStop(string scriptfile) /// Stop all script running. /// /// True: All all scripts but the current one - False: stop all scripts. (Dafault: false) - public static void ScriptStopAll(bool skipCurrent=false) + public static void ScriptStopAll(bool skipCurrent = false) { EnhancedScript currentScript = EnhancedScript.Service.CurrentScript(); - foreach (EnhancedScript script in EnhancedScript.Service.ScriptList() ) + foreach (EnhancedScript script in EnhancedScript.Service.ScriptList()) { - if ( skipCurrent && currentScript == script) { - continue; + if (skipCurrent && currentScript == script) + { + continue; } script.Stop(); } } + // Script function + /// + /// Suspend a script by file name, Script must be present in script grid. + /// + /// Name of the script. + public static void ScriptSuspend(string scriptfile) + { + EnhancedScript script = EnhancedScript.Service.Search(scriptfile); + if (script != null) + { + script.Suspend(); + } + else { + Scripts.SendMessageScriptError("ScriptRun: Script not exist"); + } + } + + /// + /// Resume a script by file name, Script must be present in script grid. + /// + /// Name of the script. + public static void ScriptResume(string scriptfile) + { + EnhancedScript script = EnhancedScript.Service.Search(scriptfile); + if (script != null) + { + script.Reseume(); + } + else + { + Scripts.SendMessageScriptError("ScriptResume: Script not exist"); + } + } + + + /// + /// Get status of script if is suspended or not, Script must be present in script grid. + /// + /// + /// True: Script is suspended - False: otherwise. + public static bool ScriptIsSuspended(string scriptfile) + { + EnhancedScript script = EnhancedScript.Service.Search(scriptfile); + if (script != null) + { + return script.Suspended; + } + else + { + Scripts.SendMessageScriptError("ScriptIsSuspended: Script not exist"); + return false; + } + } + /// /// Get status of script if running or not, Script must be present in script grid. /// diff --git a/Razor/RazorEnhanced/PythonEngine.cs b/Razor/RazorEnhanced/PythonEngine.cs index 106939e0..1a28e0e9 100644 --- a/Razor/RazorEnhanced/PythonEngine.cs +++ b/Razor/RazorEnhanced/PythonEngine.cs @@ -103,6 +103,11 @@ public PythonEngine() { } + public void SetTrace(TracebackDelegate tracebackDelegate) + { + Engine.SetTrace(tracebackDelegate); + } + public void SetStdout(Action stdoutWriter) { PythonWriter outputWriter = new PythonWriter(stdoutWriter);