Skip to content

Commit

Permalink
fix: Newer Forge versions needs to get started differently. (And use …
Browse files Browse the repository at this point in the history
…different java versions and so on)

Also fixes ability to properly launch server when it's done. (Helps if you enter the entire path)
  • Loading branch information
itssimple committed Jul 21, 2022
1 parent c8f5132 commit 2abaa64
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CurseForge.Minecraft.Serverpack.Launcher/FabricMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace CurseForge.Minecraft.Serverpack.Launcher
{
partial class Program
{
private static async Task InstallFabricAsync(string installPath, string minecraftVersion, string loaderVersion, string javaArgs, bool startServer)
private static async Task InstallFabricAsync(string installPath, string minecraftVersion, string loaderVersion, string javaArgs)
{
var fabricInstaller = Directory.EnumerateFiles(installPath).FirstOrDefault(f => f.Contains("fabric-installer-") && f.EndsWith(".jar"));
if (fabricInstaller == null)
Expand Down
25 changes: 20 additions & 5 deletions CurseForge.Minecraft.Serverpack.Launcher/ForgeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace CurseForge.Minecraft.Serverpack.Launcher
{
partial class Program
{
private static async Task InstallForgeAsync(string installPath, string javaArgs, bool startServer)
private static async Task InstallForgeAsync(string installPath, string minecraftVersion, string modloaderVersion, string javaArgs)
{
var forgeInstaller = Directory.EnumerateFiles(installPath).FirstOrDefault(f => f.Contains("forge-") && f.Contains("-installer.jar") && f.EndsWith(".jar"));
if (forgeInstaller == null)
Expand All @@ -20,7 +20,13 @@ private static async Task InstallForgeAsync(string installPath, string javaArgs,
"--installServer"
};

await RunProcessAsync(installPath, GetJavaExecutable(), false, arguments);
var mcVersion = new Version(minecraftVersion);

var javaPath = mcVersion.Minor <= 16 ? GetJavaExecutable() : Path.Combine(installPath, "runtime", "bin", GetJavaExecutable());

await RunProcessAsync(installPath, javaPath, false, arguments);

var runFile = Path.Combine(installPath, OperatingSystem.IsWindows() ? "run.bat" : "run.sh");

var forgeLoader = Directory.EnumerateFiles(installPath, "*.jar", SearchOption.AllDirectories).FirstOrDefault(f => f.Contains("forge-") && !f.Contains("-installer.jar") && f.EndsWith(".jar"));

Expand All @@ -29,9 +35,18 @@ private static async Task InstallForgeAsync(string installPath, string javaArgs,
Console.WriteLine("Could not find the loader, please launch server manually");
}

var javaPath = Path.Combine(installPath, "runtime", "bin", GetJavaExecutable());

CreateLaunchScriptIfMissing(installPath, javaPath, javaArgs, forgeLoader);
// This is if Forge started using their new run-files, instead of putting the jar in the folder
if (File.Exists(runFile))
{
var newForgePath = Path.Combine(installPath, "libraries", "net", "minecraftforge", "forge");
var forgeVersion = Directory.EnumerateDirectories(newForgePath).FirstOrDefault();
var configFile = Path.Combine(forgeVersion, OperatingSystem.IsWindows() ? "win_args.txt" : "unix_args.txt");
CreateSpecialLaunchScriptIfMissing(installPath, javaPath, javaArgs, $"@{configFile}");
}
else
{
CreateLaunchScriptIfMissing(installPath, javaPath, javaArgs, forgeLoader);
}
}
}
}
6 changes: 3 additions & 3 deletions CurseForge.Minecraft.Serverpack.Launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ private static async Task<int> InstallServer(uint modId, uint fileId, string pat
switch (modLoader)
{
case MinecraftModloader.Fabric:
await InstallFabricAsync(installPath, minecraftVersion, modloaderVersion, javaArgs, startServer);
await InstallFabricAsync(installPath, minecraftVersion, modloaderVersion, javaArgs);
break;
case MinecraftModloader.Forge:
await InstallForgeAsync(installPath, javaArgs, startServer);
await InstallForgeAsync(installPath, minecraftVersion, modloaderVersion, javaArgs);
break;
case MinecraftModloader.Unknown:
Console.WriteLine("Error: Could not determine modloader, bailing out");
Expand All @@ -264,7 +264,7 @@ private static async Task<int> InstallServer(uint modId, uint fileId, string pat

if (startServer)
{
await RunProcessAsync(installPath, OperatingSystem.IsWindows() ? "start-server.bat" : "./start-server.sh", true);
await RunProcessAsync(installPath, Path.Combine(installPath, OperatingSystem.IsWindows() ? "start-server.bat" : "start-server.sh"), true);
}
else
{
Expand Down
48 changes: 48 additions & 0 deletions CurseForge.Minecraft.Serverpack.Launcher/StartupScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,53 @@ namespace CurseForge.Minecraft.Serverpack.Launcher
{
partial class Program
{
private static void CreateSpecialLaunchScriptIfMissing(string installPath, string javaPath, string javaArgs, params string[] arguments)
{
if (OperatingSystem.IsWindows())
{
var launchScript = Path.Combine(installPath, "start-server.bat");

if (!File.Exists(launchScript))
{
File.WriteAllText(launchScript, $@"@echo OFF
cd {installPath}
{javaPath} {javaArgs} -Dsun.stdout.encoding=UTF-8 {string.Join(" ", arguments)} nogui
echo Server has stopped, press any key to continue
pause");
}
}
else
{
var launchScript = Path.Combine(installPath, "start-server.sh");

if (!File.Exists(launchScript))
{
File.WriteAllText(launchScript, $@"#!/bin/sh
cd {installPath}
{javaPath} {javaArgs} -Dsun.stdout.encoding=UTF-8 {string.Join(" ", arguments)} nogui");

var p = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = "/bin/chmod",
Arguments = $"+x {launchScript}",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true
}
};

p.Start();
p.StandardOutput.ReadToEnd();
p.WaitForExit();

}
}
}

private static void CreateLaunchScriptIfMissing(string installPath, string javaPath, string javaArgs, string jarFile)
{
if (OperatingSystem.IsWindows())
Expand All @@ -17,6 +64,7 @@ private static void CreateLaunchScriptIfMissing(string installPath, string javaP
File.WriteAllText(launchScript, $@"@echo OFF
cd {installPath}
{javaPath} {javaArgs} -Dsun.stdout.encoding=UTF-8 -jar {jarFile} nogui
echo Server has stopped, press any key to continue
pause");
}
}
Expand Down

0 comments on commit 2abaa64

Please sign in to comment.