Skip to content

Commit

Permalink
Drop 'Reference X.dll' support, only '//reference X.dll' supported now
Browse files Browse the repository at this point in the history
This way plugins/commands that need to reference other dlls can still be compiled with normal C# compiler without needing to modify the source code
  • Loading branch information
UnknownShadow200 committed Dec 19, 2020
1 parent e3f458e commit 86f143c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
8 changes: 4 additions & 4 deletions MCGalaxy/Scripting/Backends.cs
Expand Up @@ -36,8 +36,8 @@ public sealed class ScriptingCS : IScripting {
//\tFile and class should be named a specific way. For example, /update is named 'CmdUpdate.cs' for the file, and 'CmdUpdate' for the class.
// 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
// e.g. to reference the System.Data assembly, put ""Reference System.Data.dll""
// 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;
Expand Down Expand Up @@ -94,8 +94,8 @@ public sealed class ScriptingVB : IScripting {
'\tFile and class should be named a specific way. For example, /update is named 'CmdUpdate.vb' for the file, and 'CmdUpdate' for the class.
' 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
' e.g. to reference the System.Data assembly, put ""Reference System.Data.dll""
' 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 Imports statements you need after this
Imports System
Expand Down
28 changes: 10 additions & 18 deletions MCGalaxy/Scripting/Scripting.cs
Expand Up @@ -88,8 +88,7 @@ public abstract class IScripting {
/// <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) {
int offset = 0;
List<string> source = ReadSource(srcPath, args, ref offset);
List<string> source = ReadSource(srcPath, args);
CompilerResults results = CompileSource(source.Join(Environment.NewLine), args);
if (!results.Errors.HasErrors) return results;

Expand All @@ -101,7 +100,7 @@ public abstract class IScripting {

foreach (CompilerError err in results.Errors) {
string type = err.IsWarning ? "Warning" : "Error";
sb.AppendLine(type + " on line " + (err.Line + offset) + ":");
sb.AppendLine(type + " on line " + err.Line + ":");

if (err.Line > 0) sb.AppendLine(source[err.Line - 1]);
if (err.Column > 0) sb.Append(' ', err.Column - 1);
Expand All @@ -115,7 +114,7 @@ public abstract class IScripting {
int logged = 0;
foreach (CompilerError err in results.Errors) {
string type = err.IsWarning ? "Warning" : "Error";
p.Message("%W{0} #{1} on line {2} - {3}", type, err.ErrorNumber, err.Line + offset, err.ErrorText);
p.Message("%W{0} #{1} on line {2} - {3}", type, err.ErrorNumber, err.Line, err.ErrorText);

logged++;
if (logged >= maxLog) break;
Expand All @@ -128,25 +127,18 @@ public abstract class IScripting {
return results;
}

static bool IsReferenceLine(string line) {
// Originally only 'Reference X.dll' syntax was supported
// Later '//Reference X.dll' was added, and is preferred
return line.CaselessStarts("reference ") || line.CaselessStarts("//reference ");
}

static List<string> ReadSource(string path, CompilerParameters args, ref int offset) {
static List<string> ReadSource(string path, CompilerParameters args) {
List<string> lines = Utils.ReadAllLinesList(path);
// Allow referencing other assemblies using 'Reference [assembly name]' at top of the file
// Allow referencing other assemblies using '//reference [assembly name]' at top of the file
for (int i = 0; i < lines.Count; i++) {
if (!IsReferenceLine(lines[i])) break;
string line = lines[i];
if (!line.CaselessStarts("//reference ")) break;

int index = lines[i].IndexOf(' ') + 1;
// For consistency with C#, treat 'Reference X.dll;' as 'Reference X.dll'
string assem = lines[i].Substring(index).Replace(";", "");
int index = line.IndexOf(' ') + 1;
// For consistency with C#, treat '//reference X.dll;' as '//reference X.dll'
string assem = line.Substring(index).Replace(";", "");

args.ReferencedAssemblies.Add(assem);
lines.RemoveAt(i);
offset++; i--;
}
return lines;
}
Expand Down

0 comments on commit 86f143c

Please sign in to comment.