1
0

Adding Update and installer

Dieser Commit ist enthalten in:
Chaoscaot 2022-01-02 02:23:37 +01:00
Ursprung e662daeffc
Commit b636a81ec5
13 geänderte Dateien mit 224 neuen und 16 gelöschten Zeilen

1
.gitignore vendored
Datei anzeigen

@ -85,3 +85,4 @@ fabric.properties
.idea/caches/build_file_checksums.ser
/config.json
/.idea/
/test/

Datei anzeigen

@ -1,7 +1,7 @@
package de.chaos.swlnmngr;
import de.chaos.swlnmngr.config.CLIConfig;
import de.chaos.swlnmngr.config.Config;
import de.chaos.swlnmngr.route.Router;
import lombok.Getter;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
@ -19,12 +19,15 @@ public class Main {
public static void main(String[] args) {
logger.log(Level.INFO, "SteamWarLinkManager by Chaoscaot");
allArgs = new String[args.length + Config.ADDITIONAL_ARGS.length];
System.arraycopy(args, 0, allArgs, 0, args.length);
System.arraycopy(Config.ADDITIONAL_ARGS, 0, allArgs, args.length, Config.ADDITIONAL_ARGS.length);
allArgs = args;
if(CLIConfig.IS_DEBUG) {
Configurator.setRootLevel(Level.DEBUG);
}
logger.debug("Arguments: {}", Arrays.toString(allArgs));
if(CLIConfig.ARGS.length > 0) {
Router.route(CLIConfig.ARGS);
} else {
}
}
}

Datei anzeigen

@ -3,16 +3,22 @@ package de.chaos.swlnmngr.config;
import de.chaos.swlnmngr.Main;
import org.apache.commons.cli.*;
import java.io.File;
public class CLIConfig {
public static final boolean IS_DEBUG;
public static final boolean UPDATE;
public static final File CONFIG;
public static final File INSTALL_DIR;
public static final String[] ARGS;
static {
Options options = new Options();
options.addOption(new Option("d", "debug", false, "Set the Log Level to Debug"));
options.addOption(new Option("u", "update", false, "Update the Default libs"));
options.addOption(new Option("c", "config", true, "Use another Config File"));
options.addOption(new Option("i", "installdir", true, "Use other Install Dir"));
CommandLine cli = null;
CommandLineParser parser = new DefaultParser();
@ -29,5 +35,15 @@ public class CLIConfig {
ARGS = cli.getArgs();
IS_DEBUG = cli.hasOption("d");
UPDATE = cli.hasOption("u");
if(cli.hasOption("i")) {
INSTALL_DIR = new File(cli.getOptionValue("i"));
} else {
INSTALL_DIR = new File("~/.swlnmngr/");
}
if(cli.hasOption("c")) {
CONFIG = new File(cli.getOptionValue("c"));
} else {
CONFIG = new File(INSTALL_DIR, "config.json");
}
}
}

Datei anzeigen

@ -1,24 +1,36 @@
package de.chaos.swlnmngr.config;
import de.chaos.swlnmngr.Main;
import org.apache.logging.log4j.Level;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
public class Config {
private static final File CONFIG_FILE = new File("config.json");
private static final File CONFIG_FILE = new File(CLIConfig.INSTALL_DIR, "config.json");
public static final String[] ADDITIONAL_ARGS;
public static final String PROJECT_PATH;
public static final File LIB_PATH;
public static final String DEFAULTS;
public static final URI LIB_URL;
public static final String USERNAME;
public static final String PASSWORD;
static {
JSONObject config = null;
try {
if(!CONFIG_FILE.exists()) {
if(CONFIG_FILE.getParentFile().mkdir()) {
Main.getLogger().log(Level.DEBUG, "Created Config Folder");
}
Files.write(CONFIG_FILE.toPath(), Config.class.getResourceAsStream("/default_config.json").readAllBytes(), StandardOpenOption.CREATE_NEW);
}
@ -28,10 +40,19 @@ public class Config {
System.exit(1);
}
ADDITIONAL_ARGS = config.getJSONArray("additionalArgs").toList().toArray(new String[0]);
}
public static void reset() {
PROJECT_PATH = config.getString("projectPath");
LIB_PATH = new File(config.getString("libPath"));
DEFAULTS = config.getString("defaultName");
try {
LIB_URL = new URI(config.getString("libUrl"));
} catch (URISyntaxException e) {
Main.getLogger().error("libUrl is not a URL", e);
System.exit(1);
throw new SecurityException(e);
}
JSONObject credentials = config.getJSONObject("credentials");
USERNAME = credentials.getString("username");
PASSWORD = credentials.getString("password");
}
}

Datei anzeigen

@ -1,4 +1,32 @@
package de.chaos.swlnmngr.route;
import de.chaos.swlnmngr.Main;
import de.chaos.swlnmngr.route.routes.InstallRoute;
import de.chaos.swlnmngr.route.routes.Route;
import de.chaos.swlnmngr.route.routes.UpdateRoute;
import java.util.*;
public class Router {
private static final List<Route> ROUTES;
static {
ROUTES = List.of(new InstallRoute(), new UpdateRoute());
}
public static void route(String[] args) {
for (Route route : ROUTES) {
if(route.getName().equalsIgnoreCase(args[0])) {
String[] rArgs = new String[args.length - 1];
System.arraycopy(args, 1, rArgs, 0, args.length - 1);
Main.getLogger().info("Running: {}", route.getName());
if(route.route(rArgs)) {
System.exit(0);
} else {
System.exit(1);
}
}
}
}
}

Datei anzeigen

@ -0,0 +1,53 @@
package de.chaos.swlnmngr.route.routes;
import com.sun.nio.file.ExtendedCopyOption;
import de.chaos.swlnmngr.Main;
import de.chaos.swlnmngr.config.CLIConfig;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Objects;
public class InstallRoute implements Route {
private static final String[] defaultFiles = new String[] {"default_config.json", "default_swlnmngr.bat", "default_swlnmngr.sh"};
@Override
public String getName() {
return "install";
}
@Override
public boolean route(String[] args) {
File installDir = CLIConfig.INSTALL_DIR;
if(!installDir.exists()) {
installDir.mkdir();
}
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);
} catch (IOException e) {
Main.getLogger().error("Could not create File", e);
return false;
}
}
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);
} catch (URISyntaxException e) {
Main.getLogger().error("Could parse Jar Location", e);
return false;
} catch (IOException e) {
Main.getLogger().error("Could not Copy JarFile", e);
return false;
}
return true;
}
}

Datei anzeigen

@ -1,5 +0,0 @@
package de.chaos.swlnmngr.route.routes;
public enum Routes {
}

Datei anzeigen

@ -0,0 +1,77 @@
package de.chaos.swlnmngr.route.routes;
import de.chaos.swlnmngr.Main;
import de.chaos.swlnmngr.config.Config;
import org.apache.http.Header;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class UpdateRoute implements Route {
@Override
public String getName() {
return "update";
}
@Override
public boolean route(String[] args) {
File defaultLibs = new File(Config.LIB_PATH, Config.DEFAULTS);
if (!defaultLibs.mkdirs()) {
for (File file : defaultLibs.listFiles()) {
file.delete();
}
}
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(
new AuthScope(Config.LIB_URL.getHost(), Config.LIB_URL.getPort()),
new UsernamePasswordCredentials(Config.USERNAME, Config.PASSWORD));
try (CloseableHttpClient client = HttpClients.custom()
.setDefaultCredentialsProvider(provider)
.build()) {
HttpGet httpGet = new HttpGet(Config.LIB_URL);
try (CloseableHttpResponse response = client.execute(httpGet)) {
Main.getLogger().debug(response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase());
for (Header allHeader : response.getAllHeaders()) {
Main.getLogger().debug(allHeader.getName() + ": " + allHeader.getValue());
}
Main.getLogger().info("Writing libs...");
ZipInputStream zis = new ZipInputStream(response.getEntity().getContent());
byte[] buffer = new byte[2048];
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
Path filePath = defaultLibs.toPath().resolve(entry.getName());
Main.getLogger().debug("Inflating {}...", entry.getName());
try (FileOutputStream fos = new FileOutputStream(filePath.toFile());
BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length)) {
int len;
while ((len = zis.read(buffer)) > 0) {
bos.write(buffer, 0, len);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}

Datei anzeigen

@ -3,7 +3,6 @@
"libPath": "~/libs/",
"defaultName": "default",
"libUrl": "https://steamwar.de/lib.php",
"additionalArgs": [],
"credentials": {
"username": "[EnterName]",
"password": "[EnterPW]"

Datei anzeigen

@ -0,0 +1 @@
java -jar ./SteamWarLinkManager.jar

Datei anzeigen

@ -0,0 +1,2 @@
#!/bin/bash
java -jar ./SteamWarLinkManager.jar

Datei anzeigen

@ -0,0 +1,11 @@
import de.chaos.swlnmngr.Main;
import java.util.Scanner;
public class RunWithArgs {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String arg = scanner.nextLine();
Main.main(arg.split(" "));
}
}

1
test.sh Normale Datei
Datei anzeigen

@ -0,0 +1 @@
java -jar target/SteamWarLinkManager.jar -i /home/chaos/IdeaProjects/swlnmngr/test/ install