diff --git a/LaunchServer/source/LaunchServer.java b/LaunchServer/source/LaunchServer.java index 53f5d2a..31d5be2 100644 --- a/LaunchServer/source/LaunchServer.java +++ b/LaunchServer/source/LaunchServer.java @@ -190,7 +190,8 @@ LogHelper.info("Check updates from KeeperJerry..."); try { - String file = HTTPRequestHelper.getFile("https://launcher-sashok724.keeperjerry.ru/versions.json"); + URL url = new URL("https://launcher-sashok724.keeperjerry.ru/versions.json"); + String file = HTTPRequestHelper.getFile(url); JsonObject object = Json.parse(file).asObject(); String version = object.get("version").asString(); String date = object.get("date").asString(); diff --git a/LaunchServer/source/command/hash/DownloadAssetCommand.java b/LaunchServer/source/command/hash/DownloadAssetCommand.java index 8d9a1da..b37c278 100644 --- a/LaunchServer/source/command/hash/DownloadAssetCommand.java +++ b/LaunchServer/source/command/hash/DownloadAssetCommand.java @@ -5,8 +5,10 @@ import launcher.serialize.config.entry.StringConfigEntry; import launchserver.LaunchServer; import launchserver.command.Command; +import launchserver.helpers.HTTPRequestHelper; import launchserver.helpers.UnzipHelper; +import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collections; @@ -37,19 +39,27 @@ String version = args[0]; String dirName = IOHelper.verifyFileName(args[1]); Path assetDir = server.updatesDir.resolve(dirName); - - // Create asset dir - LogHelper.subInfo("Creating asset dir: '%s'", dirName); - Files.createDirectory(assetDir); - - // Download required asset - LogHelper.subInfo("Downloading asset, it may take some time"); String[] mirrors = server.config.mirrors.stream(StringConfigEntry.class).toArray(String[]::new); String assetMask = String.format("assets/%s.zip", version); - UnzipHelper.downloadZip(mirrors, assetMask, assetDir); - // Finished - server.syncUpdatesDir(Collections.singleton(dirName)); - LogHelper.subInfo("Asset successfully downloaded: '%s'", dirName); + for (String mirror : mirrors) { + URL assetUrl = new URL(mirror + assetMask); + + if (!HTTPRequestHelper.fileExist(assetUrl)) continue; + + // Create asset dir + LogHelper.subInfo("Asset found. Creating asset dir: '%s'", dirName); + Files.createDirectory(assetDir); + + // Download required asset + LogHelper.subInfo("Downloading asset, it may take some time"); + if(!UnzipHelper.downloadZip(assetUrl, assetDir)) return; + + // Finished + server.syncUpdatesDir(Collections.singleton(dirName)); + LogHelper.subInfo("Asset successfully downloaded: '%s'", dirName); + return; + } + LogHelper.error("Error download %s. All mirrors return error", dirName); } } diff --git a/LaunchServer/source/command/hash/DownloadClientCommand.java b/LaunchServer/source/command/hash/DownloadClientCommand.java index 251d9df..cdfc21a 100644 --- a/LaunchServer/source/command/hash/DownloadClientCommand.java +++ b/LaunchServer/source/command/hash/DownloadClientCommand.java @@ -1,7 +1,6 @@ package launchserver.command.hash; import launcher.client.ClientProfile; -import launcher.client.ClientProfile.Version; import launcher.helper.IOHelper; import launcher.helper.LogHelper; import launcher.serialize.config.TextConfigReader; @@ -9,13 +8,14 @@ import launcher.serialize.config.entry.StringConfigEntry; import launchserver.LaunchServer; import launchserver.command.Command; +import launchserver.helpers.HTTPRequestHelper; 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; import java.util.Collections; public final class DownloadClientCommand extends Command @@ -44,59 +44,47 @@ String version = args[0]; String dirName = IOHelper.verifyFileName(args[1]); Path clientDir = server.updatesDir.resolve(args[1]); - - // Create client dir - LogHelper.subInfo("Creating client dir: '%s'", dirName); - Files.createDirectory(clientDir); - - // Download required client - LogHelper.subInfo("Downloading client, it may take some time"); String[] mirrors = server.config.mirrors.stream(StringConfigEntry.class).toArray(String[]::new); String clientMask = String.format("clients/%s.zip", version); - UnzipHelper.downloadZip(mirrors, clientMask, clientDir); + String profileMask = String.format("clients/%s.cfg", version); - // Create profile file - LogHelper.subInfo("Creating profile file: '%s'", dirName); - ClientProfile client; - String profilePath; + for (String mirror : mirrors) + { + URL clientUrl = new URL(mirror + clientMask); + URL profileUrl = new URL(mirror + profileMask); - // Я бы выпилил это вообще нахуй, но дефолтные конфиги нужны! (Товарищи, партия требует дефолтые конфиги!!!) - if (Version.compare(version, "1.6.4") <= 0) { - profilePath = "launchserver/defaults/profile-legacy.cfg"; - } else { - if (version.contains("-")) { - String[] versionArgs = version.split("-"); - version = versionArgs[0]; - String modLoader = versionArgs[1]; + if (!HTTPRequestHelper.fileExist(clientUrl) || !HTTPRequestHelper.fileExist(profileUrl)) continue; - if (Arrays.asList("forge", "fabric").contains(modLoader)) { - profilePath = String.format("launchserver/defaults/profile-%s.cfg", modLoader); - } else { - profilePath = "launchserver/defaults/profile-default.cfg"; - } - } else { - profilePath = "launchserver/defaults/profile-default.cfg"; + // Create profile file + LogHelper.subInfo("Client found. Creating profile file: '%s'", dirName); + ClientProfile client; + + // Download required client + LogHelper.subInfo("Downloading client, it may take some time"); + Files.createDirectory(clientDir); + if(!UnzipHelper.downloadZip(clientUrl, clientDir)) return; + + try (BufferedReader reader = IOHelper.newReader(profileUrl)) + { + client = new ClientProfile(TextConfigReader.read(reader, false)); } - } + client.setTitle(dirName); + client.setVersion(version); + client.setAssetIndex(version); + client.block.getEntry("dir", StringConfigEntry.class).setValue(dirName); + try (BufferedWriter writer = IOHelper.newWriter(IOHelper.resolveIncremental(server.profilesDir, + dirName, "cfg"))) + { + TextConfigWriter.write(client.block, writer, true); + } - try (BufferedReader reader = IOHelper.newReader(IOHelper.getResourceURL(profilePath))) - { - client = new ClientProfile(TextConfigReader.read(reader, false)); + // Finished + server.syncProfilesDir(); + server.syncUpdatesDir(Collections.singleton(dirName)); + LogHelper.subInfo("Client successfully downloaded: '%s'", dirName); + LogHelper.subInfo("DON'T FORGET! Set up the assets directory!"); + return; } - client.setTitle(dirName); - client.setVersion(version); - client.setAssetIndex(version); - client.block.getEntry("dir", StringConfigEntry.class).setValue(dirName); - try (BufferedWriter writer = IOHelper.newWriter(IOHelper.resolveIncremental(server.profilesDir, - dirName, "cfg"))) - { - TextConfigWriter.write(client.block, writer, true); - } - - // Finished - server.syncProfilesDir(); - server.syncUpdatesDir(Collections.singleton(dirName)); - LogHelper.subInfo("Client successfully downloaded: '%s'", dirName); - LogHelper.subInfo("DON'T FORGET! Set up the assets directory!"); + LogHelper.error("Error download %s. All mirrors return error", dirName); } } diff --git a/LaunchServer/source/helpers/HTTPRequestHelper.java b/LaunchServer/source/helpers/HTTPRequestHelper.java index c071b25..1603248 100644 --- a/LaunchServer/source/helpers/HTTPRequestHelper.java +++ b/LaunchServer/source/helpers/HTTPRequestHelper.java @@ -7,19 +7,19 @@ import java.net.URL; public class HTTPRequestHelper { - private static HttpURLConnection makeRequest(String url, String requestMethod) throws IOException { - HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + private static HttpURLConnection makeRequest(URL url, String requestMethod) throws IOException { + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(requestMethod); return connection; } - public static boolean fileExsist(String url) throws IOException { + public static boolean fileExist(URL url) throws IOException { HttpURLConnection request = makeRequest(url, "HEAD"); int responseCode = request.getResponseCode(); return responseCode >= 200 && responseCode < 300; } - public static String getFile(String url) throws IOException { + public static String getFile(URL url) throws IOException { HttpURLConnection request = makeRequest(url, "GET"); BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream())); StringBuilder response = new StringBuilder(); diff --git a/LaunchServer/source/helpers/UnzipHelper.java b/LaunchServer/source/helpers/UnzipHelper.java index ce67e72..059fc24 100644 --- a/LaunchServer/source/helpers/UnzipHelper.java +++ b/LaunchServer/source/helpers/UnzipHelper.java @@ -4,7 +4,6 @@ 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; @@ -30,21 +29,7 @@ } } - 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(); - } + public static boolean downloadZip(URL url, Path dir) { LogHelper.debug("Try download %s", url.toString()); try { unpack(url, dir); @@ -54,5 +39,4 @@ } return true; } - } diff --git a/buildnumber b/buildnumber index c75ac8c..5297a86 100644 --- a/buildnumber +++ b/buildnumber @@ -1 +1 @@ -522, 15.02.2021 \ No newline at end of file +527, 15.02.2021 \ No newline at end of file