Skip to content

Commit

Permalink
Tidy up IScripting API
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Mar 24, 2021
1 parent 45fe8e9 commit d2875ca
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 32 deletions.
7 changes: 3 additions & 4 deletions GUI/Popups/CustomCommands.cs
Expand Up @@ -71,13 +71,12 @@ public partial class CustomCommands : Form {

IScripting engine = fileName.CaselessEnds(".cs") ? IScripting.CS : IScripting.VB;
if (!File.Exists(fileName)) return;
ConsoleHelpPlayer p = new ConsoleHelpPlayer();

CompilerParameters args = new CompilerParameters();
args.GenerateInMemory = true;
CompilerResults result = engine.Compile(fileName, args, p);
ConsoleHelpPlayer p = new ConsoleHelpPlayer();
CompilerResults result = engine.Compile(fileName, null);

if (result.Errors.HasErrors) {
IScripting.SummariseErrors(result, p);
string body = "\r\n\r\n" + Colors.StripUsed(p.Messages);
Popup.Error("Compilation error. See logs/errors/compiler.log for more details." + body);
return;
Expand Down
12 changes: 8 additions & 4 deletions MCGalaxy/Commands/Scripting/CmdCompile.cs
Expand Up @@ -16,6 +16,7 @@
permissions and limitations under the Licenses.
*/
using System;
using System.CodeDom.Compiler;
using System.IO;
using MCGalaxy.Scripting;

Expand All @@ -40,13 +41,16 @@ public sealed class CmdCompile : Command2 {
}

string srcPath = engine.SourcePath(args[0]);
string dstPath = IScripting.DllPath(args[0]);

string dstPath = IScripting.DllPath(args[0]);
if (!File.Exists(srcPath)) {
p.Message("File &9{0} &Snot found.", srcPath);
} else if (engine.Compile(srcPath, dstPath, p)) {
p.Message("File &9{0} &Snot found.", srcPath); return;
}

CompilerResults results = engine.Compile(srcPath, dstPath);
if (!results.Errors.HasErrors) {
p.Message("Command compiled successfully.");
} else {
IScripting.SummariseErrors(results, p);
p.Message("&WCompilation error. See " + IScripting.ErrorPath + " for more information.");
}
}
Expand Down
13 changes: 9 additions & 4 deletions MCGalaxy/Commands/Scripting/CmdPlugin.cs
Expand Up @@ -16,6 +16,7 @@
permissions and limitations under the Licenses.
*/
using System;
using System.CodeDom.Compiler;
using System.IO;
using MCGalaxy.Scripting;

Expand Down Expand Up @@ -56,14 +57,18 @@ public sealed class CmdPlugin : Command2 {

static void CompilePlugin(Player p, string name) {
IScripting engine = IScripting.CS;
string srcPath = "plugins/" + name + engine.Ext;
string dstPath = IScripting.PluginPath(name);

string srcPath = "plugins/" + name + engine.Ext;
string dstPath = IScripting.PluginPath(name);
if (!File.Exists(srcPath)) {
p.Message("File &9{0} &Snot found.", srcPath);
} else if (engine.Compile(srcPath, dstPath, p)) {
p.Message("File &9{0} &Snot found.", srcPath); return;
}

CompilerResults results = engine.Compile(srcPath, dstPath);
if (!results.Errors.HasErrors) {
p.Message("Plugin compiled successfully.");
} else {
IScripting.SummariseErrors(results, p);
p.Message("&WCompilation error. See " + IScripting.ErrorPath + " for more information.");
}
}
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Network/BaseWebSocket.cs
Expand Up @@ -26,7 +26,7 @@ public abstract class BaseWebSocket : INetSocket, INetProtocol {
protected bool conn, upgrade;
protected bool readingHeaders = true;

/// <summary> Computes the base64-encoded handshake verification key </summary>
/// <summary> Computes a base64-encoded handshake verification key </summary>
protected static string ComputeKey(string rawKey) {
string key = rawKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
SHA1 sha = SHA1.Create();
Expand Down
40 changes: 21 additions & 19 deletions MCGalaxy/Scripting/Scripting.cs
Expand Up @@ -74,21 +74,18 @@ public abstract class IScripting {
sw.WriteLine(syntax);
}
}

/// <summary> Attempts to compile source code from the given file. </summary>
/// <remarks> Logs errors to player (summarised) and to IScripting.ErrorPath. </remarks>
public bool Compile(string srcPath, string dstPath, Player p) {

const int maxLog = 2;
/// <summary> Attempts to compile the given source code file to a .dll file. </summary>
/// <remarks> If dstPath is null, compiles to an in-memory .dll instead. </remarks>
/// <remarks> Logs errors to IScripting.ErrorPath. </remarks>
public CompilerResults Compile(string srcPath, string dstPath) {
CompilerParameters args = new CompilerParameters();
args.GenerateExecutable = false;
args.OutputAssembly = dstPath;
return !Compile(srcPath, args, p).Errors.HasErrors;
}

const int maxLog = 2;
/// <summary> Attempts to compile source code from the given file. </summary>
/// <remarks> Logs errors to player (summarised) and to IScripting.ErrorPath. </remarks>
public CompilerResults Compile(string srcPath, CompilerParameters args, Player p) {
List<string> source = ReadSource(srcPath, args);
if (dstPath != null) args.OutputAssembly = dstPath;
if (dstPath == null) args.GenerateInMemory = true;

List<string> source = ReadSource(srcPath, args);
CompilerResults results = CompileSource(source.Join(Environment.NewLine), args);
if (!results.Errors.HasErrors) return results;

Expand All @@ -111,6 +108,14 @@ public abstract class IScripting {
sb.AppendLine();
}

using (StreamWriter w = new StreamWriter(ErrorPath, true)) {
w.Write(sb.ToString());
}
return results;
}

/// <summary> Messages a summary of warnings and errors to the given player. </summary>
public static void SummariseErrors(CompilerResults results, Player p) {
int logged = 0;
foreach (CompilerError err in results.Errors) {
string type = err.IsWarning ? "Warning" : "Error";
Expand All @@ -119,12 +124,9 @@ public abstract class IScripting {
logged++;
if (logged >= maxLog) break;
}
if (results.Errors.Count > maxLog) p.Message(" &W.. and {0} more", results.Errors.Count - maxLog);

using (StreamWriter w = new StreamWriter(ErrorPath, true)) {
w.Write(sb.ToString());
}
return results;
if (results.Errors.Count <= maxLog) return;
p.Message(" &W.. and {0} more", results.Errors.Count - maxLog);
}

static List<string> ReadSource(string path, CompilerParameters args) {
Expand All @@ -144,7 +146,7 @@ public abstract class IScripting {
}

/// <summary> Compiles the given source code. </summary>
public CompilerResults CompileSource(string source, CompilerParameters args) {
CompilerResults CompileSource(string source, CompilerParameters args) {
args.ReferencedAssemblies.Add("MCGalaxy_.dll");
PrepareArgs(args);
source = source.Replace("MCLawl", "MCGalaxy");
Expand Down

0 comments on commit d2875ca

Please sign in to comment.