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
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
public class Script
{
@ -30,7 +41,7 @@ namespace Novetus.Core
}
catch (Exception ex)
{
ErrorHandler(scriptPath + ": " + ex.ToString(), true);
ErrorHandler(scriptPath + ": " + ex.ToString());
}
return null;
@ -56,13 +67,13 @@ namespace Novetus.Core
}
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;
}
}
error:
ErrorHandler(filePath + ": Failed to load script.", true);
ErrorHandler(filePath + ": Failed to load script.");
return null;
}
@ -81,7 +92,7 @@ error:
foreach (CompilerError error in result.Errors)
{
ErrorHandler(error, filePath, error.IsWarning);
ErrorHandler(error, filePath);
}
if (result.Errors.HasErrors)
@ -92,19 +103,14 @@ error:
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);
}
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);
Util.ConsolePrint("[SCRIPT ERROR] - " + fileName + " (" + error.Line + "," + error.Column + "): " + error.ErrorText, 2);
}
}
#endregion

View File

@ -14,10 +14,8 @@ using Titanium.Web.Proxy.Models;
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 OnProxyStopped() { }
@ -54,16 +52,21 @@ namespace Novetus.Core
foreach (string file in filePaths)
{
int index = 0;
try
{
IWebProxyExtension newExt = (IWebProxyExtension)Script.LoadScriptFromContent(file);
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();
}
catch (Exception)
{
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();
}
}
catch (Exception ex)
catch (Exception)
{
Script.ErrorHandler(ex.Message);
}
}
catch (Exception e)
@ -215,9 +217,8 @@ namespace Novetus.Core
{
await extension.OnBeforeTunnelConnectRequest(sender, e);
}
catch (Exception ex)
catch (Exception)
{
Script.ErrorHandler(ex.Message);
}
}
else
@ -247,9 +248,8 @@ namespace Novetus.Core
await extension.OnRequest(sender, e);
return;
}
catch (Exception ex)
catch (Exception)
{
Script.ErrorHandler(ex.Message);
e.GenericResponse("", HttpStatusCode.InternalServerError);
return;
}
@ -270,10 +270,10 @@ namespace Novetus.Core
try
{
extension.OnProxyStopped();
extension.OnExtensionUnload();
}
catch (Exception ex)
catch (Exception)
{
Script.ErrorHandler(ex.Message);
}
}