Refactor FileDownloadTask

This commit is contained in:
huangyuhui
2018-09-07 00:35:28 +08:00
parent 1019c891e2
commit 60143d5299
15 changed files with 217 additions and 590 deletions

View File

@@ -80,7 +80,7 @@ public class DefaultGameBuilder extends GameBuilder {
}
protected Task downloadGameAsync(String gameVersion, Version version) {
return new GameDownloadTask(dependencyManager, version);
return new GameDownloadTask(dependencyManager, gameVersion, version);
}
}

View File

@@ -18,18 +18,21 @@
package org.jackhuang.hmcl.download.game;
import org.jackhuang.hmcl.download.AbstractDependencyManager;
import org.jackhuang.hmcl.game.AssetIndex;
import org.jackhuang.hmcl.game.AssetIndexInfo;
import org.jackhuang.hmcl.game.AssetObject;
import org.jackhuang.hmcl.game.Version;
import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.FileDownloadTask.IntegrityCheck;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.FileUtils;
import org.jackhuang.hmcl.util.Logging;
import org.jackhuang.hmcl.util.LocalRepository;
import org.jackhuang.hmcl.util.NetworkUtils;
import java.io.File;
import java.util.*;
import java.util.logging.Level;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
/**
*
@@ -39,7 +42,9 @@ public final class GameAssetDownloadTask extends Task {
private final AbstractDependencyManager dependencyManager;
private final Version version;
private final GameAssetRefreshTask refreshTask;
private final AssetIndexInfo assetIndexInfo;
private final File assetIndexFile;
private final List<Task> dependents = new LinkedList<>();
private final List<Task> dependencies = new LinkedList<>();
/**
@@ -51,12 +56,16 @@ public final class GameAssetDownloadTask extends Task {
public GameAssetDownloadTask(AbstractDependencyManager dependencyManager, Version version) {
this.dependencyManager = dependencyManager;
this.version = version;
this.refreshTask = new GameAssetRefreshTask(dependencyManager, version);
this.assetIndexInfo = version.getAssetIndex();
this.assetIndexFile = dependencyManager.getGameRepository().getIndexFile(version.getId(), assetIndexInfo.getId());
if (!assetIndexFile.exists())
dependents.add(new GameAssetIndexDownloadTask(dependencyManager, version));
}
@Override
public Collection<Task> getDependents() {
return Collections.singleton(refreshTask);
return dependents;
}
@Override
@@ -66,23 +75,28 @@ public final class GameAssetDownloadTask extends Task {
@Override
public void execute() throws Exception {
for (Map.Entry<File, AssetObject> entry : refreshTask.getResult()) {
if (Thread.interrupted())
throw new InterruptedException();
AssetIndex index = Constants.GSON.fromJson(FileUtils.readText(assetIndexFile), AssetIndex.class);
int progress = 0;
if (index != null)
for (AssetObject assetObject : index.getObjects().values()) {
if (Thread.interrupted())
throw new InterruptedException();
File file = entry.getKey();
AssetObject assetObject = entry.getValue();
String url = dependencyManager.getDownloadProvider().getAssetBaseURL() + assetObject.getLocation();
if (!FileUtils.makeDirectory(file.getAbsoluteFile().getParentFile())) {
Logging.LOG.log(Level.SEVERE, "Unable to create new file " + file + ", because parent directory cannot be created");
continue;
File file = dependencyManager.getGameRepository().getAssetObject(version.getId(), assetIndexInfo.getId(), assetObject);
if (file.isFile())
LocalRepository.getInstance().tryCacheFile(file.toPath(), LocalRepository.SHA1, assetObject.getHash());
else {
String url = dependencyManager.getDownloadProvider().getAssetBaseURL() + assetObject.getLocation();
FileDownloadTask task = new FileDownloadTask(NetworkUtils.toURL(url), file, new FileDownloadTask.IntegrityCheck("SHA-1", assetObject.getHash()));
task.setName(assetObject.getHash());
dependencies.add(task
.setCaching(true)
.setCandidate(LocalRepository.getInstance().getCommonDirectory()
.resolve("assets").resolve("objects").resolve(assetObject.getLocation())));
}
updateProgress(++progress, index.getObjects().size());
}
if (file.isDirectory() || file.isFile() && file.exists())
continue;
FileDownloadTask task = new FileDownloadTask(NetworkUtils.toURL(url), file, new IntegrityCheck("SHA-1", assetObject.getHash()));
task.setName(assetObject.getHash());
dependencies.add(task);
}
}
}

View File

@@ -22,11 +22,10 @@ import org.jackhuang.hmcl.game.AssetIndexInfo;
import org.jackhuang.hmcl.game.Version;
import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.FileUtils;
import org.jackhuang.hmcl.util.LocalRepository;
import org.jackhuang.hmcl.util.NetworkUtils;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
@@ -61,9 +60,6 @@ public final class GameAssetIndexDownloadTask extends Task {
@Override
public void execute() throws Exception {
AssetIndexInfo assetIndexInfo = version.getAssetIndex();
File assetDir = dependencyManager.getGameRepository().getAssetDirectory(version.getId(), assetIndexInfo.getId());
if (!FileUtils.makeDirectory(assetDir))
throw new IOException("Cannot create directory: " + assetDir);
File assetIndexFile = dependencyManager.getGameRepository().getIndexFile(version.getId(), assetIndexInfo.getId());
// We should not check the hash code of asset index file since this file is not consistent
@@ -71,7 +67,9 @@ public final class GameAssetIndexDownloadTask extends Task {
dependencies.add(new FileDownloadTask(
NetworkUtils.toURL(dependencyManager.getDownloadProvider().injectURL(assetIndexInfo.getUrl())),
assetIndexFile
));
).setCaching(true)
.setCandidate(LocalRepository.getInstance().getCommonDirectory()
.resolve("assets").resolve("indexes").resolve(assetIndexInfo.getId() + ".json")));
}
}

View File

@@ -1,94 +0,0 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2018 huangyuhui <huanghongxun2008@126.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see {http://www.gnu.org/licenses/}.
*/
package org.jackhuang.hmcl.download.game;
import org.jackhuang.hmcl.download.AbstractDependencyManager;
import org.jackhuang.hmcl.game.AssetIndex;
import org.jackhuang.hmcl.game.AssetIndexInfo;
import org.jackhuang.hmcl.game.AssetObject;
import org.jackhuang.hmcl.game.Version;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.task.TaskResult;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.FileUtils;
import org.jackhuang.hmcl.util.Pair;
import java.io.File;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import static org.jackhuang.hmcl.util.Pair.pair;
/**
* This task is to extract all asset objects described in asset index json.
*
* @author huangyuhui
*/
public final class GameAssetRefreshTask extends TaskResult<Collection<Pair<File, AssetObject>>> {
private final AbstractDependencyManager dependencyManager;
private final Version version;
private final AssetIndexInfo assetIndexInfo;
private final File assetIndexFile;
private final List<Task> dependents = new LinkedList<>();
/**
* Constructor.
*
* @param dependencyManager the dependency manager that can provides {@link org.jackhuang.hmcl.game.GameRepository}
* @param version the <b>resolved</b> version
*/
public GameAssetRefreshTask(AbstractDependencyManager dependencyManager, Version version) {
this.dependencyManager = dependencyManager;
this.version = version;
this.assetIndexInfo = version.getAssetIndex();
this.assetIndexFile = dependencyManager.getGameRepository().getIndexFile(version.getId(), assetIndexInfo.getId());
if (!assetIndexFile.exists())
dependents.add(new GameAssetIndexDownloadTask(dependencyManager, version));
}
@Override
public List<Task> getDependents() {
return dependents;
}
@Override
public String getId() {
return ID;
}
@Override
public void execute() throws Exception {
AssetIndex index = Constants.GSON.fromJson(FileUtils.readText(assetIndexFile), AssetIndex.class);
List<Pair<File, AssetObject>> res = new LinkedList<>();
int progress = 0;
if (index != null)
for (AssetObject assetObject : index.getObjects().values()) {
if (Thread.interrupted())
throw new InterruptedException();
res.add(pair(dependencyManager.getGameRepository().getAssetObject(version.getId(), assetIndexInfo.getId(), assetObject), assetObject));
updateProgress(++progress, index.getObjects().size());
}
setResult(res);
}
public static final String ID = "game_asset_refresh_task";
}

View File

@@ -22,6 +22,7 @@ import org.jackhuang.hmcl.game.Version;
import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.FileDownloadTask.IntegrityCheck;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.LocalRepository;
import org.jackhuang.hmcl.util.NetworkUtils;
import java.io.File;
@@ -34,11 +35,13 @@ import java.util.List;
*/
public final class GameDownloadTask extends Task {
private final DefaultDependencyManager dependencyManager;
private final String gameVersion;
private final Version version;
private final List<Task> dependencies = new LinkedList<>();
public GameDownloadTask(DefaultDependencyManager dependencyManager, Version version) {
public GameDownloadTask(DefaultDependencyManager dependencyManager, String gameVersion, Version version) {
this.dependencyManager = dependencyManager;
this.gameVersion = gameVersion;
this.version = version;
setSignificance(TaskSignificance.MODERATE);
@@ -56,8 +59,9 @@ public final class GameDownloadTask extends Task {
dependencies.add(new FileDownloadTask(
NetworkUtils.toURL(dependencyManager.getDownloadProvider().injectURL(version.getDownloadInfo().getUrl())),
jar,
new IntegrityCheck("SHA-1", version.getDownloadInfo().getSha1())
));
IntegrityCheck.of(LocalRepository.SHA1, version.getDownloadInfo().getSha1()))
.setCaching(true)
.setCandidate(LocalRepository.getInstance().getCommonDirectory().resolve("jars").resolve(gameVersion + ".jar")));
}
}

View File

@@ -28,7 +28,9 @@ import java.io.RandomAccessFile;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.util.Optional;
import java.util.logging.Level;
import static java.util.Objects.requireNonNull;
@@ -50,6 +52,11 @@ public class FileDownloadTask extends Task {
this.checksum = requireNonNull(checksum);
}
public static IntegrityCheck of(String algorithm, String checksum) {
if (checksum == null) return null;
else return new IntegrityCheck(algorithm, checksum);
}
public String getAlgorithm() {
return algorithm;
}
@@ -75,6 +82,8 @@ public class FileDownloadTask extends Task {
private final IntegrityCheck integrityCheck;
private final int retry;
private final EventManager<FailedEvent<URL>> onFailed = new EventManager<>();
private Path candidate;
private boolean caching;
private RandomAccessFile rFile;
private InputStream stream;
@@ -146,9 +155,35 @@ public class FileDownloadTask extends Task {
return file;
}
public FileDownloadTask setCandidate(Path candidate) {
this.candidate = candidate;
return this;
}
public FileDownloadTask setCaching(boolean caching) {
this.caching = caching;
return this;
}
@Override
public void execute() throws Exception {
URL currentURL = url;
// Check cache
if (integrityCheck != null && caching) {
Optional<Path> cache = LocalRepository.getInstance()
.checkExistentFile(candidate, integrityCheck.getAlgorithm(), integrityCheck.getChecksum());
if (cache.isPresent()) {
try {
FileUtils.copyFile(cache.get().toFile(), file);
Logging.LOG.log(Level.FINER, "Successfully verified file " + file + " from " + currentURL);
return;
} catch (IOException e) {
Logging.LOG.log(Level.WARNING, "Failed to copy cache files", e);
}
}
}
Logging.LOG.log(Level.FINER, "Downloading " + currentURL + " to " + file);
Exception exception = null;
@@ -257,6 +292,17 @@ public class FileDownloadTask extends Task {
if (exception != null)
throw new IOException("Unable to download file " + currentURL + ". " + exception.getMessage(), exception);
if (caching) {
try {
if (integrityCheck == null)
LocalRepository.getInstance().cacheFile(file.toPath(), LocalRepository.SHA1, Hex.encodeHex(DigestUtils.digest(LocalRepository.SHA1, file.toPath())));
else
LocalRepository.getInstance().cacheFile(file.toPath(), integrityCheck.getAlgorithm(), integrityCheck.getChecksum());
} catch (IOException e) {
Logging.LOG.log(Level.WARNING, "Failed to cache file", e);
}
}
}
}

View File

@@ -0,0 +1,111 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2017 huangyuhui <huanghongxun2008@126.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see {http://www.gnu.org/licenses/}.
*/
package org.jackhuang.hmcl.util;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
public class LocalRepository {
private Path commonDirectory;
private Path cacheDirectory;
protected void changeDirectory(Path commonDir) {
commonDirectory = commonDir;
cacheDirectory = commonDir.resolve("cache");
}
public Path getCommonDirectory() {
return commonDirectory;
}
public Path getCacheDirectory() {
return cacheDirectory;
}
protected Path getFile(String algorithm, String hash) {
return getCacheDirectory().resolve(algorithm).resolve(hash.substring(0, 2)).resolve(hash);
}
protected boolean fileExists(String algorithm, String hash) {
if (hash == null) return false;
Path file = getFile(algorithm, hash);
if (Files.exists(file)) {
try {
return Hex.encodeHex(DigestUtils.digest(algorithm, file)).equalsIgnoreCase(hash);
} catch (IOException e) {
return false;
}
} else {
return false;
}
}
public void tryCacheFile(Path path, String algorithm, String hash) throws IOException {
Path cache = getFile(algorithm, hash);
if (Files.exists(cache)) return;
FileUtils.copyFile(path.toFile(), cache.toFile());
}
public Path cacheFile(Path path, String algorithm, String hash) throws IOException {
Path cache = getFile(algorithm, hash);
FileUtils.copyFile(path.toFile(), cache.toFile());
return cache;
}
public Optional<Path> checkExistentFile(Path original, String algorithm, String hash) {
if (fileExists(algorithm, hash))
return Optional.of(getFile(algorithm, hash));
if (original != null && Files.exists(original)) {
if (hash != null) {
try {
String checksum = Hex.encodeHex(DigestUtils.digest(algorithm, original));
if (checksum.equalsIgnoreCase(hash))
return Optional.of(restore(original, () -> cacheFile(original, algorithm, hash)));
} catch (IOException e) {
// we cannot check the hashcode.
}
} else {
return Optional.of(original);
}
}
return Optional.empty();
}
protected Path restore(Path original, ExceptionalSupplier<Path, ? extends IOException> cacheSupplier) throws IOException {
Path cache = cacheSupplier.get();
Files.delete(original);
Files.createLink(original, cache);
return cache;
}
private static LocalRepository instance = new LocalRepository();
public static LocalRepository getInstance() {
return instance;
}
public static void setInstance(LocalRepository instance) {
LocalRepository.instance = instance;
}
public static final String SHA1 = "SHA-1";
}