Reconstruct the codes && fix wrong file separator when switch to anther os from Windows.

This commit is contained in:
huanghongxun 2015-07-10 22:53:18 +08:00
parent 244b646225
commit 0be056ac04
7 changed files with 154 additions and 70 deletions

View File

@ -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<List<String>> {
private final HashSet<Thread> al = new HashSet<>();
@Override
public boolean call(Object sender, List<String> 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<JavaProcess> 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);
}
}
}

View File

@ -0,0 +1,27 @@
/*
* Copyright 2013 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 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
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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();

View File

@ -0,0 +1,75 @@
/*
* Copyright 2013 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 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<Thread> al = new HashSet<>();
public final EventHandler<JavaProcess> stoppedEvent = new EventHandler<>(this);
private final JavaProcess p;
public JavaProcessMonitor(JavaProcess p) {
this.p = p;
}
void start() {
Event<JavaProcess> 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);
}
}
}

View File

@ -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;