From b2ebee3cb82cfd545a936dd206f428f42945b1f6 Mon Sep 17 00:00:00 2001 From: Moulberry Date: Wed, 18 Dec 2024 17:36:40 -0500 Subject: [PATCH] Prevent recording replay with incompatible mods --- .../com/moulberry/flashback/Flashback.java | 150 +++++++++++------- .../mixin/playback/MixinMinecraftServer.java | 31 ++++ 2 files changed, 125 insertions(+), 56 deletions(-) create mode 100644 src/main/java/com/moulberry/flashback/mixin/playback/MixinMinecraftServer.java diff --git a/src/main/java/com/moulberry/flashback/Flashback.java b/src/main/java/com/moulberry/flashback/Flashback.java index ba6f992..a77c263 100644 --- a/src/main/java/com/moulberry/flashback/Flashback.java +++ b/src/main/java/com/moulberry/flashback/Flashback.java @@ -66,9 +66,7 @@ import net.minecraft.FileUtil; import net.minecraft.Util; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.components.toasts.SystemToast; -import net.minecraft.client.gui.screens.PauseScreen; -import net.minecraft.client.gui.screens.Screen; -import net.minecraft.client.gui.screens.TitleScreen; +import net.minecraft.client.gui.screens.*; import net.minecraft.client.gui.screens.multiplayer.JoinMultiplayerScreen; import net.minecraft.client.player.AbstractClientPlayer; import net.minecraft.client.player.LocalPlayer; @@ -98,6 +96,7 @@ import net.minecraft.world.level.levelgen.WorldOptions; import net.minecraft.world.level.storage.LevelStorageSource; import net.minecraft.world.level.storage.PrimaryLevelData; import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -141,6 +140,7 @@ public class Flashback implements ModInitializer, ClientModInitializer { private static final List pendingReplaySave = new ArrayList<>(); private static final List pendingReplayRecovery = new ArrayList<>(); + private static List pendingUnsupportedModsForRecording = null; public static ResourceLocation createResourceLocation(String value) { return ResourceLocation.fromNamespaceAndPath("flashback", value); @@ -391,59 +391,8 @@ public class Flashback implements ModInitializer, ClientModInitializer { }); ClientTickEvents.START_CLIENT_TICK.register(minecraft -> { - if (unsupportedLoader.get() != null && canReplaceScreen(Minecraft.getInstance().screen)) { - String loaderName = unsupportedLoader.get(); - if (System.currentTimeMillis() > Flashback.getConfig().nextUnsupportedModLoaderWarning) { - String warning = String.format(""" - You are using an unsupported modloader: %s - - Do not report crashes, bugs or other issues to Flashback - - You will not receive support from Flashback - - If you need assistance, please contact %s - """, loaderName, loaderName); - - Minecraft.getInstance().setScreen(new UnsupportedLoaderScreen(Minecraft.getInstance().screen, - Component.literal("Flashback: Unsupported"), Component.literal(warning))); - } - unsupportedLoader.set(null); - } - - if (!pendingReplayRecovery.isEmpty() && canReplaceScreen(Minecraft.getInstance().screen)) { - Component title = Component.literal("Flashback: Recovery"); - Component description = Component.empty() - .append(Component.literal("Flashback has detected ").append(Component.literal("unfinished recordings\n").withStyle(ChatFormatting.YELLOW))) - .append(Component.literal("This is usually because the game closed unexpectedly while recording\n\n")) - .append(Component.literal("Unfortunately, up to 5 minutes of gameplay from the end of the recording may be lost\n\n").withStyle(ChatFormatting.RED) - .append(Component.literal("Would you like to try to recover the recording?").withStyle(ChatFormatting.GREEN))); - Minecraft.getInstance().setScreen(new RecoverRecordingsScreen(Minecraft.getInstance().screen, title, description, recover -> { - switch (recover) { - case RECOVER -> { - pendingReplaySave.addAll(pendingReplayRecovery); - pendingReplayRecovery.clear(); - } - case SKIP -> { - pendingReplayRecovery.clear(); - } - case DELETE -> { - TempFolderProvider.tryDeleteStaleFolders(TempFolderProvider.TempFolderType.RECORDING); - pendingReplayRecovery.clear(); - } - } - })); - } - - if (!pendingReplaySave.isEmpty()) { - Screen currentScreen = Minecraft.getInstance().screen; - if (canReplaceScreen(currentScreen)) { - Path recordFolder = pendingReplaySave.getFirst(); - - LocalDateTime dateTime = LocalDateTime.now(); - dateTime = dateTime.withNano(0); - Minecraft.getInstance().setScreen(new SaveReplayScreen(Minecraft.getInstance().screen, - recordFolder, dateTime.toString())); - } + if (canReplaceScreen(Minecraft.getInstance().screen)) { + openNewScreen(unsupportedLoader); } if (Minecraft.getInstance().level != null && delayedStartRecording > 0) { @@ -481,6 +430,77 @@ public class Flashback implements ModInitializer, ClientModInitializer { } } + private static void openNewScreen(AtomicReference unsupportedLoader) { + if (unsupportedLoader.get() != null) { + String loaderName = unsupportedLoader.get(); + unsupportedLoader.set(null); + if (System.currentTimeMillis() > Flashback.getConfig().nextUnsupportedModLoaderWarning) { + String warning = String.format(""" + You are using an unsupported modloader: %s + + Do not report crashes, bugs or other issues to Flashback + + You will not receive support from Flashback + + If you need assistance, please contact %s + """, loaderName, loaderName); + + Minecraft.getInstance().setScreen(new UnsupportedLoaderScreen(Minecraft.getInstance().screen, + Component.literal("Flashback: Unsupported"), Component.literal(warning))); + return; + } + } + + if (!pendingReplayRecovery.isEmpty()) { + Component title = Component.literal("Flashback: Recovery"); + Component description = Component.empty() + .append(Component.literal("Flashback has detected ").append(Component.literal("unfinished recordings\n").withStyle(ChatFormatting.YELLOW))) + .append(Component.literal("This is usually because the game closed unexpectedly while recording\n\n")) + .append(Component.literal("Unfortunately, up to 5 minutes of gameplay from the end of the recording may be lost\n\n").withStyle(ChatFormatting.RED) + .append(Component.literal("Would you like to try to recover the recording?").withStyle(ChatFormatting.GREEN))); + Minecraft.getInstance().setScreen(new RecoverRecordingsScreen(Minecraft.getInstance().screen, title, description, recover -> { + switch (recover) { + case RECOVER -> { + pendingReplaySave.addAll(pendingReplayRecovery); + pendingReplayRecovery.clear(); + } + case SKIP -> { + pendingReplayRecovery.clear(); + } + case DELETE -> { + TempFolderProvider.tryDeleteStaleFolders(TempFolderProvider.TempFolderType.RECORDING); + pendingReplayRecovery.clear(); + } + } + })); + return; + } + + if (!pendingReplaySave.isEmpty()) { + Path recordFolder = pendingReplaySave.getFirst(); + + LocalDateTime dateTime = LocalDateTime.now(); + dateTime = dateTime.withNano(0); + Minecraft.getInstance().setScreen(new SaveReplayScreen(Minecraft.getInstance().screen, + recordFolder, dateTime.toString())); + return; + } + + if (pendingUnsupportedModsForRecording != null) { + String mods = StringUtils.join(pendingUnsupportedModsForRecording, ", "); + String description = """ + You have mods which are known to cause issues when recording replays + Please remove the following mods in order to be able to record replays: + + """; + Screen screen = Minecraft.getInstance().screen; + Minecraft.getInstance().setScreen(new AlertScreen(() -> Minecraft.getInstance().setScreen(screen), + Component.literal("Incompatible Mods"), Component.literal(description).append(Component.literal(mods).withStyle(ChatFormatting.RED)))); + pendingUnsupportedModsForRecording = null; + return; + } + } + public static List getReplayIncompatibleMods() { List incompatible = new ArrayList<>(); if (FabricLoader.getInstance().isModLoaded("vmp")) { @@ -492,6 +512,17 @@ public class Flashback implements ModInitializer, ClientModInitializer { return incompatible; } + public static List getRecordingIncompatibleMods() { + List incompatible = new ArrayList<>(); + if (FabricLoader.getInstance().isModLoaded("farsight")) { + incompatible.add("Farsight"); + } + if (incompatible.isEmpty()) { + return null; + } + return incompatible; + } + private static @Nullable String findUnsupportedLoaders() { if (FabricLoader.getInstance().isModLoaded("feather")) { return "Feather Client"; @@ -692,6 +723,13 @@ public class Flashback implements ModInitializer, ClientModInitializer { Component.literal("Already Recording"), Component.literal("Cannot start new recording when already recording")); return; } + + List unsupported = getRecordingIncompatibleMods(); + if (unsupported != null && !unsupported.isEmpty()) { + pendingUnsupportedModsForRecording = unsupported; + return; + } + RECORDER = new Recorder(Minecraft.getInstance().player.registryAccess()); if (Flashback.getConfig().showRecordingToasts) { SystemToast.add(Minecraft.getInstance().getToasts(), FlashbackSystemToasts.RECORDING_TOAST, diff --git a/src/main/java/com/moulberry/flashback/mixin/playback/MixinMinecraftServer.java b/src/main/java/com/moulberry/flashback/mixin/playback/MixinMinecraftServer.java new file mode 100644 index 0000000..aba8ccc --- /dev/null +++ b/src/main/java/com/moulberry/flashback/mixin/playback/MixinMinecraftServer.java @@ -0,0 +1,31 @@ +package com.moulberry.flashback.mixin.playback; + +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import com.moulberry.flashback.playback.ReplayServer; +import net.minecraft.core.Registry; +import net.minecraft.core.RegistryAccess; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.packs.resources.ResourceManager; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +import java.util.List; + +@Mixin(MinecraftServer.class) +public class MixinMinecraftServer { + + /* + * Force the server to use the pending tags we gathered instead of trying + * to load them from the resource pack repository where it doesn't exist + */ + + @WrapOperation(method = "method_29437", at = @At(value = "INVOKE", target = "Lnet/minecraft/tags/TagLoader;loadTagsForExistingRegistries(Lnet/minecraft/server/packs/resources/ResourceManager;Lnet/minecraft/core/RegistryAccess;)Ljava/util/List;")) + public List> loadTagsForExistingRegistries(ResourceManager resourceManager, RegistryAccess registryAccess, Operation>> original) { + if ((Object) this instanceof ReplayServer replayServer && replayServer.overridePendingTags != null) { + return replayServer.overridePendingTags; + } + return original.call(resourceManager, registryAccess); + } + +}