|
| 1 | +package net.errorcraft.codecium.mixin.minecraft.util; |
| 2 | + |
| 3 | +import com.llamalad7.mixinextras.sugar.Local; |
| 4 | +import net.errorcraft.codecium.access.minecraft.util.InvalidIdentifierExceptionAccess; |
| 5 | +import net.minecraft.util.Identifier; |
| 6 | +import net.minecraft.util.InvalidIdentifierException; |
| 7 | +import org.spongepowered.asm.mixin.Final; |
| 8 | +import org.spongepowered.asm.mixin.Mixin; |
| 9 | +import org.spongepowered.asm.mixin.Shadow; |
| 10 | +import org.spongepowered.asm.mixin.Unique; |
| 11 | +import org.spongepowered.asm.mixin.injection.At; |
| 12 | +import org.spongepowered.asm.mixin.injection.ModifyArg; |
| 13 | +import org.spongepowered.asm.mixin.injection.Redirect; |
| 14 | + |
| 15 | +import java.util.function.Supplier; |
| 16 | + |
| 17 | +@Mixin(Identifier.class) |
| 18 | +public abstract class IdentifierExtender { |
| 19 | + @Shadow |
| 20 | + @Final |
| 21 | + public static char NAMESPACE_SEPARATOR; |
| 22 | + |
| 23 | + @Shadow |
| 24 | + private static boolean isNamespaceCharacterValid(char character) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | + @Shadow |
| 29 | + public static boolean isPathCharacterValid(char character) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + @ModifyArg( |
| 34 | + method = "validate", |
| 35 | + at = @At( |
| 36 | + value = "INVOKE", |
| 37 | + target = "Lcom/mojang/serialization/DataResult;error(Ljava/util/function/Supplier;)Lcom/mojang/serialization/DataResult;", |
| 38 | + remap = false |
| 39 | + ) |
| 40 | + ) |
| 41 | + private static Supplier<String> identifierExceptionUseBetterMessage(Supplier<String> message, @Local(argsOnly = true) String id, @Local InvalidIdentifierException exception) { |
| 42 | + return () -> ((InvalidIdentifierExceptionAccess) exception).codecium$messageWithoutId() + ": " + id; |
| 43 | + } |
| 44 | + |
| 45 | + @Redirect( |
| 46 | + method = "validateNamespace", |
| 47 | + at = @At( |
| 48 | + value = "INVOKE", |
| 49 | + target = "Lnet/minecraft/util/Identifier;isNamespaceValid(Ljava/lang/String;)Z" |
| 50 | + ) |
| 51 | + ) |
| 52 | + @Unique |
| 53 | + private static boolean validateNamespaceUseBetterMessage(String namespace, String path) { |
| 54 | + for (int i = 0; i < namespace.length(); ++i) { |
| 55 | + if (!isNamespaceCharacterValid(namespace.charAt(i))) { |
| 56 | + InvalidIdentifierException exception = new InvalidIdentifierException("Invalid character '" + namespace.charAt(i) + "' in namespace of resource location"); |
| 57 | + ((InvalidIdentifierExceptionAccess) exception).codecium$setGivenIdentifier(namespace + NAMESPACE_SEPARATOR + path); |
| 58 | + throw exception; |
| 59 | + } |
| 60 | + } |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + @Redirect( |
| 65 | + method = "validatePath", |
| 66 | + at = @At( |
| 67 | + value = "INVOKE", |
| 68 | + target = "Lnet/minecraft/util/Identifier;isPathValid(Ljava/lang/String;)Z" |
| 69 | + ) |
| 70 | + ) |
| 71 | + private static boolean validatePathUseBetterMessage(String path, String namespace) { |
| 72 | + for (int i = 0; i < path.length(); ++i) { |
| 73 | + if (!isPathCharacterValid(path.charAt(i))) { |
| 74 | + InvalidIdentifierException exception = new InvalidIdentifierException("Invalid character '" + path.charAt(i) + "' in path of resource location"); |
| 75 | + ((InvalidIdentifierExceptionAccess) exception).codecium$setGivenIdentifier(namespace + NAMESPACE_SEPARATOR + path); |
| 76 | + throw exception; |
| 77 | + } |
| 78 | + } |
| 79 | + return true; |
| 80 | + } |
| 81 | +} |
0 commit comments