move code out of /cmdload and into Scripting.cs

This commit is contained in:
UnknownShadow200 2021-09-02 12:38:52 +10:00
parent 4d268663fd
commit 750af9cb6c
3 changed files with 37 additions and 32 deletions

View File

@ -122,10 +122,6 @@ namespace MCGalaxy.Gui.Popups {
void LoadCommands(Assembly assembly) { void LoadCommands(Assembly assembly) {
List<Command> commands = IScripting.LoadTypes<Command>(assembly); List<Command> commands = IScripting.LoadTypes<Command>(assembly);
if (commands == null) {
Popup.Error("Error compiling files. Check logs for more details"); return;
}
for (int i = 0; i < commands.Count; i++) { for (int i = 0; i < commands.Count; i++) {
Command cmd = commands[i]; Command cmd = commands[i];

View File

@ -26,18 +26,11 @@ namespace MCGalaxy.Commands.Scripting {
public override bool MessageBlockRestricted { get { return true; } } public override bool MessageBlockRestricted { get { return true; } }
public override void Use(Player p, string cmdName, CommandData data) { public override void Use(Player p, string cmdName, CommandData data) {
if (!Formatter.CheckFilenameOnly(p, cmdName)) return; if (!Formatter.CheckFilenameOnly(p, cmdName)) return;
if (Command.Find(cmdName) != null) { string path = IScripting.CommandPath(cmdName);
p.Message("That command is already loaded!"); return;
}
string path = IScripting.CommandPath(cmdName);
if (!File.Exists(path)) {
p.Message("File &9{0} &Snot found.", path); return;
}
string error = IScripting.LoadCommands(path); string error = IScripting.LoadCommands(path);
if (error != null) { p.Message("&W" + error); return; }
if (error != null) { p.Message(error); return; }
p.Message("Command was successfully loaded."); p.Message("Command was successfully loaded.");
} }

View File

@ -79,40 +79,56 @@ namespace MCGalaxy.Scripting {
if (!File.Exists(AutoloadFile)) { File.Create(AutoloadFile); return; } if (!File.Exists(AutoloadFile)) { File.Create(AutoloadFile); return; }
string[] list = File.ReadAllLines(AutoloadFile); string[] list = File.ReadAllLines(AutoloadFile);
foreach (string cmdName in list) { foreach (string cmdName in list)
{
if (cmdName.IsCommentLine()) continue; if (cmdName.IsCommentLine()) continue;
string path = CommandPath(cmdName); string path = CommandPath(cmdName);
string error = LoadCommands(path); string error = LoadCommands(path);
if (error != null) { Logger.Log(LogType.Warning, error); continue; } if (error != null) {
Logger.Log(LogType.SystemActivity, "AUTOLOAD: Loaded Cmd{0}.dll", cmdName); Logger.Log(LogType.Warning, error);
} else {
Logger.Log(LogType.SystemActivity, "AUTOLOAD: Loaded Cmd{0}.dll", cmdName);
}
} }
} }
/// <summary> Loads and registers all the commands from the given .dll path. </summary> /// <summary> Loads and registers all the commands from the given .dll path </summary>
/// <returns> If an error occurred, a string describing the error </returns>
public static string LoadCommands(string path) { public static string LoadCommands(string path) {
try { try {
Assembly lib = LoadAssembly(path); Assembly lib = LoadAssembly(path);
List<Command> commands = LoadTypes<Command>(lib); List<Command> commands = LoadTypes<Command>(lib);
if (commands.Count == 0) return "&WNo commands in " + path;
if (commands.Count == 0) return "No commands in dll file"; foreach (Command cmd in commands)
foreach (Command cmd in commands) { Command.Register(cmd); } {
} catch (Exception ex) { if (Command.Find(cmd.name) != null)
Logger.LogError("Error loading commands from " + path, ex); return "/" + cmd.name + " is already loaded";
string file = Path.GetFileName(path); Command.Register(cmd);
if (ex is FileNotFoundException) {
return file + " does not exist in the DLL folder, or is missing a dependency. Details in the error log.";
} else if (ex is BadImageFormatException) {
return file + " is not a valid assembly, or has an invalid dependency. Details in the error log.";
} else if (ex is FileLoadException) {
return file + " or one of its dependencies could not be loaded. Details in the error log.";
} }
return "An unknown error occured. Details in the error log."; } catch (Exception ex) {
return DescribeLoadError(path, ex);
} }
return null; return null;
} }
static string DescribeLoadError(string path, Exception ex) {
if (ex is FileNotFoundException)
return "File &9" + path + " &Snot found.";
Logger.LogError("Error loading commands from " + path, ex);
string file = Path.GetFileName(path);
if (ex is BadImageFormatException) {
return "&W" + file + " is not a valid assembly, or has an invalid dependency. Details in the error log.";
} else if (ex is FileLoadException) {
return "&W" + file + " or one of its dependencies could not be loaded. Details in the error log.";
}
return "&WAn unknown error occured. Details in the error log.";
}
public static void AutoloadPlugins() { public static void AutoloadPlugins() {
string[] files = AtomicIO.TryGetFiles("plugins", "*.dll"); string[] files = AtomicIO.TryGetFiles("plugins", "*.dll");