From cb24b0b6294f0354f985e7b6367df128f950f647 Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Wed, 4 Dec 2024 09:42:04 +0100 Subject: [PATCH] Added argument parser, globalized variables --- project.godot | 7 ++++++- scripts/utils/game.gd | 36 ++++++++++++++++++++++++++++++++++++ scripts/utils/log.gd | 9 ++++----- scripts/utils/networking.gd | 16 ++++++---------- scripts/utils/test.gd | 12 ------------ test.tscn => start.tscn | 0 6 files changed, 52 insertions(+), 28 deletions(-) create mode 100644 scripts/utils/game.gd delete mode 100644 scripts/utils/test.gd rename test.tscn => start.tscn (100%) diff --git a/project.godot b/project.godot index 0913ef5..09741b5 100644 --- a/project.godot +++ b/project.godot @@ -11,6 +11,7 @@ config_version=5 [application] config/name="Hotel Madness" +run/main_scene="res://start.tscn" config/features=PackedStringArray("4.3", "Forward Plus") config/icon="res://icon.svg" @@ -18,7 +19,7 @@ config/icon="res://icon.svg" Log="*res://scripts/utils/log.gd" Networking="*res://scripts/utils/networking.gd" -Test="*res://scripts/utils/test.gd" +Game="*res://scripts/utils/game.gd" [debug] @@ -64,3 +65,7 @@ gdscript/warnings/confusable_capture_reassignment=2 gdscript/warnings/property_used_as_function=2 gdscript/warnings/constant_used_as_function=2 gdscript/warnings/function_used_as_property=2 + +[editor] + +run/main_run_args="--testmode --logmode 4" diff --git a/scripts/utils/game.gd b/scripts/utils/game.gd new file mode 100644 index 0000000..f7502f8 --- /dev/null +++ b/scripts/utils/game.gd @@ -0,0 +1,36 @@ +extends Node + +var testmode: bool = true +var errortest: bool = true + +var logmode: int = 4 + +const IP_ADDRESS: String = "127.0.0.1" +const PORT: int = 25262 +const MAX_CLIENTS: int = 1024 + +func _ready() -> void: + var args: PackedStringArray = OS.get_cmdline_args() + var skip: bool = false + for i: int in range(args.size()): + if !skip: + Log.info("Argument %s passed." % args[i]) + match args[i]: + "--testmode": + testmode = true + "--errortest": + errortest = true + "--logmode": + logmode = int(args[i+1]) + skip = true + _: + Log.warning("Unknown argument: %s!" % args[i]) + run_tests() + +func run_tests() -> void: + if testmode: + Log.info("Starting singleton tests.") + Networking.test() + Log.test() + if errortest: + Log.error("Test", "Test") diff --git a/scripts/utils/log.gd b/scripts/utils/log.gd index cc953f8..740a059 100644 --- a/scripts/utils/log.gd +++ b/scripts/utils/log.gd @@ -6,7 +6,6 @@ extends Node # 2 -> Errors/Warnings # 3 -> Errors/Warnings/Infos # 4 -> All -var logmode: int = 4 func test() -> void: debug("Test") @@ -17,7 +16,7 @@ func test() -> void: func error(message: String, alert_message: String) -> void: if message == "": message = "Empty message string passed to an error message!" - if logmode >= 1: + if Game.logmode >= 1: printerr("[ERROR] " + message) OS.alert(alert_message, "Error!") if OS.shell_open(OS.get_user_data_dir()) != OK: @@ -30,7 +29,7 @@ func error(message: String, alert_message: String) -> void: func warning(message: String) -> void: if message == "": message = "Empty message string passed to a warning message!" - if logmode >= 2: + if Game.logmode >= 2: printerr("[WARNING] " + message) # Used for simple info. @@ -38,7 +37,7 @@ func info(message: String) -> void: if message == "": warning("Empty message string passed to an info message!") return - if logmode >= 3: + if Game.logmode >= 3: print("[INFO] " + message) # Used for debugging. @@ -46,5 +45,5 @@ func debug(message: String) -> void: if message == "": warning("Empty message string passed to a debug message!") return - if logmode >= 4: + if Game.logmode >= 4: print("[DEBUG] " + message) diff --git a/scripts/utils/networking.gd b/scripts/utils/networking.gd index c4e36b7..508d0b5 100644 --- a/scripts/utils/networking.gd +++ b/scripts/utils/networking.gd @@ -1,9 +1,5 @@ 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() @@ -12,20 +8,20 @@ func test() -> void: # 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) + if peer.create_server(Game.PORT, Game.MAX_CLIENTS) != OK: + Log.warning("Couldn't create the server at port %d!" % Game.PORT) return multiplayer.multiplayer_peer = peer - Log.info("Created the server at port %d." % PORT) + Log.info("Created the server at port %d." % Game.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]) + if peer.create_client(Game.IP_ADDRESS, Game.PORT) != OK: + Log.warning("Couldn't connect to the server at %s:%d!" % [Game.IP_ADDRESS, Game.PORT]) return multiplayer.multiplayer_peer = peer - Log.info("Connected to the server at %s:%d." % [IP_ADDRESS, PORT]) + Log.info("Connected to the server at %s:%d." % [Game.IP_ADDRESS, Game.PORT]) # Close all network connections. func close_network() -> void: diff --git a/scripts/utils/test.gd b/scripts/utils/test.gd deleted file mode 100644 index 68a72b9..0000000 --- a/scripts/utils/test.gd +++ /dev/null @@ -1,12 +0,0 @@ -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") diff --git a/test.tscn b/start.tscn similarity index 100% rename from test.tscn rename to start.tscn