Skip to content

Commit

Permalink
Merge pull request #8 from cesare-montresor/ScriptEngine
Browse files Browse the repository at this point in the history
Added suspend/resume for Scripts
  • Loading branch information
cesare-montresor authored Dec 29, 2023
2 parents 6b5d408 + 20788de commit 42cb7a2
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Razor/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
131 changes: 95 additions & 36 deletions Razor/RazorEnhanced/EnhancedScript.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -155,6 +152,8 @@ public class EnhancedScript
public event Action<EnhancedScript> OnStop;
public event Action<EnhancedScript, string> OnError;
public event Action<EnhancedScript, string> OnOutput;



public static ScriptLanguage ExtToLanguage(string extenstion)
{
Expand Down Expand Up @@ -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 ----------------------------------------------------
Expand All @@ -655,16 +670,18 @@ public class EnhancedScriptEngine
public Assembly csProgram;
public UOSScript uosProgram;

public TracebackDelegate pyTraceback;
public Action<string> m_StdoutWriter;
public Action<string> m_StderrWriter;

private TracebackDelegate m_pyTraceback;
private Action<string> m_stdoutWriter;
private Action<string> 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)
Expand All @@ -673,36 +690,67 @@ 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<string> stdoutWriter)
{
m_StdoutWriter = stdoutWriter;
m_stdoutWriter = stdoutWriter;
}
public void SetStderr(Action<string> stderrWriter)
{
m_StderrWriter = stderrWriter;
m_stderrWriter = stderrWriter;
}


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);
}
}

Expand Down Expand Up @@ -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()
Expand All @@ -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);
}
);

Expand Down
68 changes: 62 additions & 6 deletions Razor/RazorEnhanced/Misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,9 +1062,10 @@ public static void ScriptRun(string scriptfile)
{
script.Start();
}
else
else {
Scripts.SendMessageScriptError("ScriptRun: Script not exist");
}
}
}

/// <summary>
/// Stop a script by file name, Script must be present in script grid.
Expand All @@ -1087,18 +1088,73 @@ public static void ScriptStop(string scriptfile)
/// Stop all script running.
/// </summary>
/// <param name="skipCurrent">True: All all scripts but the current one - False: stop all scripts. (Dafault: false)</param>
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
/// <summary>
/// Suspend a script by file name, Script must be present in script grid.
/// </summary>
/// <param name="scriptfile">Name of the script.</param>
public static void ScriptSuspend(string scriptfile)
{
EnhancedScript script = EnhancedScript.Service.Search(scriptfile);
if (script != null)
{
script.Suspend();
}
else {
Scripts.SendMessageScriptError("ScriptRun: Script not exist");
}
}

/// <summary>
/// Resume a script by file name, Script must be present in script grid.
/// </summary>
/// <param name="scriptfile">Name of the script.</param>
public static void ScriptResume(string scriptfile)
{
EnhancedScript script = EnhancedScript.Service.Search(scriptfile);
if (script != null)
{
script.Reseume();
}
else
{
Scripts.SendMessageScriptError("ScriptResume: Script not exist");
}
}


/// <summary>
/// Get status of script if is suspended or not, Script must be present in script grid.
/// </summary>
/// <param name="scriptfile"></param>
/// <returns>True: Script is suspended - False: otherwise.</returns>
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;
}
}

/// <summary>
/// Get status of script if running or not, Script must be present in script grid.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions Razor/RazorEnhanced/PythonEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public PythonEngine() {

}

public void SetTrace(TracebackDelegate tracebackDelegate)
{
Engine.SetTrace(tracebackDelegate);
}

public void SetStdout(Action<string> stdoutWriter)
{
PythonWriter outputWriter = new PythonWriter(stdoutWriter);
Expand Down

0 comments on commit 42cb7a2

Please sign in to comment.