Skip to content

Commit 64d2fab

Browse files
committed
Add option to dynamically generate launch scripts
1 parent 0a18665 commit 64d2fab

File tree

5 files changed

+96
-14
lines changed

5 files changed

+96
-14
lines changed

src/main/java/org/kettingpowered/launcher/Args.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public ParsedArgs parse(BetterUI ui, Path eula) throws IOException {
3939
if (containsArg("-accepteula"))
4040
BetterUI.forceAcceptEULA(eula);
4141

42-
boolean installOnly = containsArg("-installOnly");
42+
boolean createLaunchScripts = containsArg("-createLaunchScripts");
43+
boolean installOnly = containsArg("-installOnly") || createLaunchScripts; //run install if createLaunchScripts is enabled
4344
boolean dau = containsArg("-dau");
4445
boolean daus = containsArg("-daus");
4546
boolean enableServerUpdate = !(dau||daus);
@@ -53,7 +54,7 @@ public ParsedArgs parse(BetterUI ui, Path eula) throws IOException {
5354
minecraftVersion = getArg("-minecraftVersion");
5455
if (minecraftVersion != null) Logger.log(LogLevel.WARN, "warn.discontinued.double_dash_minecraftVersion");
5556
}
56-
return new ParsedArgs(Collections.unmodifiableList(args), installOnly, enableServerUpdate, enableLauncherUpdate, target, minecraftVersion, forgeVersion, kettingVersion);
57+
return new ParsedArgs(Collections.unmodifiableList(args), createLaunchScripts, installOnly, enableServerUpdate, enableLauncherUpdate, target, minecraftVersion, forgeVersion, kettingVersion);
5758
}
5859

5960
private static void printHelp(){

src/main/java/org/kettingpowered/launcher/KettingLauncher.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,74 @@ void prepareLaunch() throws Exception {
386386
});
387387

388388
if (args.installOnly()) {
389+
if (args.createLaunchScripts()) {
390+
try {
391+
createLaunchScripts();
392+
I18n.log("info.launcher.install_only.launch_script_success");
393+
} catch (ClassNotFoundException e) {
394+
I18n.logError("error.launcher.install_only.launch_script_failed");
395+
throw new RuntimeException(e);
396+
}
397+
}
389398
I18n.log("info.launcher.install_only.success");
390399
System.exit(0);
391400
}
392401
}
393402

403+
private void createLaunchScripts() throws ClassNotFoundException, IOException {
404+
File runBat = new File(KettingFiles.LAUNCH_DIR, "run.bat");
405+
File runSh = new File(KettingFiles.LAUNCH_DIR, "run.sh");
406+
String launchClass = findLaunchClass();
407+
408+
List<String>[] defaultArgs = Main.getDefaultArgs(this, launchClass, true);
409+
410+
String javaArgsJoined = String.join(" ", defaultArgs[0]);
411+
String launchArgsJoined = String.join(" ", defaultArgs[1]);
412+
413+
try (PrintWriter writer = new PrintWriter(new FileWriter(runBat))) {
414+
String lineSeparator = "\r\n"; //Thanks windows
415+
416+
printLine(writer, lineSeparator, "@echo off");
417+
printLine(writer, lineSeparator, "REM This file was generated by KettingLauncher");
418+
printLine(writer, lineSeparator, "REM Generated for Ketting version " + KettingConstants.KETTING_VERSION);
419+
printLine(writer, lineSeparator, "REM To add extra java arguments, run this file in a terminal and add them after the file name (example: run.bat nogui)");
420+
printLine(writer, lineSeparator);
421+
printLine(writer, lineSeparator, "REM Do not touch the following two lines, unless you know what you are doing");
422+
printLine(writer, lineSeparator, "SET JAVA_OPTS=" + javaArgsJoined.replace(File.pathSeparator, ";"));
423+
printLine(writer, lineSeparator, "SET FORGE_LAUNCH_ARGS=" + launchArgsJoined);
424+
printLine(writer, lineSeparator);
425+
printLine(writer, lineSeparator, "SET EXTRA_OPTS=-Xmx4G");
426+
printLine(writer, lineSeparator);
427+
printLine(writer, lineSeparator, "java %EXTRA_OPTS% %JAVA_OPTS% " + launchClass + " %FORGE_LAUNCH_ARGS% %*");
428+
printLine(writer, lineSeparator, "pause");
429+
}
430+
431+
try (PrintWriter writer = new PrintWriter(new FileWriter(runSh))) {
432+
String lineSeparator = "\n";
433+
434+
printLine(writer, lineSeparator, "#!/bin/bash");
435+
printLine(writer, lineSeparator, "# This file was generated by KettingLauncher");
436+
printLine(writer, lineSeparator, "# Generated for Ketting version " + KettingConstants.KETTING_VERSION);
437+
printLine(writer, lineSeparator, "# To add extra java arguments, run this file in a terminal and add them after the file name (example: ./run.sh nogui)");
438+
printLine(writer, lineSeparator);
439+
printLine(writer, lineSeparator, "# Do not touch the following two lines, unless you know what you are doing");
440+
printLine(writer, lineSeparator, "JAVA_OPTS=\"" + javaArgsJoined.replace(File.pathSeparator, ":") + "\"");
441+
printLine(writer, lineSeparator, "FORGE_LAUNCH_ARGS=\"" + launchArgsJoined + "\"");
442+
printLine(writer, lineSeparator);
443+
printLine(writer, lineSeparator, "EXTRA_OPTS=\"-Xmx4G\"");
444+
printLine(writer, lineSeparator);
445+
printLine(writer, lineSeparator, "java $EXTRA_OPTS $JAVA_OPTS " + launchClass + " $FORGE_LAUNCH_ARGS \"$@\"");
446+
}
447+
}
448+
449+
private void printLine(PrintWriter writer, String lineSeparator, String text) {
450+
writer.print(text + lineSeparator);
451+
}
452+
453+
private void printLine(PrintWriter writer, String lineSeparator) {
454+
writer.print(lineSeparator);
455+
}
456+
394457
String findLaunchClass() throws ClassNotFoundException {
395458
ClassNotFoundException exception = new ClassNotFoundException(I18n.get("error.launcher.no_launch_class"));
396459
//the AgentClassLoader is used here, because it doesn't propagate to anywhere else.

src/main/java/org/kettingpowered/launcher/Main.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public static void main(String[] args) throws Throwable {
186186
launcher.prepareLaunch();
187187
String launchClass = launcher.findLaunchClass();
188188

189-
List<String>[] defaultArgs = getDefaultArgs(launcher.args, launcher.libs, launchClass);
189+
List<String>[] defaultArgs = getDefaultArgs(launcher, launchClass, false);
190190

191191

192192
try {
@@ -225,17 +225,38 @@ private static Stream<String> getClassPath(Libraries libraries){
225225
.map(str->str.replace(File.separatorChar, '/'))//thanks windows
226226
.filter(str -> !str.isBlank());
227227
}
228-
private static List<String>[] getDefaultArgs(ParsedArgs args, Libraries libraries , String main) throws IOException {
228+
public static List<String>[] getDefaultArgs(KettingLauncher launcher, String main, boolean installScript) throws IOException {
229+
ParsedArgs args = launcher.args;
230+
Libraries libraries = launcher.libs;
231+
229232
switch (main) {
230233
case "cpw.mods.bootstraplauncher.BootstrapLauncher":
231234
System.setProperty("java.class.path", getClassPath(libraries).collect(Collectors.joining(File.pathSeparator)));
235+
236+
//these paths below would cause a duplicate
237+
Stream<String> legacyCP = getClassPath(libraries).filter(entry ->
238+
!entry.contains("org/kettingpowered/server/fmlcore") &&
239+
!entry.contains("org/kettingpowered/server/mclanguage") &&
240+
!entry.contains("org/kettingpowered/server/lowcodelanguage") &&
241+
!entry.contains("org/kettingpowered/server/javafmllanguage") &&
242+
!entry.contains("org/kettingpowered/server/forge")
243+
);
244+
245+
if (installScript) {
246+
legacyCP = legacyCP.filter(entry ->
247+
!entry.contains("commons-lang/") &&
248+
!entry.contains("cpw/mods/bootstraplauncher/")
249+
);
250+
}
251+
232252
//noinspection unchecked
233253
return new List[] {
234254
Arrays.asList(
235255
"-p " + Arrays.stream(libraries.getLoadedLibs())
236256
.filter(url ->
237257
url.toString().contains("org/ow2/asm") ||
238-
url.toString().contains("cpw/mods/securejarhandler")
258+
url.toString().contains("cpw/mods/securejarhandler") ||
259+
(installScript && url.toString().contains("cpw/mods/bootstraplauncher"))
239260
).map(url-> {
240261
try {
241262
return url.toURI();
@@ -252,14 +273,7 @@ private static List<String>[] getDefaultArgs(ParsedArgs args, Libraries librarie
252273
"--add-exports java.base/sun.security.util=cpw.mods.securejarhandler",
253274
"--add-exports jdk.naming.dns/com.sun.jndi.dns=java.naming",
254275
"-DlegacyClassPath="+
255-
//these paths below would cause a duplicate
256-
getClassPath(libraries).filter(entry->
257-
!entry.contains("org/kettingpowered/server/fmlcore")&&
258-
!entry.contains("org/kettingpowered/server/mclanguage")&&
259-
!entry.contains("org/kettingpowered/server/lowcodelanguage")&&
260-
!entry.contains("org/kettingpowered/server/javafmllanguage")&&
261-
!entry.contains("org/kettingpowered/server/forge")
262-
).collect(Collectors.joining(File.pathSeparator)),
276+
legacyCP.collect(Collectors.joining(File.pathSeparator)),
263277
"-DlibraryDirectory="+KettingConstants.INSTALLER_LIBRARIES_FOLDER,
264278
"-Djava.net.preferIPv6Addresses=system"
265279
),
@@ -302,7 +316,7 @@ private static List<String>[] getDefaultArgs(ParsedArgs args, Libraries librarie
302316
}
303317
}
304318
private static void addLoadedLib(Libraries libraries, File file) throws IOException {
305-
Main.INST.appendToSystemClassLoaderSearch(new JarFile(file));
319+
if (Main.INST != null) Main.INST.appendToSystemClassLoaderSearch(new JarFile(file));
306320
libraries.addLoadedLib(file);
307321
}
308322
}

src/main/java/org/kettingpowered/launcher/ParsedArgs.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/**
99
* @author C0D3 M4513R
1010
* @param args list of unparsed args
11+
* @param createLaunchScripts Create launch scripts, used to bypass the launcher
1112
* @param installOnly Only install the server. Don't start it.
1213
* @param enableServerUpdator should the server be updated
1314
* @param enableLauncherUpdator should the launcher (this!) be updated
@@ -18,6 +19,7 @@
1819
*/
1920
public record ParsedArgs(
2021
@NotNull List<String> args,
22+
boolean createLaunchScripts,
2123
boolean installOnly,
2224
boolean enableServerUpdator,
2325
boolean enableLauncherUpdator,

src/main/resources/lang/en_us.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ error.launcher.bin_patches_move=Failed to move server bin patches to the correct
2020
error.launcher.bundle_extract_failed=Failed to extract bundled jar content
2121
error.launcher.bundled_file_not_found=Bundled file '%s' not found
2222
error.launcher.hash_algorithm_not_found=Hash algorithm not found
23+
error.launcher.install_only.launch_script_failed=Failed to create launch scripts
2324
error.launcher.launch_failure=Failed to launch Ketting
2425
error.launcher.no_server_version=No Ketting version found for Minecraft version %s
2526
error.launcher.unrecognized_version=Unrecognized launcher version
@@ -46,6 +47,7 @@ error.processor.unknown_processor=Unknown processor '%s'
4647

4748
warn.discontinued.double_dash_minecraftVersion='--minecraftVersion' is depracticed and planned for removal in the next Major KettingLauncher Version. Please use '-minecraftVersion' instead.
4849

50+
info.launcher.install_only.launch_script_success=Successfully created launch scripts, you can now launch Ketting with it
4951
info.launcher.install_only.success=Successfully installed Ketting, exiting install only mode
5052
info.launcher.launching=Launching Ketting...
5153
info.launcher.server_download=Downloading server from determined Minecraft version %s...

0 commit comments

Comments
 (0)