Skip to content

Commit 6f310a7

Browse files
committed
Begin working on debug API
1 parent d3a8e06 commit 6f310a7

File tree

20 files changed

+361
-104
lines changed

20 files changed

+361
-104
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package gay.object.hexdebug.mixin;
2+
3+
import at.petrak.hexcasting.api.casting.eval.CastingEnvironment;
4+
import gay.object.hexdebug.core.api.debugging.DebugEnvironment;
5+
import gay.object.hexdebug.impl.IDebugEnvAccessor;
6+
import org.jetbrains.annotations.Nullable;
7+
import org.spongepowered.asm.mixin.Mixin;
8+
import org.spongepowered.asm.mixin.Unique;
9+
10+
@Mixin(CastingEnvironment.class)
11+
public abstract class MixinCastingEnvironment implements IDebugEnvAccessor {
12+
@Unique
13+
@Nullable
14+
private DebugEnvironment debugEnv$hexdebug;
15+
16+
@Override
17+
@Nullable
18+
public DebugEnvironment getDebugEnv$hexdebug() {
19+
return debugEnv$hexdebug;
20+
}
21+
22+
@Override
23+
public void setDebugEnv$hexdebug(@Nullable DebugEnvironment debugEnvironment) {
24+
debugEnv$hexdebug = debugEnvironment;
25+
}
26+
}

Common/src/main/java/gay/object/hexdebug/mixin/MixinOpEval.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import at.petrak.hexcasting.api.casting.eval.vm.SpellContinuation;
88
import at.petrak.hexcasting.api.casting.iota.Iota;
99
import at.petrak.hexcasting.common.casting.actions.eval.OpEval;
10-
import gay.object.hexdebug.casting.eval.IDebugCastEnv;
10+
import gay.object.hexdebug.core.api.HexDebugCoreAPI;
1111
import gay.object.hexdebug.core.api.debugging.DebugStepType;
1212
import org.spongepowered.asm.mixin.Mixin;
1313
import org.spongepowered.asm.mixin.injection.At;
@@ -27,9 +27,10 @@ private void setDebugStepType(
2727
Iota iota,
2828
CallbackInfoReturnable<OperationResult> cir
2929
) {
30-
if (env instanceof IDebugCastEnv debugCastEnv) {
31-
debugCastEnv.setLastEvaluatedAction(this);
32-
debugCastEnv.setLastDebugStepType(DebugStepType.IN);
30+
var debugEnv = HexDebugCoreAPI.INSTANCE.getDebugEnv(env);
31+
if (debugEnv != null) {
32+
debugEnv.setLastEvaluatedAction(this);
33+
debugEnv.setLastDebugStepType(DebugStepType.IN);
3334
}
3435
}
3536
}

Common/src/main/kotlin/gay/object/hexdebug/adapter/DebugAdapter.kt

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
3838
private var isConnected = false
3939
private var lastDebugger: HexDebugger? = null // for resolving sources after exit
4040
private val debuggers = mutableMapOf<Int, HexDebugger>()
41+
private val threadIds = mutableMapOf<UUID, Int>()
4142
private val restartArgs = mutableMapOf<Int, CastArgs>()
4243
private val state = SharedDebugState()
4344

@@ -49,6 +50,11 @@ class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
4950

5051
fun isDebugging(threadId: Int) = debugger(threadId) != null
5152

53+
fun debugger(sessionId: UUID) =
54+
threadIds[sessionId]
55+
?.let(debuggers::get)
56+
?.takeIf { it.sessionId == sessionId }
57+
5258
fun debugger(threadId: Int) = debuggers[threadId]
5359

5460
private fun packId(threadId: Int, reference: Int): Int =
@@ -85,6 +91,7 @@ class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
8591
val debugger = HexDebugger(threadId, state, args)
8692
lastDebugger = debugger
8793
debuggers[threadId] = debugger
94+
threadIds[args.debugEnv.sessionId] = threadId
8895
restartArgs[threadId] = args
8996

9097
remoteProxy.thread(ThreadEventArguments().also {
@@ -109,16 +116,17 @@ class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
109116
}
110117

111118
fun print(
112-
threadId: Int,
119+
sessionId: UUID,
113120
value: String,
114121
category: String = OutputEventArgumentsCategory.STDOUT,
115122
withSource: Boolean = true,
116123
) {
124+
val debugger = debugger(sessionId) ?: return
117125
remoteProxy.output(OutputEventArguments().also {
118126
it.category = category
119127
it.output = value
120128
if (withSource) {
121-
it.setSourceAndPosition(state.initArgs, debugger(threadId)?.lastEvaluatedMetadata)
129+
it.setSourceAndPosition(state.initArgs, debugger.lastEvaluatedMetadata)
122130
}
123131
})
124132
}
@@ -187,10 +195,18 @@ class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
187195
})
188196
}
189197

190-
sendStoppedEvent(threadId, result.reason)
198+
if (result.reason != null) {
199+
// stopped
200+
sendStoppedEvent(threadId, result.reason)
191201

192-
debugger(threadId)?.getNextIotaToEvaluate()?.also { (iota, index) ->
193-
printDebuggerStatus(iota, index)
202+
debugger(threadId)?.getNextIotaToEvaluate()?.also { (iota, index) ->
203+
printDebuggerStatus(iota, index)
204+
}
205+
} else {
206+
// running
207+
remoteProxy.continued(ContinuedEventArguments().also {
208+
it.threadId = threadId
209+
})
194210
}
195211

196212
return view
@@ -356,6 +372,7 @@ class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
356372

357373
override fun restart(args: RestartArguments?): CompletableFuture<Void> {
358374
debuggers.clear()
375+
threadIds.clear()
359376

360377
for ((threadId, castArgs) in restartArgs) {
361378
startDebugging(threadId, castArgs, false)
@@ -378,7 +395,7 @@ class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
378395
it.reason = ThreadEventArgumentsReason.EXITED
379396
it.threadId = threadId
380397
})
381-
debuggers.remove(threadId)
398+
debuggers.remove(threadId)?.let { threadIds.remove(it.sessionId) }
382399
}
383400

384401
MsgDebuggerStateS2C(

Common/src/main/kotlin/gay/object/hexdebug/casting/actions/OpIsDebugging.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import at.petrak.hexcasting.api.casting.asActionResult
44
import at.petrak.hexcasting.api.casting.castables.ConstMediaAction
55
import at.petrak.hexcasting.api.casting.eval.CastingEnvironment
66
import at.petrak.hexcasting.api.casting.iota.Iota
7-
import gay.`object`.hexdebug.casting.eval.IDebugCastEnv
7+
import gay.`object`.hexdebug.core.api.HexDebugCoreAPI
88

99
object OpIsDebugging : ConstMediaAction {
1010
override val argc = 0
1111

12-
override fun execute(args: List<Iota>, env: CastingEnvironment) = when (env) {
13-
is IDebugCastEnv -> env.isDebugging
14-
else -> false
15-
}.asActionResult
12+
override fun execute(args: List<Iota>, env: CastingEnvironment) =
13+
(HexDebugCoreAPI.INSTANCE.getDebugEnv(env) != null).asActionResult
1614
}

Common/src/main/kotlin/gay/object/hexdebug/casting/eval/DebuggerCastEnv.kt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package gay.`object`.hexdebug.casting.eval
22

3-
import at.petrak.hexcasting.api.casting.castables.Action
43
import at.petrak.hexcasting.api.casting.eval.env.PackagedItemCastEnv
54
import at.petrak.hexcasting.api.casting.eval.sideeffects.OperatorSideEffect
6-
import gay.`object`.hexdebug.core.api.debugging.DebugStepType
5+
import gay.`object`.hexdebug.core.api.debugging.IDebuggableCastEnv
76
import gay.`object`.hexdebug.utils.findMediaHolderInHand
87
import gay.`object`.hexdebug.utils.otherHand
98
import net.minecraft.network.chat.Component
@@ -13,22 +12,17 @@ import net.minecraft.world.InteractionHand
1312
class DebuggerCastEnv(
1413
caster: ServerPlayer,
1514
castingHand: InteractionHand,
16-
) : PackagedItemCastEnv(caster, castingHand), IDebugCastEnv {
15+
) : PackagedItemCastEnv(caster, castingHand), IDebuggableCastEnv {
1716
private val item = caster.getItemInHand(castingHand).item
1817

19-
override var threadId: Int? = null
20-
21-
override var lastEvaluatedAction: Action? = null
22-
override var lastDebugStepType: DebugStepType? = null
23-
2418
override fun printMessage(message: Component) {
2519
super.printMessage(message)
26-
printDebugMessage(caster, message)
20+
debugEnv?.printDebugMessage(message)
2721
}
2822

2923
override fun sendMishapMsgToPlayer(mishap: OperatorSideEffect.DoMishap) {
3024
super.sendMishapMsgToPlayer(mishap)
31-
printDebugMishap(this, caster, mishap)
25+
debugEnv?.printDebugMishap(this, mishap)
3226
}
3327

3428
override fun extractMediaEnvironment(cost: Long, simulate: Boolean): Long {
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
package gay.`object`.hexdebug.casting.eval
22

3-
import at.petrak.hexcasting.api.casting.castables.Action
43
import at.petrak.hexcasting.api.casting.eval.env.StaffCastEnv
54
import at.petrak.hexcasting.api.casting.eval.sideeffects.OperatorSideEffect
6-
import gay.`object`.hexdebug.core.api.debugging.DebugStepType
5+
import gay.`object`.hexdebug.core.api.debugging.IDebuggableCastEnv
76
import net.minecraft.network.chat.Component
87
import net.minecraft.server.level.ServerPlayer
98
import net.minecraft.world.InteractionHand
109

1110
class EvaluatorCastEnv(
1211
caster: ServerPlayer,
1312
castingHand: InteractionHand,
14-
) : StaffCastEnv(caster, castingHand), IDebugCastEnv {
15-
override var threadId: Int? = null
16-
17-
override var lastEvaluatedAction: Action? = null
18-
override var lastDebugStepType: DebugStepType? = null
19-
13+
) : StaffCastEnv(caster, castingHand), IDebuggableCastEnv {
2014
override fun printMessage(message: Component) {
2115
super.printMessage(message)
22-
printDebugMessage(caster, message)
16+
debugEnv?.printDebugMessage(message)
2317
}
2418

2519
override fun sendMishapMsgToPlayer(mishap: OperatorSideEffect.DoMishap) {
2620
super.sendMishapMsgToPlayer(mishap)
27-
printDebugMishap(this, caster, mishap)
21+
debugEnv?.printDebugMishap(this, mishap)
2822
}
2923
}

Common/src/main/kotlin/gay/object/hexdebug/casting/eval/IDebugCastEnv.kt

Lines changed: 0 additions & 44 deletions
This file was deleted.

Common/src/main/kotlin/gay/object/hexdebug/debugger/CastArgs.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package gay.`object`.hexdebug.debugger
22

33
import at.petrak.hexcasting.api.casting.eval.CastingEnvironment
44
import at.petrak.hexcasting.api.casting.iota.Iota
5+
import gay.`object`.hexdebug.core.api.debugging.DebugEnvironment
56
import net.minecraft.server.level.ServerLevel
67

78
data class CastArgs(
89
val iotas: List<Iota>,
10+
val debugEnv: DebugEnvironment,
911
val env: CastingEnvironment,
1012
val world: ServerLevel,
1113
val onExecute: ((Iota) -> Unit)? = null,

Common/src/main/kotlin/gay/object/hexdebug/debugger/DebugStepResult.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import org.eclipse.lsp4j.debug.Source
66
import org.eclipse.lsp4j.debug.LoadedSourceEventArgumentsReason as LoadedSourceReason
77

88
data class DebugStepResult(
9-
val reason: StopReason,
9+
/** If null, the debuggee has continued instead of stopping. */
10+
val reason: StopReason?,
1011
val type: DebugStepType? = null,
1112
val clientInfo: ExecutionClientView? = null,
1213
val loadedSources: Map<Source, LoadedSourceReason> = mapOf(),

0 commit comments

Comments
 (0)