Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix -levelstat in release build #2103

Merged
merged 3 commits into from
Dec 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions src/g_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,7 @@ static void G_PlayerFinishLevel(int player)

// [crispy] format time for level statistics
#define TIMESTRSIZE 16
static void G_FormatLevelStatTime(char *str, int tics, boolean total)
static void FormatLevelStatTime(char *str, int tics, boolean total)
{
int exitHours, exitMinutes;
float exitTime, exitSeconds;
Expand Down Expand Up @@ -1611,31 +1611,39 @@ static void G_FormatLevelStatTime(char *str, int tics, boolean total)
// [crispy] Write level statistics upon exit
static void G_WriteLevelStat(void)
{
static FILE *fstream = NULL;
int playerKills = 0, playerItems = 0, playerSecrets = 0;

int i, playerKills = 0, playerItems = 0, playerSecrets = 0;
char levelString[9] = {0};
char levelTimeString[TIMESTRSIZE] = {0};
char totalTimeString[TIMESTRSIZE] = {0};

char levelString[8];
char levelTimeString[TIMESTRSIZE];
char totalTimeString[TIMESTRSIZE];
static boolean firsttime = true;

if (fstream == NULL)
FILE *fstream = NULL;

if (firsttime)
{
firsttime = false;
fstream = M_fopen("levelstat.txt", "w");
}
else
{
fstream = M_fopen("levelstat.txt", "a");
}

if (fstream == NULL)
{
I_Printf(VB_ERROR, "G_WriteLevelStat: Unable to open levelstat.txt for writing!");
return;
}
if (fstream == NULL)
{
I_Printf(VB_ERROR,
"G_WriteLevelStat: Unable to open levelstat.txt for writing!");
return;
}

strcpy(levelString, MapName(gameepisode, gamemap));

G_FormatLevelStatTime(levelTimeString, leveltime, false);
G_FormatLevelStatTime(totalTimeString, totalleveltimes + leveltime, true);
FormatLevelStatTime(levelTimeString, leveltime, false);
FormatLevelStatTime(totalTimeString, totalleveltimes + leveltime, true);

for (i = 0; i < MAXPLAYERS; i++)
for (int i = 0; i < MAXPLAYERS; i++)
{
if (playeringame[i])
{
Expand All @@ -1649,6 +1657,8 @@ static void G_WriteLevelStat(void)
levelString, (secretexit ? "s" : ""),
levelTimeString, totalTimeString, playerKills, totalkills,
playerItems, totalitems, playerSecrets, totalsecret);

fclose(fstream);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, we didn't even close the levelstat.txt file handler until now? I wonder why this bug wasn't spotted earlier.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the Windows standard C library (CRT) has been updated and changed its behavior. I can't replicate it on my local Win10 build, but our release build is latest Windows Server with all updates installed and has static CRT, so release artifacts have this bug.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appending after each level is better anyway, thank you!

}

//
Expand Down