From: Holger Schemel Date: Thu, 10 Sep 2020 18:51:27 +0000 (+0200) Subject: added command to automatically patch tapes for the "EM random bug" X-Git-Tag: 4.2.0.3~53 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=7c1d6252;p=rocksndiamonds.git added command to automatically patch tapes for the "EM random bug" This adds a command line option that can be used to patch existing tapes (by setting or unsetting property bits in the tape header). The only patch mode currently available is "em_random_bug" that sets the property bit for using the 64-bit random value workaround in the EM game engine (see commit 1006b2db for details). When using "help" for "MODE", all available patch modes will be shown. --- diff --git a/src/init.c b/src/init.c index 2b43180b..ffc856e3 100644 --- a/src/init.c +++ b/src/init.c @@ -4900,6 +4900,7 @@ static void InitGlobal(void) } global.autoplay_leveldir = NULL; + global.patchtapes_leveldir = NULL; global.convert_leveldir = NULL; global.create_images_dir = NULL; @@ -5080,6 +5081,65 @@ static void Execute_Command(char *command) if (global.autoplay_mode == AUTOPLAY_MODE_TEST) program.headless = TRUE; } + else if (strPrefix(command, "patch tapes ")) + { + char *str_ptr = getStringCopy(&command[12]); // read command parameters + + // skip leading whitespace + while (*str_ptr == ' ' || *str_ptr == '\t') + str_ptr++; + + if (*str_ptr == '\0') + Error(ERR_EXIT, "cannot find MODE in command '%s'", command); + + global.patchtapes_mode = str_ptr; // store patch mode + + // advance to next whitespace (or end of string) + while (*str_ptr != ' ' && *str_ptr != '\t' && *str_ptr != '\0') + str_ptr++; + + while (*str_ptr != '\0') // continue parsing string + { + // cut leading whitespace from string, replace it by string terminator + while (*str_ptr == ' ' || *str_ptr == '\t') + *str_ptr++ = '\0'; + + if (*str_ptr == '\0') // end of string reached + break; + + if (global.patchtapes_leveldir == NULL) // read level set string + { + global.patchtapes_leveldir = str_ptr; + global.patchtapes_all = TRUE; // default: patch all tapes + + for (i = 0; i < MAX_TAPES_PER_SET; i++) + global.patchtapes_level[i] = FALSE; + } + else // read level number string + { + int level_nr = atoi(str_ptr); // get level_nr value + + if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET) + global.patchtapes_level[level_nr] = TRUE; + + global.patchtapes_all = FALSE; + } + + // advance string pointer to the next whitespace (or end of string) + while (*str_ptr != ' ' && *str_ptr != '\t' && *str_ptr != '\0') + str_ptr++; + } + + if (global.patchtapes_leveldir == NULL) + { + if (strEqual(global.patchtapes_mode, "help")) + global.patchtapes_leveldir = UNDEFINED_LEVELSET; + else + Error(ERR_EXIT, "cannot find LEVELDIR in command '%s'", command); + } + + program.headless = TRUE; + } else if (strPrefix(command, "convert ")) { char *str_copy = getStringCopy(strchr(command, ' ') + 1); @@ -6167,6 +6227,11 @@ void OpenAll(void) AutoPlayTape(); return; } + else if (global.patchtapes_leveldir) + { + PatchTapes(); + return; + } else if (global.convert_leveldir) { ConvertLevels(); diff --git a/src/main.c b/src/main.c index 824c0b59..a87d2029 100644 --- a/src/main.c +++ b/src/main.c @@ -7637,6 +7637,7 @@ static void print_usage(void) " \"autoplay LEVELDIR [NR ...]\" play level tapes for LEVELDIR\n" " \"autoffwd LEVELDIR [NR ...]\" ffwd level tapes for LEVELDIR\n" " \"autowarp LEVELDIR [NR ...]\" warp level tapes for LEVELDIR\n" + " \"patch tapes MODE LEVELDIR [NR]\" patch level tapes for LEVELDIR\n" " \"convert LEVELDIR [NR]\" convert levels in LEVELDIR\n" " \"create images DIRECTORY\" write BMP images to DIRECTORY\n" " \"create CE image DIRECTORY\" write BMP image to DIRECTORY\n" diff --git a/src/main.h b/src/main.h index c9f7a15a..92e42492 100644 --- a/src/main.h +++ b/src/main.h @@ -3219,6 +3219,11 @@ struct GlobalInfo boolean autoplay_all; boolean autoplay_mode; + char *patchtapes_mode; + char *patchtapes_leveldir; + int patchtapes_level[MAX_TAPES_PER_SET]; + boolean patchtapes_all; + char *convert_leveldir; int convert_level_nr; diff --git a/src/tape.c b/src/tape.c index 3f031f01..1763b296 100644 --- a/src/tape.c +++ b/src/tape.c @@ -1276,6 +1276,166 @@ void AutoPlayTape(void) } +// ---------------------------------------------------------------------------- +// tape patch functions +// ---------------------------------------------------------------------------- + +static boolean PatchTape(struct TapeInfo *tape, char *mode) +{ + Print("[%d.%d.%d.%d]: ", + (tape->engine_version / 1000000) % 100, + (tape->engine_version / 10000 ) % 100, + (tape->engine_version / 100 ) % 100, + (tape->engine_version ) % 100); + + if (strEqual(mode, "info")) + { + Print("property bits == 0x%02x\n", tape->property_bits); + + return FALSE; + } + + byte property_bits = tape->property_bits; + byte property_bitmask = 0; + boolean set_property_bit = TRUE; + + if (strSuffix(mode, ":0") || + strSuffix(mode, ":off") || + strSuffix(mode, ":clear")) + set_property_bit = FALSE; + + if (strEqual(mode, "em_random_bug") || strPrefix(mode, "em_random_bug:")) + { + // this bug was introduced in version 3.3.1.0 and fixed in version 4.0.1.2 + if (tape->engine_version < VERSION_IDENT(3,3,1,0) || + tape->engine_version >= VERSION_IDENT(4,0,1,2)) + { + Print("This tape version cannot be patched against EM random bug!\n"); + + return FALSE; + } + + property_bitmask = TAPE_PROPERTY_EM_RANDOM_BUG; + } + else + { + Print("Unknown patch mode '%s'!\n", mode); + + return FALSE; + } + + if (set_property_bit) + property_bits |= property_bitmask; + else + property_bits &= ~property_bitmask; + + if (property_bits == tape->property_bits) + { + Print("Tape already patched for '%s'!\n", mode); + + return FALSE; + } + + Print("Patching for '%s' ... ", mode); + + tape->property_bits = property_bits; + + return TRUE; +} + +void PatchTapes(void) +{ + static LevelDirTree *patchtapes_leveldir = NULL; + static int num_tapes_found = 0; + static int num_tapes_patched = 0; + char *mode = global.patchtapes_mode; + int i; + + if (strEqual(mode, "help")) + { + PrintLine("=", 79); + Print("Supported patch modes:\n"); + Print("- \"em_random_bug\" - use 64-bit random value bug for EM engine\n"); + PrintLine("-", 79); + Print("Supported modifiers:\n"); + Print("- add \":0\", \":off\" or \":clear\" to patch mode to un-patch tape file\n"); + PrintLine("=", 79); + + CloseAllAndExit(0); + } + + patchtapes_leveldir = getTreeInfoFromIdentifier(leveldir_first, + global.patchtapes_leveldir); + + if (patchtapes_leveldir == NULL) + Error(ERR_EXIT, "no such level identifier: '%s'", + global.patchtapes_leveldir); + + leveldir_current = patchtapes_leveldir; + + if (patchtapes_leveldir->first_level < 0) + patchtapes_leveldir->first_level = 0; + if (patchtapes_leveldir->last_level >= MAX_TAPES_PER_SET) + patchtapes_leveldir->last_level = MAX_TAPES_PER_SET - 1; + + PrintLine("=", 79); + Print("Patching level tapes for patch mode '%s'\n", mode); + PrintLine("-", 79); + Print("Level series identifier: '%s'\n", patchtapes_leveldir->identifier); + Print("Level series name: '%s'\n", patchtapes_leveldir->name); + Print("Level series author: '%s'\n", patchtapes_leveldir->author); + Print("Number of levels: %d\n", patchtapes_leveldir->levels); + PrintLine("=", 79); + Print("\n"); + + int first_level = patchtapes_leveldir->first_level; + int last_level = patchtapes_leveldir->last_level; + + for (i = first_level; i <= last_level; i++) + { + if (!global.patchtapes_all && !global.patchtapes_level[i]) + continue; + + Print("Tape %03d: ", i); + + TapeErase(); + LoadTape(i); + + if (tape.no_valid_file) + { + Print("(not found)\n"); + + continue; + } + + num_tapes_found++; + + if (PatchTape(&tape, mode)) + { + char *filename = getTapeFilename(i); + char *filename_orig = getStringCat2(filename, ".orig"); + + if (!fileExists(filename_orig)) + rename(filename, filename_orig); + + SaveTapeToFilename(filename); + + Print("patched tape saved.\n"); + + num_tapes_patched++; + } + } + + Print("\n"); + PrintLine("=", 79); + Print("Number of tapes found: %d\n", num_tapes_found); + Print("Number of tapes patched: %d\n", num_tapes_patched); + PrintLine("=", 79); + + CloseAllAndExit(0); +} + + // ---------- new tape button stuff ------------------------------------------- static struct diff --git a/src/tape.h b/src/tape.h index d5a19ac8..948227e5 100644 --- a/src/tape.h +++ b/src/tape.h @@ -256,6 +256,7 @@ boolean InsertSolutionTape(void); boolean PlaySolutionTape(void); void AutoPlayTape(void); +void PatchTapes(void); void CreateTapeButtons(void); void FreeTapeButtons(void);