From da7de8f1d879cd003250d5e523652c74a8150843 Mon Sep 17 00:00:00 2001 From: Mitch Richters Date: Tue, 16 Nov 2021 21:46:43 +1100 Subject: [PATCH] - Harden the `skill` CCMD to test for valid skill range for loaded game, not the maximum size of the `gSkillNames[]` array which may not be completely filled. Also print valid skill names in addition to numbers to help the user. --- source/core/cheats.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/source/core/cheats.cpp b/source/core/cheats.cpp index 6a34f8b55..13a913fdd 100644 --- a/source/core/cheats.cpp +++ b/source/core/cheats.cpp @@ -530,20 +530,35 @@ CCMD(skill) } else if (argsCount == 2) { + // Get maximum valid skills for loaded game. + auto maxvalidskills = 0; + for (auto i = 0; i < MAXSKILLS; i++) + { + if (gSkillNames[i].IsNotEmpty()) + { + maxvalidskills++; + } + } + + // Test and set skill if its legal. auto newSkill = atoi(argv[1]); - if (newSkill >= 0 and newSkill < MAXSKILLS) + if (newSkill >= 0 && newSkill < maxvalidskills) { g_nextskill = newSkill; Printf("Skill will be changed for next game.\n"); } else { - Printf("Please specify a skill level between 0 and %d\n", MAXSKILLS - 1); + Printf("Please specify a skill level between 0 and %d\n", maxvalidskills - 1); + for (auto i = 0; i < maxvalidskills; i++) + { + Printf("%d = '%s'\n", i, GStrings.localize(gSkillNames[i])); + } } } else if (argsCount > 2) { - Printf(PRINT_BOLD, "skill : returns the current skill level, and optionally sets the skill level for the next game.\n"); + Printf(PRINT_BOLD, "skill : returns the current skill level, and optionally sets the skill level for the next game if provided and is valid.\n"); } } else