Skip to content

Commit

Permalink
Fixed flywheel block entities still rendering after ship is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
StewStrong committed Nov 6, 2023
1 parent b3b3682 commit 9528e93
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.valkyrienskies.mod.compat;

import com.jozufozu.flywheel.backend.instancing.InstanceWorld;
import java.util.Collections;
import java.util.Set;
import java.util.WeakHashMap;
import org.valkyrienskies.core.impl.hooks.VSEvents.ShipUnloadEventClient;
import org.valkyrienskies.mod.mixinducks.MixinBlockEntityInstanceManagerDuck;

public class FlywheelEvents {
static {
registerEvents();
}

private static final Set<InstanceWorld> weakLoadedInstanceWorlds =
Collections.newSetFromMap(
new WeakHashMap<>()
);

private static synchronized void registerEvents() {
ShipUnloadEventClient.Companion.on(event -> {
for (final InstanceWorld instanceWorld : weakLoadedInstanceWorlds) {
((MixinBlockEntityInstanceManagerDuck) instanceWorld.getBlockEntityInstanceManager()).vs$removeShipManager(event.getShip());
}
});
}

public static void onInstanceWorldLoad(final InstanceWorld instanceWorld) {
weakLoadedInstanceWorlds.add(instanceWorld);
}

public static void onInstanceWorldUnload(final InstanceWorld instanceWorld) {
weakLoadedInstanceWorlds.remove(instanceWorld);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@
public abstract class MixinBlockEntityInstanceManager extends InstanceManager<BlockEntity> implements
MixinBlockEntityInstanceManagerDuck {

public WeakHashMap<ClientShip, MaterialManager> getShipMaterialManagers() {
return shipMaterialManagers;
}

@Unique
private final WeakHashMap<ClientShip, MaterialManager> shipMaterialManagers =
private final WeakHashMap<ClientShip, MaterialManager> vs$shipMaterialManagers =
new WeakHashMap<>();

public MixinBlockEntityInstanceManager(final MaterialManager materialManager) {
Expand All @@ -57,7 +53,7 @@ void preCreateRaw(final BlockEntity blockEntity, final CallbackInfoReturnable<In
level, blockEntity.getBlockPos());
if (ship != null) {
final MaterialManager manager =
shipMaterialManagers.computeIfAbsent(ship, k -> createMaterialManager());
vs$shipMaterialManagers.computeIfAbsent(ship, k -> vs$createMaterialManager());
final Vector3i c =
ship.getChunkClaim().getCenterBlockCoordinates(VSGameUtilsKt.getYRange(nullableLevel), new Vector3i());
((InstancingEngineAccessor) manager).setOriginCoordinate(new BlockPos(c.x, c.y, c.z));
Expand All @@ -67,8 +63,18 @@ void preCreateRaw(final BlockEntity blockEntity, final CallbackInfoReturnable<In
}
}

@Override
public WeakHashMap<ClientShip, MaterialManager> vs$getShipMaterialManagers() {
return vs$shipMaterialManagers;
}

@Override
public void vs$removeShipManager(final ClientShip clientShip) {
vs$shipMaterialManagers.remove(clientShip);
}

@Unique
private MaterialManager createMaterialManager() {
private MaterialManager vs$createMaterialManager() {
if (Backend.getBackendType() == BackendType.INSTANCING) {
return InstancingEngine.builder(Contexts.WORLD).build();
} else if (Backend.getBackendType() == BackendType.BATCHING) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.valkyrienskies.core.api.ships.ClientShip;
import org.valkyrienskies.mod.common.VSClientGameUtils;
import org.valkyrienskies.mod.common.util.VectorConversionsMCKt;
import org.valkyrienskies.mod.compat.FlywheelEvents;
import org.valkyrienskies.mod.mixinducks.MixinBlockEntityInstanceManagerDuck;
import org.valkyrienskies.mod.mixinducks.MixinInstancingEngineDuck;

Expand All @@ -49,17 +50,15 @@ void renderShipTiles(final RenderLayerEvent event, final CallbackInfo ci) {
//not sure if restoreState stuff should be here or in the ((MixinInstancingEngineDuck) manager).render() method
final GlStateTracker.State restoreState = GlStateTracker.getRestoreState();
final var shipManagers =
((MixinBlockEntityInstanceManagerDuck) blockEntityInstanceManager).getShipMaterialManagers();
((MixinBlockEntityInstanceManagerDuck) blockEntityInstanceManager).vs$getShipMaterialManagers();

shipManagers.forEach((ship, manager) -> {
render(ship, manager, event);
});
shipManagers.forEach((ship, manager) -> vs$render(ship, manager, event));
restoreState.restore();
}

@Unique
void render(final ClientShip ship, final MaterialManager manager, final RenderLayerEvent event) {
if (manager instanceof InstancingEngine<?> engine) {
private void vs$render(final ClientShip ship, final MaterialManager manager, final RenderLayerEvent event) {
if (manager instanceof final InstancingEngine<?> engine) {
final Vector3d origin = VectorConversionsMCKt.toJOMLD(engine.getOriginCoordinate());

final Matrix4d viewProjection = VectorConversionsMCKt.toJOML(event.viewProjection);
Expand All @@ -76,14 +75,14 @@ void render(final ClientShip ship, final MaterialManager manager, final RenderLa

final Matrix4f fnlProj = VectorConversionsMCKt.toMinecraft(finalProjection);

((MixinInstancingEngineDuck) engine).render(
((MixinInstancingEngineDuck) engine).vs$render(
fnlProj,
camInShipLocal.x,
camInShipLocal.y,
camInShipLocal.z,
event.layer
);
} else if (manager instanceof BatchingEngine engine) {
} else if (manager instanceof final BatchingEngine engine) {
event.stack.pushPose();
VSClientGameUtils.multiplyWithShipToWorld(event.stack, ship);
engine.render(taskEngine, event);
Expand All @@ -92,4 +91,14 @@ void render(final ClientShip ship, final MaterialManager manager, final RenderLa
throw new IllegalArgumentException("unrecognized engine");
}
}

@Inject(method = "<init>", at = @At("RETURN"))
private void postInit(final CallbackInfo ci) {
FlywheelEvents.onInstanceWorldLoad(InstanceWorld.class.cast(this));
}

@Inject(method = "delete", at = @At("RETURN"))
private void postDelete(final CallbackInfo ci) {
FlywheelEvents.onInstanceWorldUnload(InstanceWorld.class.cast(this));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,21 @@
import com.jozufozu.flywheel.core.shader.WorldProgram;
import com.mojang.math.Matrix4f;
import java.util.stream.Stream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.valkyrienskies.mod.mixinducks.MixinInstancingEngineDuck;

@Pseudo
@Mixin(value = InstancingEngine.class, remap = false)
public abstract class MixinInstancingEngine<P extends WorldProgram> implements MixinInstancingEngineDuck {
@Unique
private static final Logger LOGGER = LogManager.getLogger("VS2 flywheel.client.MixinInstancingEngine");

@Shadow
protected abstract Stream<InstancedMaterialGroup<P>> getGroupsToRender(@Nullable RenderLayer layer);

@Override
public void render(final Matrix4f viewProjection, final double camX, final double camY, final double camZ,
public void vs$render(final Matrix4f viewProjection, final double camX, final double camY, final double camZ,
final RenderLayer layer) {
this.getGroupsToRender(layer).forEach(g -> {
g.render(viewProjection, camX, camY, camZ, layer);
});
this.getGroupsToRender(layer).forEach(g -> g.render(viewProjection, camX, camY, camZ, layer));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

public interface MixinBlockEntityInstanceManagerDuck {

WeakHashMap<ClientShip, MaterialManager> getShipMaterialManagers();
WeakHashMap<ClientShip, MaterialManager> vs$getShipMaterialManagers();

void vs$removeShipManager(ClientShip clientShip);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import com.mojang.math.Matrix4f;

public interface MixinInstancingEngineDuck {
void render(Matrix4f viewProjection, double camX, double camY, double camZ, RenderLayer layer);
void vs$render(Matrix4f viewProjection, double camX, double camY, double camZ, RenderLayer layer);
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ forge_version=1.18.2-40.2.4
create_fabric_version=0.5.1-c-build.1092+mc1.18.2
flywheel_version_fabric=0.6.9-38
createbigcannons_version= 0.5.2-nightly-e815ca4
vs_core_version=1.1.0+05bdb9b8af
vs_core_version=1.1.0+4708cc0c0e
# Prevent kotlin from autoincluding stdlib as a dependency, which breaks
# gradle's composite builds (includeBuild) for some reason. We'll add it manually
kotlin.stdlib.default.dependency=false
Expand Down

0 comments on commit 9528e93

Please sign in to comment.