From 0b59d6c0edc3936115fce1236d744f86db1198df Mon Sep 17 00:00:00 2001 From: Gravita <12893402+gravit0@users.noreply.github.com> Date: Sat, 10 Feb 2024 14:47:13 +0700 Subject: [PATCH] [FEATURE] Profile clone command --- .../command/handler/CommandHandler.java | 6 +- .../command/profiles/CloneProfileCommand.java | 73 +++++++++++++++++++ .../MakeProfileCommand.java | 2 +- .../command/profiles/ProfilesCommand.java | 28 +++++++ .../SaveProfilesCommand.java | 2 +- modules | 2 +- 6 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/command/profiles/CloneProfileCommand.java rename LaunchServer/src/main/java/pro/gravit/launchserver/command/{hash => profiles}/MakeProfileCommand.java (97%) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/command/profiles/ProfilesCommand.java rename LaunchServer/src/main/java/pro/gravit/launchserver/command/{hash => profiles}/SaveProfilesCommand.java (98%) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java index c369e0a5..5b6d9aff 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java @@ -5,6 +5,9 @@ import pro.gravit.launchserver.command.hash.*; import pro.gravit.launchserver.command.modules.LoadModuleCommand; import pro.gravit.launchserver.command.modules.ModulesCommand; +import pro.gravit.launchserver.command.profiles.MakeProfileCommand; +import pro.gravit.launchserver.command.profiles.ProfilesCommand; +import pro.gravit.launchserver.command.profiles.SaveProfilesCommand; import pro.gravit.launchserver.command.service.*; import pro.gravit.launchserver.command.sync.*; import pro.gravit.launchserver.command.tools.SignDirCommand; @@ -37,8 +40,7 @@ public static void registerCommands(pro.gravit.utils.command.CommandHandler hand updates.registerCommand("downloadAsset", new DownloadAssetCommand(server)); updates.registerCommand("downloadClient", new DownloadClientCommand(server)); updates.registerCommand("sync", new SyncCommand(server)); - updates.registerCommand("saveProfiles", new SaveProfilesCommand(server)); - updates.registerCommand("makeProfile", new MakeProfileCommand(server)); + updates.registerCommand("profile", new ProfilesCommand(server)); Category updatesCategory = new Category(updates, "updates", "Update and Sync Management"); handler.registerCategory(updatesCategory); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/profiles/CloneProfileCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/profiles/CloneProfileCommand.java new file mode 100644 index 00000000..43e54f85 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/profiles/CloneProfileCommand.java @@ -0,0 +1,73 @@ +package pro.gravit.launchserver.command.profiles; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import pro.gravit.launcher.base.Launcher; +import pro.gravit.launcher.base.profiles.ClientProfile; +import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.command.Command; +import pro.gravit.utils.helper.IOHelper; + +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.UUID; +import java.util.stream.Stream; + +public class CloneProfileCommand extends Command { + private final transient Logger logger = LogManager.getLogger(CloneProfileCommand.class); + public CloneProfileCommand(LaunchServer server) { + super(server); + } + + @Override + public String getArgsDescription() { + return "[profile file name] [new profile title]"; + } + + @Override + public String getUsageDescription() { + return "clone profile and profile dir"; + } + + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 2); + var profilePath = server.profilesDir.resolve(args[0].concat(".json")); + if(!Files.exists(profilePath)) { + logger.error("File {} not found", profilePath); + } + ClientProfile profile; + try(Reader reader = IOHelper.newReader(profilePath)) { + profile = Launcher.gsonManager.gson.fromJson(reader, ClientProfile.class); + } + profile.setTitle(args[1]); + profile.setUUID(UUID.randomUUID()); + if(profile.getServers().size() == 1) { + profile.getServers().getFirst().name = args[1]; + } + logger.info("Copy {} to {}", profile.getDir(), args[1]); + var src = server.updatesDir.resolve(profile.getDir()); + var dest = server.updatesDir.resolve(args[1]); + try (Stream stream = Files.walk(src)) { + stream.forEach(source -> { + try { + IOHelper.copy(source, dest.resolve(src.relativize(source))); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + } + profile.setDir(args[1]); + var targetPath = server.profilesDir.resolve(args[1].concat(".json")); + try(Writer writer = IOHelper.newWriter(targetPath)) { + Launcher.gsonManager.gson.toJson(profile, writer); + } + logger.info("Profile {} cloned from {}", args[1], args[0]); + server.syncProfilesDir(); + server.syncUpdatesDir(List.of(args[1])); + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/MakeProfileCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/profiles/MakeProfileCommand.java similarity index 97% rename from LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/MakeProfileCommand.java rename to LaunchServer/src/main/java/pro/gravit/launchserver/command/profiles/MakeProfileCommand.java index a424593c..ad57670c 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/MakeProfileCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/profiles/MakeProfileCommand.java @@ -1,4 +1,4 @@ -package pro.gravit.launchserver.command.hash; +package pro.gravit.launchserver.command.profiles; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/profiles/ProfilesCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/profiles/ProfilesCommand.java new file mode 100644 index 00000000..395c1945 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/profiles/ProfilesCommand.java @@ -0,0 +1,28 @@ +package pro.gravit.launchserver.command.profiles; + +import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.command.Command; + +public class ProfilesCommand extends Command { + public ProfilesCommand(LaunchServer server) { + super(server); + this.childCommands.put("make", new MakeProfileCommand(server)); + this.childCommands.put("save", new SaveProfilesCommand(server)); + this.childCommands.put("clone", new CloneProfileCommand(server)); + } + + @Override + public String getArgsDescription() { + return "[subcommand] [args...]"; + } + + @Override + public String getUsageDescription() { + return "manage profiles"; + } + + @Override + public void invoke(String... args) throws Exception { + invokeSubcommands(args); + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/SaveProfilesCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/profiles/SaveProfilesCommand.java similarity index 98% rename from LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/SaveProfilesCommand.java rename to LaunchServer/src/main/java/pro/gravit/launchserver/command/profiles/SaveProfilesCommand.java index 32e65634..c0f0f5d0 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/SaveProfilesCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/profiles/SaveProfilesCommand.java @@ -1,4 +1,4 @@ -package pro.gravit.launchserver.command.hash; +package pro.gravit.launchserver.command.profiles; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; diff --git a/modules b/modules index cd920b0e..1c3646c8 160000 --- a/modules +++ b/modules @@ -1 +1 @@ -Subproject commit cd920b0eda048771ea3c225074db32943d27503b +Subproject commit 1c3646c8161f38c44d6578ba7aeabce27823556f