better patch

totallynotskidded from devious sex mod but not really cuz its just similar code
This commit is contained in:
Chayapak 2023-08-18 12:05:47 +07:00
parent 994cbba173
commit 6ea08ab4dc

View file

@ -3,10 +3,12 @@ package land.chipmunk.chipmunkmod.mixin;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
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.CallbackInfoReturnable;
@ -15,34 +17,46 @@ import java.lang.reflect.Type;
@Mixin(Text.Serializer.class)
public class TextSerializerMixin {
@Inject(method = "deserialize(Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lnet/minecraft/text/MutableText;", at = @At("HEAD"), cancellable = true)
private void deserialize (JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext, CallbackInfoReturnable<MutableText> cir) {
try {
if (!jsonElement.isJsonArray()) return;
@Unique private static final int LIMIT = 128;
final JsonArray array = jsonElement.getAsJsonArray();
@Unique private int i;
if (array.size() == 0) return;
@Unique
private boolean checkDepth (JsonElement element) {
if (element.isJsonPrimitive()) return false;
else if (i >= LIMIT) return true;
JsonElement item = array.get(0);
if (element.isJsonArray()) {
i++;
int depth = 0;
for (JsonElement item : element.getAsJsonArray()) if (checkDepth(item)) return true;
} else if (element.isJsonObject()) {
final JsonObject object = element.getAsJsonObject();
while (item != null && item.isJsonArray() && depth < 512) {
if (item.getAsJsonArray().size() == 0 && item.getAsJsonArray().get(0) == null) continue;
JsonArray array;
depth++;
if (object.has("extra")) array = object.get("extra").getAsJsonArray();
else if (object.has("with")) array = object.get("with").getAsJsonArray();
else return false;
item = item.getAsJsonArray().get(0);
i++;
for (JsonElement member : array) if (checkDepth(member)) return true;
}
if (depth >= 512) {
return false;
}
@Inject(method = "deserialize(Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lnet/minecraft/text/MutableText;", at = @At("HEAD"), cancellable = true)
private void deserialize (JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext, CallbackInfoReturnable<MutableText> cir) {
i = 0; // better way to do this?
final boolean overLimit = checkDepth(jsonElement);
if (!overLimit) return;
cir.setReturnValue(Text.literal("Component is too big").formatted(Formatting.RED));
cir.cancel();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}