From 994cbba173c0b138276d611bba7f6d87036ccb30 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Thu, 17 Aug 2023 19:08:13 +0700 Subject: [PATCH] patch stack overflow crash (mabe) and fix --- .../mixin/TextSerializerMixin.java | 48 +++++++++++++++++++ .../chipmunkmod/modules/SelfCare.java | 9 +++- src/main/resources/chipmunkmod.mixins.json | 3 +- 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/main/java/land/chipmunk/chipmunkmod/mixin/TextSerializerMixin.java diff --git a/src/main/java/land/chipmunk/chipmunkmod/mixin/TextSerializerMixin.java b/src/main/java/land/chipmunk/chipmunkmod/mixin/TextSerializerMixin.java new file mode 100644 index 0000000..8c168b2 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkmod/mixin/TextSerializerMixin.java @@ -0,0 +1,48 @@ +package land.chipmunk.chipmunkmod.mixin; + +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonElement; +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.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +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 cir) { + try { + if (!jsonElement.isJsonArray()) return; + + final JsonArray array = jsonElement.getAsJsonArray(); + + if (array.size() == 0) return; + + JsonElement item = array.get(0); + + int depth = 0; + + while (item != null && item.isJsonArray() && depth < 512) { + if (item.getAsJsonArray().size() == 0 && item.getAsJsonArray().get(0) == null) continue; + + depth++; + + item = item.getAsJsonArray().get(0); + } + + if (depth >= 512) { + cir.setReturnValue(Text.literal("Component is too big").formatted(Formatting.RED)); + + cir.cancel(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/land/chipmunk/chipmunkmod/modules/SelfCare.java b/src/main/java/land/chipmunk/chipmunkmod/modules/SelfCare.java index 8bd3d50..81c0ed6 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/modules/SelfCare.java +++ b/src/main/java/land/chipmunk/chipmunkmod/modules/SelfCare.java @@ -31,8 +31,8 @@ public class SelfCare extends Listener { public String skin; - private Timer timer = new Timer(); - private Timer chatTimer = new Timer(); + private Timer timer; + private Timer chatTimer; private boolean cspy = false; public boolean hasSkin = false; @@ -66,6 +66,9 @@ public class SelfCare extends Listener { } }; + timer = new Timer(); + chatTimer = new Timer(); + timer.schedule(task, interval, interval); chatTimer.schedule(chatTask, chatInterval, chatInterval); } @@ -138,6 +141,8 @@ public class SelfCare extends Listener { } public void packetReceived(PlayerPositionLookS2CPacket packet) { + if (timer == null) return; + positionPacketsPerSecond++; timer.schedule(new TimerTask() { diff --git a/src/main/resources/chipmunkmod.mixins.json b/src/main/resources/chipmunkmod.mixins.json index 53d3953..d20e865 100644 --- a/src/main/resources/chipmunkmod.mixins.json +++ b/src/main/resources/chipmunkmod.mixins.json @@ -21,7 +21,8 @@ "ClientConnectionInvoker", "ClientConnectionAccessor", "PlayerListEntryAccessor", - "SharedConstantsMixin" + "SharedConstantsMixin", + "TextSerializerMixin" ], "injectors": { "defaultRequire": 1