Skip to content

Commit 98ff006

Browse files
committed
rework(MinecraftDownloader): Add dialogs for more edgecases and refactor
1 parent 1173df0 commit 98ff006

1 file changed

Lines changed: 35 additions & 33 deletions

File tree

app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/MinecraftDownloader.java

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package net.kdt.pojavlaunch.tasks;
22

33
import static net.kdt.pojavlaunch.PojavApplication.sExecutorService;
4+
import static net.kdt.pojavlaunch.Tools.dialogOnUiThread;
45

56
import android.app.Activity;
6-
import android.content.Context;
7-
import android.net.ConnectivityManager;
8-
import android.net.NetworkInfo;
97
import android.util.Log;
108

119
import androidx.annotation.NonNull;
@@ -67,7 +65,7 @@ public class MinecraftDownloader {
6765
/**
6866
* Start the game version download process on the global executor service.
6967
* @param activity Activity, used for automatic installation of JRE 17 if needed
70-
* @param version The JMinecraftVersionList.Version from the version list, if available
68+
* @param version The JMinecraftVersionList.Version from the version list, if available from Mojang releases
7169
* @param realVersion The version ID (necessary)
7270
* @param listener The download status listener
7371
*/
@@ -85,33 +83,37 @@ public void start(@Nullable Activity activity, @Nullable JMinecraftVersionList.V
8583
}
8684

8785
sExecutorService.execute(() -> {
88-
try {
89-
if(isLocalProfile || !isOnline) {
90-
String versionMessage = realVersion; // Use provided version unless we find its a modded instance
91-
92-
// See if provided version is a modded version and if that version depends on another jar, check for presence of both jar's .json.
93-
try {
94-
// This reads the .json associated with the provided version. If it fails, we can assume it's not installed.
95-
File providedJsonFile = new File(Tools.DIR_HOME_VERSION + "/" + realVersion + "/" + realVersion + ".json");
96-
JMinecraftVersionList.Version providedJson = Tools.GLOBAL_GSON.fromJson(Tools.read(providedJsonFile.getAbsolutePath()), JMinecraftVersionList.Version.class);
97-
98-
// This checks if running modded version that depends on other jars, so we use that for the error message.
99-
File vanillaJsonFile = new File(Tools.DIR_HOME_VERSION + "/" + providedJson.inheritsFrom + "/" + providedJson.inheritsFrom + ".json");
100-
versionMessage = providedJson.inheritsFrom != null ? providedJson.inheritsFrom : versionMessage;
101-
102-
// Ensure they're both not some 0 byte corrupted json
103-
if (providedJsonFile.length() == 0 || vanillaJsonFile.exists() && vanillaJsonFile.length() == 0){
104-
throw new RuntimeException("Minecraft "+versionMessage+ " is needed by " +realVersion); }
105-
106-
listener.onDownloadDone();
107-
} catch (Exception e) {
86+
try { // Check if the version being ran is installed properly
87+
String vanillaVersion = realVersion;
88+
try {
89+
JMinecraftVersionList.Version providedJsonVersion = Tools.GLOBAL_GSON.fromJson(Tools.read(
90+
new File(Tools.DIR_HOME_VERSION + "/" + realVersion + "/" + realVersion + ".json")
91+
.getAbsolutePath()), JMinecraftVersionList.Version.class);
92+
File vanillaJsonFile = new File(Tools.DIR_HOME_VERSION + "/"
93+
+ providedJsonVersion.inheritsFrom + "/" + providedJsonVersion.inheritsFrom + ".json");
94+
// Check if modded, if yes, get the real vanilla version, else, assume it's vanilla
95+
vanillaVersion = providedJsonVersion.inheritsFrom != null ? providedJsonVersion.inheritsFrom : vanillaVersion;
96+
// Ensure its not some 0 byte corrupted json
97+
if (vanillaJsonFile.exists() && vanillaJsonFile.length() == 0) {
98+
throw new RuntimeException("Minecraft " + vanillaVersion + " is needed by " + realVersion);
99+
}
100+
} catch (NullPointerException e) { // Happens if providedJsonVersion = null, which only happens if modded instance json is corrupt
101+
if (version == null && activity != null) {
102+
dialogOnUiThread(activity, activity.getString(R.string.global_error), realVersion + " did not install properly, please reinstall");
103+
return;
104+
}
105+
} catch (FileNotFoundException e) { // Happens if no json for version is found
106+
if (isLocalProfile || !isOnline) { // Error out if local account, otherwise we download (if there's wifi ofc)
108107
String tryagain = !isOnline ? "Please ensure you have an internet connection" : "Please try again on your Microsoft Account";
109-
Tools.showErrorRemote(versionMessage + " is not currently installed. "+ tryagain, e);
108+
Tools.showErrorRemote(vanillaVersion + " is not currently installed. " + tryagain, e);
109+
return;
110110
}
111-
}else {
112-
downloadGame(activity, version, realVersion);
113-
listener.onDownloadDone();
114111
}
112+
// Only download if there's internet and not local account
113+
if(!isLocalProfile && isOnline) {
114+
downloadGame(activity, version, realVersion);
115+
}
116+
listener.onDownloadDone();
115117
}catch (Exception e) {
116118
listener.onDownloadFailed(e);
117119
}
@@ -266,7 +268,7 @@ private JAssets downloadAssetsIndex(JMinecraftVersionList.Version verInfo) throw
266268
});
267269
return Tools.GLOBAL_GSON.fromJson(Tools.read(targetFile), JAssets.class);
268270
}
269-
271+
270272
private MinecraftClientInfo getClientInfo(JMinecraftVersionList.Version verInfo) {
271273
Map<String, MinecraftClientInfo> downloads = verInfo.downloads;
272274
if(downloads == null) return null;
@@ -396,7 +398,7 @@ private void scheduleLibraryDownloads(DependentLibrary[] dependentLibraries) thr
396398
);
397399
}
398400
}
399-
401+
400402
private void scheduleAssetDownloads(JAssets assets) throws IOException {
401403
Map<String, JAssetInfo> assetObjects = assets.objects;
402404
if(assetObjects == null) return;
@@ -564,12 +566,12 @@ private void runCatching() throws Exception {
564566
verifyFileSha1();
565567
}else {
566568
mTargetSha1 = null; // Nullify SHA1 as DownloadUtils.ensureSha1 only checks for null,
567-
// not for string validity
569+
// not for string validity
568570
if(mTargetPath.exists()) finishWithoutDownloading();
569571
else downloadFile();
570572
}
571573
}
572-
574+
573575
private void verifyFileSha1() throws Exception {
574576
if(mTargetPath.isFile() && mTargetPath.canRead() && Tools.compareSHA1(mTargetPath, mTargetSha1)) {
575577
finishWithoutDownloading();
@@ -579,7 +581,7 @@ private void verifyFileSha1() throws Exception {
579581
downloadFile();
580582
}
581583
}
582-
584+
583585
private void downloadFile() throws Exception {
584586
try {
585587
DownloadUtils.ensureSha1(mTargetPath, mTargetSha1, () -> {

0 commit comments

Comments
 (0)