From 951b35ca5f785c9935002ed8ef4ba997706d46e6 Mon Sep 17 00:00:00 2001 From: alexytomi <60690056+alexytomi@users.noreply.github.com> Date: Sun, 8 Jun 2025 17:31:30 +0800 Subject: [PATCH] fix[MinecraftDownloader]: Fix local profile and offline handling so they can actually launch the game --- .../tasks/MinecraftDownloader.java | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/MinecraftDownloader.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/MinecraftDownloader.java index db34eee3e..40abf1723 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/MinecraftDownloader.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/tasks/MinecraftDownloader.java @@ -72,7 +72,7 @@ public class MinecraftDownloader { * @param listener The download status listener */ 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) { if(activity != null){ isLocalProfile = Tools.isLocalProfile(activity); @@ -86,11 +86,32 @@ public class MinecraftDownloader { sExecutorService.execute(() -> { try { - if(isLocalProfile){ - throw new RuntimeException("Download failed. Please make sure you are logged in with a Microsoft Account."); - } + if(isLocalProfile || !isOnline) { + 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); listener.onDownloadDone(); + } }catch (Exception e) { listener.onDownloadFailed(e); } @@ -484,18 +505,18 @@ public class MinecraftDownloader { File cacheFile = new File(sha1CacheDir.getAbsolutePath() + FileUtils.getFileName(mTargetUrl) + ".sha"); // 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))) { mTargetSha1 = cacheFileReader.readLine(); 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()) { - Log.i("MinecraftDownloader", "(No internet found) Deleting invalid hash from cache: " + cacheFile); + Log.i("MinecraftDownloader", "Deleting invalid hash from cache: " + cacheFile); cacheFile.delete(); } } catch (FileNotFoundException ignored) { mTargetSha1 = null; - Log.w("MinecraftDownloader", "(No internet found) Failed to read hash for " + cacheFile); + Log.w("MinecraftDownloader", "Failed to read hash for " + cacheFile); } return; }