Added argument parser, globalized variables

This commit is contained in:
Patrick_Pluto 2024-12-04 09:42:04 +01:00
parent 29f522ebb8
commit cb24b0b629
6 changed files with 52 additions and 28 deletions

View file

@ -11,6 +11,7 @@ config_version=5
[application] [application]
config/name="Hotel Madness" config/name="Hotel Madness"
run/main_scene="res://start.tscn"
config/features=PackedStringArray("4.3", "Forward Plus") config/features=PackedStringArray("4.3", "Forward Plus")
config/icon="res://icon.svg" config/icon="res://icon.svg"
@ -18,7 +19,7 @@ config/icon="res://icon.svg"
Log="*res://scripts/utils/log.gd" Log="*res://scripts/utils/log.gd"
Networking="*res://scripts/utils/networking.gd" Networking="*res://scripts/utils/networking.gd"
Test="*res://scripts/utils/test.gd" Game="*res://scripts/utils/game.gd"
[debug] [debug]
@ -64,3 +65,7 @@ gdscript/warnings/confusable_capture_reassignment=2
gdscript/warnings/property_used_as_function=2 gdscript/warnings/property_used_as_function=2
gdscript/warnings/constant_used_as_function=2 gdscript/warnings/constant_used_as_function=2
gdscript/warnings/function_used_as_property=2 gdscript/warnings/function_used_as_property=2
[editor]
run/main_run_args="--testmode --logmode 4"

36
scripts/utils/game.gd Normal file
View file

@ -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")

View file

@ -6,7 +6,6 @@ extends Node
# 2 -> Errors/Warnings # 2 -> Errors/Warnings
# 3 -> Errors/Warnings/Infos # 3 -> Errors/Warnings/Infos
# 4 -> All # 4 -> All
var logmode: int = 4
func test() -> void: func test() -> void:
debug("Test") debug("Test")
@ -17,7 +16,7 @@ func test() -> void:
func error(message: String, alert_message: String) -> void: func error(message: String, alert_message: String) -> void:
if message == "": if message == "":
message = "Empty message string passed to an error message!" message = "Empty message string passed to an error message!"
if logmode >= 1: if Game.logmode >= 1:
printerr("[ERROR] " + message) printerr("[ERROR] " + message)
OS.alert(alert_message, "Error!") OS.alert(alert_message, "Error!")
if OS.shell_open(OS.get_user_data_dir()) != OK: 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: func warning(message: String) -> void:
if message == "": if message == "":
message = "Empty message string passed to a warning message!" message = "Empty message string passed to a warning message!"
if logmode >= 2: if Game.logmode >= 2:
printerr("[WARNING] " + message) printerr("[WARNING] " + message)
# Used for simple info. # Used for simple info.
@ -38,7 +37,7 @@ func info(message: String) -> void:
if message == "": if message == "":
warning("Empty message string passed to an info message!") warning("Empty message string passed to an info message!")
return return
if logmode >= 3: if Game.logmode >= 3:
print("[INFO] " + message) print("[INFO] " + message)
# Used for debugging. # Used for debugging.
@ -46,5 +45,5 @@ func debug(message: String) -> void:
if message == "": if message == "":
warning("Empty message string passed to a debug message!") warning("Empty message string passed to a debug message!")
return return
if logmode >= 4: if Game.logmode >= 4:
print("[DEBUG] " + message) print("[DEBUG] " + message)

View file

@ -1,9 +1,5 @@
extends Node extends Node
const IP_ADDRESS: String = "127.0.0.1"
const PORT: int = 25262
const MAX_CLIENTS: int = 1024
func test() -> void: func test() -> void:
start_server() start_server()
join_server() join_server()
@ -12,20 +8,20 @@ func test() -> void:
# Start the network listener. # Start the network listener.
func start_server() -> void: func start_server() -> void:
var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new() var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
if peer.create_server(PORT, MAX_CLIENTS) != OK: if peer.create_server(Game.PORT, Game.MAX_CLIENTS) != OK:
Log.warning("Couldn't create the server at port %d!" % PORT) Log.warning("Couldn't create the server at port %d!" % Game.PORT)
return return
multiplayer.multiplayer_peer = peer 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. # Connect to a server.
func join_server() -> void: func join_server() -> void:
var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new() var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
if peer.create_client(IP_ADDRESS, PORT) != OK: if peer.create_client(Game.IP_ADDRESS, Game.PORT) != OK:
Log.warning("Couldn't connect to the server at %s:%d!" % [IP_ADDRESS, PORT]) Log.warning("Couldn't connect to the server at %s:%d!" % [Game.IP_ADDRESS, Game.PORT])
return return
multiplayer.multiplayer_peer = peer 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. # Close all network connections.
func close_network() -> void: func close_network() -> void:

View file

@ -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")