Skip to content

Commit

Permalink
Merge pull request #10 from cesare-montresor/UOSEnginePlus
Browse files Browse the repository at this point in the history
UOSteamEngine first refactoring
  • Loading branch information
cesare-montresor authored Jan 2, 2024
2 parents fe2faea + cf3497d commit 11b509c
Show file tree
Hide file tree
Showing 5 changed files with 2,419 additions and 1,882 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.103")]
[assembly: AssemblyVersion("0.8.2.104")]

//
// In order to sign your assembly you must specify a key to use. Refer to the
Expand Down
63 changes: 36 additions & 27 deletions Razor/RazorEnhanced/EnhancedScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Providers;
using Microsoft.Scripting.Utils;
using RazorEnhanced.UOScript;
using RazorEnhanced.UOS;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand All @@ -19,7 +19,6 @@
using System.Threading;
using System.Windows.Forms;
using static RazorEnhanced.Scripts;
using UOSScript = RazorEnhanced.UOScript.Script;

namespace RazorEnhanced
{
Expand Down Expand Up @@ -668,9 +667,9 @@ public class EnhancedScriptEngine
public UOSteamEngine uosEngine;

public Assembly csProgram;
public UOSScript uosProgram;

private TracebackDelegate m_pyTraceback;
private UOSTracebackDelegate m_uosTraceback;
private Action<string> m_stdoutWriter;
private Action<string> m_stderrWriter;

Expand Down Expand Up @@ -719,13 +718,18 @@ public void Reseume()
m_SuspendedMutex.Set();
}



public void SetTracebackPython(TracebackDelegate traceFunc)
{
m_pyTraceback = traceFunc;
}

public void SetTracebackUOS(UOSTracebackDelegate traceFunc)
{
m_uosTraceback = traceFunc;
}

public void SetStdout(Action<string> stdoutWriter)
{
m_stdoutWriter = stdoutWriter;
Expand Down Expand Up @@ -790,8 +794,15 @@ public bool Load()
public bool Run()
{
if (!m_Loaded && !Load()) { return false; } // not loaded, and fail automatic loading.
bool result;
DateTime lastModified = File.GetLastWriteTime(m_Script.Fullpath);
if (m_Script.LastModified < lastModified)
{
Load();
// FileChangeDate update must be the last line of threads will messup (ex: mousewheel hotkeys)
m_Script.LastModified = lastModified;
}

bool result;
try {
switch (m_Script.Language) {
default:
Expand Down Expand Up @@ -845,34 +856,34 @@ private bool LoadPython()
return true;
}

private TracebackDelegate TracebackPython(TraceBackFrame frame, string result, object payload) {
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();
}
SuspendCheck();
return TracebackPython;
}




private bool TracebackUOS(UOS.Script script, UOS.ASTNode node, UOS.Scope scope)
{
if (m_uosTraceback != null)
{
return m_uosTraceback.Invoke(script, node, scope);
}
SuspendCheck();
return true;
}
private void SuspendCheck() {
if (Suspended) m_SuspendedMutex.WaitOne();
}

private bool RunPython()
{
try
{
DateTime lastModified = File.GetLastWriteTime(m_Script.Fullpath);
if (m_Script.LastModified < lastModified)
{
LoadPython();
// FileChangeDate update must be the last line of threads will messup (ex: mousewheel hotkeys)
m_Script.LastModified = lastModified;
}

pyEngine.SetTrace(TracebackPython);

pyEngine.SetStderr(
Expand Down Expand Up @@ -950,10 +961,9 @@ private bool RunCSharp()

private bool LoadUOSteam()
{
uosProgram = null;
try
{
uosEngine = UOSteamEngine.Instance;
uosEngine = new UOSteamEngine();
// Using // only will be deprecated instead of //UOS
var text = System.IO.File.ReadAllLines(m_Script.Fullpath);
if ((text[0].Substring(0, 2) == "//") && text[0].Length < 5)
Expand All @@ -962,21 +972,20 @@ private bool LoadUOSteam()
SendOutput(message);
}

uosProgram = uosEngine.Load(m_Script.Fullpath);
return uosEngine.Load(m_Script.Fullpath);
}
catch (Exception ex)
{
return HandleException(ex);
}
return uosProgram != null;

}

private bool RunUOSteam()
{
try
{
uosEngine.Execute(uosProgram);
uosEngine.OnTraceback(TracebackUOS);
uosEngine.Execute();
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion Razor/RazorEnhanced/Scripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Threading;
using System.Windows.Forms;
using System.Threading.Tasks;
using RazorEnhanced.UOScript;
using RazorEnhanced.UOS;
using Accord.Math;

namespace RazorEnhanced
Expand Down
23 changes: 11 additions & 12 deletions Razor/RazorEnhanced/UI/EnhancedScriptEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using System.Text.RegularExpressions;
using Accord.Math;
using Microsoft.Scripting.Utils;
using RazorEnhanced.UOScript;
using RazorEnhanced.UOS;
using System.Threading.Tasks;
using CUO_API;
using IronPython.Runtime.Operations;
Expand Down Expand Up @@ -96,8 +96,8 @@ public static EnhancedScriptEditor Search(string fullpath)

internal static void Init(string filename)
{
string suffix = null;
if (filename == null)
string suffix = Path.GetExtension(filename); ;
if (suffix == null)
{
ScriptListView scriptListView = MainForm.GetCurrentAllScriptsTab();
if (scriptListView != null)
Expand All @@ -110,10 +110,6 @@ internal static void Init(string filename)
suffix = ".cs";
}
}
else
{
suffix = Path.GetExtension(filename);
}

var editor = EnhancedScriptEditor.Search(filename);
if (editor == null) {
Expand Down Expand Up @@ -252,7 +248,7 @@ public void LoadLanguage(ScriptLanguage language = ScriptLanguage.UNKNOWN) {
case ScriptLanguage.UOSTEAM:
fastColoredTextBoxEditor.Language = FastColoredTextBoxNS.Language.Uos;
fastColoredTextBoxEditor.AutoIndentExistingLines = true;
InitUOSSyntaxHighlight();
UpdateSyntaxHighlight();
break;
}
}
Expand All @@ -276,10 +272,13 @@ private void UpdateTitle()
}


public void InitUOSSyntaxHighlight()
public void UpdateSyntaxHighlight()
{
// keywords
List<String> keywords = UOSteamEngine.Instance.AllKeywords();
var uosEngine = m_Script.ScriptEngine.uosEngine;
if (uosEngine == null) return;

List<String> keywords = uosEngine.AllKeywords();
string[] syntax =
{
"and", "break", "continue", "elif", "else", "for", "if", "not", "or", "while", "true", "false",
Expand All @@ -290,7 +289,7 @@ public void InitUOSSyntaxHighlight()
this.fastColoredTextBoxEditor.SyntaxHighlighter.UosKeywordRegex = new Regex(pattern, RegexOptions.Compiled);

// attributes
List<String> aliases = UOSteamEngine.Instance.AllAliases();
List<String> aliases = uosEngine.AllAliases();
pattern = $@"\b({String.Join("|", aliases)})\b";
this.fastColoredTextBoxEditor.SyntaxHighlighter.UosAttributeRegex = new Regex(pattern, RegexOptions.Compiled|RegexOptions.IgnoreCase);

Expand All @@ -310,7 +309,7 @@ public void InitUOSSyntaxHighlight()

//
ToolTipDescriptions tooltip;
var methods = UOSteamEngine.Instance.AllKeywords().ToArray();
var methods = keywords.ToArray();
var autodocMethods = new Dictionary<string, ToolTipDescriptions>();
foreach (var method in keywords)
{
Expand Down
Loading

0 comments on commit 11b509c

Please sign in to comment.