From 4115b997f84dead49cb5b495546f9ff102a37fe3 Mon Sep 17 00:00:00 2001 From: artdeell Date: Mon, 1 May 2023 12:00:42 +0300 Subject: [PATCH] Better runtime autoselection logic + more cleanup --- .../java/net/kdt/pojavlaunch/JRE17Util.java | 9 ++- .../net/kdt/pojavlaunch/MainActivity.java | 79 +++++++++++-------- .../main/java/net/kdt/pojavlaunch/Tools.java | 43 +++++++--- 3 files changed, 82 insertions(+), 49 deletions(-) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/JRE17Util.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/JRE17Util.java index d7aec84be..f004f1307 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/JRE17Util.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/JRE17Util.java @@ -8,7 +8,6 @@ import android.util.Log; import net.kdt.pojavlaunch.multirt.MultiRTUtils; import net.kdt.pojavlaunch.multirt.Runtime; -import net.kdt.pojavlaunch.prefs.LauncherPreferences; import net.kdt.pojavlaunch.value.launcherprofiles.LauncherProfiles; import net.kdt.pojavlaunch.value.launcherprofiles.MinecraftProfile; @@ -59,10 +58,12 @@ public class JRE17Util { LauncherProfiles.update(); MinecraftProfile minecraftProfile = LauncherProfiles.getCurrentProfile(); - String selectedRuntime = Tools.pickRuntime(minecraftProfile); + String selectedRuntime = Tools.getSelectedRuntime(minecraftProfile); - Runtime runtime = selectedRuntime != null ? MultiRTUtils.read(selectedRuntime) : MultiRTUtils.read(LauncherPreferences.PREF_DEFAULT_RUNTIME); - if (runtime.javaVersion >= versionInfo.javaVersion.majorVersion) return true; + Runtime runtime = MultiRTUtils.read(selectedRuntime); + if (runtime.javaVersion >= versionInfo.javaVersion.majorVersion) { + return true; + } String appropriateRuntime = MultiRTUtils.getNearestJreName(versionInfo.javaVersion.majorVersion); if (appropriateRuntime != null) { diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MainActivity.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MainActivity.java index 55df74359..9cc5ad604 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MainActivity.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MainActivity.java @@ -5,45 +5,61 @@ import static net.kdt.pojavlaunch.Tools.currentDisplayMetrics; import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_SUSTAINED_PERFORMANCE; import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_USE_ALTERNATE_SURFACE; import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_VIRTUAL_MOUSE_START; - import static org.lwjgl.glfw.CallbackBridge.sendKeyPress; import static org.lwjgl.glfw.CallbackBridge.windowHeight; import static org.lwjgl.glfw.CallbackBridge.windowWidth; -import android.app.*; -import android.content.*; +import android.app.Activity; +import android.app.AlertDialog; +import android.content.ClipboardManager; +import android.content.Context; +import android.content.Intent; import android.content.pm.PackageManager; import android.content.res.Configuration; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; -import android.os.*; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; import android.provider.DocumentsContract; -import android.util.*; -import android.view.*; +import android.util.Log; +import android.view.KeyEvent; +import android.view.LayoutInflater; +import android.view.View; import android.webkit.MimeTypeMap; -import android.widget.*; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.SeekBar; +import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; -import androidx.drawerlayout.widget.*; +import androidx.drawerlayout.widget.DrawerLayout; import com.kdt.LoggerView; -import java.io.*; -import net.kdt.pojavlaunch.customcontrols.*; - +import net.kdt.pojavlaunch.customcontrols.ControlButtonMenuListener; +import net.kdt.pojavlaunch.customcontrols.ControlData; +import net.kdt.pojavlaunch.customcontrols.ControlDrawerData; +import net.kdt.pojavlaunch.customcontrols.ControlLayout; +import net.kdt.pojavlaunch.customcontrols.CustomControls; import net.kdt.pojavlaunch.customcontrols.keyboard.LwjglCharSender; import net.kdt.pojavlaunch.customcontrols.keyboard.TouchCharInput; -import net.kdt.pojavlaunch.multirt.MultiRTUtils; - -import net.kdt.pojavlaunch.prefs.*; +import net.kdt.pojavlaunch.prefs.LauncherPreferences; import net.kdt.pojavlaunch.services.GameService; -import net.kdt.pojavlaunch.utils.*; -import net.kdt.pojavlaunch.value.*; +import net.kdt.pojavlaunch.utils.JREUtils; +import net.kdt.pojavlaunch.utils.MCOptionUtils; +import net.kdt.pojavlaunch.value.MinecraftAccount; import net.kdt.pojavlaunch.value.launcherprofiles.LauncherProfiles; import net.kdt.pojavlaunch.value.launcherprofiles.MinecraftProfile; -import org.lwjgl.glfw.*; -import android.net.*; +import org.lwjgl.glfw.CallbackBridge; + +import java.io.File; +import java.io.IOException; public class MainActivity extends BaseActivity implements ControlButtonMenuListener{ public static volatile ClipboardManager GLOBAL_CLIPBOARD; @@ -61,8 +77,6 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe private GyroControl mGyroControl = null; public static ControlLayout mControlLayout; - - MinecraftAccount mProfile; MinecraftProfile minecraftProfile; private ArrayAdapter gameActionArrayAdapter; @@ -70,13 +84,9 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe public ArrayAdapter ingameControlsEditorArrayAdapter; public AdapterView.OnItemClickListener ingameControlsEditorListener; - protected volatile String mVersionId; - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - - mProfile = PojavProfile.getCurrentProfileContent(this, null); minecraftProfile = LauncherProfiles.getCurrentProfile(); MCOptionUtils.load(Tools.getGameDirPath(minecraftProfile).getAbsolutePath()); GameService.startService(this); @@ -138,8 +148,8 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe String version = getIntent().getStringExtra(INTENT_MINECRAFT_VERSION); version = version == null ? minecraftProfile.lastVersionId : version; - mVersionId = version; - isInputStackCall = Tools.getVersionInfo(mVersionId).arguments != null; + JMinecraftVersionList.Version mVersionInfo = Tools.getVersionInfo(version); + isInputStackCall = mVersionInfo.arguments != null; CallbackBridge.nativeSetUseInputStackQueue(isInputStackCall); Tools.getDisplayMetrics(this); @@ -166,7 +176,7 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe drawerLayout.closeDrawers(); - + final String finalVersion = version; minecraftGLView.setSurfaceReadyListener(() -> { try { // Setup virtual mouse right before launching @@ -174,7 +184,7 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe touchpad.post(() -> touchpad.switchState()); } - runCraft(); + runCraft(finalVersion, mVersionInfo); }catch (Throwable e){ Tools.showError(getApplicationContext(), e, true); } @@ -301,27 +311,30 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe return Build.VERSION.SDK_INT >= 26; } - private void runCraft() throws Throwable { + private void runCraft(String versionId, JMinecraftVersionList.Version version) throws Throwable { if(Tools.LOCAL_RENDERER == null) { Tools.LOCAL_RENDERER = LauncherPreferences.PREF_RENDERER; } + MinecraftAccount minecraftAccount = PojavProfile.getCurrentProfileContent(this, null); Logger.getInstance().appendToLog("--------- beginning with launcher debug"); - printLauncherInfo(); + printLauncherInfo(versionId); if (Tools.LOCAL_RENDERER.equals("vulkan_zink")) { checkVulkanZinkIsSupported(); } JREUtils.redirectAndPrintJRELog(); LauncherProfiles.update(); - Tools.launchMinecraft(this, mProfile, minecraftProfile, mVersionId); + int requiredJavaVersion = 8; + if(version.javaVersion != null) requiredJavaVersion = version.javaVersion.majorVersion; + Tools.launchMinecraft(this, minecraftAccount, minecraftProfile, versionId, requiredJavaVersion); } - private void printLauncherInfo() { + private void printLauncherInfo(String gameVersion) { Logger logger = Logger.getInstance(); logger.appendToLog("Info: Launcher version: " + BuildConfig.VERSION_NAME); logger.appendToLog("Info: Architecture: " + Architecture.archAsString(Tools.DEVICE_ARCHITECTURE)); logger.appendToLog("Info: Device model: " + Build.MANUFACTURER + " " +Build.MODEL); logger.appendToLog("Info: API version: " + Build.VERSION.SDK_INT); - logger.appendToLog("Info: Selected Minecraft version: " + mVersionId); + logger.appendToLog("Info: Selected Minecraft version: " + gameVersion); logger.appendToLog("Info: Custom Java arguments: \"" + LauncherPreferences.PREF_CUSTOM_JAVA_ARGS + "\""); } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java index a634334a1..782f3a404 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java @@ -15,7 +15,6 @@ import android.app.ProgressDialog; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.content.res.Configuration; import android.database.Cursor; @@ -152,7 +151,7 @@ public final class Tools { public static void launchMinecraft(final Activity activity, MinecraftAccount minecraftAccount, - MinecraftProfile minecraftProfile, String versionId) throws Throwable { + MinecraftProfile minecraftProfile, String versionId, int versionJavaRequirement) throws Throwable { int freeDeviceMemory = getFreeDeviceMemory(activity); if(LauncherPreferences.PREF_RAM_ALLOCATION > freeDeviceMemory) { Object memoryErrorLock = new Object(); @@ -167,7 +166,7 @@ public final class Tools { memoryErrorLock.wait(); } } - Runtime runtime = MultiRTUtils.forceReread(Tools.pickRuntime(minecraftProfile)); + Runtime runtime = MultiRTUtils.forceReread(Tools.pickRuntime(minecraftProfile, versionJavaRequirement)); JMinecraftVersionList.Version versionInfo = Tools.getVersionInfo(versionId); LauncherProfiles.update(); File gamedir = Tools.getGameDirPath(minecraftProfile); @@ -551,10 +550,10 @@ public final class Tools { Runnable runnable = () -> { final String errMsg = showMore ? printToString(e) : rolledMessage != null ? rolledMessage : e.getMessage(); - AlertDialog.Builder builder = new AlertDialog.Builder((Context) ctx) + AlertDialog.Builder builder = new AlertDialog.Builder(ctx) .setTitle(titleId) .setMessage(errMsg) - .setPositiveButton(android.R.string.ok, (DialogInterface.OnClickListener) (p1, p2) -> { + .setPositiveButton(android.R.string.ok, (p1, p2) -> { if(exitIfOk) { if (ctx instanceof MainActivity) { MainActivity.fullyExit(); @@ -563,8 +562,8 @@ public final class Tools { } } }) - .setNegativeButton(showMore ? R.string.error_show_less : R.string.error_show_more, (DialogInterface.OnClickListener) (p1, p2) -> showError(ctx, titleId, rolledMessage, e, exitIfOk, !showMore)) - .setNeutralButton(android.R.string.copy, (DialogInterface.OnClickListener) (p1, p2) -> { + .setNegativeButton(showMore ? R.string.error_show_less : R.string.error_show_more, (p1, p2) -> showError(ctx, titleId, rolledMessage, e, exitIfOk, !showMore)) + .setNeutralButton(android.R.string.copy, (p1, p2) -> { ClipboardManager mgr = (ClipboardManager) ctx.getSystemService(Context.CLIPBOARD_SERVICE); mgr.setPrimaryClip(ClipData.newPlainText("error", Log.getStackTraceString(e))); if(exitIfOk) { @@ -938,14 +937,34 @@ public final class Tools { public static boolean isValidString(String string) { return string != null && !string.isEmpty(); } - public static String pickRuntime(MinecraftProfile minecraftProfile) { + + public static String getRuntimeName(String prefixedName) { + if(prefixedName == null) return prefixedName; + if(!prefixedName.startsWith(Tools.LAUNCHERPROFILES_RTPREFIX)) return null; + return prefixedName.substring(Tools.LAUNCHERPROFILES_RTPREFIX.length()); + } + + public static String getSelectedRuntime(MinecraftProfile minecraftProfile) { String runtime = LauncherPreferences.PREF_DEFAULT_RUNTIME; - if(minecraftProfile.javaDir != null && minecraftProfile.javaDir.startsWith(Tools.LAUNCHERPROFILES_RTPREFIX)) { - String runtimeName = minecraftProfile.javaDir.substring(Tools.LAUNCHERPROFILES_RTPREFIX.length()); - if(MultiRTUtils.forceReread(runtimeName).versionString != null) { - runtime = runtimeName; + String profileRuntime = getRuntimeName(minecraftProfile.javaDir); + if(profileRuntime != null) { + if(MultiRTUtils.forceReread(profileRuntime).versionString != null) { + runtime = profileRuntime; } } return runtime; } + + public static @NonNull String pickRuntime(MinecraftProfile minecraftProfile, int targetJavaVersion) { + String runtime = getSelectedRuntime(minecraftProfile); + String profileRuntime = getRuntimeName(minecraftProfile.javaDir); + Runtime pickedRuntime = MultiRTUtils.read(runtime); + if(runtime == null || pickedRuntime.javaVersion == 0 || pickedRuntime.javaVersion < targetJavaVersion) { + String preferredRuntime = MultiRTUtils.getNearestJreName(targetJavaVersion); + if(preferredRuntime == null) throw new RuntimeException("Failed to autopick runtime!"); + if(profileRuntime != null) minecraftProfile.javaDir = Tools.LAUNCHERPROFILES_RTPREFIX+preferredRuntime; + runtime = preferredRuntime; + } + return runtime; + } }