From cadde3b0c4b83c500eac7dc281a4bb21136e7cd7 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 23 Jul 2022 13:57:07 +1000 Subject: [PATCH] Improve /pcreate C# example, and simplify ICompiler slightly since a full CompilerResults instance is not required --- GUI/Popups/CustomCommands.cs | 10 ++--- MCGalaxy/Modules/Compiling/Backends.cs | 43 +++++++++++++------ MCGalaxy/Modules/Compiling/Compiler.cs | 22 ++++------ .../Modules/Compiling/CompilerOperations.cs | 24 +++++------ MCGalaxy/Modules/Compiling/Roslyn.cs | 20 +++------ MCGalaxy/Scripting/Plugin.cs | 2 +- 6 files changed, 63 insertions(+), 58 deletions(-) diff --git a/GUI/Popups/CustomCommands.cs b/GUI/Popups/CustomCommands.cs index eae0bff99..eb549e745 100644 --- a/GUI/Popups/CustomCommands.cs +++ b/GUI/Popups/CustomCommands.cs @@ -145,12 +145,12 @@ namespace MCGalaxy.Gui.Popups { if (compiler == null) { Popup.Warning("Unsupported file '" + path + "'"); return null; - } + } + string tmp = "extra/commands/" + Path.GetRandomFileName() + ".dll"; - - ConsoleHelpPlayer p = new ConsoleHelpPlayer(); - CompilerResults result = CompilerOperations.Compile(p, compiler, "Command", new[] { path }, tmp); - if (result != null && !result.Errors.HasErrors) return tmp; + ConsoleHelpPlayer p = new ConsoleHelpPlayer(); + if (CompilerOperations.Compile(p, compiler, "Command", new[] { path }, tmp)) + return tmp; Popup.Error(Colors.StripUsed(p.Messages)); DeleteAssembly(tmp); diff --git a/MCGalaxy/Modules/Compiling/Backends.cs b/MCGalaxy/Modules/Compiling/Backends.cs index 78848ca38..9a844813e 100644 --- a/MCGalaxy/Modules/Compiling/Backends.cs +++ b/MCGalaxy/Modules/Compiling/Backends.cs @@ -29,22 +29,22 @@ namespace MCGalaxy.Modules.Compiling public override string ShortName { get { return "C#"; } } public override string FullName { get { return "CSharp"; } } - protected override CompilerResults DoCompile(string[] srcPaths, CompilerParameters args) { + protected override CompilerErrorCollection DoCompile(string[] srcPaths, CompilerParameters args) { args.CompilerOptions += " /unsafe"; #if NETSTANDARD return RoslynCSharpCompiler.Compile(args, srcPaths); #else InitCompiler("CSharp"); - return compiler.CompileAssemblyFromFile(args, srcPaths); + return compiler.CompileAssemblyFromFile(args, srcPaths).Errors; #endif } public override string CommandSkeleton { get { - return @"//\tAuto-generated command skeleton class. -//\tUse this as a basis for custom MCGalaxy commands. -//\tNaming should be kept consistent. (e.g. /update command should have a class name of 'CmdUpdate' and a filename of 'CmdUpdate.cs') + return @"//\tAuto-generated command skeleton class +//\tUse this as a basis for custom MCGalaxy commands +//\tNaming should be kept consistent (e.g. /update command should have a class name of 'CmdUpdate' and a filename of 'CmdUpdate.cs') // As a note, MCGalaxy is designed for .NET 4.0 // To reference other assemblies, put a ""//reference [assembly filename]"" at the top of the file @@ -92,30 +92,47 @@ public class Cmd{0} : Command public override string PluginSkeleton { get { - return @"//This is an example plugin source! + return @"//\tAuto-generated plugin skeleton class +//\tUse this as a basis for custom MCGalaxy plugins + +// To reference other assemblies, put a ""//reference [assembly filename]"" at the top of the file +// e.g. to reference the System.Data assembly, put ""//reference System.Data.dll"" + +// Add any other using statements you need after this using System; + namespace MCGalaxy {{ \tpublic class {0} : Plugin \t{{ +\t\t// The plugin's name (i.e what shows in /Plugins) \t\tpublic override string name {{ get {{ return ""{0}""; }} }} + +\t\t// The oldest version of MCGalaxy this plugin is compatible with \t\tpublic override string MCGalaxy_Version {{ get {{ return ""{2}""; }} }} + +\t\t// Message displayed in server logs when this plugin is loaded \t\tpublic override string welcome {{ get {{ return ""Loaded Message!""; }} }} + +\t\t// Who created/authored this plugin \t\tpublic override string creator {{ get {{ return ""{1}""; }} }} +\t\t// Called when this plugin is being loaded (e.g. on server startup) \t\tpublic override void Load(bool startup) \t\t{{ -\t\t\t//LOAD YOUR PLUGIN WITH EVENTS OR OTHER THINGS! +\t\t\t//code to hook into events, load state/resources etc goes here \t\t}} - + +\t\t// Called when this plugin is being unloaded (e.g. on server shutdown) \t\tpublic override void Unload(bool shutdown) \t\t{{ -\t\t\t//UNLOAD YOUR PLUGIN BY SAVING FILES OR DISPOSING OBJECTS! +\t\t\t//code to unhook from events, dispose of state/resources etc goes here \t\t}} - + +\t\t// Displays help for or information about this plugin \t\tpublic override void Help(Player p) \t\t{{ -\t\t\t//HELP INFO! +\t\t\tp.Message(""No help is available for this plugin.""); \t\t}} \t}} }}"; @@ -130,12 +147,12 @@ namespace MCGalaxy public override string FullName { get { return "Visual Basic"; } } public override string CommentPrefix { get { return "'"; } } - protected override CompilerResults DoCompile(string[] srcPaths, CompilerParameters args) { + protected override CompilerErrorCollection DoCompile(string[] srcPaths, CompilerParameters args) { #if NETSTANDARD throw new NotSupportedException("Compiling Visual Basic is not supported in .NET Standard build"); #else InitCompiler("VisualBasic"); - return compiler.CompileAssemblyFromFile(args, srcPaths); + return compiler.CompileAssemblyFromFile(args, srcPaths).Errors; #endif } diff --git a/MCGalaxy/Modules/Compiling/Compiler.cs b/MCGalaxy/Modules/Compiling/Compiler.cs index 03f2f7320..1799327b4 100644 --- a/MCGalaxy/Modules/Compiling/Compiler.cs +++ b/MCGalaxy/Modules/Compiling/Compiler.cs @@ -79,17 +79,11 @@ namespace MCGalaxy.Modules.Compiling } - /// Attempts to compile the given source code file to a .dll file. - /// Logs errors to IScripting.ErrorPath. - public CompilerResults Compile(string srcPath, string dstPath) { - return Compile(new [] { srcPath }, dstPath); - } - /// Attempts to compile the given source code files to a .dll file. /// Logs errors to IScripting.ErrorPath. - public CompilerResults Compile(string[] srcPaths, string dstPath) { - CompilerResults results = DoCompile(srcPaths, dstPath); - if (!results.Errors.HasErrors) return results; + public CompilerErrorCollection Compile(string[] srcPaths, string dstPath) { + CompilerErrorCollection errors = DoCompile(srcPaths, dstPath); + if (!errors.HasErrors) return errors; SourceMap sources = new SourceMap(srcPaths); StringBuilder sb = new StringBuilder(); @@ -98,7 +92,7 @@ namespace MCGalaxy.Modules.Compiling sb.AppendLine("############################################################"); sb.AppendLine(); - foreach (CompilerError err in results.Errors) + foreach (CompilerError err in errors) { string type = err.IsWarning ? "Warning" : "Error"; sb.AppendLine(DescribeError(err, srcPaths, "") + ":"); @@ -115,11 +109,11 @@ namespace MCGalaxy.Modules.Compiling using (StreamWriter w = new StreamWriter(ERROR_LOG_PATH, true)) { w.Write(sb.ToString()); } - return results; + return errors; } /// Compiles the given source code. - protected abstract CompilerResults DoCompile(string[] srcPaths, string dstPath); + protected abstract CompilerErrorCollection DoCompile(string[] srcPaths, string dstPath); public static string DescribeError(CompilerError err, string[] srcs, string text) { string type = err.IsWarning ? "Warning" : "Error"; @@ -152,7 +146,7 @@ namespace MCGalaxy.Modules.Compiling } } - protected override CompilerResults DoCompile(string[] srcPaths, string dstPath) { + protected override CompilerErrorCollection DoCompile(string[] srcPaths, string dstPath) { CompilerParameters args = new CompilerParameters(); args.GenerateExecutable = false; args.IncludeDebugInformation = true; @@ -173,7 +167,7 @@ namespace MCGalaxy.Modules.Compiling return DoCompile(srcPaths, args); } - protected abstract CompilerResults DoCompile(string[] srcPaths, CompilerParameters args); + protected abstract CompilerErrorCollection DoCompile(string[] srcPaths, CompilerParameters args); void AddReferences(string path, CompilerParameters args) { // Allow referencing other assemblies using '//reference [assembly name]' at top of the file diff --git a/MCGalaxy/Modules/Compiling/CompilerOperations.cs b/MCGalaxy/Modules/Compiling/CompilerOperations.cs index 241d86b86..4fb1a25bc 100644 --- a/MCGalaxy/Modules/Compiling/CompilerOperations.cs +++ b/MCGalaxy/Modules/Compiling/CompilerOperations.cs @@ -78,30 +78,30 @@ namespace MCGalaxy.Modules.Compiling /// Type of files being compiled (e.g. Plugin, Command) /// Path of the source code files /// Path to the destination .dll - /// The compiler results, or null if compilation failed - public static CompilerResults Compile(Player p, ICompiler compiler, string type, string[] srcs, string dst) { + /// Whether compilation succeeded + public static bool Compile(Player p, ICompiler compiler, string type, string[] srcs, string dst) { foreach (string path in srcs) { if (File.Exists(path)) continue; p.Message("File &9{0} &Snot found.", path); - return null; + return false; } - CompilerResults results = compiler.Compile(srcs, dst); - if (!results.Errors.HasErrors) { + CompilerErrorCollection errors = compiler.Compile(srcs, dst); + if (!errors.HasErrors) { p.Message("{0} compiled successfully from {1}", type, srcs.Join(file => Path.GetFileName(file))); - return results; + return true; } - SummariseErrors(results, srcs, p); - return null; + SummariseErrors(errors, srcs, p); + return false; } - static void SummariseErrors(CompilerResults results, string[] srcs, Player p) { + static void SummariseErrors(CompilerErrorCollection errors, string[] srcs, Player p) { int logged = 0; - foreach (CompilerError err in results.Errors) + foreach (CompilerError err in errors) { p.Message("&W{1} - {0}", err.ErrorText, ICompiler.DescribeError(err, srcs, " #" + err.ErrorNumber)); @@ -109,8 +109,8 @@ namespace MCGalaxy.Modules.Compiling if (logged >= MAX_LOG) break; } - if (results.Errors.Count > MAX_LOG) { - p.Message(" &W.. and {0} more", results.Errors.Count - MAX_LOG); + if (errors.Count > MAX_LOG) { + p.Message(" &W.. and {0} more", errors.Count - MAX_LOG); } p.Message("&WCompilation error. See " + ICompiler.ERROR_LOG_PATH + " for more information."); } diff --git a/MCGalaxy/Modules/Compiling/Roslyn.cs b/MCGalaxy/Modules/Compiling/Roslyn.cs index 69c96144e..bb22d77ec 100644 --- a/MCGalaxy/Modules/Compiling/Roslyn.cs +++ b/MCGalaxy/Modules/Compiling/Roslyn.cs @@ -28,7 +28,7 @@ namespace MCGalaxy.Modules.Compiling static Regex outputRegWithFileAndLine; static Regex outputRegSimple; - public static CompilerResults Compile(CompilerParameters options, string[] fileNames) { + public static CompilerErrorCollection Compile(CompilerParameters options, string[] fileNames) { try { return DoCompile(options, fileNames); } finally { @@ -36,29 +36,23 @@ namespace MCGalaxy.Modules.Compiling } } - static CompilerResults DoCompile(CompilerParameters options, string[] fileNames) { - CompilerResults results = new CompilerResults(options.TempFiles); - List output = new List(); - - results.TempFiles.AddExtension("pdb"); // for .pdb debug files - string args = GetCommandLineArguments(options, fileNames); - + static CompilerErrorCollection DoCompile(CompilerParameters options, string[] fileNames) { + string args = GetCommandLineArguments(options, fileNames); string netPath = GetBinaryFile("MCG_DOTNET_PATH", "'dotnet' executable - e.g. /home/test/.dotnet/dotnet"); string cscPath = GetBinaryFile("MCG_COMPILER_PATH", "'csc.dll' file - e.g. /home/test/.dotnet/sdk/6.0.300/Roslyn/bincore/csc.dll"); + CompilerErrorCollection errors = new CompilerErrorCollection(); + List output = new List(); int retValue = Compile(netPath, cscPath, args, output); - results.NativeCompilerReturnValue = retValue; // only look for errors/warnings if the compile failed or the caller set the warning level if (retValue != 0 || options.WarningLevel > 0) { foreach (string line in output) { - ProcessCompilerOutputLine(results, line); + ProcessCompilerOutputLine(errors, line); } } - - results.PathToAssembly = options.OutputAssembly; - return results; + return errors; } static string Quote(string value) { return "\"" + value + "\""; } diff --git a/MCGalaxy/Scripting/Plugin.cs b/MCGalaxy/Scripting/Plugin.cs index 2633d48b1..ec7b57d84 100644 --- a/MCGalaxy/Scripting/Plugin.cs +++ b/MCGalaxy/Scripting/Plugin.cs @@ -45,7 +45,7 @@ namespace MCGalaxy /// Name of the plugin. public abstract string name { get; } - /// Oldest version of MCGalaxy this plugin is compatible with. + /// The oldest version of MCGalaxy this plugin is compatible with. public abstract string MCGalaxy_Version { get; } /// Version of this plugin. public virtual int build { get { return 0; } }