diff --git a/LaunchServer/source/command/hash/DownloadAssetCommand.java b/LaunchServer/source/command/hash/DownloadAssetCommand.java index f9526a3..8d9a1da 100644 --- a/LaunchServer/source/command/hash/DownloadAssetCommand.java +++ b/LaunchServer/source/command/hash/DownloadAssetCommand.java @@ -5,69 +5,19 @@ import launcher.serialize.config.entry.StringConfigEntry; import launchserver.LaunchServer; import launchserver.command.Command; +import launchserver.helpers.UnzipHelper; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collections; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; public final class DownloadAssetCommand extends Command { - private static String ASSET_URL_MASK; - public DownloadAssetCommand(LaunchServer server) { super(server); } - public static void unpack(URL url, Path dir) throws IOException - { - try (ZipInputStream input = IOHelper.newZipInput(url)) - { - for (ZipEntry entry = input.getNextEntry(); entry != null; entry = input.getNextEntry()) - { - if (entry.isDirectory()) - { - continue; // Skip directories - } - - // Unpack entry - String name = entry.getName(); - LogHelper.subInfo("Downloading file: '%s'", name); - IOHelper.transfer(input, dir.resolve(IOHelper.toPath(name))); - } - } - } - - public static void downloadZip(String[] mirrors, String mask, Path dir) { - for (String mirror : mirrors) { - if (downloadZip(mirror + mask, dir)) return; - } - LogHelper.error("Error download %s. All mirrors return error", dir.getFileName().toString()); - } - - public static boolean downloadZip(String link, Path dir) { - URL url = null; - // Нам тут IDEA мозг ебет - try { - url = new URL(link); - } catch (MalformedURLException e) { - e.printStackTrace(); - } - LogHelper.debug("Try download %s", url.toString()); - try { - unpack(url, dir); - } catch (IOException e) { - LogHelper.error("Download %s failed (%s: %s)", url.toString(), e.getClass().getName(), e.getMessage()); - return false; - } - return true; - } - @Override public String getArgsDescription() { @@ -94,9 +44,9 @@ // Download required asset LogHelper.subInfo("Downloading asset, it may take some time"); - ASSET_URL_MASK = String.format("assets/%s.zip", version); String[] mirrors = server.config.mirrors.stream(StringConfigEntry.class).toArray(String[]::new); - downloadZip(mirrors, ASSET_URL_MASK, assetDir); + String assetMask = String.format("assets/%s.zip", version); + UnzipHelper.downloadZip(mirrors, assetMask, assetDir); // Finished server.syncUpdatesDir(Collections.singleton(dirName)); diff --git a/LaunchServer/source/command/hash/DownloadClientCommand.java b/LaunchServer/source/command/hash/DownloadClientCommand.java index f6886c9..251d9df 100644 --- a/LaunchServer/source/command/hash/DownloadClientCommand.java +++ b/LaunchServer/source/command/hash/DownloadClientCommand.java @@ -9,10 +9,10 @@ import launcher.serialize.config.entry.StringConfigEntry; import launchserver.LaunchServer; import launchserver.command.Command; +import launchserver.helpers.UnzipHelper; import java.io.BufferedReader; import java.io.BufferedWriter; -import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; @@ -20,8 +20,6 @@ public final class DownloadClientCommand extends Command { - private static String CLIENT_URL_MASK; - public DownloadClientCommand(LaunchServer server) { super(server); @@ -53,12 +51,12 @@ // Download required client LogHelper.subInfo("Downloading client, it may take some time"); - CLIENT_URL_MASK = String.format("clients/%s.zip", version); String[] mirrors = server.config.mirrors.stream(StringConfigEntry.class).toArray(String[]::new); - DownloadAssetCommand.downloadZip(mirrors, CLIENT_URL_MASK, clientDir); + String clientMask = String.format("clients/%s.zip", version); + UnzipHelper.downloadZip(mirrors, clientMask, clientDir); // Create profile file - LogHelper.subInfo("Creaing profile file: '%s'", dirName); + LogHelper.subInfo("Creating profile file: '%s'", dirName); ClientProfile client; String profilePath; diff --git a/LaunchServer/source/helpers/HTTPRequestHelper.java b/LaunchServer/source/helpers/HTTPRequestHelper.java index a17ff32..c071b25 100644 --- a/LaunchServer/source/helpers/HTTPRequestHelper.java +++ b/LaunchServer/source/helpers/HTTPRequestHelper.java @@ -16,11 +16,7 @@ public static boolean fileExsist(String url) throws IOException { HttpURLConnection request = makeRequest(url, "HEAD"); int responseCode = request.getResponseCode(); - if (responseCode >= 200 && responseCode < 300) { - return true; - } else { - return false; - } + return responseCode >= 200 && responseCode < 300; } public static String getFile(String url) throws IOException { diff --git a/LaunchServer/source/helpers/UnzipHelper.java b/LaunchServer/source/helpers/UnzipHelper.java new file mode 100644 index 0000000..ce67e72 --- /dev/null +++ b/LaunchServer/source/helpers/UnzipHelper.java @@ -0,0 +1,58 @@ +package launchserver.helpers; + +import launcher.helper.IOHelper; +import launcher.helper.LogHelper; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.file.Path; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +public class UnzipHelper { + private static void unpack(URL url, Path dir) throws IOException + { + try (ZipInputStream input = IOHelper.newZipInput(url)) + { + for (ZipEntry entry = input.getNextEntry(); entry != null; entry = input.getNextEntry()) + { + if (entry.isDirectory()) + { + continue; // Skip directories + } + + // Unpack entry + String name = entry.getName(); + LogHelper.subInfo("Downloading file: '%s'", name); + IOHelper.transfer(input, dir.resolve(IOHelper.toPath(name))); + } + } + } + + public static void downloadZip(String[] mirrors, String mask, Path dir) { + for (String mirror : mirrors) { + if (downloadZip(mirror + mask, dir)) return; + } + LogHelper.error("Error download %s. All mirrors return error", dir.getFileName().toString()); + } + + private static boolean downloadZip(String link, Path dir) { + URL url = null; + // Нам тут IDEA мозг ебет + try { + url = new URL(link); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + LogHelper.debug("Try download %s", url.toString()); + try { + unpack(url, dir); + } catch (IOException e) { + LogHelper.error("Download %s failed (%s: %s)", url.toString(), e.getClass().getName(), e.getMessage()); + return false; + } + return true; + } + +} diff --git a/buildnumber b/buildnumber index 96b0eda..c75ac8c 100644 --- a/buildnumber +++ b/buildnumber @@ -1 +1 @@ -520, 14.02.2021 \ No newline at end of file +522, 15.02.2021 \ No newline at end of file