StreamOverlay/addons/sc_editor/plugin.gd

93 lines
2.7 KiB
GDScript3
Raw Permalink Normal View History

@tool
extends EditorPlugin
var _socket: WebSocketPeer
var connection_state: WebSocketPeer.State = WebSocketPeer.STATE_CLOSED
var auto_reconnect: bool = false
var connected: bool = false
func _enable_plugin() -> void:
pass
func _disable_plugin() -> void:
pass
func _enter_tree() -> void:
print("Plugin enabled")
_socket = WebSocketPeer.new()
connection_state = WebSocketPeer.STATE_CONNECTING
_socket.connect_to_url("localhost:8765")
func _exit_tree() -> void:
print("Plugin disabled")
if connection_state == WebSocketPeer.STATE_OPEN:
_socket.close(1000, "Plugin Disabled")
_socket = null
func _process(d: float) -> void:
if connection_state == WebSocketPeer.STATE_CLOSED: return
_socket.poll()
var state = _socket.get_ready_state()
if state == WebSocketPeer.STATE_CLOSED and not auto_reconnect:
if connected:
print("Connection closed to StreamController, NOT reconnecting.")
connected = false
connection_state = state
return
if state == WebSocketPeer.STATE_CLOSED and auto_reconnect:
print("Connection lost to StreamController, attempting to reconnect...")
_socket.connect_to_url("localhost:8765")
connection_state = WebSocketPeer.STATE_CONNECTING
return
if state == WebSocketPeer.STATE_OPEN and not connected:
print("Connecting to StreamController.")
connected = true
connection_state = state
if state == WebSocketPeer.STATE_OPEN:
_read_data()
func _read_data() -> void:
while (_socket.get_available_packet_count()):
var pckt = _socket.get_packet().get_string_from_utf8()
var cmd := JSON.parse_string(pckt)
if cmd == null or cmd is not Dictionary:
print("Invalid Packet received: %s" % pckt)
continue
if not cmd.has("identifier") or not cmd.has("action"):
print("Invalid command received: %s" % cmd)
continue
if cmd.identifier != "org.godotengine.sc-plugin":
continue
if cmd.action == "run-main":
EditorInterface.play_main_scene()
elif cmd.action == "run-current":
EditorInterface.play_current_scene()
elif cmd.action == "run-scene":
EditorInterface.play_custom_scene(cmd.arguments)
elif cmd.action == "stop-run":
if EditorInterface.is_playing_scene():
EditorInterface.stop_playing_scene()
elif cmd.action == "save-project":
EditorInterface.save_all_scenes()
elif cmd.action == "show-2d":
EditorInterface.set_main_screen_editor("2D")
elif cmd.action == "show-3d":
EditorInterface.set_main_screen_editor("3D")
elif cmd.action == "show-script":
EditorInterface.set_main_screen_editor("Script")
elif cmd.action == "show-assetlib":
EditorInterface.set_main_screen_editor("AssetLib")
elif cmd.action == "restart-editor-no-save":
EditorInterface.restart_editor(false)
elif cmd.action == "restart-editor-save":
EditorInterface.restart_editor(true)