diff --git a/HMCL/src/core/java/org/jackhuang/hellominecraft/launcher/core/install/forge/ForgeInstaller.java b/HMCL/src/core/java/org/jackhuang/hellominecraft/launcher/core/install/forge/ForgeInstaller.java index 2a7266a740..7d0d76e11a 100755 --- a/HMCL/src/core/java/org/jackhuang/hellominecraft/launcher/core/install/forge/ForgeInstaller.java +++ b/HMCL/src/core/java/org/jackhuang/hellominecraft/launcher/core/install/forge/ForgeInstaller.java @@ -18,7 +18,6 @@ package org.jackhuang.hellominecraft.launcher.core.install.forge; import org.jackhuang.hellominecraft.launcher.core.install.InstallProfile; -import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; @@ -32,6 +31,7 @@ import org.jackhuang.hellominecraft.util.NetUtils; import org.jackhuang.hellominecraft.launcher.core.version.MinecraftLibrary; import org.jackhuang.hellominecraft.util.MessageBox; +import org.jackhuang.hellominecraft.util.system.IOUtils; /** * @@ -79,10 +79,8 @@ public void executeTask() throws Exception { File file = new File(gameDir, "libraries/" + forge.getDownloadInfo().path); if (file.getParentFile().mkdirs()) HMCLog.warn("Failed to make library directory " + file.getParent()); - try (FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos)) { - int c; - while ((c = is.read()) != -1) - bos.write((byte) c); + try (FileOutputStream fos = new FileOutputStream(file)) { + IOUtils.copyStream(is, fos); } mp.version().refreshVersions(); } diff --git a/HMCL/src/core/java/org/jackhuang/hellominecraft/launcher/core/launch/MinecraftLoader.java b/HMCL/src/core/java/org/jackhuang/hellominecraft/launcher/core/launch/MinecraftLoader.java index 2b828da274..d43fe8938b 100755 --- a/HMCL/src/core/java/org/jackhuang/hellominecraft/launcher/core/launch/MinecraftLoader.java +++ b/HMCL/src/core/java/org/jackhuang/hellominecraft/launcher/core/launch/MinecraftLoader.java @@ -45,8 +45,12 @@ public MinecraftLoader(LaunchOptions p, IMinecraftService provider, UserProfileP protected void makeSelf(List res) throws GameException { StringBuilder library = new StringBuilder(""); for (MinecraftLibrary l : version.libraries) - if (l.allow() && !l.isRequiredToUnzip()) - library.append(l.getFilePath(gameDir).getAbsolutePath()).append(File.pathSeparator); + if (l.allow() && !l.isRequiredToUnzip()) { + File f = l.getFilePath(gameDir); + if (f == null) + continue; + library.append(f.getAbsolutePath()).append(File.pathSeparator); + } File f = version.getJar(service.baseDirectory()); if (!f.exists()) throw new GameException("Minecraft jar does not exists"); @@ -55,6 +59,8 @@ protected void makeSelf(List res) throws GameException { res.add(library.toString().substring(0, library.length() - File.pathSeparator.length())); res.add(version.mainClass); + if (version.minecraftArguments == null) + throw new GameException(new NullPointerException("Minecraft Arguments can not be null.")); String[] splitted = StrUtils.tokenize(version.minecraftArguments); String game_assets = assetProvider.apply(version, !options.isNotCheckGame()); diff --git a/HMCL/src/core/java/org/jackhuang/hellominecraft/launcher/core/version/MinecraftLibrary.java b/HMCL/src/core/java/org/jackhuang/hellominecraft/launcher/core/version/MinecraftLibrary.java index 42717940f3..5c6baede3f 100755 --- a/HMCL/src/core/java/org/jackhuang/hellominecraft/launcher/core/version/MinecraftLibrary.java +++ b/HMCL/src/core/java/org/jackhuang/hellominecraft/launcher/core/version/MinecraftLibrary.java @@ -105,7 +105,10 @@ public String formatName() { @Override public File getFilePath(File gameDir) { - return new File(gameDir, "libraries/" + getDownloadInfo().path); + LibraryDownloadInfo info = getDownloadInfo(); + if (info == null) + return null; + return new File(gameDir, "libraries/" + info.path); } @Override diff --git a/HMCSM/build.gradle b/HMCSM/build.gradle index d420f94577..ba1d513c53 100755 --- a/HMCSM/build.gradle +++ b/HMCSM/build.gradle @@ -63,6 +63,12 @@ task macAppCompressed(type: Zip, dependsOn: createApp) { from "$buildDir/macApp" } +macAppBundle { + mainClassName = mainClass + icon = "src/main/icon.icns" + javaProperties.put("apple.laf.useScreenMenuBar", "true") +} + configure(install.repositories.mavenInstaller) { pom.project { groupId = mavenGroupId diff --git a/HMCSM/src/main/icon.icns b/HMCSM/src/main/icon.icns new file mode 100755 index 0000000000..17f903023d Binary files /dev/null and b/HMCSM/src/main/icon.icns differ diff --git a/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/DoubleOutputStream.java b/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/DoubleOutputStream.java index 57ef4da8ae..e11fd40330 100755 --- a/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/DoubleOutputStream.java +++ b/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/DoubleOutputStream.java @@ -51,21 +51,21 @@ public final void write(byte[] arr, int off, int len) throws IOException { } @Override - public final void write(byte[] paramArrayOfByte) throws IOException { + public final void write(byte[] arr) throws IOException { if (this.a != null) - this.a.write(paramArrayOfByte); + this.a.write(arr); if (this.b != null) - this.b.write(paramArrayOfByte); + this.b.write(arr); if (this.c) flush(); } @Override - public final void write(int paramInt) throws IOException { + public final void write(int i) throws IOException { if (this.a != null) - this.a.write(paramInt); + this.a.write(i); if (this.b != null) - this.b.write(paramInt); + this.b.write(i); if (this.c) flush(); } diff --git a/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/NetUtils.java b/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/NetUtils.java index 2f92693757..adc12fd317 100755 --- a/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/NetUtils.java +++ b/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/NetUtils.java @@ -37,13 +37,10 @@ public final class NetUtils { public static byte[] getBytesFromStream(InputStream is) throws IOException { - ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream(); - byte[] arrayOfByte1 = new byte[1024]; - int i; - while ((i = is.read(arrayOfByte1)) >= 0) - localByteArrayOutputStream.write(arrayOfByte1, 0, i); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + IOUtils.copyStream(is, out); is.close(); - return localByteArrayOutputStream.toByteArray(); + return out.toByteArray(); } public static String getStreamContent(InputStream is) throws IOException { @@ -83,12 +80,8 @@ public static String get(URL url, Proxy proxy) throws IOException { public static String post(URL u, Map params) throws IOException { StringBuilder sb = new StringBuilder(); if (params != null) { - for (Map.Entry e : params.entrySet()) { - sb.append(e.getKey()); - sb.append("="); - sb.append(e.getValue()); - sb.append("&"); - } + for (Map.Entry e : params.entrySet()) + sb.append(e.getKey()).append("=").append(e.getValue()).append("&"); sb.deleteCharAt(sb.length() - 1); } return post(u, sb.toString()); @@ -150,9 +143,9 @@ public static URL constantURL(String url) { public static URL concatenateURL(URL url, String query) { try { - if ((url.getQuery() != null) && (url.getQuery().length() > 0)) - return new URL(url.getProtocol(), url.getHost(), url.getPort(), new StringBuilder().append(url.getFile()).append("&").append(query).toString()); - return new URL(url.getProtocol(), url.getHost(), url.getPort(), new StringBuilder().append(url.getFile()).append("?").append(query).toString()); + if (url.getQuery() != null && url.getQuery().length() > 0) + return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "&" + query); + return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "?" + query); } catch (MalformedURLException ex) { throw new IllegalArgumentException("Could not concatenate given URL with GET arguments!", ex); } diff --git a/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/system/Compressor.java b/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/system/Compressor.java index 267206c6fd..9a5a3653a1 100755 --- a/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/system/Compressor.java +++ b/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/system/Compressor.java @@ -109,7 +109,6 @@ private static void zipFile(File source, String basePath, } String pathName;//存相对路径(相对于待压缩的根目录) byte[] buf = new byte[1024]; - int length; for (File file : files) if (file.isDirectory()) { pathName = file.getPath().substring(basePath.length() + 1) @@ -129,8 +128,7 @@ private static void zipFile(File source, String basePath, try (InputStream is = new FileInputStream(file)) { BufferedInputStream bis = new BufferedInputStream(is); zos.putNextEntry(new ZipEntry(pathName)); - while ((length = bis.read(buf)) > 0) - zos.write(buf, 0, length); + IOUtils.copyStream(bis, zos, buf); } } } @@ -154,6 +152,7 @@ public static void unzip(File zipFileName, File extPlace) throws IOException { * @throws java.io.IOException 解压失败或无法写入 */ public static void unzip(File zipFileName, File extPlace, Predicate callback, boolean ignoreExistsFile) throws IOException { + byte[] buf = new byte[1024]; extPlace.mkdirs(); try (ZipInputStream zipFile = new ZipInputStream(new FileInputStream(zipFileName))) { if (zipFileName.exists()) { @@ -184,10 +183,8 @@ public static void unzip(File zipFileName, File extPlace, Predicate call } if (ignoreExistsFile && new File(strtemp).exists()) continue; - try (FileOutputStream fos = new FileOutputStream(strtemp); BufferedOutputStream bos = new BufferedOutputStream(fos)) { - int c; - while ((c = zipFile.read()) != -1) - bos.write((byte) c); + try (FileOutputStream fos = new FileOutputStream(strtemp)) { + IOUtils.copyStream(zipFile, fos, buf); } } } diff --git a/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/system/IOUtils.java b/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/system/IOUtils.java index 124ae8ebe5..86f3e77909 100755 --- a/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/system/IOUtils.java +++ b/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/system/IOUtils.java @@ -288,4 +288,14 @@ public static List readProcessByErrorStream(String[] cmd) throws IOExcep } return lines; } + + public static void copyStream(InputStream input, OutputStream output) throws IOException { + copyStream(input, output, new byte[1024]); + } + + public static void copyStream(InputStream input, OutputStream output, byte[] buf) throws IOException { + int length; + while ((length = input.read(buf)) != -1) + output.write(buf, 0, length); + } } diff --git a/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/system/ZipEngine.java b/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/system/ZipEngine.java index a4bf5d52bd..aeab0c3758 100644 --- a/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/system/ZipEngine.java +++ b/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/system/ZipEngine.java @@ -121,11 +121,9 @@ public void putFile(File file, String pathName) throws IOException { } public void putStream(InputStream is, String pathName) throws IOException { - int length; try (BufferedInputStream bis = new BufferedInputStream(is)) { put(new ZipEntry(pathName)); - while ((length = bis.read(buf)) > 0) - zos.write(buf, 0, length); + IOUtils.copyStream(bis, zos, buf); } } diff --git a/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/tasks/download/HTTPGetTask.java b/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/tasks/download/HTTPGetTask.java index 7aa7c0ec53..9fe25a852f 100755 --- a/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/tasks/download/HTTPGetTask.java +++ b/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/tasks/download/HTTPGetTask.java @@ -62,12 +62,12 @@ public void executeTask() throws Exception { URLConnection conn = new URL(url).openConnection(); InputStream is = conn.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); - int i; - int size = conn.getContentLength(), read = 0; + byte[] buf = new byte[1024]; + int size = conn.getContentLength(), read = 0, len; long lastTime = System.currentTimeMillis(); - while ((i = is.read()) != -1) { - baos.write(i); - ++read; + while ((len = is.read(buf)) != -1) { + baos.write(buf, 0, len); + read += len; long now = System.currentTimeMillis(); if (ppl != null && (now - lastTime) >= 1000) { ppl.setProgress(this, read, size); diff --git a/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/ui/LogWindowOutputStream.java b/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/ui/LogWindowOutputStream.java index 57d884acb9..a400f150c5 100755 --- a/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/ui/LogWindowOutputStream.java +++ b/HMCUtils/src/main/java/org/jackhuang/hellominecraft/util/ui/LogWindowOutputStream.java @@ -18,6 +18,7 @@ package org.jackhuang.hellominecraft.util.ui; import java.io.OutputStream; +import java.util.Objects; import java.util.Timer; import javax.swing.SwingUtilities; import org.jackhuang.hellominecraft.util.logging.Level; @@ -34,24 +35,26 @@ public class LogWindowOutputStream extends OutputStream { private final Level sas; public LogWindowOutputStream(LogWindow logWindow, Level l) { + Objects.nonNull(logWindow); + Objects.nonNull(l); txt = logWindow; - this.sas = l; + sas = l; } @Override - public final void write(byte[] paramArrayOfByte) { - write(paramArrayOfByte, 0, paramArrayOfByte.length); + public final void write(byte[] arr) { + write(arr, 0, arr.length); } @Override - public final void write(byte[] paramArrayOfByte, int off, int len) { - append(new String(paramArrayOfByte, off, len)); + public final void write(byte[] arr, int off, int len) { + append(new String(arr, off, len)); } - private void append(final String newString) { + private void append(final String str) { try { SwingUtilities.invokeLater(() -> { - txt.log(newString, Level.guessLevel(newString, sas)); + txt.log(str, Level.guessLevel(str, sas)); }); } catch (Throwable e) { e.printStackTrace(); @@ -59,8 +62,8 @@ private void append(final String newString) { } @Override - public final void write(int paramInt) { - append(new String(new byte[] { (byte) paramInt })); + public final void write(int i) { + append(new String(new byte[] { (byte) i })); } public static void dispose() {