diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/DefaultGameRepository.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/DefaultGameRepository.java index b31592683..0689f5fda 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/DefaultGameRepository.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/DefaultGameRepository.java @@ -137,7 +137,7 @@ public class DefaultGameRepository implements GameRepository { FileUtils.writeText(toJson, Constants.GSON.toJson(fromVersion.setId(to))); return true; - } catch (IOException | JsonSyntaxException | VersionNotFoundException e) { + } catch (IOException | JsonParseException | VersionNotFoundException e) { return false; } } @@ -158,6 +158,8 @@ public class DefaultGameRepository implements GameRepository { versions.put(version.getId(), version); } + SimpleVersionProvider provider = new SimpleVersionProvider(); + File[] files = new File(getBaseDirectory(), "versions").listFiles(); if (files != null) for (File dir : files) @@ -194,15 +196,25 @@ public class DefaultGameRepository implements GameRepository { try { FileUtils.writeText(json, Constants.GSON.toJson(version)); } catch (Exception e) { - Logging.LOG.log(Level.WARNING, "Ignoring version {0} because wrong id {1} is set and cannot correct it.", new Object[] { id, version.getId() }); + Logging.LOG.log(Level.WARNING, "Ignoring version {0} because wrong id {1} is set and cannot correct it.", new Object[]{id, version.getId()}); continue; } } - if (EventBus.EVENT_BUS.fireEvent(new LoadedOneVersionEvent(this, version)) != Event.Result.DENY) - versions.put(id, version); + provider.addVersion(version); } + for (Version version : provider.getVersionMap().values()) { + try { + Version resolved = version.resolve(provider); + + if (EventBus.EVENT_BUS.fireEvent(new LoadedOneVersionEvent(this, resolved)) != Event.Result.DENY) + versions.put(version.getId(), version); + } catch (VersionNotFoundException e) { + Logging.LOG.log(Level.WARNING, "Ignoring version {0} because it inherits from a nonexistent version."); + } + } + loaded = true; } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/GameRepository.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/GameRepository.java index 0749a0daf..c354b393f 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/GameRepository.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/GameRepository.java @@ -46,9 +46,10 @@ public interface GameRepository extends VersionProvider { * * @param id the id of version * @return the version you want + * @throws VersionNotFoundException if no version is id. */ @Override - Version getVersion(String id); + Version getVersion(String id) throws VersionNotFoundException; /** * How many version are there? @@ -125,7 +126,7 @@ public interface GameRepository extends VersionProvider { * @param version version id * @return the minecraft jar */ - default File getVersionJar(String version) { + default File getVersionJar(String version) throws VersionNotFoundException { return getVersionJar(getVersion(version).resolve(this)); } @@ -135,8 +136,7 @@ public interface GameRepository extends VersionProvider { * @param from The id of original version * @param to The new id of the version * @throws UnsupportedOperationException if this game repository does not support renaming a version - * @throws java.io.IOException if I/O operation fails. - * @return true if the operation is done successfully. + * @return true if the operation is done successfully, false if version `from` not found, version json is malformed or I/O errors occurred. */ boolean renameVersion(String from, String to); @@ -147,7 +147,6 @@ public interface GameRepository extends VersionProvider { * * @param version the id of specific version that is relevant to [assetId] * @param assetId the asset id, you can find it in [AssetIndexInfo.id] [Version.actualAssetIndex.id] - * @throws java.io.IOException if I/O operation fails. * @return the actual asset directory */ File getActualAssetDirectory(String version, String assetId); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/SimpleVersionProvider.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/SimpleVersionProvider.java index 3d77b1889..084ad2c0a 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/SimpleVersionProvider.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/SimpleVersionProvider.java @@ -44,4 +44,8 @@ public class SimpleVersionProvider implements VersionProvider { public void addVersion(Version version) { versionMap.put(version.getId(), version); } + + public Map getVersionMap() { + return versionMap; + } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/Version.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/Version.java index ad210e64b..a757fcf3f 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/Version.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/Version.java @@ -143,11 +143,11 @@ public class Version implements Comparable, Validation { /** * Resolve given version */ - public Version resolve(VersionProvider provider) { + public Version resolve(VersionProvider provider) throws VersionNotFoundException { return resolve(provider, new HashSet<>()); } - protected Version resolve(VersionProvider provider, Set resolvedSoFar) { + protected Version resolve(VersionProvider provider, Set resolvedSoFar) throws VersionNotFoundException { if (inheritsFrom == null) return this; diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/VersionProvider.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/VersionProvider.java index fcfdd568a..64819f63b 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/VersionProvider.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/VersionProvider.java @@ -39,5 +39,5 @@ public interface VersionProvider { * @param id the id of version * @return the version you want */ - Version getVersion(String id); + Version getVersion(String id) throws VersionNotFoundException; } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FileDownloadTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FileDownloadTask.java index a21ed15fa..ac6b0d960 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FileDownloadTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FileDownloadTask.java @@ -203,7 +203,7 @@ public class FileDownloadTask extends Task { if (!FileUtils.makeDirectory(file.getAbsoluteFile().getParentFile())) throw new IOException("Unable to make parent directory " + file); try { - FileUtils.cutFile(temp, file); + FileUtils.moveFile(temp, file); } catch (Exception e) { throw new IOException("Unable to move temp file from " + temp + " to " + file, e); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/FileUtils.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/FileUtils.java index e48f22120..5679e6cfc 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/FileUtils.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/FileUtils.java @@ -236,7 +236,7 @@ public final class FileUtils { doCopyFile(srcFile, destFile); } - public static void cutFile(File srcFile, File destFile) throws IOException { + public static void moveFile(File srcFile, File destFile) throws IOException { copyFile(srcFile, destFile); srcFile.delete(); }