Skip to content

Commit

Permalink
Hacky sodium fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dima-dencep committed Feb 28, 2024
1 parent 4f0f248 commit 0c68f3f
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id "architectury-plugin" version "3.4-SNAPSHOT"
id "dev.architectury.loom" version "1.1.+" apply false
id "dev.architectury.loom" version "1.5.+" apply false
}

architectury {
Expand Down
1 change: 1 addition & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
modImplementation "net.fabricmc:fabric-loader:${rootProject.loader_version}"

modCompileOnly("maven.modrinth:3dskinlayers:1.5.2-1.19.3-fabric") //TODO
modCompileOnly("maven.modrinth:sodium:mc1.20.1-0.5.8")
}

sourceSets {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.kosmx.bendylib.compat.sodium;

import net.minecraft.client.model.ModelPart;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Pair;

public interface ModelCuboidAccessor {
boolean isHackedByEmoteCraft();

ModelPart.Cuboid getOriginal();
void setOriginal(ModelPart.Cuboid cuboid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.kosmx.bendylib.ModelPartAccessor;
import net.minecraft.client.model.ModelPart;
import net.minecraft.client.render.VertexConsumer;

import java.util.List;
import java.util.Map;
Expand All @@ -17,4 +18,9 @@ public interface IModelPartAccessor {

void setWorkaround(ModelPartAccessor.Workaround workaround);

ModelPartAccessor.Workaround getWorkaround();

VertexConsumer getVertexConsumer();

boolean hasMutatedCuboid();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.List;
import java.util.Map;

@Mixin(ModelPart.class)
@Mixin(value = ModelPart.class, priority = 999)
public abstract class IModelPartMixin implements IModelPartAccessor {

@Shadow @Final private Map<String, ModelPart> children;
Expand All @@ -26,6 +26,9 @@ public abstract class IModelPartMixin implements IModelPartAccessor {

@Unique
private boolean hasMutatedCuboid = false;

@Unique
private VertexConsumer getVertexConsumer = null;
/**
* VanillaDraw won't cause slowdown in vanilla and will fix many issues.
* If needed, use {@link IModelPartAccessor#setWorkaround(ModelPartAccessor.Workaround)} to set the workaround function
Expand Down Expand Up @@ -60,6 +63,16 @@ private void copyTransformExtended(ModelPart part, CallbackInfo ci){

}

@Inject(
method = "render(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumer;IIFFFF)V",
at = @At(
value = "HEAD"
)
)
public void dfgfdgdf(MatrixStack matrices, VertexConsumer vertices, int light, int overlay, float red, float green, float blue, float alpha, CallbackInfo ci) {
getVertexConsumer = vertices;
}

@Redirect(method = "render(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumer;IIFFFF)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/model/ModelPart;renderCuboids(Lnet/minecraft/client/util/math/MatrixStack$Entry;Lnet/minecraft/client/render/VertexConsumer;IIFFFF)V"), require = 0) //It might not find anything if OF already broke the game
private void redirectRenderCuboids(ModelPart modelPart, MatrixStack.Entry entry, VertexConsumer vertexConsumer, int light, int overlay, float red, float green, float blue, float alpha){
redirectedFunction(modelPart, entry, vertexConsumer, light, overlay, red, green, blue, alpha);
Expand Down Expand Up @@ -101,4 +114,19 @@ else if(workaround == ModelPartAccessor.Workaround.VanillaDraw){
public void setWorkaround(ModelPartAccessor.Workaround workaround) {
this.workaround = workaround;
}

@Override
public ModelPartAccessor.Workaround getWorkaround() {
return workaround;
}

@Override
public boolean hasMutatedCuboid() {
return hasMutatedCuboid;
}

@Override
public VertexConsumer getVertexConsumer() {
return getVertexConsumer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.kosmx.bendylib.mixin.sodium;

import io.github.kosmx.bendylib.compat.sodium.ModelCuboidAccessor;
import me.jellysquid.mods.sodium.client.render.immediate.model.ModelCuboid;
import net.minecraft.client.model.ModelPart;
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;

@Mixin(ModelPart.Cuboid.class)
public class CuboidMixin {
@Inject(
method = "sodium$copy",
at = @At(
value = "RETURN"
)
)
public void sodium$copy(CallbackInfoReturnable<ModelCuboid> cir) {
((ModelCuboidAccessor) cir.getReturnValue())
.setOriginal((ModelPart.Cuboid) (Object) this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package io.github.kosmx.bendylib.mixin.sodium;

import com.llamalad7.mixinextras.sugar.Local;
import io.github.kosmx.bendylib.ModelPartAccessor;
import io.github.kosmx.bendylib.compat.sodium.ModelCuboidAccessor;
import io.github.kosmx.bendylib.impl.accessors.CuboidSideAccessor;
import io.github.kosmx.bendylib.impl.accessors.IModelPartAccessor;
import me.jellysquid.mods.sodium.client.render.immediate.model.EntityRenderer;
import me.jellysquid.mods.sodium.client.render.immediate.model.ModelCuboid;
import net.caffeinemc.mods.sodium.api.util.ColorABGR;
import net.caffeinemc.mods.sodium.api.vertex.buffer.VertexBufferWriter;
import net.caffeinemc.mods.sodium.api.vertex.format.common.ModelVertex;
import net.minecraft.client.model.ModelPart;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.util.math.MatrixStack;
import org.apache.logging.log4j.LogManager;
import org.lwjgl.system.MemoryStack;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(EntityRenderer.class)
public abstract class EntityRendererMixin {
@Shadow
private static void prepareNormals(MatrixStack.Entry matrices) {
}

@Shadow
private static void prepareVertices(MatrixStack.Entry matrices, ModelCuboid cuboid) {
}

@Shadow
private static int emitQuads(ModelCuboid cuboid, int color, int overlay, int light) {
return 0;
}

@Shadow @Final private static long SCRATCH_BUFFER;

@Unique
private static VertexConsumer getVertexConsumer = null;

@Redirect(
method = "render",
at = @At(
value = "INVOKE",
target = "Lme/jellysquid/mods/sodium/client/render/immediate/model/EntityRenderer;renderCuboids(Lnet/minecraft/client/util/math/MatrixStack$Entry;Lnet/caffeinemc/mods/sodium/api/vertex/buffer/VertexBufferWriter;[Lme/jellysquid/mods/sodium/client/render/immediate/model/ModelCuboid;III)V"
)
)
private static void redirectRenderCuboids(MatrixStack.Entry matrices, VertexBufferWriter writer, ModelCuboid[] cuboids, int light, int overlay, int color, @Local(argsOnly = true) ModelPart modelPart) {
IModelPartAccessor accessor = (IModelPartAccessor) modelPart;
ModelPartAccessor.Workaround workaround = accessor.getWorkaround();

getVertexConsumer = accessor.getVertexConsumer();

LogManager.getLogger().info(getVertexConsumer);

if(workaround == ModelPartAccessor.Workaround.ExportQuads){ // Unchecked
for (ModelCuboid cuboid : cuboids) {
((CuboidSideAccessor) ((ModelCuboidAccessor) cuboid).getOriginal()).doSideSwapping(); //:D
}

renderCuboids(matrices, writer, cuboids, light, overlay, color);

for(ModelCuboid cuboid : cuboids){
((CuboidSideAccessor) ((ModelCuboidAccessor) cuboid).getOriginal()).resetSides(); //:D
}
} else if(workaround == ModelPartAccessor.Workaround.VanillaDraw){
if(!accessor.hasMutatedCuboid() || cuboids.length == 1 && !((ModelCuboidAccessor) cuboids[0]).isHackedByEmoteCraft()) {
renderCuboids(matrices, writer, cuboids, light, overlay, color);
}
else {
for (ModelCuboid cuboid : cuboids) {
((ModelCuboidAccessor) cuboid).getOriginal()
.renderCuboid(matrices, getVertexConsumer, light, overlay, ColorABGR.unpackRed(color), ColorABGR.unpackGreen(color), ColorABGR.unpackBlue(color), ColorABGR.unpackAlpha(color));
}
}
} else {
renderCuboids(matrices, writer, cuboids, light, overlay, color);
}
}

/**
* @author dima_dencep
* @reason fix bend
*/
@Overwrite
private static void renderCuboids(MatrixStack.Entry matrices, VertexBufferWriter writer, ModelCuboid[] cuboids, int light, int overlay, int color) {
prepareNormals(matrices);

for (ModelCuboid cuboid : cuboids) {

if (cuboid instanceof ModelCuboidAccessor accessor && accessor.isHackedByEmoteCraft()) {
accessor.getOriginal()
.renderCuboid(matrices, getVertexConsumer, light, overlay, ColorABGR.unpackRed(color), ColorABGR.unpackGreen(color), ColorABGR.unpackBlue(color), ColorABGR.unpackAlpha(color));
} else {
prepareVertices(matrices, cuboid);

var vertexCount = emitQuads(cuboid, color, overlay, light);

try (MemoryStack stack = MemoryStack.stackPush()) {
writer.push(stack, SCRATCH_BUFFER, vertexCount, ModelVertex.FORMAT);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.github.kosmx.bendylib.mixin.sodium;

import io.github.kosmx.bendylib.MutableCuboid;
import io.github.kosmx.bendylib.compat.sodium.ModelCuboidAccessor;
import me.jellysquid.mods.sodium.client.render.immediate.model.ModelCuboid;
import net.minecraft.client.model.ModelPart;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Pair;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;

@Mixin(ModelCuboid.class)
public class ModelCuboidMixin implements ModelCuboidAccessor {
@Unique
public ModelPart.Cuboid orgiginal;

@Override
public boolean isHackedByEmoteCraft() {
if (!(orgiginal instanceof MutableCuboid mutableCuboid))
return false;

return mutableCuboid.getActiveMutator() != null;
}

@Override
public ModelPart.Cuboid getOriginal() {
return orgiginal;
}

@Override
public void setOriginal(ModelPart.Cuboid cuboid) {
this.orgiginal = cuboid;
}
}
9 changes: 7 additions & 2 deletions common/src/main/resources/bendylib.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
"client": [
"CuboidMutator",
"IModelPartMixin",
"QuadMixin"
"QuadMixin",
"sodium.CuboidMixin"
],
"server": [],
"injectors": {
"defaultRequire": 1
}
},
"mixins": [
"sodium.EntityRendererMixin",
"sodium.ModelCuboidMixin"
]
}
12 changes: 6 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ org.gradle.jvmargs=-Xmx4G

enabled_platforms=fabric,forge

mod_version=4.0.0
mod_version=4.0.1
maven_group=io.github.kosmx.bendy-lib
archives_base_name=bendy-lib

minecraft_version=1.19.4
yarn_mappings=1.19.4+build.1
loader_version=0.14.17
minecraft_version=1.20.1
yarn_mappings=1.20.1+build.10
loader_version=0.15.7

#Fabric api
fabric_version=0.75.3+1.19.4
fabric_version=0.92.0+1.20.1

#Forge thingy
forge_version=1.19.4-45.0.1
forge_version=1.20.1-47.1.3
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

0 comments on commit 0c68f3f

Please sign in to comment.