Refactor: centralise download with sha1

This commit is contained in:
Mathias-Boulay 2023-11-29 23:54:41 +01:00 committed by Maksim Belov
parent 017965faa5
commit 26cad1a47b
3 changed files with 46 additions and 25 deletions

View File

@ -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<Void>) () -> {
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 {

View File

@ -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<Void>) () -> {
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));

View File

@ -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> T downloadFileEnsureSha1(File outputFile, @Nullable String sha1, Callable<T> 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> {
T process(String input) throws ParseException;
}