better extension management.
This commit is contained in:
parent
c3fa404daa
commit
5b518175e9
|
|
@ -5,6 +5,7 @@ using Microsoft.CSharp;
|
|||
using System.CodeDom.Compiler;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
#endregion
|
||||
|
||||
// based on https://stackoverflow.com/questions/137933/what-is-the-best-scripting-language-to-embed-in-a-c-sharp-desktop-application
|
||||
|
|
@ -15,12 +16,142 @@ namespace Novetus.Core
|
|||
{
|
||||
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 string Author() { return GlobalVars.UserConfiguration.PlayerName; }
|
||||
public virtual string FullInfoString() { return (Name() + " v" + Version() + " by " + Author()); }
|
||||
public virtual void OnExtensionLoad() { }
|
||||
public virtual void OnExtensionUnload() { }
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ExtensionManager
|
||||
public class ExtensionManager
|
||||
{
|
||||
private List<IExtension> ExtensionList = new List<IExtension>();
|
||||
private string directory = "";
|
||||
|
||||
public ExtensionManager()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual List<IExtension> GetExtensionList()
|
||||
{
|
||||
return ExtensionList;
|
||||
}
|
||||
|
||||
public virtual void LoadExtensions(string dirPath)
|
||||
{
|
||||
string nothingFoundError = "No extensions found.";
|
||||
|
||||
if (!Directory.Exists(dirPath))
|
||||
{
|
||||
Util.ConsolePrint(nothingFoundError, 5);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
directory = dirPath;
|
||||
}
|
||||
|
||||
// load up all .cs files.
|
||||
string[] filePaths = Directory.GetFiles(dirPath, "*.cs", SearchOption.TopDirectoryOnly);
|
||||
|
||||
if (filePaths.Count() == 0)
|
||||
{
|
||||
Util.ConsolePrint(nothingFoundError, 5);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (string file in filePaths)
|
||||
{
|
||||
int index = 0;
|
||||
|
||||
try
|
||||
{
|
||||
IExtension newExt = (IExtension)Script.LoadScriptFromContent(file);
|
||||
ExtensionList.Add(newExt);
|
||||
index = ExtensionList.IndexOf(newExt);
|
||||
Util.ConsolePrint("Loaded extension " + newExt.FullInfoString() + " from " + Path.GetFileName(file), 3);
|
||||
newExt.OnExtensionLoad();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Util.ConsolePrint("Failed to load script " + Path.GetFileName(file), 2);
|
||||
ExtensionList.RemoveAt(index);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void ReloadExtensions()
|
||||
{
|
||||
string nothingFoundError = "No extensions found. There is nothing to reload.";
|
||||
|
||||
if (!ExtensionList.Any())
|
||||
{
|
||||
Util.ConsolePrint(nothingFoundError, 5);
|
||||
return;
|
||||
}
|
||||
|
||||
Util.ConsolePrint("Reloading Extensions...", 2);
|
||||
|
||||
UnloadExtensions();
|
||||
LoadExtensions(directory);
|
||||
}
|
||||
|
||||
public virtual void UnloadExtensions()
|
||||
{
|
||||
string nothingFoundError = "No extensions found. There is nothing to unload.";
|
||||
|
||||
if (!ExtensionList.Any())
|
||||
{
|
||||
Util.ConsolePrint(nothingFoundError, 5);
|
||||
return;
|
||||
}
|
||||
|
||||
Util.ConsolePrint("Unloading all Extensions...", 2);
|
||||
|
||||
foreach (IExtension extension in ExtensionList.ToArray())
|
||||
{
|
||||
try
|
||||
{
|
||||
extension.OnExtensionUnload();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
ExtensionList.Clear();
|
||||
}
|
||||
|
||||
public virtual string GenerateExtensionList()
|
||||
{
|
||||
string nothingFoundError = "No extensions found.";
|
||||
|
||||
if (!ExtensionList.Any())
|
||||
{
|
||||
return nothingFoundError;
|
||||
}
|
||||
|
||||
string result = "";
|
||||
|
||||
foreach (IExtension extension in ExtensionList.ToArray())
|
||||
{
|
||||
try
|
||||
{
|
||||
result += "- " + extension.FullInfoString() + "\n";
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
result.Trim();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Script
|
||||
public class Script
|
||||
{
|
||||
|
|
|
|||
|
|
@ -30,49 +30,9 @@ namespace Novetus.Core
|
|||
|
||||
public class WebProxy
|
||||
{
|
||||
private static List<IWebProxyExtension> ExtensionList = new List<IWebProxyExtension>();
|
||||
private static ProxyServer Server = new ProxyServer();
|
||||
private static ExplicitProxyEndPoint end;
|
||||
|
||||
public void LoadExtensions()
|
||||
{
|
||||
string nothingFoundError = "No extensions found. The Web Proxy will run with limited functionality.";
|
||||
|
||||
if (!Directory.Exists(GlobalPaths.NovetusExtsWebProxy))
|
||||
{
|
||||
Util.ConsolePrint(nothingFoundError, 5);
|
||||
return;
|
||||
}
|
||||
|
||||
// load up all .cs files.
|
||||
string[] filePaths = Directory.GetFiles(GlobalPaths.NovetusExtsWebProxy, "*.cs", SearchOption.TopDirectoryOnly);
|
||||
|
||||
if (filePaths.Count() == 0)
|
||||
{
|
||||
Util.ConsolePrint(nothingFoundError, 5);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (string file in filePaths)
|
||||
{
|
||||
int index = 0;
|
||||
|
||||
try
|
||||
{
|
||||
IWebProxyExtension newExt = (IWebProxyExtension)Script.LoadScriptFromContent(file);
|
||||
ExtensionList.Add(newExt);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
private ProxyServer Server = null;
|
||||
private ExplicitProxyEndPoint end;
|
||||
public ExtensionManager Manager = new ExtensionManager();
|
||||
|
||||
public bool HasStarted()
|
||||
{
|
||||
|
|
@ -117,6 +77,8 @@ namespace Novetus.Core
|
|||
|
||||
public void Start()
|
||||
{
|
||||
Server = new ProxyServer();
|
||||
|
||||
if (Server.ProxyRunning)
|
||||
{
|
||||
Util.ConsolePrint("The web proxy is already on and running.", 2);
|
||||
|
|
@ -125,7 +87,8 @@ namespace Novetus.Core
|
|||
|
||||
try
|
||||
{
|
||||
LoadExtensions();
|
||||
Manager.LoadExtensions(GlobalPaths.NovetusExtsWebProxy);
|
||||
Util.ConsolePrint("Booting up Web Proxy...", 3);
|
||||
Server.CertificateManager.RootCertificateIssuerName = "Novetus";
|
||||
Server.CertificateManager.RootCertificateName = "Novetus Web Proxy";
|
||||
Server.BeforeRequest += new AsyncEventHandler<SessionEventArgs>(OnRequest);
|
||||
|
|
@ -133,9 +96,13 @@ namespace Novetus.Core
|
|||
Util.ConsolePrint("Web Proxy started on port " + GlobalVars.WebProxyPort, 3);
|
||||
try
|
||||
{
|
||||
foreach (IWebProxyExtension extension in ExtensionList.ToArray())
|
||||
foreach (IExtension extension in Manager.GetExtensionList().ToArray())
|
||||
{
|
||||
extension.OnProxyStart();
|
||||
IWebProxyExtension webProxyExtension = extension as IWebProxyExtension;
|
||||
if (webProxyExtension != null)
|
||||
{
|
||||
webProxyExtension.OnProxyStart();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
|
|
@ -218,22 +185,26 @@ namespace Novetus.Core
|
|||
|
||||
Uri uri = e.HttpClient.Request.RequestUri;
|
||||
|
||||
foreach (IWebProxyExtension extension in ExtensionList.ToArray())
|
||||
foreach (IExtension extension in Manager.GetExtensionList().ToArray())
|
||||
{
|
||||
if (extension.IsValidURL(uri.AbsolutePath.ToLowerInvariant(), uri.Host))
|
||||
IWebProxyExtension webProxyExtension = extension as IWebProxyExtension;
|
||||
if (webProxyExtension != null)
|
||||
{
|
||||
try
|
||||
if (webProxyExtension.IsValidURL(uri.AbsolutePath.ToLowerInvariant(), uri.Host))
|
||||
{
|
||||
await extension.OnBeforeTunnelConnectRequest(sender, e);
|
||||
try
|
||||
{
|
||||
await webProxyExtension.OnBeforeTunnelConnectRequest(sender, e);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -248,25 +219,29 @@ namespace Novetus.Core
|
|||
|
||||
Uri uri = e.HttpClient.Request.RequestUri;
|
||||
|
||||
foreach (IWebProxyExtension extension in ExtensionList.ToArray())
|
||||
foreach (IExtension extension in Manager.GetExtensionList().ToArray())
|
||||
{
|
||||
if (extension.IsValidURL(uri.AbsolutePath.ToLowerInvariant(), uri.Host))
|
||||
IWebProxyExtension webProxyExtension = extension as IWebProxyExtension;
|
||||
if (webProxyExtension != null)
|
||||
{
|
||||
try
|
||||
if (webProxyExtension.IsValidURL(uri.AbsolutePath.ToLowerInvariant(), uri.Host))
|
||||
{
|
||||
await extension.OnRequest(sender, e);
|
||||
return;
|
||||
try
|
||||
{
|
||||
await webProxyExtension.OnRequest(sender, e);
|
||||
return;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
e.GenericResponse("", HttpStatusCode.InternalServerError);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
else
|
||||
{
|
||||
e.GenericResponse("", HttpStatusCode.InternalServerError);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
e.GenericResponse("", HttpStatusCode.NotFound);
|
||||
|
|
@ -284,20 +259,25 @@ namespace Novetus.Core
|
|||
Server.BeforeRequest -= new AsyncEventHandler<SessionEventArgs>(OnRequest);
|
||||
Server.Stop();
|
||||
Server.Dispose();
|
||||
Server = null;
|
||||
|
||||
foreach (IWebProxyExtension extension in ExtensionList.ToArray())
|
||||
foreach (IExtension extension in Manager.GetExtensionList().ToArray())
|
||||
{
|
||||
try
|
||||
{
|
||||
extension.OnProxyStopped();
|
||||
extension.OnExtensionUnload();
|
||||
IWebProxyExtension webProxyExtension = extension as IWebProxyExtension;
|
||||
if (webProxyExtension != null)
|
||||
{
|
||||
webProxyExtension.OnProxyStopped();
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
ExtensionList.Clear();
|
||||
Manager.UnloadExtensions();
|
||||
Manager.GetExtensionList().Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -224,6 +224,8 @@ namespace NovetusLauncher
|
|||
|
||||
CommandBox.Text = "";
|
||||
|
||||
Util.ConsolePrint("> " + cmd, 1, true);
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case string server when server.Contains("server", StringComparison.InvariantCultureIgnoreCase) == true:
|
||||
|
|
@ -248,12 +250,15 @@ namespace NovetusLauncher
|
|||
{
|
||||
ConsoleForm.StartGame(ScriptType.Server, false, false, true);
|
||||
}
|
||||
ScrollToEnd();
|
||||
break;
|
||||
case string client when string.Compare(client, "client", true, CultureInfo.InvariantCulture) == 0:
|
||||
ConsoleForm.StartGame(ScriptType.Client);
|
||||
ScrollToEnd();
|
||||
break;
|
||||
case string solo when string.Compare(solo, "solo", true, CultureInfo.InvariantCulture) == 0:
|
||||
ConsoleForm.StartGame(ScriptType.Solo);
|
||||
ScrollToEnd();
|
||||
break;
|
||||
case string studio when studio.Contains("studio", StringComparison.InvariantCultureIgnoreCase) == true:
|
||||
try
|
||||
|
|
@ -277,6 +282,7 @@ namespace NovetusLauncher
|
|||
{
|
||||
ConsoleForm.StartGame(ScriptType.Studio, false, false, true);
|
||||
}
|
||||
ScrollToEnd();
|
||||
break;
|
||||
case string config when config.Contains("config", StringComparison.InvariantCultureIgnoreCase) == true:
|
||||
try
|
||||
|
|
@ -304,15 +310,18 @@ namespace NovetusLauncher
|
|||
{
|
||||
Util.ConsolePrint("Please specify 'save', 'load', or 'reset'.", 2);
|
||||
}
|
||||
ScrollToEnd();
|
||||
break;
|
||||
case string help when string.Compare(help, "help", true, CultureInfo.InvariantCulture) == 0:
|
||||
ConsoleHelp();
|
||||
break;
|
||||
case string documentation when string.Compare(documentation, "documentation", true, CultureInfo.InvariantCulture) == 0:
|
||||
ClientScriptDoc();
|
||||
ScrollToEnd();
|
||||
break;
|
||||
case string sdk when string.Compare(sdk, "sdk", true, CultureInfo.InvariantCulture) == 0:
|
||||
ConsoleForm.LoadLauncher();
|
||||
ScrollToEnd();
|
||||
break;
|
||||
case string dlldelete when string.Compare(dlldelete, "dlldelete", true, CultureInfo.InvariantCulture) == 0:
|
||||
if (GlobalVars.UserConfiguration.DisableReshadeDelete == true)
|
||||
|
|
@ -325,6 +334,7 @@ namespace NovetusLauncher
|
|||
GlobalVars.UserConfiguration.DisableReshadeDelete = true;
|
||||
Util.ConsolePrint("ReShade DLL deletion disabled.", 4);
|
||||
}
|
||||
ScrollToEnd();
|
||||
break;
|
||||
case string altip when altip.Contains("altip", StringComparison.InvariantCultureIgnoreCase) == true:
|
||||
try
|
||||
|
|
@ -346,6 +356,7 @@ namespace NovetusLauncher
|
|||
{
|
||||
Util.ConsolePrint("Please specify the IP address you would like to set Novetus to. Type 'none' to disable this.", 2);
|
||||
}
|
||||
ScrollToEnd();
|
||||
break;
|
||||
case string clear when clear.Contains("clear", StringComparison.InvariantCultureIgnoreCase) == true:
|
||||
ClearConsole();
|
||||
|
|
@ -354,11 +365,13 @@ namespace NovetusLauncher
|
|||
GlobalVars.AdminMode = true;
|
||||
Util.ConsolePrint("ADMIN MODE ENABLED.", 4);
|
||||
Util.ConsolePrint("YOU ARE GOD.", 2);
|
||||
ScrollToEnd();
|
||||
break;
|
||||
case string decode when (string.Compare(decode, "decode", true, CultureInfo.InvariantCulture) == 0 || string.Compare(decode, "decrypt", true, CultureInfo.InvariantCulture) == 0):
|
||||
Util.ConsolePrint("???", 2);
|
||||
Decoder de = new Decoder();
|
||||
de.Show();
|
||||
Util.ConsolePrint("???", 2);
|
||||
ScrollToEnd();
|
||||
break;
|
||||
case string proxy when proxy.Contains("proxy", StringComparison.InvariantCultureIgnoreCase) == true:
|
||||
try
|
||||
|
|
@ -374,17 +387,18 @@ namespace NovetusLauncher
|
|||
}
|
||||
else
|
||||
{
|
||||
// fast start it.
|
||||
if (!GlobalVars.UserConfiguration.WebProxyEnabled)
|
||||
{
|
||||
GlobalVars.UserConfiguration.WebProxyEnabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
GlobalVars.Proxy.Start();
|
||||
GlobalVars.Proxy.Start();
|
||||
}
|
||||
}
|
||||
else if (vals[1].Equals("off", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
if (!GlobalVars.UserConfiguration.WebProxyEnabled)
|
||||
if (!GlobalVars.Proxy.HasStarted() && !GlobalVars.UserConfiguration.WebProxyEnabled)
|
||||
{
|
||||
Util.ConsolePrint("The web proxy is disabled. Please turn it on in order to use this command.", 2);
|
||||
return;
|
||||
|
|
@ -409,6 +423,30 @@ namespace NovetusLauncher
|
|||
|
||||
Util.ConsolePrint("The web proxy has been disabled. To re-enable it, use the 'proxy on' command.", 2);
|
||||
}
|
||||
else if (vals[1].Equals("extensions", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
if (!GlobalVars.Proxy.HasStarted() && !GlobalVars.UserConfiguration.WebProxyEnabled)
|
||||
{
|
||||
Util.ConsolePrint("The web proxy is disabled. Please turn it on in order to use this command.", 2);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (vals[2].Equals("reload", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
GlobalVars.Proxy.Manager.ReloadExtensions();
|
||||
}
|
||||
else if (vals[2].Equals("list", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
Util.ConsolePrintMultiLine(GlobalVars.Proxy.Manager.GenerateExtensionList(), 3);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Util.ConsolePrint("Please specify 'reload', or 'list'.", 2);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Util.ConsolePrint("Please specify 'on', 'off', or 'disable'.", 2);
|
||||
|
|
@ -416,11 +454,13 @@ namespace NovetusLauncher
|
|||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Util.ConsolePrint("Please specify 'on' or 'off', or 'disable'.", 2);
|
||||
Util.ConsolePrint("Please specify 'on', 'off', or 'disable'.", 2);
|
||||
}
|
||||
ScrollToEnd();
|
||||
break;
|
||||
default:
|
||||
Util.ConsolePrint("Command is either not registered or valid", 2);
|
||||
ScrollToEnd();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -484,6 +524,12 @@ namespace NovetusLauncher
|
|||
ConsoleBox.ScrollToCaret();
|
||||
}
|
||||
|
||||
private void ScrollToEnd()
|
||||
{
|
||||
ConsoleBox.SelectionStart = ConsoleBox.Text.Length;
|
||||
ConsoleBox.ScrollToCaret();
|
||||
}
|
||||
|
||||
private void ConsoleClose(object sender, FormClosingEventArgs e)
|
||||
{
|
||||
CommandLineArguments.Arguments ConsoleArgs = new CommandLineArguments.Arguments(argList);
|
||||
|
|
|
|||
Loading…
Reference in New Issue