|
| 1 | +package gay.object.hexdebug.core.api.debugging.env; |
| 2 | + |
| 3 | +import at.petrak.hexcasting.api.casting.castables.Action; |
| 4 | +import at.petrak.hexcasting.api.casting.eval.CastingEnvironment; |
| 5 | +import at.petrak.hexcasting.api.casting.eval.ResolvedPatternType; |
| 6 | +import at.petrak.hexcasting.api.casting.eval.sideeffects.OperatorSideEffect; |
| 7 | +import at.petrak.hexcasting.api.casting.eval.vm.CastingImage; |
| 8 | +import gay.object.hexdebug.core.api.HexDebugCoreAPI; |
| 9 | +import gay.object.hexdebug.core.api.debugging.DebugOutputCategory; |
| 10 | +import gay.object.hexdebug.core.api.debugging.DebugStepType; |
| 11 | +import net.minecraft.network.chat.Component; |
| 12 | +import net.minecraft.server.level.ServerPlayer; |
| 13 | +import org.jetbrains.annotations.ApiStatus; |
| 14 | +import org.jetbrains.annotations.Contract; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | +import org.jetbrains.annotations.Nullable; |
| 17 | + |
| 18 | +import java.util.UUID; |
| 19 | + |
| 20 | +/** |
| 21 | + * Like {@link CastingEnvironment}, but for debugging. |
| 22 | + */ |
| 23 | +public abstract class DebugEnvironment { |
| 24 | + @NotNull |
| 25 | + private final ServerPlayer caster; |
| 26 | + @NotNull |
| 27 | + private final UUID sessionId = UUID.randomUUID(); |
| 28 | + @Nullable |
| 29 | + private DebugStepType lastDebugStepType = null; |
| 30 | + @Nullable |
| 31 | + private Action lastEvaluatedAction = null; |
| 32 | + |
| 33 | + protected DebugEnvironment(@NotNull ServerPlayer caster) { |
| 34 | + this.caster = caster; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Attempts to resume execution of the debuggee. This is called by the debugger after the |
| 39 | + * current continuation is successfully evaluated to completion. |
| 40 | + * @return true if the debug session can continue, or false if the debuggee should be terminated |
| 41 | + */ |
| 42 | + public abstract boolean resume( |
| 43 | + @NotNull CastingEnvironment env, |
| 44 | + @NotNull CastingImage image, |
| 45 | + @NotNull ResolvedPatternType resolutionType |
| 46 | + ); |
| 47 | + |
| 48 | + /** |
| 49 | + * Attempts to restart the debuggee on the given debug thread. This is called by the debugger |
| 50 | + * when requested by the user. |
| 51 | + * <br> |
| 52 | + * The previous debug thread is removed before this method is called, so the implementation may |
| 53 | + * use {@link HexDebugCoreAPI#createDebugThread} and {@link HexDebugCoreAPI#startDebuggingIotas}. |
| 54 | + * However, note that {@link DebugEnvironment} is <strong>not</strong> called before this |
| 55 | + * method, so it's up to the implementation whether they need to call that or not. |
| 56 | + */ |
| 57 | + public abstract void restart(int threadId); |
| 58 | + |
| 59 | + /** |
| 60 | + * Terminates the debuggee. This is called by the debugger after {@link DebugEnvironment#resume} |
| 61 | + * returns {@code false}, during {@link HexDebugCoreAPI#terminateDebugThread}, or when requested |
| 62 | + * by the user. |
| 63 | + */ |
| 64 | + public abstract void terminate(); |
| 65 | + |
| 66 | + /** |
| 67 | + * For in-world debugees, returns whether the caster is close enough to the debuggee to allow |
| 68 | + * debug-related actions to be performed (eg. pause, step, restart). |
| 69 | + */ |
| 70 | + @Contract(pure = true) |
| 71 | + public abstract boolean isCasterInRange(); |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns a display name for this debug session. |
| 75 | + * <br> |
| 76 | + * For example, debugger items return the name of the item, and spell circles return the name of |
| 77 | + * the impetus. |
| 78 | + */ |
| 79 | + @Contract(pure = true) |
| 80 | + @NotNull |
| 81 | + public abstract Component getName(); |
| 82 | + |
| 83 | + public void printDebugMessage(@NotNull Component message) { |
| 84 | + printDebugMessage(message, DebugOutputCategory.STDOUT, true); |
| 85 | + } |
| 86 | + |
| 87 | + public void printDebugMessage( |
| 88 | + @NotNull Component message, |
| 89 | + @NotNull DebugOutputCategory category |
| 90 | + ) { |
| 91 | + printDebugMessage(message, category, true); |
| 92 | + } |
| 93 | + |
| 94 | + public void printDebugMessage( |
| 95 | + @NotNull Component message, |
| 96 | + @NotNull DebugOutputCategory category, |
| 97 | + boolean withSource |
| 98 | + ) { |
| 99 | + HexDebugCoreAPI.INSTANCE.printDebugMessage(caster, sessionId, message, category, withSource); |
| 100 | + } |
| 101 | + |
| 102 | + public void printDebugMishap( |
| 103 | + @NotNull CastingEnvironment env, |
| 104 | + @NotNull OperatorSideEffect.DoMishap sideEffect |
| 105 | + ) { |
| 106 | + var message = sideEffect.getMishap().errorMessageWithName(env, sideEffect.getErrorCtx()); |
| 107 | + if (message != null) { |
| 108 | + printDebugMessage(message, DebugOutputCategory.STDERR); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Contract(pure = true) |
| 113 | + public boolean isDebugging() { |
| 114 | + return HexDebugCoreAPI.INSTANCE.isSessionDebugging(this); |
| 115 | + } |
| 116 | + |
| 117 | + @NotNull |
| 118 | + public ServerPlayer getCaster() { |
| 119 | + return caster; |
| 120 | + } |
| 121 | + |
| 122 | + @NotNull |
| 123 | + public UUID getSessionId() { |
| 124 | + return sessionId; |
| 125 | + } |
| 126 | + |
| 127 | + @ApiStatus.Internal |
| 128 | + @Nullable |
| 129 | + public DebugStepType getLastDebugStepType() { |
| 130 | + return lastDebugStepType; |
| 131 | + } |
| 132 | + |
| 133 | + @ApiStatus.Internal |
| 134 | + public void setLastDebugStepType(@Nullable DebugStepType lastDebugStepType) { |
| 135 | + this.lastDebugStepType = lastDebugStepType; |
| 136 | + } |
| 137 | + |
| 138 | + @ApiStatus.Internal |
| 139 | + @Nullable |
| 140 | + public Action getLastEvaluatedAction() { |
| 141 | + return lastEvaluatedAction; |
| 142 | + } |
| 143 | + |
| 144 | + @ApiStatus.Internal |
| 145 | + public void setLastEvaluatedAction(@Nullable Action lastEvaluatedAction) { |
| 146 | + this.lastEvaluatedAction = lastEvaluatedAction; |
| 147 | + } |
| 148 | +} |
0 commit comments