Skip to content

Commit

Permalink
feat: offline user name config
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNuclearNexus committed Jul 20, 2024
1 parent 61bd902 commit 04b601f
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ default <T> T get(Property<T> property, T defaultValue) {
return getValue(property, () -> defaultValue);
}

// Implement a setter function for config properties, this allows for overwriting
// the values of them from the cli which enables per invocation configuration.
<T> T setValue(Property<T> property, Supplier<T> value);

default <T> T set(Property<T> property, T value) { return setValue(property, () -> value); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public <T> T getValue(Property<T> property, Supplier<T> defaultValue) {
return result == null ? defaultValue.get() : result;
}

@Override
public <T> T setValue(Property<T> property, Supplier<T> value) {
return (T)properties.setProperty(property.getName(), value.get().toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public interface LauncherProperties extends HmcProperties {

Property<String> FABRIC_URL = string("hmc.fabric.url");
Property<Boolean> OFFLINE = bool("hmc.offline");
Property<String> OFFLINE_USERNAME = string("hmc.offline.username");
Property<String> OFFLINE_UUID = string("hmc.offline.uuid");
Property<Boolean> RE_THROW_LAUNCH_EXCEPTIONS = bool("hmc.rethrow.launch.exceptions");

// TODO: also check hashes for the libraries?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public Account login(Config config) throws AuthException {
}

if (offlineChecker.isOffline()) {
return new Account("Offline", OFFLINE_UUID, "", "", "", "");
val username = config.get(LauncherProperties.OFFLINE_USERNAME, "Offline");
val uuid = config.get(LauncherProperties.OFFLINE_UUID, OFFLINE_UUID);
return new Account(username, uuid, "", "", "", "");
}

log.warning("No valid account found!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public LaunchCommand(Launcher launcher) {
// TODO: is this really necessary?
args.put("-noout", "Doesn't print Minecrafts output to the console.");
args.put("-quit", "Quit HeadlessMc after launching the game.");
args.put("-username", "Set the username in offline mode");
args.put("--jvm", "Jvm args to use.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ public <T> T getValue(Property<T> property, Supplier<T> defaultValue) {
return result == null ? defaultValue.get() : result;
}

@Override
public <T> T setValue(Property<T> property, Supplier<T> value) {
return property.parse(System.setProperty(property.getName(), value.get().toString()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ public class LaunchOptions {
private final boolean noOut;
private final boolean noIn;
private final boolean inMemory;
private final String username;

public static class LaunchOptionsBuilder {
private LaunchOptionsBuilder() {
this.additionalJvmArgs = Collections.emptyList();
}

public LaunchOptionsBuilder parseFlags(
Launcher ctx, boolean quit, String... args) {
Launcher ctx, boolean quit, String... args) {
boolean lwjgl = flag(ctx, "-lwjgl", INVERT_LWJGL_FLAG, args);
// if offline only allow launching with the lwjgl flag!
if (!lwjgl && launcher.getAccountManager().getOfflineChecker().isOffline()) {
Expand All @@ -57,23 +58,24 @@ public LaunchOptionsBuilder parseFlags(
.paulscode(flag(ctx, "-paulscode", INVERT_PAULS_FLAG, args))
.noOut(quit || CommandUtil.hasFlag("-noout", args))
.parseJvmArgs(args)
.noIn(quit);
.noIn(quit)
.username(CommandUtil.getOption("-username", args));
}

public LaunchOptionsBuilder parseJvmArgs(String... args) {
String jvmArgs = CommandUtil.getOption("--jvm", args);
if (jvmArgs != null) {
this.additionalJvmArgs = new ArrayList<>(
Arrays.asList(CommandUtil.split(jvmArgs)));
Arrays.asList(CommandUtil.split(jvmArgs)));
}

return this;
}

private boolean flag(
HasConfig ctx, String flg, Property<Boolean> inv, String... args) {
HasConfig ctx, String flg, Property<Boolean> inv, String... args) {
return CommandUtil.hasFlag(flg, args)
^ ctx.getConfig().get(inv, false);
^ ctx.getConfig().get(inv, false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.zip.ZipFile;

@CustomLog
Expand All @@ -40,7 +37,14 @@ public Process run(LaunchOptions options)

public Process run(LaunchOptions options, Instrumentation instrumentation)
throws IOException, LaunchException, AuthException {

val launcher = options.getLauncher();

if (options.getUsername() != null) {
launcher.getConfig().set(LauncherProperties.OFFLINE_USERNAME, options.getUsername());
launcher.getConfig().set(LauncherProperties.OFFLINE_UUID, UUID.randomUUID().toString());
}

if (launcher.getAccountManager().getLastAccount() == null) {
launcher.getAccountManager().login(launcher.getConfig());
}
Expand Down

0 comments on commit 04b601f

Please sign in to comment.