fix[MinecraftDownloader]: Fix local profile and offline handling so they can actually launch the game

This commit is contained in:
alexytomi 2025-06-08 17:31:30 +08:00
parent c5e4d7e027
commit 951b35ca5f

View File

@ -72,7 +72,7 @@ public class MinecraftDownloader {
* @param listener The download status listener * @param listener The download status listener
*/ */
public void start(@Nullable Activity activity, @Nullable JMinecraftVersionList.Version version, public void start(@Nullable Activity activity, @Nullable JMinecraftVersionList.Version version,
@NonNull String realVersion, // this was there for a reason @NonNull String realVersion,
@NonNull AsyncMinecraftDownloader.DoneListener listener) { @NonNull AsyncMinecraftDownloader.DoneListener listener) {
if(activity != null){ if(activity != null){
isLocalProfile = Tools.isLocalProfile(activity); isLocalProfile = Tools.isLocalProfile(activity);
@ -86,11 +86,32 @@ public class MinecraftDownloader {
sExecutorService.execute(() -> { sExecutorService.execute(() -> {
try { try {
if(isLocalProfile){ if(isLocalProfile || !isOnline) {
throw new RuntimeException("Download failed. Please make sure you are logged in with a Microsoft Account."); String versionMessage = realVersion; // Use provided version unless we find its a modded instance
// See if provided version is a modded version and if that version depends on another jar, check for presence of both jar's .json.
try {
// This reads the .json associated with the provided version. If it fails, we can assume it's not installed.
File providedJsonFile = new File(Tools.DIR_HOME_VERSION + "/" + realVersion + "/" + realVersion + ".json");
JMinecraftVersionList.Version providedJson = Tools.GLOBAL_GSON.fromJson(Tools.read(providedJsonFile.getAbsolutePath()), JMinecraftVersionList.Version.class);
// This checks if running modded version that depends on other jars, so we use that for the error message.
File vanillaJsonFile = new File(Tools.DIR_HOME_VERSION + "/" + providedJson.inheritsFrom + "/" + providedJson.inheritsFrom + ".json");
versionMessage = providedJson.inheritsFrom != null ? providedJson.inheritsFrom : versionMessage;
// Ensure they're both not some 0 byte corrupted json
if (providedJsonFile.length() == 0 || vanillaJsonFile.exists() && vanillaJsonFile.length() == 0){
throw new RuntimeException("Minecraft "+versionMessage+ " is needed by " +realVersion); }
listener.onDownloadDone();
} catch (Exception e) {
String tryagain = !isOnline ? "Please ensure you have an internet connection" : "Please try again on your Microsoft Account";
Tools.showErrorRemote(versionMessage + " is not currently installed. "+ tryagain, e);
} }
}else {
downloadGame(activity, version, realVersion); downloadGame(activity, version, realVersion);
listener.onDownloadDone(); listener.onDownloadDone();
}
}catch (Exception e) { }catch (Exception e) {
listener.onDownloadFailed(e); listener.onDownloadFailed(e);
} }
@ -484,18 +505,18 @@ public class MinecraftDownloader {
File cacheFile = new File(sha1CacheDir.getAbsolutePath() + FileUtils.getFileName(mTargetUrl) + ".sha"); File cacheFile = new File(sha1CacheDir.getAbsolutePath() + FileUtils.getFileName(mTargetUrl) + ".sha");
// Only use cache when its offline. No point in having cache invalidation now! // Only use cache when its offline. No point in having cache invalidation now!
if (!isOnline) { if (!isOnline || !LauncherPreferences.PREF_CHECK_LIBRARY_SHA) { // Well not only offlines..this setting speeds up launch times at least!
try (BufferedReader cacheFileReader = new BufferedReader(new FileReader(cacheFile))) { try (BufferedReader cacheFileReader = new BufferedReader(new FileReader(cacheFile))) {
mTargetSha1 = cacheFileReader.readLine(); mTargetSha1 = cacheFileReader.readLine();
if (mTargetSha1 != null) { if (mTargetSha1 != null) {
Log.i("MinecraftDownloader", "(No internet found) Reading Hash: " + mTargetSha1 + " from " + cacheFile); Log.i("MinecraftDownloader", "Reading Hash from cache: " + mTargetSha1 + " from " + cacheFile);
} else if (cacheFile.exists()) { } else if (cacheFile.exists()) {
Log.i("MinecraftDownloader", "(No internet found) Deleting invalid hash from cache: " + cacheFile); Log.i("MinecraftDownloader", "Deleting invalid hash from cache: " + cacheFile);
cacheFile.delete(); cacheFile.delete();
} }
} catch (FileNotFoundException ignored) { } catch (FileNotFoundException ignored) {
mTargetSha1 = null; mTargetSha1 = null;
Log.w("MinecraftDownloader", "(No internet found) Failed to read hash for " + cacheFile); Log.w("MinecraftDownloader", "Failed to read hash for " + cacheFile);
} }
return; return;
} }