From 5588b4aac13c333e9b3d2d0862f22192f2633b30 Mon Sep 17 00:00:00 2001 From: microwin7 Date: Sat, 18 Feb 2023 07:00:35 +0300 Subject: [PATCH] [FEATURE] Default value for client profile and assets dir --- .../command/hash/DownloadAssetCommand.java | 4 +- .../command/hash/DownloadClientCommand.java | 45 ++++++++++--------- .../java/pro/gravit/utils/HttpDownloader.java | 1 + 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/DownloadAssetCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/DownloadAssetCommand.java index 997240a3..a1f79553 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/DownloadAssetCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/DownloadAssetCommand.java @@ -40,11 +40,11 @@ public String getUsageDescription() { @Override public void invoke(String... args) throws Exception { - verifyArgs(args, 2); + verifyArgs(args, 1); //Version version = Version.byName(args[0]); String versionName = args[0]; + String dirName = IOHelper.verifyFileName(args[1] != null ? args[1] : "assets"); String type = args.length > 2 ? args[2] : "mojang"; - String dirName = IOHelper.verifyFileName(args[1]); Path assetDir = server.updatesDir.resolve(dirName); // Create asset dir diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/DownloadClientCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/DownloadClientCommand.java index 36fb964a..942be0c2 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/DownloadClientCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/DownloadClientCommand.java @@ -41,18 +41,14 @@ public void invoke(String... args) throws IOException, CommandException { verifyArgs(args, 2); //Version version = Version.byName(args[0]); String versionName = args[0]; - String dirName = IOHelper.verifyFileName(args[1]); - Path clientDir = server.updatesDir.resolve(args[1]); + String dirName = IOHelper.verifyFileName(args[1] != null ? args[1] : args[0]); + Path clientDir = server.updatesDir.resolve(dirName); boolean isMirrorClientDownload = false; if (args.length > 2) { isMirrorClientDownload = args[2].equals("mirror"); } - // Create client dir - logger.info("Creating client dir: '{}'", dirName); - Files.createDirectory(clientDir); - // Download required client logger.info("Downloading client, it may take some time"); //HttpDownloader.downloadZip(server.mirrorManager.getDefaultMirror().getClientsURL(version.name), clientDir); @@ -60,7 +56,25 @@ public void invoke(String... args) throws IOException, CommandException { // Create profile file logger.info("Creaing profile file: '{}'", dirName); - ClientProfile client = null; + ClientProfile clientProfile = null; + if (isMirrorClientDownload) { + try { + JsonElement clientJson = server.mirrorManager.jsonRequest(null, "GET", "clients/%s.json", versionName); + clientProfile = Launcher.gsonManager.configGson.fromJson(clientJson, ClientProfile.class); + clientProfile.setTitle(dirName); + clientProfile.setDir(dirName); + clientProfile.setUUID(UUID.randomUUID()); + if (clientProfile.getServers() != null) { + ClientProfile.ServerProfile serverProfile = clientProfile.getDefaultServerProfile(); + if (serverProfile != null) { + serverProfile.name = dirName; + } + } + } catch (Exception e) { + logger.error("Filed download clientProfile from mirror: '{}' Generation through MakeProfileHelper", versionName); + isMirrorClientDownload = false; + } + } if (!isMirrorClientDownload) { try { String internalVersion = versionName; @@ -75,27 +89,14 @@ public void invoke(String... args) throws IOException, CommandException { for (MakeProfileHelper.MakeProfileOption option : options) { logger.debug("Detected option {}", option.getClass().getSimpleName()); } - client = MakeProfileHelper.makeProfile(version, dirName, options); + clientProfile = MakeProfileHelper.makeProfile(version, dirName, options); } catch (Throwable e) { isMirrorClientDownload = true; } } - if (isMirrorClientDownload) { - JsonElement clientJson = server.mirrorManager.jsonRequest(null, "GET", "clients/%s.json", versionName); - client = Launcher.gsonManager.configGson.fromJson(clientJson, ClientProfile.class); - client.setTitle(dirName); - client.setDir(dirName); - client.setUUID(UUID.randomUUID()); - if (client.getServers() != null) { - ClientProfile.ServerProfile serverProfile = client.getDefaultServerProfile(); - if (serverProfile != null) { - serverProfile.name = dirName; - } - } - } try (BufferedWriter writer = IOHelper.newWriter(IOHelper.resolveIncremental(server.profilesDir, dirName, "json"))) { - Launcher.gsonManager.configGson.toJson(client, writer); + Launcher.gsonManager.configGson.toJson(clientProfile, writer); } // Finished diff --git a/LauncherCore/src/main/java/pro/gravit/utils/HttpDownloader.java b/LauncherCore/src/main/java/pro/gravit/utils/HttpDownloader.java index 161d317b..6a606f8d 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/HttpDownloader.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/HttpDownloader.java @@ -56,6 +56,7 @@ public static void downloadFile(URL url, Path file, Consumer chanheTrac public static void downloadZip(URL url, Path dir) throws IOException { try (ZipInputStream input = IOHelper.newZipInput(url)) { + Files.createDirectory(dir); for (ZipEntry entry = input.getNextEntry(); entry != null; entry = input.getNextEntry()) { if (entry.isDirectory()) { Files.createDirectory(dir.resolve(IOHelper.toPath(entry.getName())));