Move cache repository to HMCLCore

This commit is contained in:
huanghongxun
2018-09-07 11:27:14 +08:00
parent 9360854150
commit 84c18c8693
17 changed files with 124 additions and 245 deletions

View File

@@ -17,6 +17,8 @@
*/
package org.jackhuang.hmcl.download;
import org.jackhuang.hmcl.util.CacheRepository;
/**
*
* @author huangyuhui
@@ -25,6 +27,9 @@ public abstract class AbstractDependencyManager implements DependencyManager {
public abstract DownloadProvider getDownloadProvider();
@Override
public abstract DefaultCacheRepository getCacheRepository();
@Override
public VersionList<?> getVersionList(String id) {
return getDownloadProvider().getVersionListById(id);

View File

@@ -0,0 +1,271 @@
/*
* 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.download;
import org.jackhuang.hmcl.download.game.LibraryDownloadTask;
import org.jackhuang.hmcl.game.Library;
import org.jackhuang.hmcl.game.LibraryDownloadInfo;
import org.jackhuang.hmcl.util.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class DefaultCacheRepository extends CacheRepository {
private Path librariesDir;
private Path indexFile;
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private Index index = null;
public DefaultCacheRepository() {
this(OperatingSystem.getWorkingDirectory("minecraft").toPath());
}
public DefaultCacheRepository(Path commonDirectory) {
changeDirectory(commonDirectory);
}
@Override
public void changeDirectory(Path commonDir) {
super.changeDirectory(commonDir);
librariesDir = commonDir.resolve("libraries");
indexFile = getCacheDirectory().resolve("index.json");
lock.writeLock().lock();
try {
index = Constants.GSON.fromJson(FileUtils.readText(indexFile.toFile()), Index.class);
} catch (IOException e) {
Logging.LOG.log(Level.WARNING, "Unable to read index file", e);
index = new Index();
} finally {
lock.writeLock().unlock();
}
}
/**
* Try to cache the library given.
* This library will be cached only if it is verified.
* If cannot be verified, the library will not be cached.
*
* @param library the library being cached
* @param jar the file of library
*/
public void tryCacheLibrary(Library library, Path jar) {
lock.readLock().lock();
try {
if (index.getLibraries().stream().anyMatch(it -> library.getName().equals(it.getName())))
return;
} finally {
lock.readLock().unlock();
}
try {
LibraryDownloadInfo info = library.getDownload();
String hash = info.getSha1();
if (hash != null) {
String checksum = Hex.encodeHex(DigestUtils.digest("SHA-1", jar));
if (hash.equalsIgnoreCase(checksum))
cacheLibrary(library, jar, false);
} else if (library.getChecksums() != null && !library.getChecksums().isEmpty()) {
if (LibraryDownloadTask.checksumValid(jar.toFile(), library.getChecksums()))
cacheLibrary(library, jar, true);
} else {
// or we will not cache the library
}
} catch (IOException e) {
Logging.LOG.log(Level.WARNING, "Unable to calc hash value of file " + jar, e);
}
}
/**
* Get the path of cached library, empty if not cached
*
* @param library the library we check if cached.
* @return the cached path if exists, otherwise empty
*/
public Optional<Path> getLibrary(Library library) {
LibraryDownloadInfo info = library.getDownload();
String hash = info.getSha1();
if (fileExists(SHA1, hash))
return Optional.of(getFile(SHA1, hash));
Lock readLock = lock.readLock();
readLock.lock();
try {
// check if this library is from Forge
List<LibraryIndex> libraries = index.getLibraries().stream()
.filter(it -> it.getName().equals(library.getName()))
.collect(Collectors.toList());
for (LibraryIndex libIndex : libraries) {
if (fileExists(SHA1, libIndex.getHash())) {
Path file = getFile(SHA1, libIndex.getHash());
if (libIndex.getType().equalsIgnoreCase(LibraryIndex.TYPE_FORGE)) {
if (LibraryDownloadTask.checksumValid(file.toFile(), library.getChecksums()))
return Optional.of(file);
}
}
}
} finally {
readLock.unlock();
}
// check old common directory
Path jar = librariesDir.resolve(info.getPath());
if (Files.exists(jar)) {
try {
if (hash != null) {
String checksum = Hex.encodeHex(DigestUtils.digest("SHA-1", jar));
if (hash.equalsIgnoreCase(checksum))
return Optional.of(restore(jar, () -> cacheLibrary(library, jar, false)));
} else if (library.getChecksums() != null && !library.getChecksums().isEmpty()) {
if (LibraryDownloadTask.checksumValid(jar.toFile(), library.getChecksums()))
return Optional.of(restore(jar, () -> cacheLibrary(library, jar, true)));
} else {
return Optional.of(jar);
}
} catch (IOException e) {
// we cannot check the hashcode or unable to move file.
}
}
return Optional.empty();
}
/**
* Caches the library file to repository.
*
* @param library the library to cache
* @param path the file being cached, must be verified
* @param forge true if this library is provided by Forge
* @return cached file location
* @throws IOException if failed to calculate hash code of {@code path} or copy the file to cache
*/
public Path cacheLibrary(Library library, Path path, boolean forge) throws IOException {
String hash = library.getDownload().getSha1();
if (hash == null)
hash = Hex.encodeHex(DigestUtils.digest(SHA1, path));
Path cache = getFile(SHA1, hash);
FileUtils.copyFile(path.toFile(), cache.toFile());
Lock writeLock = lock.writeLock();
writeLock.lock();
try {
LibraryIndex libIndex = new LibraryIndex(library.getName(), hash, forge ? LibraryIndex.TYPE_FORGE : LibraryIndex.TYPE_JAR);
index.getLibraries().add(libIndex);
saveIndex();
} finally {
writeLock.unlock();
}
return cache;
}
private void saveIndex() {
if (indexFile == null || index == null) return;
try {
FileUtils.writeText(indexFile.toFile(), Constants.GSON.toJson(index));
} catch (IOException e) {
Logging.LOG.log(Level.SEVERE, "Unable to save index.json", e);
}
}
/**
* {
* "libraries": {
* // allow a library has multiple hash code.
* [
* "name": "net.minecraftforge:forge:1.11.2-13.20.0.2345",
* "hash": "blablabla",
* "type": "forge"
* ]
* }
* // assets and versions will not be included in index.
* }
*/
private class Index {
private final Set<LibraryIndex> libraries;
public Index() {
this(new HashSet<>());
}
public Index(Set<LibraryIndex> libraries) {
this.libraries = libraries;
}
public Set<LibraryIndex> getLibraries() {
return libraries;
}
}
private class LibraryIndex {
private final String name;
private final String hash;
private final String type;
public LibraryIndex() {
this(null, null, null);
}
public LibraryIndex(String name, String hash, String type) {
this.name = name;
this.hash = hash;
this.type = type;
}
public String getName() {
return name;
}
public String getHash() {
return hash;
}
public String getType() {
return type;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LibraryIndex that = (LibraryIndex) o;
return Objects.equals(name, that.name) &&
Objects.equals(hash, that.hash) &&
Objects.equals(type, that.type);
}
@Override
public int hashCode() {
return Objects.hash(name, hash, type);
}
public static final String TYPE_FORGE = "forge";
public static final String TYPE_JAR = "jar";
}
}

View File

@@ -43,10 +43,12 @@ public class DefaultDependencyManager extends AbstractDependencyManager {
private final DefaultGameRepository repository;
private final DownloadProvider downloadProvider;
private final DefaultCacheRepository cacheRepository;
public DefaultDependencyManager(DefaultGameRepository repository, DownloadProvider downloadProvider) {
public DefaultDependencyManager(DefaultGameRepository repository, DownloadProvider downloadProvider, DefaultCacheRepository cacheRepository) {
this.repository = repository;
this.downloadProvider = downloadProvider;
this.cacheRepository = cacheRepository;
}
@Override
@@ -59,6 +61,11 @@ public class DefaultDependencyManager extends AbstractDependencyManager {
return downloadProvider;
}
@Override
public DefaultCacheRepository getCacheRepository() {
return cacheRepository;
}
@Override
public GameBuilder gameBuilder() {
return new DefaultGameBuilder(this);

View File

@@ -20,6 +20,7 @@ package org.jackhuang.hmcl.download;
import org.jackhuang.hmcl.game.GameRepository;
import org.jackhuang.hmcl.game.Version;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.CacheRepository;
/**
* Do everything that will connect to Internet.
@@ -34,6 +35,11 @@ public interface DependencyManager {
*/
GameRepository getGameRepository();
/**
* The cache repository
*/
CacheRepository getCacheRepository();
/**
* Check if the game is complete.
* Check libraries, assets files and so on.

View File

@@ -26,7 +26,7 @@ import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.Constants;
import org.jackhuang.hmcl.util.FileUtils;
import org.jackhuang.hmcl.util.LocalRepository;
import org.jackhuang.hmcl.util.CacheRepository;
import org.jackhuang.hmcl.util.NetworkUtils;
import java.io.File;
@@ -84,14 +84,15 @@ public final class GameAssetDownloadTask extends Task {
File file = dependencyManager.getGameRepository().getAssetObject(version.getId(), assetIndexInfo.getId(), assetObject);
if (file.isFile())
LocalRepository.getInstance().tryCacheFile(file.toPath(), LocalRepository.SHA1, assetObject.getHash());
dependencyManager.getCacheRepository().tryCacheFile(file.toPath(), CacheRepository.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
.setCacheRepository(dependencyManager.getCacheRepository())
.setCaching(true)
.setCandidate(LocalRepository.getInstance().getCommonDirectory()
.setCandidate(dependencyManager.getCacheRepository().getCommonDirectory()
.resolve("assets").resolve("objects").resolve(assetObject.getLocation())));
}

View File

@@ -22,7 +22,7 @@ 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.LocalRepository;
import org.jackhuang.hmcl.util.CacheRepository;
import org.jackhuang.hmcl.util.NetworkUtils;
import java.io.File;
@@ -68,7 +68,8 @@ public final class GameAssetIndexDownloadTask extends Task {
NetworkUtils.toURL(dependencyManager.getDownloadProvider().injectURL(assetIndexInfo.getUrl())),
assetIndexFile
).setCaching(true)
.setCandidate(LocalRepository.getInstance().getCommonDirectory()
.setCacheRepository(dependencyManager.getCacheRepository())
.setCandidate(dependencyManager.getCacheRepository().getCommonDirectory()
.resolve("assets").resolve("indexes").resolve(assetIndexInfo.getId() + ".json")));
}

View File

@@ -22,7 +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.CacheRepository;
import org.jackhuang.hmcl.util.NetworkUtils;
import java.io.File;
@@ -59,9 +59,10 @@ public final class GameDownloadTask extends Task {
dependencies.add(new FileDownloadTask(
NetworkUtils.toURL(dependencyManager.getDownloadProvider().injectURL(version.getDownloadInfo().getUrl())),
jar,
IntegrityCheck.of(LocalRepository.SHA1, version.getDownloadInfo().getSha1()))
IntegrityCheck.of(CacheRepository.SHA1, version.getDownloadInfo().getSha1()))
.setCaching(true)
.setCandidate(LocalRepository.getInstance().getCommonDirectory().resolve("jars").resolve(gameVersion + ".jar")));
.setCacheRepository(dependencyManager.getCacheRepository())
.setCandidate(dependencyManager.getCacheRepository().getCommonDirectory().resolve("jars").resolve(gameVersion + ".jar")));
}
}

View File

@@ -61,6 +61,8 @@ public final class GameLibrariesTask extends Task {
File file = dependencyManager.getGameRepository().getLibraryFile(version, library);
if (!file.exists())
dependencies.add(new LibraryDownloadTask(dependencyManager, file, library));
else
dependencyManager.getCacheRepository().tryCacheLibrary(library, file.toPath());
});
}

View File

@@ -1,6 +1,7 @@
package org.jackhuang.hmcl.download.game;
import org.jackhuang.hmcl.download.AbstractDependencyManager;
import org.jackhuang.hmcl.download.DefaultCacheRepository;
import org.jackhuang.hmcl.game.Library;
import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.FileDownloadTask.IntegrityCheck;
@@ -14,11 +15,13 @@ import org.tukaani.xz.XZInputStream;
import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;
import java.util.logging.Level;
import static org.jackhuang.hmcl.util.DigestUtils.digest;
import static org.jackhuang.hmcl.util.Hex.encodeHex;
@@ -26,10 +29,12 @@ import static org.jackhuang.hmcl.util.Hex.encodeHex;
public class LibraryDownloadTask extends Task {
private FileDownloadTask task;
protected final File jar;
protected final DefaultCacheRepository cacheRepository;
private final File xzFile;
protected final Library library;
protected final String url;
protected boolean xz;
private boolean cached = false;
public LibraryDownloadTask(AbstractDependencyManager dependencyManager, File file, Library library) {
setSignificance(TaskSignificance.MODERATE);
@@ -38,6 +43,7 @@ public class LibraryDownloadTask extends Task {
library = library.setClassifier("universal");
this.library = library;
this.cacheRepository = dependencyManager.getCacheRepository();
url = dependencyManager.getDownloadProvider().injectURL(library.getDownload().getUrl());
jar = file;
@@ -47,7 +53,8 @@ public class LibraryDownloadTask extends Task {
@Override
public Collection<? extends Task> getDependents() {
return Collections.singleton(task);
if (cached) return Collections.emptyList();
else return Collections.singleton(task);
}
@Override
@@ -57,6 +64,8 @@ public class LibraryDownloadTask extends Task {
@Override
public void execute() throws Exception {
if (cached) return;
if (!isDependentsSucceeded()) {
// Since FileDownloadTask wraps the actual exception with another IOException.
// We should extract it letting the error message clearer.
@@ -76,6 +85,19 @@ public class LibraryDownloadTask extends Task {
@Override
public void preExecute() throws Exception {
Optional<Path> libPath = cacheRepository.getLibrary(library);
if (libPath.isPresent()) {
try {
FileUtils.copyFile(libPath.get().toFile(), jar);
cached = true;
return;
} catch (IOException e) {
Logging.LOG.log(Level.WARNING, "Failed to copy file from cache", e);
// We cannot copy cached file to current location
// so we try to download a new one.
}
}
try {
URL packXz = NetworkUtils.toURL(url + ".pack.xz");
if (NetworkUtils.URLExists(packXz)) {
@@ -92,6 +114,12 @@ public class LibraryDownloadTask extends Task {
}
}
@Override
public void postExecute() throws Exception {
if (!cached)
cacheRepository.cacheLibrary(library, jar.toPath(), xz);
}
public static boolean checksumValid(File libPath, List<String> checksums) {
try {
if (checksums == null || checksums.isEmpty()) {

View File

@@ -84,6 +84,7 @@ public class FileDownloadTask extends Task {
private final EventManager<FailedEvent<URL>> onFailed = new EventManager<>();
private Path candidate;
private boolean caching;
private CacheRepository repository = CacheRepository.getInstance();
private RandomAccessFile rFile;
private InputStream stream;
@@ -165,14 +166,18 @@ public class FileDownloadTask extends Task {
return this;
}
public FileDownloadTask setCacheRepository(CacheRepository repository) {
this.repository = repository;
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());
Optional<Path> cache = repository.checkExistentFile(candidate, integrityCheck.getAlgorithm(), integrityCheck.getChecksum());
if (cache.isPresent()) {
try {
FileUtils.copyFile(cache.get().toFile(), file);
@@ -296,9 +301,9 @@ public class FileDownloadTask extends Task {
if (caching) {
try {
if (integrityCheck == null)
LocalRepository.getInstance().cacheFile(file.toPath(), LocalRepository.SHA1, Hex.encodeHex(DigestUtils.digest(LocalRepository.SHA1, file.toPath())));
repository.cacheFile(file.toPath(), CacheRepository.SHA1, Hex.encodeHex(DigestUtils.digest(CacheRepository.SHA1, file.toPath())));
else
LocalRepository.getInstance().cacheFile(file.toPath(), integrityCheck.getAlgorithm(), integrityCheck.getChecksum());
repository.cacheFile(file.toPath(), integrityCheck.getAlgorithm(), integrityCheck.getChecksum());
} catch (IOException e) {
Logging.LOG.log(Level.WARNING, "Failed to cache file", e);
}

View File

@@ -22,11 +22,11 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
public class LocalRepository {
public class CacheRepository {
private Path commonDirectory;
private Path cacheDirectory;
protected void changeDirectory(Path commonDir) {
public void changeDirectory(Path commonDir) {
commonDirectory = commonDir;
cacheDirectory = commonDir.resolve("cache");
}
@@ -97,14 +97,14 @@ public class LocalRepository {
return cache;
}
private static LocalRepository instance = new LocalRepository();
private static CacheRepository instance = new CacheRepository();
public static LocalRepository getInstance() {
public static CacheRepository getInstance() {
return instance;
}
public static void setInstance(LocalRepository instance) {
LocalRepository.instance = instance;
public static void setInstance(CacheRepository instance) {
CacheRepository.instance = instance;
}
public static final String SHA1 = "SHA-1";