From 25d79a94f81ebe5b3d8d9607a593a31848347f11 Mon Sep 17 00:00:00 2001 From: huangyuhui Date: Sun, 9 Sep 2018 18:55:01 +0800 Subject: [PATCH] Move specialize and globalize VersionSetting to HMCLGameRepository --- .../hmcl/game/HMCLGameRepository.java | 21 +++++++++++++ .../jackhuang/hmcl/game/ModpackHelper.java | 7 +++-- .../MultiMCInstallVersionSettingTask.java | 2 +- .../org/jackhuang/hmcl/setting/Profile.java | 30 ++----------------- .../hmcl/ui/versions/VersionSettingsPage.java | 4 +-- 5 files changed, 31 insertions(+), 33 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLGameRepository.java b/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLGameRepository.java index e223ff037..5ae8b0d88 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLGameRepository.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLGameRepository.java @@ -164,6 +164,27 @@ public class HMCLGameRepository extends DefaultGameRepository { } } + /** + * Make version use self version settings instead of the global one. + * @param id the version id. + * @return specialized version setting, null if given version does not exist. + */ + public VersionSetting specializeVersionSetting(String id) { + VersionSetting vs = getVersionSetting(id); + if (vs == null) + vs = createVersionSetting(id); + if (vs == null) + return null; + vs.setUsesGlobal(false); + return vs; + } + + public void globalizeVersionSetting(String id) { + VersionSetting vs = getVersionSetting(id); + if (vs != null) + vs.setUsesGlobal(true); + } + public boolean forbidsVersion(String id) { return FORBIDDEN.contains(id); } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/game/ModpackHelper.java b/HMCL/src/main/java/org/jackhuang/hmcl/game/ModpackHelper.java index e8d9618ae..f399a21d3 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/ModpackHelper.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/ModpackHelper.java @@ -87,9 +87,10 @@ public final class ModpackHelper { FinalizedCallback finalizeTask = (variables, isDependentsSucceeded) -> { if (isDependentsSucceeded) { - profile.getRepository().refreshVersions(); - VersionSetting vs = profile.specializeVersionSetting(name); - profile.getRepository().undoMark(name); + HMCLGameRepository repository = profile.getRepository(); + repository.refreshVersions(); + VersionSetting vs = repository.specializeVersionSetting(name); + repository.undoMark(name); if (vs != null) vs.setGameDirType(EnumGameDirectory.VERSION_FOLDER); } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/game/MultiMCInstallVersionSettingTask.java b/HMCL/src/main/java/org/jackhuang/hmcl/game/MultiMCInstallVersionSettingTask.java index 329b6a7f9..7685d84f3 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/MultiMCInstallVersionSettingTask.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/MultiMCInstallVersionSettingTask.java @@ -44,7 +44,7 @@ public final class MultiMCInstallVersionSettingTask extends Task { @Override public void execute() { - VersionSetting vs = Objects.requireNonNull(profile.specializeVersionSetting(version)); + VersionSetting vs = Objects.requireNonNull(profile.getRepository().specializeVersionSetting(version)); ModpackHelper.toVersionSetting(manifest, vs); } } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/setting/Profile.java b/HMCL/src/main/java/org/jackhuang/hmcl/setting/Profile.java index 84c0d1250..1fbbbfc58 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/setting/Profile.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/setting/Profile.java @@ -28,6 +28,8 @@ import org.jackhuang.hmcl.game.HMCLCacheRepository; import org.jackhuang.hmcl.game.HMCLGameRepository; import org.jackhuang.hmcl.game.Version; import org.jackhuang.hmcl.mod.ModManager; +import org.jackhuang.hmcl.task.Schedulers; +import org.jackhuang.hmcl.ui.FXUtils; import org.jackhuang.hmcl.ui.WeakListenerHelper; import org.jackhuang.hmcl.util.*; @@ -174,32 +176,6 @@ public final class Profile implements Observable { return vs; } - public boolean isVersionGlobal(String id) { - VersionSetting vs = repository.getVersionSetting(id); - return vs == null || vs.isUsesGlobal(); - } - - /** - * Make version use self version settings instead of the global one. - * @param id the version id. - * @return specialized version setting, null if given version does not exist. - */ - public VersionSetting specializeVersionSetting(String id) { - VersionSetting vs = repository.getVersionSetting(id); - if (vs == null) - vs = repository.createVersionSetting(id); - if (vs == null) - return null; - vs.setUsesGlobal(false); - return vs; - } - - public void globalizeVersionSetting(String id) { - VersionSetting vs = repository.getVersionSetting(id); - if (vs != null) - vs.setUsesGlobal(true); - } - @Override public String toString() { return new ToStringBuilder(this) @@ -231,7 +207,7 @@ public final class Profile implements Observable { } protected void invalidate() { - observableHelper.invalidate(); + Schedulers.computation().schedule(observableHelper::invalidate); } public static final class Serializer implements JsonSerializer, JsonDeserializer { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/VersionSettingsPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/VersionSettingsPage.java index bdf8defde..fdec7553c 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/VersionSettingsPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/VersionSettingsPage.java @@ -134,9 +134,9 @@ public final class VersionSettingsPage extends StackPane implements DecoratorPag // because versionSettings can be the global one. // global versionSettings.usesGlobal is always true. if (newValue) - profile.specializeVersionSetting(versionId); + profile.getRepository().specializeVersionSetting(versionId); else - profile.globalizeVersionSetting(versionId); + profile.getRepository().globalizeVersionSetting(versionId); Platform.runLater(() -> loadVersionSetting(profile, versionId)); });