diff --git a/Novetus/NovetusCore/Classes/Script.cs b/Novetus/NovetusCore/Classes/Script.cs index b0caa45..e8069b2 100644 --- a/Novetus/NovetusCore/Classes/Script.cs +++ b/Novetus/NovetusCore/Classes/Script.cs @@ -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 diff --git a/Novetus/NovetusCore/Classes/WebProxy.cs b/Novetus/NovetusCore/Classes/WebProxy.cs index 89dd76e..c838b72 100644 --- a/Novetus/NovetusCore/Classes/WebProxy.cs +++ b/Novetus/NovetusCore/Classes/WebProxy.cs @@ -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); } }