Misc improvements

This commit is contained in:
JFronny 2021-11-24 19:53:06 +01:00
parent 85cb5cce71
commit e52c0277b6
No known key found for this signature in database
GPG Key ID: BEC5ACBBD4EE17E5
13 changed files with 181 additions and 59 deletions

View File

@ -16,7 +16,7 @@ repositories {
}
allprojects {
version "$project.ver" + (project.hasProperty('pipeline') ? "+" + project.getProperty('pipeline') : "")
version "$project.ver" + (project.hasProperty('pipeline') ? "-" + project.getProperty('pipeline') : "")
group 'io.gitlab.jfronny.inceptum'
}
@ -46,12 +46,11 @@ if (flavor == 'custom') {
dependencies {
implementation 'com.google.code.gson:gson:2.8.9'
implementation 'org.slf4j:slf4j-api:1.7.32'
implementation 'ch.qos.logback:logback-classic:1.2.6'
implementation 'ch.qos.logback:logback-classic:1.2.7'
implementation 'net.freeutils:jlhttp:2.6'
implementation 'org.eclipse.jgit:org.eclipse.jgit:5.13.0.202109080827-r'
implementation project(":wrapper")
implementation platform("org.lwjgl:lwjgl-bom:$lwjglVersion")
['', '-opengl', '-glfw'].each {
@ -81,6 +80,9 @@ processResources {
shadowJar {
archiveClassifier.set(flavorProp)
exclude "about.html"
exclude "plugin.properties"
exclude "META-INF/**"
}
publishing {

View File

@ -13,7 +13,8 @@ import io.gitlab.jfronny.inceptum.model.mojang.Rules;
import io.gitlab.jfronny.inceptum.util.MetaHolder;
import io.gitlab.jfronny.inceptum.util.Utils;
import io.gitlab.jfronny.inceptum.util.api.McApi;
import io.gitlab.jfronny.inceptum.windows.AlertWindow;
import io.gitlab.jfronny.inceptum.windows.dialog.AlertWindow;
import io.gitlab.jfronny.inceptum.windows.dialog.TextBoxWindow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -21,6 +22,8 @@ import java.io.IOException;
import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Scanner;
import java.util.function.Consumer;
//TODO allow instance sync through metadata
public class Inceptum {
@ -29,7 +32,7 @@ public class Inceptum {
public static final Path ASSETS_DIR = MetaHolder.BASE_PATH.resolve("assets");
public static final Path LIBRARIES_DIR = MetaHolder.BASE_PATH.resolve("libraries");
public static final Path NATIVES_DIR = MetaHolder.BASE_PATH.resolve("natives");
private static final Path CONFIG_PATH = MetaHolder.BASE_PATH.resolve("glaunch2.json");
private static final Path CONFIG_PATH = MetaHolder.BASE_PATH.resolve("inceptum.json");
public static final Path ACCOUNTS_PATH = MetaHolder.BASE_PATH.resolve("accounts.json");
public static final Path FORCE_LOAD_PATH = NATIVES_DIR.resolve("forceload");
public static final Logger LOGGER = LoggerFactory.getLogger("Inceptum");
@ -56,7 +59,15 @@ public class Inceptum {
LOGGER.info("Loading from " + MetaHolder.BASE_PATH);
}
if (!Files.exists(CONFIG_PATH.getParent())) Files.createDirectories(CONFIG_PATH.getParent());
if (!Files.exists(CONFIG_PATH)) Utils.writeObject(CONFIG_PATH, new Config());
if (!Files.exists(CONFIG_PATH)) {
Path gLaunch2 = MetaHolder.BASE_PATH.resolve("glaunch2.json");
if (Files.exists(gLaunch2)) {
Files.move(gLaunch2, CONFIG_PATH);
}
else {
Utils.writeObject(CONFIG_PATH, new Config());
}
}
CONFIG = Utils.loadObject(CONFIG_PATH, Config.class);
if (!Files.exists(CACHE_DIR)) Files.createDirectories(CACHE_DIR);
try {
@ -112,4 +123,14 @@ public class Inceptum {
if (IS_GUI) InceptumGui.WINDOWS.add(new AlertWindow(title, message, ok, cancel));
else if (!defaultCancel) ok.run();
}
public static void getInput(String prompt, String defaultValue, Consumer<String> ok, Runnable cancel) throws IOException {
if (IS_GUI) InceptumGui.WINDOWS.add(new TextBoxWindow(prompt, prompt, defaultValue, ok, cancel));
else {
Scanner scanner = new Scanner(System.in);
System.out.println(prompt);
System.out.print("> ");
ok.accept(scanner.nextLine());
}
}
}

View File

@ -6,6 +6,7 @@ import io.gitlab.jfronny.inceptum.util.ClientLauncher;
import io.gitlab.jfronny.inceptum.util.ProcessUtils;
import io.gitlab.jfronny.inceptum.util.ServerLauncher;
import io.gitlab.jfronny.inceptum.util.Utils;
import io.gitlab.jfronny.inceptum.util.api.account.AccountManager;
import java.io.IOException;
import java.nio.file.Files;
@ -14,7 +15,7 @@ import java.util.ArrayList;
public class LaunchCommand extends Command {
public LaunchCommand() {
super("Launches the game. Optionally specify \"server\" or \"client\". Non-blocking (batch commands will continue if this is ran)", "run", "launch", "start");
super("Launches the game. Optionally specify \"server\" [\"restart\"] or \"client\". Non-blocking (batch commands will continue if this is ran)", "run", "launch", "start");
}
@Override
@ -23,7 +24,23 @@ public class LaunchCommand extends Command {
Inceptum.LOGGER.error("You must provide an instance name or path");
return;
}
String pArg = args.last();
int pArgIndex;
boolean restart = false;
boolean server = false;
if (args.get(0).equals("server")) {
server = true;
if (args.length > 1 && args.get(1).equals("restart")) {
if (args.length == 2) {
Inceptum.LOGGER.error("You must provide an instance name or path");
return;
}
restart = true;
pArgIndex = 2;
}
else pArgIndex = 1;
}
else pArgIndex = args.get(0).equals("client") ? 1 : 0;
String pArg = args.get(pArgIndex);
Path instanceDir = Files.exists(Path.of(pArg)) ? Path.of(pArg) : Inceptum.INSTANCE_DIR.resolve(pArg);
if (!Files.exists(instanceDir.resolve("instance.json"))) {
Inceptum.LOGGER.error("Not a valid instance");
@ -53,10 +70,12 @@ public class LaunchCommand extends Command {
}
if (args.length > 2) {
instance.gameArgsCustom = instance.gameArgsCustom == null ? new ArrayList<>() : new ArrayList<>(instance.gameArgsCustom);
instance.gameArgsCustom.addAll(args.after(1));
instance.gameArgsCustom.addAll(args.after(pArgIndex));
}
if (server) ServerLauncher.launch(instanceDir, instance, restart);
else {
AccountManager.loadAccounts();
ClientLauncher.launch(instanceDir, instance);
}
if (args.length >= 2 && args.contains("server"))
ServerLauncher.launch(instanceDir, instance);
else ClientLauncher.launch(instanceDir, instance);
}
}

View File

@ -32,7 +32,7 @@ public class DownloadClientStep implements Step {
clientPath = clientPath.resolve(minecraftVersion + ".jar");
serverPath = serverPath.resolve(minecraftVersion + ".jar");
FabricInstallerVersion installerVersion = FabricMetaApi.getInstallerVersion();
installerPath = installerPath.resolve("fabric-installer-" + installerVersion.version + "-" + minecraftVersion + ".jar");
installerPath = installerPath.resolve("fabric-installer-" + installerVersion.version + "-" + info.loader().version() + "-" + minecraftVersion + ".jar");
if (!Files.exists(clientPath)) {
MojangFileDownload client = info.version().downloads.client;
info.setState("Downloading Client");

View File

@ -1,12 +1,10 @@
package io.gitlab.jfronny.inceptum.install.steps;
import io.gitlab.jfronny.inceptum.Inceptum;
import io.gitlab.jfronny.inceptum.InceptumGui;
import io.gitlab.jfronny.inceptum.install.SetupStepInfo;
import io.gitlab.jfronny.inceptum.install.Step;
import io.gitlab.jfronny.inceptum.model.inceptum.InstanceMeta;
import io.gitlab.jfronny.inceptum.util.Utils;
import io.gitlab.jfronny.inceptum.windows.AlertWindow;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;

View File

@ -28,8 +28,25 @@ public class ClientLauncher {
Inceptum.showError("You have not set up an account.\nDoing so is required to play Minecraft", "Not authenticated");
return;
}
AuthInfo authInfo = AccountManager.getSelectedAccount();
if (authInfo.equals(AccountManager.NULL_AUTH)) {
try {
Inceptum.getInput("User name", "Joe", name -> {
AuthInfo infoNew = new AuthInfo(name, authInfo.uuid(), authInfo.accessToken(), authInfo.userType());
launchI(path, instance, infoNew);
}, () -> launchI(path, instance, authInfo));
} catch (IOException e) {
Inceptum.showError("Failed to request input", e);
}
}
else launchI(path, instance, authInfo);
}
private static void launchI(Path path, InstanceMeta instance, AuthInfo authInfo) {
boolean found = false;
for (VersionsListInfo version : McApi.getVersions().versions) {
if (version.id.equals(instance.getMinecraftVersion())) {
found = true;
try {
List<String> args = new ArrayList<>();
VersionInfo info = McApi.getVersionInfo(version);
@ -38,9 +55,9 @@ public class ClientLauncher {
}
args.add(Objects.requireNonNullElseGet(instance.java, () ->
JvmUtils.getJvmMain(Inceptum.NATIVES_DIR
.resolve(info.javaVersion.component)
.resolve(Integer.toString(info.javaVersion.majorVersion)))
.toAbsolutePath().toString()));
.resolve(info.javaVersion.component)
.resolve(Integer.toString(info.javaVersion.majorVersion)))
.toAbsolutePath().toString()));
DownloadLibrariesStep.execute(info, new AtomicBoolean(false), new AtomicReference<>());
StringBuilder classPath = new StringBuilder();
for (ArtifactInfo artifact : VersionInfoLibraryResolver.getRelevant(info)) {
@ -48,7 +65,7 @@ public class ClientLauncher {
classPath.append(File.pathSeparatorChar);
}
classPath.append(Inceptum.LIBRARIES_DIR.resolve("net/minecraft/client").resolve(version.id + ".jar").toAbsolutePath());
if (info.arguments != null) args.addAll(parse(info.arguments.jvm, info, instance, classPath.toString(), path.toAbsolutePath().toString()));
if (info.arguments != null) args.addAll(parse(info.arguments.jvm, info, instance, classPath.toString(), path.toAbsolutePath().toString(), authInfo));
if (instance.minMem != null) args.add("-Xms" + instance.minMem);
if (instance.maxMem != null) args.add("-Xmx" + instance.maxMem);
if (instance.jvmArgsCustom != null) args.addAll(instance.jvmArgsCustom);
@ -58,10 +75,10 @@ public class ClientLauncher {
args.add("-Djava.library.path=" + Inceptum.NATIVES_DIR.resolve(instance.getMinecraftVersion()).toAbsolutePath());
}
args.add(info.mainClass);
if (info.arguments != null) args.addAll(parse(info.arguments.game, info, instance, classPath.toString(), path.toAbsolutePath().toString()));
if (info.arguments != null) args.addAll(parse(info.arguments.game, info, instance, classPath.toString(), path.toAbsolutePath().toString(), authInfo));
else if (info.minecraftArguments != null) {
for (String s : info.minecraftArguments.split(" ")) {
args.add(expandArg(s, info, instance, classPath.toString(), path.toAbsolutePath().toString()));
args.add(expandArg(s, info, instance, classPath.toString(), path.toAbsolutePath().toString(), authInfo));
}
}
else {
@ -80,20 +97,22 @@ public class ClientLauncher {
}
}
}
if (!found) {
Inceptum.showError("The version number specified for this instance could not be matched with a known version", "Could not launch client");
}
}
private static List<String> parse(List<MinecraftArgument> arguments, VersionInfo info, InstanceMeta instance, String classPath, String gameDirectory) {
private static List<String> parse(List<MinecraftArgument> arguments, VersionInfo info, InstanceMeta instance, String classPath, String gameDirectory, AuthInfo authInfo) {
List<String> res = new ArrayList<>();
for (MinecraftArgument argument : arguments) {
for (String s : argument.arg()) {
res.add(expandArg(s, info, instance, classPath, gameDirectory));
res.add(expandArg(s, info, instance, classPath, gameDirectory, authInfo));
}
}
return res;
}
private static String expandArg(String arg, VersionInfo info, InstanceMeta instance, String classPath, String gameDirectory) {
AuthInfo authInfo = AccountManager.getSelectedAccount();
private static String expandArg(String arg, VersionInfo info, InstanceMeta instance, String classPath, String gameDirectory, AuthInfo authInfo) {
return arg
// game args
.replace("${auth_player_name}", authInfo.name())

View File

@ -11,23 +11,22 @@ import io.gitlab.jfronny.inceptum.util.api.McApi;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
public class ServerLauncher {
public static void launch(Path path, InstanceMeta instance) {
public static void launch(Path path, InstanceMeta instance, boolean restart) {
boolean found = false;
for (VersionsListInfo version : McApi.getVersions().versions) {
if (version.id.equals(instance.getMinecraftVersion())) {
found = true;
try {
List<String> args = new ArrayList<>();
VersionInfo info = McApi.getVersionInfo(version);
@ -44,7 +43,7 @@ public class ServerLauncher {
classPath.append(File.pathSeparatorChar);
}
Path serverJar = instance.isFabric()
? Inceptum.LIBRARIES_DIR.resolve("net/fabricmc/fabric-installer").resolve("fabric-installer-" + FabricMetaApi.getInstallerVersion().version + "-" + version.id + ".jar")
? Inceptum.LIBRARIES_DIR.resolve("net/fabricmc/fabric-installer").resolve("fabric-installer-" + FabricMetaApi.getInstallerVersion().version + "-" + instance.getLoaderVersion() + "-" + version.id + ".jar")
: Inceptum.LIBRARIES_DIR.resolve("net/minecraft/server").resolve(version.id + ".jar");
classPath.append(serverJar.toAbsolutePath());
if (instance.minMem != null) args.add("-Xms" + instance.minMem);
@ -69,11 +68,40 @@ public class ServerLauncher {
pb.directory(path.toFile());
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
Files.writeString(path.resolve("inceptum.lock"), Long.toString(pb.start().pid()));
AtomicReference<Process> proc = new AtomicReference<>();
Runnable starterRunner = () -> {
try {
proc.set(pb.start());
Files.writeString(path.resolve("inceptum.lock"), Long.toString(proc.get().pid()));
} catch (IOException e) {
Inceptum.LOGGER.error("Could not start server", e);
}
};
starterRunner.run();
if (restart) {
new Thread(() -> {
while (true) {
try {
proc.get().waitFor();
} catch (InterruptedException e) {
Inceptum.LOGGER.error("Could not wait for server to finish", e);
}
Inceptum.LOGGER.info("Restarting server");
starterRunner.run();
if (!proc.get().isAlive()) {
Inceptum.LOGGER.error("Could not restart server");
return;
}
}
}).start();
}
} catch (IOException | URISyntaxException e) {
Inceptum.showError("Could not launch server", e);
}
}
}
if (!found) {
Inceptum.showError("The version number specified for this instance could not be matched with a known version", "Could not launch server");
}
}
}

View File

@ -14,7 +14,7 @@ public class AccountManager {
private static final Type abstractAccountListType = new TypeToken<List<MicrosoftAccount>>() {}.getType();
private static MicrosoftAccount SELECTED_ACCOUNT;
private static final List<MicrosoftAccount> ACCOUNTS = new ArrayList<>();
private static final AuthInfo NULL_AUTH = new AuthInfo("Joe", "2536abce90e8476a871679918164abc5", "99abe417230342cb8e9e2168ab46297a", "legacy");
public static final AuthInfo NULL_AUTH = new AuthInfo("Joe", "2536abce90e8476a871679918164abc5", "99abe417230342cb8e9e2168ab46297a", "legacy");
public static AuthInfo getSelectedAccount() {
if (accountMissing()) return NULL_AUTH;

View File

@ -49,7 +49,9 @@ public class InstanceView {
}
}
if (disabled) ImGui.beginDisabled();
if (ImGui.button(path.getFileName().toString())) ClientLauncher.launch(path, instance);
if (ImGui.button(path.getFileName().toString())) {
ClientLauncher.launch(path, instance);
}
ImGui.tableNextColumn();
if (ImGui.button("Edit##" + path)) InceptumGui.open(new InstanceEditWindow(path, instance));
if (disabled) ImGui.endDisabled();

View File

@ -1,7 +1,8 @@
package io.gitlab.jfronny.inceptum.windows;
package io.gitlab.jfronny.inceptum.windows.dialog;
import imgui.ImGui;
import imgui.flag.ImGuiWindowFlags;
import io.gitlab.jfronny.inceptum.windows.Window;
public class AlertWindow extends Window {
private final String message;
@ -23,13 +24,13 @@ public class AlertWindow extends Window {
public void draw() {
ImGui.text(message);
if (ImGui.button("OK")) {
close();
super.close();
if (onOk != null) onOk.run();
}
if (onOk != null || onCancel != null) {
ImGui.sameLine();
if (ImGui.button("Cancel")) {
close();
super.close();
if (onCancel != null) onCancel.run();
}
}
@ -39,4 +40,10 @@ public class AlertWindow extends Window {
public int getFlags() {
return super.getFlags() | ImGuiWindowFlags.NoSavedSettings;
}
@Override
public void close() {
super.close();
onCancel.run();
}
}

View File

@ -0,0 +1,45 @@
package io.gitlab.jfronny.inceptum.windows.dialog;
import imgui.ImGui;
import imgui.type.ImString;
import io.gitlab.jfronny.inceptum.windows.Window;
import java.util.function.Consumer;
public class TextBoxWindow extends Window {
private final String message;
private final Consumer<String> onOk;
private final Runnable onCancel;
private final ImString selected;
public TextBoxWindow(String name, String message, String defaultValue, Consumer<String> onOk, Runnable onCancel) {
super(name);
this.message = message;
this.onOk = onOk;
this.onCancel = onCancel;
selected = new ImString(defaultValue, 128);
}
@Override
public void draw() {
ImGui.text(message);
ImGui.inputText("##yes", selected);
if (ImGui.button("OK")) {
super.close();
if (onOk != null) onOk.accept(selected.get());
}
if (onOk != null || onCancel != null) {
ImGui.sameLine();
if (ImGui.button("Cancel")) {
super.close();
if (onCancel != null) onCancel.run();
}
}
}
@Override
public void close() {
super.close();
onCancel.run();
}
}

View File

@ -1,20 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config" packages="io.gitlab.jfronny.inceptum.util">
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
</Console>
<MapAppender name="MapAppender" />
<File name="FILE" fileName="run/logs/latest.log">
<PatternLayout pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Logger name="com.jcg" level="debug" />
<Root level="info">
<AppenderRef ref="STDOUT" />
<AppenderRef ref="FILE" />
<AppenderRef ref="MapAppender" />
</Root>
</Loggers>
</Configuration>

View File

@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configuration debug="false">
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{HH:mm:ss.SSS} %boldCyan(%thread) %boldGreen(%logger{0}) %highlight(%level) %msg%n</pattern>
</encoder>
</layout>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">