Files
SpaceBots/.svn/pristine/b7/b7136531db5d554460f782ce3b3a4d17cc51286a.svn-base

35 lines
848 B
Plaintext
Raw Normal View History

2026-01-21 23:51:53 +01:00
extends RefCounted
class_name NorayProtocolHandler
## This class parses incoming data from noray's protocol.
##
## Unless you're writing your own noray integration, [Noray] should cover most
## use cases.
## Emitted for every command parsed during a [method ingest] call.
signal on_command(command: String, data: String)
var _strbuf: String = ""
## Resets the parser.
func reset():
_strbuf = ""
## Parse an incoming piece of data.
func ingest(data: String):
_strbuf += data
if not _strbuf.contains("\n"):
return
var idx = _strbuf.rfind("\n")
var lines = _strbuf.substr(0, idx).split("\n", false)
_strbuf = _strbuf.erase(0, idx + 1)
for line in lines:
if not line.contains(" "):
on_command.emit(line, "")
else:
var parts = line.split(" ")
var command = parts[0]
var param = parts[1]
on_command.emit(command, param)