Skip to content

Commit

Permalink
Fix Mines of Madness string compare bug
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceLR committed Apr 28, 2020
1 parent 2b0fef9 commit 1012da0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.txt
Expand Up @@ -43,6 +43,8 @@ USERS
counters or strings lists beyond 1 billion. Checks have been
added to prevent them from expanding beyond 2^31 (please don't
actually use this many counters/strings).
+ Fixed string comparison for pre-2.81 games that relied on
null-terminated string compares. This fixes Mines of Madness.
+ Fixed hlp2html bug where certain chars wouldn't be escaped and
decreased the size of the output HTML files somewhat.
+ Fixed libxmp playback for S3Ms containing ADPCM4 samples. This
Expand Down
34 changes: 22 additions & 12 deletions src/run_robot.c
Expand Up @@ -1726,8 +1726,6 @@ void run_robot(context *ctx, int id, int x, int y)
{
struct string dest;
struct string src;
int allow_wildcards = 0;
int exact_case = 0;

// NOTE: versions prior to 2.92 did tr_msg here instead of above.

Expand Down Expand Up @@ -1756,19 +1754,31 @@ void run_robot(context *ctx, int id, int x, int y)
src.length = tmp;
}

// String equality extensions (2.91+)
if(mzx_world->version >= V291)
// Non-terminated string compares (2.81+)
if(mzx_world->version >= V281)
{
if(comparison == EXACTLY_EQUAL || comparison == WILD_EXACTLY_EQUAL)
exact_case = 1;
boolean exact_case = false;
boolean wildcards = false;

if(comparison == WILD_EQUAL || comparison == WILD_EXACTLY_EQUAL)
allow_wildcards = 1;
}
// String equality extensions (2.91+)
if(mzx_world->version >= V291)
{
if(comparison == EXACTLY_EQUAL || comparison == WILD_EXACTLY_EQUAL)
exact_case = true;

src_value = 0;
dest_value = compare_strings(&dest, &src,
exact_case, allow_wildcards);
if(comparison == WILD_EQUAL || comparison == WILD_EXACTLY_EQUAL)
wildcards = true;
}

src_value = 0;
dest_value = compare_strings(&dest, &src, exact_case, wildcards);
}
// Null-terminated string compares (2.80X and prior).
else
{
src_value = 0;
dest_value = compare_strings_null_terminated(&dest, &src);
}
}
else

Expand Down
26 changes: 25 additions & 1 deletion src/str.c
Expand Up @@ -1768,7 +1768,7 @@ static int compare_wildcard(const char *str, size_t str_len,
int compare_strings(struct string *A, struct string *B, boolean exact_case,
boolean allow_wildcards)
{
size_t cmp_length = (A->length < B->length) ? A->length : B->length;
size_t cmp_length = MIN(A->length, B->length);
int res = 0;

if(!allow_wildcards)
Expand Down Expand Up @@ -1802,6 +1802,30 @@ int compare_strings(struct string *A, struct string *B, boolean exact_case,
return compare_wildcard(A->value, A->length, B->value, B->length, exact_case);
}

/**
* String comparison prior to 2.81 used strcasecmp, which worked because string
* values were guaranteed to be null terminated. Some games relied on this
* behavior by inserting nulls into strings (Mines of Madness). String values
* are not null terminated anymore, so approximate this with strncasecmp.
*/
int compare_strings_null_terminated(struct string *A, struct string *B)
{
size_t cmp_length = MIN(A->length, B->length);
int res;

res = strncasecmp(A->value, B->value, cmp_length);
if(res)
return res;

if(A->length > B->length)
return A->value[cmp_length];

if(A->length < B->length)
return -B->value[cmp_length];

return 0;
}

// Create a new string from loading a save file. This skips find_string.
struct string *load_new_string(struct string_list *string_list, int index,
const char *name, int name_length, int str_length)
Expand Down
1 change: 1 addition & 0 deletions src/str.h
Expand Up @@ -61,6 +61,7 @@ void dec_string_int(struct world *mzx_world, const char *name, int value,
int id);
int compare_strings(struct string *A, struct string *B, boolean exact_case,
boolean allow_wildcards);
int compare_strings_null_terminated(struct string *A, struct string *B);

struct string *load_new_string(struct string_list *string_list, int index,
const char *name, int name_length, int str_length);
Expand Down
Binary file added testworlds/2.70/004 Terminated Str Cmp.mzx
Binary file not shown.

0 comments on commit 1012da0

Please sign in to comment.