Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
A lot of changes
Browse files Browse the repository at this point in the history
- Force profiles, no more profiles on/off switch!
- (probably partially) Conventionalize all profile changes
- Rename most variables for the changes to look nice
- Squash a couple null checks
- Separate cache from adapter and change how it works
- Don't keep Context
  • Loading branch information
artdeell committed Mar 17, 2022
1 parent a4eabb6 commit 965cad4
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 274 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,31 +107,28 @@ public void launchGame(View v) {
} else if (canBack) {
v.setEnabled(false);
mTask = new MinecraftDownloaderTask(this);
if(LauncherPreferences.PREF_ENABLE_PROFILES) {
LauncherProfiles.update();
if (LauncherProfiles.mainProfileJson != null && LauncherProfiles.mainProfileJson.profiles != null && LauncherProfiles.mainProfileJson.profiles.containsKey(mProfile.selectedProfile + "")) {
MinecraftProfile prof = LauncherProfiles.mainProfileJson.profiles.get(mProfile.selectedProfile + "");
if (prof != null && prof.lastVersionId != null) {
if (mProfile.accessToken.equals("0")) {
File verJsonFile = new File(Tools.DIR_HOME_VERSION,
mProfile.selectedVersion + "/" + mProfile.selectedVersion + ".json");
if (verJsonFile.exists()) {
mTask.onPostExecute(null);
} else {
new AlertDialog.Builder(this)
.setTitle(R.string.global_error)
.setMessage(R.string.mcl_launch_error_localmode)
.setPositiveButton(android.R.string.ok, null)
.show();
}
String versionId = getVersionId(prof.lastVersionId);
File verJsonFile = new File(Tools.DIR_HOME_VERSION,
versionId + "/" + versionId + ".json");
if (verJsonFile.exists()) {
mTask.onPostExecute(null);
} else {
new AlertDialog.Builder(this)
.setTitle(R.string.global_error)
.setMessage(R.string.mcl_launch_error_localmode)
.setPositiveButton(android.R.string.ok, null)
.show();
}
}else {
mTask.execute(getVersionId(prof.lastVersionId));
}
mTask.execute(getVersionId(prof.lastVersionId));
}
}
}else{
mTask.execute(mProfile.selectedVersion);
}

}
}

Expand Down Expand Up @@ -196,7 +193,6 @@ public void onNothingSelected(AdapterView<?> p1)
@Override
protected void onPause() {
super.onPause();
if((!LauncherPreferences.PREF_ENABLE_PROFILES) && versionListener != null) ExtraCore.removeExtraListenerFromValue("lac_version_list",versionListener);
}

public static void updateVersionSpinner(Context ctx, ArrayList<String> value, Spinner mVersionSelector, String defaultSelection) {
Expand All @@ -213,22 +209,6 @@ public static void updateVersionSpinner(Context ctx, ArrayList<String> value, Sp
protected void onResume(){
super.onResume();
new RefreshVersionListTask(this).execute();
if(!LauncherPreferences.PREF_ENABLE_PROFILES) {

ArrayList<String> vlst = (ArrayList<String>) ExtraCore.getValue("lac_version_list");
if(vlst != null) {
setupVersionSelector();
updateVersionSpinner(this, vlst, mVersionSelector, mProfile.selectedVersion);
}
versionListener = (key, value) -> {
if(value != null) {
setupVersionSelector();
updateVersionSpinner(this, value, mVersionSelector, mProfile.selectedVersion);
}
return false;
};
ExtraCore.addExtraListener("lac_version_list",versionListener);
}
if(listRefreshListener != null) {
LauncherPreferences.DEFAULT_PREF.unregisterOnSharedPreferenceChangeListener(listRefreshListener);
}
Expand All @@ -243,17 +223,6 @@ protected void onResume(){
}
};
LauncherPreferences.DEFAULT_PREF.registerOnSharedPreferenceChangeListener(listRefreshListener);
if(profileEnableListener != null) {
LauncherPreferences.DEFAULT_PREF.unregisterOnSharedPreferenceChangeListener(profileEnableListener);
}
profileEnableListener = ((sharedPreferences, key) -> {
if(key.equals("enable_profiles")) {
LauncherPreferences.PREF_ENABLE_PROFILES = sharedPreferences.getBoolean("enable_profiles",false);
this.recreate();
profileEnableListener = null;
}
});
LauncherPreferences.DEFAULT_PREF.registerOnSharedPreferenceChangeListener(profileEnableListener);
System.out.println("call to onResume");
final int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
final View decorView = getWindow().getDecorView();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import net.kdt.pojavlaunch.multirt.MultiRTUtils;

import net.kdt.pojavlaunch.prefs.*;
import net.kdt.pojavlaunch.profiles.ProfileAdapter;
import net.kdt.pojavlaunch.utils.*;
import net.kdt.pojavlaunch.value.*;
import net.kdt.pojavlaunch.value.launcherprofiles.LauncherProfiles;
Expand All @@ -50,6 +49,7 @@ public class BaseMainActivity extends BaseActivity {
private LoggerView loggerView;

MinecraftAccount mProfile;
MinecraftProfile minecraftProfile;

private DrawerLayout drawerLayout;
private NavigationView navDrawer;
Expand All @@ -71,44 +71,29 @@ protected void initLayout(int resId) {
loggerView = findViewById(R.id.mainLoggerView);

mProfile = PojavProfile.getCurrentProfileContent(this);
minecraftProfile = LauncherProfiles.mainProfileJson.profiles.get(mProfile.selectedProfile);
if(minecraftProfile == null) {
Toast.makeText(this,"Attempted to launch nonexistent profile",Toast.LENGTH_SHORT).show();
finish();
return;
}
String runtime = LauncherPreferences.PREF_DEFAULT_RUNTIME;
if(!LauncherPreferences.PREF_ENABLE_PROFILES) {
mVersionInfo = Tools.getVersionInfo(null, mProfile.selectedVersion);
PerVersionConfig.update();
PerVersionConfig.VersionConfig config = PerVersionConfig.configMap.get(mProfile.selectedVersion);
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;
}
}
}else{
LauncherProfiles.update();
MinecraftProfile prof = LauncherProfiles.mainProfileJson.profiles.get(mProfile.selectedProfile);
if(prof == null) {
Toast.makeText(this,"Attempted to launch nonexistent profile",Toast.LENGTH_SHORT).show();
finish();
return;
}

mVersionInfo = Tools.getVersionInfo(null, BaseLauncherActivity.getVersionId(
prof.lastVersionId));
if(prof.javaDir != null && prof.javaDir.startsWith(Tools.LAUNCHERPROFILES_RTPREFIX)) {
String runtimeName = prof.javaDir.substring(Tools.LAUNCHERPROFILES_RTPREFIX.length());
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) {
runtime = runtimeName;
}
}
if(prof.__P_renderer_name != null) {
Log.i("RdrDebug","__P_renderer="+prof.__P_renderer_name);
Tools.LOCAL_RENDERER = prof.__P_renderer_name;
if(minecraftProfile.pojavRendererName != null) {
Log.i("RdrDebug","__P_renderer="+minecraftProfile.pojavRendererName);
Tools.LOCAL_RENDERER = minecraftProfile.pojavRendererName;
}
}

setTitle("Minecraft " + mProfile.selectedVersion);
setTitle("Minecraft " + minecraftProfile.lastVersionId);

MultiRTUtils.setRuntimeNamed(this,runtime);
// Minecraft 1.13+
Expand Down Expand Up @@ -231,14 +216,10 @@ private void runCraft() throws Throwable {
"" : " (" + mVersionInfo.inheritsFrom + ")"));


JREUtils.redirectAndPrintJRELog(this);
if(!LauncherPreferences.PREF_ENABLE_PROFILES){
Tools.launchMinecraft(this, mProfile, mProfile.selectedVersion);
}else{
JREUtils.redirectAndPrintJRELog();
LauncherProfiles.update();
Tools.launchMinecraft(this, mProfile, BaseLauncherActivity.getVersionId(
LauncherProfiles.mainProfileJson.profiles.get(mProfile.selectedProfile).lastVersionId));
}
minecraftProfile.lastVersionId));
}

private void checkJavaArgsIsLaunchable(String jreVersion) throws Throwable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import net.kdt.pojavlaunch.prefs.screens.LauncherPreferenceFragment;
import net.kdt.pojavlaunch.profiles.ProfileAdapter;
import net.kdt.pojavlaunch.profiles.ProfileEditor;
import net.kdt.pojavlaunch.profiles.ProfileIconCache;
import net.kdt.pojavlaunch.value.MinecraftAccount;
import net.kdt.pojavlaunch.value.launcherprofiles.LauncherProfiles;

Expand Down Expand Up @@ -91,7 +92,7 @@ public PojavLauncherActivity() {
protected void onDestroy() {
ExtraCore.removeExtraListenerFromValue("back_preference", backPreferenceListener);
super.onDestroy();
ProfileAdapter.clearIconCache();
ProfileIconCache.clearIconCache();
Log.i("LauncherActivity","Destroyed!");
}

Expand Down Expand Up @@ -191,32 +192,27 @@ public void onNothingSelected(AdapterView<?> p1) {
setupBasicList(this);

//mAvailableVersions;
if(!LauncherPreferences.PREF_ENABLE_PROFILES) {
ArrayAdapter<String> adapterVer = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, basicVersionList);
adapterVer.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
mVersionSelector.setAdapter(adapterVer);
}else{
ProfileAdapter pad = new ProfileAdapter(this);
ProfileEditor dialog = new ProfileEditor(this,(name, isNew, deleting)->{
ProfileAdapter profileAdapter = new ProfileAdapter(this);
ProfileEditor editor = new ProfileEditor(this,(name, isNew, deleting)->{
LauncherProfiles.update();
if(isNew) {
mVersionSelector.setSelection(pad.resolveProfileIndex(name));
mVersionSelector.setSelection(profileAdapter.resolveProfileIndex(name));
}
if(deleting) {
mVersionSelector.setSelection(0);
}
pad.notifyDataSetChanged();
profileAdapter.notifyDataSetChanged();
});
mVersionSelector.setOnLongClickListener((v)->dialog.show(mProfile.selectedProfile));
mVersionSelector.setAdapter(pad);
mVersionSelector.setSelection(pad.resolveProfileIndex(mProfile.selectedProfile));
mVersionSelector.setOnLongClickListener((v)->editor.show(mProfile.selectedProfile));
mVersionSelector.setAdapter(profileAdapter);
mVersionSelector.setSelection(profileAdapter.resolveProfileIndex(mProfile.selectedProfile));
mVersionSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> p1, View p2, int p3, long p4)
{
String profileName = p1.getItemAtPosition(p3).toString();
if(profileName.equals(ProfileAdapter.CREATE_PROFILE_MAGIC)) {
dialog.show(profileName);
editor.show(profileName);
mVersionSelector.setSelection(0);
}else {
mProfile.selectedProfile = p1.getItemAtPosition(p3).toString();
Expand All @@ -236,7 +232,6 @@ public void onNothingSelected(AdapterView<?> p1)
// TODO: Implement this method
}
});
}
//
statusIsLaunching(false);

Expand Down
21 changes: 6 additions & 15 deletions app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,15 @@ public static void launchMinecraft(final Activity activity, MinecraftAccount pro

JMinecraftVersionList.Version versionInfo = Tools.getVersionInfo(null,versionName);
String gamedirPath = Tools.DIR_GAME_NEW;
if(!LauncherPreferences.PREF_ENABLE_PROFILES) {
PerVersionConfig.update();
PerVersionConfig.VersionConfig pvcConfig = PerVersionConfig.configMap.get(versionName);
if (pvcConfig != null && pvcConfig.gamePath != null && !pvcConfig.gamePath.isEmpty())
gamedirPath = pvcConfig.gamePath;
if (pvcConfig != null && pvcConfig.jvmArgs != null && !pvcConfig.jvmArgs.isEmpty())
LauncherPreferences.PREF_CUSTOM_JAVA_ARGS = pvcConfig.jvmArgs;
}else{
if(activity instanceof BaseMainActivity) {
LauncherProfiles.update();
MinecraftProfile prof = LauncherProfiles.mainProfileJson.profiles.get(((BaseMainActivity)activity).mProfile.selectedProfile);
if(prof == null) throw new Exception("Launching empty Profile");
if(prof.gameDir != null && !prof.gameDir.isEmpty())
gamedirPath = prof.gameDir;
if(prof.javaArgs != null && !prof.javaArgs.isEmpty())
LauncherPreferences.PREF_CUSTOM_JAVA_ARGS = prof.javaArgs;
MinecraftProfile minecraftProfile = ((BaseMainActivity)activity).minecraftProfile;
if(minecraftProfile == null) throw new Exception("Launching empty Profile");
if(minecraftProfile.gameDir != null && !minecraftProfile.gameDir.isEmpty())
gamedirPath = minecraftProfile.gameDir;
if(minecraftProfile.javaArgs != null && !minecraftProfile.javaArgs.isEmpty())
LauncherPreferences.PREF_CUSTOM_JAVA_ARGS = minecraftProfile.javaArgs;
}
}
PojavLoginActivity.disableSplash(gamedirPath);
String[] launchArgs = getMinecraftArgs(profile, versionInfo, gamedirPath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class LauncherPreferences
public static int PREF_CONTROL_BOTTOM_OFFSET = 0;
public static int PREF_CONTROL_LEFT_OFFSET = 0;
public static boolean PREF_SUSTAINED_PERFORMANCE = false;
public static boolean PREF_ENABLE_PROFILES = true;
public static String PREF_GLES_SHRINK_HACK = "0";
public static boolean PREF_VBO_DISABLE_HACK = false;
public static boolean PREF_VIRTUAL_MOUSE_START = false;
Expand Down Expand Up @@ -71,7 +70,6 @@ public static void loadPreferences(Context ctx) {
PREF_SUSTAINED_PERFORMANCE = DEFAULT_PREF.getBoolean("sustainedPerformance", false);
PREF_GLES_SHRINK_HACK = DEFAULT_PREF.getString("gl4es_shrink_hack", "0");
PREF_VBO_DISABLE_HACK = DEFAULT_PREF.getBoolean("vbo_disable_hack", false);
PREF_ENABLE_PROFILES = DEFAULT_PREF.getBoolean("enable_profiles", false);
PREF_VIRTUAL_MOUSE_START = DEFAULT_PREF.getBoolean("mouse_start", false);
PREF_OPENGL_VERSION_HACK = DEFAULT_PREF.getBoolean("gles_version_hack", false);
PREF_ARC_CAPES = DEFAULT_PREF.getBoolean("arc_capes",false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@
*/
public class ProfileAdapter extends BaseAdapter {
Map<String, MinecraftProfile> profiles;
ArrayList<DataSetObserver> observers = new ArrayList<>();
static final Map<String, Bitmap> iconCache = new HashMap<>();
static final String BASE64_PNG_HEADER = "data:image/png;base64,";
public static final String CREATE_PROFILE_MAGIC = "___extra____profile-create";
static final MinecraftProfile DUMMY = new MinecraftProfile();
static MinecraftProfile CREATE_PROFILE;
List<String> profileList;
public ProfileAdapter(Context ctx) {
public ProfileAdapter(Context context) {
ProfileIconCache.initDefault(context);
LauncherProfiles.update();
profiles = new HashMap<>(LauncherProfiles.mainProfileJson.profiles);
if(CREATE_PROFILE == null) {
Expand All @@ -45,8 +43,7 @@ public ProfileAdapter(Context ctx) {
profileList = new ArrayList<>(Arrays.asList(profiles.keySet().toArray(new String[0])));
profileList.add(ProfileAdapter.CREATE_PROFILE_MAGIC);
profiles.put(CREATE_PROFILE_MAGIC, CREATE_PROFILE);
if(!iconCache.containsKey(null))
iconCache.put(null,BitmapFactory.decodeResource(ctx.getResources(),R.drawable.ic_menu_java));


}
/*
Expand All @@ -57,15 +54,6 @@ public ProfileAdapter(Context ctx) {
public int getCount() {
return profileList.size();
}
public static void clearIconCache() {
for(String s : iconCache.keySet()) {
Bitmap bmp = iconCache.get(s);
if(bmp != null) {
bmp.recycle();
}
}
iconCache.clear();
}
/*
* Gets the profile at a given index
* @param position index to retreive
Expand Down Expand Up @@ -115,35 +103,27 @@ public View getView(int position, View convertView, ViewGroup parent) {
return v;
}
public void setViewProfile(View v, String nm) {
MinecraftProfile prof = profiles.get(nm);
if(prof == null) prof = DUMMY;
Bitmap cachedIcon = iconCache.get(nm);
MinecraftProfile minecraftProfile = profiles.get(nm);
if(minecraftProfile == null) minecraftProfile = DUMMY;
Bitmap cachedIcon = ProfileIconCache.getCachedIcon(nm);
ImageView iconView = v.findViewById(R.id.vprof_icon_view);
if(cachedIcon == null && prof.icon != null) {
if (prof.icon.startsWith(BASE64_PNG_HEADER)) {
byte[] pngBytes = Base64.decode(prof.icon.substring(BASE64_PNG_HEADER.length()), Base64.DEFAULT);
cachedIcon = BitmapFactory.decodeByteArray(pngBytes,0,pngBytes.length);
iconCache.put(nm,cachedIcon);
}else{
Log.i("IconParser","Unsupported icon: "+prof.icon);
cachedIcon = iconCache.get(null);
}
if(cachedIcon == null) {
cachedIcon = ProfileIconCache.tryResolveIcon(nm,minecraftProfile.icon);
}

iconView.setImageBitmap(cachedIcon);
if(prof.name != null && !prof.name.isEmpty())
((TextView)v.findViewById(R.id.vprof_profile_name_view)).setText(prof.name);
if(minecraftProfile.name != null && !minecraftProfile.name.isEmpty())
((TextView)v.findViewById(R.id.vprof_profile_name_view)).setText(minecraftProfile.name);
else
((TextView)v.findViewById(R.id.vprof_profile_name_view)).setText(R.string.unnamed);

TextView tv = v.findViewById(R.id.vprof_version_id_view);
if(prof.lastVersionId != null) switch (prof.lastVersionId) {
if(minecraftProfile.lastVersionId != null) switch (minecraftProfile.lastVersionId) {
case "latest-release":
tv.setText(R.string.profiles_latest_release);
case "latest-snapshot":
tv.setText(R.string.profiles_latest_snapshot);
default:
tv.setText(prof.lastVersionId);
tv.setText(minecraftProfile.lastVersionId);
} else tv.setText(android.R.string.unknownName);

}
Expand Down
Loading

0 comments on commit 965cad4

Please sign in to comment.