Fix LaunchCommand

This commit is contained in:
Johannes Frohnmeyer 2022-02-06 14:16:12 +01:00
parent 9519ba6da3
commit de8fcbb267
Signed by: Johannes
GPG Key ID: E76429612C2929F4
6 changed files with 43 additions and 38 deletions

View File

@ -56,4 +56,9 @@ public class CommandArgs implements Iterable<String> {
public CommandArgs subArgs(boolean wrapped) {
return new CommandArgs(args.subList(1, args.size()), wrapped);
}
@Override
public String toString() {
return "[" + String.join(", ", args) + "]";
}
}

View File

@ -45,45 +45,29 @@ public class LaunchCommand extends BaseInstanceCommand {
@Override
protected void invoke(CommandArgs args, Path instancePath, InstanceMeta meta) throws IOException, InstanceLauncher.LaunchException {
if (args.length == 0) {
Utils.LOGGER.error("You must provide an instance name or path");
return;
}
String pArg = args.get(0);
Path instanceDir = Files.exists(Paths.get(pArg)) ? Paths.get(pArg) : MetaHolder.INSTANCE_DIR.resolve(pArg);
if (!Files.exists(instanceDir.resolve("instance.json"))) {
Utils.LOGGER.error("Not a valid instance");
return;
}
if (InstanceLock.isSetupLocked(instanceDir)) {
if (InstanceLock.isSetupLocked(instancePath)) {
Utils.LOGGER.error("This instance is still being set up");
return;
}
if (InstanceLock.isRunningLocked(instanceDir)) {
if (InstanceLock.isRunningLocked(instancePath)) {
Utils.LOGGER.error("This instance is already running");
return;
}
InstanceMeta instance;
try {
instance = Utils.loadObject(instanceDir.resolve("instance.json"), InstanceMeta.class);
} catch (IOException e) {
throw new IOException("Not a valid instance", e);
}
if (args.length > 1) {
if (instance.arguments == null) instance.arguments = new InstanceMeta.Arguments();
instance.arguments.client = instance.arguments.client == null ? new ArrayList<>() : new ArrayList<>(instance.arguments.client);
instance.arguments.server = instance.arguments.server == null ? new ArrayList<>() : new ArrayList<>(instance.arguments.server);
instance.arguments.jvm = instance.arguments.jvm == null ? new ArrayList<>() : new ArrayList<>(instance.arguments.jvm);
instance.arguments.client.addAll(args.after(0));
instance.arguments.server.addAll(args.after(0));
if (meta.arguments == null) meta.arguments = new InstanceMeta.Arguments();
meta.arguments.client = meta.arguments.client == null ? new ArrayList<>() : new ArrayList<>(meta.arguments.client);
meta.arguments.server = meta.arguments.server == null ? new ArrayList<>() : new ArrayList<>(meta.arguments.server);
meta.arguments.jvm = meta.arguments.jvm == null ? new ArrayList<>() : new ArrayList<>(meta.arguments.jvm);
meta.arguments.client.addAll(args.after(0));
meta.arguments.server.addAll(args.after(0));
}
Steps.reDownload(instanceDir, Steps.createProcessState());
Steps.reDownload(instancePath, Steps.createProcessState());
if (server) {
InstanceLauncher.launch(instanceDir, instance, InstanceLauncher.LaunchType.Server, restart, AccountManager.NULL_AUTH);
InstanceLauncher.launch(instancePath, meta, InstanceLauncher.LaunchType.Server, restart, AccountManager.NULL_AUTH);
}
else {
AccountManager.loadAccounts();
InstanceLauncher.launchClient(instanceDir, instance);
InstanceLauncher.launchClient(instancePath, meta);
}
}
}

View File

@ -26,12 +26,13 @@ public class FabricMetaApi {
}
}
public static FabricVersionLoaderInfo getLoaderVersion(VersionsListInfo gameVersion, String fabricVersion) throws IOException {
return Utils.downloadObject(META_URL + "v2/versions/loader/" + gameVersion.id + "/" + fabricVersion, FabricVersionLoaderInfo.WithMeta.class);
public static FabricVersionLoaderInfo getLoaderVersion(String gameVersion, String fabricVersion) throws IOException {
return Utils.downloadObject(META_URL + "v2/versions/loader/" + gameVersion + "/" + fabricVersion, FabricVersionLoaderInfo.WithMeta.class);
}
//TODO don't mutate the object to allow cache, then re-enable cache in McApi
public static void addFabric(VersionInfo version, String fabricVersion, FabricVersionInfoType type) throws IOException {
FabricVersionLoaderInfo ver = getLoaderVersion(version, fabricVersion);
FabricVersionLoaderInfo ver = getLoaderVersion(version.id, fabricVersion);
if (!(ver instanceof FabricVersionLoaderInfo.WithMeta verWithMeta)) throw new IOException("Doesn't hold metadata");
FabricVersionLoaderInfo.WithMeta.LauncherMeta meta = verWithMeta.launcherMeta;
if (meta.version != 1) throw new IOException("Unsupported fabric launcherMeta version: " + meta.version);

View File

@ -24,7 +24,7 @@ public class McApi {
}
public static VersionInfo getVersionInfo(VersionsListInfo listInfo) throws IOException {
return downloadObject(listInfo.url, listInfo.sha1, VersionInfo.class);
return downloadObject(listInfo.url, listInfo.sha1, VersionInfo.class, false);
}
public static AssetIndex getAssetIndex(VersionInfo info) throws IOException, URISyntaxException {

View File

@ -3,6 +3,7 @@ package io.gitlab.jfronny.inceptum.util.mds;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
//Mirrors java.util.concurrent.Executors.DefaultThreadFactory
public class NamedThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
@ -10,8 +11,7 @@ public class NamedThreadFactory implements ThreadFactory {
private final String namePrefix;
public NamedThreadFactory(String name) {
SecurityManager s = System.getSecurityManager();
this.group = s != null ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
this.group = Thread.currentThread().getThreadGroup();
this.namePrefix = name + "-pool-" + poolNumber.getAndIncrement() + "-thread-";
}

View File

@ -46,20 +46,35 @@ public class Utils {
}
public static <T> T downloadObject(String url, Class<T> type) throws IOException {
return downloadObject(url, () -> HttpUtils.get(url).sendString(), type);
return downloadObject(url, type, true);
}
public static <T> T downloadObject(String url, Class<T> type, boolean cache) throws IOException {
return downloadObject(url, () -> HttpUtils.get(url).sendString(), type, cache);
}
public static <T> T downloadObject(String url, Type type) throws IOException {
return downloadObject(url, () -> HttpUtils.get(url).sendString(), type);
return downloadObject(url, type, true);
}
public static <T> T downloadObject(String url, Type type, boolean cache) throws IOException {
return downloadObject(url, () -> HttpUtils.get(url).sendString(), type, cache);
}
public static <T> T downloadObject(String url, String sha1, Class<T> type) throws IOException {
return downloadObject(url, () -> downloadString(url, sha1), type);
return downloadObject(url, sha1, type, true);
}
private static <T> T downloadObject(String url, ThrowingSupplier<String, Exception> sourceString, Type type) throws IOException {
public static <T> T downloadObject(String url, String sha1, Class<T> type, boolean cache) throws IOException {
return downloadObject(url, () -> downloadString(url, sha1), type, cache);
}
private static <T> T downloadObject(String url, ThrowingSupplier<String, Exception> sourceString, Type type, boolean cache) throws IOException {
try {
return OBJECT_CACHE.get(HashUtils.sha1(url.getBytes(StandardCharsets.UTF_8)), () -> GsonHolder.getGson().fromJson(sourceString.get(), type), type);
ThrowingSupplier<T, Exception> builder = () -> GsonHolder.getGson().fromJson(sourceString.get(), type);
if (cache)
return OBJECT_CACHE.get(HashUtils.sha1(url.getBytes(StandardCharsets.UTF_8)), builder, type);
return builder.get();
} catch (Exception e) {
throw new IOException("Could not download object and no cache exists", e);
}