Skip to content

Commit

Permalink
demoplayer: ignore suspend/resume requests when not initialized
Browse files Browse the repository at this point in the history
This fixes a nasty interaction between the intro sequence and the
demoplayer introduced in 3ff6204. The cutscene tries to suspend the
demoplayer for its duration and then resume it later. When the
suspension counter hits 0, the demoplayer event handlers are installed
globally. And then AFTER that happens, the demoplayer is actually
initialized, resetting the suspension counter and installing the event
handlers AGAIN. When the demoplayer is then suspended (e.g. by starting
the actual demo playback), only one set of those handlers is removed,
which has the effect of the demoplayer remaining active even in-game.
Eventually, it tries to play a demo while a game is already running,
at which point all hell breaks loose.

Fix this by making suspend and resume no-ops until the demoplayer has
been properly initialized.

Fixes #371
  • Loading branch information
Akaricchi committed Sep 10, 2023
1 parent 94213b4 commit a08d68a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/replay/demoplayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct {
char **demo_files;
size_t num_demo_files;
int suspend_level;
bool initialized;
} dplr;

static bool demo_path_filter(const char *path) {
Expand All @@ -49,6 +50,10 @@ static inline int get_inter_wait_time(void) {
}

void demoplayer_init(void) {
if(dplr.initialized) {
return;
}

dplr.demo_files = vfs_dir_list_sorted(
DEMOPLAYER_DIR_PATH, &dplr.num_demo_files, vfs_dir_list_order_ascending, demo_path_filter
);
Expand All @@ -59,17 +64,24 @@ void demoplayer_init(void) {

dplr.next_demo_countdown = get_initial_wait_time();
dplr.suspend_level = 1;
dplr.initialized = true;

demoplayer_resume();
}

void demoplayer_shutdown(void) {
if(!dplr.initialized) {
return;
}

if(dplr.suspend_level == 0) {
demoplayer_suspend();
}

vfs_dir_list_free(dplr.demo_files, dplr.num_demo_files);
dplr.demo_files = NULL;
dplr.num_demo_files = 0;
dplr.initialized = false;
}

typedef struct DemoPlayerContext {
Expand Down Expand Up @@ -158,6 +170,11 @@ static bool demoplayer_activity_event(SDL_Event *evt, void *arg) {
}

void demoplayer_suspend(void) {
if(!dplr.initialized) {
log_debug("Not initialized");
return;
}

dplr.suspend_level++;
assert(dplr.suspend_level > 0);
log_debug("Suspend level is now %i", dplr.suspend_level);
Expand All @@ -173,6 +190,11 @@ void demoplayer_suspend(void) {
}

void demoplayer_resume(void) {
if(!dplr.initialized) {
log_debug("Not initialized");
return;
}

dplr.suspend_level--;
assert(dplr.suspend_level >= 0);
log_debug("Suspend level is now %i", dplr.suspend_level);
Expand Down

0 comments on commit a08d68a

Please sign in to comment.