mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-14 07:05:40 -04:00
Refactor: centralise download with sha1
This commit is contained in:
parent
017965faa5
commit
26cad1a47b
@ -8,6 +8,7 @@ import net.kdt.pojavlaunch.utils.DownloadUtils;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InterruptedIOException;
|
import java.io.InterruptedIOException;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
import java.util.concurrent.ThreadPoolExecutor;
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@ -117,33 +118,22 @@ public class ModDownloader {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
IOException exception = null;
|
|
||||||
for(String sourceUrl : mDownloadUrls) {
|
for(String sourceUrl : mDownloadUrls) {
|
||||||
try {
|
try {
|
||||||
int i=0;
|
DownloadUtils.downloadFileEnsureSha1(mDestination, mSha1, (Callable<Void>) () -> {
|
||||||
while (i < 5){
|
IOException exception = tryDownload(sourceUrl);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}catch (InterruptedException e) {
|
if(exception != null) {
|
||||||
|
downloadFailed(exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
}catch (IOException e) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(exception != null) {
|
|
||||||
downloadFailed(exception);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private IOException tryDownload(String sourceUrl) throws InterruptedException {
|
private IOException tryDownload(String sourceUrl) throws InterruptedException {
|
||||||
|
@ -15,6 +15,7 @@ import net.kdt.pojavlaunch.value.launcherprofiles.MinecraftProfile;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
public class ModpackInstaller {
|
public class ModpackInstaller {
|
||||||
|
|
||||||
@ -30,13 +31,12 @@ public class ModpackInstaller {
|
|||||||
ModLoader modLoaderInfo;
|
ModLoader modLoaderInfo;
|
||||||
try {
|
try {
|
||||||
byte[] downloadBuffer = new byte[8192];
|
byte[] downloadBuffer = new byte[8192];
|
||||||
int attempt = 0;
|
DownloadUtils.downloadFileEnsureSha1(modpackFile, versionHash, (Callable<Void>) () -> {
|
||||||
while (attempt < 3 && (!modpackFile.exists() || !Tools.compareSHA1(modpackFile, versionHash))){
|
|
||||||
attempt++;
|
|
||||||
DownloadUtils.downloadFileMonitored(versionUrl, modpackFile, downloadBuffer,
|
DownloadUtils.downloadFileMonitored(versionUrl, modpackFile, downloadBuffer,
|
||||||
new DownloaderProgressWrapper(R.string.modpack_download_downloading_metadata,
|
new DownloaderProgressWrapper(R.string.modpack_download_downloading_metadata,
|
||||||
ProgressLayout.INSTALL_MODPACK));
|
ProgressLayout.INSTALL_MODPACK));
|
||||||
}
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
// Install the modpack
|
// Install the modpack
|
||||||
modLoaderInfo = installFunction.installModpack(modpackFile, new File(Tools.DIR_GAME_HOME, "custom_instances/"+modpackName));
|
modLoaderInfo = installFunction.installModpack(modpackFile, new File(Tools.DIR_GAME_HOME, "custom_instances/"+modpackName));
|
||||||
|
@ -7,6 +7,8 @@ import androidx.annotation.Nullable;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.nio.charset.*;
|
import java.nio.charset.*;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
import net.kdt.pojavlaunch.*;
|
import net.kdt.pojavlaunch.*;
|
||||||
import org.apache.commons.io.*;
|
import org.apache.commons.io.*;
|
||||||
|
|
||||||
@ -183,6 +185,35 @@ public class DownloadUtils {
|
|||||||
conn.disconnect();
|
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> {
|
public interface ParseCallback<T> {
|
||||||
T process(String input) throws ParseException;
|
T process(String input) throws ParseException;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user