Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.scherso.notsoessential;

import net.minecraft.util.ChatComponentScore;
import net.minecraft.util.ChatComponentSelector;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.IChatComponent;

public final class ChatComponentSanitizer
{

private ChatComponentSanitizer() {}

/**
* Vanilla can't serialize Essential's UTextComponent, which crashes LabyMod's chat.
* Anything that isn't a vanilla component is copied into a ChatComponentText.
*
* @param component component about to be serialized
* @return the component, or a vanilla copy of it
*/
public static IChatComponent sanitize(IChatComponent component)
{
if (component == null
|| component instanceof ChatComponentText
|| component instanceof ChatComponentTranslation
|| component instanceof ChatComponentScore
|| component instanceof ChatComponentSelector)
return (component);

final ChatComponentText copy = new ChatComponentText(component.getUnformattedTextForChat());
copy.setChatStyle(component.getChatStyle().createShallowCopy());

/* Siblings get sanitized when the serializer recurses into them. */
for (Object sibling : component.getSiblings())
copy.appendSibling((IChatComponent) sibling);

return (copy);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class ClassTransformer implements IClassTransformer

public ClassTransformer()
{
registerTransformer(new ChatComponentSerializerTransformer());
registerTransformer(new CommandConfigTransformer());
registerTransformer(new ConnectionManagerKtTransformer());
registerTransformer(new ConnectionManagerTransformer());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.github.scherso.notsoessential.asm.transformer;

import com.github.scherso.notsoessential.asm.ITransformer;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.VarInsnNode;

import static org.objectweb.asm.Opcodes.*;

public class ChatComponentSerializerTransformer implements ITransformer
{

/* Used to find the compiled serialize method, its name and first argument are obfuscated. */
private static final String SERIALIZE_TAIL =
";Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;";

@Override
public String getClassName()
{
return ("net.minecraft.util.IChatComponent$Serializer");
}

/**
* Runs the component through {@link com.github.scherso.notsoessential.ChatComponentSanitizer}
* at the head of {@code serialize}, fixing the crash with LabyMod on Essential's UTextComponent.
*
* <p> The class is still obfuscated when this runs, so {@code serialize} is found by its
* descriptor instead of its name. </p>
*
* @param classNode transformed class node
* @param name transformed class name
*/
@Override
public void transform(ClassNode classNode, String name)
{
for (MethodNode method : classNode.methods)
{
if (!method.desc.startsWith("(L") || !method.desc.endsWith(SERIALIZE_TAIL)) continue;

/* Skip the extra (Object, ...) overload the compiler adds, it just calls this one. */
final String component = Type.getArgumentTypes(method.desc)[0].getInternalName();
if (component.equals("java/lang/Object")) continue;

method.instructions.insert(this.createInsnList(
new VarInsnNode(ALOAD, 1),
new MethodInsnNode(
INVOKESTATIC,
"com/github/scherso/notsoessential/ChatComponentSanitizer",
"sanitize",
"(L" + component + ";)L" + component + ";",
false
),
new VarInsnNode(ASTORE, 1)
));
}
}

}