From 67fac9a61078e7fb1d162902436b5cf7ad37c81b Mon Sep 17 00:00:00 2001 From: artdeell Date: Thu, 22 Jul 2021 16:20:15 +0300 Subject: [PATCH] Changes "Localize" renderer selection mechanism to several other classes Add renderer changer to Per-Version Config Move Per-Version Config to a separate class --- .../net/kdt/pojavlaunch/BaseMainActivity.java | 21 +-- .../main/java/net/kdt/pojavlaunch/Tools.java | 8 +- .../prefs/LauncherPreferences.java | 18 +-- .../prefs/PerVersionConfigDialog.java | 120 ++++++++++++++++++ .../tasks/RefreshVersionListTask.java | 55 +------- .../net/kdt/pojavlaunch/utils/JREUtils.java | 42 ++++-- .../pojavlaunch/value/PerVersionConfig.java | 1 + .../src/main/res/layout/pvc_popup.xml | 11 ++ 8 files changed, 182 insertions(+), 94 deletions(-) create mode 100644 app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/PerVersionConfigDialog.java diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/BaseMainActivity.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/BaseMainActivity.java index 963179274..54330cfea 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/BaseMainActivity.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/BaseMainActivity.java @@ -103,7 +103,7 @@ public class BaseMainActivity extends LoggableActivity { private File logFile; private PrintStream logStream; - + private PerVersionConfig.VersionConfig config; /* private LinearLayout contentCanvas; private AWTSurfaceView contentCanvasView; @@ -140,14 +140,17 @@ public class BaseMainActivity extends LoggableActivity { setTitle("Minecraft " + mProfile.selectedVersion); PerVersionConfig.update(); - PerVersionConfig.VersionConfig cfg = PerVersionConfig.configMap.get(mProfile.selectedVersion); + config = PerVersionConfig.configMap.get(mProfile.selectedVersion); String runtime = LauncherPreferences.PREF_DEFAULT_RUNTIME; - if(cfg != null) { - if(cfg.selectedRuntime != null) { - if(MultiRTUtils.forceReread(cfg.selectedRuntime).versionString != null) { - runtime = cfg.selectedRuntime; + if(config != null) { + if(config.selectedRuntime != null) { + if(MultiRTUtils.forceReread(config.selectedRuntime).versionString != null) { + runtime = config.selectedRuntime; } } + if(config.renderer != null) { + Tools.LOCAL_RENDERER = config.renderer; + } } MultiRTUtils.setRuntimeNamed(this,runtime); // Minecraft 1.13+ @@ -904,10 +907,12 @@ public class BaseMainActivity extends LoggableActivity { mLogObserver.startWatching(); } */ - + if(Tools.LOCAL_RENDERER == null) { + Tools.LOCAL_RENDERER = LauncherPreferences.PREF_RENDERER; + } appendlnToLog("--------- beggining with launcher debug"); appendlnToLog("Info: Launcher version: " + BuildConfig.VERSION_NAME); - if (LauncherPreferences.PREF_RENDERER.equals("vulkan_zink")) { + if (Tools.LOCAL_RENDERER.equals("vulkan_zink")) { checkVulkanZinkIsSupported(); } checkLWJGL3Installed(); 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 fd8c28407..4ea68c69e 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java @@ -45,6 +45,7 @@ public final class Tools public static String DIR_DATA = "/data/data/" + BuildConfig.APPLICATION_ID; public static String MULTIRT_HOME = DIR_DATA+"/runtimes"; + public static String LOCAL_RENDERER = null; public static String CURRENT_ARCHITECTURE; // New since 3.3.1 @@ -79,7 +80,8 @@ public final class Tools ctx.runOnUiThread(() -> { androidx.appcompat.app.AlertDialog.Builder b = new androidx.appcompat.app.AlertDialog.Builder(ctx) .setMessage(ctx.getString(R.string.memory_warning_msg,(mi.availMem/1048576L),LauncherPreferences.PREF_RAM_ALLOCATION)) - .setPositiveButton(android.R.string.ok, (dialogInterface, i) -> {synchronized(memoryErrorLock){memoryErrorLock.notifyAll();}}); + .setPositiveButton(android.R.string.ok, (dialogInterface, i) -> {synchronized(memoryErrorLock){memoryErrorLock.notifyAll();}}) + .setOnCancelListener((i) -> {synchronized(memoryErrorLock){memoryErrorLock.notifyAll();}}); b.show(); }); synchronized (memoryErrorLock) { @@ -155,7 +157,7 @@ public final class Tools javaArgList.add(cacioClasspath.toString()); } - public static void getJavaArgs(Context ctx, List javaArgList) { + public static void getJavaArgs(Context ctx, List javaArgList, String renderLib) { List overrideableArgList = new ArrayList(); overrideableArgList.add("-Djava.home=" + Tools.DIR_HOME_JRE); @@ -173,7 +175,7 @@ public final class Tools // javaArgList.add("-Dorg.lwjgl.libname=liblwjgl3.so"); // javaArgList.add("-Dorg.lwjgl.system.jemalloc.libname=libjemalloc.so"); - overrideableArgList.add("-Dorg.lwjgl.opengl.libname=" + LauncherPreferences.PREF_CUSTOM_OPENGL_LIBNAME); + overrideableArgList.add("-Dorg.lwjgl.opengl.libname=" + renderLib); // overrideableArgList.add("-Dorg.lwjgl.opengl.libname=libgl4es_115.so"); // javaArgList.add("-Dorg.lwjgl.opengl.libname=libRegal.so"); diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/LauncherPreferences.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/LauncherPreferences.java index 5f7767125..b6b82e15d 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/LauncherPreferences.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/LauncherPreferences.java @@ -85,23 +85,7 @@ public class LauncherPreferences if (PREF_RENDERER.equals("2") || PREF_RENDERER.equals("3")) { PREF_RENDERER = "opengles" + PREF_RENDERER; - } - - switch (PREF_RENDERER) { - case "opengles2": - PREF_CUSTOM_OPENGL_LIBNAME = "libgl4es_114.so"; - break; - case "opengles2_5": - case "opengles3": - PREF_CUSTOM_OPENGL_LIBNAME = "libgl4es_115.so"; - break; - case "vulkan_zink": - PREF_CUSTOM_OPENGL_LIBNAME = "libOSMesa_8.so"; - break; - default: - throw new RuntimeException("Undefined renderer: " + PREF_RENDERER); - } - + } String argLwjglLibname = "-Dorg.lwjgl.opengl.libname="; for (String arg : PREF_CUSTOM_JAVA_ARGS.split(" ")) { if (arg.startsWith(argLwjglLibname)) { diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/PerVersionConfigDialog.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/PerVersionConfigDialog.java new file mode 100644 index 000000000..44a92a176 --- /dev/null +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/prefs/PerVersionConfigDialog.java @@ -0,0 +1,120 @@ +package net.kdt.pojavlaunch.prefs; + +import android.content.Context; +import android.content.DialogInterface; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.EditText; +import android.widget.Spinner; + +import androidx.appcompat.app.AlertDialog; + +import net.kdt.pojavlaunch.R; +import net.kdt.pojavlaunch.multirt.MultiRTUtils; +import net.kdt.pojavlaunch.multirt.RTSpinnerAdapter; +import net.kdt.pojavlaunch.tasks.RefreshVersionListTask; +import net.kdt.pojavlaunch.value.PerVersionConfig; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +public class PerVersionConfigDialog{ + final Context ctx; + final AlertDialog dialog; + final View v; + List runtimes; + final Spinner javaVMSpinner; + final Spinner rendererSpinner; + final EditText customDirText; + final EditText jvmArgsEditText; + final List renderNames; + String selectedGameVersion = null; + public PerVersionConfigDialog(Context _ctx) { + ctx = _ctx; + v = LayoutInflater.from(ctx).inflate(R.layout.pvc_popup,null); + javaVMSpinner = v.findViewById(R.id.pvc_javaVm); + rendererSpinner = v.findViewById(R.id.pvc_renderer); + { + List renderList = new ArrayList<>(); + Collections.addAll(renderList, ctx.getResources().getStringArray(R.array.renderer)); + renderList.add("Default"); + renderNames = Arrays.asList(ctx.getResources().getStringArray(R.array.renderer_values)); + rendererSpinner.setAdapter(new ArrayAdapter<>(ctx, android.R.layout.simple_spinner_dropdown_item,renderList)); + } + customDirText = v.findViewById(R.id.pvc_customDir); + jvmArgsEditText = v.findViewById(R.id.pvc_jvmArgs); + AlertDialog.Builder builder = new AlertDialog.Builder(ctx); + builder.setView(v); + builder.setTitle("Per-version settings"); + builder.setNegativeButton(android.R.string.cancel,(dialogInterface,i)->dialogInterface.dismiss()); + builder.setPositiveButton(android.R.string.ok,this::save); + dialog = builder.create(); + } + public void refreshRuntimes() { + if(runtimes!=null)runtimes.clear(); + runtimes = MultiRTUtils.getRuntimes(); + //runtimes.add(new MultiRTUtils.Runtime("")); + } + private void save(DialogInterface i, int which) { + if(selectedGameVersion == null) { + i.dismiss(); + return; + } + PerVersionConfig.VersionConfig conf1 = PerVersionConfig.configMap.get(selectedGameVersion); + if(conf1==null){ + conf1=new PerVersionConfig.VersionConfig(); + } + conf1.jvmArgs=jvmArgsEditText.getText().toString(); + conf1.gamePath=customDirText.getText().toString(); + + if(rendererSpinner.getSelectedItemPosition() == renderNames.size()) conf1.renderer = null; + else conf1.renderer = renderNames.get(rendererSpinner.getSelectedItemPosition()); + + String runtime=((MultiRTUtils.Runtime)javaVMSpinner.getSelectedItem()).name;; + if(!runtime.equals(""))conf1.selectedRuntime=runtime; + else conf1.selectedRuntime=null; + + PerVersionConfig.configMap.put(selectedGameVersion,conf1); + try{ + PerVersionConfig.update(); + }catch(IOException e){ + e.printStackTrace(); + } + } + public boolean openConfig(String selectedVersion) { + selectedGameVersion = selectedVersion; + try{ + PerVersionConfig.update(); + }catch(IOException e){ + e.printStackTrace(); + } + PerVersionConfig.VersionConfig conf=PerVersionConfig.configMap.get(selectedGameVersion); + refreshRuntimes(); + javaVMSpinner.setAdapter(new RTSpinnerAdapter(ctx,runtimes)); + { + int jvm_index = runtimes.indexOf(new MultiRTUtils.Runtime("")); + int rnd_index = rendererSpinner.getAdapter().getCount()-1; + if (conf != null) { + customDirText.setText(conf.gamePath); + jvmArgsEditText.setText(conf.jvmArgs); + if (conf.selectedRuntime != null) { + int nindex = runtimes.indexOf(new MultiRTUtils.Runtime(conf.selectedRuntime)); + if (nindex != -1) jvm_index = nindex; + } + if(conf.renderer != null) { + int nindex = renderNames.indexOf(conf.renderer); + if (nindex != -1) rnd_index = nindex; + } + } + javaVMSpinner.setSelection(jvm_index); + rendererSpinner.setSelection(rnd_index); + } + dialog.show(); + return true; + } +} diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/RefreshVersionListTask.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/RefreshVersionListTask.java index cd3d28bd9..11169c39b 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/RefreshVersionListTask.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/RefreshVersionListTask.java @@ -71,60 +71,9 @@ public class RefreshVersionListTask extends AsyncTask runtimes = MultiRTUtils.getRuntimes(); - final Spinner javaVMSpinner = ((Spinner)v.findViewById(R.id.pvc_javaVm)); - final EditText customDirText = ((EditText)v.findViewById(R.id.pvc_customDir)); - final EditText jvmArgsEditText = ((EditText)v.findViewById(R.id.pvc_jvmArgs)); - javaVMSpinner.setAdapter(new RTSpinnerAdapter(RefreshVersionListTask.this.mActivity,runtimes)); - try { - PerVersionConfig.update(); - }catch (IOException e) { - e.printStackTrace(); - } - PerVersionConfig.VersionConfig conf = PerVersionConfig.configMap.get(mActivity.mProfile.selectedVersion); - int index = runtimes.indexOf(new MultiRTUtils.Runtime("")); - - if(conf != null) { - customDirText.setText(conf.gamePath); - jvmArgsEditText.setText(conf.jvmArgs); - if(conf.selectedRuntime != null) { - int nindex = runtimes.indexOf(new MultiRTUtils.Runtime(conf.selectedRuntime)); - if(nindex != -1) index = nindex; - } - } - javaVMSpinner.setSelection(index); - builder.setView(v); - builder.setTitle("Per-version settings"); - builder.setNegativeButton(android.R.string.cancel, (dialogInterface, i) -> dialogInterface.dismiss()); - builder.setPositiveButton(android.R.string.ok, (dialogInterface, i) -> { - PerVersionConfig.VersionConfig conf1 = PerVersionConfig.configMap.get(mActivity.mProfile.selectedVersion); - if(conf1 == null) { - conf1 = new PerVersionConfig.VersionConfig(); - } - conf1.jvmArgs = jvmArgsEditText.getText().toString(); - conf1.gamePath = customDirText.getText().toString(); - String runtime = ((MultiRTUtils.Runtime)javaVMSpinner.getSelectedItem()).name;; - if(!runtime.equals("")) conf1.selectedRuntime = runtime; - else conf1.selectedRuntime = null; - PerVersionConfig.configMap.put(mActivity.mProfile.selectedVersion, conf1); - try { - PerVersionConfig.update(); - }catch (IOException e) { - e.printStackTrace(); - } - }); - builder.show(); - return true; - } - }; - mActivity.mVersionSelector.setOnLongClickListener(listener); + PerVersionConfigDialog dialog = new PerVersionConfigDialog(this.mActivity); + mActivity.mVersionSelector.setOnLongClickListener((v)->dialog.openConfig(mActivity.mProfile.selectedVersion)); mActivity.mVersionSelector.setOnItemSelectedListener(new OnItemSelectedListener(){ - @Override public void onItemSelected(AdapterView p1, View p2, int p3, long p4) { diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/utils/JREUtils.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/utils/JREUtils.java index fd85b8ef2..310c64c32 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/utils/JREUtils.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/utils/JREUtils.java @@ -86,16 +86,7 @@ public class JREUtils dlopen(f.getAbsolutePath()); } dlopen(nativeLibDir + "/libopenal.so"); - - // may not necessary - if (LauncherPreferences.PREF_CUSTOM_OPENGL_LIBNAME.equals("libgl4es_114.so")) { - LauncherPreferences.PREF_CUSTOM_OPENGL_LIBNAME = nativeLibDir + "/libgl4es_114.so"; - } - if (!dlopen(LauncherPreferences.PREF_CUSTOM_OPENGL_LIBNAME) && !dlopen(findInLdLibPath(LauncherPreferences.PREF_CUSTOM_OPENGL_LIBNAME))) { - System.err.println("Failed to load custom OpenGL library " + LauncherPreferences.PREF_CUSTOM_OPENGL_LIBNAME + ". Fallbacking to GL4ES."); - dlopen(nativeLibDir + "/libgl4es_114.so"); - } } public static Map readJREReleaseProperties() throws IOException { @@ -225,7 +216,7 @@ public class JREUtils envMap.put("REGAL_GL_RENDERER", "Regal"); envMap.put("REGAL_GL_VERSION", "4.5"); - envMap.put("POJAV_RENDERER", LauncherPreferences.PREF_RENDERER); + envMap.put("POJAV_RENDERER", Tools.LOCAL_RENDERER); envMap.put("AWTSTUB_WIDTH", Integer.toString(CallbackBridge.windowWidth > 0 ? CallbackBridge.windowWidth : CallbackBridge.physicalWidth)); envMap.put("AWTSTUB_HEIGHT", Integer.toString(CallbackBridge.windowHeight > 0 ? CallbackBridge.windowHeight : CallbackBridge.physicalHeight)); @@ -248,8 +239,8 @@ public class JREUtils if (glesMajor < 3) { //fallback to 2 since it's the minimum for the entire app envMap.put("LIBGL_ES","2"); - } else if (LauncherPreferences.PREF_RENDERER.startsWith("opengles")) { - envMap.put("LIBGL_ES", LauncherPreferences.PREF_RENDERER.replace("opengles", "").replace("_5", "")); + } else if (Tools.LOCAL_RENDERER.startsWith("opengles")) { + envMap.put("LIBGL_ES", Tools.LOCAL_RENDERER.replace("opengles", "").replace("_5", "")); } else { // TODO if can: other backends such as Vulkan. // Sure, they should provide GLES 3 support. @@ -273,10 +264,34 @@ public class JREUtils public static int launchJavaVM(final LoggableActivity ctx, final List args) throws Throwable { JREUtils.relocateLibPath(ctx); // ctx.appendlnToLog("LD_LIBRARY_PATH = " + JREUtils.LD_LIBRARY_PATH); + final String graphicsLib; + if(Tools.LOCAL_RENDERER != null){ + switch (Tools.LOCAL_RENDERER) { + case "opengles2": + graphicsLib = "libgl4es_114.so"; + break; + case "opengles2_5": + case "opengles3": + graphicsLib = "libgl4es_115.so"; + break; + case "vulkan_zink": + graphicsLib = "libOSMesa_8.so"; + break; + default: + throw new RuntimeException("Undefined renderer: " + Tools.LOCAL_RENDERER); + } + if (!dlopen(graphicsLib) && !dlopen(findInLdLibPath(graphicsLib))) { + System.err.println("Failed to load renderer " + graphicsLib + ". Falling back to GL4ES 1.1.4"); + Tools.LOCAL_RENDERER = "opengles2"; + dlopen(nativeLibDir + "/libgl4es_114.so"); + } + }else{ + graphicsLib = null; + } List javaArgList = new ArrayList(); javaArgList.add(Tools.DIR_HOME_JRE + "/bin/java"); - Tools.getJavaArgs(ctx, javaArgList); + Tools.getJavaArgs(ctx, javaArgList,graphicsLib); purgeArg(javaArgList,"-Xms"); purgeArg(javaArgList,"-Xmx"); /*if(Tools.CURRENT_ARCHITECTURE.contains("32") && ((mi.availMem / 1048576L)-50) > 300) { @@ -421,6 +436,7 @@ public class JREUtils static { System.loadLibrary("pojavexec"); System.loadLibrary("pojavexec_awt"); + dlopen("libxhook.so"); System.loadLibrary("istdio"); } } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/value/PerVersionConfig.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/value/PerVersionConfig.java index 5cab7330e..25b1a55e0 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/value/PerVersionConfig.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/value/PerVersionConfig.java @@ -28,5 +28,6 @@ public class PerVersionConfig { public String jvmArgs; public String gamePath; public String selectedRuntime; + public String renderer; } } diff --git a/app_pojavlauncher/src/main/res/layout/pvc_popup.xml b/app_pojavlauncher/src/main/res/layout/pvc_popup.xml index 6d674d2e2..ac4706826 100644 --- a/app_pojavlauncher/src/main/res/layout/pvc_popup.xml +++ b/app_pojavlauncher/src/main/res/layout/pvc_popup.xml @@ -44,4 +44,15 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/pvc_customDir" /> + + \ No newline at end of file