diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/modpacks/api/ModDownloader.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/modpacks/api/ModDownloader.java index 9d1de62ad..c2ba796dd 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/modpacks/api/ModDownloader.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/modpacks/api/ModDownloader.java @@ -8,6 +8,7 @@ import net.kdt.pojavlaunch.utils.DownloadUtils; import java.io.File; import java.io.IOException; import java.io.InterruptedIOException; +import java.util.concurrent.Callable; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -117,33 +118,22 @@ public class ModDownloader { @Override public void run() { - IOException exception = null; for(String sourceUrl : mDownloadUrls) { try { - int i=0; - while (i < 5){ - exception = tryDownload(sourceUrl); - if(exception == null) { - if(mSha1 != null && !Tools.compareSHA1(mDestination, mSha1)){ - // Remove the target file and retry - i++; - mDestination.delete(); - continue; - } - // No Sha or it matched - break; - } - // If there is an exception, try another source - break; - } + DownloadUtils.downloadFileEnsureSha1(mDestination, mSha1, (Callable) () -> { + IOException exception = tryDownload(sourceUrl); - }catch (InterruptedException e) { + if(exception != null) { + downloadFailed(exception); + } + + return null; + }); + + }catch (IOException e) { return; } } - if(exception != null) { - downloadFailed(exception); - } } private IOException tryDownload(String sourceUrl) throws InterruptedException { diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/modpacks/api/ModpackInstaller.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/modpacks/api/ModpackInstaller.java index b76ccc2bc..8deaa000c 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/modpacks/api/ModpackInstaller.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/modpacks/api/ModpackInstaller.java @@ -15,6 +15,7 @@ import net.kdt.pojavlaunch.value.launcherprofiles.MinecraftProfile; import java.io.File; import java.io.IOException; import java.util.Locale; +import java.util.concurrent.Callable; public class ModpackInstaller { @@ -30,13 +31,12 @@ public class ModpackInstaller { ModLoader modLoaderInfo; try { byte[] downloadBuffer = new byte[8192]; - int attempt = 0; - while (attempt < 3 && (!modpackFile.exists() || !Tools.compareSHA1(modpackFile, versionHash))){ - attempt++; + DownloadUtils.downloadFileEnsureSha1(modpackFile, versionHash, (Callable) () -> { DownloadUtils.downloadFileMonitored(versionUrl, modpackFile, downloadBuffer, new DownloaderProgressWrapper(R.string.modpack_download_downloading_metadata, ProgressLayout.INSTALL_MODPACK)); - } + return null; + }); // Install the modpack modLoaderInfo = installFunction.installModpack(modpackFile, new File(Tools.DIR_GAME_HOME, "custom_instances/"+modpackName)); diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/utils/DownloadUtils.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/utils/DownloadUtils.java index 73c702ace..2f70d0cae 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/utils/DownloadUtils.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/utils/DownloadUtils.java @@ -7,6 +7,8 @@ import androidx.annotation.Nullable; import java.io.*; import java.net.*; import java.nio.charset.*; +import java.util.concurrent.Callable; + import net.kdt.pojavlaunch.*; import org.apache.commons.io.*; @@ -183,6 +185,35 @@ public class DownloadUtils { conn.disconnect(); } + public static T downloadFileEnsureSha1(File outputFile, @Nullable String sha1, Callable downloadFunction) throws IOException { + // Skip if needed + if(sha1 == null){ + try { + return downloadFunction.call(); + } catch (IOException e){ + throw e; + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + + int attempts = 0; + T result = null; + while (attempts < 5 && (!outputFile.exists() || Tools.compareSHA1(outputFile, sha1))){ + attempts++; + try { + result = downloadFunction.call(); + } catch (IOException e){ + throw e; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + return result; + + } + public interface ParseCallback { T process(String input) throws ParseException; }