Skip to content

Commit

Permalink
dotnet: Avoid need for compiler env variable, try to avoid need for d…
Browse files Browse the repository at this point in the history
…otnet path env variable too
  • Loading branch information
UnknownShadow200 committed Jul 4, 2024
1 parent 60df0ea commit dee6bee
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 20 deletions.
56 changes: 42 additions & 14 deletions MCGalaxy/Modules/Compiling/CompilerBackends.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace MCGalaxy.Modules.Compiling
#if !MCG_DOTNET
/// <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, List<string> referenced) {
CompilerParameters args = new CompilerParameters();
args.GenerateExecutable = false;
Expand All @@ -64,11 +64,11 @@ public static class ICodeDomCompiler
if (compiler != null) return;
compiler = CodeDomProvider.CreateProvider(language);
if (compiler != null) return;

Logger.Log(LogType.Warning,
"WARNING: {0} compiler is missing, you will be unable to compile {1} files.",
c.FullName, c.FileExtension);
// TODO: Should we log "You must have .net developer tools. (You need a visual studio)" ?
// TODO: Should we log "You must have .net developer tools. (You need a visual studio)" ?
}

public static ICompilerErrors Compile(CompilerParameters args, string[] srcPaths, CodeDomProvider compiler) {
Expand All @@ -92,15 +92,15 @@ public static class ICodeDomCompiler
}
#else
/// <summary> Compiles C# source code files, using Roslyn for the compiler </summary>
public static class RoslynCSharpCompiler
public static class RoslynCSharpCompiler
{
static Regex outputRegWithFileAndLine;
static Regex outputRegSimple;

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");
string netPath = GetDotnetPath();
string cscPath = GetCompilerPath(netPath);

ICompilerErrors errors = new ICompilerErrors();
List<string> output = new List<string>();
Expand All @@ -109,7 +109,7 @@ public static class RoslynCSharpCompiler
// Only look for errors/warnings if the compile failed
// TODO still log warnings anyways error when success?
if (retValue != 0) {
foreach (string line in output)
foreach (string line in output)
{
ProcessCompilerOutputLine(errors, line);
}
Expand All @@ -118,6 +118,13 @@ public static class RoslynCSharpCompiler
}

static string Quote(string value) { return "\"" + value + "\""; }

static string GetDotnetPath() {
string path = Server.GetRuntimeProcessExePath();
if (path.EndsWith("dotnet")) return path;

return GetBinaryFile("MCG_DOTNET_PATH", "'dotnet' executable - e.g. /home/test/.dotnet/dotnet");
}

static string GetBinaryFile(string varName, string desc) {
string path = Environment.GetEnvironmentVariable(varName);
Expand All @@ -128,18 +135,39 @@ public static class RoslynCSharpCompiler
using (Stream tmp = File.OpenRead(path)) { }
return path;
}

static string GetCompilerPath(string dotnetPath) {
ProcessStartInfo psi = CreateStartInfo(dotnetPath, "--list-sdks");
string rootFolder = Path.GetDirectoryName(dotnetPath);

using (Process p = new Process())
{
p.StartInfo = psi;
p.Start();

static int Compile(string path, string exeArgs, string args, List<string> output) {
// e.g. /home/test/.dotnet/dotnet exec "/home/test/.dotnet/sdk/6.0.300/Roslyn/bincore/csc.dll" [COMPILER ARGS]
args = "exec " + Quote(exeArgs) + " " + args;

// https://stackoverflow.com/questions/285760/how-to-spawn-a-process-and-capture-its-stdout-in-net
string sdk = p.StandardOutput.ReadLine();
p.WaitForExit();
return Path.Combine(rootFolder, "sdk", sdk, "Roslyn", "bincore", "csc.dll");
}
}


static ProcessStartInfo CreateStartInfo(string path, string args) {
ProcessStartInfo psi = new ProcessStartInfo(path, args);
psi.WorkingDirectory = Environment.CurrentDirectory;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
return psi;
}

static int Compile(string path, string exeArgs, string args, List<string> output) {
// e.g. /home/test/.dotnet/dotnet exec "/home/test/.dotnet/sdk/6.0.300/Roslyn/bincore/csc.dll" [COMPILER ARGS]
args = "exec " + Quote(exeArgs) + " " + args;

// https://stackoverflow.com/questions/285760/how-to-spawn-a-process-and-capture-its-stdout-in-net
ProcessStartInfo psi = CreateStartInfo(path, args);

using (Process p = new Process())
{
Expand Down Expand Up @@ -218,7 +246,7 @@ public static class RoslynCSharpCompiler
AddReferencedAssembly(sb, sysAssemblyPaths, "System.Runtime.dll");
AddReferencedAssembly(sb, sysAssemblyPaths, "netstandard.dll");

foreach (string path in referencedAssemblies)
foreach (string path in referencedAssemblies)
{
AddReferencedAssembly(sb, sysAssemblyPaths, path);
}
Expand All @@ -232,7 +260,7 @@ public static class RoslynCSharpCompiler
sb.Append("/warnaserror- ");
sb.Append("/unsafe ");

foreach (string path in srcPaths)
foreach (string path in srcPaths)
{
sb.AppendFormat("{0} ", Quote(path));
}
Expand Down
10 changes: 6 additions & 4 deletions MCGalaxy/Server/Maintenance/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class Updater
public const string BaseURL = "https://raw.githubusercontent.com/ClassiCube/MCGalaxy/master/";
public const string UploadsURL = "https://github.com/ClassiCube/MCGalaxy/tree/master/Uploads";
const string CurrentVersionURL = BaseURL + "Uploads/current_version.txt";
const string changelogURL = BaseURL + "Changelog.txt";
const string CHANGELOG_URL = BaseURL + "Changelog.txt";

const string CDN_URL = "https://cdn.classicube.net/client/mcg/{0}/";
#if NET8_0
Expand Down Expand Up @@ -98,13 +98,15 @@ public static class Updater
WebClient client = HttpUtil.CreateWebClient();

client.DownloadFile(DLL_URL.Replace("{0}", mode), "MCGalaxy_.update");
#if MCG_DOTNET
#if MCG_STANDALONE
// Self contained executable, no separate CLI or GUI to download
#elif MCG_DOTNET
client.DownloadFile(CLI_URL.Replace("{0}", mode), "MCGalaxyCLI.update");
#elif !MCG_STANDALONE
#else
client.DownloadFile(GUI_URL.Replace("{0}", mode), "MCGalaxy.update");
client.DownloadFile(CLI_URL.Replace("{0}", mode), "MCGalaxyCLI.update");
#endif
client.DownloadFile(changelogURL, "Changelog.txt");
client.DownloadFile(CHANGELOG_URL, "Changelog.txt");

Server.SaveAllLevels();
Player[] players = PlayerInfo.Online.Items;
Expand Down
5 changes: 4 additions & 1 deletion MCGalaxy/Server/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ public sealed partial class Server
}


/// <summary> Returns the full path to the server core DLL </summary>
public static string GetServerDLLPath() {
#if MCG_STANDALONE
return GetRuntimeProcessExePath();
Expand All @@ -268,14 +269,16 @@ public sealed partial class Server
#endif
}

public static string GetRestartPath() {
/// <summary> Returns the full path to the server executable </summary>
public static string GetServerExePath() {
#if MCG_STANDALONE
return GetRuntimeProcessExePath();
#else
return DotNetBackend.GetExePath(RestartPath);
#endif
}

/// <summary> Returns the full path to the runtime executable path </summary>
public static string GetRuntimeProcessExePath() {
return Process.GetCurrentProcess().MainModule.FileName;
}
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/util/OperatingSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public abstract class IOperatingSystem
/// <remarks> Does not return if the restart is performed in-place
/// (since the current process image is replaced) </remarks>
public virtual void RestartProcess() {
Process.Start(Server.GetRestartPath());
Process.Start(Server.GetServerExePath());
}


Expand Down

0 comments on commit dee6bee

Please sign in to comment.