optimize the code.

This commit is contained in:
huanghongxun
2015-06-28 18:55:36 +08:00
parent 42ef2fe128
commit 5bc76299af
17 changed files with 74 additions and 83 deletions

View File

@@ -35,7 +35,6 @@ import org.jackhuang.hellominecraft.utils.DoubleOutputStream;
import org.jackhuang.hellominecraft.utils.JdkVersion;
import org.jackhuang.hellominecraft.utils.LauncherPrintStream;
import org.jackhuang.hellominecraft.utils.MessageBox;
import org.jackhuang.hellominecraft.utils.OS;
import org.jackhuang.hellominecraft.utils.Platform;
import org.jackhuang.hellominecraft.utils.Utils;
@@ -106,15 +105,16 @@ public final class Launcher {
return;
}
if (!JdkVersion.isJava64Bit() && OS.getPlatform() == Platform.BIT_32)
if (!JdkVersion.isJava64Bit() && Platform.getPlatform() == Platform.BIT_32)
MessageBox.Show(C.i18n("advice.os64butjdk32"));
Method minecraftMain;
try {
minecraftMain = new URLClassLoader(urls).loadClass(mainClass).getMethod("main", String[].class);
} catch (Throwable t) {
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException t) {
MessageBox.Show(C.i18n("crash.main_class_not_found"));
println("Minecraft main class not found.");
t.printStackTrace();
return;
}

View File

@@ -1,31 +0,0 @@
/*
* 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.launch;
/**
*
* @author hyh
*/
public enum GameLauncherRequest {
ERROR,
DOWNLOAD_ASSETS,
DOWNLOAD_LIBRARIES,
UNZIP_LIBRARIES,
PROCESS_STARTED,
SUCCEED
}

View File

@@ -88,7 +88,7 @@ public abstract class IMinecraftLoader {
res.add("-Xmn128m");
}
if (jv != null && jv.platform == Platform.BIT_32 && OS.getPlatform() == Platform.BIT_64)
if (jv != null && jv.platform == Platform.BIT_32 && Platform.getPlatform() == Platform.BIT_64)
MessageBox.Show(C.i18n("advice.os64butjdk32"));
if (!StrUtils.isBlank(v.getMaxMemory())) {

View File

@@ -87,7 +87,7 @@ public class MinecraftLoader extends IMinecraftLoader {
t = t.replace("${assets_root}", provider.getAssets().getAbsolutePath());
t = t.replace("${auth_access_token}", lr.getAccessToken());
t = t.replace("${user_type}", lr.getUserType());
t = t.replace("${assets_index_name}", version.assets == null ? "legacy" : version.assets);
t = t.replace("${assets_index_name}", version.getAssets());
t = t.replace("${user_properties}", lr.getUserProperties());
t = t.replace("${user_property_map}", lr.getUserPropertyMap());
res.add(t);
@@ -117,7 +117,7 @@ public class MinecraftLoader extends IMinecraftLoader {
File assetsDir = new File(provider.getBaseFolder(), "assets");
File indexDir = new File(assetsDir, "indexes");
File objectDir = new File(assetsDir, "objects");
String assetVersion = version.assets == null ? "legacy" : version.assets;
String assetVersion = version.getAssets();
File indexFile = new File(indexDir, assetVersion + ".json");
File virtualRoot = new File(new File(assetsDir, "virtual"), assetVersion);

View File

@@ -16,10 +16,12 @@
*/
package org.jackhuang.hellominecraft.launcher.utils.assets;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.jackhuang.hellominecraft.HMCLog;
import org.jackhuang.hellominecraft.utils.EventHandler;
import org.jackhuang.hellominecraft.utils.MathUtils;
@@ -27,6 +29,7 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
*
@@ -39,8 +42,8 @@ public class AssetsLoader extends Thread {
NodeList nodes;
public String uri;
ArrayList<Contents> al;
public final EventHandler<Throwable> failedEvent = new EventHandler<Throwable>(this);
public final EventHandler<List<Contents>> successEvent = new EventHandler<List<Contents>>(this);
public final EventHandler<Throwable> failedEvent = new EventHandler<>(this);
public final EventHandler<List<Contents>> successEvent = new EventHandler<>(this);
AssetsLoader(String uri) {
this.uri = uri;
@@ -74,7 +77,7 @@ public class AssetsLoader extends Thread {
@Override
public void run() {
Thread.currentThread().setName("AssetsLoader");
al = new ArrayList<Contents>();
al = new ArrayList<>();
try {
HMCLog.log("AssetsLoader - Download begin.");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
@@ -94,7 +97,7 @@ public class AssetsLoader extends Thread {
HMCLog.log("AssetsLoader - Format end.");
successEvent.execute(al);
} catch (Exception e) {
} catch (ParserConfigurationException | SAXException | IOException e) {
HMCLog.warn("AssetsLoader - Failed", e);
failedEvent.execute(e);
}

View File

@@ -35,8 +35,7 @@ public final class OfflineAuthenticator extends IAuthenticator {
UserProfileProvider result = new UserProfileProvider();
result.setSuccess(StrUtils.isNotBlank(info.username));
result.setUserName(info.username);
String md5 = DigestUtils.md5Hex(info.username);
String uuid = md5.substring(0, 8) + '-' + md5.substring(8, 12) + '-' + md5.substring(12, 16) + '-' + md5.substring(16, 21) + md5.substring(21);
String uuid = getUUIDFromUserName(info.username);
result.setSession(uuid);
result.setUserId(uuid);
result.setAccessToken("${auth_access_token}");
@@ -44,6 +43,11 @@ public final class OfflineAuthenticator extends IAuthenticator {
result.setErrorReason(C.i18n("login.no_Player007"));
return result;
}
public static String getUUIDFromUserName(String str) {
String md5 = DigestUtils.md5Hex(str);
return md5.substring(0, 8) + '-' + md5.substring(8, 12) + '-' + md5.substring(12, 16) + '-' + md5.substring(16, 21) + md5.substring(21);
}
@Override
public String getName() {

View File

@@ -99,7 +99,7 @@ public final class YggdrasilAuthenticator extends IAuthenticator {
}
result.setUserName(username);
result.setSuccess(true);
result.setUserId(UUIDTypeAdapter.fromUUID(selectedProfile.getId()));
result.setUserId(selectedProfile == null ? OfflineAuthenticator.getUUIDFromUserName(username) : UUIDTypeAdapter.fromUUID(selectedProfile.getId()));
result.setUserProperties(new GsonBuilder().registerTypeAdapter(PropertyMap.class, new LegacyPropertyMapSerializer()).create().toJson(ua.getUserProperties()));
result.setUserPropertyMap(new GsonBuilder().registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()).create().toJson(ua.getUserProperties()));
result.setAccessToken(ua.getAuthenticatedToken());

View File

@@ -23,7 +23,7 @@ import org.jackhuang.hellominecraft.launcher.utils.download.DownloadType;
*
* @author hyh
*/
public abstract class IMinecraftLibrary {
public abstract class IMinecraftLibrary implements Cloneable {
public String name;
public IMinecraftLibrary(String name) {
@@ -50,7 +50,7 @@ public abstract class IMinecraftLibrary {
hash = 89 * hash + (this.name != null ? this.name.hashCode() : 0);
return hash;
}
@Override
protected abstract Object clone();
public abstract Object clone();
}

View File

@@ -21,13 +21,14 @@ import java.util.ArrayList;
import java.util.Arrays;
import org.jackhuang.hellominecraft.launcher.utils.download.DownloadType;
import org.jackhuang.hellominecraft.utils.OS;
import org.jackhuang.hellominecraft.utils.Platform;
import org.jackhuang.hellominecraft.utils.StrUtils;
/**
*
* @author hyh
*/
public class MinecraftLibrary extends IMinecraftLibrary implements Cloneable {
public class MinecraftLibrary extends IMinecraftLibrary {
public ArrayList<Rules> rules;
public String url, formatted=null;
@@ -72,10 +73,7 @@ public class MinecraftLibrary extends IMinecraftLibrary implements Cloneable {
break;
}
} else {
if (r.os != null && (StrUtils.isBlank(r.os.name) || r.os.name.equalsIgnoreCase(OS.os().toString()))) {
flag = true;
}
if (r.os == null) {
if (r.os == null || (r.os != null && (StrUtils.isBlank(r.os.name) || r.os.name.equalsIgnoreCase(OS.os().toString())))) {
flag = true;
}
}
@@ -85,16 +83,7 @@ public class MinecraftLibrary extends IMinecraftLibrary implements Cloneable {
}
private String formatArch(String nati) {
String arch = System.getProperty("os.arch");
if (arch.contains("64")) {
arch = "64";
} else {
arch = "32";
}
if (nati == null) {
return "";
}
return nati.replace("${arch}", arch);
return nati == null ? "" : nati.replace("${arch}", Platform.getPlatform().getBit());
}
private String getNative() {

View File

@@ -22,6 +22,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.jackhuang.hellominecraft.launcher.launch.IMinecraftProvider;
import org.jackhuang.hellominecraft.launcher.utils.assets.AssetsIndex;
import org.jackhuang.hellominecraft.launcher.utils.download.DownloadType;
import org.jackhuang.hellominecraft.utils.ArrayUtils;
@@ -117,6 +118,10 @@ public class MinecraftVersion implements Cloneable, Comparable<MinecraftVersion>
public boolean isAllowedToUnpackNatives() {
return true;
}
public String getAssets() {
return assets == null ? AssetsIndex.DEFAULT_ASSET_NAME : assets;
}
@Override
public int compareTo(MinecraftVersion o) {

View File

@@ -126,7 +126,7 @@ public final class MinecraftVersionManager extends IMinecraftProvider {
}
continue;
}
MinecraftVersion mcVersion = null;
MinecraftVersion mcVersion;
try {
mcVersion = gson.fromJson(FileUtils.readFileToString(jsonFile), MinecraftVersion.class);
if (mcVersion == null) throw new RuntimeException("Wrong json format, got null.");
@@ -142,7 +142,7 @@ public final class MinecraftVersionManager extends IMinecraftProvider {
HMCLog.warn("Ignoring: " + dir + ", the json of this Minecraft is malformed.", ex);
continue;
}
}
} else continue;
}
try {
if (!id.equals(mcVersion.id)) {

View File

@@ -1176,7 +1176,7 @@ btnRefreshLiteLoader.addActionListener(new java.awt.event.ActionListener() {
}
}
Profile getProfile() {
final Profile getProfile() {
if (cboProfiles.getSelectedIndex() >= 0)
return Settings.getVersion(cboProfiles.getSelectedItem().toString());
else return null;
@@ -1346,9 +1346,9 @@ btnRefreshLiteLoader.addActionListener(new java.awt.event.ActionListener() {
while (model.getRowCount() > 0)
model.removeRow(0);
String mcver = StrUtils.formatVersion(getMinecraftVersionFormatted());
List<InstallerVersionList.InstallerVersion> versions = list.getVersions(mcver);
if (versions != null) {
for (InstallerVersionList.InstallerVersion v : versions) {
List<InstallerVersionList.InstallerVersion> ver = list.getVersions(mcver);
if (ver != null) {
for (InstallerVersionList.InstallerVersion v : ver) {
Object a = v.selfVersion == null ? "null" : v.selfVersion;
Object b = v.mcVersion == null ? "null" : v.mcVersion;
Object[] row = new Object[]{a, b};
@@ -1356,7 +1356,7 @@ btnRefreshLiteLoader.addActionListener(new java.awt.event.ActionListener() {
}
table.updateUI();
}
return versions;
return ver;
}
}

View File

@@ -16,7 +16,6 @@
*/
package org.jackhuang.hellominecraft.launcher.views;
import com.sun.awt.AWTUtilities;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
@@ -85,7 +84,8 @@ public final class MainFrame extends DraggableFrame {
if (enableShadow)
try {
AWTUtilities.setWindowOpaque(this, false);
//AWTUtilities.setWindowOpaque(this, false);
setBackground(new Color(0,0,0,0));
getRootPane().setBorder(border = new DropShadowBorder(borderColor, 4));
} catch (Throwable ex) {
HMCLog.err("Failed to set window transparent.", ex);

View File

@@ -25,9 +25,6 @@ import org.jackhuang.hellominecraft.launcher.utils.settings.Settings;
*/
public final class NewProfileWindow extends javax.swing.JDialog {
/**
* Creates new form NewProfileWindow
*/
public NewProfileWindow(java.awt.Frame parent) {
super(parent, true);
initComponents();

View File

@@ -54,11 +54,6 @@ public enum OS {
return OS.UNKOWN;
}
public static Platform getPlatform() {
String arch = System.getProperty("os.arch");
return arch.contains("64") ? Platform.BIT_64 : Platform.BIT_32;
}
/**
* @return Free Physical Memory Size (Byte)
*/

View File

@@ -21,7 +21,36 @@ package org.jackhuang.hellominecraft.utils;
* @author huangyuhui
*/
public enum Platform {
UNKNOWN,
BIT_32,
BIT_64
UNKNOWN {
@Override
public String getBit() {
return "unknown";
}
},
BIT_32 {
@Override
public String getBit() {
return "32";
}
},
BIT_64 {
@Override
public String getBit() {
return "64";
}
};
public abstract String getBit();
public static Platform getPlatform() {
String arch = System.getProperty("os.arch");
return arch.contains("64") ? Platform.BIT_64 : Platform.BIT_32;
}
}

View File

@@ -4,5 +4,5 @@ Hello Minecraft! Launcher is a Minecraft launcher.
### Joining
If you really want to join the development, here's some requests.
* The Development Environment is Netbeans 8.0.2 with Gradle Support plugin.
* The project is built in Java 1.8, using the retrolambda backported to Java 1.7. So DO NOT use the libraries of Java 8 like Stream APIs.
* The project is built on Java 1.8, using the retrolambda backported to Java 1.7. So DO NOT use the libraries of Java 8 like Stream APIs.
* DO NOT modify *.gradle.