Skip to content

Commit

Permalink
use uuid of player for the folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Jab125 committed Oct 18, 2024
1 parent 25d8ac8 commit aad11eb
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 36 deletions.
13 changes: 8 additions & 5 deletions src/main/java/dev/jab125/hotjoin/HotJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.minecraft.client.server.IntegratedServer;
import org.apache.commons.io.file.PathUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import wily.legacy.client.screen.Assort;
Expand Down Expand Up @@ -337,16 +338,17 @@ public static <T, R extends Throwable> T crashgoByeBye(SupplierButBetter<T, R>
static int hotJoin(CommandContext<FabricClientCommandSource> a) {
HotJoin.canLaunchOtherwiseThrow();
try {
launchMinecraftClient(null, null, null);
launchMinecraftClient(null, null, null, null);
} catch (IOException e) {
throw new RuntimeException(e);
}
return 0;
}

// The goal is to launch Minecraft a second time, under a different directory.
public static UUID launchMinecraftClient(String compat, String magic, String legacy4jData) throws IOException {
public static UUID launchMinecraftClient(String compat, String magic, String legacy4jData, @Nullable String folderName) throws IOException {
HotJoin.canLaunchOtherwiseThrow();
if (folderName == null || folderName.isBlank()) folderName = "second";
if (magic != null) magic = magic.replace("=", "$");
if (legacy4jData != null) legacy4jData = legacy4jData.replace("=", "$");
UUID uuid = UUID.randomUUID();
Expand All @@ -362,9 +364,10 @@ public static UUID launchMinecraftClient(String compat, String magic, String leg
}
i++;
}
Path path = Path.of("second");
path.toFile().mkdirs();
transfer(path);
Path path = Path.of(".hotjoin-instances/" + folderName);
boolean directory = path.toFile().isDirectory();
if (!directory) path.toFile().mkdirs();
if (!directory) transfer(path);
launchArguments[i + 1] = path.toAbsolutePath().toString();
String cp = System.getProperty("java.class.path");
List<String> splitClassPath = Arrays.stream(cp.split(File.pathSeparator)).toList();
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/dev/jab125/hotjoin/api/HotJoinAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ public static UUID launchMinecraftClient(String compat, String magic) {
return launchMinecraftClient(compat, magic, null);
}
public static <T extends Throwable> UUID launchMinecraftClient(String compat, String magic, @Nullable String legacy4jData) throws T {
return launchMinecraftClient(compat, magic, legacy4jData, null);
}

public static <T extends Throwable> UUID launchMinecraftClient(String compat, String magic, @Nullable String legacy4jData, @Nullable String folderName) throws T {
try {
return HotJoin.launchMinecraftClient(compat, magic, legacy4jData);
return HotJoin.launchMinecraftClient(compat, magic, legacy4jData, folderName);
} catch (Throwable t) {
throw (T) t;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public int hotJoinAuthMeMicrosoft(CommandContext<FabricClientCommandSource> a) {
return 0;
}

private static void launchAuthMeClient(String magic) {
HotJoinAccess.launchMinecraftClient("authme", magic);
private static void launchAuthMeClient(String uuid, String magic) {
HotJoinAccess.launchMinecraftClient("authme", magic, null, uuid);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@
import java.util.Base64;
import java.util.HashMap;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import static dev.jab125.hotjoin.HotJoin.crashgoByeBye;

public class Legacy4JModCompat implements ILegacy4JModCompat {
// used by MCAccountMixin due to the lack of mutable static fields in interfaces
public static Consumer<String> authConsumer;
public static BiConsumer<String, String> authConsumer;
public static final HashMap<UUID, Legacy4JData> uuidLegacy4JMap = new HashMap<>();
public Legacy4JModCompat() {
ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
Expand All @@ -69,16 +70,16 @@ public static void openLegacy4JUserPicker() {
public static void openLegacy4JUserPicker(Legacy4JData data) {
if (data != null) ScreenUtil.getLegacyOptions().selectedController().set(data.controllerIndex());
ChooseUserScreen chooseUserScreen = new ChooseUserScreen(null);
((AuthCallback)chooseUserScreen).hotjoin$authResponse(s -> {
((AuthCallback)chooseUserScreen).hotjoin$authResponse((uuid, s) -> {
Minecraft.getInstance().getToasts().addToast(new LegacyTip(Component.literal("Success, joining world...")));
launchLegacy4jClient(s, data);
launchLegacy4jClient(s, data, uuid);
});
((AuthCallback) chooseUserScreen).hotjoin$legacy4jData(data);
Minecraft.getInstance().setScreen(chooseUserScreen);
}

@SuppressWarnings("unchecked")
private static <T extends Throwable> void launchLegacy4jClient(String magic, Legacy4JData legacy4JData) throws T {
private static <T extends Throwable> void launchLegacy4jClient(String magic, Legacy4JData legacy4JData, String _uuid) throws T {
String data;
if (legacy4JData != null) {
Tag tag = Legacy4JData.CODEC.encodeStart(NbtOps.INSTANCE, legacy4JData).resultOrPartial(HotJoin.LOGGER::error).orElseThrow();
Expand All @@ -92,7 +93,7 @@ private static <T extends Throwable> void launchLegacy4jClient(String magic, Leg
} else {
data = null;
}
UUID uuid = HotJoinAccess.launchMinecraftClient("legacy4j", magic, data);
UUID uuid = HotJoinAccess.launchMinecraftClient("legacy4j", magic, data, _uuid);
if (legacy4JData != null) uuidLegacy4JMap.put(uuid, legacy4JData);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import dev.jab125.hotjoin.HotJoin;
import dev.jab125.hotjoin.mixin.UserAccessor;
import dev.jab125.hotjoin.util.AuthCallback;
import dev.jab125.hotjoin.util.HotJoinCodecs;
import me.axieum.mcmod.authme.api.gui.AuthScreen;
Expand All @@ -21,14 +22,14 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Base64;
import java.util.function.Consumer;
import java.util.function.BiConsumer;

@Mixin(MicrosoftAuthScreen.class)
public abstract class MicrosoftAuthScreenMixin extends AuthScreen implements AuthCallback {
public MicrosoftAuthScreenMixin() {
super(null, null, null);
}
private @Unique @Nullable Consumer<String> authResponse;
private @Unique @Nullable BiConsumer<String, String> authResponse;

@Inject(method = "lambda$init$7", at = @At("HEAD"), cancellable = true)
void d(User session, CallbackInfo ci) {
Expand All @@ -37,7 +38,7 @@ void d(User session, CallbackInfo ci) {
Tag tag = HotJoinCodecs.USER_CODEC.encodeStart(NbtOps.INSTANCE, session).resultOrPartial(HotJoin.LOGGER::error).orElseThrow();
ByteArrayDataOutput byteArrayDataOutput = ByteStreams.newDataOutput();
NbtIo.write((CompoundTag) tag, byteArrayDataOutput);
authResponse.accept(Base64.getEncoder().encodeToString(byteArrayDataOutput.toByteArray()));
authResponse.accept(((UserAccessor)session).getUUID().toString(), Base64.getEncoder().encodeToString(byteArrayDataOutput.toByteArray()));
this.success = true;
ci.cancel();
return;
Expand All @@ -48,12 +49,12 @@ void d(User session, CallbackInfo ci) {
}

@Override
public void hotjoin$authResponse(Consumer<String> authConsumer) {
public void hotjoin$authResponse(BiConsumer<String, String> authConsumer) {
authResponse = authConsumer;
}

@Override
public Consumer<String> hotjoin$authResponse() {
public BiConsumer<String, String> hotjoin$authResponse() {
return authResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,30 @@

import dev.jab125.hotjoin.compat.legacy4j.Legacy4JData;
import dev.jab125.hotjoin.util.AuthCallback;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Renderable;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import wily.legacy.client.screen.ChooseUserScreen;
import wily.legacy.client.screen.Panel;
import wily.legacy.client.screen.PanelVListScreen;
import wily.legacy.client.screen.RenderableVListScreen;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.BiConsumer;

@Mixin(ChooseUserScreen.class)
public abstract class ChooseUserScreenMixin extends PanelVListScreen implements AuthCallback {
private @Unique @Nullable Consumer<String> authResponse;
private @Unique @Nullable BiConsumer<String, String> authResponse;

@SuppressWarnings("DataFlowIssue")
public ChooseUserScreenMixin() {
super(null, null);
}

@Override
public void hotjoin$authResponse(Consumer<String> authConsumer) {
public void hotjoin$authResponse(BiConsumer<String, String> authConsumer) {
this.authResponse = authConsumer;
}

@Override
public Consumer<String> hotjoin$authResponse() {
public BiConsumer<String, String> hotjoin$authResponse() {
return this.authResponse;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.io.ByteStreams;
import dev.jab125.hotjoin.HotJoin;
import dev.jab125.hotjoin.compat.legacy4j.Legacy4JModCompat;
import dev.jab125.hotjoin.mixin.UserAccessor;
import dev.jab125.hotjoin.util.AuthCallback;
import dev.jab125.hotjoin.util.HotJoinCodecs;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -43,7 +44,7 @@ private static void interceptSetUser(User user, CallbackInfo ci) throws IOExcept
Tag tag = HotJoinCodecs.USER_CODEC.encodeStart(NbtOps.INSTANCE, user).resultOrPartial(HotJoin.LOGGER::error).orElseThrow();
ByteArrayDataOutput byteArrayDataOutput = ByteStreams.newDataOutput();
NbtIo.write((CompoundTag) tag, byteArrayDataOutput);
Legacy4JModCompat.authConsumer.accept(Base64.getEncoder().encodeToString(byteArrayDataOutput.toByteArray()));
Legacy4JModCompat.authConsumer.accept(((UserAccessor)user).getUUID().toString(), Base64.getEncoder().encodeToString(byteArrayDataOutput.toByteArray()));
Legacy4JModCompat.authConsumer = null;
ci.cancel();
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dev/jab125/hotjoin/util/AuthCallback.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package dev.jab125.hotjoin.util;

import java.util.function.Consumer;
import java.util.function.BiConsumer;

public interface AuthCallback {
void hotjoin$authResponse(Consumer<String> authConsumer);
void hotjoin$authResponse(BiConsumer<String /*uuid*/, String /*magic*/> authConsumer);

Consumer<String> hotjoin$authResponse();
BiConsumer<String, String> hotjoin$authResponse();

@SuppressWarnings("UnnecessaryReturnStatement")
default void hotjoin$legacy4jData(Object object) {
Expand Down

0 comments on commit aad11eb

Please sign in to comment.