Merge pull request #11226 from Jamiras/rcheevos_globals

(cheevos) eliminate exposed variables for tracking hardcore
This commit is contained in:
Autechre 2020-08-23 15:05:01 +02:00 committed by GitHub
commit 0b2f19e898
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 111 additions and 122 deletions

View File

@ -153,6 +153,8 @@ typedef struct
slock_t* task_lock;
#endif
bool hardcore_active;
bool loaded;
bool core_supports;
bool invalid_peek_address;
@ -166,6 +168,7 @@ typedef struct
char token[32];
char hash[33];
char user_agent_prefix[128];
} rcheevos_locals_t;
static rcheevos_locals_t rcheevos_locals =
@ -174,6 +177,8 @@ static rcheevos_locals_t rcheevos_locals =
#ifdef HAVE_THREADS
NULL, /* task_lock */
#endif
false,/* hardcore_active */
false,/* loaded */
true, /* core_supports */
false,/* invalid_peek_address */
{0}, /* patchdata */
@ -184,15 +189,9 @@ static rcheevos_locals_t rcheevos_locals =
{{0}},/* memory */
{0}, /* token */
"N/A",/* hash */
"", /* user_agent_prefix */
};
/* TODO/FIXME - public global variables */
bool rcheevos_loaded = false;
bool rcheevos_hardcore_active = false;
bool rcheevos_hardcore_paused = false;
bool rcheevos_state_loaded_flag = false;
char rcheevos_user_agent_prefix[128] = "";
#ifdef HAVE_THREADS
#define CHEEVOS_LOCK(l) do { slock_lock(l); } while (0)
#define CHEEVOS_UNLOCK(l) do { slock_unlock(l); } while (0)
@ -220,13 +219,13 @@ static void rcheevos_get_user_agent(char* buffer)
const char* scan;
char* ptr;
if (!rcheevos_user_agent_prefix[0])
if (!rcheevos_locals.user_agent_prefix[0])
{
const frontend_ctx_driver_t *frontend = frontend_get_ptr();
int major, minor;
char tmp[64];
ptr = rcheevos_user_agent_prefix + sprintf(rcheevos_user_agent_prefix, "RetroArch/%s", PACKAGE_VERSION);
ptr = rcheevos_locals.user_agent_prefix + sprintf(rcheevos_locals.user_agent_prefix, "RetroArch/%s", PACKAGE_VERSION);
if (frontend && frontend->get_os)
{
@ -235,7 +234,7 @@ static void rcheevos_get_user_agent(char* buffer)
}
}
ptr = buffer + sprintf(buffer, "%s", rcheevos_user_agent_prefix);
ptr = buffer + sprintf(buffer, "%s", rcheevos_locals.user_agent_prefix);
if (system && !string_is_empty(system->library_name))
{
@ -869,12 +868,11 @@ static int rcheevos_has_indirect_memref(const rc_memref_value_t* memrefs)
static void rcheevos_test_cheevo_set(bool official)
{
settings_t *settings = config_get_ptr();
int mode = RCHEEVOS_ACTIVE_SOFTCORE;
rcheevos_cheevo_t* cheevo;
int i, count;
if (settings && settings->bools.cheevos_hardcore_mode_enable && !rcheevos_hardcore_paused)
if (rcheevos_locals.hardcore_active)
mode = RCHEEVOS_ACTIVE_HARDCORE;
if (official)
@ -1121,9 +1119,7 @@ void rcheevos_get_achievement_state(unsigned index, char *buffer, size_t buffer_
else if (!(cheevo->active & RCHEEVOS_ACTIVE_SOFTCORE))
{
/* if in hardcore mode, track progress towards hardcore unlock */
const settings_t* settings = config_get_ptr();
const bool hardcore = settings->bools.cheevos_hardcore_mode_enable && !rcheevos_hardcore_paused;
check_measured = hardcore;
check_measured = rcheevos_locals.hardcore_active;
enum_idx = MENU_ENUM_LABEL_VALUE_CHEEVOS_UNLOCKED_ENTRY;
}
@ -1188,9 +1184,9 @@ void rcheevos_populate_menu(void* data)
if ( cheevos_enable
&& cheevos_hardcore_mode_enable
&& rcheevos_loaded)
&& rcheevos_locals.loaded)
{
if (!rcheevos_hardcore_paused)
if (rcheevos_locals.hardcore_active)
menu_entries_append_enum(info->list,
msg_hash_to_str(MENU_ENUM_LABEL_VALUE_ACHIEVEMENT_PAUSE),
msg_hash_to_str(MENU_ENUM_LABEL_ACHIEVEMENT_PAUSE),
@ -1236,7 +1232,7 @@ bool rcheevos_get_description(rcheevos_ctx_desc_t* desc)
*desc->s = 0;
if (rcheevos_loaded)
if (rcheevos_locals.loaded)
{
idx = desc->idx;
@ -1258,9 +1254,15 @@ bool rcheevos_get_description(rcheevos_ctx_desc_t* desc)
return true;
}
bool rcheevos_hardcore_active(void)
{
return rcheevos_locals.hardcore_active;
}
void rcheevos_pause_hardcore(void)
{
rcheevos_hardcore_paused = true;
if (rcheevos_locals.hardcore_active)
rcheevos_toggle_hardcore_paused();
}
bool rcheevos_unload(void)
@ -1288,7 +1290,7 @@ bool rcheevos_unload(void)
#endif
}
if (rcheevos_loaded)
if (rcheevos_locals.loaded)
{
for (i = 0, count = rcheevos_locals.patchdata.core_count; i < count; i++)
{
@ -1320,10 +1322,8 @@ bool rcheevos_unload(void)
rcheevos_locals.lboards = NULL;
rcheevos_locals.richpresence.richpresence = NULL;
rcheevos_loaded = false;
rcheevos_hardcore_active = false;
rcheevos_hardcore_paused = false;
rcheevos_state_loaded_flag = false;
rcheevos_locals.loaded = false;
rcheevos_locals.hardcore_active = false;
}
/* if the config-level token has been cleared, we need to re-login on loading the next game */
@ -1333,40 +1333,63 @@ bool rcheevos_unload(void)
return true;
}
bool rcheevos_toggle_hardcore_mode(void)
static void rcheevos_toggle_hardcore_active(void)
{
settings_t *settings = config_get_ptr();
bool cheevos_hardcore_mode_enable = settings->bools.cheevos_hardcore_mode_enable;
bool rewind_enable = settings->bools.rewind_enable;
settings_t* settings = config_get_ptr();
bool rewind_enable = settings->bools.rewind_enable;
/* reset and deinit rewind to avoid cheat the score */
if (cheevos_hardcore_mode_enable
&& !rcheevos_hardcore_paused)
if (!rcheevos_locals.hardcore_active)
{
const char *msg = msg_hash_to_str(
MSG_CHEEVOS_HARDCORE_MODE_ENABLE);
/* activate hardcore */
rcheevos_locals.hardcore_active = true;
/* reset the state loaded flag in case it was set */
rcheevos_state_loaded_flag = false;
if (rcheevos_locals.loaded)
{
const char* msg = msg_hash_to_str(MSG_CHEEVOS_HARDCORE_MODE_ENABLE);
CHEEVOS_LOG("%s\n", msg);
runloop_msg_queue_push(msg, 0, 3 * 60, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
/* send reset core cmd to avoid any user
* savestate previusly loaded. */
command_event(CMD_EVENT_RESET, NULL);
/* reset the game */
command_event(CMD_EVENT_RESET, NULL);
}
/* deinit rewind */
if (rewind_enable)
command_event(CMD_EVENT_REWIND_DEINIT, NULL);
CHEEVOS_LOG("%s\n", msg);
runloop_msg_queue_push(msg, 0, 3 * 60, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
else
{
/* pause hardcore */
rcheevos_locals.hardcore_active = false;
if (rcheevos_locals.loaded)
{
CHEEVOS_LOG(RCHEEVOS_TAG "Hardcore paused\n");
}
/* re-init rewind */
if (rewind_enable)
command_event(CMD_EVENT_REWIND_INIT, NULL);
}
}
return true;
void rcheevos_toggle_hardcore_paused(void)
{
settings_t* settings = config_get_ptr();
/* if hardcore mode is not enabled, we can't toggle it */
if (settings->bools.cheevos_hardcore_mode_enable)
rcheevos_toggle_hardcore_active();
}
void rcheevos_hardcore_enabled_changed(void)
{
const settings_t* settings = config_get_ptr();
const bool enabled = settings && settings->bools.cheevos_enable && settings->bools.cheevos_hardcore_mode_enable;
if (enabled != rcheevos_locals.hardcore_active)
rcheevos_toggle_hardcore_active();
}
/*****************************************************************************
@ -1374,7 +1397,12 @@ Test all the achievements (call once per frame).
*****************************************************************************/
void rcheevos_test(void)
{
settings_t *settings = config_get_ptr();
settings_t* settings;
if (!rcheevos_locals.loaded)
return;
settings = config_get_ptr();
if (rcheevos_locals.memory.count == 0)
{
@ -1383,10 +1411,11 @@ void rcheevos_test(void)
{
CHEEVOS_ERR(RCHEEVOS_TAG "No memory exposed by core\n");
if (settings->bools.cheevos_verbose_enable)
if (settings && settings->bools.cheevos_verbose_enable)
runloop_msg_queue_push("Cannot activate achievements using this core.", 0, 4 * 60, false, NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_WARNING);
rcheevos_loaded = false;
rcheevos_locals.loaded = false;
rcheevos_pause_hardcore();
return;
}
}
@ -1398,9 +1427,7 @@ void rcheevos_test(void)
if (settings->bools.cheevos_test_unofficial)
rcheevos_test_cheevo_set(false);
if (settings->bools.cheevos_hardcore_mode_enable &&
settings->bools.cheevos_leaderboards_enable &&
!rcheevos_hardcore_paused)
if (rcheevos_locals.hardcore_active && settings->bools.cheevos_leaderboards_enable)
rcheevos_test_leaderboards();
}
}
@ -1556,7 +1583,7 @@ static int rcheevos_iterate(rcheevos_coro_t* coro)
{
CHEEVOS_LOG(RCHEEVOS_TAG "this game doesn't feature achievements\n");
strcpy(rcheevos_locals.hash, "N/A");
rcheevos_hardcore_paused = true;
rcheevos_pause_hardcore();
CORO_STOP();
}
@ -1609,12 +1636,11 @@ static int rcheevos_iterate(rcheevos_coro_t* coro)
"This game has no achievements.",
0, 5 * 60, false, NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
rcheevos_hardcore_paused = true;
rcheevos_pause_hardcore();
CORO_STOP();
}
rcheevos_loaded = true;
rcheevos_locals.loaded = true;
/*
* Inputs: CHEEVOS_VAR_GAMEID
@ -1638,7 +1664,7 @@ static int rcheevos_iterate(rcheevos_coro_t* coro)
int number_of_unlocked = rcheevos_locals.patchdata.core_count;
int number_of_unsupported = 0;
if (coro->settings->bools.cheevos_hardcore_mode_enable && !rcheevos_hardcore_paused)
if (rcheevos_locals.hardcore_active)
mode = RCHEEVOS_ACTIVE_HARDCORE;
for (; cheevo < end; cheevo++)
@ -2256,15 +2282,17 @@ bool rcheevos_load(const void *data)
struct rc_hash_filereader filereader;
struct rc_hash_cdreader cdreader;
rcheevos_loaded = false;
rcheevos_hardcore_paused = false;
rcheevos_locals.loaded = false;
if (!cheevos_enable || !rcheevos_locals.core_supports || !data)
{
rcheevos_hardcore_paused = true;
rcheevos_pause_hardcore();
return false;
}
/* reset hardcore mode based on configs */
rcheevos_hardcore_enabled_changed();
coro = (rcheevos_coro_t*)calloc(1, sizeof(*coro));
if (!coro)

View File

@ -53,7 +53,8 @@ void rcheevos_pause_hardcore();
bool rcheevos_unload(void);
bool rcheevos_toggle_hardcore_mode(void);
void rcheevos_hardcore_enabled_changed(void);
void rcheevos_toggle_hardcore_paused(void);
void rcheevos_test(void);
@ -67,10 +68,7 @@ const char *rcheevos_get_richpresence(void);
uint8_t* rcheevos_patch_address(unsigned address);
extern bool rcheevos_loaded;
extern bool rcheevos_hardcore_active;
extern bool rcheevos_hardcore_paused;
extern bool rcheevos_state_loaded_flag;
bool rcheevos_hardcore_active(void);
RETRO_END_DECLS

View File

@ -108,11 +108,8 @@ void cheat_manager_apply_cheats(void)
}
#ifdef HAVE_CHEEVOS
if (idx != 0)
if ( rcheevos_hardcore_active
&& rcheevos_loaded
&& !rcheevos_hardcore_paused)
cheat_manager_pause_cheevos();
if (idx != 0 && rcheevos_hardcore_active())
cheat_manager_pause_cheevos();
#endif
}
@ -1568,11 +1565,8 @@ void cheat_manager_apply_retro_cheats(void)
}
#ifdef HAVE_CHEEVOS
if (cheat_applied)
if ( rcheevos_hardcore_active
&& rcheevos_loaded
&& !rcheevos_hardcore_paused)
cheat_manager_pause_cheevos();
if (cheat_applied && rcheevos_hardcore_active())
cheat_manager_pause_cheevos();
#endif
}

View File

@ -85,10 +85,6 @@
#include "../../network/netplay/netplay_discovery.h"
#endif
#ifdef HAVE_CHEEVOS
#include "../../cheevos/cheevos.h"
#endif
#ifdef __WINRT__
#include "../../uwp/uwp_func.h"
#endif
@ -3965,9 +3961,6 @@ static int action_ok_save_state(const char *path,
static int action_ok_cheevos_toggle_hardcore_mode(const char *path,
const char *label, unsigned type, size_t idx, size_t entry_idx)
{
#ifdef HAVE_CHEEVOS
rcheevos_hardcore_paused = !rcheevos_hardcore_paused;
#endif
generic_action_ok_command(CMD_EVENT_CHEEVOS_HARDCORE_MODE_TOGGLE);
return generic_action_ok_command(CMD_EVENT_RESUME);
}

View File

@ -2509,7 +2509,7 @@ static int menu_displaylist_parse_load_content_settings(
count++;
#ifdef HAVE_CHEEVOS
if (!rcheevos_hardcore_active)
if (!rcheevos_hardcore_active())
#endif
{
if (menu_entries_append_enum(list,
@ -2525,7 +2525,7 @@ static int menu_displaylist_parse_load_content_settings(
settings->bools.quick_menu_show_undo_save_load_state)
{
#ifdef HAVE_CHEEVOS
if (!rcheevos_hardcore_active)
if (!rcheevos_hardcore_active())
#endif
{
if (menu_entries_append_enum(list,

View File

@ -7389,16 +7389,7 @@ static void change_handler_video_layout_selected_view(rarch_setting_t *setting)
#ifdef HAVE_CHEEVOS
static void achievement_hardcore_mode_write_handler(rarch_setting_t *setting)
{
settings_t *settings = config_get_ptr();
if (!setting)
return;
if (settings && settings->bools.cheevos_enable && settings->bools.cheevos_hardcore_mode_enable)
{
rcheevos_toggle_hardcore_mode();
command_event(CMD_EVENT_RESET, NULL);
}
rcheevos_hardcore_enabled_changed();
}
#endif

View File

@ -12971,7 +12971,7 @@ static bool command_write_ram(const char *arg)
if (!data)
return false;
if (rcheevos_hardcore_active && rcheevos_loaded && !rcheevos_hardcore_paused)
if (rcheevos_hardcore_active())
{
RARCH_LOG("Achievements hardcore mode disabled by WRITE_CORE_RAM\n");
rcheevos_pause_hardcore();
@ -14845,7 +14845,7 @@ static void command_event_load_auto_state(
if (!global || !savestate_auto_load)
return;
#ifdef HAVE_CHEEVOS
if (rcheevos_hardcore_active)
if (rcheevos_hardcore_active())
return;
#endif
#ifdef HAVE_NETWORKING
@ -15248,7 +15248,7 @@ static bool command_event_save_auto_state(
return false;
#ifdef HAVE_CHEEVOS
if (rcheevos_hardcore_active)
if (rcheevos_hardcore_active())
return false;
#endif
@ -15551,8 +15551,11 @@ static bool command_event_main_state(
if (content_load_state(state_path, false, false))
{
#ifdef HAVE_CHEEVOS
if (rcheevos_hardcore_active)
rcheevos_state_loaded_flag = true;
if (rcheevos_hardcore_active())
{
rcheevos_pause_hardcore();
runloop_msg_queue_push(msg_hash_to_str(MSG_CHEEVOS_HARDCORE_MODE_DISABLED), 0, 180, true, NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
#endif
ret = true;
#ifdef HAVE_NETWORKING
@ -15977,7 +15980,7 @@ bool command_event(enum event_command cmd, void *data)
#endif
#ifdef HAVE_CHEEVOS
if (rcheevos_hardcore_active)
if (rcheevos_hardcore_active())
return false;
#endif
if (!command_event_main_state(p_rarch, cmd))
@ -16004,10 +16007,6 @@ bool command_event(enum event_command cmd, void *data)
#endif
break;
case CMD_EVENT_RESET:
#ifdef HAVE_CHEEVOS
rcheevos_state_loaded_flag = false;
rcheevos_hardcore_paused = false;
#endif
RARCH_LOG("%s.\n", msg_hash_to_str(MSG_RESET));
runloop_msg_queue_push(msg_hash_to_str(MSG_RESET), 1, 120, true, NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
@ -16140,7 +16139,7 @@ bool command_event(enum event_command cmd, void *data)
break;
case CMD_EVENT_CHEEVOS_HARDCORE_MODE_TOGGLE:
#ifdef HAVE_CHEEVOS
rcheevos_toggle_hardcore_mode();
rcheevos_toggle_hardcore_paused();
#endif
break;
case CMD_EVENT_REINIT_FROM_TOGGLE:
@ -16158,10 +16157,6 @@ bool command_event(enum event_command cmd, void *data)
break;
case CMD_EVENT_REWIND_DEINIT:
#ifdef HAVE_REWIND
#ifdef HAVE_CHEEVOS
if (rcheevos_hardcore_active)
return false;
#endif
state_manager_event_deinit();
#endif
break;
@ -16171,7 +16166,7 @@ bool command_event(enum event_command cmd, void *data)
bool rewind_enable = settings->bools.rewind_enable;
unsigned rewind_buf_size = settings->sizes.rewind_buffer_size;
#ifdef HAVE_CHEEVOS
if (rcheevos_hardcore_active)
if (rcheevos_hardcore_active())
return false;
#endif
if (rewind_enable)
@ -39377,17 +39372,7 @@ static enum runloop_state runloop_check_state(
HOTKEY_CHECK(RARCH_LOAD_STATE_KEY, CMD_EVENT_LOAD_STATE, true, NULL);
#ifdef HAVE_CHEEVOS
rcheevos_hardcore_active = settings->bools.cheevos_enable
&& settings->bools.cheevos_hardcore_mode_enable
&& !rcheevos_hardcore_paused;
if (rcheevos_hardcore_active && rcheevos_state_loaded_flag)
{
rcheevos_hardcore_paused = true;
runloop_msg_queue_push(msg_hash_to_str(MSG_CHEEVOS_HARDCORE_MODE_DISABLED), 0, 180, true, NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
if (!rcheevos_hardcore_active)
if (!rcheevos_hardcore_active())
#endif
#ifdef HAVE_REWIND
{
@ -39418,7 +39403,7 @@ static enum runloop_state runloop_check_state(
/* Checks if slowmotion toggle/hold was being pressed and/or held. */
#ifdef HAVE_CHEEVOS
if (!rcheevos_hardcore_active)
if (!rcheevos_hardcore_active())
#endif
{
static bool old_slowmotion_button_state = false;
@ -39745,7 +39730,7 @@ int runloop_iterate(void)
p_rarch, current_time);
#ifdef HAVE_CHEEVOS
if (settings->bools.cheevos_enable && rcheevos_loaded)
if (settings->bools.cheevos_enable)
rcheevos_test();
#endif
#ifdef HAVE_CHEATS

View File

@ -1059,10 +1059,10 @@ static bool content_file_load(
if (type == RARCH_CONTENT_NONE && !string_is_empty(content_path))
rcheevos_load(info);
else
rcheevos_hardcore_paused = true;
rcheevos_pause_hardcore();
}
else
rcheevos_hardcore_paused = true;
rcheevos_pause_hardcore();
#endif
return true;