From 7050a8a2fb9b61a4ec5831741e3b57f8dd24094e Mon Sep 17 00:00:00 2001 From: Duncan Brown Date: Mon, 11 Apr 2022 16:03:10 -0400 Subject: [PATCH] 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 --- .../core-scripts/esc/commands/accept_input.gd | 5 +- .../game/core-scripts/esc/commands/anim.gd | 5 +- .../core-scripts/esc/commands/anim_block.gd | 5 +- .../core-scripts/esc/commands/camera_push.gd | 5 +- .../esc/commands/camera_set_limits.gd | 5 +- .../esc/commands/camera_set_target.gd | 5 +- .../esc/commands/camera_set_zoom_height.gd | 5 +- .../core-scripts/esc/commands/camera_shift.gd | 5 +- .../core-scripts/esc/commands/change_scene.gd | 6 +- .../game/core-scripts/esc/commands/custom.gd | 5 +- .../core-scripts/esc/commands/dec_global.gd | 5 +- .../core-scripts/esc/commands/hide_menu.gd | 5 +- .../core-scripts/esc/commands/inc_global.gd | 5 +- .../esc/commands/inventory_add.gd | 5 +- .../core-scripts/esc/commands/play_snd.gd | 5 +- .../core-scripts/esc/commands/queue_event.gd | 5 +- .../esc/commands/queue_resource.gd | 5 +- .../game/core-scripts/esc/commands/say.gd | 5 +- .../core-scripts/esc/commands/sched_event.gd | 5 +- .../core-scripts/esc/commands/set_active.gd | 5 +- .../core-scripts/esc/commands/set_angle.gd | 5 +- .../esc/commands/set_animations.gd | 5 +- .../esc/commands/set_interactive.gd | 5 +- .../core-scripts/esc/commands/set_speed.gd | 5 +- .../core-scripts/esc/commands/set_state.gd | 5 +- .../core-scripts/esc/commands/show_menu.gd | 5 +- .../game/core-scripts/esc/commands/slide.gd | 5 +- .../game/core-scripts/esc/commands/spawn.gd | 5 +- .../core-scripts/esc/commands/stop_snd.gd | 5 +- .../core-scripts/esc/commands/teleport.gd | 5 +- .../core-scripts/esc/commands/teleport_pos.gd | 5 +- .../core-scripts/esc/commands/transition.gd | 5 +- .../game/core-scripts/esc/commands/turn_to.gd | 5 +- .../game/core-scripts/esc/commands/walk.gd | 5 +- .../core-scripts/esc/commands/walk_block.gd | 5 +- .../core-scripts/esc/commands/walk_to_pos.gd | 5 +- .../esc/commands/walk_to_pos_block.gd | 5 +- .../esc/types/esc_base_command.gd | 6 +- .../core-scripts/esc/types/esc_command.gd | 6 +- .../types/esc_command_argument_descriptor.gd | 85 +++++++++++-------- 40 files changed, 202 insertions(+), 81 deletions(-) diff --git a/addons/escoria-core/game/core-scripts/esc/commands/accept_input.gd b/addons/escoria-core/game/core-scripts/esc/commands/accept_input.gd index 2b056131..1a4ce389 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/accept_input.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/accept_input.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/anim.gd b/addons/escoria-core/game/core-scripts/esc/commands/anim.gd index 376b506f..8c0f7468 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/anim.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/anim.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/anim_block.gd b/addons/escoria-core/game/core-scripts/esc/commands/anim_block.gd index 303cb9c8..5f9c6c96 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/anim_block.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/anim_block.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/camera_push.gd b/addons/escoria-core/game/core-scripts/esc/commands/camera_push.gd index 3e402575..67eeff4c 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/camera_push.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/camera_push.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/camera_set_limits.gd b/addons/escoria-core/game/core-scripts/esc/commands/camera_set_limits.gd index e211cd0f..92106128 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/camera_set_limits.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/camera_set_limits.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/camera_set_target.gd b/addons/escoria-core/game/core-scripts/esc/commands/camera_set_target.gd index 63ba9591..88ad86bb 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/camera_set_target.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/camera_set_target.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/camera_set_zoom_height.gd b/addons/escoria-core/game/core-scripts/esc/commands/camera_set_zoom_height.gd index 1cadea17..3d5d1347 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/camera_set_zoom_height.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/camera_set_zoom_height.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/camera_shift.gd b/addons/escoria-core/game/core-scripts/esc/commands/camera_shift.gd index db979989..827ba97a 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/camera_shift.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/camera_shift.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/change_scene.gd b/addons/escoria-core/game/core-scripts/esc/commands/change_scene.gd index 4ccc47e6..499e8f57 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/change_scene.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/change_scene.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/custom.gd b/addons/escoria-core/game/core-scripts/esc/commands/custom.gd index 218725ba..c8af83f9 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/custom.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/custom.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/dec_global.gd b/addons/escoria-core/game/core-scripts/esc/commands/dec_global.gd index acd0c42a..6b560215 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/dec_global.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/dec_global.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/hide_menu.gd b/addons/escoria-core/game/core-scripts/esc/commands/hide_menu.gd index f42f18aa..3b821d76 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/hide_menu.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/hide_menu.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/inc_global.gd b/addons/escoria-core/game/core-scripts/esc/commands/inc_global.gd index 9c3971e0..da046271 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/inc_global.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/inc_global.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/inventory_add.gd b/addons/escoria-core/game/core-scripts/esc/commands/inventory_add.gd index 125fffa3..cf9a5961 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/inventory_add.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/inventory_add.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/play_snd.gd b/addons/escoria-core/game/core-scripts/esc/commands/play_snd.gd index 66a58052..b4ca57eb 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/play_snd.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/play_snd.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/queue_event.gd b/addons/escoria-core/game/core-scripts/esc/commands/queue_event.gd index 72a43847..5dd37b7a 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/queue_event.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/queue_event.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/queue_resource.gd b/addons/escoria-core/game/core-scripts/esc/commands/queue_resource.gd index e2f4b350..00200609 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/queue_resource.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/queue_resource.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/say.gd b/addons/escoria-core/game/core-scripts/esc/commands/say.gd index 64237bba..1f55aa93 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/say.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/say.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/sched_event.gd b/addons/escoria-core/game/core-scripts/esc/commands/sched_event.gd index d3eee617..0c1ceeae 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/sched_event.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/sched_event.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/set_active.gd b/addons/escoria-core/game/core-scripts/esc/commands/set_active.gd index 7c678a9f..cf699884 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/set_active.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/set_active.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/set_angle.gd b/addons/escoria-core/game/core-scripts/esc/commands/set_angle.gd index 2a9cd12a..5eb03136 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/set_angle.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/set_angle.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/set_animations.gd b/addons/escoria-core/game/core-scripts/esc/commands/set_animations.gd index c93b3d97..111c2e66 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/set_animations.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/set_animations.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/set_interactive.gd b/addons/escoria-core/game/core-scripts/esc/commands/set_interactive.gd index 79322ec3..596f45fd 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/set_interactive.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/set_interactive.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/set_speed.gd b/addons/escoria-core/game/core-scripts/esc/commands/set_speed.gd index b362474c..61a4ccde 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/set_speed.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/set_speed.gd @@ -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: diff --git a/addons/escoria-core/game/core-scripts/esc/commands/set_state.gd b/addons/escoria-core/game/core-scripts/esc/commands/set_state.gd index fd670375..db8f35bb 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/set_state.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/set_state.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/show_menu.gd b/addons/escoria-core/game/core-scripts/esc/commands/show_menu.gd index 1f329b7f..0d772a8d 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/show_menu.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/show_menu.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/slide.gd b/addons/escoria-core/game/core-scripts/esc/commands/slide.gd index d9aff098..0bb546eb 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/slide.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/slide.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/spawn.gd b/addons/escoria-core/game/core-scripts/esc/commands/spawn.gd index 59a0f9d4..054a2039 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/spawn.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/spawn.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/stop_snd.gd b/addons/escoria-core/game/core-scripts/esc/commands/stop_snd.gd index 4187877b..1d417e4d 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/stop_snd.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/stop_snd.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/teleport.gd b/addons/escoria-core/game/core-scripts/esc/commands/teleport.gd index cc2000a0..a6da5da1 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/teleport.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/teleport.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/teleport_pos.gd b/addons/escoria-core/game/core-scripts/esc/commands/teleport_pos.gd index 1b987613..253d625f 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/teleport_pos.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/teleport_pos.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/transition.gd b/addons/escoria-core/game/core-scripts/esc/commands/transition.gd index 19b89fb4..358c9535 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/transition.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/transition.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/turn_to.gd b/addons/escoria-core/game/core-scripts/esc/commands/turn_to.gd index 6a178d46..1aa2789f 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/turn_to.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/turn_to.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/walk.gd b/addons/escoria-core/game/core-scripts/esc/commands/walk.gd index 263d47f3..2318ad15 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/walk.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/walk.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/walk_block.gd b/addons/escoria-core/game/core-scripts/esc/commands/walk_block.gd index 5efc9623..ba34974e 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/walk_block.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/walk_block.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/walk_to_pos.gd b/addons/escoria-core/game/core-scripts/esc/commands/walk_to_pos.gd index fa596d39..639390a5 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/walk_to_pos.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/walk_to_pos.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/commands/walk_to_pos_block.gd b/addons/escoria-core/game/core-scripts/esc/commands/walk_to_pos_block.gd index 85eca24c..3861d873 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/walk_to_pos_block.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/walk_to_pos_block.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/types/esc_base_command.gd b/addons/escoria-core/game/core-scripts/esc/types/esc_base_command.gd index e43b6136..52739ebc 100644 --- a/addons/escoria-core/game/core-scripts/esc/types/esc_base_command.gd +++ b/addons/escoria-core/game/core-scripts/esc/types/esc_base_command.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/types/esc_command.gd b/addons/escoria-core/game/core-scripts/esc/types/esc_command.gd index c76fca99..fb5d2ccc 100644 --- a/addons/escoria-core/game/core-scripts/esc/types/esc_command.gd +++ b/addons/escoria-core/game/core-scripts/esc/types/esc_command.gd @@ -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 diff --git a/addons/escoria-core/game/core-scripts/esc/types/esc_command_argument_descriptor.gd b/addons/escoria-core/game/core-scripts/esc/types/esc_command_argument_descriptor.gd index 4f6a7d2f..1d179886 100644 --- a/addons/escoria-core/game/core-scripts/esc/types/esc_command_argument_descriptor.gd +++ b/addons/escoria-core/game/core-scripts/esc/types/esc_command_argument_descriptor.gd @@ -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