Fix|Refactor: Handle custom resolutions on custom paths

This commit is contained in:
Boulay Mathias 2022-11-01 16:37:37 +01:00
parent f52814f0dc
commit 00435f2fd7
7 changed files with 124 additions and 70 deletions

View File

@ -57,7 +57,7 @@ public class MainActivity extends BaseActivity {
private LoggerView loggerView;
MinecraftAccount mProfile;
MinecraftProfile minecraftProfile;
public static MinecraftProfile minecraftProfile;
private DrawerLayout drawerLayout;
private ListView navDrawer;
@ -75,6 +75,12 @@ public class MainActivity extends BaseActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mProfile = PojavProfile.getCurrentProfileContent(this, null);
if(LauncherProfiles.mainProfileJson == null) LauncherProfiles.update();
minecraftProfile = LauncherProfiles.mainProfileJson.profiles.get(LauncherPreferences.DEFAULT_PREF.getString(LauncherPreferences.PREF_KEY_CURRENT_PROFILE,""));
MCOptionUtils.load(Tools.getGameDirPath(minecraftProfile));
initLayout(R.layout.activity_basemain);
// Set the sustained performance mode for available APIs
@ -134,7 +140,7 @@ public class MainActivity extends BaseActivity {
loggerView = findViewById(R.id.mainLoggerView);
mControlLayout = findViewById(R.id.main_control_layout);
mProfile = PojavProfile.getCurrentProfileContent(this);
LauncherProfiles.update();
minecraftProfile = LauncherProfiles.mainProfileJson.profiles.get(LauncherPreferences.DEFAULT_PREF.getString(LauncherPreferences.PREF_KEY_CURRENT_PROFILE,""));
if(minecraftProfile == null) {
@ -144,8 +150,7 @@ public class MainActivity extends BaseActivity {
}
String runtime = LauncherPreferences.PREF_DEFAULT_RUNTIME;
mVersionInfo = Tools.getVersionInfo(null, BaseLauncherActivity.getVersionId(
minecraftProfile.lastVersionId));
mVersionInfo = Tools.getVersionInfo(BaseLauncherActivity.getVersionId(minecraftProfile.lastVersionId));
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) {
@ -304,9 +309,8 @@ public class MainActivity extends BaseActivity {
JREUtils.redirectAndPrintJRELog();
LauncherProfiles.update();
Tools.launchMinecraft(this, mProfile, BaseLauncherActivity.getVersionId(
minecraftProfile.lastVersionId));
LauncherProfiles.update();
Tools.launchMinecraft(this, mProfile, minecraftProfile);
}
private void checkJavaArgsIsLaunchable(String jreVersion) throws Throwable {

View File

@ -53,7 +53,7 @@ public class MinecraftGLSurface extends View {
private final TapDetector mSingleTapDetector = new TapDetector(1, TapDetector.DETECTION_METHOD_BOTH);
private final TapDetector mDoubleTapDetector = new TapDetector(2, TapDetector.DETECTION_METHOD_DOWN);
/* MC GUI scale, listened by MCOptionUtils */
private int mGuiScale = getMcScale();
private int mGuiScale;
private final MCOptionUtils.MCOptionListener mGuiScaleListener = () -> mGuiScale = getMcScale();
/* Surface ready listener, used by the activity to launch minecraft */
SurfaceReadyListener mSurfaceReadyListener = null;
@ -621,7 +621,6 @@ public class MinecraftGLSurface extends View {
refreshSize();
//Load Minecraft options:
MCOptionUtils.load();
MCOptionUtils.set("fullscreen", "off");
MCOptionUtils.set("overrideWidth", String.valueOf(windowWidth));
MCOptionUtils.set("overrideHeight", String.valueOf(windowHeight));

View File

@ -281,7 +281,7 @@ public class PojavLauncherActivity extends BaseLauncherActivity
}
private void pickAccount() {
try {
mProfile = PojavProfile.getCurrentProfileContent(this);
mProfile = PojavProfile.getCurrentProfileContent(this, null);
accountFaceImageView.setImageBitmap(mProfile.getSkinFace());
//TODO FULL BACKGROUND LOGIN

View File

@ -5,6 +5,9 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.gson.JsonSyntaxException;
import java.io.File;
import java.io.IOException;
@ -24,15 +27,10 @@ public class PojavProfile {
public static SharedPreferences getPrefs(Context ctx) {
return ctx.getSharedPreferences(PROFILE_PREF, Context.MODE_PRIVATE);
}
public static MinecraftAccount getCurrentProfileContent(Context ctx) throws JsonSyntaxException {
MinecraftAccount build = MinecraftAccount.load(getCurrentProfileName(ctx));
if (build == null) {
System.out.println("isTempProfile null? " + (getTempProfileContent() == null));
return getTempProfileContent();
}
return build;
}
public static MinecraftAccount getCurrentProfileContent(@NonNull Context ctx, @Nullable String profileName) throws JsonSyntaxException {
return MinecraftAccount.load(profileName == null ? getCurrentProfileName(ctx) : profileName);
}
public static MinecraftAccount getTempProfileContent() {
try {
@ -107,7 +105,7 @@ public class PojavProfile {
if(!LauncherProfiles.mainProfileJson.profilesWereMigrated && LauncherProfiles.mainProfileJson.profiles != null) {
MinecraftProfile defaultProfile = LauncherProfiles.mainProfileJson.profiles.get("(Default)");
if(defaultProfile != null) {
defaultProfile.lastVersionId = PojavProfile.getCurrentProfileContent(ctx).selectedVersion;
defaultProfile.lastVersionId = PojavProfile.getCurrentProfileContent(ctx, null).selectedVersion;
}
LauncherProfiles.mainProfileJson.profilesWereMigrated = true;
LauncherProfiles.update();

View File

@ -16,6 +16,8 @@ import java.net.URL;
import java.nio.charset.*;
import java.util.*;
import net.kdt.pojavlaunch.extra.ExtraConstants;
import net.kdt.pojavlaunch.extra.ExtraCore;
import net.kdt.pojavlaunch.prefs.*;
import net.kdt.pojavlaunch.utils.*;
import net.kdt.pojavlaunch.value.*;
@ -31,6 +33,8 @@ import static android.os.Build.VERSION_CODES.P;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_IGNORE_NOTCH;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_NOTCH_SIZE;
import androidx.annotation.NonNull;
public final class Tools {
public static final boolean ENABLE_DEV_FEATURES = BuildConfig.DEBUG;
@ -94,7 +98,7 @@ public final class Tools {
}
public static void launchMinecraft(final Activity activity, MinecraftAccount profile, String versionName) throws Throwable {
public static void launchMinecraft(final Activity activity, MinecraftAccount minecraftAccount, MinecraftProfile minecraftProfile) throws Throwable {
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
((ActivityManager)activity.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryInfo(mi);
if(LauncherPreferences.PREF_RAM_ALLOCATION > (mi.availMem/1048576L)) {
@ -111,26 +115,25 @@ public final class Tools {
}
}
JMinecraftVersionList.Version versionInfo = Tools.getVersionInfo(null,versionName);
String gamedirPath = Tools.DIR_GAME_NEW;
if(activity instanceof MainActivity) {
LauncherProfiles.update();
MinecraftProfile minecraftProfile = ((MainActivity)activity).minecraftProfile;
if(minecraftProfile == null) throw new Exception("Launching empty Profile");
if(minecraftProfile.gameDir != null && minecraftProfile.gameDir.startsWith(Tools.LAUNCHERPROFILES_RTPREFIX))
gamedirPath = minecraftProfile.gameDir.replace(Tools.LAUNCHERPROFILES_RTPREFIX,Tools.DIR_GAME_HOME+"/");
if(minecraftProfile.javaArgs != null && !minecraftProfile.javaArgs.isEmpty())
LauncherPreferences.PREF_CUSTOM_JAVA_ARGS = minecraftProfile.javaArgs;
}
PojavLoginActivity.disableSplash(gamedirPath);
String[] launchArgs = getMinecraftClientArgs(profile, versionInfo, gamedirPath);
JMinecraftVersionList.Version versionInfo = Tools.getVersionInfo(MainActivity.minecraftProfile.lastVersionId);
LauncherProfiles.update();
String gamedirPath = Tools.getGameDirPath(minecraftProfile);
if(minecraftProfile.javaArgs != null && !minecraftProfile.javaArgs.isEmpty())
LauncherPreferences.PREF_CUSTOM_JAVA_ARGS = minecraftProfile.javaArgs;
// Pre-process specific files
disableSplash(gamedirPath);
String[] launchArgs = getMinecraftClientArgs(minecraftAccount, versionInfo, gamedirPath);
// Select the appropriate openGL version
OldVersionsUtils.selectOpenGlVersion(versionInfo);
// ctx.appendlnToLog("Minecraft Args: " + Arrays.toString(launchArgs));
String launchClassPath = generateLaunchClassPath(versionInfo,versionName);
String launchClassPath = generateLaunchClassPath(versionInfo, minecraftProfile.lastVersionId);
List<String> javaArgList = new ArrayList<String>();
@ -156,7 +159,7 @@ public final class Tools {
}
javaArgList.add("-Dlog4j.configurationFile=" + configFile);
}
javaArgList.addAll(Arrays.asList(getMinecraftJVMArgs(versionName, gamedirPath)));
javaArgList.addAll(Arrays.asList(getMinecraftJVMArgs(minecraftProfile.lastVersionId, gamedirPath)));
javaArgList.add("-cp");
javaArgList.add(getLWJGL3ClassPath() + ":" + launchClassPath);
@ -165,6 +168,38 @@ public final class Tools {
// ctx.appendlnToLog("full args: "+javaArgList.toString());
JREUtils.launchJavaVM(activity, javaArgList);
}
public static String getGameDirPath(@NonNull MinecraftProfile minecraftProfile){
if(minecraftProfile.gameDir != null){
if(minecraftProfile.gameDir.startsWith(Tools.LAUNCHERPROFILES_RTPREFIX))
return minecraftProfile.gameDir.replace(Tools.LAUNCHERPROFILES_RTPREFIX,Tools.DIR_GAME_HOME+"/");
else
return Tools.DIR_GAME_HOME + minecraftProfile.gameDir;
}
return Tools.DIR_GAME_NEW;
}
private static boolean mkdirs(String path) {
File file = new File(path);
return file.mkdirs();
}
public static void disableSplash(String dir) {
mkdirs(dir + "/config");
File forgeSplashFile = new File(dir, "config/splash.properties");
String forgeSplashContent = "enabled=true";
try {
if (forgeSplashFile.exists()) {
forgeSplashContent = Tools.read(forgeSplashFile.getAbsolutePath());
}
if (forgeSplashContent.contains("enabled=true")) {
Tools.write(forgeSplashFile.getAbsolutePath(),
forgeSplashContent.replace("enabled=true", "enabled=false"));
}
} catch (IOException e) {
Log.w(Tools.APP_NAME, "Could not disable Forge 1.12.2 and below splash screen!", e);
}
}
public static void getCacioJavaArgs(List<String> javaArgList, boolean isJava8) {
// Caciocavallo config AWT-enabled version
@ -215,7 +250,7 @@ public final class Tools {
}
public static String[] getMinecraftJVMArgs(String versionName, String strGameDir) {
JMinecraftVersionList.Version versionInfo = Tools.getVersionInfo(null, versionName, true);
JMinecraftVersionList.Version versionInfo = Tools.getVersionInfo(versionName, true);
// Parse Forge 1.17+ additional JVM Arguments
if (versionInfo.inheritsFrom == null || versionInfo.arguments == null || versionInfo.arguments.jvm == null) {
return new String[0];
@ -605,11 +640,11 @@ public final class Tools {
return libDir.toArray(new String[0]);
}
public static JMinecraftVersionList.Version getVersionInfo(BaseLauncherActivity bla, String versionName) {
return getVersionInfo(bla, versionName, false);
public static JMinecraftVersionList.Version getVersionInfo(String versionName) {
return getVersionInfo(versionName, false);
}
public static JMinecraftVersionList.Version getVersionInfo(BaseLauncherActivity bla, String versionName, boolean skipInheriting) {
public static JMinecraftVersionList.Version getVersionInfo(String versionName, boolean skipInheriting) {
try {
JMinecraftVersionList.Version customVer = Tools.GLOBAL_GSON.fromJson(read(DIR_HOME_VERSION + "/" + versionName + "/" + versionName + ".json"), JMinecraftVersionList.Version.class);
for (DependentLibrary lib : customVer.libraries) {
@ -621,24 +656,23 @@ public final class Tools {
return customVer;
} else {
JMinecraftVersionList.Version inheritsVer = null;
if(bla != null) if (bla.mVersionList != null) {
for (JMinecraftVersionList.Version valueVer : bla.mVersionList.versions) {
if (valueVer.id.equals(customVer.inheritsFrom) && (!new File(DIR_HOME_VERSION + "/" + customVer.inheritsFrom + "/" + customVer.inheritsFrom + ".json").exists()) && (valueVer.url != null)) {
Tools.downloadFile(valueVer.url,DIR_HOME_VERSION + "/" + customVer.inheritsFrom + "/" + customVer.inheritsFrom + ".json");
}
for (JMinecraftVersionList.Version valueVer : ((JMinecraftVersionList) ExtraCore.getValue(ExtraConstants.RELEASE_TABLE)).versions) {
if (valueVer.id.equals(customVer.inheritsFrom) && (!new File(DIR_HOME_VERSION + "/" + customVer.inheritsFrom + "/" + customVer.inheritsFrom + ".json").exists()) && (valueVer.url != null)) {
Tools.downloadFile(valueVer.url,DIR_HOME_VERSION + "/" + customVer.inheritsFrom + "/" + customVer.inheritsFrom + ".json");
}
}//If it won't download, just search for it
try{
inheritsVer = Tools.GLOBAL_GSON.fromJson(read(DIR_HOME_VERSION + "/" + customVer.inheritsFrom + "/" + customVer.inheritsFrom + ".json"), JMinecraftVersionList.Version.class);
}catch(IOException e) {
throw new RuntimeException("Can't find the source version for "+ versionName +" (req version="+customVer.inheritsFrom+")");
}
}
//If it won't download, just search for it
try{
inheritsVer = Tools.GLOBAL_GSON.fromJson(read(DIR_HOME_VERSION + "/" + customVer.inheritsFrom + "/" + customVer.inheritsFrom + ".json"), JMinecraftVersionList.Version.class);
}catch(IOException e) {
throw new RuntimeException("Can't find the source version for "+ versionName +" (req version="+customVer.inheritsFrom+")");
}
//inheritsVer.inheritsFrom = inheritsVer.id;
insertSafety(inheritsVer, customVer,
"assetIndex", "assets", "id",
"mainClass", "minecraftArguments",
"optifineLib", "releaseTime", "time", "type"
);
"assetIndex", "assets", "id",
"mainClass", "minecraftArguments",
"optifineLib", "releaseTime", "time", "type"
);
List<DependentLibrary> libList = new ArrayList<DependentLibrary>(Arrays.asList(inheritsVer.libraries));
try {
@ -648,11 +682,11 @@ public final class Tools {
for (int i = 0; i < libList.size(); i++) {
DependentLibrary libAdded = libList.get(i);
String libAddedName = libAdded.name.substring(0, libAdded.name.lastIndexOf(":"));
if (libAddedName.equals(libName)) {
Log.d(APP_NAME, "Library " + libName + ": Replaced version " +
libName.substring(libName.lastIndexOf(":") + 1) + " with " +
libAddedName.substring(libAddedName.lastIndexOf(":") + 1));
Log.d(APP_NAME, "Library " + libName + ": Replaced version " +
libName.substring(libName.lastIndexOf(":") + 1) + " with " +
libAddedName.substring(libAddedName.lastIndexOf(":") + 1));
libList.set(i, lib);
continue loop_1;
}
@ -668,14 +702,14 @@ public final class Tools {
if (inheritsVer.arguments != null && customVer.arguments != null) {
List totalArgList = new ArrayList();
totalArgList.addAll(Arrays.asList(inheritsVer.arguments.game));
int nskip = 0;
for (int i = 0; i < customVer.arguments.game.length; i++) {
if (nskip > 0) {
nskip--;
continue;
}
Object perCustomArg = customVer.arguments.game[i];
if (perCustomArg instanceof String) {
String perCustomArgStr = (String) perCustomArg;

View File

@ -89,7 +89,7 @@ public class MinecraftDownloaderTask extends AsyncTask<String, String, Throwable
}
}
verInfo = Tools.getVersionInfo(mActivity,p1[0]);
verInfo = Tools.getVersionInfo(p1[0]);
//Now we have the reliable information to check if our runtime settings are good enough
if(verInfo.javaVersion != null && !verInfo.javaVersion.component.equalsIgnoreCase("jre-legacy")) { //1.17+
@ -522,6 +522,6 @@ public class MinecraftDownloaderTask extends AsyncTask<String, String, Throwable
}
// Custom version, inherits from base.
return Tools.getVersionInfo(mActivity,version);
return Tools.getVersionInfo(version);
}
}

View File

@ -6,6 +6,7 @@ import android.os.Build;
import android.os.FileObserver;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.kdt.pojavlaunch.Tools;
@ -19,25 +20,43 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
public class MCOptionUtils {
private static final HashMap<String,String> sParameterMap = new HashMap<>();
private static final ArrayList<WeakReference<MCOptionListener>> sOptionListeners = new ArrayList<>();
private static FileObserver sFileObserver;
private static String sOptionFolderPath = null;
public interface MCOptionListener {
/** Called when an option is changed. Don't know which one though */
void onOptionChanged();
}
public static void load() {
if(sFileObserver == null){
public static void load(){
load(sOptionFolderPath == null
? Tools.DIR_GAME_NEW
: sOptionFolderPath);
}
public static void load(@NonNull String folderPath) {
File optionFile = new File(folderPath + "/options.txt");
if(!optionFile.exists()) {
try { // Needed for new instances I guess :think:
optionFile.createNewFile();
} catch (IOException e) { e.printStackTrace(); }
}
if(sFileObserver == null || !Objects.equals(sOptionFolderPath, folderPath)){
sOptionFolderPath = folderPath;
setupFileObserver();
}
sOptionFolderPath = folderPath; // Yeah I know, it may be redundant
sParameterMap.clear();
try {
BufferedReader reader = new BufferedReader(new FileReader(Tools.DIR_GAME_NEW + "/options.txt"));
BufferedReader reader = new BufferedReader(new FileReader(sOptionFolderPath + "/options.txt"));
String line;
while ((line = reader.readLine()) != null) {
int firstColonIndex = line.indexOf(':');
@ -89,7 +108,7 @@ public class MCOptionUtils {
.append('\n');
try {
Tools.write(Tools.DIR_GAME_NEW + "/options.txt", result.toString());
Tools.write(sOptionFolderPath + "/options.txt", result.toString());
} catch (IOException e) {
Log.w(Tools.APP_NAME, "Could not save options.txt", e);
}
@ -113,7 +132,7 @@ public class MCOptionUtils {
* Listeners get notified of the change */
private static void setupFileObserver(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
sFileObserver = new FileObserver(new File(Tools.DIR_GAME_NEW + "/options.txt"), FileObserver.MODIFY) {
sFileObserver = new FileObserver(new File(sOptionFolderPath + "/options.txt"), FileObserver.MODIFY) {
@Override
public void onEvent(int i, @Nullable String s) {
MCOptionUtils.load();
@ -121,7 +140,7 @@ public class MCOptionUtils {
}
};
}else{
sFileObserver = new FileObserver(Tools.DIR_GAME_NEW + "/options.txt", FileObserver.MODIFY) {
sFileObserver = new FileObserver(sOptionFolderPath + "/options.txt", FileObserver.MODIFY) {
@Override
public void onEvent(int i, @Nullable String s) {
MCOptionUtils.load();