Remove chunk provider mixin and refactor config packets

This commit is contained in:
Jozufozu 2021-10-31 17:24:57 -07:00
parent 2bdd548636
commit bf2525cb2e
8 changed files with 48 additions and 132 deletions

View file

@ -43,7 +43,6 @@ public class Backend {
private Matrix4f projectionMatrix = new Matrix4f();
private boolean instancedArrays;
private boolean enabled;
public boolean chunkCachingEnabled;
private final List<IShaderContext<?>> contexts = new ArrayList<>();
private final Map<ResourceLocation, MaterialSpec<?>> materialRegistry = new HashMap<>();
@ -154,8 +153,6 @@ public class Backend {
enabled = FlwConfig.get()
.enabled() && !OptifineHandler.usingShaders();
chunkCachingEnabled = FlwConfig.get()
.chunkCaching();
}
public boolean canUseInstancing(@Nullable World world) {

View file

@ -8,6 +8,7 @@ import com.jozufozu.flywheel.backend.OptifineHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.text.IFormattableTextComponent;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
@ -18,7 +19,6 @@ import net.minecraftforge.api.distmarker.OnlyIn;
public enum BooleanConfig {
ENGINE(() -> BooleanConfig::enabled),
NORMAL_OVERLAY(() -> BooleanConfig::normalOverlay),
CHUNK_CACHING(() -> BooleanConfig::chunkCaching),
;
final Supplier<Consumer<BooleanDirective>> receiver;
@ -31,6 +31,27 @@ public enum BooleanConfig {
return new SConfigureBooleanPacket(this, directive);
}
/**
* Encode a variant of BooleanConfig. Symmetrical function to {@link #decode}
*/
public void encode(PacketBuffer buffer) {
buffer.writeByte(this.ordinal());
}
/**
* Safely decode a variant of BooleanConfig. Symmetrical function to {@link #encode}
*/
public static BooleanConfig decode(PacketBuffer buffer) {
byte t = buffer.readByte();
BooleanConfig[] values = values();
// Protects against version differences.
// Shouldn't ever happen but do a sanity check for safety.
if (t >= 0 && t < values.length)
return values[t];
else
return null;
}
@OnlyIn(Dist.CLIENT)
private static void enabled(BooleanDirective state) {
ClientPlayerEntity player = Minecraft.getInstance().player;
@ -72,25 +93,6 @@ public enum BooleanConfig {
player.displayClientMessage(text, false);
}
@OnlyIn(Dist.CLIENT)
private static void chunkCaching(BooleanDirective state) {
ClientPlayerEntity player = Minecraft.getInstance().player;
if (player == null || state == null) return;
if (state == BooleanDirective.DISPLAY) {
ITextComponent text = new StringTextComponent("Chunk caching is currently: ").append(boolToText(FlwConfig.get().client.chunkCaching.get()));
player.displayClientMessage(text, false);
return;
}
FlwConfig.get().client.chunkCaching.set(state.get());
ITextComponent text = boolToText(FlwConfig.get().client.chunkCaching.get()).append(new StringTextComponent(" chunk caching").withStyle(TextFormatting.WHITE));
player.displayClientMessage(text, false);
Backend.reloadWorldRenderers();
}
private static IFormattableTextComponent boolToText(boolean b) {
return b ? new StringTextComponent("enabled").withStyle(TextFormatting.DARK_GREEN) : new StringTextComponent("disabled").withStyle(TextFormatting.RED);
}

View file

@ -1,5 +1,7 @@
package com.jozufozu.flywheel.config;
import net.minecraft.network.PacketBuffer;
public enum BooleanDirective {
TRUE(true),
FALSE(false),
@ -16,7 +18,21 @@ public enum BooleanDirective {
}
public boolean get() {
if (this == DISPLAY) throw new IllegalStateException("Cannot get value from DISPLAY directive");
if (this == DISPLAY) throw new IllegalStateException("DISPLAY directive has no value");
return b;
}
/**
* Encode a variant of BooleanDirective. Symmetrical function to {@link #decode}
*/
public void encode(PacketBuffer buffer) {
buffer.writeByte(this.ordinal());
}
/**
* Safely decode a variant of BooleanDirective. Symmetrical function to {@link #encode}
*/
public static BooleanDirective decode(PacketBuffer buffer) {
return values()[buffer.readByte()];
}
}

View file

@ -17,7 +17,6 @@ public class FlwCommands {
dispatcher.register(Commands.literal("flywheel")
.then(new BooleanConfigCommand("backend", BooleanConfig.ENGINE).register())
.then(new BooleanConfigCommand("debugNormals", BooleanConfig.NORMAL_OVERLAY).register())
.then(new BooleanConfigCommand("chunkCaching", BooleanConfig.CHUNK_CACHING).register())
);
}
}

View file

@ -34,17 +34,12 @@ public class FlwConfig {
return client.debugNormals.get();
}
public boolean chunkCaching() {
return client.chunkCaching.get();
}
public static void init() {
}
public static class ClientConfig {
public final BooleanValue enabled;
public final BooleanValue debugNormals;
public final BooleanValue chunkCaching;
public ClientConfig(ForgeConfigSpec.Builder builder) {
@ -53,9 +48,6 @@ public class FlwConfig {
debugNormals = builder.comment("Enable or disable a debug overlay that colors pixels by their normal")
.define("debugNormals", false);
chunkCaching = builder.comment("Cache chunk lookups to improve performance.")
.define("chunkCaching", true);
}
}
}

View file

@ -19,18 +19,20 @@ public class SConfigureBooleanPacket {
}
public SConfigureBooleanPacket(PacketBuffer buffer) {
target = BooleanConfig.values()[buffer.readByte()];
directive = BooleanDirective.values()[buffer.readByte()];
target = BooleanConfig.decode(buffer);
directive = BooleanDirective.decode(buffer);
}
public void encode(PacketBuffer buffer) {
buffer.writeByte(target.ordinal());
buffer.writeByte(directive.ordinal());
target.encode(buffer);
directive.encode(buffer);
}
public void execute(Supplier<NetworkEvent.Context> ctx) {
target.receiver.get()
.accept(directive);
if (directive != null) {
target.receiver.get()
.accept(directive);
}
ctx.get()
.setPacketHandled(true);
}

View file

@ -1,92 +0,0 @@
package com.jozufozu.flywheel.mixin;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
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.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import com.jozufozu.flywheel.backend.Backend;
import net.minecraft.client.multiplayer.ClientChunkProvider;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import net.minecraft.world.biome.BiomeContainer;
import net.minecraft.world.chunk.AbstractChunkProvider;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.ChunkStatus;
import net.minecraft.world.chunk.IChunk;
@Mixin(ClientChunkProvider.class)
public abstract class FastChunkProviderMixin extends AbstractChunkProvider {
@Shadow
@Final
private ClientWorld level;
@Unique
private int lastX;
@Unique
private int lastZ;
@Unique
private IChunk lastChunk;
@Inject(method = "getChunk",
at = @At("HEAD"),
cancellable = true)
public void returnCachedChunk(int x, int z, ChunkStatus status, boolean create, CallbackInfoReturnable<IChunk> cir) {
if (Backend.getInstance().chunkCachingEnabled && status.isOrAfter(ChunkStatus.FULL)) {
synchronized (level) {
if (lastChunk != null && x == lastX && z == lastZ) {
cir.setReturnValue(lastChunk);
}
}
}
}
@Inject(method = "getChunk",
at = @At("RETURN"))
public void cacheChunk(int x, int z, ChunkStatus status, boolean create, CallbackInfoReturnable<IChunk> cir) {
if (Backend.getInstance().chunkCachingEnabled && status.isOrAfter(ChunkStatus.FULL)) {
synchronized (level) {
lastChunk = cir.getReturnValue();
lastX = x;
lastZ = z;
}
}
}
@Inject(method = "drop", at = @At("HEAD"))
public void invalidateOnDrop(int x, int z, CallbackInfo ci) {
if (Backend.getInstance().chunkCachingEnabled) {
synchronized (level) {
if (x == lastX && z == lastZ) lastChunk = null;
}
}
}
@Inject(method = "replaceWithPacketData", at = @At("HEAD"))
public void invalidateOnPacket(int x, int z, BiomeContainer p_228313_3_, PacketBuffer p_228313_4_, CompoundNBT p_228313_5_, int p_228313_6_, boolean p_228313_7_, CallbackInfoReturnable<Chunk> cir) {
if (Backend.getInstance().chunkCachingEnabled) {
synchronized (level) {
if (x == lastX && z == lastZ) lastChunk = null;
}
}
}
@Redirect(method = "isTickingChunk", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/multiplayer/ClientChunkProvider;hasChunk(II)Z"))
public boolean redirectTicking(ClientChunkProvider clientChunkProvider, int x, int z) {
if (Backend.getInstance().chunkCachingEnabled) {
synchronized (level) {
if (lastChunk != null && x == lastX && z == lastZ) return true;
}
}
return clientChunkProvider.hasChunk(x, z);
}
}

View file

@ -4,7 +4,7 @@
"package": "com.jozufozu.flywheel.mixin",
"compatibilityLevel": "JAVA_8",
"refmap": "flywheel.refmap.json",
"client": ["BeginFrameMixin", "CancelEntityRenderMixin", "CancelTileEntityRenderMixin", "FastChunkProviderMixin", "FixFabulousDepthMixin", "FogColorTrackerMixin", "RenderHooksMixin", "ShaderCloseMixin", "StoreProjectionMatrixMixin", "TileRemoveMixin", "TileWorldHookMixin", "atlas.AtlasDataMixin", "atlas.SheetDataAccessor", "light.LightUpdateMixin", "light.NetworkLightUpdateMixin"],
"client": ["BeginFrameMixin", "CancelEntityRenderMixin", "CancelTileEntityRenderMixin", "FixFabulousDepthMixin", "FogColorTrackerMixin", "RenderHooksMixin", "ShaderCloseMixin", "StoreProjectionMatrixMixin", "TileRemoveMixin", "TileWorldHookMixin", "atlas.AtlasDataMixin", "atlas.SheetDataAccessor", "light.LightUpdateMixin", "light.NetworkLightUpdateMixin"],
"injectors": {
"defaultRequire": 0
}