Optimize code

This commit is contained in:
huangyuhui
2018-03-02 23:42:38 +08:00
parent 99e85eba37
commit c6ab7878e7
23 changed files with 32 additions and 53 deletions

View File

@@ -1,3 +1,6 @@
import org.apache.tools.ant.filters.ReplaceTokens
import proguard.gradle.ProGuardTask
import java.security.MessageDigest import java.security.MessageDigest
import java.util.jar.JarFile import java.util.jar.JarFile
import java.util.jar.Pack200 import java.util.jar.Pack200
@@ -17,9 +20,7 @@ def versionroot = System.getenv("VERSION_ROOT")
if (versionroot == null) if (versionroot == null)
versionroot = "3.0" versionroot = "3.0"
String mavenGroupId = 'HMCL'
String mavenVersion = versionroot + '.' + buildnumber String mavenVersion = versionroot + '.' + buildnumber
String bundleName = "Hello Minecraft! Launcher"
version = mavenVersion version = mavenVersion
dependencies { dependencies {
@@ -30,7 +31,7 @@ dependencies {
task generateSources(type: Sync) { task generateSources(type: Sync) {
from 'src/main/java' from 'src/main/java'
into "$buildDir/generated-src" into "$buildDir/generated-src"
filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: [ filter(ReplaceTokens, tokens: [
'HELLO_MINECRAFT_LAUNCHER_VERSION_FOR_GRADLE_REPLACING': mavenVersion 'HELLO_MINECRAFT_LAUNCHER_VERSION_FOR_GRADLE_REPLACING': mavenVersion
]) ])
} }
@@ -122,7 +123,7 @@ task makePackGZ(dependsOn: jar) doLast {
fileEx.append sha1Hex fileEx.append sha1Hex
} }
task proguard(type: proguard.gradle.ProGuardTask, dependsOn: jar) { task proguard(type: ProGuardTask, dependsOn: jar) {
ext { ext {
def re = jar.classifier def re = jar.classifier
injar = jar.archivePath injar = jar.archivePath

View File

@@ -17,9 +17,9 @@
*/ */
package org.jackhuang.hmcl; package org.jackhuang.hmcl;
import org.apache.commons.compress.utils.Charsets;
import org.jackhuang.hmcl.util.Logging; import org.jackhuang.hmcl.util.Logging;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.io.File; import java.io.File;
public final class Main { public final class Main {

View File

@@ -42,7 +42,6 @@ public final class HMCLModpackInstallTask extends Task {
private final String name; private final String name;
private final HMCLGameRepository repository; private final HMCLGameRepository repository;
private final Modpack modpack; private final Modpack modpack;
private final File run;
private final List<Task> dependencies = new LinkedList<>(); private final List<Task> dependencies = new LinkedList<>();
private final List<Task> dependents = new LinkedList<>(); private final List<Task> dependents = new LinkedList<>();
@@ -52,8 +51,8 @@ public final class HMCLModpackInstallTask extends Task {
this.zipFile = zipFile; this.zipFile = zipFile;
this.name = name; this.name = name;
this.modpack = modpack; this.modpack = modpack;
this.run = repository.getRunDirectory(name);
File run = repository.getRunDirectory(name);
File json = repository.getModpackConfiguration(name); File json = repository.getModpackConfiguration(name);
if (repository.hasVersion(name) && !json.exists()) if (repository.hasVersion(name) && !json.exists())
throw new IllegalArgumentException("Version " + name + " already exists"); throw new IllegalArgumentException("Version " + name + " already exists");

View File

@@ -63,7 +63,7 @@ public class Settings {
private final Config SETTINGS = initSettings(); private final Config SETTINGS = initSettings();
private Map<String, Account> accounts = new HashMap<>(); private final Map<String, Account> accounts = new HashMap<>();
{ {
loadProxy(); loadProxy();
@@ -473,7 +473,7 @@ public class Settings {
checkProfileMap(); checkProfileMap();
if (!hasProfile(SETTINGS.getSelectedProfile())) { if (!hasProfile(SETTINGS.getSelectedProfile())) {
SETTINGS.setSelectedProfile(getProfileMap().keySet().stream().findFirst().get()); getProfileMap().keySet().stream().findFirst().ifPresent(SETTINGS::setSelectedProfile);
Schedulers.computation().schedule(this::onProfileChanged); Schedulers.computation().schedule(this::onProfileChanged);
} }
return getProfile(SETTINGS.getSelectedProfile()); return getProfile(SETTINGS.getSelectedProfile());

View File

@@ -34,7 +34,6 @@ import org.jackhuang.hmcl.setting.Accounts;
import org.jackhuang.hmcl.setting.Settings; import org.jackhuang.hmcl.setting.Settings;
import org.jackhuang.hmcl.setting.Theme; import org.jackhuang.hmcl.setting.Theme;
import org.jackhuang.hmcl.task.Schedulers; import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.ui.construct.ComponentList; import org.jackhuang.hmcl.ui.construct.ComponentList;
import org.jackhuang.hmcl.ui.wizard.DecoratorPage; import org.jackhuang.hmcl.ui.wizard.DecoratorPage;

View File

@@ -45,7 +45,6 @@ import javafx.scene.shape.Rectangle;
import javafx.stage.Screen; import javafx.stage.Screen;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.stage.StageStyle; import javafx.stage.StageStyle;
import org.jackhuang.hmcl.Launcher;
import org.jackhuang.hmcl.setting.EnumBackgroundImage; import org.jackhuang.hmcl.setting.EnumBackgroundImage;
import org.jackhuang.hmcl.setting.Settings; import org.jackhuang.hmcl.setting.Settings;
import org.jackhuang.hmcl.setting.Theme; import org.jackhuang.hmcl.setting.Theme;

View File

@@ -28,12 +28,10 @@ import org.jackhuang.hmcl.ui.wizard.WizardPage;
import java.util.Map; import java.util.Map;
public final class InstallTypePage extends StackPane implements WizardPage { public final class InstallTypePage extends StackPane implements WizardPage {
private final WizardController controller;
@FXML private JFXListView<Object> list; @FXML private JFXListView<Object> list;
public InstallTypePage(WizardController controller) { public InstallTypePage(WizardController controller) {
this.controller = controller;
FXUtils.loadFXML(this, "/assets/fxml/download/dltype.fxml"); FXUtils.loadFXML(this, "/assets/fxml/download/dltype.fxml");
list.setOnMouseClicked(e -> { list.setOnMouseClicked(e -> {

View File

@@ -37,8 +37,6 @@ import java.util.Map;
public class InstallersPage extends StackPane implements WizardPage { public class InstallersPage extends StackPane implements WizardPage {
private final WizardController controller; private final WizardController controller;
private final GameRepository repository;
private final DownloadProvider downloadProvider;
@FXML @FXML
private VBox list; private VBox list;
@@ -72,8 +70,6 @@ public class InstallersPage extends StackPane implements WizardPage {
public InstallersPage(WizardController controller, GameRepository repository, DownloadProvider downloadProvider) { public InstallersPage(WizardController controller, GameRepository repository, DownloadProvider downloadProvider) {
this.controller = controller; this.controller = controller;
this.repository = repository;
this.downloadProvider = downloadProvider;
FXUtils.loadFXML(this, "/assets/fxml/download/installers.fxml"); FXUtils.loadFXML(this, "/assets/fxml/download/installers.fxml");

View File

@@ -44,11 +44,9 @@ import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public final class VersionsPage extends StackPane implements WizardPage, Refreshable { public final class VersionsPage extends StackPane implements WizardPage, Refreshable {
private final WizardController controller;
private final String gameVersion; private final String gameVersion;
private final DownloadProvider downloadProvider; private final DownloadProvider downloadProvider;
private final String libraryId; private final String libraryId;
private final Runnable callback;
private final String title; private final String title;
@FXML @FXML
@@ -73,12 +71,10 @@ public final class VersionsPage extends StackPane implements WizardPage, Refresh
private TaskExecutor executor; private TaskExecutor executor;
public VersionsPage(WizardController controller, String title, String gameVersion, DownloadProvider downloadProvider, String libraryId, Runnable callback) { public VersionsPage(WizardController controller, String title, String gameVersion, DownloadProvider downloadProvider, String libraryId, Runnable callback) {
this.controller = controller;
this.title = title; this.title = title;
this.gameVersion = gameVersion; this.gameVersion = gameVersion;
this.downloadProvider = downloadProvider; this.downloadProvider = downloadProvider;
this.libraryId = libraryId; this.libraryId = libraryId;
this.callback = callback;
this.versionList = downloadProvider.getVersionListById(libraryId); this.versionList = downloadProvider.getVersionListById(libraryId);
FXUtils.loadFXML(this, "/assets/fxml/download/versions.fxml"); FXUtils.loadFXML(this, "/assets/fxml/download/versions.fxml");

View File

@@ -176,7 +176,7 @@ public class AppDataUpgrader extends IUpgrader {
@Override @Override
public Collection<Task> getDependents() { public Collection<Task> getDependents() {
return Arrays.asList(new FileDownloadTask(downloadLink, tempFile, Proxy.NO_PROXY, hash)); return Collections.singleton(new FileDownloadTask(downloadLink, tempFile, Proxy.NO_PROXY, hash));
} }
@Override @Override

View File

@@ -79,18 +79,16 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler {
THROWABLE_SET.add(stackTrace); THROWABLE_SET.add(stackTrace);
try { try {
StringBuilder builder = new StringBuilder(); String text = "---- Hello Minecraft! Crash Report ----\n" +
builder.append("---- Hello Minecraft! Crash Report ----\n"); " Version: " + Launcher.VERSION + "\n" +
builder.append(" Version: " + Launcher.VERSION + "\n"); " Time: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "\n" +
builder.append(" Time: ").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())).append("\n"); " Thread: " + t.toString() + "\n" +
builder.append(" Thread: ").append(t.toString()).append("\n"); "\n Content: \n " +
builder.append("\n Content: \n "); stackTrace + "\n\n" +
builder.append(stackTrace).append("\n\n"); "-- System Details --\n" +
builder.append("-- System Details --\n"); " Operating System: " + System.getProperty("os.name") + ' ' + OperatingSystem.SYSTEM_VERSION + "\n" +
builder.append(" Operating System: ").append(System.getProperty("os.name")).append(' ').append(OperatingSystem.SYSTEM_VERSION).append("\n"); " Java Version: " + System.getProperty("java.version") + ", " + System.getProperty("java.vendor") + "\n" +
builder.append(" Java Version: ").append(System.getProperty("java.version")).append(", ").append(System.getProperty("java.vendor")).append("\n"); " Java VM Version: " + System.getProperty("java.vm.name") + " (" + System.getProperty("java.vm.info") + "), " + System.getProperty("java.vm.vendor") + "\n";
builder.append(" Java VM Version: ").append(System.getProperty("java.vm.name")).append(" (").append(System.getProperty("java.vm.info")).append("), ").append(System.getProperty("java.vm.vendor")).append("\n");
String text = builder.toString();
Logging.LOG.log(Level.SEVERE, text); Logging.LOG.log(Level.SEVERE, text);

View File

@@ -5,7 +5,6 @@
<?import javafx.geometry.Insets?> <?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?> <?import javafx.scene.control.*?>
<?import javafx.scene.image.Image?> <?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.*?> <?import javafx.scene.layout.*?>
<?import org.jackhuang.hmcl.ui.construct.*?> <?import org.jackhuang.hmcl.ui.construct.*?>
<?import org.jackhuang.hmcl.ui.*?> <?import org.jackhuang.hmcl.ui.*?>

View File

@@ -28,7 +28,7 @@ import org.jackhuang.hmcl.download.optifine.OptiFineVersionList;
*/ */
public class MojangDownloadProvider implements DownloadProvider { public class MojangDownloadProvider implements DownloadProvider {
private boolean isChina; private final boolean isChina;
public MojangDownloadProvider(boolean isChina) { public MojangDownloadProvider(boolean isChina) {
this.isChina = isChina; this.isChina = isChina;

View File

@@ -47,7 +47,7 @@ public final class ForgeInstallTask extends TaskResult<Version> {
private final DefaultDependencyManager dependencyManager; private final DefaultDependencyManager dependencyManager;
private final Version version; private final Version version;
private final File installer = new File("forge-installer.jar").getAbsoluteFile(); private final File installer = new File("forge-installer.jar").getAbsoluteFile();
private ForgeRemoteVersion remote; private final ForgeRemoteVersion remote;
private final List<Task> dependents = new LinkedList<>(); private final List<Task> dependents = new LinkedList<>();
private final List<Task> dependencies = new LinkedList<>(); private final List<Task> dependencies = new LinkedList<>();

View File

@@ -41,7 +41,7 @@ public final class LiteLoaderInstallTask extends TaskResult<Version> {
private final DefaultDependencyManager dependencyManager; private final DefaultDependencyManager dependencyManager;
private final Version version; private final Version version;
private LiteLoaderRemoteVersion remote; private final LiteLoaderRemoteVersion remote;
private final List<Task> dependents = new LinkedList<>(); private final List<Task> dependents = new LinkedList<>();
private final List<Task> dependencies = new LinkedList<>(); private final List<Task> dependencies = new LinkedList<>();

View File

@@ -38,7 +38,7 @@ public final class OptiFineInstallTask extends TaskResult<Version> {
private final DefaultDependencyManager dependencyManager; private final DefaultDependencyManager dependencyManager;
private final Version version; private final Version version;
private OptiFineRemoteVersion remote; private final OptiFineRemoteVersion remote;
private final List<Task> dependents = new LinkedList<>(); private final List<Task> dependents = new LinkedList<>();
private final List<Task> dependencies = new LinkedList<>(); private final List<Task> dependencies = new LinkedList<>();

View File

@@ -30,7 +30,7 @@ public class Event {
/** /**
* The object on which the Event initially occurred. * The object on which the Event initially occurred.
*/ */
protected transient Object source; protected final transient Object source;
/** /**
* Constructs a prototypical Event. * Constructs a prototypical Event.

View File

@@ -39,7 +39,7 @@ public final class LibrariesDownloadInfo {
public LibrariesDownloadInfo(LibraryDownloadInfo artifact, Map<String, LibraryDownloadInfo> classifiers) { public LibrariesDownloadInfo(LibraryDownloadInfo artifact, Map<String, LibraryDownloadInfo> classifiers) {
this.artifact = artifact; this.artifact = artifact;
this.classifiers = new HashMap<>(classifiers); this.classifiers = classifiers == null ? null : new HashMap<>(classifiers);
} }
public LibraryDownloadInfo getArtifact() { public LibraryDownloadInfo getArtifact() {

View File

@@ -45,7 +45,7 @@ public final class StringArgument implements Argument {
@Override @Override
public List<String> toString(Map<String, String> keys, Map<String, Boolean> features) { public List<String> toString(Map<String, String> keys, Map<String, Boolean> features) {
String res = argument; String res = argument;
Pattern pattern = Pattern.compile("\\$\\{(.*?)\\}"); Pattern pattern = Pattern.compile("\\$\\{(.*?)}");
Matcher m = pattern.matcher(argument); Matcher m = pattern.matcher(argument);
while (m.find()) { while (m.find()) {
String entry = m.group(); String entry = m.group();

View File

@@ -44,23 +44,20 @@ import java.util.Objects;
*/ */
public final class MultiMCModpackInstallTask extends Task { public final class MultiMCModpackInstallTask extends Task {
private final DefaultDependencyManager dependencyManager;
private final File zipFile; private final File zipFile;
private final MultiMCInstanceConfiguration manifest; private final MultiMCInstanceConfiguration manifest;
private final String name; private final String name;
private final File run;
private final DefaultGameRepository repository; private final DefaultGameRepository repository;
private final List<Task> dependencies = new LinkedList<>(); private final List<Task> dependencies = new LinkedList<>();
private final List<Task> dependents = new LinkedList<>(); private final List<Task> dependents = new LinkedList<>();
public MultiMCModpackInstallTask(DefaultDependencyManager dependencyManager, File zipFile, MultiMCInstanceConfiguration manifest, String name) { public MultiMCModpackInstallTask(DefaultDependencyManager dependencyManager, File zipFile, MultiMCInstanceConfiguration manifest, String name) {
this.dependencyManager = dependencyManager;
this.zipFile = zipFile; this.zipFile = zipFile;
this.manifest = manifest; this.manifest = manifest;
this.name = name; this.name = name;
this.repository = dependencyManager.getGameRepository(); this.repository = dependencyManager.getGameRepository();
this.run = repository.getRunDirectory(name);
File run = repository.getRunDirectory(name);
File json = repository.getModpackConfiguration(name); File json = repository.getModpackConfiguration(name);
if (repository.hasVersion(name) && !json.exists()) if (repository.hasVersion(name) && !json.exists())
throw new IllegalArgumentException("Version " + name + " already exists."); throw new IllegalArgumentException("Version " + name + " already exists.");

View File

@@ -35,9 +35,7 @@ public final class Schedulers {
private static synchronized ExecutorService getCachedExecutorService() { private static synchronized ExecutorService getCachedExecutorService() {
if (CACHED_EXECUTOR == null) if (CACHED_EXECUTOR == null)
CACHED_EXECUTOR = new ThreadPoolExecutor(0, Integer.MAX_VALUE, CACHED_EXECUTOR = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60, TimeUnit.SECONDS, new SynchronousQueue<>(), runnable -> { 60, TimeUnit.SECONDS, new SynchronousQueue<>(), Executors.defaultThreadFactory());
return Executors.defaultThreadFactory().newThread(runnable);
});
return CACHED_EXECUTOR; return CACHED_EXECUTOR;
} }

View File

@@ -54,8 +54,8 @@ public enum Log4jLevel {
public boolean lessOrEqual(Log4jLevel level) { public boolean lessOrEqual(Log4jLevel level) {
return this.level <= level.level; return this.level <= level.level;
} }
public static final Pattern MINECRAFT_LOGGER = Pattern.compile("\\[(?<timestamp>[0-9:]+)\\] \\[[^/]+/(?<level>[^\\]]+)\\]"); public static final Pattern MINECRAFT_LOGGER = Pattern.compile("\\[(?<timestamp>[0-9:]+)] \\[[^/]+/(?<level>[^]]+)]");
public static final Pattern MINECRAFT_LOGGER_CATEGORY = Pattern.compile("\\[(?<timestamp>[0-9:]+)\\] \\[[^/]+/(?<level>[^\\]]+)\\] \\[(?<category>[^\\]]+)\\]"); public static final Pattern MINECRAFT_LOGGER_CATEGORY = Pattern.compile("\\[(?<timestamp>[0-9:]+)] \\[[^/]+/(?<level>[^]]+)] \\[(?<category>[^]]+)]");
public static final String JAVA_SYMBOL = "([a-zA-Z_$][a-zA-Z\\d_$]*\\.)+[a-zA-Z_$][a-zA-Z\\d_$]*"; public static final String JAVA_SYMBOL = "([a-zA-Z_$][a-zA-Z\\d_$]*\\.)+[a-zA-Z_$][a-zA-Z\\d_$]*";
public static Log4jLevel guessLevel(String line) { public static Log4jLevel guessLevel(String line) {

View File

@@ -20,7 +20,6 @@ package org.jackhuang.hmcl.util;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.text.MessageFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.logging.*; import java.util.logging.*;