fix some bugs

This commit is contained in:
huangyuhui
2015-12-30 18:51:38 +08:00
parent 3471b4fec7
commit 5f78f2725a
15 changed files with 113 additions and 83 deletions

View File

@@ -28,6 +28,7 @@ import org.jackhuang.hellominecraft.launcher.Launcher;
import org.jackhuang.hellominecraft.launcher.utils.auth.UserProfileProvider; import org.jackhuang.hellominecraft.launcher.utils.auth.UserProfileProvider;
import org.jackhuang.hellominecraft.launcher.settings.Profile; import org.jackhuang.hellominecraft.launcher.settings.Profile;
import org.jackhuang.hellominecraft.launcher.settings.Settings; import org.jackhuang.hellominecraft.launcher.settings.Settings;
import org.jackhuang.hellominecraft.launcher.version.MinecraftVersion;
import org.jackhuang.hellominecraft.utils.system.JdkVersion; import org.jackhuang.hellominecraft.utils.system.JdkVersion;
import org.jackhuang.hellominecraft.utils.MathUtils; import org.jackhuang.hellominecraft.utils.MathUtils;
import org.jackhuang.hellominecraft.utils.MessageBox; import org.jackhuang.hellominecraft.utils.MessageBox;
@@ -46,6 +47,7 @@ public abstract class AbstractMinecraftLoader implements IMinecraftLoader {
protected UserProfileProvider lr; protected UserProfileProvider lr;
protected File gameDir; protected File gameDir;
protected IMinecraftProvider provider; protected IMinecraftProvider provider;
protected final MinecraftVersion version;
public AbstractMinecraftLoader(Profile ver, IMinecraftProvider provider, UserProfileProvider lr) { public AbstractMinecraftLoader(Profile ver, IMinecraftProvider provider, UserProfileProvider lr) {
this.lr = lr; this.lr = lr;
@@ -53,6 +55,12 @@ public abstract class AbstractMinecraftLoader implements IMinecraftLoader {
v = ver; v = ver;
this.provider = provider; this.provider = provider;
gameDir = v.getCanonicalGameDirFile(); gameDir = v.getCanonicalGameDirFile();
version = provider.getSelectedVersion().resolve(provider);
}
@Override
public MinecraftVersion getMinecraftVersion() {
return version;
} }
public void makeHeadCommand(List<String> res) { public void makeHeadCommand(List<String> res) {
@@ -126,7 +134,7 @@ public abstract class AbstractMinecraftLoader implements IMinecraftLoader {
res.add(a); res.add(a);
} }
res.add("-Djava.library.path=" + provider.getDecompressNativesToLocation().getPath()); res.add("-Djava.library.path=" + provider.getDecompressNativesToLocation(version).getPath());
res.add("-Dfml.ignoreInvalidMinecraftCertificates=true"); res.add("-Dfml.ignoreInvalidMinecraftCertificates=true");
res.add("-Dfml.ignorePatchDiscrepancies=true"); res.add("-Dfml.ignorePatchDiscrepancies=true");
@@ -160,7 +168,8 @@ public abstract class AbstractMinecraftLoader implements IMinecraftLoader {
} }
String serverIp = v.getServerIp(); String serverIp = v.getServerIp();
if (lr.getServer() != null) serverIp = lr.getServer().addr; if (lr.getServer() != null)
serverIp = lr.getServer().addr;
if (StrUtils.isNotBlank(serverIp)) { if (StrUtils.isNotBlank(serverIp)) {
String[] args = serverIp.split(":"); String[] args = serverIp.split(":");
res.add("--server"); res.add("--server");

View File

@@ -31,7 +31,6 @@ import org.jackhuang.hellominecraft.launcher.utils.auth.IAuthenticator;
import org.jackhuang.hellominecraft.launcher.utils.auth.LoginInfo; import org.jackhuang.hellominecraft.launcher.utils.auth.LoginInfo;
import org.jackhuang.hellominecraft.launcher.utils.auth.UserProfileProvider; import org.jackhuang.hellominecraft.launcher.utils.auth.UserProfileProvider;
import org.jackhuang.hellominecraft.launcher.settings.Profile; import org.jackhuang.hellominecraft.launcher.settings.Profile;
import org.jackhuang.hellominecraft.launcher.utils.auth.AuthenticationException;
import org.jackhuang.hellominecraft.utils.system.FileUtils; import org.jackhuang.hellominecraft.utils.system.FileUtils;
import org.jackhuang.hellominecraft.utils.system.IOUtils; import org.jackhuang.hellominecraft.utils.system.IOUtils;
import org.jackhuang.hellominecraft.utils.system.JavaProcess; import org.jackhuang.hellominecraft.utils.system.JavaProcess;
@@ -90,9 +89,13 @@ public class GameLauncher {
HMCLog.err("Failed to get minecraft loader", e); HMCLog.err("Failed to get minecraft loader", e);
failEvent.execute(C.i18n("launch.circular_dependency_versions")); failEvent.execute(C.i18n("launch.circular_dependency_versions"));
return null; return null;
} catch (Exception e) {
failEvent.execute(C.i18n("launch.failed"));
HMCLog.err("Failed to get minecraft loader", e);
return null;
} }
File file = provider.getDecompressNativesToLocation(); File file = provider.getDecompressNativesToLocation(loader.getMinecraftVersion());
if (file != null) if (file != null)
FileUtils.cleanDirectoryQuietly(file); FileUtils.cleanDirectoryQuietly(file);
@@ -103,11 +106,20 @@ public class GameLauncher {
} }
HMCLog.log("Unpacking natives..."); HMCLog.log("Unpacking natives...");
if (!decompressNativesEvent.execute(provider.getDecompressLibraries())) { if (!decompressNativesEvent.execute(provider.getDecompressLibraries(loader.getMinecraftVersion()))) {
failEvent.execute(C.i18n("launch.failed")); failEvent.execute(C.i18n("launch.failed"));
return null; return null;
} }
successEvent.execute(loader.makeLaunchingCommand());
List<String> lst = null;
try {
lst = loader.makeLaunchingCommand();
} catch (Exception e) {
failEvent.execute(C.i18n("launch.failed"));
HMCLog.err("Failed to launch game", e);
return null;
}
successEvent.execute(lst);
return loader; return loader;
} }

View File

@@ -18,6 +18,7 @@
package org.jackhuang.hellominecraft.launcher.launch; package org.jackhuang.hellominecraft.launcher.launch;
import java.util.List; import java.util.List;
import org.jackhuang.hellominecraft.launcher.version.MinecraftVersion;
/** /**
* *
@@ -25,5 +26,7 @@ import java.util.List;
*/ */
public interface IMinecraftLoader { public interface IMinecraftLoader {
MinecraftVersion getMinecraftVersion();
List<String> makeLaunchingCommand(); List<String> makeLaunchingCommand();
} }

View File

@@ -53,7 +53,7 @@ public abstract class IMinecraftProvider {
public abstract File getRunDirectory(String id); public abstract File getRunDirectory(String id);
public File getRunDirectory(String id, String subFolder) { public File getRunDirectory(String id, String subFolder) {
return new File(getRunDirectory(getSelectedMinecraftVersion().id), subFolder); return new File(getRunDirectory(getSelectedVersion().id), subFolder);
} }
public abstract void open(String version, String folder); public abstract void open(String version, String folder);
@@ -71,9 +71,14 @@ public abstract class IMinecraftProvider {
*/ */
public abstract File getResourcePacks(); public abstract File getResourcePacks();
public abstract GameLauncher.DecompressLibraryJob getDecompressLibraries(); /**
*
* @param v should be resolved
* @return libraries of resolved minecraft version v.
*/
public abstract GameLauncher.DecompressLibraryJob getDecompressLibraries(MinecraftVersion v);
public abstract File getDecompressNativesToLocation(); public abstract File getDecompressNativesToLocation(MinecraftVersion v);
/** /**
* @return the Minecraft jar of selected version. * @return the Minecraft jar of selected version.
@@ -151,18 +156,10 @@ public abstract class IMinecraftProvider {
public abstract MinecraftVersion getVersionById(String id); public abstract MinecraftVersion getVersionById(String id);
public MinecraftVersion getSelectedVersion() { public MinecraftVersion getSelectedVersion() {
return profile.getSelectedMinecraftVersion(); String versionName = profile.getSelectedMinecraftVersionName();
} MinecraftVersion v = null;
if (StrUtils.isNotBlank(versionName))
public MinecraftVersion getSelectedMinecraftVersion() { v = getVersionById(versionName);
if (StrUtils.isBlank(profile.getSelectedMinecraftVersionName())) {
MinecraftVersion v = getOneVersion();
if (v == null)
return null;
profile.setSelectedMinecraftVersion(v.id);
return v;
}
MinecraftVersion v = getVersionById(profile.getSelectedMinecraftVersionName());
if (v == null) if (v == null)
v = getOneVersion(); v = getOneVersion();
if (v != null) if (v != null)

View File

@@ -33,7 +33,6 @@ import org.jackhuang.hellominecraft.launcher.utils.assets.IAssetsHandler;
import org.jackhuang.hellominecraft.launcher.utils.download.DownloadType; import org.jackhuang.hellominecraft.launcher.utils.download.DownloadType;
import org.jackhuang.hellominecraft.utils.system.OS; import org.jackhuang.hellominecraft.utils.system.OS;
import org.jackhuang.hellominecraft.launcher.version.MinecraftLibrary; import org.jackhuang.hellominecraft.launcher.version.MinecraftLibrary;
import org.jackhuang.hellominecraft.launcher.version.MinecraftVersion;
import org.jackhuang.hellominecraft.tasks.TaskWindow; import org.jackhuang.hellominecraft.tasks.TaskWindow;
import org.jackhuang.hellominecraft.utils.system.FileUtils; import org.jackhuang.hellominecraft.utils.system.FileUtils;
import org.jackhuang.hellominecraft.utils.MessageBox; import org.jackhuang.hellominecraft.utils.MessageBox;
@@ -44,13 +43,11 @@ import org.jackhuang.hellominecraft.utils.MessageBox;
*/ */
public class MinecraftLoader extends AbstractMinecraftLoader { public class MinecraftLoader extends AbstractMinecraftLoader {
private final MinecraftVersion version;
DownloadType dt; DownloadType dt;
String text; String text;
public MinecraftLoader(Profile ver, IMinecraftProvider provider, UserProfileProvider lr) throws IllegalStateException { public MinecraftLoader(Profile ver, IMinecraftProvider provider, UserProfileProvider lr) throws IllegalStateException {
super(ver, provider, lr); super(ver, provider, lr);
version = ver.getSelectedMinecraftVersion().resolve(provider);
} }
@Override @Override
@@ -61,7 +58,7 @@ public class MinecraftLoader extends AbstractMinecraftLoader {
if (l.allow() && !l.isRequiredToUnzip()) if (l.allow() && !l.isRequiredToUnzip())
library += l.getFilePath(gameDir).getAbsolutePath() + File.pathSeparator; library += l.getFilePath(gameDir).getAbsolutePath() + File.pathSeparator;
} }
library += IOUtils.tryGetCanonicalFilePath(provider.getMinecraftJar()) + File.pathSeparator; library += IOUtils.tryGetCanonicalFilePath(version.getJar(provider.getBaseFolder())) + File.pathSeparator;
library = library.substring(0, library.length() - File.pathSeparator.length()); library = library.substring(0, library.length() - File.pathSeparator.length());
if (v.isCanceledWrapper()) if (v.isCanceledWrapper())
res.add("-cp"); res.add("-cp");

View File

@@ -111,19 +111,7 @@ public final class Profile {
} }
public MinecraftVersion getSelectedMinecraftVersion() { public MinecraftVersion getSelectedMinecraftVersion() {
if (StrUtils.isBlank(selectedMinecraftVersion)) { return getMinecraftProvider().getSelectedVersion();
MinecraftVersion v = getMinecraftProvider().getOneVersion();
if (v == null)
return null;
setSelectedMinecraftVersion(v.id);
return v;
}
MinecraftVersion v = getMinecraftProvider().getVersionById(selectedMinecraftVersion);
if (v == null)
v = getMinecraftProvider().getOneVersion();
if (v != null)
setSelectedMinecraftVersion(v.id);
return v;
} }
public transient final EventHandler<String> selectedVersionChangedEvent = new EventHandler<>(this); public transient final EventHandler<String> selectedVersionChangedEvent = new EventHandler<>(this);

View File

@@ -17,6 +17,8 @@
*/ */
package org.jackhuang.hellominecraft.launcher.utils.installers; package org.jackhuang.hellominecraft.launcher.utils.installers;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import org.jackhuang.hellominecraft.utils.functions.Consumer; import org.jackhuang.hellominecraft.utils.functions.Consumer;
@@ -32,13 +34,39 @@ public abstract class InstallerVersionList implements Consumer<String[]> {
* *
* @param versions Minecraft versions you need to refresh * @param versions Minecraft versions you need to refresh
* *
* @throws java.lang.Exception * @throws java.lang.Exception including network exceptions, IO exceptions.
*/ */
public abstract void refreshList(String[] versions) throws Exception; public abstract void refreshList(String[] versions) throws Exception;
/**
* Installer name.
*
* @return installer name.
*/
public abstract String getName(); public abstract String getName();
public abstract List<InstallerVersion> getVersions(String mcVersion); /**
* Get installers you want.
*
* @param mcVersion the installers to this Minecraft version.
* @return cached result.
*/
protected abstract List<InstallerVersion> getVersionsImpl(String mcVersion);
/**
* Get installers you want, please cache this method's result to save time.
*
* @param mcVersion the installers to this Minecraft version.
* @return a copy of the cached data to prevent
* ConcurrentModificationException.
*/
public List<InstallerVersion> getVersions(String mcVersion) {
List<InstallerVersion> a = getVersionsImpl(mcVersion);
if (a == null)
return null;
else
return new ArrayList<>(a);
}
public static class InstallerVersion implements Comparable<InstallerVersion> { public static class InstallerVersion implements Comparable<InstallerVersion> {

View File

@@ -97,7 +97,7 @@ public class MinecraftForgeVersionList extends InstallerVersionList {
} }
@Override @Override
public List<InstallerVersion> getVersions(String mcVersion) { public List<InstallerVersion> getVersionsImpl(String mcVersion) {
if (versions == null || versionMap == null) if (versions == null || versionMap == null)
return null; return null;
if (StrUtils.isBlank(mcVersion)) if (StrUtils.isBlank(mcVersion))

View File

@@ -83,7 +83,7 @@ public class LiteLoaderVersionList extends InstallerVersionList {
} }
@Override @Override
public List<InstallerVersion> getVersions(String mcVersion) { public List<InstallerVersion> getVersionsImpl(String mcVersion) {
if (versions == null || versionMap == null) if (versions == null || versionMap == null)
return null; return null;
if (StrUtils.isBlank(mcVersion)) if (StrUtils.isBlank(mcVersion))

View File

@@ -83,7 +83,7 @@ public class OptiFineBMCLVersionList extends InstallerVersionList {
} }
@Override @Override
public List<InstallerVersion> getVersions(String mcVersion) { public List<InstallerVersion> getVersionsImpl(String mcVersion) {
if (versions == null || versionMap == null) if (versions == null || versionMap == null)
return null; return null;
if (StrUtils.isBlank(mcVersion)) if (StrUtils.isBlank(mcVersion))

View File

@@ -17,8 +17,8 @@
*/ */
package org.jackhuang.hellominecraft.launcher.utils.installers.optifine.vanilla; package org.jackhuang.hellominecraft.launcher.utils.installers.optifine.vanilla;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.StringBufferInputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@@ -71,7 +71,7 @@ public class OptiFineVersionList extends InstallerVersionList {
try { try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder(); DocumentBuilder db = factory.newDocumentBuilder();
Document doc = db.parse(new StringBufferInputStream(content)); Document doc = db.parse(new ByteArrayInputStream(content.getBytes()));
Element r = doc.getDocumentElement(); Element r = doc.getDocumentElement();
NodeList tables = r.getElementsByTagName("table"); NodeList tables = r.getElementsByTagName("table");
for (int i = 0; i < tables.getLength(); i++) { for (int i = 0; i < tables.getLength(); i++) {
@@ -121,7 +121,7 @@ public class OptiFineVersionList extends InstallerVersionList {
} }
@Override @Override
public List<InstallerVersion> getVersions(String mcVersion) { public List<InstallerVersion> getVersionsImpl(String mcVersion) {
if (versions == null || versionMap == null) if (versions == null || versionMap == null)
return null; return null;
if (StrUtils.isBlank(mcVersion)) if (StrUtils.isBlank(mcVersion))

View File

@@ -21,7 +21,6 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.jackhuang.hellominecraft.HMCLog; import org.jackhuang.hellominecraft.HMCLog;
@@ -56,9 +55,9 @@ public class MinecraftModService extends IMinecraftModService {
@Override @Override
public List<ModInfo> recacheMods() { public List<ModInfo> recacheMods() {
if (mgr.getSelectedMinecraftVersion() == null) if (mgr.getSelectedVersion() == null)
return modCache = new ArrayList<>(); return modCache = new ArrayList<>();
File modsFolder = mgr.getRunDirectory(mgr.getSelectedMinecraftVersion().id, "mods"); File modsFolder = mgr.getRunDirectory(mgr.getSelectedVersion().id, "mods");
ArrayList<ModInfo> mods = new ArrayList<>(); ArrayList<ModInfo> mods = new ArrayList<>();
File[] fs = modsFolder.listFiles(); File[] fs = modsFolder.listFiles();
if (fs != null) if (fs != null)
@@ -84,11 +83,11 @@ public class MinecraftModService extends IMinecraftModService {
@Override @Override
public boolean addMod(File f) { public boolean addMod(File f) {
try { try {
if (mgr.getSelectedMinecraftVersion() == null) if (mgr.getSelectedVersion() == null)
return false; return false;
if (!ModInfo.isFileMod(f)) if (!ModInfo.isFileMod(f))
return false; return false;
File modsFolder = mgr.getRunDirectory(mgr.getSelectedMinecraftVersion().id, "mods"); File modsFolder = mgr.getRunDirectory(mgr.getSelectedVersion().id, "mods");
if (modsFolder == null) if (modsFolder == null)
return false; return false;
modsFolder.mkdirs(); modsFolder.mkdirs();

View File

@@ -209,13 +209,9 @@ public class MinecraftVersionManager extends IMinecraftProvider {
} }
@Override @Override
public GameLauncher.DecompressLibraryJob getDecompressLibraries() { public GameLauncher.DecompressLibraryJob getDecompressLibraries(MinecraftVersion v) {
MinecraftVersion v = getSelectedMinecraftVersion();
if (v == null)
return null;
v = v.resolve(this);
if (v.libraries == null) if (v.libraries == null)
return null; throw new IllegalStateException("Wrong format: minecraft.json");
ArrayList<File> unzippings = new ArrayList<>(); ArrayList<File> unzippings = new ArrayList<>();
ArrayList<String[]> extractRules = new ArrayList<>(); ArrayList<String[]> extractRules = new ArrayList<>();
for (IMinecraftLibrary l : v.libraries) { for (IMinecraftLibrary l : v.libraries) {
@@ -225,18 +221,17 @@ public class MinecraftVersionManager extends IMinecraftProvider {
extractRules.add(l.getDecompressExtractRules()); extractRules.add(l.getDecompressExtractRules());
} }
} }
return new GameLauncher.DecompressLibraryJob(unzippings.toArray(new File[0]), extractRules.toArray(new String[0][]), getDecompressNativesToLocation()); return new GameLauncher.DecompressLibraryJob(unzippings.toArray(new File[0]), extractRules.toArray(new String[0][]), getDecompressNativesToLocation(v));
} }
@Override @Override
public File getDecompressNativesToLocation() { public File getDecompressNativesToLocation(MinecraftVersion v) {
MinecraftVersion v = profile.getSelectedMinecraftVersion();
return v == null ? null : v.getNatives(profile.getCanonicalGameDirFile()); return v == null ? null : v.getNatives(profile.getCanonicalGameDirFile());
} }
@Override @Override
public File getMinecraftJar() { public File getMinecraftJar() {
return profile.getSelectedMinecraftVersion().getJar(baseFolder); return getSelectedVersion().getJar(baseFolder);
} }
@Override @Override

View File

@@ -113,7 +113,7 @@
<Component class="javax.swing.JLabel" name="lblCrash"> <Component class="javax.swing.JLabel" name="lblCrash">
<Properties> <Properties>
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor"> <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
<ResourceString bundle="org/jackhuang/hellominecraft/launcher/I18N.properties" key="ui.label.crashing" replaceFormat="C.I18N.getString(&quot;{key}&quot;)"/> <ResourceString bundle="org/jackhuang/hellominecraft/launcher/I18N.properties" key="ui.label.crashing" replaceFormat="C.i18n(&quot;{key}&quot;)"/>
</Property> </Property>
</Properties> </Properties>
</Component> </Component>

View File

@@ -108,7 +108,7 @@ public class LogWindow extends javax.swing.JFrame {
} }
}); });
lblCrash.setText(C.I18N.getString("ui.label.crashing")); // NOI18N lblCrash.setText(C.i18n("ui.label.crashing")); // NOI18N
btnMCBBS.setText("MCBBS"); btnMCBBS.setText("MCBBS");
btnMCBBS.addActionListener(new java.awt.event.ActionListener() { btnMCBBS.addActionListener(new java.awt.event.ActionListener() {
@@ -200,8 +200,10 @@ public class LogWindow extends javax.swing.JFrame {
private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
boolean flag = false; boolean flag = false;
for (Frame f : Frame.getFrames()) { for (Frame f : Frame.getFrames()) {
if (f == this) continue; if (f == this)
if (f.isVisible()) flag = true; continue;
if (f.isVisible())
flag = true;
} }
if (flag) if (flag)
this.dispose(); this.dispose();