Move plugin loading from disc stuff into IScripting

This commit is contained in:
UnknownShadow200 2021-03-25 19:50:57 +11:00
parent 0c26ffb88a
commit 2f1d731a8f
8 changed files with 61 additions and 62 deletions

View File

@ -31,7 +31,7 @@ namespace MCGalaxy.Commands.Scripting {
p.Message("That command is already loaded!"); return;
}
string path = IScripting.DllPath(cmdName);
string path = IScripting.CommandPath(cmdName);
if (!File.Exists(path)) {
p.Message("File &9{0} &Snot found.", path); return;
}

View File

@ -41,7 +41,7 @@ namespace MCGalaxy.Commands.Scripting {
}
string srcPath = engine.SourcePath(args[0]);
string dstPath = IScripting.DllPath(args[0]);
string dstPath = IScripting.CommandPath(args[0]);
if (!File.Exists(srcPath)) {
p.Message("File &9{0} &Snot found.", srcPath); return;
}
@ -51,7 +51,7 @@ namespace MCGalaxy.Commands.Scripting {
p.Message("Command compiled successfully.");
} else {
ICompiler.SummariseErrors(results, p);
p.Message("&WCompilation error. See " + IScripting.ErrorPath + " for more information.");
p.Message("&WCompilation error. See " + ICompiler.ErrorPath + " for more information.");
}
}

View File

@ -69,20 +69,20 @@ namespace MCGalaxy.Commands.Scripting {
p.Message("Plugin compiled successfully.");
} else {
ICompiler.SummariseErrors(results, p);
p.Message("&WCompilation error. See " + IScripting.ErrorPath + " for more information.");
p.Message("&WCompilation error. See " + ICompiler.ErrorPath + " for more information.");
}
}
static void LoadPlugin(Player p, string name) {
string path = IScripting.PluginPath(name);
if (File.Exists(path)) {
if (Plugin.Load(path, false)) {
p.Message("Plugin loaded successfully.");
} else {
p.Message("&WError loading plugin. See error logs for more information.");
}
if (!File.Exists(path)) {
p.Message("File &9{0} &Snot found.", path); return;
}
if (IScripting.LoadPlugin(path, false)) {
p.Message("Plugin loaded successfully.");
} else {
p.Message("File &9{0} &Snot found.", path);
p.Message("&WError loading plugin. See error logs for more information.");
}
}

View File

@ -27,22 +27,6 @@ namespace MCGalaxy {
public abstract partial class Plugin {
internal static List<Plugin> core = new List<Plugin>();
public static List<Plugin> all = new List<Plugin>();
/// <summary> Loads all plugins from the given path. </summary>
public static bool Load(string path, bool startup) {
try {
Assembly lib = IScripting.LoadAssembly(path);
List<Plugin> plugins = IScripting.LoadTypes<Plugin>(lib);
foreach (Plugin plugin in plugins) {
if (!Load(plugin, startup)) return false;
}
return true;
} catch (Exception ex) {
Logger.LogError("Error loading plugins from " + path, ex);
return false;
}
}
public static bool Load(Plugin p, bool startup) {
try {
@ -92,13 +76,7 @@ namespace MCGalaxy {
public static void LoadAll() {
LoadCorePlugin(new CorePlugin());
LoadCorePlugin(new NotesPlugin());
if (Directory.Exists("plugins")) {
string[] files = Directory.GetFiles("plugins", "*.dll");
foreach (string path in files) { Load(path, true); }
} else {
Directory.CreateDirectory("plugins");
}
IScripting.AutoloadPlugins();
}
static void LoadCorePlugin(Plugin plugin) {

View File

@ -30,12 +30,10 @@ namespace MCGalaxy.Scripting {
public static class IScripting {
public const string AutoloadFile = "text/cmdautoload.txt";
public const string SourceDir = "extra/commands/source/";
public const string DllDir = "extra/commands/dll/";
public const string ErrorPath = "logs/errors/compiler.log";
public static string DllPath(string cmdName) { return DllDir + "Cmd" + cmdName + ".dll"; }
public static string PluginPath(string name) { return "plugins/" + name + ".dll"; }
public static string CommandPath(string name) { return DllDir + "Cmd" + name + ".dll"; }
public static string PluginPath(string name) { return "plugins/" + name + ".dll"; }
/// <summary> Constructs instances of all types which derive from T in the given assembly. </summary>
/// <returns> The list of constructed instances. </returns>
@ -67,6 +65,7 @@ namespace MCGalaxy.Scripting {
}
}
/// <summary> Loads the given assembly from disc (and associated .pdb debug data) </summary>
public static Assembly LoadAssembly(string path) {
byte[] data = File.ReadAllBytes(path);
byte[] debug = GetDebugData(path);
@ -75,12 +74,12 @@ namespace MCGalaxy.Scripting {
public static void AutoloadCommands() {
if (!File.Exists(AutoloadFile)) { File.Create(AutoloadFile); return; }
if (!File.Exists(AutoloadFile)) { File.Create(AutoloadFile); return; }
string[] list = File.ReadAllLines(AutoloadFile);
foreach (string cmdName in list) {
if (cmdName.IsCommentLine()) continue;
string path = DllPath(cmdName);
string path = CommandPath(cmdName);
string error = LoadCommands(path);
if (error != null) { Logger.Log(LogType.Warning, error); continue; }
@ -88,7 +87,7 @@ namespace MCGalaxy.Scripting {
}
}
/// <summary> Loads and registers all the commands in the given dll. </summary>
/// <summary> Loads and registers all the commands from the given .dll path. </summary>
public static string LoadCommands(string path) {
try {
Assembly lib = LoadAssembly(path);
@ -111,14 +110,38 @@ namespace MCGalaxy.Scripting {
}
return null;
}
public static void AutoloadPlugins() {
if (Directory.Exists("plugins")) {
string[] files = Directory.GetFiles("plugins", "*.dll");
foreach (string path in files) { LoadPlugin(path, true); }
} else {
Directory.CreateDirectory("plugins");
}
}
/// <summary> Loads all plugins from the given .dll path. </summary>
public static bool LoadPlugin(string path, bool auto) {
try {
Assembly lib = LoadAssembly(path);
List<Plugin> plugins = IScripting.LoadTypes<Plugin>(lib);
foreach (Plugin plugin in plugins) {
if (!Plugin.Load(plugin, auto)) return false;
}
return true;
} catch (Exception ex) {
Logger.LogError("Error loading plugins from " + path, ex);
return false;
}
}
}
/// <summary> Compiles source code files from a particular language into a .dll file. </summary>
public abstract class ICompiler {
public const string AutoloadFile = "text/cmdautoload.txt";
public const string SourceDir = "extra/commands/source/";
public const string DllDir = "extra/commands/dll/";
public const string ErrorPath = "logs/errors/compiler.log";
protected CodeDomProvider compiler;
@ -143,8 +166,6 @@ namespace MCGalaxy.Scripting {
}
}
public static string DllPath(string cmdName) { return DllDir + "Cmd" + cmdName + ".dll"; }
public static string PluginPath(string name) { return "plugins/" + name + ".dll"; }
public string SourcePath(string cmdName) { return SourceDir + "Cmd" + cmdName + Ext; }
public void CreateNew(string path, string cmdName) {

View File

@ -191,7 +191,7 @@ namespace MCGalaxy {
int filenameLen = r.ReadUInt16();
int extraLen = r.ReadUInt16();
int commentLen = r.ReadUInt16();
r.ReadUInt16(); // disk number
r.ReadUInt16(); // disc number
r.ReadUInt16(); // internal attributes
r.ReadUInt32(); // external attributes
entry.LocalHeaderOffset = r.ReadUInt32();
@ -220,27 +220,27 @@ namespace MCGalaxy {
r.ReadInt64(); // zip64 end of central dir size
r.ReadUInt16(); // version
r.ReadUInt16(); // version
r.ReadUInt32(); // disk number
r.ReadUInt32(); // disk number of central directory
r.ReadUInt32(); // disc number
r.ReadUInt32(); // disc number of central directory
numEntries = (int)r.ReadInt64();
r.ReadInt64(); // num entries on disk
r.ReadInt64(); // num entries on disc
r.ReadInt64(); // central dir size
centralDirOffset = r.ReadInt64();
}
void ReadZip64EndOfCentralDirectoryLocator() {
BinaryReader r = reader;
r.ReadUInt32(); // disk number of zip64 end of central directory
r.ReadUInt32(); // disc number of zip64 end of central directory
zip64EndOffset = reader.ReadInt64();
r.ReadUInt32(); // total number of disks
r.ReadUInt32(); // total number of discs
}
void ReadEndOfCentralDirectoryRecord() {
BinaryReader r = reader;
r.ReadUInt16(); // disk number
r.ReadUInt16(); // disk number of start
r.ReadUInt16(); // disc number
r.ReadUInt16(); // disc number of start
numEntries = r.ReadUInt16();
r.ReadUInt16(); // num entries on disk
r.ReadUInt16(); // num entries on disc
r.ReadUInt32(); // cental dir size
centralDirOffset = r.ReadUInt32();
r.ReadUInt16(); // comment length

View File

@ -245,7 +245,7 @@ namespace MCGalaxy {
w.Write((ushort)entry.Filename.Length);
w.Write(extraLen);
w.Write((ushort)0); // file comment length
w.Write((ushort)0); // disk number
w.Write((ushort)0); // disc number
w.Write((ushort)0); // internal attributes
w.Write(0); // external attributes
w.Write((uint)entry.LocalHeaderOffset);
@ -275,8 +275,8 @@ namespace MCGalaxy {
w.Write(zip64EndDataSize);
w.Write(ver_zip64);
w.Write(ver_zip64);
w.Write(0); // disk number
w.Write(0); // disk number of central directory
w.Write(0); // disc number
w.Write(0); // disc number of central directory
w.Write((long)numEntries);
w.Write((long)numEntries);
w.Write(centralDirSize);
@ -286,16 +286,16 @@ namespace MCGalaxy {
void WriteZip64EndOfCentralDirectoryLocator() {
BinaryWriter w = writer;
w.Write(ZipEntry.SigZip64Loc);
w.Write(0); // disk number of zip64 end of central directory
w.Write(0); // disc number of zip64 end of central directory
w.Write(zip64EndOffset);
w.Write(1); // total number of disks
w.Write(1); // total number of discs
}
void WriteEndOfCentralDirectoryRecord() {
BinaryWriter w = writer;
w.Write(ZipEntry.SigEnd);
w.Write((ushort)0); // disk number
w.Write((ushort)0); // disk number of start
w.Write((ushort)0); // disc number
w.Write((ushort)0); // disc number of start
w.Write((ushort)numEntries);
w.Write((ushort)numEntries);
w.Write((uint)centralDirSize);

View File

@ -150,7 +150,7 @@ namespace MCGalaxy {
EnsureDirectoryExists(Paths.ImportsDir);
EnsureDirectoryExists("blockdefs");
EnsureDirectoryExists(IScripting.DllDir);
EnsureDirectoryExists(IScripting.SourceDir);
EnsureDirectoryExists(ICompiler.SourceDir);
}
static void EnsureDirectoryExists(string dir) {