Skip to content

Commit

Permalink
Pass args specified to the starter jar to MC
Browse files Browse the repository at this point in the history
And fix compatibility with Forge 1.17
  • Loading branch information
Matyrobbrt committed Jul 1, 2024
1 parent 2121b77 commit c6e0a6b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
7 changes: 2 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import java.nio.file.Files
import java.nio.file.Paths

plugins {
id 'java'
Expand Down Expand Up @@ -36,7 +35,7 @@ dependencies {

java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
languageVersion = JavaLanguageVersion.of(16)
}
}

Expand All @@ -45,9 +44,7 @@ jar {
manifest.attributes([
'Main-Class' : 'net.neoforged.serverstarterjar.Main',
'Premain-Class' : 'net.neoforged.serverstarterjar.Agent',
'Launcher-Agent-Class': 'net.neoforged.serverstarterjar.Agent',
'Add-Exports' : 'java.base/jdk.internal.loader',
'Add-Opens' : 'java.base/java.lang',
'Launcher-Agent-Class': 'net.neoforged.serverstarterjar.Agent'
])
}

Expand Down
55 changes: 41 additions & 14 deletions src/main/java/net/neoforged/serverstarterjar/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class Main {
public static final MethodHandle SET_BOOT_LAYER;

static {
// Open the needed packages below to ourselves
open(ModuleLayer.boot().findModule("java.base").orElseThrow(), "java.lang", Main.class.getModule());
export(ModuleLayer.boot().findModule("java.base").orElseThrow(), "jdk.internal.loader", Main.class.getModule());

var lookup = MethodHandles.lookup();
try {
LOAD_MODULE = lookup.unreflect(lookup.findClass("jdk.internal.loader.BuiltinClassLoader").getDeclaredMethod("loadModule", ModuleReference.class));
Expand All @@ -41,7 +45,7 @@ public class Main {
}
}

public static void main(String[] $) throws Throwable {
public static void main(String[] starterArgs) throws Throwable {
// Attempt to locate the run.bat/run.sh file
final var runPath = Path.of(OS.runFile);
if (Files.notExists(runPath)) {
Expand Down Expand Up @@ -71,23 +75,17 @@ public static void main(String[] $) throws Throwable {

findValues(args, "--add-opens").stream()
.map(arg -> arg.split("="))
.forEach(toOpen -> Agent.instrumentation.redefineModule(
.forEach(toOpen -> open(
bootPath.layer().findModule(toOpen[0].split("/")[0]).orElseThrow(),
Set.of(),
Map.of(),
Map.of(toOpen[0].split("/")[1], Set.of(bootPath.layer().findModule(toOpen[1]).orElseThrow())),
Set.of(),
Map.of()
toOpen[0].split("/")[1],
bootPath.layer().findModule(toOpen[1]).orElseThrow()
));
findValues(args, "--add-exports").stream()
.map(arg -> arg.split("="))
.forEach(toExport -> Agent.instrumentation.redefineModule(
.forEach(toExport -> export(
bootPath.layer().findModule(toExport[0].split("/")[0]).orElseThrow(),
Set.of(),
Map.of(toExport[0].split("/")[1], Set.of(bootPath.layer().findModule(toExport[1]).orElseThrow())),
Map.of(),
Set.of(),
Map.of()
toExport[0].split("/")[1],
(bootPath.layer().findModule(toExport[1]).orElseThrow())
));

SET_BOOT_LAYER.invokeExact(bootPath.layer());
Expand All @@ -110,10 +108,39 @@ public static void main(String[] $) throws Throwable {
throw new Exception("Failed to find main class \"" + mainName + "\"", e);
}

// Pass any args specified to the start jar to MC
args.addAll(Arrays.asList(starterArgs));

// If the main class isn't exported, export it so that we can access it
if (!main.getDeclaringClass().getModule().isExported(main.getDeclaringClass().getPackageName())) {
export(main.getDeclaringClass().getModule(), main.getDeclaringClass().getPackageName(), Main.class.getModule());
}

main.invoke(null, new Object[] { args.toArray(String[]::new) });
}

@SuppressWarnings("removal")
private static void export(Module module, String pkg, Module to) {
Agent.instrumentation.redefineModule(
module,
Set.of(),
Map.of(pkg, Set.of(to)),
Map.of(),
Set.of(),
Map.of()
);
}

private static void open(Module module, String pkg, Module to) {
Agent.instrumentation.redefineModule(
module,
Set.of(),
Map.of(),
Map.of(pkg, Set.of(to)),
Set.of(),
Map.of()
);
}

private static boolean runInstaller() throws Throwable {
try (final var stream = Files.find(Path.of("."), 1, (path, basicFileAttributes) -> path.getFileName().toString().endsWith("installer.jar"))) {
var inst = stream.findFirst();
Expand Down

0 comments on commit c6e0a6b

Please sign in to comment.