From bc1d50fb8ecc94c6341f8a2adbc40b7157b907be Mon Sep 17 00:00:00 2001 From: Chaos Date: Tue, 11 Jan 2022 20:56:07 +0100 Subject: [PATCH 1/4] Add saveAdmin Script --- pom.xml | 5 ----- .../chaos/swlnmngr/route/routes/InstallRoute.java | 11 +++-------- src/main/resources/default_swlnmngr.bat | 15 +++++++++++++-- src/main/resources/default_swlnmngr_admin.bat | 5 +++++ 4 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 src/main/resources/default_swlnmngr_admin.bat diff --git a/pom.xml b/pom.xml index d4b7c89..1f35540 100644 --- a/pom.xml +++ b/pom.xml @@ -98,10 +98,5 @@ commons-lang 2.6 - - com.github.vatbub - mslinks - 1.0.6.2 - \ No newline at end of file diff --git a/src/main/java/de/chaos/swlnmngr/route/routes/InstallRoute.java b/src/main/java/de/chaos/swlnmngr/route/routes/InstallRoute.java index cf51554..6ecbaaf 100644 --- a/src/main/java/de/chaos/swlnmngr/route/routes/InstallRoute.java +++ b/src/main/java/de/chaos/swlnmngr/route/routes/InstallRoute.java @@ -2,26 +2,23 @@ package de.chaos.swlnmngr.route.routes; import de.chaos.swlnmngr.Main; import de.chaos.swlnmngr.config.CLIConfig; -import mslinks.ShellLink; import org.apache.commons.lang.SystemUtils; import org.json.JSONObject; import org.json.JSONTokener; import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.nio.file.StandardOpenOption; -import java.nio.file.attribute.FileAttribute; -import java.nio.file.attribute.PosixFilePermission; import java.util.Objects; public class InstallRoute implements Route { - private static final String[] defaultFiles = new String[] {"default_swlnmngr.bat", "default_swlnmngr.sh"}; + private static final String[] defaultFiles = new String[] {"default_swlnmngr.bat", "default_swlnmngr.sh", "default_swlnmngr_admin.bat"}; @Override public String getName() { @@ -61,10 +58,8 @@ public class InstallRoute implements Route { } } else if(SystemUtils.IS_OS_WINDOWS) { try { - ShellLink link = ShellLink.createLink(new File(CLIConfig.INSTALL_DIR, "swlnmngr.bat").getAbsolutePath(), new File(CLIConfig.INSTALL_DIR, "swlnmngr.lnk").getAbsolutePath()).setWorkingDir(CLIConfig.INSTALL_DIR.getAbsolutePath()); - link.getHeader().getLinkFlags().setRunAsUser(); - link.saveTo(new File(CLIConfig.INSTALL_DIR, "swlnmngr.lnk").getAbsolutePath()); Files.writeString(new File(CLIConfig.INSTALL_DIR, "swlnmngr.bat").toPath(), Files.readString(new File(CLIConfig.INSTALL_DIR, "swlnmngr.bat").toPath()).replace("${iDir}", CLIConfig.INSTALL_DIR.getAbsolutePath()), StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING); + Files.writeString(new File(CLIConfig.INSTALL_DIR, "swlnmngr_admin.bat").toPath(), Files.readString(new File(CLIConfig.INSTALL_DIR, "swlnmngr_admin.bat").toPath()).replace("${iDir}", CLIConfig.INSTALL_DIR.getAbsolutePath()), StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING); } catch (IOException e) { Main.getLogger().error("Could not create Link", e); return false; diff --git a/src/main/resources/default_swlnmngr.bat b/src/main/resources/default_swlnmngr.bat index eb0de03..f8a4e14 100644 --- a/src/main/resources/default_swlnmngr.bat +++ b/src/main/resources/default_swlnmngr.bat @@ -1,5 +1,16 @@ +@echo off +:: Variables +SET JavaHome=java + +:: Code +title Request Admin + +Net session >nul 2>&1 || (PowerShell start -verb runas '%~0' & exit) + +title SteamWarLinkManager set args=--help set /p args=Arguments: echo %args% -java -jar ${iDir}\SteamWarLinkManager.jar %args% %* -pause \ No newline at end of file +%JavaHome% -jar ${iDir}\SteamWarLinkManager.jar %args% %* +pause +exit \ No newline at end of file diff --git a/src/main/resources/default_swlnmngr_admin.bat b/src/main/resources/default_swlnmngr_admin.bat new file mode 100644 index 0000000..9dce24f --- /dev/null +++ b/src/main/resources/default_swlnmngr_admin.bat @@ -0,0 +1,5 @@ +@echo off + +title Admin +runas /noprofile /savecred /user:Admin ${iDir}\swlnmngr.bat +exit \ No newline at end of file From 8659bce67134853c5f2971756b7b433e1e226b92 Mon Sep 17 00:00:00 2001 From: Chaos Date: Tue, 11 Jan 2022 21:18:23 +0100 Subject: [PATCH 2/4] Fixing more Stuff --- pom.xml | 6 ------ src/main/java/de/chaos/swlnmngr/Main.java | 11 +++++++--- .../java/de/chaos/swlnmngr/UpdateChecker.java | 4 ++-- .../de/chaos/swlnmngr/config/CLIConfig.java | 17 +++++++++++++++- .../swlnmngr/route/routes/InstallRoute.java | 20 ++++++++++--------- src/main/resources/default_swlnmngr.bat | 3 ++- 6 files changed, 39 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index 1f35540..8a47e77 100644 --- a/pom.xml +++ b/pom.xml @@ -67,12 +67,6 @@ json 20211205 - - org.projectlombok - lombok - 1.18.22 - provided - commons-cli commons-cli diff --git a/src/main/java/de/chaos/swlnmngr/Main.java b/src/main/java/de/chaos/swlnmngr/Main.java index 87d668d..75e3296 100644 --- a/src/main/java/de/chaos/swlnmngr/Main.java +++ b/src/main/java/de/chaos/swlnmngr/Main.java @@ -2,7 +2,6 @@ package de.chaos.swlnmngr; import de.chaos.swlnmngr.config.CLIConfig; import de.chaos.swlnmngr.route.Router; -import lombok.Getter; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -12,9 +11,7 @@ import java.util.Arrays; public class Main { - @Getter private static final Logger logger = LogManager.getLogger(Main.class); - @Getter private static String[] allArgs; public static void main(String[] args) { @@ -33,4 +30,12 @@ public class Main { Router.printRoutes(); } } + + public static Logger getLogger() { + return logger; + } + + public static String[] getAllArgs() { + return allArgs; + } } diff --git a/src/main/java/de/chaos/swlnmngr/UpdateChecker.java b/src/main/java/de/chaos/swlnmngr/UpdateChecker.java index f85ee07..ccf5ff5 100644 --- a/src/main/java/de/chaos/swlnmngr/UpdateChecker.java +++ b/src/main/java/de/chaos/swlnmngr/UpdateChecker.java @@ -1,7 +1,6 @@ package de.chaos.swlnmngr; import de.chaos.swlnmngr.config.Config; -import lombok.experimental.UtilityClass; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; @@ -13,9 +12,10 @@ import org.json.JSONTokener; import java.io.IOException; import java.net.URI; -@UtilityClass public class UpdateChecker { + private UpdateChecker() {} + private static final URI repoUrl = URI.create("https://steamwar.de/devlabs/api/v1/repos/Chaoscaot/SteamwarLinkManager/releases?draft=false&pre-release=" + Config.PRE_RELEASES + "&page=1&limit=1"); public static final String CURRENT_VERSION; diff --git a/src/main/java/de/chaos/swlnmngr/config/CLIConfig.java b/src/main/java/de/chaos/swlnmngr/config/CLIConfig.java index 2d164b6..2f9c2bf 100644 --- a/src/main/java/de/chaos/swlnmngr/config/CLIConfig.java +++ b/src/main/java/de/chaos/swlnmngr/config/CLIConfig.java @@ -4,6 +4,8 @@ import de.chaos.swlnmngr.Main; import org.apache.commons.cli.*; import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; public class CLIConfig { @@ -12,6 +14,7 @@ public class CLIConfig { public static final File CONFIG; public static final File INSTALL_DIR; public static final String[] ARGS; + public static final boolean INSTALL_DIR_IS_SET; static { Options options = new Options(); @@ -43,8 +46,20 @@ public class CLIConfig { NO_UPDATE = cli.hasOption("u"); if(cli.hasOption("i")) { INSTALL_DIR = new File(cli.getOptionValue("i")); + INSTALL_DIR_IS_SET = true; } else { - INSTALL_DIR = new File(System.getProperty("user.home"), ".swlnmngr/").toPath().normalize().toFile(); + try { + INSTALL_DIR = new File(CLIConfig.class.getProtectionDomain() + .getCodeSource() + .getLocation() + .toURI() + .getPath()) + .getCanonicalFile(); + INSTALL_DIR_IS_SET = false; + } catch (IOException | URISyntaxException e) { + Main.getLogger().error(e.getMessage(), e); + throw new SecurityException(e); + } } if(cli.hasOption("c")) { CONFIG = new File(cli.getOptionValue("c")); diff --git a/src/main/java/de/chaos/swlnmngr/route/routes/InstallRoute.java b/src/main/java/de/chaos/swlnmngr/route/routes/InstallRoute.java index 6ecbaaf..49e00ba 100644 --- a/src/main/java/de/chaos/swlnmngr/route/routes/InstallRoute.java +++ b/src/main/java/de/chaos/swlnmngr/route/routes/InstallRoute.java @@ -7,7 +7,6 @@ import org.json.JSONObject; import org.json.JSONTokener; import java.io.File; -import java.io.FileWriter; import java.io.IOException; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; @@ -28,9 +27,12 @@ public class InstallRoute implements Route { @Override public boolean route(String[] args) { File installDir = CLIConfig.INSTALL_DIR; + if (!CLIConfig.INSTALL_DIR_IS_SET) { + installDir = new File(System.getProperty("user.home", ".swlnmngr")); + } if(!installDir.exists()) { try { - Files.createDirectories(CLIConfig.INSTALL_DIR.toPath()); + Files.createDirectories(installDir.toPath()); } catch (IOException e) { Main.getLogger().error("Could not create Install Directory", e); return false; @@ -40,8 +42,8 @@ public class InstallRoute implements Route { for (String defaultFile : defaultFiles) { String normalName = defaultFile.replace("default_", ""); try { - Files.copy(Objects.requireNonNull(InstallRoute.class.getResourceAsStream("/" + defaultFile)), new File(CLIConfig.INSTALL_DIR, normalName).toPath(), StandardCopyOption.REPLACE_EXISTING); - new File(CLIConfig.INSTALL_DIR, normalName).setExecutable(true, true); + Files.copy(Objects.requireNonNull(InstallRoute.class.getResourceAsStream("/" + defaultFile)), new File(installDir, normalName).toPath(), StandardCopyOption.REPLACE_EXISTING); + new File(installDir, normalName).setExecutable(true, true); } catch (IOException e) { Main.getLogger().error("Could not create File", e); return false; @@ -50,16 +52,16 @@ public class InstallRoute implements Route { if(SystemUtils.IS_OS_UNIX) { try { - Files.deleteIfExists(new File(CLIConfig.INSTALL_DIR, "swlnmngr").toPath()); - Files.createSymbolicLink(new File(CLIConfig.INSTALL_DIR, "swlnmngr").toPath(), new File(CLIConfig.INSTALL_DIR, "swlnmngr.sh").toPath()); + Files.deleteIfExists(new File(installDir, "swlnmngr").toPath()); + Files.createSymbolicLink(new File(installDir, "swlnmngr").toPath(), new File(installDir, "swlnmngr.sh").toPath()); } catch (IOException e) { Main.getLogger().error("Could not create SymLink", e); return false; } } else if(SystemUtils.IS_OS_WINDOWS) { try { - Files.writeString(new File(CLIConfig.INSTALL_DIR, "swlnmngr.bat").toPath(), Files.readString(new File(CLIConfig.INSTALL_DIR, "swlnmngr.bat").toPath()).replace("${iDir}", CLIConfig.INSTALL_DIR.getAbsolutePath()), StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING); - Files.writeString(new File(CLIConfig.INSTALL_DIR, "swlnmngr_admin.bat").toPath(), Files.readString(new File(CLIConfig.INSTALL_DIR, "swlnmngr_admin.bat").toPath()).replace("${iDir}", CLIConfig.INSTALL_DIR.getAbsolutePath()), StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING); + Files.writeString(new File(installDir, "swlnmngr.bat").toPath(), Files.readString(new File(installDir, "swlnmngr.bat").toPath()).replace("${iDir}", installDir.getAbsolutePath()), StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING); + Files.writeString(new File(installDir, "swlnmngr_admin.bat").toPath(), Files.readString(new File(installDir, "swlnmngr_admin.bat").toPath()).replace("${iDir}", installDir.getAbsolutePath()), StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING); } catch (IOException e) { Main.getLogger().error("Could not create Link", e); return false; @@ -104,7 +106,7 @@ public class InstallRoute implements Route { try { File jar = new File(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()); Main.getLogger().debug(jar); - Files.copy(jar.toPath(), new File(CLIConfig.INSTALL_DIR, "SteamWarLinkManager.jar").toPath(), StandardCopyOption.REPLACE_EXISTING); + Files.copy(jar.toPath(), new File(installDir, "SteamWarLinkManager.jar").toPath(), StandardCopyOption.REPLACE_EXISTING); } catch (URISyntaxException e) { Main.getLogger().error("Could parse Jar Location", e); return false; diff --git a/src/main/resources/default_swlnmngr.bat b/src/main/resources/default_swlnmngr.bat index f8a4e14..b944970 100644 --- a/src/main/resources/default_swlnmngr.bat +++ b/src/main/resources/default_swlnmngr.bat @@ -1,6 +1,7 @@ @echo off :: Variables SET JavaHome=java +SET Home=${iDir} :: Code title Request Admin @@ -11,6 +12,6 @@ title SteamWarLinkManager set args=--help set /p args=Arguments: echo %args% -%JavaHome% -jar ${iDir}\SteamWarLinkManager.jar %args% %* +%JavaHome% -jar ${iDir}\SteamWarLinkManager.jar %args% %* -i %Home% pause exit \ No newline at end of file From bb80ee110d5fcd64d07db0c2b363ba8e9706f004 Mon Sep 17 00:00:00 2001 From: Chaos Date: Tue, 11 Jan 2022 21:20:17 +0100 Subject: [PATCH 3/4] Fix --- src/main/java/de/chaos/swlnmngr/config/CLIConfig.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/chaos/swlnmngr/config/CLIConfig.java b/src/main/java/de/chaos/swlnmngr/config/CLIConfig.java index 2f9c2bf..058ece6 100644 --- a/src/main/java/de/chaos/swlnmngr/config/CLIConfig.java +++ b/src/main/java/de/chaos/swlnmngr/config/CLIConfig.java @@ -54,7 +54,8 @@ public class CLIConfig { .getLocation() .toURI() .getPath()) - .getCanonicalFile(); + .getCanonicalFile() + .getParentFile(); INSTALL_DIR_IS_SET = false; } catch (IOException | URISyntaxException e) { Main.getLogger().error(e.getMessage(), e); From 634e9319203004e92e0b488529ce9cb22709ca27 Mon Sep 17 00:00:00 2001 From: Chaos Date: Tue, 11 Jan 2022 21:43:59 +0100 Subject: [PATCH 4/4] Fix --- pom.xml | 5 +++++ .../java/de/chaos/swlnmngr/route/routes/LinkRoute.java | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 8a47e77..3394dd2 100644 --- a/pom.xml +++ b/pom.xml @@ -92,5 +92,10 @@ commons-lang 2.6 + + commons-io + commons-io + 2.11.0 + \ No newline at end of file diff --git a/src/main/java/de/chaos/swlnmngr/route/routes/LinkRoute.java b/src/main/java/de/chaos/swlnmngr/route/routes/LinkRoute.java index 3b2fd95..debf7e8 100644 --- a/src/main/java/de/chaos/swlnmngr/route/routes/LinkRoute.java +++ b/src/main/java/de/chaos/swlnmngr/route/routes/LinkRoute.java @@ -2,6 +2,7 @@ package de.chaos.swlnmngr.route.routes; import de.chaos.swlnmngr.Main; import de.chaos.swlnmngr.config.Config; +import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; @@ -36,7 +37,13 @@ public class LinkRoute implements Route { } File link = new File(projectDir, "lib"); try { - Files.deleteIfExists(link.toPath()); + if(link.exists()) { + if(link.isDirectory()) { + FileUtils.deleteDirectory(link); + } else { + FileUtils.delete(link); + } + } Main.getLogger().debug(libsFile); Main.getLogger().debug(link); Path linkPath = Files.createSymbolicLink(link.toPath(), libsFile.toPath());