parse cli arguments

This commit is contained in:
Bixilon 2020-11-20 14:02:27 +01:00
parent a904d69926
commit f44cf53268
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
8 changed files with 95 additions and 16 deletions

View File

@ -77,5 +77,10 @@
<artifactId>jcl-core</artifactId>
<version>2.8</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>

View File

@ -29,6 +29,7 @@ import de.bixilon.minosoft.modding.loading.ModLoader;
import de.bixilon.minosoft.modding.loading.Priorities;
import de.bixilon.minosoft.protocol.protocol.LANServerListener;
import de.bixilon.minosoft.util.CountUpAndDownLatch;
import de.bixilon.minosoft.util.MinosoftCommandLineArguments;
import de.bixilon.minosoft.util.Util;
import de.bixilon.minosoft.util.mojang.api.MojangAccount;
import de.bixilon.minosoft.util.task.AsyncTaskWorker;
@ -53,6 +54,7 @@ public final class Minosoft {
public static Configuration config;
public static void main(String[] args) {
MinosoftCommandLineArguments.parseCommandLineArguments(args);
Log.info("Starting...");
AsyncTaskWorker taskWorker = new AsyncTaskWorker("StartUp");

View File

@ -33,19 +33,19 @@ public class Configuration {
private final Object lock = new Object();
public Configuration() throws IOException, ConfigMigrationException {
File file = new File(StaticConfiguration.HOME_DIR + "config/" + StaticConfiguration.CONFIG_FILENAME);
File file = new File(StaticConfiguration.HOME_DIRECTORY + "config/" + StaticConfiguration.CONFIG_FILENAME);
if (!file.exists()) {
// no configuration file
InputStream input = getClass().getResourceAsStream("/config/" + StaticConfiguration.CONFIG_FILENAME);
if (input == null) {
throw new FileNotFoundException(String.format("[Config] Missing default config: %s!", StaticConfiguration.CONFIG_FILENAME));
}
File folder = new File(StaticConfiguration.HOME_DIR + "config/");
File folder = new File(StaticConfiguration.HOME_DIRECTORY + "config/");
if (!folder.exists() && !folder.mkdirs()) {
throw new IOException("[Config] Could not create config folder!");
}
Files.copy(input, Paths.get(file.getAbsolutePath()));
file = new File(StaticConfiguration.HOME_DIR + "config/" + StaticConfiguration.CONFIG_FILENAME);
file = new File(StaticConfiguration.HOME_DIRECTORY + "config/" + StaticConfiguration.CONFIG_FILENAME);
}
config = Util.readJsonFromFile(file.getAbsolutePath());
int configVersion = getInt(ConfigurationPaths.IntegerPaths.GENERAL_CONFIG_VERSION);
@ -67,7 +67,7 @@ public class Configuration {
}
}
// write config to temp file, delete original config, rename temp file to original file to avoid conflicts if minosoft gets closed while saving the config
File tempFile = new File(StaticConfiguration.HOME_DIR + "config/" + StaticConfiguration.CONFIG_FILENAME + ".tmp");
File tempFile = new File(StaticConfiguration.HOME_DIRECTORY + "config/" + StaticConfiguration.CONFIG_FILENAME + ".tmp");
Gson gson = new GsonBuilder().setPrettyPrinting().create();
FileWriter writer;
try {

View File

@ -18,13 +18,13 @@ import de.bixilon.minosoft.util.OSUtil;
import java.io.File;
public class StaticConfiguration {
public static final String CONFIG_FILENAME = "config.json"; // Filename of minosoft's base configuration (located in AppData/Minosoft/config)
public static final boolean SKIP_MOJANG_AUTHENTICATION = false; // disables all connections to mojang
public static final boolean COLORED_LOG = true; // the log should be colored with ANSI (does not affect base components)
public static final boolean LOG_RELATIVE_TIME = false; // prefix all log messages with the relative start time in milliseconds instead of the formatted time
public static final boolean VERBOSE_ENTITY_META_DATA_LOGGING = true; // if true, the entity meta data is getting serial
public static String CONFIG_FILENAME = "config.json"; // Filename of minosoft's base configuration (located in AppData/Minosoft/config)
public static boolean SKIP_MOJANG_AUTHENTICATION = false; // disables all connections to mojang
public static boolean COLORED_LOG = true; // the log should be colored with ANSI (does not affect base components)
public static boolean LOG_RELATIVE_TIME = false; // prefix all log messages with the relative start time in milliseconds instead of the formatted time
public static boolean VERBOSE_ENTITY_META_DATA_LOGGING = false; // if true, the entity meta data is getting serial
public static final String HOME_DIR;
public static String HOME_DIRECTORY;
static {
// Sets Config.homeDir to the correct folder per OS
@ -44,6 +44,6 @@ public class StaticConfiguration {
// failed creating folder
throw new RuntimeException(String.format("Could not create home folder (%s)!", homeDir));
}
HOME_DIR = folder.getAbsolutePath() + "/";
HOME_DIRECTORY = folder.getAbsolutePath() + "/";
}
}

View File

@ -302,6 +302,6 @@ public class AssetsManager {
if (hash == null) {
throw new FileNotFoundException("Could not find asset with hash: null");
}
return StaticConfiguration.HOME_DIR + String.format("assets/objects/%s/%s.gz", hash.substring(0, 2), hash);
return StaticConfiguration.HOME_DIRECTORY + String.format("assets/objects/%s/%s.gz", hash.substring(0, 2), hash);
}
}

View File

@ -41,7 +41,7 @@ public class ModLoader {
// load all jars, parse the mod.json
// sort the list and prioritize
// load all lists and dependencies async
File[] files = new File(StaticConfiguration.HOME_DIR + "mods").listFiles();
File[] files = new File(StaticConfiguration.HOME_DIRECTORY + "mods").listFiles();
if (files == null) {
// no mods to load
return;

View File

@ -0,0 +1,72 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util;
import de.bixilon.minosoft.config.StaticConfiguration;
import org.apache.commons.cli.*;
public class MinosoftCommandLineArguments {
public static void parseCommandLineArguments(String[] args) {
Options options = new Options();
Option help = new Option("?", "help", false, "Displays this help");
options.addOption(help);
Option homeFolder = new Option("home_folder", true, "Home of Minosoft");
options.addOption(homeFolder);
Option coloredLog = new Option("colored_log", true, "Should the log be colored");
options.addOption(coloredLog);
Option verboseEntityLogLevel = new Option("verbose_entity_logging", true, "Should entity meta data be printed");
options.addOption(verboseEntityLogLevel);
Option relativeTimeLogging = new Option("log_time_relativ", true, "Should time in log timestamp be relative");
options.addOption(relativeTimeLogging);
HelpFormatter formatter = new HelpFormatter();
CommandLine commandLine;
try {
commandLine = new DefaultParser().parse(options, args);
} catch (ParseException e) {
System.out.println(e.getMessage());
formatter.printHelp("java -jar Minosoft.jar", options);
System.exit(1);
return;
}
if (commandLine.hasOption(help.getOpt())) {
formatter.printHelp("java -jar Minosoft.jar", options);
System.exit(1);
return;
}
if (commandLine.hasOption(homeFolder.getOpt())) {
StaticConfiguration.HOME_DIRECTORY = commandLine.getOptionValue(homeFolder.getOpt());
}
if (commandLine.hasOption(coloredLog.getOpt())) {
StaticConfiguration.COLORED_LOG = Boolean.parseBoolean(commandLine.getOptionValue(coloredLog.getOpt()));
}
if (commandLine.hasOption(verboseEntityLogLevel.getOpt())) {
StaticConfiguration.VERBOSE_ENTITY_META_DATA_LOGGING = Boolean.parseBoolean(commandLine.getOptionValue(verboseEntityLogLevel.getOpt()));
}
if (commandLine.hasOption(relativeTimeLogging.getOpt())) {
StaticConfiguration.LOG_RELATIVE_TIME = Boolean.parseBoolean(commandLine.getOptionValue(relativeTimeLogging.getOpt()));
}
}
}