Skip to content

Commit

Permalink
Tidy up assembly referencing
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Jul 1, 2024
1 parent 96ebcf2 commit a818e54
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/actions/notify_failure/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ runs:
steps:
- name: Notify failure
shell: sh
if: ${{ inputs.WEBHOOK_URL != '' }}
run: |
curl ${{ inputs.WEBHOOK_URL }} -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"username\": \"${{ inputs.BOT_USERNAME }}\", \"avatar_url\": \"${{ inputs.BOT_AVATAR }}\", \"content\": \"${{ inputs.NOTIFY_MESSAGE }}\" }"
16 changes: 7 additions & 9 deletions MCGalaxy/Modules/Compiling/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public abstract class ICompiler

/// <summary> Converts source file paths to full paths,
/// then returns list of parsed referenced assemblies </summary>
public static List<string> ProcessInput(string[] srcPaths, string commentPrefix) {
protected List<string> ProcessInput(string[] srcPaths, string commentPrefix) {
List<string> referenced = new List<string>();

for (int i = 0; i < srcPaths.Length; i++)
Expand All @@ -142,9 +142,9 @@ public abstract class ICompiler
return referenced;
}

static void AddReferences(string path, string commentPrefix, List<string> referenced) {
void AddReferences(string path, string commentPrefix, List<string> referenced) {
// Allow referencing other assemblies using '//reference [assembly name]' at top of the file
using (StreamReader r = new StreamReader(path)) {
using (StreamReader r = new StreamReader(path)) {
string refPrefix = commentPrefix + "reference ";
string plgPrefix = commentPrefix + "pluginref ";
string line;
Expand All @@ -156,18 +156,16 @@ public abstract class ICompiler
} else if (line.CaselessStarts(plgPrefix)) {
path = Path.Combine(IScripting.PLUGINS_DLL_DIR, GetDLL(line));
referenced.Add(Path.GetFullPath(path));
#if NETSTANDARD
} else if (line.CaselessStarts(commentPrefix + "dotnetref")) {
referenced.Add(GetDLL(line));
#endif
} else {
continue;
ProcessInputLine(line, referenced);
}
}
}
}

static string GetDLL(string line) {
protected virtual void ProcessInputLine(string line, List<string> referenced) { }

protected static string GetDLL(string line) {
int index = line.IndexOf(' ') + 1;
// For consistency with C#, treat '//reference X.dll;' as '//reference X.dll'
return line.Substring(index).Replace(";", "");
Expand Down
9 changes: 2 additions & 7 deletions MCGalaxy/Modules/Compiling/CompilerBackends.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ namespace MCGalaxy.Modules.Compiling
/// <summary> Compiles source code files from a particular language, using a CodeDomProvider for the compiler </summary>
public static class ICodeDomCompiler
{
public static CompilerParameters PrepareInput(string[] srcPaths, string dstPath, string commentPrefix) {
public static CompilerParameters PrepareInput(string[] srcPaths, string dstPath, List<string> referenced) {
CompilerParameters args = new CompilerParameters();
args.GenerateExecutable = false;
args.IncludeDebugInformation = true;
args.OutputAssembly = dstPath;

List<string> referenced = ICompiler.ProcessInput(srcPaths, commentPrefix);
foreach (string assembly in referenced)
{
args.ReferencedAssemblies.Add(assembly);
Expand Down Expand Up @@ -97,12 +96,8 @@ public static class RoslynCSharpCompiler
{
static Regex outputRegWithFileAndLine;
static Regex outputRegSimple;

public static List<string> PrepareInput(string[] srcPaths) {
return ICompiler.ProcessInput(srcPaths, "//");
}

public static ICompilerErrors Compile(string[] srcPaths, string dstPath, List<string> referenced) {
public static ICompilerErrors Compile(string[] srcPaths, string dstPath, List<string> referenced) {
string args = GetCommandLineArguments(srcPaths, dstPath, referenced);
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");
Expand Down
13 changes: 10 additions & 3 deletions MCGalaxy/Modules/Compiling/CompilerFrontends.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,24 @@ public sealed class CSCompiler : ICompiler
CodeDomProvider compiler;

protected override ICompilerErrors DoCompile(string[] srcPaths, string dstPath) {
CompilerParameters args = ICodeDomCompiler.PrepareInput(srcPaths, dstPath, "//");
List<string> referenced = ProcessInput(srcPaths, "//");
CompilerParameters args = ICodeDomCompiler.PrepareInput(srcPaths, dstPath, referenced);
args.CompilerOptions += " /unsafe";
// NOTE: Make sure to keep CompilerOptions in sync with RoslynCSharpCompiler

ICodeDomCompiler.InitCompiler(this, "CSharp", ref compiler);
return ICodeDomCompiler.Compile(args, srcPaths, compiler);
}
#else
protected override ICompilerErrors DoCompile(string[] srcPaths, string dstPath) {
List<string> referenced = RoslynCSharpCompiler.PrepareInput(srcPaths);
protected override ICompilerErrors DoCompile(string[] srcPaths, string dstPath) {
List<string> referenced = ProcessInput(srcPaths, "//");
return RoslynCSharpCompiler.Compile(srcPaths, dstPath, referenced);
}

protected override void ProcessInputLine(string line, List<string> referenced) {
if (!line.CaselessStarts("//dotnetref")) return;

referenced.Add(GetDLL(line));
}
#endif

Expand Down

0 comments on commit a818e54

Please sign in to comment.