hamncheese/hamncheese/Scripts/main.gd
2023-09-21 19:49:34 -05:00

151 lines
4.2 KiB
GDScript

# Things to do:
# - Embed the "edge" binary and extract it if needed.
#
# Issues to watch:
# - https://github.com/ntop/n2n/issues/1090 (P2P connections still occupy server traffic)
# - https://github.com/ntop/n2n/issues/1087 (Slow shutdown on Windows)
# - https://github.com/ntop/n2n/issues/1082 (A large ping value every 20 seconds)
# - https://github.com/ntop/n2n/issues/610 (n2nGaming Application?)
# - https://github.com/ntop/n2n/issues/934 (Upnp is missing)
#
extends Control
# This port, and the next 255 ports, will be used.
# PEER_BASE_PORT Process Password Listening Port.
# PEER_BASE_PORT + 1 Unused Listening Port for Incoming Edge Management Data.
# PEER_BASE_PORT + 2 From here on, mesh peer sending ports and our mesh listening port.
const PEER_BASE_PORT := 20000
const FILE_ID_SETTINGS = 0
const FILE_ID_EXIT = 2
const HELP_ID_ABOUT = 0
const HELP_ID_MANUAL = 1
@onready var online_check_button = %OnlineCheckButton
@onready var my_ip_label = %MyIPLabel
@onready var peers_tree = %PeersTree
var _ip := ""
func _clear_ui():
my_ip_label.text = ""
if peers_tree.get_root() != null:
peers_tree.clear()
func _data_recieved(type, data):
#print(type, ": ", data)
if type == "info":
_ip = data[0]["ip4addr"]
my_ip_label.text = _ip
if !Peers.is_running():
# This is PEER_BASE_PORT + 1 because there will never be a peer ID of 0
# so the first usable port is therefore PEER_BASE_PORT + 2.
Peers.start_server(_ip, PEER_BASE_PORT + 1, Settings.user_name_line_edit.text, Settings.uuid)
if type == "edges":
if Peers.update(data):
_draw_tree()
# Handle empty peer list.
if data.size() == 0 and peers_tree.get_root() != null:
peers_tree.clear()
func _draw_tree():
# Redraw peer tree.
peers_tree.clear()
#***TODO**** We should really be displaying things in columns.
var root = peers_tree.create_item()
for peer in Peers.peerArray:
if peer["online"] or true:
var debug = str(peer["online"]) + " "
if peer["tcp"] == null:
debug = debug + "NULL"
else:
match peer["tcp"].get_status():
StreamPeerTCP.STATUS_NONE:
debug = debug + "None"
StreamPeerTCP.STATUS_CONNECTING:
debug = debug + "Connecting"
StreamPeerTCP.STATUS_CONNECTED:
debug = debug + "Connected " + peer["tcp"].get_connected_host()
StreamPeerTCP.STATUS_ERROR:
debug = debug + "Error"
_:
debug = debug + "Unknown"
var child = peers_tree.create_item(root)
child.set_text(0, peer["user"] + " (" + peer["ip"] + ") " + debug)
func _go_offline():
if Peers.is_running():
Peers.stop_server()
Edge.stop_edge()
_clear_ui()
func _network_died():
if Peers.is_running():
Peers.stop_server()
online_check_button.set_pressed_no_signal(false)
_clear_ui()
await Dialog.alert("Error", "The network layer unexpectedly stopped.")
func _notification(what):
if what == NOTIFICATION_WM_CLOSE_REQUEST:
_show_exit_dialog()
func _on_file_id_pressed(id):
match id:
FILE_ID_SETTINGS:
Settings.settings_window.visible = true
Settings.settings_window.move_to_foreground()
FILE_ID_EXIT:
_show_exit_dialog()
func _on_help_id_pressed(id):
match id:
HELP_ID_MANUAL:
Manual.manual_window.visible = true
Manual.manual_window.move_to_foreground()
HELP_ID_ABOUT:
About.about_window.visible = true
About.about_window.move_to_foreground()
func _on_online_check_button_toggled(toggled_on):
if toggled_on:
#***TODO*** Not all Edge options are supported yet.
if await Edge.start_edge(Settings.network_name_line_edit.text, Settings.network_password_line_edit.text, Settings.server_line_edit.text, Settings.server_port_spin_box.value, PEER_BASE_PORT + 1) == false:
online_check_button.set_pressed_no_signal(false)
else:
_go_offline()
func _ready():
Edge.data_recieved.connect(_data_recieved)
Edge.network_died.connect(_network_died)
Process.startup(PEER_BASE_PORT)
func _show_exit_dialog():
var message = "Are you sure you wish to exit?"
if online_check_button.button_pressed:
message = message + "\n\nThis will disconnect you from the network!\n "
var result = await Dialog.confirm("Exit", message)
if result:
if online_check_button.button_pressed:
_go_offline()
Process.shutdown()
Engine.get_main_loop().quit()