From bdce8d04947387f5ef1fd8cba69f86cdc177559f Mon Sep 17 00:00:00 2001 From: cpw Date: Sun, 1 Sep 2019 14:40:39 -0400 Subject: [PATCH] Add a modfolderfactory. Fix up login payload exposure. Signed-off-by: cpw --- build.gradle | 2 +- .../loading/moddiscovery/ModDiscoverer.java | 3 +++ .../fml/network/FMLHandshakeHandler.java | 20 ++++++++----------- .../fml/network/FMLHandshakeMessages.java | 10 +++++++++- .../fml/network/simple/SimpleChannel.java | 12 +++++++++++ 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/build.gradle b/build.gradle index 12b03c6ed..3e6999e8d 100644 --- a/build.gradle +++ b/build.gradle @@ -408,7 +408,7 @@ project(':forge') { installer 'cpw.mods:grossjava9hacks:1.1.+' installer 'net.minecraftforge:accesstransformers:1.0.+:shadowed' installer 'net.minecraftforge:eventbus:1.0.+:service' - installer 'net.minecraftforge:forgespi:1.2.+' + installer 'net.minecraftforge:forgespi:1.3.+' installer 'net.minecraftforge:coremods:1.0.+' installer 'net.minecraftforge:unsafe:0.2.+' installer 'com.electronwill.night-config:core:3.6.0' diff --git a/src/fmllauncher/java/net/minecraftforge/fml/loading/moddiscovery/ModDiscoverer.java b/src/fmllauncher/java/net/minecraftforge/fml/loading/moddiscovery/ModDiscoverer.java index 49b596d7a..f9338208a 100644 --- a/src/fmllauncher/java/net/minecraftforge/fml/loading/moddiscovery/ModDiscoverer.java +++ b/src/fmllauncher/java/net/minecraftforge/fml/loading/moddiscovery/ModDiscoverer.java @@ -19,10 +19,12 @@ package net.minecraftforge.fml.loading.moddiscovery; +import cpw.mods.modlauncher.Launcher; import cpw.mods.modlauncher.ServiceLoaderStreamUtils; import net.minecraftforge.fml.loading.FMLLoader; import net.minecraftforge.fml.loading.LoadingModList; import net.minecraftforge.fml.loading.ModSorter; +import net.minecraftforge.forgespi.Environment; import net.minecraftforge.forgespi.locating.IModFile; import net.minecraftforge.forgespi.locating.IModLocator; import org.apache.logging.log4j.LogManager; @@ -63,6 +65,7 @@ public class ModDiscoverer { private final List locatorList; public ModDiscoverer(Map arguments) { + Launcher.INSTANCE.environment().computePropertyIfAbsent(Environment.Keys.MODFOLDERFACTORY.get(), v->ModsFolderLocator::new); locators = ServiceLoader.load(IModLocator.class); locatorList = ServiceLoaderStreamUtils.toList(this.locators); locatorList.forEach(l->l.initArguments(arguments)); diff --git a/src/main/java/net/minecraftforge/fml/network/FMLHandshakeHandler.java b/src/main/java/net/minecraftforge/fml/network/FMLHandshakeHandler.java index df7401d01..83a5bc424 100644 --- a/src/main/java/net/minecraftforge/fml/network/FMLHandshakeHandler.java +++ b/src/main/java/net/minecraftforge/fml/network/FMLHandshakeHandler.java @@ -38,15 +38,11 @@ import org.apache.logging.log4j.MarkerManager; import com.google.common.collect.Maps; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.BiConsumer; +import java.util.function.IntSupplier; import java.util.function.Supplier; import static net.minecraftforge.registries.ForgeRegistry.REGISTRIES; @@ -142,7 +138,7 @@ public class FMLHandshakeHandler { * @param message type * @return A {@link BiConsumer} for use in message handling */ - public static BiConsumer> biConsumerFor(ThreeConsumer> consumer) + public static BiConsumer> biConsumerFor(ThreeConsumer> consumer) { return (m, c) -> ThreeConsumer.bindArgs(consumer, m, c).accept(getHandshake(c)); } @@ -157,7 +153,7 @@ public class FMLHandshakeHandler { * @param message type * @return A {@link BiConsumer} for use in message handling */ - public static BiConsumer> indexFirst(ThreeConsumer> next) + public static BiConsumer> indexFirst(ThreeConsumer> next) { final BiConsumer> loginIndexedMessageSupplierBiConsumer = biConsumerFor(FMLHandshakeHandler::handleIndexedMessage); return loginIndexedMessageSupplierBiConsumer.andThen(biConsumerFor(next)); @@ -194,12 +190,12 @@ public class FMLHandshakeHandler { LOGGER.debug(REGISTRIES, "Expecting {} registries: {}", ()->this.registriesToReceive.size(), ()->this.registriesToReceive); } - void handleIndexedMessage(MSG message, Supplier c) + void handleIndexedMessage(MSG message, Supplier c) { - LOGGER.debug(FMLHSMARKER, "Received client indexed reply {} of type {}", message.getLoginIndex(), message.getClass().getName()); - boolean removed = this.sentMessages.removeIf(i->i==message.getLoginIndex()); + LOGGER.debug(FMLHSMARKER, "Received client indexed reply {} of type {}", message.getAsInt(), message.getClass().getName()); + boolean removed = this.sentMessages.removeIf(i-> i == message.getAsInt()); if (!removed) { - LOGGER.error(FMLHSMARKER, "Recieved unexpected index {} in client reply", message.getLoginIndex()); + LOGGER.error(FMLHSMARKER, "Recieved unexpected index {} in client reply", message.getAsInt()); } } diff --git a/src/main/java/net/minecraftforge/fml/network/FMLHandshakeMessages.java b/src/main/java/net/minecraftforge/fml/network/FMLHandshakeMessages.java index e43317ffd..8d1958162 100644 --- a/src/main/java/net/minecraftforge/fml/network/FMLHandshakeMessages.java +++ b/src/main/java/net/minecraftforge/fml/network/FMLHandshakeMessages.java @@ -30,6 +30,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.IntSupplier; +import java.util.function.Supplier; import java.util.stream.Collectors; import javax.annotation.Nullable; @@ -38,7 +40,8 @@ import com.google.common.collect.Maps; public class FMLHandshakeMessages { - static class LoginIndexedMessage { + static class LoginIndexedMessage implements IntSupplier + { private int loginIndex; void setLoginIndex(final int loginIndex) { @@ -48,6 +51,11 @@ public class FMLHandshakeMessages int getLoginIndex() { return loginIndex; } + + @Override + public int getAsInt() { + return getLoginIndex(); + } } /** * Server to client "list of mods". Always first handshake message. diff --git a/src/main/java/net/minecraftforge/fml/network/simple/SimpleChannel.java b/src/main/java/net/minecraftforge/fml/network/simple/SimpleChannel.java index 912794031..030d68069 100644 --- a/src/main/java/net/minecraftforge/fml/network/simple/SimpleChannel.java +++ b/src/main/java/net/minecraftforge/fml/network/simple/SimpleChannel.java @@ -33,6 +33,7 @@ import java.util.Collections; import java.util.List; import java.util.function.BiConsumer; import java.util.function.Function; +import java.util.function.IntSupplier; import java.util.function.Supplier; public class SimpleChannel @@ -112,6 +113,14 @@ public class SimpleChannel context.getPacketDispatcher().sendPacket(instance.getChannelName(), toBuffer(msgToReply).getLeft()); } + /** + * Build a new MessageBuilder. The type should implement {@link java.util.function.IntSupplier} if it is a login + * packet. + * @param type Type of message + * @param id id in the indexed codec + * @param Type of type + * @return a MessageBuilder + */ public MessageBuilder messageBuilder(final Class type, int id) { return MessageBuilder.forType(this, type, id); } @@ -179,6 +188,9 @@ public class SimpleChannel message.setLoginIndexSetter(this.loginIndexSetter); } if (this.loginIndexGetter != null) { + if (!IntSupplier.class.isAssignableFrom(this.type)) { + throw new IllegalArgumentException("Login packet type that does not supply an index as an IntSupplier"); + } message.setLoginIndexGetter(this.loginIndexGetter); } if (this.loginPacketGenerators != null) {