Skip to content

Commit

Permalink
Sanitize level description in window title and HUD
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-drexler committed Jun 23, 2024
1 parent 4395c4a commit f211636
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 26 deletions.
64 changes: 44 additions & 20 deletions Quake/gl_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -2468,6 +2468,48 @@ static void Mod_LoadBrushModel (qmodel_t *mod, void *buffer)
}
}

/*
=================
Mod_SanitizeMapDescription
Cleans up map descriptions:
- removes colors
- replaces newlines with spaces
- replaces consecutive spaces with single one
- removes leading/trailing spaces
Returns dst string length (excluding NUL terminator)
=================
*/
size_t Mod_SanitizeMapDescription (char *dst, size_t dstsize, const char *src)
{
int srcpos, dstpos;

if (!dstsize)
return 0;

for (srcpos = dstpos = 0; src[srcpos] && (size_t)dstpos + 1 < dstsize; srcpos++)
{
char c = src[srcpos] & 0x7f; // remove color
if (c == '\n' || c == '\r') // replace newlines with spaces
c = ' ';
else if (c == '\\' && src[srcpos + 1] == 'n') // replace '\\' followed by 'n' with space
{
c = ' ';
srcpos++;
}
// remove leading spaces, replace consecutive spaces with single one
if (c != ' ' || (dstpos > 0 && dst[dstpos - 1] != c))
dst[dstpos++] = c;
}
// remove trailing space, if any
if (dstpos > 0 && dst[dstpos - 1] == ' ')
--dstpos;

dst[dstpos] = '\0';
return dstpos;
}

/*
=================
Mod_LoadMapDescription
Expand All @@ -2485,7 +2527,7 @@ qboolean Mod_LoadMapDescription (char *desc, size_t maxchars, const char *map)
FILE *f;
lump_t *entlump;
dheader_t header;
int i, j, k, filesize;
int i, filesize;
qboolean ret = false;

if (!maxchars)
Expand Down Expand Up @@ -2578,25 +2620,7 @@ qboolean Mod_LoadMapDescription (char *desc, size_t maxchars, const char *map)

if (is_message)
{
// copy map title and clean it up a bit
for (j = k = 0; com_token[j] && (size_t)k + 1 < maxchars; j++)
{
char c = com_token[j] & 0x7f;
if (c == '\n' || c == '\r') // replace newlines with spaces
c = ' ';
else if (c == '\\' && com_token[j + 1] == 'n') // replace '\\' followed by 'n' with space
{
c = ' ';
j++;
}
// remove leading spaces, replace consecutive spaces with single one
if (c != ' ' || (k > 0 && desc[k - 1] != c))
desc[k++] = c;
}
// remove trailing space, if any
if (k > 0 && desc[k - 1] == ' ')
--k;
desc[k++] = '\0';
Mod_SanitizeMapDescription (desc, maxchars, com_token);
if (ret)
return true;
}
Expand Down
1 change: 1 addition & 0 deletions Quake/gl_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ byte *Mod_LeafPVS (mleaf_t *leaf, qmodel_t *model);
byte *Mod_NoVisPVS (qmodel_t *model);

void Mod_SetExtraFlags (qmodel_t *mod);
size_t Mod_SanitizeMapDescription (char *dst, size_t dstsize, const char *src);
qboolean Mod_LoadMapDescription (char *desc, size_t maxchars, const char *map);

#endif /* GL_MODEL_H */
11 changes: 7 additions & 4 deletions Quake/host.c
Original file line number Diff line number Diff line change
Expand Up @@ -1015,15 +1015,18 @@ static void UpdateWindowTitle (void)

if (current.map[0])
{
char levelname[4 * sizeof (cl.levelname)];
char cleanname[sizeof (cl.levelname)];
char utf8name[4 * sizeof (cl.levelname)];
char title[1024];

UTF8_FromQuake (levelname, sizeof (levelname), cl.levelname);
Mod_SanitizeMapDescription (cleanname, sizeof (cleanname), cl.levelname);

UTF8_FromQuake (utf8name, sizeof (utf8name), cleanname);
q_snprintf (title, sizeof (title),
levelname[0] ?
utf8name[0] ?
"%s (%s) | skill %d | %d/%d kills | %d/%d secrets - " WINDOW_TITLE_STRING :
"%s%s | skill %d | %d/%d kills | %d/%d secrets - " WINDOW_TITLE_STRING,
levelname, current.map,
utf8name, current.map,
current.stats.skill,
current.stats.monsters, current.stats.total_monsters,
current.stats.secrets, current.stats.total_secrets
Expand Down
11 changes: 9 additions & 2 deletions Quake/sbar.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,11 @@ void Sbar_SoloScoreboard (void)
Sbar_DrawString ((left + right) / 2 - strlen (str) * 4, 12, str);

if (cl.levelname[0])
q_snprintf (str, sizeof(str), "%s (%s)", cl.levelname, cl.mapname);
{
char cleanname[sizeof (cl.levelname)];
Mod_SanitizeMapDescription (cleanname, sizeof (cleanname), cl.levelname);
q_snprintf (str, sizeof (str), "%s (%s)", cleanname, cl.mapname);
}
else
q_strlcpy (str, cl.mapname, sizeof(str));
len = strlen (str);
Expand All @@ -504,7 +508,10 @@ void Sbar_SoloScoreboard (void)
sprintf (str,"%i:%i%i", minutes, tens, units);
Sbar_DrawString ((left + right)/2 - strlen(str)*4, 12, str);

len = q_strlcpy (str, cl.levelname[0] ? cl.levelname : cl.mapname, sizeof(str));
if (cl.levelname[0])
len = Mod_SanitizeMapDescription (str, sizeof (str), cl.levelname);
else
len = q_strlcpy (str, cl.mapname, sizeof(str));
if (len > 40)
Sbar_DrawScrollString (0, 4, 320, str);
else
Expand Down

0 comments on commit f211636

Please sign in to comment.