diff options
author | George L. Albany <Megacake1234@gmail.com> | 2023-06-14 05:29:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-14 05:29:21 +0200 |
commit | 40cf0fa95e325f3bf875e42c11254da23192f506 (patch) | |
tree | e52a31ca0a52522c2ca08a6ddfdc1e8f9507c3dc | |
parent | acda3217349a0ebdb8b48a3787cb15d8eb250e7c (diff) | |
parent | cd7a5260d4d2791f98f284cf0bea905bda18cbe8 (diff) |
Merge pull request #133 from Spartan322/argparser-corrections
-rw-r--r-- | game/src/Game/Autoload/Argument/ArgumentParser.gd | 80 | ||||
-rw-r--r-- | game/src/Game/Autoload/Argument/ArgumentParser.tscn | 5 | ||||
-rw-r--r-- | game/src/Game/Autoload/Events.gd | 5 | ||||
-rw-r--r-- | game/src/Game/Autoload/Events/GameDebug.gd | 17 |
4 files changed, 71 insertions, 36 deletions
diff --git a/game/src/Game/Autoload/Argument/ArgumentParser.gd b/game/src/Game/Autoload/Argument/ArgumentParser.gd index ce89dd8..123a733 100644 --- a/game/src/Game/Autoload/Argument/ArgumentParser.gd +++ b/game/src/Game/Autoload/Argument/ArgumentParser.gd @@ -44,6 +44,48 @@ const color_name_array : PackedStringArray =[ "whitesmoke", "yellow", "yellowgreen" ] +func has_argument_support(arg_name : StringName, include_aliases : bool = false) -> bool: + return option_array.any( + func(opt): + if include_aliases and arg_name in opt.aliases: + return true + return opt.name == arg_name + ) + +func get_argument(arg_name : StringName, default_val : Variant = null) -> Variant: + if ProjectSettings.has_setting(argument_setting_path): + var arg_setting = ProjectSettings.get_setting_with_override(argument_setting_path) + if not arg_setting is Dictionary: + push_error("Argument setting is not a dictionary.") + return default_val + return arg_setting.get(arg_name, default_val) + else: + return default_val + +func set_argument(arg_name : StringName, value : Variant) -> void: + if ProjectSettings.has_setting(argument_setting_path): + var arg_setting = null + arg_setting = ProjectSettings.get_setting_with_override(argument_setting_path) + if not arg_setting is Dictionary: + push_error("Argument setting is not a dictionary.") + return + arg_setting[arg_name] = value + ProjectSettings.set_setting(argument_setting_path, arg_setting) + return + push_error("Argument setting has not been set yet.") + +func _set_argument_setting() -> void: + var argument_dictionary : Dictionary = {} + if ProjectSettings.has_setting(argument_setting_path): + argument_dictionary = ProjectSettings.get_setting_with_override(argument_setting_path) + for option in option_array: + argument_dictionary[option.name] = option.default_value + + _parse_argument_list(argument_dictionary, OS.get_cmdline_args()) + _parse_argument_list(argument_dictionary, OS.get_cmdline_user_args()) + + ProjectSettings.set_setting(argument_setting_path, argument_dictionary) + func _parse_value(arg_name : StringName, value_string : String, type : Variant.Type) -> Variant: match type: TYPE_NIL: return null @@ -183,22 +225,36 @@ func _parse_argument_list(dictionary : Dictionary, arg_list : PackedStringArray) # eg: "-abc" means a == true, b == true, c == true if arg.length() > 1 and arg[0] != "-" and arg[1] != "=": for c in arg: + if not ((c >= "a" and c <= "z") or (c >= "A" and c <= "Z")): + push_warning("Parsing shorthand alias containing '%s', perhaps you meant '--%s'? Skipping argument." % [c, arg]) + break + var alias_found := false for o in option_array: if o.aliases.any(func(v): return c == v): - dictionary[o.name] = true + if o.type == TYPE_BOOL: + dictionary[o.name] = true + else: + push_warning("Shorthand alias '%s is not boolean, skipping." % c) + # TODO: Support POSIX 12.1 2.b? + # https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html + break + alias_found = true + break + if not alias_found: + push_warning("Shorthand alias '%s' not found, skipping." % c) continue # Support for = key/value split # eg: "-v=5" and "--value=5" means v == 5 and value == 5 var first_equal := arg.find("=") if first_equal > -1: - key = arg.substr(0, first_equal - 1) + key = arg.substr(0, first_equal) value = arg.substr(first_equal + 1) else: key = arg # Removes - for full name arguments - if key.begins_with("-"): + if key.length() > 2 and key.begins_with("-"): key = key.substr(1) for o in option_array: @@ -216,6 +272,10 @@ func _parse_argument_list(dictionary : Dictionary, arg_list : PackedStringArray) if arg_result != null: dictionary[current_option.name] = arg_result current_option = null + elif current_option.type == TYPE_BOOL: + dictionary[current_option.name] = true + else: + push_warning("Argument '%s' treated like a boolean but does not support a boolean value, skipping." % key) return dictionary @@ -251,17 +311,7 @@ Options: ]) func _ready(): if Engine.is_editor_hint(): return - - var argument_dictionary : Dictionary = {} - if ProjectSettings.has_setting(argument_setting_path): - argument_dictionary = ProjectSettings.get_setting_with_override(argument_setting_path) - for option in option_array: - argument_dictionary[option.name] = option.default_value - - _parse_argument_list(argument_dictionary, OS.get_cmdline_args()) - _parse_argument_list(argument_dictionary, OS.get_cmdline_user_args()) - - ProjectSettings.set_setting(argument_setting_path, argument_dictionary) - if argument_dictionary[&"help"]: + _set_argument_setting() + if get_argument(&"help"): _print_help() get_tree().quit() diff --git a/game/src/Game/Autoload/Argument/ArgumentParser.tscn b/game/src/Game/Autoload/Argument/ArgumentParser.tscn index 09e5e6c..84ebd50 100644 --- a/game/src/Game/Autoload/Argument/ArgumentParser.tscn +++ b/game/src/Game/Autoload/Argument/ArgumentParser.tscn @@ -14,7 +14,7 @@ default_value = false [sub_resource type="Resource" id="Resource_j1to4"] script = ExtResource("2_4hguj") name = &"game-debug" -aliases = Array[StringName]([&"d", &"-debug", &"-debug-mode"]) +aliases = Array[StringName]([&"d", &"debug", &"debug-mode"]) type = 1 description = "Start in debug mode." default_value = false @@ -22,11 +22,12 @@ default_value = false [sub_resource type="Resource" id="Resource_tiax1"] script = ExtResource("2_4hguj") name = &"compatibility-mode" -aliases = Array[StringName]([&"-compat"]) +aliases = Array[StringName]([&"compat"]) type = 4 description = "Load Victoria 2 assets from this path." default_value = "" [node name="ArgumentParser" type="Node"] +editor_description = "SS-56" script = ExtResource("1_pc7xr") option_array = Array[ExtResource("2_4hguj")]([SubResource("Resource_tq3y4"), SubResource("Resource_j1to4"), SubResource("Resource_tiax1")]) diff --git a/game/src/Game/Autoload/Events.gd b/game/src/Game/Autoload/Events.gd index 786ece4..f979301 100644 --- a/game/src/Game/Autoload/Events.gd +++ b/game/src/Game/Autoload/Events.gd @@ -28,10 +28,7 @@ func load_events(loading_screen: LoadingScreen): # into the mod's dir for a temporary fix) # Usage: OpenVic --compatibility-mode <path> - var compatibility_mode_path : String - if ProjectSettings.has_setting(ArgumentParser.argument_setting_path): - var arg_dictionary : Dictionary = ProjectSettings.get_setting(ArgumentParser.argument_setting_path) - compatibility_mode_path = arg_dictionary.get(&"compatibility-mode", compatibility_mode_path) + var compatibility_mode_path : String = ArgumentParser.get_argument(&"compatibility-mode") var start := Time.get_ticks_usec() diff --git a/game/src/Game/Autoload/Events/GameDebug.gd b/game/src/Game/Autoload/Events/GameDebug.gd index ea4a157..9e18343 100644 --- a/game/src/Game/Autoload/Events/GameDebug.gd +++ b/game/src/Game/Autoload/Events/GameDebug.gd @@ -1,22 +1,9 @@ extends RefCounted class_name GameDebugSingleton -# REQUIREMENTS: -# * SS-56 -func _init(): - for engine_args in OS.get_cmdline_args(): - match(engine_args): - "--game-debug": - set_debug_mode(true) - - for engine_args in OS.get_cmdline_user_args(): - match(engine_args): - "--game-debug", "-d", "--debug", "--debug-mode": - set_debug_mode(true) - func set_debug_mode(value : bool) -> void: - ProjectSettings.set_setting("openvic/debug/enabled", value) + ArgumentParser.set_argument(&"game-debug", value) print("Set debug mode to: ", value) func is_debug_mode() -> bool: - return ProjectSettings.get_setting("openvic/debug/enabled", false) + return ArgumentParser.get_argument(&"game-debug", false) |