Skip to content

Commit

Permalink
Fix[gui_launcher]: general fixes
Browse files Browse the repository at this point in the history
- Re-add missing return
- Add comment on why Java version is limited to 17 or below
- Allow null javaArgs
- Remove excessive logging
  • Loading branch information
artdeell committed Jul 27, 2024
1 parent 3b1ad64 commit ae48e8e
Showing 1 changed file with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,10 @@ public Runtime selectRuntime(File modFile) {
}
Runtime selectedRuntime = MultiRTUtils.forceReread(nearestRuntime);
int selectedJavaVersion = Math.max(javaVersion, selectedRuntime.javaVersion);
// Don't allow versions higher than Java 17 because our caciocavallo implementation does not allow for it
if(selectedJavaVersion > 17) {
finalErrorDialog(getString(R.string.execute_jar_incompatible_runtime, selectedJavaVersion));
return null;
}
return selectedRuntime;
}
Expand All @@ -233,9 +235,9 @@ private File findModPath(List<String> argList) {
private void startModInstaller(File modFile, String javaArgs) {
new Thread(() -> {
// Maybe replace with more advanced arg parsing logic later
List<String> argList = Arrays.asList(javaArgs.split(" "));
List<String> argList = javaArgs != null ? Arrays.asList(javaArgs.split(" ")) : null;
File selectedMod = modFile;
if(selectedMod == null) {
if(selectedMod == null && argList != null) {
// If modFile is not specified directly, try to extract the -jar argument from the javaArgs
selectedMod = findModPath(argList);
}
Expand Down Expand Up @@ -356,8 +358,9 @@ public void launchJavaRuntime(Runtime runtime, File modFile, List<String> javaAr

// Enable Caciocavallo
Tools.getCacioJavaArgs(javaArgList,runtime.javaVersion == 8);

javaArgList.addAll(javaArgs);
if(javaArgs != null) {
javaArgList.addAll(javaArgs);
}
if(modFile != null) {
javaArgList.add("-jar");
javaArgList.add(modFile.getAbsolutePath());
Expand Down Expand Up @@ -397,30 +400,24 @@ public void performPaste(View view) {
public int getJavaVersion(File modFile) {
try (ZipFile zipFile = new ZipFile(modFile)){
ZipEntry manifest = zipFile.getEntry("META-INF/MANIFEST.MF");
Log.i("JavaVersion", "Mf entry: "+manifest);
if(manifest == null) return -1;
Log.i("JavaVersion", "Found manifest entry");

String manifestString = Tools.read(zipFile.getInputStream(manifest));
String mainClass = Tools.extractUntilCharacter(manifestString, "Main-Class:", '\n');
if(mainClass == null) return -1;
Log.i("JavaVersion", "Found main class: "+mainClass);

mainClass = mainClass.trim().replace('.', '/') + ".class";
ZipEntry mainClassFile = zipFile.getEntry(mainClass);
if(mainClassFile == null) return -1;
Log.i("JavaVersion", "Found main class entry: "+mainClass);

InputStream classStream = zipFile.getInputStream(mainClassFile);
byte[] bytesWeNeed = new byte[8];
int readCount = classStream.read(bytesWeNeed);
classStream.close();
if(readCount < 8) return -1;
Log.i("JavaVersion", "File is long enough!");

ByteBuffer byteBuffer = ByteBuffer.wrap(bytesWeNeed);
if(byteBuffer.getInt() != 0xCAFEBABE) return -1;
Log.i("JavaVersion", "File is Java class!");
short minorVersion = byteBuffer.getShort();
short majorVersion = byteBuffer.getShort();
Log.i("JavaGUILauncher", majorVersion+","+minorVersion);
Expand Down

0 comments on commit ae48e8e

Please sign in to comment.