This commit is contained in:
huanghongxun 2020-06-14 20:06:19 +08:00
parent 64d9fa7c6b
commit 0d3e204f0d
2 changed files with 37 additions and 11 deletions

View File

@ -65,15 +65,7 @@ public final class GameAssetDownloadTask extends Task<Void> {
this.assetIndexFile = dependencyManager.getGameRepository().getIndexFile(version.getId(), assetIndexInfo.getId()); this.assetIndexFile = dependencyManager.getGameRepository().getIndexFile(version.getId(), assetIndexInfo.getId());
this.integrityCheck = integrityCheck; this.integrityCheck = integrityCheck;
if (!assetIndexFile.exists() || forceDownloadingIndex) { dependents.add(new GameAssetIndexDownloadTask(dependencyManager, this.version, forceDownloadingIndex));
dependents.add(new GameAssetIndexDownloadTask(dependencyManager, this.version));
} else {
try {
JsonUtils.fromNonNullJson(FileUtils.readText(assetIndexFile), AssetIndex.class);
} catch (IOException | JsonParseException e) {
dependents.add(new GameAssetIndexDownloadTask(dependencyManager, this.version));
}
}
} }
@Override @Override

View File

@ -17,16 +17,26 @@
*/ */
package org.jackhuang.hmcl.download.game; package org.jackhuang.hmcl.download.game;
import com.google.gson.JsonParseException;
import org.jackhuang.hmcl.download.AbstractDependencyManager; import org.jackhuang.hmcl.download.AbstractDependencyManager;
import org.jackhuang.hmcl.game.AssetIndex;
import org.jackhuang.hmcl.game.AssetIndexInfo; import org.jackhuang.hmcl.game.AssetIndexInfo;
import org.jackhuang.hmcl.game.Version; import org.jackhuang.hmcl.game.Version;
import org.jackhuang.hmcl.task.FileDownloadTask; import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.DigestUtils;
import org.jackhuang.hmcl.util.Hex;
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.gson.JsonUtils;
import org.jackhuang.hmcl.util.io.FileUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import static org.jackhuang.hmcl.util.Logging.LOG;
/** /**
* This task is to download asset index file provided in minecraft.json. * This task is to download asset index file provided in minecraft.json.
@ -37,6 +47,7 @@ public final class GameAssetIndexDownloadTask extends Task<Void> {
private final AbstractDependencyManager dependencyManager; private final AbstractDependencyManager dependencyManager;
private final Version version; private final Version version;
private final boolean forceDownloading;
private final List<Task<?>> dependencies = new LinkedList<>(); private final List<Task<?>> dependencies = new LinkedList<>();
/** /**
@ -45,9 +56,10 @@ public final class GameAssetIndexDownloadTask extends Task<Void> {
* @param dependencyManager the dependency manager that can provides {@link org.jackhuang.hmcl.game.GameRepository} * @param dependencyManager the dependency manager that can provides {@link org.jackhuang.hmcl.game.GameRepository}
* @param version the <b>resolved</b> version * @param version the <b>resolved</b> version
*/ */
public GameAssetIndexDownloadTask(AbstractDependencyManager dependencyManager, Version version) { public GameAssetIndexDownloadTask(AbstractDependencyManager dependencyManager, Version version, boolean forceDownloading) {
this.dependencyManager = dependencyManager; this.dependencyManager = dependencyManager;
this.version = version; this.version = version;
this.forceDownloading = forceDownloading;
setSignificance(TaskSignificance.MODERATE); setSignificance(TaskSignificance.MODERATE);
} }
@ -60,12 +72,34 @@ public final class GameAssetIndexDownloadTask extends Task<Void> {
public void execute() { public void execute() {
AssetIndexInfo assetIndexInfo = version.getAssetIndex(); AssetIndexInfo assetIndexInfo = version.getAssetIndex();
File assetIndexFile = dependencyManager.getGameRepository().getIndexFile(version.getId(), assetIndexInfo.getId()); File assetIndexFile = dependencyManager.getGameRepository().getIndexFile(version.getId(), assetIndexInfo.getId());
boolean verifyHashCode = StringUtils.isNotBlank(assetIndexInfo.getSha1()) && assetIndexInfo.getUrl().contains(assetIndexInfo.getSha1());
if (assetIndexFile.exists() && !forceDownloading) {
// verify correctness of file content
if (verifyHashCode) {
try {
String actualSum = Hex.encodeHex(DigestUtils.digest("SHA-1", assetIndexFile.toPath()));
if (actualSum.equalsIgnoreCase(assetIndexInfo.getSha1()))
return;
} catch (IOException e) {
LOG.log(Level.WARNING, "Failed to calculate sha1sum of file " + assetIndexInfo, e);
// continue downloading
}
} else {
try {
JsonUtils.fromNonNullJson(FileUtils.readText(assetIndexFile), AssetIndex.class);
return;
} catch (IOException | JsonParseException ignore) {
}
}
}
// We should not check the hash code of asset index file since this file is not consistent // We should not check the hash code of asset index file since this file is not consistent
// And Mojang will modify this file anytime. So assetIndex.hash might be outdated. // And Mojang will modify this file anytime. So assetIndex.hash might be outdated.
FileDownloadTask task = new FileDownloadTask( FileDownloadTask task = new FileDownloadTask(
dependencyManager.getDownloadProvider().injectURLWithCandidates(assetIndexInfo.getUrl()), dependencyManager.getDownloadProvider().injectURLWithCandidates(assetIndexInfo.getUrl()),
assetIndexFile assetIndexFile,
verifyHashCode ? new FileDownloadTask.IntegrityCheck("SHA-1", assetIndexInfo.getSha1()) : null
); );
task.setCacheRepository(dependencyManager.getCacheRepository()); task.setCacheRepository(dependencyManager.getCacheRepository());
dependencies.add(task); dependencies.add(task);