Skip to content

Commit

Permalink
(cheevos) include achievement state in netplay states (#17416)
Browse files Browse the repository at this point in the history
* add achievement data to netplay save state

* honor achievement state from netplay server

* keep processing achievements if menu doesn't pause game

* remove unused variable

* only CRC coremem

* force send savestate on join and hardcore change

* allow hardcore enablement to be synced to clients

* still calculate cheevos_size for non-server

* use appropriate buffer

* optimizations for when achievements are disabled

* support interfacing with older protocols

* formatting

* c89
  • Loading branch information
Jamiras authored Jan 16, 2025
1 parent beceb88 commit 0113226
Show file tree
Hide file tree
Showing 9 changed files with 419 additions and 90 deletions.
80 changes: 39 additions & 41 deletions cheevos/cheevos.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ static rcheevos_locals_t rcheevos_locals =
{{0}},/* memory */
#ifdef HAVE_THREADS
CMD_EVENT_NONE, /* queued_command */
false, /* game_placard_requested */
#endif
"", /* user_agent_prefix */
"", /* user_agent_core */
Expand All @@ -112,8 +111,8 @@ rcheevos_locals_t* get_rcheevos_locals(void)
Supporting functions.
*****************************************************************************/

#define CMD_CHEEVOS_NON_COMMAND -1
static void rcheevos_show_game_placard(void);
#define CMD_CHEEVOS_FINALIZE_LOAD -1
static void rcheevos_finalize_game_load_on_ui_thread(void);

#ifndef CHEEVOS_VERBOSE
void rcheevos_log(const char* fmt, ...)
Expand Down Expand Up @@ -686,7 +685,6 @@ bool rcheevos_unload(void)

#ifdef HAVE_THREADS
rcheevos_locals.queued_command = CMD_EVENT_NONE;
rcheevos_locals.game_placard_requested = false;
#endif

if (rcheevos_locals.memory.count > 0)
Expand Down Expand Up @@ -827,6 +825,11 @@ static void rcheevos_toggle_hardcore_active(rcheevos_locals_t* locals)
command_event(CMD_EVENT_REWIND_INIT, NULL);
}
}

#ifdef HAVE_NETWORKING
/* force sending a savestate to clients so they'll drop out of hardcore too */
netplay_force_send_savestate();
#endif
}

void rcheevos_toggle_hardcore_paused(void)
Expand Down Expand Up @@ -978,16 +981,12 @@ void rcheevos_test(void)
#ifdef HAVE_THREADS
if (rcheevos_locals.queued_command != CMD_EVENT_NONE)
{
if ((int)rcheevos_locals.queued_command != CMD_CHEEVOS_NON_COMMAND)
if ((int)rcheevos_locals.queued_command == CMD_CHEEVOS_FINALIZE_LOAD)
rcheevos_finalize_game_load_on_ui_thread();
else
command_event(rcheevos_locals.queued_command, NULL);

rcheevos_locals.queued_command = CMD_EVENT_NONE;

if (rcheevos_locals.game_placard_requested)
{
rcheevos_locals.game_placard_requested = false;
rcheevos_show_game_placard();
}
}
#endif

Expand Down Expand Up @@ -1382,6 +1381,25 @@ static void rcheevos_finalize_game_load(rc_client_t* client)
}
}

static void rcheevos_finalize_game_load_on_ui_thread(void)
{
rcheevos_show_game_placard();

#if HAVE_REWIND
if (!rcheevos_hardcore_active())
{
const settings_t* settings = config_get_ptr();
/* Re-enable rewind. Additional space will be allocated for the achievement state data */
if (settings->bools.rewind_enable)
command_event(CMD_EVENT_REWIND_REINIT, NULL);
}
#endif

#ifdef HAVE_NETWORKING
netplay_reinit_serialization();
#endif
}

static void rcheevos_client_load_game_callback(int result,
const char* error_message, rc_client_t* client, void* userdata)
{
Expand Down Expand Up @@ -1446,17 +1464,6 @@ static void rcheevos_client_load_game_callback(int result,
rc_client_set_read_memory_function(client, rcheevos_client_read_memory);
}

#ifdef HAVE_THREADS
if (!video_driver_is_threaded() && !task_is_on_main_thread())
{
/* have to "schedule" this. game image should not be loaded on background thread */
rcheevos_locals.queued_command = CMD_CHEEVOS_NON_COMMAND;
rcheevos_locals.game_placard_requested = true;
}
else
#endif
rcheevos_show_game_placard();

rcheevos_finalize_game_load(client);

if (rcheevos_hardcore_active())
Expand All @@ -1466,27 +1473,18 @@ static void rcheevos_client_load_game_callback(int result,
rcheevos_validate_config_settings();
rcheevos_enforce_hardcore_settings();
}
else
{
#if HAVE_REWIND
/* Re-enable rewind. Additional space will be allocated for the achievement state data */
if (settings->bools.rewind_enable)
{
#ifdef HAVE_THREADS
if (!task_is_on_main_thread())
{
/* Have to "schedule" this. CMD_EVENT_REWIND_REINIT should
* only be called on the main thread */
rcheevos_locals.queued_command = CMD_EVENT_REWIND_REINIT;
}
else
#endif
command_event(CMD_EVENT_REWIND_REINIT, NULL);
}
#endif
}

rcheevos_spectating_changed(); /* synchronize spectating state */

#ifdef HAVE_THREADS
if (!task_is_on_main_thread())
{
/* have to "schedule" this. game image should not be loaded into memory on background thread */
rcheevos_locals.queued_command = CMD_CHEEVOS_FINALIZE_LOAD;
}
else
#endif
rcheevos_finalize_game_load_on_ui_thread();
}

static rc_clock_t rcheevos_client_get_time_millisecs(const rc_client_t* client)
Expand Down
1 change: 0 additions & 1 deletion cheevos/cheevos_locals.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ typedef struct rcheevos_locals_t

#ifdef HAVE_THREADS
enum event_command queued_command; /* action queued by background thread to be run on main thread */
bool game_placard_requested; /* request to display game placard */
#endif

char user_agent_prefix[128]; /* RetroArch/OS version information */
Expand Down
8 changes: 8 additions & 0 deletions intl/msg_hash_us.h
Original file line number Diff line number Diff line change
Expand Up @@ -15583,6 +15583,14 @@ MSG_HASH(
MSG_CHEEVOS_HARDCORE_MODE_DISABLED_CHEAT,
"A cheat was activated. Achievements Hardcore Mode disabled for the current session."
)
MSG_HASH(
MSG_CHEEVOS_HARDCORE_MODE_CHANGED_BY_HOST,
"Achievements Hardcore Mode changed by host."
)
MSG_HASH(
MSG_CHEEVOS_HARDCORE_MODE_REQUIRES_NEWER_HOST,
"Netplay host needs to be updated. Achievements Hardcore Mode disabled for current session."
)
MSG_HASH(
MSG_CHEEVOS_MASTERED_GAME,
"Mastered %s"
Expand Down
2 changes: 2 additions & 0 deletions msg_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -4097,6 +4097,8 @@ enum msg_hash_enums
MSG_CHEEVOS_LOAD_STATE_PREVENTED_BY_HARDCORE_MODE,
MSG_CHEEVOS_HARDCORE_MODE_DISABLED,
MSG_CHEEVOS_HARDCORE_MODE_DISABLED_CHEAT,
MSG_CHEEVOS_HARDCORE_MODE_CHANGED_BY_HOST,
MSG_CHEEVOS_HARDCORE_MODE_REQUIRES_NEWER_HOST,
MSG_CHEEVOS_MASTERED_GAME,
MSG_CHEEVOS_COMPLETED_GAME,
MSG_CHEEVOS_HARDCORE_MODE_ENABLE,
Expand Down
2 changes: 2 additions & 0 deletions network/netplay/netplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ void deinit_netplay(void);

bool netplay_driver_ctl(enum rarch_netplay_ctl_state state, void *data);

bool netplay_reinit_serialization(void);
bool netplay_is_spectating(void);
void netplay_force_send_savestate(void);

#ifdef HAVE_NETPLAYDISCOVERY
/** Initialize Netplay discovery */
Expand Down
Loading

0 comments on commit 0113226

Please sign in to comment.