feat: adds in max args checking; also removes duplicate command validation and now properly validates min_args; re-orders validation to allow argument descriptor validation to always be done first; misc. cleanup of error strings

This commit is contained in:
Duncan Brown
2022-04-11 16:03:10 -04:00
committed by Julian Murgia
parent b1b301d917
commit 7050a8a2fb
40 changed files with 202 additions and 81 deletions

View File

@@ -33,6 +33,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not arguments[0] in ["ALL", "NONE", "SKIP"]:
escoria.logger.report_errors(
"accept_input: invalid parameter",
@@ -42,7 +45,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -26,6 +26,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"anim: invalid object",
@@ -34,7 +37,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -27,6 +27,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"anim_block.gd:validate",
@@ -35,7 +38,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -44,6 +44,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"camera_push: invalid object",
@@ -62,7 +65,7 @@ func validate(arguments: Array):
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -29,6 +29,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if escoria.main.current_scene.camera_limits.size() < arguments[0]:
escoria.logger.report_errors(
"camera_set_limits: invalid limits id",
@@ -41,7 +44,7 @@ func validate(arguments: Array):
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -28,6 +28,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[1]):
escoria.logger.report_errors(
"camera_set_target: invalid object",
@@ -37,7 +40,7 @@ func validate(arguments: Array):
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -26,6 +26,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if arguments[0] < 0:
escoria.logger.report_errors(
"camera_set_zoom_height: invalid height",
@@ -35,7 +38,7 @@ func validate(arguments: Array):
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -55,6 +55,9 @@ func run(command_params: Array) -> int:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not arguments[3] in SUPPORTED_TRANSITIONS:
escoria.logger.report_errors(
"camera_shift: invalid transition type",
@@ -65,4 +68,4 @@ func validate(arguments: Array):
)
return false
return .validate(arguments)
return true

View File

@@ -27,6 +27,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array) -> bool:
if not .validate(arguments):
return false
if not ResourceLoader.exists(arguments[0]):
escoria.logger.report_errors(
"change_scene: Invalid scene",
@@ -46,7 +49,8 @@ func validate(arguments: Array) -> bool:
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -28,6 +28,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"custom: invalid object",
@@ -67,7 +70,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -23,6 +23,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.globals_manager.get_global(arguments[0]) is int:
escoria.logger.report_errors(
"dec_global: invalid global",
@@ -31,7 +34,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -30,6 +30,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not arguments[0] in ["main", "pause"]:
escoria.logger.report_errors(
"hide_menu: invalid menu ",
@@ -38,7 +41,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -23,6 +23,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.globals_manager.has(arguments[0]):
escoria.logger.report_errors(
"inc_global: invalid global",
@@ -39,7 +42,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -24,6 +24,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if arguments[0].begins_with("i/"):
escoria.logger.report_errors(
"inventory_add: invalid item name",
@@ -32,7 +35,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -25,6 +25,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[1]):
escoria.logger.report_errors(
"play_snd: invalid sound player",
@@ -37,7 +40,7 @@ func validate(arguments: Array):
["File %s not found" % arguments[0]]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -27,6 +27,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"queue_event.gd:validate",
@@ -63,7 +66,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -24,13 +24,16 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array) -> bool:
if not .validate(arguments):
return false
if not ResourceLoader.exists(arguments[0]):
escoria.logger.report_errors(
"queue_resource: Invalid resource",
["Resource %s was not found" % arguments[0]]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -55,6 +55,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"anim: invalid object",
@@ -72,7 +75,7 @@ func validate(arguments: Array):
"Please set a default dialog type."
]
)
return .validate(arguments)
return true
# Run the command

View File

@@ -28,6 +28,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[1]):
escoria.logger.report_errors(
"sched_event: invalid object",
@@ -48,7 +51,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -24,6 +24,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"set_active: invalid object",
@@ -32,7 +35,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -29,6 +29,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"set_angle: invalid object",
@@ -37,7 +40,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -23,6 +23,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"set_animations: invalid object",
@@ -39,7 +42,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -23,6 +23,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"set_interactive: invalid object",
@@ -31,7 +34,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -22,6 +22,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"set_speed: invalid object",
@@ -30,7 +33,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command
func run(command_params: Array) -> int:

View File

@@ -30,6 +30,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"set_state: invalid object",
@@ -38,7 +41,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -27,6 +27,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not arguments[0] in ["main", "pause"]:
escoria.logger.report_errors(
"show_menu: invalid menu ",
@@ -35,7 +38,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -31,6 +31,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"slide: invalid first object",
@@ -47,7 +50,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Slide the object by generating a tween

View File

@@ -26,6 +26,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if arguments[0].empty() \
or arguments[0] in escoria.object_manager.RESERVED_OBJECTS:
escoria.logger.report_errors(
@@ -51,7 +54,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -24,13 +24,16 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"stop_snd: invalid sound player",
["Sound player %s not registered" % arguments[1]]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -24,6 +24,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"teleport: invalid first object",
@@ -40,7 +43,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -24,6 +24,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"teleport_pos: invalid first object",
@@ -32,7 +35,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -25,6 +25,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.main.scene_transition.has_transition(arguments[0]) \
and not arguments[0].empty():
escoria.logger.report_errors(
@@ -42,7 +45,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -31,6 +31,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"turn_to: invalid object",
@@ -47,7 +50,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -27,6 +27,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"walk: invalid first object",
@@ -43,7 +46,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -27,6 +27,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"walk_block: invalid first object",
@@ -43,7 +46,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -29,6 +29,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"walk_to_pos: invalid first object",
@@ -37,7 +40,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -29,6 +29,9 @@ func configure() -> ESCCommandArgumentDescriptor:
# Validate whether the given arguments match the command descriptor
func validate(arguments: Array):
if not .validate(arguments):
return false
if not escoria.object_manager.has(arguments[0]):
escoria.logger.report_errors(
"walk_to_pos_block: invalid first object",
@@ -37,7 +40,7 @@ func validate(arguments: Array):
]
)
return false
return .validate(arguments)
return true
# Run the command

View File

@@ -23,12 +23,12 @@ func _init() -> void:
# Return the descriptor of the arguments of this command
func configure() -> ESCCommandArgumentDescriptor:
escoria.logger.error("Command %s did not override configure." % get_class())
return ESCCommandArgumentDescriptor.new(0)
return ESCCommandArgumentDescriptor.new()
# Validate whether the given arguments match the command descriptor
func validate(user_arguments: Array, default_arguments: Array) -> bool:
return self.configure().validate(get_class(), user_arguments, default_arguments)
func validate(arguments: Array) -> bool:
return self.configure().validate(get_command_name(), arguments)
# Run the command

View File

@@ -120,10 +120,8 @@ func run() -> int:
var prepared_arguments = argument_descriptor.prepare_arguments(
self.parameters
)
var missing_arguments = argument_descriptor.get_missing_args_from_prepared(
prepared_arguments
)
if command_object.validate(self.parameters, missing_arguments):
if command_object.validate(prepared_arguments):
escoria.logger.debug("Running command %s with parameters %s" % [
self.name,
prepared_arguments

View File

@@ -50,6 +50,14 @@ func prepare_arguments(arguments: Array) -> Array:
var complete_arguments = defaults
for index in range(arguments.size()):
# If we have too many arguments passed in, complete_arguments won't
# be able to match 1:1. This condition will be validated later but so
# to avoid duplicating validation code, just grow complete_arguments
# since the arguments won't be used anyway.
if index >= complete_arguments.size():
complete_arguments.append(arguments[index])
continue
complete_arguments[index] = escoria.utils.get_typed_value(
arguments[index],
types[index]
@@ -67,52 +75,33 @@ func prepare_arguments(arguments: Array) -> Array:
return complete_arguments
# Splits out any missing (default) arguments (in order) for a given set of
# prepared arguments
func get_missing_args_from_prepared(user_arguments:Array, \
prepared_arguments: Array) -> Array:
for index in range(user_arguments.size()):
# Arrays are passed in by value, so this is safe
prepared_arguments.pop_front()
return prepared_arguments
# Validate whether the given arguments match the command descriptor
func validate(command: String, user_arguments: Array, default_arguments: Array) -> bool:
if user_arguments.size() < self.min_args:
func validate(command: String, arguments: Array) -> bool:
var required_args_count: int = _count_leading_non_null_values(arguments, min_args)
if required_args_count < min_args:
var verb = "was" if required_args_count == 1 else "were"
escoria.logger.report_errors(
"ESCCommandArgumentDescriptor:validate()",
[
"Invalid command arguments for command %s" % command,
"Arguments didn't match minimum size {num}: {args}".format({"num":self.min_args,"args":user_arguments})
"Invalid arguments for command %s" % command,
"Arguments didn't match minimum size {num}: Only {args} {verb} found" \
.format({"num":self.min_args,"args":required_args_count,"verb":verb})
]
)
if user_arguments.size() > self.max_args:
if arguments.size() > self.max_args:
escoria.logger.report_errors(
"ESCCommandArgumentDescriptor:validate()",
[
"Invalid command arguments for command %s" % command,
"Maximum number of arguments ({num}) exceeded: {args}".format({"num":self.max_args,"args":user_arguments})
"Invalid arguments for command %s" % command,
"Maximum number of arguments ({num}) exceeded: {args}".format({"num":self.max_args,"args":arguments})
]
)
var all_arguments: Array = user_arguments
all_arguments.append_array(default_arguments)
if all_arguments.size() > self.max_args:
escoria.logger.report_errors(
"ESCCommandArgumentDescriptor:validate()",
[
"Invalid command arguments for command %s" % command,
"Maximum number of arguments ({num}) exceeded: {args}".format({"num":self.max_args,"args":all_arguments})
]
)
for index in range(all_arguments.size()):
if all_arguments[index] == null:
for index in range(arguments.size()):
if arguments[index] == null:
# No type checking for null values
continue
var correct = false
@@ -123,7 +112,7 @@ func validate(command: String, user_arguments: Array, default_arguments: Array)
self.types[types_index] = [self.types[index]]
for type in self.types[types_index]:
if not correct:
correct = self._is_type(all_arguments[index], type)
correct = self._is_type(arguments[index], type)
if not correct:
var allowed_types = "[ "
@@ -136,8 +125,8 @@ func validate(command: String, user_arguments: Array, default_arguments: Array)
[
"Argument %d (\"%s\") is of type %s. Expected %s" % [
index,
all_arguments[index],
GODOT_TYPE_LIST[typeof(all_arguments[index])],
arguments[index],
GODOT_TYPE_LIST[typeof(arguments[index])],
allowed_types
]
]
@@ -151,7 +140,29 @@ func validate(command: String, user_arguments: Array, default_arguments: Array)
#
# - argument: Argument to test
# - type: Type to check
# *Returns* Wether the argument is of the given type
# *Returns* Whether the argument is of the given type
func _is_type(argument, type: int) -> bool:
return typeof(argument) == type
# Counts the number of non-null values that exist at the beginning of the array up
# to a specified index.
#
# #### Parameters
#
# - array_to_check: Array to check for leading non-null values
# - max_index: Maximum (inclusive) index to check in array_to_check
#
# *Returns* the total number of entries at the start of
# array_to_check that are not null
func _count_leading_non_null_values(array_to_check: Array, max_index: int) -> int:
if array_to_check == null or max_index < 0:
return 0
var leading_non_nulls_count: int = 0
for i in range(max_index):
if array_to_check[i] != null:
leading_non_nulls_count += 1
return leading_non_nulls_count