Implement PVC migrator, remove PVC dialog, add a couple of helper methods and sanitize the code according to serpent's recommendations

This commit is contained in:
artdeell 2022-03-17 20:31:53 +03:00
parent 965cad4fd7
commit 19037b8db1
6 changed files with 41 additions and 163 deletions

View File

@ -159,41 +159,6 @@ public abstract class BaseLauncherActivity extends BaseActivity {
Tools.updateWindowSize(this);
System.out.println("call to onPostResume; E");
}
public void setupVersionSelector() {
final androidx.appcompat.widget.PopupMenu popup = new PopupMenu(this, mVersionSelector);
popup.getMenuInflater().inflate(R.menu.menu_versionopt, popup.getMenu());
PerVersionConfigDialog dialog = new PerVersionConfigDialog(this);
this.mVersionSelector.setOnLongClickListener((v)->dialog.openConfig(this.mProfile.selectedVersion));
this.mVersionSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> p1, View p2, int p3, long p4)
{
mProfile.selectedVersion = p1.getItemAtPosition(p3).toString();
PojavProfile.setCurrentProfile(BaseLauncherActivity.this, mProfile);
if (PojavProfile.isFileType(BaseLauncherActivity.this)) {
try {
PojavProfile.setCurrentProfile(BaseLauncherActivity.this, mProfile.save());
} catch (IOException e) {
Tools.showError(BaseLauncherActivity.this, e);
}
}
}
@Override
public void onNothingSelected(AdapterView<?> p1)
{
// TODO: Implement this method
}
});
popup.setOnMenuItemClickListener(item -> true);
}
ExtraListener<ArrayList<String>> versionListener;
@Override
protected void onPause() {
super.onPause();
}
public static void updateVersionSpinner(Context ctx, ArrayList<String> value, Spinner mVersionSelector, String defaultSelection) {
if(value != null && value.size() > 0) {

View File

@ -37,7 +37,6 @@ import net.kdt.pojavlaunch.fragments.ConsoleFragment;
import net.kdt.pojavlaunch.fragments.CrashFragment;
import net.kdt.pojavlaunch.fragments.LauncherFragment;
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
import net.kdt.pojavlaunch.prefs.PerVersionConfigDialog;
import net.kdt.pojavlaunch.prefs.screens.LauncherPreferenceFragment;
import net.kdt.pojavlaunch.profiles.ProfileAdapter;
import net.kdt.pojavlaunch.profiles.ProfileEditor;
@ -193,7 +192,7 @@ public class PojavLauncherActivity extends BaseLauncherActivity
//mAvailableVersions;
ProfileAdapter profileAdapter = new ProfileAdapter(this);
ProfileEditor editor = new ProfileEditor(this,(name, isNew, deleting)->{
ProfileEditor profileEditor = new ProfileEditor(this,(name, isNew, deleting)->{
LauncherProfiles.update();
if(isNew) {
mVersionSelector.setSelection(profileAdapter.resolveProfileIndex(name));
@ -203,7 +202,7 @@ public class PojavLauncherActivity extends BaseLauncherActivity
}
profileAdapter.notifyDataSetChanged();
});
mVersionSelector.setOnLongClickListener((v)->editor.show(mProfile.selectedProfile));
mVersionSelector.setOnLongClickListener((v)->profileEditor.show(mProfile.selectedProfile));
mVersionSelector.setAdapter(profileAdapter);
mVersionSelector.setSelection(profileAdapter.resolveProfileIndex(mProfile.selectedProfile));
mVersionSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@ -212,7 +211,7 @@ public class PojavLauncherActivity extends BaseLauncherActivity
{
String profileName = p1.getItemAtPosition(p3).toString();
if(profileName.equals(ProfileAdapter.CREATE_PROFILE_MAGIC)) {
editor.show(profileName);
profileEditor.show(profileName);
mVersionSelector.setSelection(0);
}else {
mProfile.selectedProfile = p1.getItemAtPosition(p3).toString();

View File

@ -51,6 +51,9 @@ import net.kdt.pojavlaunch.multirt.MultiRTUtils;
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
import net.kdt.pojavlaunch.utils.LocaleUtils;
import net.kdt.pojavlaunch.value.MinecraftAccount;
import net.kdt.pojavlaunch.value.PerVersionConfig;
import net.kdt.pojavlaunch.value.launcherprofiles.LauncherProfiles;
import net.kdt.pojavlaunch.value.launcherprofiles.MinecraftProfile;
import org.apache.commons.io.FileUtils;
@ -336,6 +339,7 @@ public class PojavLoginActivity extends BaseActivity {
mLockSelectJRE.wait();
}
}
migrateToProfiles();
if(Build.VERSION.SDK_INT > 28) runOnUiThread(this::showStorageDialog);
LauncherPreferences.loadPreferences(getApplicationContext());
}
@ -388,6 +392,33 @@ public class PojavLoginActivity extends BaseActivity {
}
}
}
private void migrateToProfiles() {
try {
if(!PerVersionConfig.exists()) return;
LauncherProfiles.update();
PerVersionConfig.update();
if(PerVersionConfig.erase()) {
for (String version : PerVersionConfig.configMap.keySet()) {
PerVersionConfig.VersionConfig config = PerVersionConfig.configMap.get(version);
if (config != null) {
MinecraftProfile profile = new MinecraftProfile();
profile.lastVersionId = version;
profile.name = getString(R.string.migrated_profile_str, version);
profile.pojavRendererName = config.renderer;
profile.gameDir = config.gamePath;
profile.javaDir = Tools.LAUNCHERPROFILES_RTPREFIX + config.selectedRuntime;
profile.javaArgs = config.jvmArgs;
LauncherProfiles.mainProfileJson.profiles.put("pvc-migrated-" + version, profile);
}
}
LauncherProfiles.update();
}else{
Log.e("ProfileMigrator"," Unable to remove Per Version Config files.");
}
}catch (IOException e) {
Log.e("ProfileMigrator","Failed to migrate!",e);
}
}
private boolean installRuntimeAutomatically(AssetManager am, boolean otherRuntimesAvailable) {
/* Check if JRE is included */
String rt_version = null;

View File

@ -1,124 +0,0 @@
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.multirt.Runtime;
import net.kdt.pojavlaunch.value.PerVersionConfig;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class PerVersionConfigDialog{
final Context mContext;
final AlertDialog mDialog;
final View mRootView;
List<Runtime> mRuntimes;
final Spinner mJvmSpinner;
final Spinner mRendererSpinner;
final EditText mCustomDirEditText;
final EditText mJvmArgsEditText;
final List<String> mRendererNames;
String mSelectedGameVersion = null;
public PerVersionConfigDialog(Context ctx) {
mContext = ctx;
mRootView = LayoutInflater.from(mContext).inflate(R.layout.dialog_per_version_control,null);
mJvmSpinner = mRootView.findViewById(R.id.pvc_javaVm);
mRendererSpinner = mRootView.findViewById(R.id.pvc_renderer);
ArrayList<String> renderList = new ArrayList<>(5);
Collections.addAll(renderList, mContext.getResources().getStringArray(R.array.renderer));
renderList.add("Default");
mRendererNames = Arrays.asList(mContext.getResources().getStringArray(R.array.renderer_values));
mRendererSpinner.setAdapter(new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_dropdown_item, renderList));
mCustomDirEditText = mRootView.findViewById(R.id.pvc_customDir);
mJvmArgsEditText = mRootView.findViewById(R.id.pvc_jvmArgs);
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setView(mRootView);
builder.setTitle(R.string.pvc_title);
builder.setNegativeButton(android.R.string.cancel, null);
builder.setPositiveButton(android.R.string.ok, this::save);
mDialog = builder.create();
}
public void refreshRuntimes() {
if(mRuntimes !=null) mRuntimes.clear();
mRuntimes = MultiRTUtils.getRuntimes();
//runtimes.add(new Runtime("<Default>"));
}
private void save(DialogInterface dialogInterface, int which) {
if(mSelectedGameVersion == null) {
dialogInterface.dismiss();
return;
}
PerVersionConfig.VersionConfig versionConfig = PerVersionConfig.configMap.get(mSelectedGameVersion);
if(versionConfig == null){
versionConfig = new PerVersionConfig.VersionConfig();
}
versionConfig.jvmArgs= mJvmArgsEditText.getText().toString();
versionConfig.gamePath= mCustomDirEditText.getText().toString();
if(mRendererSpinner.getSelectedItemPosition() == mRendererNames.size()) versionConfig.renderer = null;
else versionConfig.renderer = mRendererNames.get(mRendererSpinner.getSelectedItemPosition());
String runtime=((Runtime) mJvmSpinner.getSelectedItem()).name;;
if(!runtime.equals("<Default>"))versionConfig.selectedRuntime=runtime;
else versionConfig.selectedRuntime = null;
PerVersionConfig.configMap.put(mSelectedGameVersion, versionConfig);
try{
PerVersionConfig.update();
}catch(IOException e){
e.printStackTrace();
}
}
public boolean openConfig(String selectedVersion) {
mSelectedGameVersion = selectedVersion;
try{
PerVersionConfig.update();
}catch(IOException e){
e.printStackTrace();
}
PerVersionConfig.VersionConfig versionConfig = PerVersionConfig.configMap.get(mSelectedGameVersion);
refreshRuntimes();
mJvmSpinner.setAdapter(new RTSpinnerAdapter(mContext, mRuntimes));
int jvmIndex = mRuntimes.indexOf(new Runtime("<Default>"));
int rendererIndex = mRendererSpinner.getAdapter().getCount()-1;
if (versionConfig != null) {
mCustomDirEditText.setText(versionConfig.gamePath);
mJvmArgsEditText.setText(versionConfig.jvmArgs);
if (versionConfig.selectedRuntime != null) {
int nIndex = mRuntimes.indexOf(new Runtime(versionConfig.selectedRuntime));
if (nIndex != -1) jvmIndex = nIndex;
}
if(versionConfig.renderer != null) {
int nIndex = mRendererNames.indexOf(versionConfig.renderer);
if (nIndex != -1) rendererIndex = nIndex;
}
}
mJvmSpinner.setSelection(jvmIndex);
mRendererSpinner.setSelection(rendererIndex);
mDialog.show();
return true;
}
}

View File

@ -31,6 +31,12 @@ public class PerVersionConfig {
Tools.write(pvcFile.getAbsolutePath(),Tools.GLOBAL_GSON.toJson(configMap));
}
}
public static boolean erase() {
return new File(Tools.DIR_GAME_HOME,"per-version-config.json").delete();
}
public static boolean exists() {
return new File(Tools.DIR_GAME_HOME,"per-version-config.json").exists();
}
public static class VersionConfig {
public String jvmArgs;
public String gamePath;

View File

@ -300,4 +300,5 @@
<string name="gles_version_hack_description">Help with compatibility on some old versions</string>
<string name="arc_capes_title">Arc Capes</string>
<string name="arc_capes_desc">Enables capes from Arc. For more information please visit https://arccapes.com. Requires OptiFine.</string>
<string name="migrated_profile_str">%s version configuration</string>
</resources>