Plugins loaded dynamically, some more bug fixes

This commit is contained in:
RaidMax 2015-08-22 11:41:14 -05:00
parent d42a329097
commit 38c8f81051
12 changed files with 69 additions and 19 deletions

View File

@ -189,22 +189,24 @@ namespace IW4MAdmin
else
{
int count = 0;
String _commands = String.Empty;
StringBuilder helpResponse = new StringBuilder();
List<Command> test = E.Owner.getCommands();
foreach (Command C in E.Owner.getCommands())
foreach (Command C in test)
{
if (E.Origin.Level >= C.Permission)
{
_commands = _commands + " [^3" + C.Name + "^7] ";
helpResponse.Append(" [^3" + C.Name + "^7] ");
if (count >= 4)
{
E.Origin.Tell(_commands);
_commands = String.Empty;
E.Origin.Tell(helpResponse.ToString());
helpResponse = new StringBuilder();
count = 0;
}
count++;
}
}
E.Origin.Tell(helpResponse.ToString());
E.Origin.Tell("Type !help <cmd> to get command usage example");
}
}

View File

@ -8,14 +8,12 @@ namespace IW4MAdmin
{
public class PluginImporter
{
public static List<Command> potentialCommands;
public static List<Plugin> potentialNotifies;
public static List<Command> potentialCommands = new List<Command>();
public static List<Plugin> potentialPlugins = new List<Plugin>();
public static bool Load()
{
string[] dllFileNames = null;
potentialCommands = new List<Command>();
potentialNotifies = new List<Plugin>();
if (Directory.Exists(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\plugins"))
dllFileNames = Directory.GetFiles(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\plugins", "*.dll");
@ -35,8 +33,8 @@ namespace IW4MAdmin
ICollection<Assembly> assemblies = new List<Assembly>(dllFileNames.Length);
foreach (string dllFile in dllFileNames)
{
AssemblyName an = AssemblyName.GetAssemblyName(dllFile);
Assembly assembly = Assembly.Load(an);
byte[] rawDLL = File.ReadAllBytes(dllFile); // because we want to update the plugin without restarting
Assembly assembly = Assembly.Load(rawDLL);
assemblies.Add(assembly);
}
@ -52,8 +50,21 @@ namespace IW4MAdmin
{
Object notifyObject = Activator.CreateInstance(assemblyType);
Plugin newNotify = (Plugin)notifyObject;
potentialNotifies.Add(newNotify);
potentialPlugins.Add(newNotify);
try
{
newNotify.onLoad();
}
catch (Exception E)
{
Program.getManager().mainLog.Write("There was an error starting \"" + newNotify.Name + "\" plugin", Log.Level.Debug);
Program.getManager().mainLog.Write("Error Message: " + E.Message, Log.Level.Debug);
Program.getManager().mainLog.Write("Error Trace: " + E.StackTrace, Log.Level.Debug);
continue;
}
Program.getManager().mainLog.Write("Loaded plugin \"" + newNotify.Name + "\"" + " [" + newNotify.Version + "]", Log.Level.Debug);
totalLoaded++;
}
@ -73,5 +84,28 @@ namespace IW4MAdmin
Program.getManager().mainLog.Write("Loaded " + totalLoaded + " plugins.", Log.Level.Production);
return true;
}
public static void Unload()
{
foreach (Plugin P in potentialPlugins)
{
try
{
P.onUnload();
}
catch (Exception E)
{
Program.getManager().mainLog.Write("There was an error unloading \"" + P.Name + "\" plugin", Log.Level.Debug);
Program.getManager().mainLog.Write("Error Message: " + E.Message, Log.Level.Debug);
Program.getManager().mainLog.Write("Error Trace: " + E.StackTrace, Log.Level.Debug);
continue;
}
}
potentialCommands = new List<Command>();
potentialPlugins = new List<Plugin>();
}
}
}

View File

@ -428,7 +428,7 @@ namespace IW4MAdmin
{
Event curEvent = events.Peek();
processEvent(curEvent);
foreach (Plugin P in PluginImporter.potentialNotifies)
foreach (Plugin P in PluginImporter.potentialPlugins)
{
try
{
@ -884,6 +884,7 @@ namespace IW4MAdmin
initMaps();
initMessages();
initRules();
PluginImporter.Unload();
PluginImporter.Load();
return true;
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -118,6 +118,11 @@ namespace SamplePlugin
playerStats = new StatsDB("stats.rm");
}
public override void onUnload()
{
}
public override string Name
{
get { return "Basic Stats"; }

View File

@ -130,22 +130,22 @@ namespace SharedLibrary
public void Kick(String Message)
{
currentServer.Kick(Message, this);
lastEvent.Owner.Kick(Message, this);
}
public void tempBan(String Message)
{
currentServer.tempBan(Message, this);
lastEvent.Owner.tempBan(Message, this);
}
public void Ban(String Message, Player Sender)
{
currentServer.Ban(Message, this, Sender);
lastEvent.Owner.Ban(Message, this, Sender);
}
public void Alert()
{
currentServer.Alert(this);
lastEvent.Owner.Alert(this);
}
public String Name { get; private set; }

View File

@ -8,6 +8,7 @@ namespace SharedLibrary
public abstract class Plugin
{
public abstract void onLoad();
public abstract void onUnload();
public abstract void onEvent(Event E);
//for logging purposes

View File

@ -7,6 +7,7 @@ namespace Webfront_Plugin
public class Webfront : Plugin
{
private static Manager webManager;
private static Thread webManagerThread;
public override void onEvent(Event E)
{
@ -28,12 +29,18 @@ namespace Webfront_Plugin
public override void onLoad()
{
webManager = new Manager();
Thread webManagerThread = new Thread(new ThreadStart(webManager.Init));
webManagerThread = new Thread(new ThreadStart(webManager.Init));
webManagerThread.Name = "Webfront";
webManagerThread.Start();
}
public override void onUnload()
{
webManager.webScheduler.Stop();
webManagerThread.Join();
}
public override String Name
{
get { return "Webfront"; }