From d84877dfcc5bd6085b8713126ce4b6e0a153355e Mon Sep 17 00:00:00 2001 From: huanghongxun Date: Mon, 30 Mar 2020 16:16:41 +0800 Subject: [PATCH] refactor: Move EnumGameDirectory into HMCLCore --- .../hmcl/game/HMCLGameRepository.java | 35 ++++++++++--------- .../jackhuang/hmcl/game/ModpackHelper.java | 7 ++-- .../hmcl/setting/VersionSetting.java | 11 +++--- .../hmcl/ui/versions/VersionSettingsPage.java | 9 ++--- .../jackhuang/hmcl/ui/versions/Versions.java | 4 +-- .../hmcl/game/DefaultGameRepository.java | 10 +++++- .../hmcl/game/GameDirectoryType.java | 4 +-- 7 files changed, 45 insertions(+), 35 deletions(-) rename HMCL/src/main/java/org/jackhuang/hmcl/setting/EnumGameDirectory.java => HMCLCore/src/main/java/org/jackhuang/hmcl/game/GameDirectoryType.java (93%) 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 1e5bc3a99..8f6c2c743 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLGameRepository.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/HMCLGameRepository.java @@ -21,7 +21,6 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import javafx.scene.image.Image; import org.jackhuang.hmcl.mod.Modpack; -import org.jackhuang.hmcl.setting.EnumGameDirectory; import org.jackhuang.hmcl.setting.Profile; import org.jackhuang.hmcl.setting.VersionSetting; import org.jackhuang.hmcl.util.Logging; @@ -42,8 +41,6 @@ public class HMCLGameRepository extends DefaultGameRepository { private final Map localVersionSettings = new HashMap<>(); private final Set beingModpackVersions = new HashSet<>(); - public boolean checkedModpack = false, checkingModpack = false; - public HMCLGameRepository(Profile profile, File baseDirectory) { super(baseDirectory); this.profile = profile; @@ -53,18 +50,22 @@ public class HMCLGameRepository extends DefaultGameRepository { return profile; } + @Override + public GameDirectoryType getGameDirectoryType(String id) { + if (beingModpackVersions.contains(id) || isModpack(id)) { + return GameDirectoryType.VERSION_FOLDER; + } else { + return getVersionSetting(id).getGameDirType(); + } + } + @Override public File getRunDirectory(String id) { - if (beingModpackVersions.contains(id) || isModpack(id)) - return getVersionRoot(id); - else { - VersionSetting vs = getVersionSetting(id); - switch (vs.getGameDirType()) { - case VERSION_FOLDER: return getVersionRoot(id); - case ROOT_FOLDER: return super.getRunDirectory(id); - case CUSTOM: return new File(vs.getGameDir()); - default: throw new Error(); - } + switch (getGameDirectoryType(id)) { + case VERSION_FOLDER: return getVersionRoot(id); + case ROOT_FOLDER: return super.getRunDirectory(id); + case CUSTOM: return new File(getVersionSetting(id).getGameDir()); + default: throw new Error(); } } @@ -107,9 +108,9 @@ public class HMCLGameRepository extends DefaultGameRepository { Files.move(dstDir.toPath().resolve(srcId + ".jar"), dstDir.toPath().resolve(dstId + ".jar")); Files.move(dstDir.toPath().resolve(srcId + ".json"), dstDir.toPath().resolve(dstId + ".json")); VersionSetting oldVersionSetting = getVersionSetting(srcId).clone(); - EnumGameDirectory originalGameDirType = oldVersionSetting.getGameDirType(); + GameDirectoryType originalGameDirType = oldVersionSetting.getGameDirType(); oldVersionSetting.setUsesGlobal(false); - oldVersionSetting.setGameDirType(EnumGameDirectory.VERSION_FOLDER); + oldVersionSetting.setGameDirType(GameDirectoryType.VERSION_FOLDER); VersionSetting newVersionSetting = initLocalVersionSetting(dstId, oldVersionSetting); saveVersionSetting(dstId); @@ -132,7 +133,7 @@ public class HMCLGameRepository extends DefaultGameRepository { if (!copySaves) blackList.add("saves"); - if (originalGameDirType != EnumGameDirectory.VERSION_FOLDER) + if (originalGameDirType != GameDirectoryType.VERSION_FOLDER) FileUtils.copyDirectory(srcGameDir.toPath(), dstGameDir.toPath(), path -> Modpack.acceptFile(path, blackList, null)); } @@ -184,7 +185,7 @@ public class HMCLGameRepository extends DefaultGameRepository { loadLocalVersionSetting(id); VersionSetting setting = localVersionSettings.get(id); if (setting != null && isModpack(id)) - setting.setGameDirType(EnumGameDirectory.VERSION_FOLDER); + setting.setGameDirType(GameDirectoryType.VERSION_FOLDER); return setting; } 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 b3e49bc12..59817058d 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/ModpackHelper.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/ModpackHelper.java @@ -28,7 +28,6 @@ import org.jackhuang.hmcl.mod.multimc.MultiMCModpackInstallTask; import org.jackhuang.hmcl.mod.server.ServerModpackLocalInstallTask; import org.jackhuang.hmcl.mod.server.ServerModpackManifest; import org.jackhuang.hmcl.mod.server.ServerModpackRemoteInstallTask; -import org.jackhuang.hmcl.setting.EnumGameDirectory; import org.jackhuang.hmcl.setting.Profile; import org.jackhuang.hmcl.setting.VersionSetting; import org.jackhuang.hmcl.task.Schedulers; @@ -109,7 +108,7 @@ public final class ModpackHelper { VersionSetting vs = repository.specializeVersionSetting(name); repository.undoMark(name); if (vs != null) - vs.setGameDirType(EnumGameDirectory.VERSION_FOLDER); + vs.setGameDirType(GameDirectoryType.VERSION_FOLDER); }; ExceptionalConsumer failure = ex -> { @@ -132,7 +131,7 @@ public final class ModpackHelper { VersionSetting vs = repository.specializeVersionSetting(name); repository.undoMark(name); if (vs != null) - vs.setGameDirType(EnumGameDirectory.VERSION_FOLDER); + vs.setGameDirType(GameDirectoryType.VERSION_FOLDER); }; ExceptionalConsumer failure = ex -> { @@ -184,7 +183,7 @@ public final class ModpackHelper { public static void toVersionSetting(MultiMCInstanceConfiguration c, VersionSetting vs) { vs.setUsesGlobal(false); - vs.setGameDirType(EnumGameDirectory.VERSION_FOLDER); + vs.setGameDirType(GameDirectoryType.VERSION_FOLDER); if (c.isOverrideJavaLocation()) { vs.setJavaDir(Lang.nonNull(c.getJavaPath(), "")); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/setting/VersionSetting.java b/HMCL/src/main/java/org/jackhuang/hmcl/setting/VersionSetting.java index a15a41e9a..a5532d50a 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/setting/VersionSetting.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/setting/VersionSetting.java @@ -22,6 +22,7 @@ import com.google.gson.annotations.JsonAdapter; import javafx.beans.InvalidationListener; import javafx.beans.property.*; import org.jackhuang.hmcl.Metadata; +import org.jackhuang.hmcl.game.GameDirectoryType; import org.jackhuang.hmcl.game.LaunchOptions; import org.jackhuang.hmcl.util.Lang; import org.jackhuang.hmcl.util.StringUtils; @@ -416,17 +417,17 @@ public final class VersionSetting implements Cloneable { * 0 - .minecraft
* 1 - .minecraft/versions/<version>/
*/ - private final ObjectProperty gameDirTypeProperty = new SimpleObjectProperty<>(this, "gameDirType", EnumGameDirectory.ROOT_FOLDER); + private final ObjectProperty gameDirTypeProperty = new SimpleObjectProperty<>(this, "gameDirType", GameDirectoryType.ROOT_FOLDER); - public ObjectProperty gameDirTypeProperty() { + public ObjectProperty gameDirTypeProperty() { return gameDirTypeProperty; } - public EnumGameDirectory getGameDirType() { + public GameDirectoryType getGameDirType() { return gameDirTypeProperty.get(); } - public void setGameDirType(EnumGameDirectory gameDirType) { + public void setGameDirType(GameDirectoryType gameDirType) { gameDirTypeProperty.set(gameDirType); } @@ -654,7 +655,7 @@ public final class VersionSetting implements Cloneable { vs.setNotCheckJVM(Optional.ofNullable(obj.get("notCheckJVM")).map(JsonElement::getAsBoolean).orElse(false)); vs.setShowLogs(Optional.ofNullable(obj.get("showLogs")).map(JsonElement::getAsBoolean).orElse(false)); vs.setLauncherVisibility(LauncherVisibility.values()[Optional.ofNullable(obj.get("launcherVisibility")).map(JsonElement::getAsInt).orElse(1)]); - vs.setGameDirType(EnumGameDirectory.values()[Optional.ofNullable(obj.get("gameDirType")).map(JsonElement::getAsInt).orElse(0)]); + vs.setGameDirType(GameDirectoryType.values()[Optional.ofNullable(obj.get("gameDirType")).map(JsonElement::getAsInt).orElse(0)]); vs.setDefaultJavaPath(Optional.ofNullable(obj.get("defaultJavaPath")).map(JsonElement::getAsString).orElse(null)); return vs; 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 972f42602..8a1fe7c73 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 @@ -34,6 +34,7 @@ import javafx.scene.layout.BorderPane; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.stage.FileChooser; +import org.jackhuang.hmcl.game.GameDirectoryType; import org.jackhuang.hmcl.setting.*; import org.jackhuang.hmcl.task.Schedulers; import org.jackhuang.hmcl.task.Task; @@ -91,7 +92,7 @@ public final class VersionSettingsPage extends StackPane implements DecoratorPag @FXML private JFXToggleButton chkNoGameCheck; @FXML private JFXToggleButton chkNoJVMCheck; @FXML private MultiFileItem javaItem; - @FXML private MultiFileItem gameDirItem; + @FXML private MultiFileItem gameDirItem; @FXML private JFXToggleButton chkShowLogs; @FXML private ImagePickerItem iconPickerItem; @FXML private JFXCheckBox chkEnableSpecificSettings; @@ -131,10 +132,10 @@ public final class VersionSettingsPage extends StackPane implements DecoratorPag if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) javaItem.getExtensionFilters().add(new FileChooser.ExtensionFilter("Java", "java.exe", "javaw.exe")); - gameDirItem.setCustomUserData(EnumGameDirectory.CUSTOM); + gameDirItem.setCustomUserData(GameDirectoryType.CUSTOM); gameDirItem.loadChildren(Arrays.asList( - gameDirItem.createChildren(i18n("settings.advanced.game_dir.default"), EnumGameDirectory.ROOT_FOLDER), - gameDirItem.createChildren(i18n("settings.advanced.game_dir.independent"), EnumGameDirectory.VERSION_FOLDER) + gameDirItem.createChildren(i18n("settings.advanced.game_dir.default"), GameDirectoryType.ROOT_FOLDER), + gameDirItem.createChildren(i18n("settings.advanced.game_dir.independent"), GameDirectoryType.VERSION_FOLDER) )); chkEnableSpecificSettings.selectedProperty().addListener((a, b, newValue) -> { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/Versions.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/Versions.java index 2aa7f5591..1efa254d3 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/Versions.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/Versions.java @@ -22,7 +22,7 @@ import org.jackhuang.hmcl.download.game.GameAssetDownloadTask; import org.jackhuang.hmcl.game.GameRepository; import org.jackhuang.hmcl.game.LauncherHelper; import org.jackhuang.hmcl.setting.Accounts; -import org.jackhuang.hmcl.setting.EnumGameDirectory; +import org.jackhuang.hmcl.game.GameDirectoryType; import org.jackhuang.hmcl.setting.Profile; import org.jackhuang.hmcl.task.Schedulers; import org.jackhuang.hmcl.task.Task; @@ -48,7 +48,7 @@ import static org.jackhuang.hmcl.util.i18n.I18n.i18n; public class Versions { public static void deleteVersion(Profile profile, String version) { - boolean isIndependent = profile.getVersionSetting(version).getGameDirType() == EnumGameDirectory.VERSION_FOLDER; + boolean isIndependent = profile.getVersionSetting(version).getGameDirType() == GameDirectoryType.VERSION_FOLDER; boolean isMovingToTrashSupported = FileUtils.isMovingToTrashSupported(); String message = isIndependent ? i18n("version.manage.remove.confirm.independent", version) : isMovingToTrashSupported ? i18n("version.manage.remove.confirm.trash", version, version + "_removed") : 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 f82d897e3..8a1a26407 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/DefaultGameRepository.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/DefaultGameRepository.java @@ -105,9 +105,17 @@ public class DefaultGameRepository implements GameRepository { return artifact.getPath(getBaseDirectory().toPath().resolve("libraries")); } + public GameDirectoryType getGameDirectoryType(String id) { + return GameDirectoryType.ROOT_FOLDER; + } + @Override public File getRunDirectory(String id) { - return getBaseDirectory(); + switch (getGameDirectoryType(id)) { + case VERSION_FOLDER: return getVersionRoot(id); + case ROOT_FOLDER: return getBaseDirectory(); + default: throw new IllegalStateException(); + } } @Override diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/setting/EnumGameDirectory.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/GameDirectoryType.java similarity index 93% rename from HMCL/src/main/java/org/jackhuang/hmcl/setting/EnumGameDirectory.java rename to HMCLCore/src/main/java/org/jackhuang/hmcl/game/GameDirectoryType.java index 4698a49de..a40b16adf 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/setting/EnumGameDirectory.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/GameDirectoryType.java @@ -15,14 +15,14 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package org.jackhuang.hmcl.setting; +package org.jackhuang.hmcl.game; /** * Determines where game runs in and game files such as mods. * * @author huangyuhui */ -public enum EnumGameDirectory { +public enum GameDirectoryType { /** * .minecraft */