|
| 1 | +package gay.object.hexdebug.mixin; |
| 2 | + |
| 3 | +import at.petrak.hexcasting.api.casting.circles.BlockEntityAbstractImpetus; |
| 4 | +import at.petrak.hexcasting.api.casting.circles.CircleExecutionState; |
| 5 | +import at.petrak.hexcasting.api.casting.eval.env.CircleCastEnv; |
| 6 | +import at.petrak.hexcasting.api.casting.eval.vm.CastingImage; |
| 7 | +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; |
| 8 | +import com.llamalad7.mixinextras.injector.ModifyReturnValue; |
| 9 | +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; |
| 10 | +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; |
| 11 | +import gay.object.hexdebug.core.api.HexDebugCoreAPI; |
| 12 | +import gay.object.hexdebug.core.api.debugging.DebuggableCircleComponent; |
| 13 | +import gay.object.hexdebug.debugger.circles.CircleDebugEnv; |
| 14 | +import gay.object.hexdebug.debugger.circles.IMixinCircleExecutionState; |
| 15 | +import gay.object.hexdebug.impl.IDebugEnvAccessor; |
| 16 | +import net.minecraft.core.BlockPos; |
| 17 | +import net.minecraft.core.Direction; |
| 18 | +import net.minecraft.nbt.CompoundTag; |
| 19 | +import net.minecraft.server.level.ServerLevel; |
| 20 | +import net.minecraft.server.level.ServerPlayer; |
| 21 | +import org.jetbrains.annotations.Nullable; |
| 22 | +import org.spongepowered.asm.mixin.Final; |
| 23 | +import org.spongepowered.asm.mixin.Mixin; |
| 24 | +import org.spongepowered.asm.mixin.Shadow; |
| 25 | +import org.spongepowered.asm.mixin.Unique; |
| 26 | +import org.spongepowered.asm.mixin.injection.At; |
| 27 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 28 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 29 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
| 30 | + |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +@Mixin(CircleExecutionState.class) |
| 34 | +public abstract class MixinCircleExecutionState implements IMixinCircleExecutionState { |
| 35 | + @Shadow(remap = false) |
| 36 | + @Final |
| 37 | + public List<BlockPos> reachedPositions; |
| 38 | + @Shadow |
| 39 | + public BlockPos currentPos; |
| 40 | + @Shadow |
| 41 | + public Direction enteredFrom; |
| 42 | + @Shadow(remap = false) |
| 43 | + public CastingImage currentImage; |
| 44 | + |
| 45 | + @Unique |
| 46 | + @Nullable |
| 47 | + private CircleDebugEnv debugEnv$hexdebug; |
| 48 | + |
| 49 | + @Shadow |
| 50 | + @Nullable |
| 51 | + public abstract ServerPlayer getCaster(ServerLevel world); |
| 52 | + |
| 53 | + @Nullable |
| 54 | + @Override |
| 55 | + public CircleDebugEnv getDebugEnv$hexdebug() { |
| 56 | + return debugEnv$hexdebug; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void setDebugEnv$hexdebug(@Nullable CircleDebugEnv debugEnv) { |
| 61 | + debugEnv$hexdebug = debugEnv; |
| 62 | + } |
| 63 | + |
| 64 | + @SuppressWarnings("UnreachableCode") |
| 65 | + @Inject(method = "tick", at = @At("HEAD"), cancellable = true, remap = false) |
| 66 | + private void hexdebug$debugTick(BlockEntityAbstractImpetus impetus, CallbackInfoReturnable<Boolean> cir) { |
| 67 | + if (debugEnv$hexdebug == null) return; |
| 68 | + |
| 69 | + var world = (ServerLevel) impetus.getLevel(); |
| 70 | + if (world == null) return; |
| 71 | + |
| 72 | + var bs = world.getBlockState(currentPos); |
| 73 | + if (!(bs.getBlock() instanceof DebuggableCircleComponent debuggable)) return; |
| 74 | + |
| 75 | + var caster = getCaster(world); |
| 76 | + if (caster == null) return; |
| 77 | + |
| 78 | + // we'll be executing this many times, so only energize etc the first time |
| 79 | + if (!debuggable.isEnergized(currentPos, bs, world)) { |
| 80 | + bs = debuggable.startEnergized(currentPos, bs, world); |
| 81 | + reachedPositions.add(currentPos); |
| 82 | + |
| 83 | + debugEnv$hexdebug.setPaused(true); |
| 84 | + |
| 85 | + var env = new CircleCastEnv(world, (CircleExecutionState) (Object) this); |
| 86 | + debuggable.acceptDebugControlFlow(caster, debugEnv$hexdebug, currentImage, env, enteredFrom, currentPos, bs); |
| 87 | + } |
| 88 | + |
| 89 | + // if we stopped on entry or a breakpoint, continue ticking but skip the normal logic |
| 90 | + // if we got terminated, stop now |
| 91 | + // otherwise, do the regular tick logic |
| 92 | + if (debugEnv$hexdebug.isPaused()) { |
| 93 | + cir.setReturnValue(true); |
| 94 | + } else if (HexDebugCoreAPI.INSTANCE.getDebugEnv(caster, debugEnv$hexdebug.getSessionId()) == null) { |
| 95 | + cir.setReturnValue(false); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @WrapOperation( |
| 100 | + method = "tick", |
| 101 | + at = @At( |
| 102 | + value = "INVOKE", |
| 103 | + target = "Ljava/util/List;add(Ljava/lang/Object;)Z", |
| 104 | + ordinal = 0 |
| 105 | + ), |
| 106 | + require = 0, |
| 107 | + remap = false |
| 108 | + ) |
| 109 | + private boolean hexdebug$maybeSkipAddingToReachedPositions( |
| 110 | + List<Object> instance, |
| 111 | + Object pos, |
| 112 | + Operation<Boolean> original |
| 113 | + ) { |
| 114 | + if ( |
| 115 | + pos instanceof BlockPos |
| 116 | + && !reachedPositions.isEmpty() |
| 117 | + && reachedPositions.get(reachedPositions.size() - 1) == pos |
| 118 | + ) { |
| 119 | + return true; |
| 120 | + } |
| 121 | + return original.call(instance, pos); |
| 122 | + } |
| 123 | + |
| 124 | + @ModifyExpressionValue( |
| 125 | + method = "tick", |
| 126 | + at = @At( |
| 127 | + value = "NEW", |
| 128 | + target = "(Lnet/minecraft/server/level/ServerLevel;Lat/petrak/hexcasting/api/casting/circles/CircleExecutionState;)Lat/petrak/hexcasting/api/casting/eval/env/CircleCastEnv;" |
| 129 | + ) |
| 130 | + ) |
| 131 | + private CircleCastEnv hexdebug$setDebugEnv(CircleCastEnv env) { |
| 132 | + ((IDebugEnvAccessor) env).setDebugEnv$hexdebug(debugEnv$hexdebug); |
| 133 | + return env; |
| 134 | + } |
| 135 | + |
| 136 | + @Inject(method = "endExecution", at = @At("HEAD"), remap = false) |
| 137 | + private void hexdebug$stopDebugging(BlockEntityAbstractImpetus impetus, CallbackInfo ci) { |
| 138 | + if (debugEnv$hexdebug != null) { |
| 139 | + HexDebugCoreAPI.INSTANCE.removeDebugThread(debugEnv$hexdebug); |
| 140 | + debugEnv$hexdebug = null; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @ModifyReturnValue(method = "save", at = @At("RETURN")) |
| 145 | + private CompoundTag hexdebug$saveDebugEnvSessionId(CompoundTag out) { |
| 146 | + if (debugEnv$hexdebug != null) { |
| 147 | + out.putUUID(TAG_HEXDEBUG_SESSION_ID, debugEnv$hexdebug.getSessionId()); |
| 148 | + } |
| 149 | + return out; |
| 150 | + } |
| 151 | + |
| 152 | + @ModifyReturnValue(method = "load", at = @At("RETURN")) |
| 153 | + private static CircleExecutionState hexdebug$loadDebugEnv(CircleExecutionState state, CompoundTag nbt, ServerLevel level) { |
| 154 | + var caster = state.getCaster(level); |
| 155 | + if (caster != null && nbt.contains(TAG_HEXDEBUG_SESSION_ID)) { |
| 156 | + var sessionId = nbt.getUUID(TAG_HEXDEBUG_SESSION_ID); |
| 157 | + var debugEnv = HexDebugCoreAPI.INSTANCE.getDebugEnv(caster, sessionId); |
| 158 | + if (debugEnv instanceof CircleDebugEnv circleEnv && circleEnv.getPos() == state.impetusPos) { |
| 159 | + ((MixinCircleExecutionState) (Object) state).debugEnv$hexdebug = circleEnv; |
| 160 | + } |
| 161 | + } |
| 162 | + return state; |
| 163 | + } |
| 164 | + |
| 165 | + @Unique |
| 166 | + private static final String TAG_HEXDEBUG_SESSION_ID = "hexdebug:session_id"; |
| 167 | +} |
0 commit comments