From 29f522ebb818c45017b2e0624139c958c46dc405 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 3 Dec 2024 21:44:07 +0100 Subject: [PATCH] Log fixups, initial networking setup and testing system. --- project.godot | 2 ++ scripts/utils/log.gd | 15 +++++++++++++-- scripts/utils/networking.gd | 33 +++++++++++++++++++++++++++++++++ scripts/utils/test.gd | 12 ++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 scripts/utils/networking.gd create mode 100644 scripts/utils/test.gd diff --git a/project.godot b/project.godot index 2f994a3..0913ef5 100644 --- a/project.godot +++ b/project.godot @@ -17,6 +17,8 @@ config/icon="res://icon.svg" [autoload] Log="*res://scripts/utils/log.gd" +Networking="*res://scripts/utils/networking.gd" +Test="*res://scripts/utils/test.gd" [debug] diff --git a/scripts/utils/log.gd b/scripts/utils/log.gd index ccf4750..cc953f8 100644 --- a/scripts/utils/log.gd +++ b/scripts/utils/log.gd @@ -8,32 +8,43 @@ extends Node # 4 -> All var logmode: int = 4 -func _ready() -> void: +func test() -> void: debug("Test") info("Test") warning("Test") - error("Test", "Test Message") # Used for errors. func error(message: String, alert_message: String) -> void: + if message == "": + message = "Empty message string passed to an error message!" if logmode >= 1: printerr("[ERROR] " + message) OS.alert(alert_message, "Error!") + if OS.shell_open(OS.get_user_data_dir()) != OK: + Log.warning("Couldn't open file explorer!") else: OS.alert("An error has occured. The program will now exit.", "Error!") get_tree().quit() # Used for warnings. func warning(message: String) -> void: + if message == "": + message = "Empty message string passed to a warning message!" if logmode >= 2: printerr("[WARNING] " + message) # Used for simple info. func info(message: String) -> void: + if message == "": + warning("Empty message string passed to an info message!") + return if logmode >= 3: print("[INFO] " + message) # Used for debugging. func debug(message: String) -> void: + if message == "": + warning("Empty message string passed to a debug message!") + return if logmode >= 4: print("[DEBUG] " + message) diff --git a/scripts/utils/networking.gd b/scripts/utils/networking.gd new file mode 100644 index 0000000..c4e36b7 --- /dev/null +++ b/scripts/utils/networking.gd @@ -0,0 +1,33 @@ +extends Node + +const IP_ADDRESS: String = "127.0.0.1" +const PORT: int = 25262 +const MAX_CLIENTS: int = 1024 + +func test() -> void: + start_server() + join_server() + close_network() + +# Start the network listener. +func start_server() -> void: + var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new() + if peer.create_server(PORT, MAX_CLIENTS) != OK: + Log.warning("Couldn't create the server at port %d!" % PORT) + return + multiplayer.multiplayer_peer = peer + Log.info("Created the server at port %d." % PORT) + +# Connect to a server. +func join_server() -> void: + var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new() + if peer.create_client(IP_ADDRESS, PORT) != OK: + Log.warning("Couldn't connect to the server at %s:%d!" % [IP_ADDRESS, PORT]) + return + multiplayer.multiplayer_peer = peer + Log.info("Connected to the server at %s:%d." % [IP_ADDRESS, PORT]) + +# Close all network connections. +func close_network() -> void: + multiplayer.multiplayer_peer = null + Log.info("Closed all network connections.") diff --git a/scripts/utils/test.gd b/scripts/utils/test.gd new file mode 100644 index 0000000..68a72b9 --- /dev/null +++ b/scripts/utils/test.gd @@ -0,0 +1,12 @@ +extends Node + +const testmode: bool = true +const errortest: bool = true + +func _ready() -> void: + if testmode: + Log.info("Starting singleton tests.") + Networking.test() + Log.test() + if errortest: + Log.error("Test", "Test")