Skip to content

Commit af361dd

Browse files
authored
Add deepest level guard on --print-seed-catalog (#629)
1 parent 8567eab commit af361dd

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

src/brogue/Rogue.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ typedef long long fixpt;
115115
#define SCREENSHOT_SUFFIX ".png"
116116

117117
#define BROGUE_FILENAME_MAX (min(1024*4, FILENAME_MAX))
118+
#define ERROR_MESSAGE_LENGTH 100
118119

119120
// Date format used when listing recordings and high scores
120121
#define DATE_FORMAT "%Y-%m-%d" // see strftime() documentation
@@ -2822,6 +2823,7 @@ extern "C" {
28222823
boolean chooseFile(char *path, char *prompt, char *defaultName, char *suffix);
28232824
boolean openFile(const char *path);
28242825
void initializeGameVariant(void);
2826+
int deepestLevelForGameVariant(void);
28252827
void initializeRogue(uint64_t seed);
28262828
void gameOver(char *killedBy, boolean useCustomPhrasing);
28272829
void victory(boolean superVictory);
@@ -3428,7 +3430,7 @@ extern "C" {
34283430
int quitImmediately(void);
34293431
void dialogAlert(char *message);
34303432
void mainBrogueJunction(void);
3431-
void printSeedCatalog(uint64_t startingSeed, uint64_t numberOfSeedsToScan, unsigned int scanThroughDepth, boolean isCsvFormat);
3433+
int printSeedCatalog(uint64_t startingSeed, uint64_t numberOfSeedsToScan, unsigned int scanThroughDepth, boolean isCsvFormat, char *errorMessage);
34323434

34333435
void initializeButton(brogueButton *button);
34343436
void drawButtonsInState(buttonState *state);

src/brogue/SeedCatalog.c

+13-3
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,24 @@ static void printSeedCatalogAltars(boolean isCsvFormat) {
251251
}
252252
}
253253

254-
void printSeedCatalog(uint64_t startingSeed, uint64_t numberOfSeedsToScan, unsigned int scanThroughDepth,
255-
boolean isCsvFormat) {
254+
int printSeedCatalog(uint64_t startingSeed, uint64_t numberOfSeedsToScan, unsigned int scanThroughDepth,
255+
boolean isCsvFormat, char *errorMessage) {
256256
uint64_t theSeed;
257257
char message[1000] = "";
258258
rogue.nextGame = NG_NOTHING;
259259

260260
initializeGameVariant();
261261

262+
if (startingSeed == 0) {
263+
strncpy(errorMessage, "Starting seed must be 1+", ERROR_MESSAGE_LENGTH);
264+
return 1;
265+
}
266+
267+
if (scanThroughDepth == 0 || scanThroughDepth > gameConst->deepestLevel) {
268+
snprintf(errorMessage, ERROR_MESSAGE_LENGTH, "Depth must be between 1 and %d", gameConst->deepestLevel);
269+
return 1;
270+
}
271+
262272
sprintf(message, "Brogue seed catalog, seeds %llu to %llu, through depth %u.\n"
263273
"Generated with %s. Dungeons unchanged since %s.\n\n"
264274
"To play one of these seeds, select Play>New Seeded Game from the title screen.\n",
@@ -303,5 +313,5 @@ void printSeedCatalog(uint64_t startingSeed, uint64_t numberOfSeedsToScan, unsig
303313

304314
freeEverything();
305315
}
306-
316+
return 0;
307317
}

src/platform/main.c

+29-12
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ static void printCommandlineHelp() {
5252
return;
5353
}
5454

55-
static void badArgument(const char *arg) {
56-
printf("Bad argument: %s\n\n", arg);
55+
static void cliError(const char *prefix, const char *errorMsg) {
56+
printf("%s%s\n\n", prefix, errorMsg);
5757
printCommandlineHelp();
5858
}
5959

60+
static void badArgument(const char *arg) {
61+
cliError("Bad argument: ", arg);
62+
}
63+
6064
boolean tryParseUint64(char *str, uint64_t *num) {
6165
unsigned long long n;
6266
char buf[100];
@@ -195,20 +199,33 @@ int main(int argc, char *argv[])
195199
}
196200

197201
if (strcmp(argv[i], "--print-seed-catalog") == 0) {
202+
uint64_t startingSeed, numberOfSeeds;
203+
int numberOfLevels;
204+
198205
if (i + 3 < argc) {
199-
uint64_t startingSeed, numberOfSeeds;
200-
// Use converter for the type the next size up, because it returns signed
201-
unsigned int numberOfLevels = atol(argv[i + 3]);
202-
203-
if (tryParseUint64(argv[i+1], &startingSeed) && tryParseUint64(argv[i+2], &numberOfSeeds)
204-
&& startingSeed > 0 && numberOfLevels <= 40) {
205-
printSeedCatalog(startingSeed, numberOfSeeds, numberOfLevels, isCsvFormat);
206-
return 0;
206+
numberOfLevels = atoi(argv[i + 3]);
207+
if (!tryParseUint64(argv[i+1], &startingSeed)) {
208+
cliError("Bad params for seed catalog, starting seed: ", argv[i+1]);
209+
return 1;
210+
}
211+
if (!tryParseUint64(argv[i+2], &numberOfSeeds)) {
212+
cliError("Bad params for seed catalog, number of seeds: ", argv[i+2]);
213+
return 1;
207214
}
208215
} else {
209-
printSeedCatalog(1, 1000, 5, isCsvFormat);
210-
return 0;
216+
startingSeed = 1;
217+
numberOfSeeds = 1000;
218+
numberOfLevels = 5;
219+
}
220+
221+
int errorCode;
222+
char errorMessage[ERROR_MESSAGE_LENGTH];
223+
224+
errorCode = printSeedCatalog(startingSeed, numberOfSeeds, numberOfLevels, isCsvFormat, errorMessage);
225+
if (errorCode) {
226+
cliError("Bad params for seed catalog, ", errorMessage);
211227
}
228+
return errorCode;
212229
}
213230

214231
if (strcmp(argv[i], "-V") == 0 || strcmp(argv[i], "--version") == 0) {

0 commit comments

Comments
 (0)