4
0
Fork 0
WolfBox/scripts/host.gd

123 lines
4.1 KiB
GDScript3
Raw Permalink Normal View History

2025-05-31 22:21:22 +01:00
extends Control
@export var use_https: bool = false
@export var online: bool = true
@export var http_port: int = 2480
@export var multiplayer_port: int = 2481
var local_ip: String = "127.0.0.1"
var global_ip: String = "0.0.0.0"
var caddy_path: String = "/home/jimj316/Godot/JackIt/caddy/caddy"
var caddy_file_path: String = "/home/jimj316/Godot/JackIt/Caddyfile"
var caddy_pid: int = -1
var check_caddy_timer: float = 0
var upnp: UPNP = UPNP.new()
func _ready() -> void:
$JoinLocalBox.visible = false
$JoinInternetBox.visible = false
#$VBoxContainer.model = $"../Players"
# get the local address
for local_address in IP.get_local_addresses():
if local_address.begins_with("127") or local_address == "::1":
continue
else:
local_ip = local_address
break
var global_ok: bool = setup_global_connection()
get_parent().multiplayer.peer_connected.connect(on_peer_connected)
get_parent().multiplayer.peer_disconnected.connect(on_peer_disconnected)
start_caddy()
start_multiplayer()
func _process(delta: float) -> void:
check_caddy_timer += delta
if check_caddy_timer >= 1:
check_caddy_timer = 0
if !OS.is_process_running(caddy_pid):
start_caddy()
func start_caddy() -> void:
OS.set_environment("SITE_ADDRESS", local_ip)
print("Local IP address is %s" % local_ip)
#var caddy_args: PackedStringArray = ["file-server", "--root", "/home/jimj316/Godot/JackIt/build/web/", "--listen", ":%d"%port, "--domain", local_ip]
caddy_pid = OS.create_process(caddy_path, ["run", "--config", caddy_file_path])
#caddy_pid = OS.create_process(caddy_path, caddy_args)
print("Caddy created with PID %d" % caddy_pid)
#print("Caddy args: %s" % caddy_args)
var url: String = "http://%s:%d/" % [local_ip, http_port]
$JoinLocalBox/QRCodeRect.data = url
$JoinLocalBox/IPLabel.text = url
$JoinLocalBox.visible = true
func start_multiplayer() -> void:
var peer: MultiplayerPeer = WebSocketMultiplayerPeer.new()
var error: Error = peer.create_server(multiplayer_port)
if error != OK:
OS.alert("Failed to initialise server! Error: %d" % error)
get_parent().multiplayer.multiplayer_peer = peer
func setup_global_connection() -> bool:
var status: int = upnp.discover()
if status != UPNP.UPNPResult.UPNP_RESULT_SUCCESS:
return false
var gateway: UPNPDevice = upnp.get_gateway()
if gateway == null or !gateway.is_valid_gateway():
return false
status = gateway.add_port_mapping(http_port, http_port, "JackIt HTTP Port", "TCP", 0)
if status != UPNP.UPNPResult.UPNP_RESULT_SUCCESS: return false
#status = gateway.add_port_mapping(http_port, http_port, "JackIt HTTP Port", "UDP", 0)
#if status != UPNP.UPNPResult.UPNP_RESULT_SUCCESS: return false
status = gateway.add_port_mapping(multiplayer_port, multiplayer_port, "JackIt WS Port", "TCP", 0)
if status != UPNP.UPNPResult.UPNP_RESULT_SUCCESS: return false
#status = gateway.add_port_mapping(multiplayer_port, multiplayer_port, "JackIt WS Port", "UDP", 0)
#if status != UPNP.UPNPResult.UPNP_RESULT_SUCCESS: return false
global_ip = upnp.query_external_address()
var url: String = "http://%s:%d/" % [global_ip, http_port]
$JoinInternetBox/QRCodeRect.data = url
$JoinInternetBox/IPLabel.text = url
$JoinInternetBox.visible = true
return true
func _notification(what):
if what == NOTIFICATION_WM_CLOSE_REQUEST or what == NOTIFICATION_CRASH or what == NOTIFICATION_EXIT_TREE:
if caddy_pid != -1:
OS.kill(caddy_pid)
print("Caddy terminated with PID %d" % caddy_pid)
caddy_pid = -1
func on_peer_connected(id: int) -> void:
print("Peer connected with ID %d!" % id)
#send_player_list(id)
update_peer_list()
func on_peer_disconnected(id: int) -> void:
print("Peer disconnected with ID %d!" % id)
update_peer_list()
#func send_player_list(peer_id: int) -> void:
#for player in $"../Players".get_children():
#print("Sending player %d to peer %d" % [player.session_id, peer_id])
#$"../Players".create_player_for_session_id.rpc_id(peer_id, player.session_id)
func update_peer_list():
$PeerListLabel.text = ""
for peer_id in multiplayer.get_peers():
$PeerListLabel.text = $PeerListLabel.text + ("Peer %d\n" % peer_id)