@@ -10,14 +10,15 @@ import at.petrak.hexcasting.api.spell.math.HexPattern
1010import at.petrak.hexcasting.common.network.MsgNewSpellPatternAck
1111import at.petrak.hexcasting.xplat.IXplatAbstractions
1212import gay.`object`.hexdebug.HexDebug
13- import gay.`object`.hexdebug.adapter.DebugAdapterState.*
13+ import gay.`object`.hexdebug.adapter.DebugAdapterState.Debugging
14+ import gay.`object`.hexdebug.adapter.DebugAdapterState.NotDebugging
1415import gay.`object`.hexdebug.adapter.proxy.DebugProxyServerLauncher
1516import gay.`object`.hexdebug.debugger.*
1617import gay.`object`.hexdebug.items.ItemDebugger
1718import gay.`object`.hexdebug.items.ItemEvaluator
18- import gay.`object`.hexdebug.networking.HexDebugNetworking
19- import gay.`object`.hexdebug.networking.MsgDebuggerStateS2C
20- import gay.`object`.hexdebug.networking.MsgEvaluatorStateS2C
19+ import gay.`object`.hexdebug.networking.msg.MsgDebuggerStateS2C
20+ import gay.`object`.hexdebug.networking.msg.MsgEvaluatorStateS2C
21+ import gay.`object`.hexdebug.networking.msg.MsgPrintDebuggerStatusS2C
2122import gay.`object`.hexdebug.utils.futureOf
2223import gay.`object`.hexdebug.utils.paginate
2324import gay.`object`.hexdebug.utils.toFuture
@@ -36,16 +37,14 @@ import java.util.concurrent.CompletableFuture
3637import java.util.concurrent.CompletionException
3738
3839open class DebugAdapter (val player : ServerPlayer ) : IDebugProtocolServer {
39- private var state: DebugAdapterState = NotConnected
40+ private var state: DebugAdapterState = NotDebugging ()
4041 set(value) {
4142 field = value
4243 setDebuggerState(value)
4344 }
4445
4546 val isDebugging get() = state is Debugging
4647
47- val canStartDebugging get() = state is ReadyToDebug
48-
4948 val debugger get() = (state as ? Debugging )?.debugger
5049
5150 open val launcher: IHexDebugLauncher by lazy {
@@ -62,7 +61,7 @@ open class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
6261 )
6362
6463 protected open fun setDebuggerState (debuggerState : ItemDebugger .DebugState ) {
65- HexDebugNetworking .sendToPlayer(player, MsgDebuggerStateS2C (debuggerState))
64+ MsgDebuggerStateS2C (debuggerState).sendToPlayer(player )
6665
6766 // close the debugger grid if we stopped debugging
6867 if (debuggerState == ItemDebugger .DebugState .NOT_DEBUGGING ) {
@@ -72,21 +71,29 @@ open class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
7271 }
7372
7473 protected open fun setEvaluatorState (evalState : ItemEvaluator .EvalState ) {
75- HexDebugNetworking .sendToPlayer(player, MsgEvaluatorStateS2C (evalState))
74+ MsgEvaluatorStateS2C (evalState).sendToPlayer(player)
75+ }
76+
77+ protected open fun printDebuggerStatus (iota : String , index : Int ) {
78+ MsgPrintDebuggerStatusS2C (
79+ iota = iota,
80+ index = index,
81+ line = state.initArgs.indexToLine(index),
82+ isConnected = state.isConnected,
83+ ).sendToPlayer(player)
7684 }
7785
7886 fun startDebugging (args : CastArgs ): Boolean {
79- val state = state as ? NotDebugging ? : return false
80- this . state = Debugging (state, args)
81- remoteProxy.initialized( )
87+ if ( state is Debugging ) return false
88+ val state = Debugging (state, args). also { state = it }
89+ handleDebuggerStep(state.debugger.start() )
8290 return true
8391 }
8492
8593 fun disconnectClient () {
86- if (state !is NotConnected ) {
94+ if (state.isConnected ) {
8795 remoteProxy.terminated(TerminatedEventArguments ())
88-
89- state = NotConnected
96+ state.isConnected = false
9097 }
9198 }
9299
@@ -99,11 +106,7 @@ open class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
99106 it.category = category
100107 it.output = value
101108 if (withSource) {
102- debugger?.lastEvaluatedMetadata?.also { meta ->
103- it.source = meta.source
104- it.line = meta.line
105- if (meta.column != null ) it.column = meta.column
106- }
109+ it.setSourceAndPosition(state.initArgs, debugger?.lastEvaluatedMetadata)
107110 }
108111 })
109112 }
@@ -123,7 +126,7 @@ open class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
123126 fun resetEvaluator () {
124127 setEvaluatorState(ItemEvaluator .EvalState .DEFAULT )
125128 if (debugger?.resetEvaluator() == true ) {
126- sendStoppedEvent(" step " )
129+ sendStoppedEvent(StopReason . STEP )
127130 }
128131 }
129132
@@ -146,6 +149,7 @@ open class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
146149 IXplatAbstractions .INSTANCE .sendPacketToPlayer(player, MsgNewSpellPatternAck (it, - 1 ))
147150 }
148151
152+ // TODO: set nonzero exit code if we hit a mishap
149153 if (result.isDone) {
150154 terminate(null )
151155 return view
@@ -158,14 +162,19 @@ open class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
158162 })
159163 }
160164
161- sendStoppedEvent(result.reason.value)
165+ sendStoppedEvent(result.reason)
166+
167+ debugger?.getNextIotaToEvaluate()?.also { (iota, index) ->
168+ printDebuggerStatus(iota, index)
169+ }
170+
162171 return view
163172 }
164173
165- private fun sendStoppedEvent (reason : String ) {
174+ private fun sendStoppedEvent (reason : StopReason ) {
166175 remoteProxy.stopped(StoppedEventArguments ().also {
167176 it.threadId = 0
168- it.reason = reason
177+ it.reason = reason.value
169178 })
170179 }
171180
@@ -180,7 +189,7 @@ open class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
180189 // initialization
181190
182191 override fun initialize (args : InitializeRequestArguments ): CompletableFuture <Capabilities > {
183- state = Initialized ( args)
192+ state.initArgs = args
184193 return Capabilities ().apply {
185194 supportsConfigurationDoneRequest = true
186195 supportsLoadedSourcesRequest = true
@@ -205,8 +214,11 @@ open class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
205214 }
206215
207216 override fun attach (args : MutableMap <String , Any >): CompletableFuture <Void > {
208- val initArgs = state.initArgs ? : return futureOf()
209- state = NotDebugging (initArgs, LaunchArgs (args))
217+ state.apply {
218+ isConnected = true
219+ launchArgs = LaunchArgs (args)
220+ }
221+ remoteProxy.initialized()
210222 player.displayClientMessage(Component .translatable(" text.hexdebug.connected" ), true )
211223 return futureOf()
212224 }
@@ -233,7 +245,7 @@ open class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
233245
234246 override fun configurationDone (args : ConfigurationDoneArguments ? ): CompletableFuture <Void > {
235247 knownPlayers.add(player.uuid)
236- debugger?.start()?. also (::handleDebuggerStep )
248+ if (isDebugging) sendStoppedEvent( StopReason . STEP )
237249 return futureOf()
238250 }
239251
@@ -265,34 +277,28 @@ open class DebugAdapter(val player: ServerPlayer) : IDebugProtocolServer {
265277 }
266278
267279 override fun restart (args : RestartArguments ? ): CompletableFuture <Void > {
268- val state = state
269- if (state is ReadyToDebug ) {
270- this .state = NotDebugging (state)
271- state.restartArgs?.run (::startDebugging)
272- }
280+ state = NotDebugging (state)
281+ state.restartArgs?.run (::startDebugging)
273282 return futureOf()
274283 }
275284
276285 override fun terminate (args : TerminateArguments ? ): CompletableFuture <Void > {
277- val state = state
278- if (state is ReadyToDebug ) {
279- this .state = NotDebugging (state)
280- remoteProxy.exited(ExitedEventArguments ().also { it.exitCode = 0 })
281- if (state is Debugging ) {
282- for (source in state.debugger.getSources()) {
283- remoteProxy.loadedSource(LoadedSourceEventArguments ().also {
284- it.source = source
285- it.reason = LoadedSourceEventArgumentsReason .REMOVED
286- })
287- }
286+ debugger?.let { debugger ->
287+ for (source in debugger.getSources()) {
288+ remoteProxy.loadedSource(LoadedSourceEventArguments ().also {
289+ it.source = source
290+ it.reason = LoadedSourceEventArgumentsReason .REMOVED
291+ })
288292 }
289- remoteProxy.invalidated(InvalidatedEventArguments ())
290293 }
294+ remoteProxy.invalidated(InvalidatedEventArguments ())
295+ remoteProxy.exited(ExitedEventArguments ().also { it.exitCode = 0 })
296+ state = NotDebugging (state)
291297 return futureOf()
292298 }
293299
294300 override fun disconnect (args : DisconnectArguments ? ): CompletableFuture <Void > {
295- state = NotConnected
301+ state.isConnected = false
296302 return futureOf()
297303 }
298304
0 commit comments