Improve /pcreate C# example, and simplify ICompiler slightly since a full CompilerResults instance is not required

This commit is contained in:
UnknownShadow200 2022-07-23 13:57:07 +10:00
parent de386a39c5
commit cadde3b0c4
6 changed files with 63 additions and 58 deletions

View File

@ -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);

View File

@ -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
}

View File

@ -79,17 +79,11 @@ namespace MCGalaxy.Modules.Compiling
}
/// <summary> Attempts to compile the given source code file to a .dll file. </summary>
/// <remarks> Logs errors to IScripting.ErrorPath. </remarks>
public CompilerResults Compile(string srcPath, string dstPath) {
return Compile(new [] { srcPath }, dstPath);
}
/// <summary> Attempts to compile the given source code files to a .dll file. </summary>
/// <remarks> Logs errors to IScripting.ErrorPath. </remarks>
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;
}
/// <summary> Compiles the given source code. </summary>
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

View File

@ -78,30 +78,30 @@ namespace MCGalaxy.Modules.Compiling
/// <param name="type"> Type of files being compiled (e.g. Plugin, Command) </param>
/// <param name="srcs"> Path of the source code files </param>
/// <param name="dst"> Path to the destination .dll </param>
/// <returns> The compiler results, or null if compilation failed </returns>
public static CompilerResults Compile(Player p, ICompiler compiler, string type, string[] srcs, string dst) {
/// <returns> Whether compilation succeeded </returns>
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.");
}

View File

@ -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<string> output = new List<string>();
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<string> output = new List<string>();
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 + "\""; }

View File

@ -45,7 +45,7 @@ namespace MCGalaxy
/// <summary> Name of the plugin. </summary>
public abstract string name { get; }
/// <summary> Oldest version of MCGalaxy this plugin is compatible with. </summary>
/// <summary> The oldest version of MCGalaxy this plugin is compatible with. </summary>
public abstract string MCGalaxy_Version { get; }
/// <summary> Version of this plugin. </summary>
public virtual int build { get { return 0; } }