Skip to content

Commit

Permalink
Added mod command line option
Browse files Browse the repository at this point in the history
  • Loading branch information
Hop311 committed Jan 14, 2024
1 parent dd008b3 commit dcd3107
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 21 deletions.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,11 @@ bin/*
!game/common/map/*.obj

# JetBrains
.idea
.idea

# scons stuff
.cache
compile_commands.json

# ccls
.ccls-cache
4 changes: 3 additions & 1 deletion game/src/Game/Autoload/Argument/ArgumentOption.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extends Resource
TYPE_FLOAT: default_value = 0.0
TYPE_STRING: default_value = ""
TYPE_STRING_NAME: default_value = &""
TYPE_PACKED_STRING_ARRAY: default_value = PackedStringArray()
TYPE_COLOR: default_value = Color()
_: default_value = null
notify_property_list_changed()
Expand All @@ -38,6 +39,7 @@ func get_type_string() -> StringName:
TYPE_INT: return "integer"
TYPE_FLOAT: return "float"
TYPE_STRING, TYPE_STRING_NAME: return "string"
TYPE_PACKED_STRING_ARRAY: return "string array"
TYPE_COLOR: return "color"
return "<invalid type>"

Expand All @@ -52,7 +54,7 @@ func _set(property : StringName, value : Variant) -> bool:
return false

func _get_property_list() -> Array[Dictionary]:
var properties := []
var properties := [] as Array[Dictionary]

properties.append({
"name": "default_value",
Expand Down
30 changes: 19 additions & 11 deletions game/src/Game/Autoload/Argument/ArgumentParser.gd
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func _parse_value(arg_name : StringName, value_string : String, type : Variant.T
return value_string.to_float()
push_error("'%s' must be a valid float, '%s' is an invalid value." % [arg_name, value_string])
return null
TYPE_STRING, TYPE_STRING_NAME:
TYPE_STRING, TYPE_STRING_NAME, TYPE_PACKED_STRING_ARRAY:
return value_string
TYPE_COLOR:
if Color.html_is_valid(value_string) or value_string.to_lower() in color_name_array:
Expand Down Expand Up @@ -196,24 +196,30 @@ func _parse_value(arg_name : StringName, value_string : String, type : Variant.T
# TYPE_PACKED_INT64_ARRAY = 31
# TYPE_PACKED_FLOAT32_ARRAY = 32
# TYPE_PACKED_FLOAT64_ARRAY = 33
# TYPE_PACKED_STRING_ARRAY = 34
# TYPE_PACKED_VECTOR2_ARRAY = 35
# TYPE_PACKED_VECTOR3_ARRAY = 36
# TYPE_PACKED_COLOR_ARRAY = 37

func _add_argument(dictionary : Dictionary, option : ArgumentOption, argument : Variant) -> void:
if option.type == TYPE_PACKED_STRING_ARRAY:
dictionary[option.name] += PackedStringArray([argument])
else:
dictionary[option.name] = argument


func _parse_argument_list(dictionary : Dictionary, arg_list : PackedStringArray) -> Dictionary:
var current_key : String = ""
var current_option : ArgumentOption = null
for arg in arg_list:
if current_option != null and not arg.begins_with("-"):
var result : Variant = _parse_value(current_key, arg, current_option.type)
if result != null:
dictionary[current_option.name] = result
current_option = null
continue

if current_option != null:
push_warning("Valid argument '%s' was not set as a value, skipping." % current_key)
if not arg.begins_with("-"):
var result : Variant = _parse_value(current_key, arg, current_option.type)
if result != null:
_add_argument(dictionary, current_option, result)
current_option = null
continue
else:
push_warning("Valid argument '%s' was not set as a value, skipping." % current_key)

if arg.begins_with("-"):
current_option = null
Expand Down Expand Up @@ -270,12 +276,14 @@ func _parse_argument_list(dictionary : Dictionary, arg_list : PackedStringArray)
if first_equal > -1:
var arg_result : Variant = _parse_value(key, value, current_option.type)
if arg_result != null:
dictionary[current_option.name] = arg_result
_add_argument(dictionary, current_option, 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)
else:
push_warning("Non argument '%s' found, skipping." % arg)

return dictionary

Expand Down
16 changes: 12 additions & 4 deletions game/src/Game/Autoload/Argument/ArgumentParser.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=7 format=3 uid="uid://dayjmgc34tqo6"]
[gd_scene load_steps=8 format=3 uid="uid://dayjmgc34tqo6"]

[ext_resource type="Script" path="res://src/Game/Autoload/Argument/ArgumentParser.gd" id="1_pc7xr"]
[ext_resource type="Script" path="res://src/Game/Autoload/Argument/ArgumentOption.gd" id="2_4hguj"]
Expand All @@ -22,20 +22,28 @@ default_value = false
[sub_resource type="Resource" id="Resource_tiax1"]
script = ExtResource("2_4hguj")
name = &"base-path"
aliases = Array[StringName]([])
aliases = Array[StringName]([&"b"])
type = 4
description = "Load Victoria 2 assets from this exact path."
default_value = ""

[sub_resource type="Resource" id="Resource_sh3m3"]
script = ExtResource("2_4hguj")
name = &"search-path"
aliases = Array[StringName]([])
aliases = Array[StringName]([&"s"])
type = 4
description = "Search for Victoria 2 assets at this path."
default_value = ""

[sub_resource type="Resource" id="Resource_8ab4j"]
script = ExtResource("2_4hguj")
name = &"mod"
aliases = Array[StringName]([&"m"])
type = 34
description = "Load Victoria 2 mods with these names."
default_value = PackedStringArray()

[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"), SubResource("Resource_sh3m3")])
option_array = Array[ExtResource("2_4hguj")]([SubResource("Resource_tq3y4"), SubResource("Resource_j1to4"), SubResource("Resource_tiax1"), SubResource("Resource_sh3m3"), SubResource("Resource_8ab4j")])
11 changes: 8 additions & 3 deletions game/src/Game/GameStart.gd
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func _load_compatibility_mode() -> void:
push_warning("Exact base path and search base path arguments both used:\nBase: ", arg_base_path, "\nSearch: ", arg_search_path)
actual_base_path = arg_base_path
elif arg_search_path:
# This will also search for a Steam install if the hint doesn't help
actual_base_path = GameSingleton.search_for_game_path(arg_search_path)
if not actual_base_path:
push_warning("Failed to find assets using search hint: ", arg_search_path)
Expand All @@ -65,7 +66,9 @@ func _load_compatibility_mode() -> void:
if _settings_base_path:
actual_base_path = _settings_base_path
else:
actual_base_path = GameSingleton.search_for_game_path()
# Check if the program is being run from inside the install directory,
# and if not also search for a Steam install
actual_base_path = GameSingleton.search_for_game_path("..")
if not actual_base_path:
var title : String = "Failed to find game asset path!"
var msg : String = "The path can be specified with the \"base-path\" command line option."
Expand All @@ -80,8 +83,10 @@ func _load_compatibility_mode() -> void:

var paths : PackedStringArray = [actual_base_path]

# Example for adding mod paths
#paths.push_back(actual_base_path + "/mod/TGC")
# Add mod paths
var settings_mod_names : PackedStringArray = ArgumentParser.get_argument(&"mod", "")
for mod_name : String in settings_mod_names:
paths.push_back(actual_base_path + "/mod/" + mod_name)

if GameSingleton.load_defines_compatibility_mode(paths) != OK:
push_error("Errors loading game defines!")
Expand Down

0 comments on commit dcd3107

Please sign in to comment.