From 0be056ac043d664eaa5d2719402a706cb7441587 Mon Sep 17 00:00:00 2001 From: huanghongxun Date: Fri, 10 Jul 2015 22:53:18 +0800 Subject: [PATCH] Reconstruct the codes && fix wrong file separator when switch to anther os from Windows. --- .../launcher/launch/LaunchFinisher.java | 52 ++----------- .../launcher/settings/LauncherVisibility.java | 27 +++++++ .../launcher/settings/Profile.java | 12 ++- .../launcher/settings/Settings.java | 22 ++++-- .../launcher/views/GameSettingsPanel.java | 22 +++--- .../utils/system/JavaProcessMonitor.java | 75 +++++++++++++++++++ .../hellominecraft/utils/system/OS.java | 14 +++- 7 files changed, 154 insertions(+), 70 deletions(-) create mode 100644 HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/settings/LauncherVisibility.java create mode 100644 HMCLAPI/src/main/java/org/jackhuang/hellominecraft/utils/system/JavaProcessMonitor.java diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/launch/LaunchFinisher.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/launch/LaunchFinisher.java index 4cc1be811..162c53556 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/launch/LaunchFinisher.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/launch/LaunchFinisher.java @@ -16,18 +16,12 @@ */ package org.jackhuang.hellominecraft.launcher.launch; -import java.util.Arrays; -import java.util.HashSet; import java.util.List; -import org.jackhuang.hellominecraft.C; +import org.jackhuang.hellominecraft.launcher.settings.LauncherVisibility; import org.jackhuang.hellominecraft.utils.functions.TrueFunction; import org.jackhuang.hellominecraft.launcher.views.MainFrame; -import org.jackhuang.hellominecraft.utils.CollectionUtils; import org.jackhuang.hellominecraft.utils.Event; -import org.jackhuang.hellominecraft.utils.system.JavaProcess; -import org.jackhuang.hellominecraft.utils.system.MessageBox; -import org.jackhuang.hellominecraft.utils.system.ProcessThread; -import org.jackhuang.hellominecraft.utils.StrUtils; +import org.jackhuang.hellominecraft.utils.system.JavaProcessMonitor; import org.jackhuang.hellominecraft.views.LogWindow; /** @@ -36,58 +30,28 @@ import org.jackhuang.hellominecraft.views.LogWindow; */ public class LaunchFinisher implements Event> { - private final HashSet al = new HashSet<>(); - @Override public boolean call(Object sender, List str) { final GameLauncher obj = (GameLauncher) sender; obj.launchEvent.register((sender1, p) -> { - if (obj.getProfile().getLauncherVisibility() == 0 && !LogWindow.instance.isVisible()) + if (obj.getProfile().getLauncherVisibility() == LauncherVisibility.CLOSE && !LogWindow.instance.isVisible()) System.exit(0); - else if (obj.getProfile().getLauncherVisibility() == 2) + else if (obj.getProfile().getLauncherVisibility() == LauncherVisibility.KEEP) MainFrame.instance.closeMessage(); else { if (LogWindow.instance.isVisible()) LogWindow.instance.setExit(TrueFunction.instance); MainFrame.instance.dispose(); } - Event event = (sender2, t) -> { - processThreadStopped((ProcessThread) sender2, obj, t, false); - return true; - }; - ProcessThread a = new ProcessThread(p, true, true); - a.stopEvent.register((sender3, p1) -> { - if (p1.getExitCode() != 0 && p1.getStdErrLines().size() > 0 && StrUtils.containsOne(p1.getStdErrLines(), Arrays.asList("Could not create the Java Virtual Machine.", - "Error occurred during initialization of VM", - "A fatal exception has occurred. Program will exit."))) MessageBox.Show(C.i18n("launch.cannot_create_jvm")); - processThreadStopped((ProcessThread) sender3, obj, p1, false); + JavaProcessMonitor jpm = new JavaProcessMonitor(p); + jpm.stoppedEvent.register((sender3, t) -> { + if (obj.getProfile().getLauncherVisibility() != LauncherVisibility.KEEP && !LogWindow.instance.isVisible()) + System.exit(0); return true; }); - a.start(); - al.add(a); - a = new ProcessThread(p, false, true); - a.stopEvent.register(event); - a.start(); - al.add(a); - a = new ProcessThread(p, false, false); - a.stopEvent.register(event); - a.start(); - al.add(a); return true; }); obj.launch(str); return true; } - - void processThreadStopped(ProcessThread t, GameLauncher obj, JavaProcess p, boolean forceTermintate) { - al.remove(t); - al.removeAll(CollectionUtils.sortOut(al, t1 -> !t1.isAlive())); - if (al.isEmpty() || forceTermintate) { - for (Thread a : al) a.interrupt(); - al.clear(); - GameLauncher.PROCESS_MANAGER.onProcessStopped(p); - if (obj.getProfile().getLauncherVisibility() != 2 && !LogWindow.instance.isVisible()) - System.exit(0); - } - } } diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/settings/LauncherVisibility.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/settings/LauncherVisibility.java new file mode 100644 index 000000000..1ec1e55bd --- /dev/null +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/settings/LauncherVisibility.java @@ -0,0 +1,27 @@ +/* + * Copyright 2013 huangyuhui + * + * 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 2 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. + */ +package org.jackhuang.hellominecraft.launcher.settings; + +/** + * + * @author huangyuhui + */ +public enum LauncherVisibility { + CLOSE, + HIDE, + KEEP +} diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/settings/Profile.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/settings/Profile.java index 4800a16f1..8ccfa2fe0 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/settings/Profile.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/settings/Profile.java @@ -25,6 +25,7 @@ import org.jackhuang.hellominecraft.utils.StrUtils; import org.jackhuang.hellominecraft.utils.Utils; import org.jackhuang.hellominecraft.launcher.version.MinecraftVersion; import org.jackhuang.hellominecraft.launcher.version.MinecraftVersionManager; +import org.jackhuang.hellominecraft.utils.system.OS; /** * @@ -235,12 +236,12 @@ public final class Profile { Settings.save(); } - public int getLauncherVisibility() { - return launcherVisibility; + public LauncherVisibility getLauncherVisibility() { + return LauncherVisibility.values()[launcherVisibility]; } - public void setLauncherVisibility(int launcherVisibility) { - this.launcherVisibility = launcherVisibility; + public void setLauncherVisibility(LauncherVisibility launcherVisibility) { + this.launcherVisibility = launcherVisibility.ordinal(); Settings.save(); } @@ -306,4 +307,7 @@ public final class Profile { Settings.save(); } + public void checkFormat() { + gameDir = gameDir.replace('/', OS.os().fileSeparator).replace('\\', OS.os().fileSeparator); + } } diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/settings/Settings.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/settings/Settings.java index 4d060276a..8a58f7899 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/settings/Settings.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/settings/Settings.java @@ -38,6 +38,8 @@ import org.jackhuang.hellominecraft.utils.VersionNumber; * @author huangyuhui */ public final class Settings { + + public static final String DEFAULT_PROFILE = "Default"; public static final File settingsFile = new File(IOUtils.currentDir(), "hmcl.json"); //public static final Gson gson = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(Platform.class, new EnumAdapter<>(Platform.values())).create(); @@ -56,8 +58,12 @@ public final class Settings { static { settings = initSettings(); - if (!getVersions().containsKey("Default")) - getVersions().put("Default", new Profile()); + if (!getVersions().containsKey(DEFAULT_PROFILE)) + getVersions().put(DEFAULT_PROFILE, new Profile()); + + for(Profile e : getVersions().values()) { + e.checkFormat(); + } UPDATE_CHECKER = new UpdateChecker(new VersionNumber(Main.firstVer, Main.secondVer, Main.thirdVer), "hmcl", settings.isCheckUpdate(), () -> Main.invokeUpdate()); @@ -126,11 +132,15 @@ public final class Settings { return true; } - public static void delVersion(Profile ver) { - delVersion(ver.getName()); + public static boolean delVersion(Profile ver) { + return delVersion(ver.getName()); } - public static void delVersion(String ver) { - getVersions().remove(ver); + public static boolean delVersion(String ver) { + if (DEFAULT_PROFILE.equals(ver)) { + MessageBox.Show(C.i18n("settings.cannot_remove_default_config")); + return false; + } + return getVersions().remove(ver) != null; } } diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/views/GameSettingsPanel.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/views/GameSettingsPanel.java index 581a4dbe3..e80c3e997 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/views/GameSettingsPanel.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/views/GameSettingsPanel.java @@ -34,6 +34,7 @@ import javax.swing.SwingUtilities; import javax.swing.table.DefaultTableModel; import org.jackhuang.hellominecraft.C; import org.jackhuang.hellominecraft.HMCLog; +import org.jackhuang.hellominecraft.launcher.settings.LauncherVisibility; import org.jackhuang.hellominecraft.launcher.utils.MCUtils; import org.jackhuang.hellominecraft.launcher.utils.assets.IAssetsHandler; import org.jackhuang.hellominecraft.launcher.utils.installers.InstallerVersionList; @@ -912,17 +913,14 @@ btnRefreshLiteLoader.addActionListener(new java.awt.event.ActionListener() { private void btnRemoveProfileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveProfileActionPerformed if (profile == null) return; - if ("Default".equals(profile.getName())) { - MessageBox.Show(C.i18n("settings.cannot_remove_default_config")); - return; - } if (MessageBox.Show(C.i18n("ui.message.sure_remove", profile.getName()), MessageBox.YES_NO_OPTION) == MessageBox.NO_OPTION) return; - cboProfiles.removeItem(profile.getName()); - Settings.delVersion(profile); - profile = Settings.getOneProfile(); - if (profile != null) { - prepare(profile); - loadVersions(); + if(Settings.delVersion(profile)) { + cboProfiles.removeItem(profile.getName()); + profile = Settings.getOneProfile(); + if (profile != null) { + prepare(profile); + loadVersions(); + } } }//GEN-LAST:event_btnRemoveProfileActionPerformed @@ -1094,7 +1092,7 @@ btnRefreshLiteLoader.addActionListener(new java.awt.event.ActionListener() { private void cboLauncherVisibilityFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cboLauncherVisibilityFocusLost if (cboLauncherVisibility.getSelectedIndex() >= 0) - profile.setLauncherVisibility(cboLauncherVisibility.getSelectedIndex()); + profile.setLauncherVisibility(LauncherVisibility.values()[cboLauncherVisibility.getSelectedIndex()]); }//GEN-LAST:event_cboLauncherVisibilityFocusLost private void btnDownloadAllAssetsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDownloadAllAssetsActionPerformed @@ -1197,7 +1195,7 @@ btnRefreshLiteLoader.addActionListener(new java.awt.event.ActionListener() { chkNoJVMArgs.setSelected(profile.isNoJVMArgs()); chkFullscreen.setSelected(profile.isFullscreen()); chkCancelWrapper.setSelected(profile.isCanceledWrapper()); - cboLauncherVisibility.setSelectedIndex(profile.getLauncherVisibility()); + cboLauncherVisibility.setSelectedIndex(profile.getLauncherVisibility().ordinal()); cboGameDirType.setSelectedIndex(profile.getGameDirType().ordinal()); loadVersions(); diff --git a/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/utils/system/JavaProcessMonitor.java b/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/utils/system/JavaProcessMonitor.java new file mode 100644 index 000000000..1a9b1189e --- /dev/null +++ b/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/utils/system/JavaProcessMonitor.java @@ -0,0 +1,75 @@ +/* + * Copyright 2013 huangyuhui + * + * 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 2 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. + */ +package org.jackhuang.hellominecraft.utils.system; + +import java.util.Arrays; +import java.util.HashSet; +import org.jackhuang.hellominecraft.C; +import org.jackhuang.hellominecraft.utils.CollectionUtils; +import org.jackhuang.hellominecraft.utils.Event; +import org.jackhuang.hellominecraft.utils.EventHandler; +import org.jackhuang.hellominecraft.utils.StrUtils; + +/** + * + * @author huangyuhui + */ +public class JavaProcessMonitor { + + private final HashSet al = new HashSet<>(); + public final EventHandler stoppedEvent = new EventHandler<>(this); + private final JavaProcess p; + + public JavaProcessMonitor(JavaProcess p) { + this.p = p; + } + + void start() { + Event event = (sender2, t) -> { + processThreadStopped((ProcessThread) sender2, false); + return true; + }; + ProcessThread a = new ProcessThread(p, true, true); + a.stopEvent.register((sender3, p1) -> { + if (p1.getExitCode() != 0 && p1.getStdErrLines().size() > 0 && StrUtils.containsOne(p1.getStdErrLines(), Arrays.asList("Could not create the Java Virtual Machine.", + "Error occurred during initialization of VM", + "A fatal exception has occurred. Program will exit."))) MessageBox.Show(C.i18n("launch.cannot_create_jvm")); + processThreadStopped((ProcessThread) sender3, false); + return true; + }); + a.start(); + al.add(a); + a = new ProcessThread(p, false, true); + a.stopEvent.register(event); + a.start(); + al.add(a); + a = new ProcessThread(p, false, false); + a.stopEvent.register(event); + a.start(); + al.add(a); + } + + void processThreadStopped(ProcessThread t, boolean forceTermintate) { + al.remove(t); + al.removeAll(CollectionUtils.sortOut(al, t1 -> !t1.isAlive())); + if (al.isEmpty() || forceTermintate) { + for (Thread a : al) a.interrupt(); + al.clear(); + stoppedEvent.execute(p); + } + } +} diff --git a/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/utils/system/OS.java b/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/utils/system/OS.java index f32a4a667..d3f8d682e 100644 --- a/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/utils/system/OS.java +++ b/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/utils/system/OS.java @@ -25,10 +25,16 @@ import org.jackhuang.hellominecraft.HMCLog; */ public enum OS { - LINUX, - WINDOWS, - OSX, - UNKOWN; + LINUX('/'), + WINDOWS('\\'), + OSX('/'), + UNKOWN('/'); + + public final char fileSeparator; + + private OS(char fileSeparator) { + this.fileSeparator = fileSeparator; + } public static OS os() { String str;