Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
migrate renderStandardBlockWithAmbientOcclusionPartial to mixin & asm
Browse files Browse the repository at this point in the history
  • Loading branch information
mist475 committed Nov 1, 2023
1 parent 3fe5583 commit ea52807
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 1,448 deletions.
202 changes: 154 additions & 48 deletions src/main/java/mist475/mcpatcherforge/asm/RenderBlocksTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,30 @@ private static byte[] patchRenderBlocks(byte[] basicClass) {
int ifEndsHandled = 0;
boolean secondWrappedIfHandled = false;

int ifStartsHandledPartial = 0;
int ifEndsHandledPartial = 0;
boolean secondWrappedIfHandledPartial = false;

// search sequences

AbstractInsnNode ifSequence1 = new VarInsnNode(Opcodes.ILOAD, 13);
AbstractInsnNode[] ifSequence2 = getStartIfSequence2();
AbstractInsnNode[] endIfSequence = getEndIfSequence();

// code to inject
Pair<InsnList, InsnList> ifWrapper1 = getRenderBlocksIfWrapper(0, 4601, 4626);
Pair<InsnList, InsnList> ifWrapper2 = getRenderBlocksIfWrapper2();
Pair<InsnList, InsnList> ifWrapper3 = getRenderBlocksIfWrapper(2, 4822, 4847);
Pair<InsnList, InsnList> ifWrapper4 = getRenderBlocksIfWrapper(3, 4956, 4981);
Pair<InsnList, InsnList> ifWrapper5 = getRenderBlocksIfWrapper(4, 5090, 5115);
Pair<InsnList, InsnList> ifWrapper6 = getRenderBlocksIfWrapper(5, 5224, 5249);
Pair<InsnList, InsnList> ifWrapper1 = getRenderBlocksIfWrapper(0, 4601, 4626, false);
Pair<InsnList, InsnList> ifWrapper2 = getRenderBlocksIfWrapper(1, 4715, 4730, true);
Pair<InsnList, InsnList> ifWrapper3 = getRenderBlocksIfWrapper(2, 4822, 4847, false);
Pair<InsnList, InsnList> ifWrapper4 = getRenderBlocksIfWrapper(3, 4956, 4981, false);
Pair<InsnList, InsnList> ifWrapper5 = getRenderBlocksIfWrapper(4, 5090, 5115, false);
Pair<InsnList, InsnList> ifWrapper6 = getRenderBlocksIfWrapper(5, 5224, 5249, false);

Pair<InsnList, InsnList> ifWrapper7 = getRenderBlocksIfWrapper(0, 5405, 5419, false);
Pair<InsnList, InsnList> ifWrapper8 = getRenderBlocksIfWrapper(1, 5519, 5523, true);
Pair<InsnList, InsnList> ifWrapper9 = getRenderBlocksIfWrapper(2, 5509, 5523, false);
Pair<InsnList, InsnList> ifWrapper10 = getRenderBlocksIfWrapper(3, 5642, 5655, false);
Pair<InsnList, InsnList> ifWrapper11 = getRenderBlocksIfWrapper(4, 5784, 5798, false);
Pair<InsnList, InsnList> ifWrapper12 = getRenderBlocksIfWrapper(5, 5926, 5940, false);

for (MethodNode methodNode : classNode.methods) {
if (isRenderStandardBlockWithAmbientOcclusion(methodNode)) {
Expand Down Expand Up @@ -189,14 +200,140 @@ private static byte[] patchRenderBlocks(byte[] basicClass) {
}
}
}

else if (isRenderStandardBlockWithAmbientOcclusionPartial(methodNode)) {
logger.debug("found renderStandardBlockWithAmbientOcclusionPartial");
for (AbstractInsnNode node : methodNode.instructions.toArray()) {

// start if-statements

if (ifStartsHandledPartial == 0 && matchesNodeSequence(node, ifSequence1)) {
methodNode.instructions.insertBefore(node, ifWrapper7.getLeft());
ifStartsHandledPartial++;
continue;
}

if (!secondWrappedIfHandledPartial && matchesNodeSequence(node, ifSequence2)) {
methodNode.instructions.insertBefore(node, ifWrapper8.getLeft());
secondWrappedIfHandledPartial = true;
continue;
}

if (ifStartsHandledPartial == 1 && matchesNodeSequence(node, ifSequence1)) {
methodNode.instructions.insertBefore(node, ifWrapper9.getLeft());
ifStartsHandledPartial++;
continue;
}

if (ifStartsHandledPartial == 2 && matchesNodeSequence(node, ifSequence1)) {
methodNode.instructions.insertBefore(node, ifWrapper10.getLeft());
ifStartsHandledPartial++;
continue;
}

if (ifStartsHandledPartial == 3 && matchesNodeSequence(node, ifSequence1)) {
methodNode.instructions.insertBefore(node, ifWrapper11.getLeft());
ifStartsHandledPartial++;
continue;
}

if (ifStartsHandledPartial == 4 && matchesNodeSequence(node, ifSequence1)) {
methodNode.instructions.insertBefore(node, ifWrapper12.getLeft());
ifStartsHandledPartial++;
continue;
}

// end if-statements

if (ifEndsHandledPartial == 0 && matchesNodeSequence(node, endIfSequence)) {
methodNode.instructions.remove(
node.getNext()
.getNext()
.getNext());
methodNode.instructions.insert(
node.getNext()
.getNext(),
ifWrapper7.getRight());
ifEndsHandledPartial++;
continue;
}

if (ifEndsHandledPartial == 1 && matchesNodeSequence(node, endIfSequence)) {
methodNode.instructions.remove(
node.getNext()
.getNext()
.getNext());
methodNode.instructions.insert(
node.getNext()
.getNext(),
ifWrapper8.getRight());
ifEndsHandledPartial++;
continue;
}

if (ifEndsHandledPartial == 2 && matchesNodeSequence(node, endIfSequence)) {
methodNode.instructions.remove(
node.getNext()
.getNext()
.getNext());
methodNode.instructions.insert(
node.getNext()
.getNext(),
ifWrapper9.getRight());
ifEndsHandledPartial++;
continue;
}

if (ifEndsHandledPartial == 3 && matchesNodeSequence(node, endIfSequence)) {
methodNode.instructions.remove(
node.getNext()
.getNext()
.getNext());
methodNode.instructions.insert(
node.getNext()
.getNext(),
ifWrapper10.getRight());
ifEndsHandledPartial++;
continue;
}

if (ifEndsHandledPartial == 4 && matchesNodeSequence(node, endIfSequence)) {
methodNode.instructions.remove(
node.getNext()
.getNext()
.getNext());
methodNode.instructions.insert(
node.getNext()
.getNext(),
ifWrapper11.getRight());
ifEndsHandledPartial++;
continue;
}

if (ifEndsHandledPartial == 5 && matchesNodeSequence(node, endIfSequence)) {
methodNode.instructions.remove(
node.getNext()
.getNext()
.getNext());
methodNode.instructions.insert(
node.getNext()
.getNext(),
ifWrapper12.getRight());
ifEndsHandledPartial++;
break;
}
}
}

}
final ClassWriter classWriter = new ClassWriter(0);
classNode.accept(classWriter);
return classWriter.toByteArray();
}

// faces 1, 3-6 only differ on a single node
private static Pair<InsnList, InsnList> getRenderBlocksIfWrapper(int face, int lineNumber1, int lineNumber2) {
private static Pair<InsnList, InsnList> getRenderBlocksIfWrapper(int face, int lineNumber1, int lineNumber2,
boolean second) {
int iConst = switch (face) {
case 0 -> Opcodes.ICONST_0;
case 1 -> Opcodes.ICONST_1;
Expand Down Expand Up @@ -241,47 +378,11 @@ private static Pair<InsnList, InsnList> getRenderBlocksIfWrapper(int face, int l
final InsnList ifEnd = new InsnList();
ifEnd.add(new LabelNode(label1));
ifEnd.add(new LineNumberNode(lineNumber2, new LabelNode(label1)));
ifEnd.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));

return Pair.of(ifStart, ifEnd);
}

private static Pair<InsnList, InsnList> getRenderBlocksIfWrapper2() {
final InsnList ifStart = new InsnList();
ifStart.add(new VarInsnNode(Opcodes.ALOAD, 0));
ifStart.add(new VarInsnNode(Opcodes.ALOAD, 1));
ifStart.add(new VarInsnNode(Opcodes.ALOAD, 0));
ifStart.add(
new FieldInsnNode(
Opcodes.GETFIELD,
Names.renderBlocks_blockAccess.clas,
Names.renderBlocks_blockAccess.name,
Names.renderBlocks_blockAccess.desc));
ifStart.add(new VarInsnNode(Opcodes.ILOAD, 2));
ifStart.add(new VarInsnNode(Opcodes.ILOAD, 3));
ifStart.add(new VarInsnNode(Opcodes.ILOAD, 4));
ifStart.add(new InsnNode(Opcodes.ICONST_1));
ifStart.add(new VarInsnNode(Opcodes.FLOAD, 9));
ifStart.add(new VarInsnNode(Opcodes.FLOAD, 10));
ifStart.add(new VarInsnNode(Opcodes.FLOAD, 11));
ifStart.add(new VarInsnNode(Opcodes.FLOAD, 12));
ifStart.add(
new MethodInsnNode(
Opcodes.INVOKESTATIC,
"com/prupe/mcpatcher/cc/ColorizeBlock",
"setupBlockSmoothing",
"(" + Names.renderBlocks_.desc + Names.block_.desc + Names.iBlockAccess_.desc + "IIIIFFFF)Z",
false));
Label label176 = new Label();
ifStart.add(new JumpInsnNode(Opcodes.IFNE, new LabelNode(label176)));
Label label177 = new Label();
ifStart.add(new LabelNode(label177));
ifStart.add(new LineNumberNode(4715, new LabelNode(label177)));

final InsnList ifEnd = new InsnList();
ifEnd.add(new LabelNode(label176));
ifEnd.add(new LineNumberNode(4730, new LabelNode(label176)));
ifEnd.add(new FrameNode(Opcodes.F_APPEND, 1, new Object[] { Opcodes.FLOAT }, 0, null));
if (second) {
ifEnd.add(new FrameNode(Opcodes.F_APPEND, 1, new Object[] { Opcodes.FLOAT }, 0, null));
} else {
ifEnd.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));
}

return Pair.of(ifStart, ifEnd);
}
Expand Down Expand Up @@ -312,4 +413,9 @@ private static boolean isRenderStandardBlockWithAmbientOcclusion(MethodNode meth
return Names.renderBlocks_renderStandardBlockWithAmbientOcclusion
.equalsNameDesc(methodNode.name, methodNode.desc);
}

private static boolean isRenderStandardBlockWithAmbientOcclusionPartial(MethodNode methodNode) {
return Names.renderBlocks_renderStandardBlockWithAmbientOcclusionPartial
.equalsNameDesc(methodNode.name, methodNode.desc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public void setNames1_7_10() {
"a",
"(" + Names.block_.desc + "IIIFFF)Z");

Names.renderBlocks_renderStandardBlockWithAmbientOcclusionPartial = m(
Names.renderBlocks_,
"b",
"(" + Names.block_.desc + "IIIFFF)Z");

Names.worldRenderer_updateRenderer = m(Names.worldRenderer_, "a", "(" + Names.entityLivingBase_.desc + ")V");

Names.block_getRenderBlockPass = m(Names.block_, "w", "()I");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public void setNamesSrg() {
"func_147751_a",
"(" + Names.block_.desc + "IIIFFF)Z");

Names.renderBlocks_renderStandardBlockWithAmbientOcclusionPartial = m(
Names.renderBlocks_,
"func_147808_b",
"(" + Names.block_.desc + "IIIFFF)Z");

Names.worldRenderer_updateRenderer = m(
Names.worldRenderer_,
"func_147892_a",
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/mist475/mcpatcherforge/asm/mappings/Names.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public boolean equalsNameDesc(String name, String desc) {

public static Meth renderBlocks_renderStandardBlockWithAmbientOcclusion;

public static Meth renderBlocks_renderStandardBlockWithAmbientOcclusionPartial;

public static Meth worldRenderer_updateRenderer;

public static Meth block_getRenderBlockPass;
Expand Down
Loading

0 comments on commit ea52807

Please sign in to comment.