-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mode Validation Checks Between Client and Server
- Loading branch information
1 parent
11b9eff
commit 26fd909
Showing
7 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/com/nomiceu/nomilabs/mixin/vanilla/FMLHandshakeMessageModListMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.nomiceu.nomilabs.mixin.vanilla; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import net.minecraftforge.fml.common.ModContainer; | ||
import net.minecraftforge.fml.common.network.handshake.FMLHandshakeMessage; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
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 com.nomiceu.nomilabs.NomiLabs; | ||
import com.nomiceu.nomilabs.mixinhelper.AccessibleModListMessage; | ||
import com.nomiceu.nomilabs.util.LabsModeHelper; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
|
||
/** | ||
* Sends Labs Mode Data in Mod List Handshake. | ||
*/ | ||
@Mixin(value = FMLHandshakeMessage.ModList.class, remap = false) | ||
public class FMLHandshakeMessageModListMixin implements AccessibleModListMessage { | ||
|
||
@Shadow | ||
private Map<String, String> modTags; | ||
@Unique | ||
private static final String labs$modeId = "\0LABS_MODE_DATA\0"; | ||
|
||
@Unique | ||
private String labs$mode = ""; | ||
|
||
@Inject(method = "<init>(Ljava/util/List;)V", at = @At("TAIL")) | ||
private void initModeData(List<ModContainer> modList, CallbackInfo ci) { | ||
NomiLabs.LOGGER.info("[Labs Mode Validation] Injecting Mode Data into Mod List Handshake..."); | ||
labs$mode = LabsModeHelper.getMode(); | ||
|
||
modTags.put(labs$modeId, labs$mode); | ||
} | ||
|
||
@Inject(method = "fromBytes", at = @At("TAIL")) | ||
private void initModeFromBytes(ByteBuf buffer, CallbackInfo ci) { | ||
NomiLabs.LOGGER.info("[Labs Mode Validation] Reading Mode Data from Mod List Handshake..."); | ||
labs$mode = modTags.get(labs$modeId); | ||
|
||
if (labs$mode == null) { | ||
NomiLabs.LOGGER.error("[Labs Mode Validation] No Mode Data in Mod List Handshake!"); | ||
labs$mode = ""; | ||
} | ||
} | ||
|
||
@Unique | ||
@Override | ||
public String labs$getMode() { | ||
return labs$mode; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/nomiceu/nomilabs/mixin/vanilla/FMLNetworkHandlerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.nomiceu.nomilabs.mixin.vanilla; | ||
|
||
import net.minecraftforge.fml.common.network.handshake.FMLHandshakeMessage; | ||
import net.minecraftforge.fml.common.network.internal.FMLNetworkHandler; | ||
import net.minecraftforge.fml.relauncher.Side; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import com.nomiceu.nomilabs.NomiLabs; | ||
import com.nomiceu.nomilabs.config.LabsConfig; | ||
import com.nomiceu.nomilabs.mixinhelper.AccessibleModListMessage; | ||
import com.nomiceu.nomilabs.util.LabsModeHelper; | ||
|
||
/** | ||
* Checks Mode Data in Mod List Handshake. | ||
*/ | ||
@Mixin(value = FMLNetworkHandler.class, remap = false) | ||
public class FMLNetworkHandlerMixin { | ||
|
||
@Inject(method = "checkModList(Lnet/minecraftforge/fml/common/network/handshake/FMLHandshakeMessage$ModList;Lnet/minecraftforge/fml/relauncher/Side;)Ljava/lang/String;", | ||
at = @At("RETURN"), | ||
cancellable = true) | ||
private static void checkModeInfo(FMLHandshakeMessage.ModList modListPacket, Side side, | ||
CallbackInfoReturnable<String> cir) { | ||
if (cir.getReturnValue() != null) { | ||
NomiLabs.LOGGER.error("[Labs Mode Validation] Ignoring Mode Checks due to Invalid Mod List!"); | ||
return; | ||
} | ||
|
||
String mode = ((AccessibleModListMessage) modListPacket).labs$getMode(); | ||
if (mode.isEmpty()) { | ||
NomiLabs.LOGGER.error("[Labs Mode Validation] No Mode Data!"); | ||
cir.setReturnValue( | ||
"§cMode Validation Failed!§r\n\nCheck your client and server, and make sure they are on the same version!"); | ||
return; | ||
} | ||
|
||
if (mode.equals(LabsModeHelper.getMode())) { | ||
NomiLabs.LOGGER.info("[Labs Mode Validation] Mode Match."); | ||
return; | ||
} | ||
|
||
NomiLabs.LOGGER.error("[Labs Mode Validation] Mode Mismatch! Received: {}, Expected: {}", mode, | ||
LabsModeHelper.getMode()); | ||
|
||
String modeMismatchText; | ||
// Data coming from server | ||
if (side.isServer()) | ||
modeMismatchText = String.format("Server expects mode §a%s§r, but you are on mode §e%s§r!", | ||
LabsModeHelper.formatMode(mode), | ||
LabsModeHelper.getFormattedMode()); | ||
else | ||
modeMismatchText = String.format("Server expects mode §a%s§r, but you are on mode §e%s§r!", | ||
LabsModeHelper.getFormattedMode(), LabsModeHelper.formatMode(mode)); | ||
|
||
String linkText = ""; | ||
if (LabsConfig.advanced.modeCheckNomiCeuLink) | ||
linkText = "\n\nSee §bhttps://github.com/Nomi-CEu/Nomi-CEu/blob/main/overrides/README.md§r for information on switching modes!"; | ||
|
||
cir.setReturnValue("§cServer Mode Rejection:§r" + "\n\n" + modeMismatchText + linkText); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/nomiceu/nomilabs/mixinhelper/AccessibleModListMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.nomiceu.nomilabs.mixinhelper; | ||
|
||
public interface AccessibleModListMessage { | ||
|
||
String labs$getMode(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters