used findbugs

This commit is contained in:
huangyuhui
2016-02-05 13:10:22 +08:00
parent cd972f5945
commit d4dccd1391
39 changed files with 388 additions and 427 deletions

View File

@@ -19,6 +19,7 @@ package org.jackhuang.hellominecraft.launcher;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
@@ -89,14 +90,16 @@ public final class Launcher {
});
try {
File logFile = new File("hmclmc.log");
if (!logFile.exists())
logFile.createNewFile();
FileOutputStream tc = new FileOutputStream(logFile);
DoubleOutputStream out = new DoubleOutputStream(tc, System.out);
System.setOut(new LauncherPrintStream(out));
DoubleOutputStream err = new DoubleOutputStream(tc, System.err);
System.setErr(new LauncherPrintStream(err));
} catch (Exception e) {
if (!logFile.exists() && !logFile.createNewFile())
println("Failed to create log file");
else {
FileOutputStream tc = new FileOutputStream(logFile);
DoubleOutputStream out = new DoubleOutputStream(tc, System.out);
System.setOut(new LauncherPrintStream(out));
DoubleOutputStream err = new DoubleOutputStream(tc, System.err);
System.setErr(new LauncherPrintStream(err));
}
} catch (IOException e) {
println("Failed to add log file appender.");
e.printStackTrace();
}

View File

@@ -22,6 +22,7 @@ import com.google.gson.reflect.TypeToken;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@@ -56,7 +57,7 @@ public class ModInfo implements Comparable<ModInfo> {
@Override
public int compareTo(ModInfo o) {
return getFileName().toLowerCase().compareTo(o.getFileName().toLowerCase());
return getFileName().compareToIgnoreCase(o.getFileName());
}
public String getName() {
@@ -102,12 +103,13 @@ public class ModInfo implements Comparable<ModInfo> {
return name.endsWith(".zip") || name.endsWith(".jar") || name.endsWith("litemod");
}
private static final Type TYPE = new TypeToken<List<ModInfo>>() {
}.getType();
private static ModInfo getForgeModInfo(File f, ZipFile jar, ZipEntry entry) throws IOException {
ModInfo i = new ModInfo();
i.location = f;
List<ModInfo> m = C.GSON.fromJson(new InputStreamReader(jar.getInputStream(entry)),
new TypeToken<List<ModInfo>>() {
}.getType());
List<ModInfo> m = C.GSON.fromJson(new InputStreamReader(jar.getInputStream(entry)), TYPE);
if (m != null && m.size() > 0) {
i = m.get(0);
i.location = f;
@@ -116,8 +118,7 @@ public class ModInfo implements Comparable<ModInfo> {
}
private static ModInfo getLiteLoaderModInfo(File f, ZipFile jar, ZipEntry entry) throws IOException {
ModInfo m = C.GSON.fromJson(new InputStreamReader(jar.getInputStream(entry)),
ModInfo.class);
ModInfo m = C.GSON.fromJson(new InputStreamReader(jar.getInputStream(entry)), ModInfo.class);
if (m == null)
m = new ModInfo();
m.location = f;

View File

@@ -29,8 +29,8 @@ import java.util.Set;
public class AssetsIndex {
public static final String DEFAULT_ASSET_NAME = "legacy";
private Map<String, AssetsObject> objects;
private boolean virtual;
public Map<String, AssetsObject> objects;
public boolean virtual;
public AssetsIndex() {
this.objects = new LinkedHashMap();

View File

@@ -97,7 +97,7 @@ public abstract class IAssetsHandler {
@Override
public void executeTask() {
if (assetsDownloadURLs == null)
if (assetsDownloadURLs == null || assetsLocalNames == null)
throw new IllegalStateException(C.i18n("assets.not_refreshed"));
int max = assetsDownloadURLs.size();
al = new ArrayList<>();

View File

@@ -25,6 +25,7 @@ import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import java.util.Objects;
import java.util.UUID;
import org.jackhuang.hellominecraft.util.StrUtils;
@@ -46,9 +47,16 @@ public class GameProfile {
return id != null && StrUtils.isNotBlank(name);
}
@Override
public int hashCode() {
int hash = 7;
hash = 29 * hash + Objects.hashCode(this.id);
hash = 29 * hash + Objects.hashCode(this.name);
return hash;
}
@Override
public boolean equals(Object o) {
System.out.println("hello?");
if (this == o)
return true;
if (o == null || getClass() != o.getClass())

View File

@@ -51,6 +51,7 @@ public class PropertyMap extends HashMap<String, Property> {
for (Map<String, String> propertyMap : list) {
String name = propertyMap.get("name");
String value = propertyMap.get("value");
put(name, new Property(name, value));
}
} catch (Throwable t) {
HMCLog.warn("Failed to deserialize properties", t);
@@ -76,6 +77,7 @@ public class PropertyMap extends HashMap<String, Property> {
JsonObject object = (JsonObject) element;
String name = object.getAsJsonPrimitive("name").getAsString();
String value = object.getAsJsonPrimitive("value").getAsString();
result.put(name, new Property(name, value));
}
return result;
@@ -101,10 +103,10 @@ public class PropertyMap extends HashMap<String, Property> {
@Override
public JsonElement serialize(PropertyMap src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject result = new JsonObject();
for (String key : src.keySet()) {
for (PropertyMap.Entry<String, Property> entry : src.entrySet()) {
JsonArray values = new JsonArray();
values.add(new JsonPrimitive(src.get(key).value));
result.add(key, values);
values.add(new JsonPrimitive(entry.getValue().value));
result.add(entry.getKey(), values);
}
return result;
}

View File

@@ -37,7 +37,7 @@ import org.jackhuang.hellominecraft.util.StrUtils;
*/
public class LiteLoaderVersionList extends InstallerVersionList {
private static LiteLoaderVersionList instance;
private static volatile LiteLoaderVersionList instance = null;
public static LiteLoaderVersionList getInstance() {
if (instance == null)

View File

@@ -18,6 +18,7 @@
package org.jackhuang.hellominecraft.launcher.core.install.optifine.bmcl;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -50,6 +51,9 @@ public class OptiFineBMCLVersionList extends InstallerVersionList {
public Map<String, List<InstallerVersion>> versionMap;
public List<InstallerVersion> versions;
private static final Type TYPE = new TypeToken<ArrayList<OptiFineVersion>>() {
}.getType();
@Override
public void refreshList(String[] needed) throws Exception {
String s = NetUtils.get("http://bmclapi.bangbang93.com/optifine/versionlist");
@@ -59,8 +63,7 @@ public class OptiFineBMCLVersionList extends InstallerVersionList {
if (s == null)
return;
root = C.GSON.fromJson(s, new TypeToken<ArrayList<OptiFineVersion>>() {
}.getType());
root = C.GSON.fromJson(s, TYPE);
for (OptiFineVersion v : root) {
v.mirror = v.mirror.replace("http://optifine.net/http://optifine.net/", "http://optifine.net/");

View File

@@ -118,7 +118,7 @@ public class GameLauncher {
if (StrUtils.isNotBlank(options.getPrecalledCommand())) {
Process p = Runtime.getRuntime().exec(options.getPrecalledCommand());
try {
if (p != null && p.isAlive())
if (p.isAlive())
p.waitFor();
} catch (InterruptedException ex) {
HMCLog.warn("Failed to invoke precalled command", ex);

View File

@@ -144,32 +144,26 @@ public final class ModpackManager {
* &lt; String, Boolean, Boolean &gt;: Folder/File name, Is Directory,
* Return 0: non blocked, 1: non shown, 2: suggested, checked.
*/
public static final BiFunction<String, Boolean, Integer> MODPACK_PREDICATE = new BiFunction<String, Boolean, Integer>() {
@Override
public Integer apply(String x, Boolean y) {
if (ModpackManager.MODPACK_BLACK_LIST_PREDICATE.apply(x, y))
return 1;
if (ModpackManager.MODPACK_SUGGESTED_BLACK_LIST_PREDICATE.apply(x, y))
return 2;
return 0;
}
public static final BiFunction<String, Boolean, Integer> MODPACK_PREDICATE = (String x, Boolean y) -> {
if (ModpackManager.MODPACK_BLACK_LIST_PREDICATE.apply(x, y))
return 1;
if (ModpackManager.MODPACK_SUGGESTED_BLACK_LIST_PREDICATE.apply(x, y))
return 2;
return 0;
};
public static final BiFunction<String, Boolean, Boolean> MODPACK_BLACK_LIST_PREDICATE = modpackPredicateMaker(MODPACK_BLACK_LIST);
public static final BiFunction<String, Boolean, Boolean> MODPACK_SUGGESTED_BLACK_LIST_PREDICATE = modpackPredicateMaker(MODPACK_SUGGESTED_BLACK_LIST);
private static BiFunction<String, Boolean, Boolean> modpackPredicateMaker(final List<String> l) {
return new BiFunction<String, Boolean, Boolean>() {
@Override
public Boolean apply(String x, Boolean y) {
for (String s : l)
if (y) {
if (x.startsWith(s + "/"))
return true;
} else if (x.equals(s))
return (String x, Boolean y) -> {
for (String s : l)
if (y) {
if (x.startsWith(s + "/"))
return true;
return false;
}
} else if (x.equals(s))
return true;
return false;
};
}
@@ -193,17 +187,14 @@ public final class ModpackManager {
ZipEngine zip = null;
try {
zip = new ZipEngine(output);
zip.putDirectory(provider.getRunDirectory(version), new BiFunction<String, Boolean, String>() {
@Override
public String apply(String x, Boolean y) {
for (String s : b)
if (y) {
if (x.startsWith(s + "/"))
return null;
} else if (x.equals(s))
zip.putDirectory(provider.getRunDirectory(version), (String x, Boolean y) -> {
for (String s : b)
if (y) {
if (x.startsWith(s + "/"))
return null;
return "minecraft/" + x;
}
} else if (x.equals(s))
return null;
return "minecraft/" + x;
});
MinecraftVersion mv = provider.getVersionById(version).resolve(provider);

View File

@@ -59,5 +59,7 @@ public abstract class IMinecraftLibrary implements Cloneable {
}
@Override
public abstract Object clone();
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}

View File

@@ -52,8 +52,12 @@ public class MinecraftLibrary extends IMinecraftLibrary {
}
@Override
public Object clone() {
return new MinecraftLibrary(rules, url, checksums, natives, name, extract);
public Object clone() throws CloneNotSupportedException {
MinecraftLibrary ml = (MinecraftLibrary) super.clone();
ml.extract = (Extract) ml.extract.clone();
ml.natives = (Natives) ml.natives.clone();
ml.rules = (ArrayList<Rules>) ml.rules.clone();
return ml;
}
/**
@@ -69,11 +73,11 @@ public class MinecraftLibrary extends IMinecraftLibrary {
else
for (Rules r : rules)
if (r.action.equals("disallow")) {
if (r.os != null && (StrUtils.isBlank(r.os.name) || r.os.name.equalsIgnoreCase(OS.os().toString()))) {
if (r.os != null && (StrUtils.isBlank(r.os.getName()) || r.os.getName().equalsIgnoreCase(OS.os().toString()))) {
flag = false;
break;
}
} else if (r.os == null || (r.os != null && (StrUtils.isBlank(r.os.name) || r.os.name.equalsIgnoreCase(OS.os().toString()))))
} else if (r.os == null || (r.os != null && (StrUtils.isBlank(r.os.getName()) || r.os.getName().equalsIgnoreCase(OS.os().toString()))))
flag = true;
return flag;
}
@@ -83,13 +87,14 @@ public class MinecraftLibrary extends IMinecraftLibrary {
}
private String getNative() {
OS os = OS.os();
if (os == OS.WINDOWS)
switch (OS.os()) {
case WINDOWS:
return formatArch(natives.windows);
else if (os == OS.OSX)
case OSX:
return formatArch(natives.osx);
else
default:
return formatArch(natives.linux);
}
}
@Override

View File

@@ -26,11 +26,7 @@ public class Natives implements Cloneable {
public String windows, osx, linux;
@Override
protected Object clone() {
Natives n = new Natives();
n.windows = windows;
n.osx = osx;
n.linux = linux;
return n;
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}

View File

@@ -23,5 +23,21 @@ package org.jackhuang.hellominecraft.launcher.core.version;
*/
public class OS {
public String version, name;
private String version, name;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@@ -19,7 +19,6 @@ package org.jackhuang.hellominecraft.launcher.ui;
import javax.swing.table.DefaultTableModel;
import org.jackhuang.hellominecraft.util.C;
import org.jackhuang.hellominecraft.util.logging.HMCLog;
import org.jackhuang.hellominecraft.util.MessageBox;
import org.jackhuang.hellominecraft.util.StrUtils;
import org.jackhuang.hellominecraft.util.ui.SwingUtils;

View File

@@ -22,7 +22,6 @@ import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.swing.JFileChooser;
@@ -45,7 +44,6 @@ import org.jackhuang.hellominecraft.lookandfeel.GraphicsUtils;
import org.jackhuang.hellominecraft.util.Event;
import org.jackhuang.hellominecraft.lookandfeel.comp.ConstomButton;
import org.jackhuang.hellominecraft.util.system.FileUtils;
import org.jackhuang.hellominecraft.util.system.IOUtils;
import org.jackhuang.hellominecraft.util.system.JavaProcessMonitor;
import org.jackhuang.hellominecraft.util.tasks.TaskWindow;
import org.jackhuang.hellominecraft.util.ui.LogWindow;
@@ -86,9 +84,9 @@ public class MainPagePanel extends AnimatedPanel {
pnlMore.setBackground(GraphicsUtils.getWebColorWithAlpha("FFFFFF7F"));
pnlMore.setOpaque(true);
prepareAuths();
Settings.getInstance().authChangedEvent.register(onAuthChanged);
prepareAuths();
}
/**
@@ -464,7 +462,7 @@ public class MainPagePanel extends AnimatedPanel {
void runGame() {
MainFrame.INSTANCE.showMessage(C.i18n("ui.message.launching"));
getProfile().launcher().genLaunchCode(value -> {
value.successEvent.register(new LaunchFinisher());
value.successEvent.register(launchFinisher);
value.successEvent.register(this::prepareAuths);
}, this::failed, txtPassword.getText());
}
@@ -472,7 +470,7 @@ public class MainPagePanel extends AnimatedPanel {
void makeLaunchScript() {
MainFrame.INSTANCE.showMessage(C.i18n("ui.message.launching"));
getProfile().launcher().genLaunchCode(value -> {
value.successEvent.register(new LaunchScriptFinisher());
value.successEvent.register(launchScriptFinisher);
value.successEvent.register(this::prepareAuths);
}, this::failed, txtPassword.getText());
}
@@ -483,68 +481,59 @@ public class MainPagePanel extends AnimatedPanel {
MainFrame.INSTANCE.closeMessage();
}
public class LaunchFinisher implements Event<List<String>> {
@Override
public boolean call(Object sender, List<String> str) {
final GameLauncher obj = (GameLauncher) sender;
obj.launchEvent.register(p -> {
if ((LauncherVisibility) obj.getTag() == LauncherVisibility.CLOSE && !LogWindow.INSTANCE.isVisible()) {
final Event<List<String>> launchFinisher = (sender, str) -> {
final GameLauncher obj = (GameLauncher) sender;
obj.launchEvent.register(p -> {
if ((LauncherVisibility) obj.getTag() == LauncherVisibility.CLOSE && !LogWindow.INSTANCE.isVisible()) {
HMCLog.log("Without the option of keeping the launcher visible, this application will exit and will NOT catch game logs, but you can turn on \"Debug Mode\".");
System.exit(0);
} else if ((LauncherVisibility) obj.getTag() == LauncherVisibility.KEEP)
MainFrame.INSTANCE.closeMessage();
else {
if (LogWindow.INSTANCE.isVisible())
LogWindow.INSTANCE.setExit(() -> true);
MainFrame.INSTANCE.dispose();
}
JavaProcessMonitor jpm = new JavaProcessMonitor(p);
jpm.applicationExitedAbnormallyEvent.register(t -> {
HMCLog.err("The game exited abnormally, exit code: " + t);
MessageBox.Show(C.i18n("launch.exited_abnormally") + ", exit code: " + t);
});
jpm.jvmLaunchFailedEvent.register(t -> {
HMCLog.err("Cannot create jvm, exit code: " + t);
MessageBox.Show(C.i18n("launch.cannot_create_jvm") + ", exit code: " + t);
});
jpm.stoppedEvent.register(() -> {
if ((LauncherVisibility) obj.getTag() != LauncherVisibility.KEEP && !LogWindow.INSTANCE.isVisible()) {
HMCLog.log("Without the option of keeping the launcher visible, this application will exit and will NOT catch game logs, but you can turn on \"Debug Mode\".");
System.exit(0);
} else if ((LauncherVisibility) obj.getTag() == LauncherVisibility.KEEP)
MainFrame.INSTANCE.closeMessage();
else {
if (LogWindow.INSTANCE.isVisible())
LogWindow.INSTANCE.setExit(() -> true);
MainFrame.INSTANCE.dispose();
}
JavaProcessMonitor jpm = new JavaProcessMonitor(p);
jpm.applicationExitedAbnormallyEvent.register(t -> {
HMCLog.err("The game exited abnormally, exit code: " + t);
MessageBox.Show(C.i18n("launch.exited_abnormally") + ", exit code: " + t);
});
jpm.jvmLaunchFailedEvent.register(t -> {
HMCLog.err("Cannot create jvm, exit code: " + t);
MessageBox.Show(C.i18n("launch.cannot_create_jvm") + ", exit code: " + t);
});
jpm.stoppedEvent.register(() -> {
if ((LauncherVisibility) obj.getTag() != LauncherVisibility.KEEP && !LogWindow.INSTANCE.isVisible()) {
HMCLog.log("Without the option of keeping the launcher visible, this application will exit and will NOT catch game logs, but you can turn on \"Debug Mode\".");
System.exit(0);
}
});
jpm.start();
});
try {
obj.launch(str);
} catch (IOException e) {
failed(C.i18n("launch.failed_creating_process") + "\n" + e.getMessage());
HMCLog.err("Failed to launch when creating a new process.", e);
}
return true;
jpm.start();
});
try {
obj.launch(str);
} catch (IOException e) {
failed(C.i18n("launch.failed_creating_process") + "\n" + e.getMessage());
HMCLog.err("Failed to launch when creating a new process.", e);
}
}
return true;
};
public class LaunchScriptFinisher implements Event<List<String>> {
@Override
public boolean call(Object sender, List str) {
boolean flag = false;
try {
String s = JOptionPane.showInputDialog(C.i18n("mainwindow.enter_script_name"));
if (s != null)
MessageBox.Show(C.i18n("mainwindow.make_launch_succeed") + " " + ((GameLauncher) sender).makeLauncher(s, str).getAbsolutePath());
flag = true;
} catch (IOException ex) {
MessageBox.Show(C.i18n("mainwindow.make_launch_script_failed"));
HMCLog.err("Failed to create script file.", ex);
}
MainFrame.INSTANCE.closeMessage();
return flag;
final Event<List<String>> launchScriptFinisher = (sender, str) -> {
boolean flag = false;
try {
String s = JOptionPane.showInputDialog(C.i18n("mainwindow.enter_script_name"));
if (s != null)
MessageBox.Show(C.i18n("mainwindow.make_launch_succeed") + " " + ((GameLauncher) sender).makeLauncher(s, str).getAbsolutePath());
flag = true;
} catch (IOException ex) {
MessageBox.Show(C.i18n("mainwindow.make_launch_script_failed"));
HMCLog.err("Failed to create script file.", ex);
}
}
MainFrame.INSTANCE.closeMessage();
return flag;
};
public Profile getProfile() {
return Settings.getProfile((String) cboProfiles.getSelectedItem());

View File

@@ -130,8 +130,12 @@ public final class NewProfileWindow extends javax.swing.JDialog {
Profile newProfile = new Profile(Settings.getProfile(cboProfiles.getSelectedItem().toString()));
newProfile.setName(txtNewProfileName.getText());
Settings.trySetProfile(newProfile);
break;
case 27:
this.dispose();
break;
default:
break;
}
}//GEN-LAST:event_txtNewProfileNameKeyTyped

View File

@@ -84,7 +84,7 @@ public class AppDataUpgrader extends IUpgrader {
}
}
} catch (Throwable t) {
t.printStackTrace();
HMCLog.err("Failed to execute newer version application", t);
}
return false;
}

View File

@@ -18,8 +18,6 @@
package org.jackhuang.hellominecraft.launcher.util.upgrade;
import org.jackhuang.hellominecraft.util.Event;
import org.jackhuang.hellominecraft.util.Event;
import org.jackhuang.hellominecraft.util.VersionNumber;
import org.jackhuang.hellominecraft.util.VersionNumber;
/**

View File

@@ -19,8 +19,6 @@ package org.jackhuang.hellominecraft.launcher.util.upgrade;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jackhuang.hellominecraft.util.logging.HMCLog;
import org.jackhuang.hellominecraft.util.tasks.TaskWindow;
import org.jackhuang.hellominecraft.util.tasks.download.FileDownloadTask;