Skip to content

Commit

Permalink
Fix reobfuscation issue for IColumn
Browse files Browse the repository at this point in the history
  • Loading branch information
Barteks2x committed Aug 19, 2023
1 parent 37eb714 commit 381eb08
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@
*/
package io.github.opencubicchunks.cubicchunks.core.asm.mixin.core.client;

import io.github.opencubicchunks.cubicchunks.core.asm.mixin.core.common.MixinChunk_Column;
import io.github.opencubicchunks.cubicchunks.core.world.cube.BlankCube;
import io.github.opencubicchunks.cubicchunks.core.world.cube.Cube;
import io.github.opencubicchunks.cubicchunks.api.world.IColumn;
import io.github.opencubicchunks.cubicchunks.api.world.ICube;
import io.github.opencubicchunks.cubicchunks.api.world.ICubicWorld;
import io.github.opencubicchunks.cubicchunks.core.asm.mixin.core.common.MixinChunk_Column;
import io.github.opencubicchunks.cubicchunks.core.world.cube.BlankCube;
import io.github.opencubicchunks.cubicchunks.core.world.cube.Cube;
import mcp.MethodsReturnNonnullByDefault;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.EmptyChunk;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -48,6 +49,10 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
@Mixin(EmptyChunk.class)
// soft implements for IColumn
// we can't implement them directly as that causes FG6+ to reobfuscate IColumn#getHeightValue(int, int)
// into vanilla SRG name, which breaks API and mixins
@Implements(@Interface(iface = IColumn.class, prefix = "chunk$"))
public abstract class MixinEmptyChunk extends MixinChunk_Column {

private Cube blankCube;
Expand All @@ -59,27 +64,22 @@ private void cubicChunkColumn_construct(World worldIn, int x, int z, CallbackInf
}
}

@Override
public Cube getCube(int cubeY) {
public ICube chunk$getCube(int cubeY) {
return blankCube;
}

@Override
public Cube removeCube(int cubeY) {
public ICube chunk$removeCube(int cubeY) {
return blankCube;
}

@Override
public void addCube(ICube cube) {
public void chunk$addCube(ICube cube) {
}

@Override
public Collection<Cube> getLoadedCubes() {
public Collection<ICube> chunk$getLoadedCubes() {
return Collections.emptySet();
}

@Override
public Iterable<Cube> getLoadedCubes(int startY, int endY) {
public Iterable<ICube> chunk$getLoadedCubes(int startY, int endY) {
return Collections.emptySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
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 javax.annotation.ParametersAreNonnullByDefault;
import java.util.Collection;
Expand All @@ -52,8 +53,14 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
@Mixin(value = Chunk.class, priority = 2000)
@Implements(@Interface(iface = IColumn.class, prefix = "chunk$"))
public abstract class MixinChunk_Column implements IColumn, IColumnInternal {
// soft implements for IColumn and IColumnInternal
// we can't implement them directly as that causes FG6+ to reobfuscate IColumn#getHeightValue(int, int)
// into vanilla SRG name, which breaks API and mixins
@Implements({
@Interface(iface = IColumn.class, prefix = "chunk$"),
@Interface(iface = IColumnInternal.class, prefix = "chunk_internal$")
})
public abstract class MixinChunk_Column {

/*
* WARNING: WHEN YOU RENAME ANY OF THESE 3 FIELDS RENAME CORRESPONDING
Expand All @@ -77,67 +84,65 @@ public abstract class MixinChunk_Column implements IColumn, IColumnInternal {

@Shadow @Final private int[] heightMap;

@Override public Cube getLoadedCube(int cubeY) {
public ICube chunk$getLoadedCube(int cubeY) {
if (cachedCube != null && cachedCube.getY() == cubeY) {
return cachedCube;
}
return getWorld().getCubeCache().getLoadedCube(x, cubeY, z);
}


@Override public Cube getCube(int cubeY) {
public ICube chunk$getCube(int cubeY) {
if (cachedCube != null && cachedCube.getY() == cubeY) {
return cachedCube;
}
return getWorld().getCubeCache().getCube(x, cubeY, z);
}


@Override public void addCube(ICube cube) {
public void chunk$addCube(ICube cube) {
this.cubeMap.put((Cube) cube);
}


@Override public Cube removeCube(int cubeY) {
public ICube chunk$removeCube(int cubeY) {
if (cachedCube != null && cachedCube.getY() == cubeY) {
invalidateCachedCube();
cubicChunks$invalidateCachedCube();
}
return this.cubeMap.remove(cubeY);
}

@Override
public void removeFromStagingHeightmap(ICube cube) {
public void chunk_internal$removeFromStagingHeightmap(ICube cube) {
stagingHeightMap.removeStagedCube(cube);
}

@Override
public void addToStagingHeightmap(ICube cube) {
public void chunk_internal$addToStagingHeightmap(ICube cube) {
stagingHeightMap.addStagedCube(cube);
}

@Override
public int getTopYWithStaging(int localX, int localZ) {
public int chunk_internal$getTopYWithStaging(int localX, int localZ) {
if (!isColumn) {
return heightMap[localZ << 4 | localX] - 1;
}
return Math.max(opacityIndex.getTopBlockY(localX, localZ), stagingHeightMap.getTopBlockY(localX, localZ));
}

private void invalidateCachedCube() {
@Unique
private void cubicChunks$invalidateCachedCube() {
cachedCube = null;
}


@Override public boolean hasLoadedCubes() {
public boolean chunk$hasLoadedCubes() {
return !cubeMap.isEmpty();
}

@SuppressWarnings("unchecked")
@Unique @SuppressWarnings({"unchecked", "AddedMixinMembersNamePattern"})
public <T extends World & ICubicWorldInternal> T getWorld() {
return (T) this.world;
}

@Override public boolean shouldTick() {
public boolean chunk$shouldTick() {
for (Cube cube : cubeMap) {
if (cube.getTickets().shouldTick()) {
return true;
Expand All @@ -147,49 +152,47 @@ public <T extends World & ICubicWorldInternal> T getWorld() {
}


@Override public IHeightMap getOpacityIndex() {
public IHeightMap chunk$getOpacityIndex() {
return this.opacityIndex;
}


@Override public Collection<? extends ICube> getLoadedCubes() {
public Collection<? extends ICube> chunk$getLoadedCubes() {
return this.cubeMap.all();
}


@Override public Iterable<? extends ICube> getLoadedCubes(int startY, int endY) {
public Iterable<? extends ICube> chunk$getLoadedCubes(int startY, int endY) {
return this.cubeMap.cubes(startY, endY);
}


@Override public void preCacheCube(ICube cube) {
public void chunk$preCacheCube(ICube cube) {
this.cachedCube = (Cube) cube;
}

@Override public int getX() {
@Intrinsic public int chunk$getX() {
return x;
}

@Override public int getZ() {
@Intrinsic public int chunk$getZ() {
return z;
}

@Override
public int getHeightValue(int localX, int blockY, int localZ) {
return getTopYWithStaging(localX, localZ) + 1;
public int chunk$getHeightValue(int localX, int blockY, int localZ) {
return chunk_internal$getTopYWithStaging(localX, localZ) + 1;
}

/**
* @author Barteks2x
* @reason go through staging heightmap
*/
@SuppressWarnings("deprecation") @Overwrite
public int getHeightValue(int localX, int localZ) {
return getTopYWithStaging(localX, localZ) + 1;
}
@SuppressWarnings("deprecation")
@Overwrite
public int getHeightValue(int localX, int localZ) {
return chunk_internal$getTopYWithStaging(localX, localZ) + 1;
}

@Intrinsic
public int chunk$getHeightValue(int localX, int localZ) {
return getTopYWithStaging(localX, localZ) + 1;
return chunk_internal$getTopYWithStaging(localX, localZ) + 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.common.base.Predicate;
import io.github.opencubicchunks.cubicchunks.api.util.Coords;
import io.github.opencubicchunks.cubicchunks.api.util.CubePos;
import io.github.opencubicchunks.cubicchunks.api.world.IColumn;
import io.github.opencubicchunks.cubicchunks.api.world.ICube;
import io.github.opencubicchunks.cubicchunks.api.world.ICubicWorld;
import io.github.opencubicchunks.cubicchunks.api.world.IHeightMap;
Expand Down Expand Up @@ -60,10 +61,13 @@
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.ChunkEvent.Load;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Implements;
import org.spongepowered.asm.mixin.Interface;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
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.Constant;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -89,7 +93,14 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
@Mixin(value = Chunk.class, priority = 999)
public abstract class MixinChunk_Cubes implements IColumnInternal {
// soft implements for IColumn and IColumnInternal
// we can't implement them directly as that causes FG6+ to reobfuscate IColumn#getHeightValue(int, int)
// into vanilla SRG name, which breaks API and mixins
@Implements({
@Interface(iface = IColumn.class, prefix = "chunk$"),
@Interface(iface = IColumnInternal.class, prefix = "chunk_internal$")
})
public abstract class MixinChunk_Cubes {

@Shadow @Final private ExtendedBlockStorage[] storageArrays;
@Shadow @Final public static ExtendedBlockStorage NULL_BLOCK_STORAGE;
Expand Down Expand Up @@ -127,14 +138,14 @@ public abstract class MixinChunk_Cubes implements IColumnInternal {

@Shadow public abstract int getLightFor(EnumSkyBlock type, BlockPos pos);

@SuppressWarnings("unchecked")
@Unique @SuppressWarnings({"unchecked", "AddedMixinMembersNamePattern"})
public <T extends World & ICubicWorldInternal> T getWorld() {
return (T) this.world;
}

// TODO: make it go through cube raw access methods
// TODO: make cube an interface, use the implementation only here
@Nullable
@Unique @Nullable
private ExtendedBlockStorage getEBS_CubicChunks(int index) {
if (!isColumn) {
return storageArrays[index];
Expand All @@ -150,7 +161,7 @@ private ExtendedBlockStorage getEBS_CubicChunks(int index) {
}

// setEBS is unlikely to be used extremely frequently, no caching
private void setEBS_CubicChunks(int index, ExtendedBlockStorage ebs) {
@Unique private void setEBS_CubicChunks(int index, ExtendedBlockStorage ebs) {
if (!isColumn) {
storageArrays[index] = ebs;
return;
Expand Down Expand Up @@ -202,7 +213,7 @@ private void cubicChunkColumn_construct(World world, int x, int z, CallbackInfo
}
this.stagingHeightMap = new StagingHeightMap();
// instead of redirecting access to this map, just make the map do the work
this.tileEntities = new ColumnTileEntityMap(this);
this.tileEntities = new ColumnTileEntityMap((IColumn) this);

// this.chunkSections = null;
// this.skylightUpdateMap = null;
Expand All @@ -220,8 +231,7 @@ private int getInitChunkLoopEnd(int _16, World world, ChunkPrimer primer, int x,
return _16;
}

@Override
public ChunkPrimer getCompatGenerationPrimer() {
public ChunkPrimer chunk_internal$getCompatGenerationPrimer() {
return compatGenerationPrimer;
}

Expand Down Expand Up @@ -345,7 +355,7 @@ private void setBlockState_CubicChunks_relightBlockReplace(BlockPos pos, IBlockS
int localX, int y, int localZ, int packedXZ, int oldHeightValue, IBlockState oldState, Block newBlock, Block oldBlock,
int oldOpacity, ExtendedBlockStorage ebs, boolean createdNewEbsAboveTop, int newOpacity) {

if (isColumn && getCube(blockToCube(y)).isInitialLightingDone()) {
if (isColumn && ((IColumn) this).getCube(blockToCube(y)).isInitialLightingDone()) {
if (oldHeightValue == y + 1) { // oldHeightValue is the previous block Y above the top block, so this is the "removing to block" case
getWorld().getLightingManager().doOnBlockSetLightUpdates((Chunk) (Object) this, localX, getHeightValue(localX, localZ), y, localZ);
} else {
Expand Down Expand Up @@ -380,7 +390,7 @@ private boolean getBlockLightOpacity_isChunkLoadedCubeRedirect(Chunk chunk, int
if (!isColumn) {
return loaded;
}
ICube cube = this.getLoadedCube(blockToCube(y));
ICube cube = ((IColumn) this).getLoadedCube(blockToCube(y));
return cube != null && cube.isCubeLoaded();
}

Expand Down Expand Up @@ -426,7 +436,7 @@ private void onEBSSet_setBlockState_setOpacity(BlockPos pos, IBlockState state,
return;
}
this.dirty = true;
if (getCube(blockToCube(pos.getY())).isSurfaceTracked()) {
if (((IColumn) this).getCube(blockToCube(pos.getY())).isSurfaceTracked()) {
opacityIndex.onOpacityChange(blockToLocal(pos.getX()), pos.getY(), blockToLocal(pos.getZ()), state.getLightOpacity(world, pos));
getWorld().getLightingManager().onHeightUpdate(pos);
} else {
Expand Down Expand Up @@ -482,7 +492,7 @@ private void replacedGetLightForCC(EnumSkyBlock type, BlockPos pos, CallbackInfo
return;
}
((ICubicWorldInternal) world).getLightingManager().onGetLight(type, pos);
cir.setReturnValue(((Cube) getCube(blockToCube(pos.getY()))).getCachedLightFor(type, pos));
cir.setReturnValue(((Cube) ((IColumn) this).getCube(blockToCube(pos.getY()))).getCachedLightFor(type, pos));
}

@Nullable
Expand Down Expand Up @@ -689,7 +699,7 @@ private boolean addTileEntity_isChunkLoadedCubeRedirect(Chunk chunk, TileEntity
if (!isColumn) {
return loaded;
}
ICube cube = this.getLoadedCube(blockToCube(te.getPos().getY()));
ICube cube = ((IColumn) this).getLoadedCube(blockToCube(te.getPos().getY()));
return cube != null && cube.isCubeLoaded();
}

Expand All @@ -702,7 +712,7 @@ private boolean removeTileEntity_isChunkLoadedCubeRedirect(Chunk chunk, BlockPos
if (!isColumn) {
return loaded;
}
ICube cube = this.getLoadedCube(blockToCube(pos.getY()));
ICube cube = ((IColumn) this).getLoadedCube(blockToCube(pos.getY()));
return cube != null && cube.isCubeLoaded();
}

Expand Down Expand Up @@ -828,7 +838,7 @@ private <T extends Entity> void getEntitiesOfTypeWithinAAAB_CubicChunks(Class<?
private void getPrecipitationHeight_CubicChunks_Replace(BlockPos pos, CallbackInfoReturnable<BlockPos> cbi) {
if (isColumn) {
// TODO: precipitationHeightMap
BlockPos ret = new BlockPos(pos.getX(), getHeightValue(blockToLocal(pos.getX()), pos.getY(), blockToLocal(pos.getZ())), pos.getZ());
BlockPos ret = new BlockPos(pos.getX(), ((IColumn) this).getHeightValue(blockToLocal(pos.getX()), pos.getY(), blockToLocal(pos.getZ())), pos.getZ());
cbi.setReturnValue(ret);
}
}
Expand Down Expand Up @@ -918,7 +928,7 @@ private boolean removeInvalidTileEntity_isChunkLoadedCubeRedirect(Chunk chunk, B
if (!isColumn) {
return loaded;
}
ICube cube = this.getLoadedCube(blockToCube(pos.getY()));
ICube cube = ((IColumn) this).getLoadedCube(blockToCube(pos.getY()));
return cube != null && cube.isCubeLoaded();
}

Expand Down
Loading

0 comments on commit 381eb08

Please sign in to comment.