script engine changes.

This commit is contained in:
Bitl 2023-01-05 21:19:37 -07:00
parent 3998afcc62
commit c3f3a26dca
2 changed files with 31 additions and 25 deletions

View File

@ -10,6 +10,17 @@ using System.Linq;
// based on https://stackoverflow.com/questions/137933/what-is-the-best-scripting-language-to-embed-in-a-c-sharp-desktop-application // based on https://stackoverflow.com/questions/137933/what-is-the-best-scripting-language-to-embed-in-a-c-sharp-desktop-application
namespace Novetus.Core namespace Novetus.Core
{ {
#region IExtension
public class IExtension
{
public virtual string Name() { return "Unnamed Object"; }
public virtual string Version() { return "1.0.0"; }
public virtual string FullInfoString() { return (Name() + " v" + Version()); }
public virtual void OnExtensionLoad() { }
public virtual void OnExtensionUnload() { }
}
#endregion
#region Script #region Script
public class Script public class Script
{ {
@ -30,7 +41,7 @@ namespace Novetus.Core
} }
catch (Exception ex) catch (Exception ex)
{ {
ErrorHandler(scriptPath + ": " + ex.ToString(), true); ErrorHandler(scriptPath + ": " + ex.ToString());
} }
return null; return null;
@ -56,13 +67,13 @@ namespace Novetus.Core
} }
else else
{ {
ErrorHandler(filePath + ": Constructor does not exist or it is not public.", true); ErrorHandler(filePath + ": Constructor does not exist or it is not public.");
return null; return null;
} }
} }
error: error:
ErrorHandler(filePath + ": Failed to load script.", true); ErrorHandler(filePath + ": Failed to load script.");
return null; return null;
} }
@ -81,7 +92,7 @@ error:
foreach (CompilerError error in result.Errors) foreach (CompilerError error in result.Errors)
{ {
ErrorHandler(error, filePath, error.IsWarning); ErrorHandler(error, filePath);
} }
if (result.Errors.HasErrors) if (result.Errors.HasErrors)
@ -92,19 +103,14 @@ error:
return result.CompiledAssembly; return result.CompiledAssembly;
} }
public static void ErrorHandler(string error) private static void ErrorHandler(string error)
{ {
ErrorHandler(error, false); Util.ConsolePrint("[SCRIPT ERROR] - " + error, 2);
} }
private static void ErrorHandler(string error, bool warning) private static void ErrorHandler(CompilerError error, string fileName)
{ {
Util.ConsolePrint(warning ? "[SCRIPT WARNING] - " : "[SCRIPT ERROR] - " + error, warning ? 5 : 2); Util.ConsolePrint("[SCRIPT ERROR] - " + fileName + " (" + error.Line + "," + error.Column + "): " + error.ErrorText, 2);
}
private static void ErrorHandler(CompilerError error, string fileName, bool warning)
{
Util.ConsolePrint(warning ? "[SCRIPT WARNING] - " : "[SCRIPT ERROR] - " + fileName + " (" + error.Line + "," + error.Column + "): " + error.ErrorText, warning ? 5 : 2);
} }
} }
#endregion #endregion

View File

@ -14,10 +14,8 @@ using Titanium.Web.Proxy.Models;
namespace Novetus.Core namespace Novetus.Core
{ {
public class IWebProxyExtension public class IWebProxyExtension : IExtension
{ {
public virtual string Name() { return "Unnamed Web Proxy Extension"; }
public virtual void OnExtensionLoad() { }
public virtual void OnProxyStart() { } public virtual void OnProxyStart() { }
public virtual void OnProxyStopped() { } public virtual void OnProxyStopped() { }
@ -54,16 +52,21 @@ namespace Novetus.Core
foreach (string file in filePaths) foreach (string file in filePaths)
{ {
int index = 0;
try try
{ {
IWebProxyExtension newExt = (IWebProxyExtension)Script.LoadScriptFromContent(file); IWebProxyExtension newExt = (IWebProxyExtension)Script.LoadScriptFromContent(file);
ExtensionList.Add(newExt); ExtensionList.Add(newExt);
Util.ConsolePrint("Web Proxy: Loaded extension " + newExt.Name() + " from " + Path.GetFileName(file), 3); index = ExtensionList.IndexOf(newExt);
Util.ConsolePrint("Web Proxy: Loaded extension " + newExt.FullInfoString() + " from " + Path.GetFileName(file), 3);
newExt.OnExtensionLoad(); newExt.OnExtensionLoad();
} }
catch (Exception) catch (Exception)
{ {
Util.ConsolePrint("Web Proxy: Failed to load script " + Path.GetFileName(file), 2); Util.ConsolePrint("Web Proxy: Failed to load script " + Path.GetFileName(file), 2);
ExtensionList.RemoveAt(index);
continue;
} }
} }
} }
@ -126,9 +129,8 @@ namespace Novetus.Core
extension.OnProxyStart(); extension.OnProxyStart();
} }
} }
catch (Exception ex) catch (Exception)
{ {
Script.ErrorHandler(ex.Message);
} }
} }
catch (Exception e) catch (Exception e)
@ -215,9 +217,8 @@ namespace Novetus.Core
{ {
await extension.OnBeforeTunnelConnectRequest(sender, e); await extension.OnBeforeTunnelConnectRequest(sender, e);
} }
catch (Exception ex) catch (Exception)
{ {
Script.ErrorHandler(ex.Message);
} }
} }
else else
@ -247,9 +248,8 @@ namespace Novetus.Core
await extension.OnRequest(sender, e); await extension.OnRequest(sender, e);
return; return;
} }
catch (Exception ex) catch (Exception)
{ {
Script.ErrorHandler(ex.Message);
e.GenericResponse("", HttpStatusCode.InternalServerError); e.GenericResponse("", HttpStatusCode.InternalServerError);
return; return;
} }
@ -270,10 +270,10 @@ namespace Novetus.Core
try try
{ {
extension.OnProxyStopped(); extension.OnProxyStopped();
extension.OnExtensionUnload();
} }
catch (Exception ex) catch (Exception)
{ {
Script.ErrorHandler(ex.Message);
} }
} }