(Cheevos) Cleanups

This commit is contained in:
twinaphex 2020-02-19 22:20:24 +01:00
parent 068529929b
commit c182916554

View File

@ -617,13 +617,13 @@ Test all the achievements (call once per frame).
static void rcheevos_award_task_softcore(retro_task_t *task, void* task_data, void* user_data, static void rcheevos_award_task_softcore(retro_task_t *task, void* task_data, void* user_data,
const char* error) const char* error)
{ {
int ret;
char buffer[256], user_agent[256];
settings_t *settings = config_get_ptr(); settings_t *settings = config_get_ptr();
const rcheevos_cheevo_t* cheevo = (const rcheevos_cheevo_t*)user_data; const rcheevos_cheevo_t* cheevo = (const rcheevos_cheevo_t*)user_data;
char buffer[256], user_agent[256];
int ret;
buffer[0] = 0; buffer[0] = 0;
if (error == NULL) if (!error)
{ {
CHEEVOS_LOG(RCHEEVOS_TAG "Awarded achievement %u\n", cheevo->info->id); CHEEVOS_LOG(RCHEEVOS_TAG "Awarded achievement %u\n", cheevo->info->id);
return; return;
@ -650,13 +650,13 @@ static void rcheevos_award_task_softcore(retro_task_t *task, void* task_data, vo
static void rcheevos_award_task_hardcore(retro_task_t *task, void* task_data, void* user_data, static void rcheevos_award_task_hardcore(retro_task_t *task, void* task_data, void* user_data,
const char* error) const char* error)
{ {
settings_t *settings = config_get_ptr();
const rcheevos_cheevo_t* cheevo = (const rcheevos_cheevo_t*)user_data;
char buffer[256], user_agent[256];
int ret; int ret;
char buffer[256], user_agent[256];
const rcheevos_cheevo_t* cheevo = (const rcheevos_cheevo_t*)user_data;
settings_t *settings = config_get_ptr();
buffer[0] = 0; buffer[0] = 0;
if (error == NULL) if (!error)
{ {
CHEEVOS_LOG(RCHEEVOS_TAG "Awarded achievement %u\n", cheevo->info->id); CHEEVOS_LOG(RCHEEVOS_TAG "Awarded achievement %u\n", cheevo->info->id);
return; return;
@ -682,8 +682,8 @@ static void rcheevos_award_task_hardcore(retro_task_t *task, void* task_data, vo
static void rcheevos_award(rcheevos_cheevo_t* cheevo, int mode) static void rcheevos_award(rcheevos_cheevo_t* cheevo, int mode)
{ {
settings_t *settings = config_get_ptr();
char buffer[256]; char buffer[256];
settings_t *settings = config_get_ptr();
buffer[0] = 0; buffer[0] = 0;
CHEEVOS_LOG(RCHEEVOS_TAG "awarding cheevo %u: %s (%s)\n", CHEEVOS_LOG(RCHEEVOS_TAG "awarding cheevo %u: %s (%s)\n",
@ -845,14 +845,15 @@ static void rcheevos_test_cheevo_set(bool official)
static void rcheevos_lboard_submit_task(retro_task_t *task, void* task_data, void* user_data, static void rcheevos_lboard_submit_task(retro_task_t *task, void* task_data, void* user_data,
const char* error) const char* error)
{ {
settings_t *settings = config_get_ptr(); int ret;
const rcheevos_lboard_t* lboard = (const rcheevos_lboard_t*)user_data;
MD5_CTX ctx; MD5_CTX ctx;
uint8_t hash[16]; uint8_t hash[16];
char signature[64]; char signature[64];
char buffer[256]; char buffer[256];
char user_agent[256]; char user_agent[256];
int ret; const rcheevos_lboard_t* lboard = (const rcheevos_lboard_t*)user_data;
settings_t *settings = config_get_ptr();
const char *cheevos_username = settings->arrays.cheevos_username;
if (!error) if (!error)
{ {
@ -866,15 +867,15 @@ static void rcheevos_lboard_submit_task(retro_task_t *task, void* task_data, voi
/* Evaluate the signature. */ /* Evaluate the signature. */
snprintf(signature, sizeof(signature), "%u%s%u", lboard->info->id, snprintf(signature, sizeof(signature), "%u%s%u", lboard->info->id,
settings->arrays.cheevos_username, cheevos_username, lboard->info->id);
lboard->info->id);
MD5_Init(&ctx); MD5_Init(&ctx);
MD5_Update(&ctx, (void*)signature, strlen(signature)); MD5_Update(&ctx, (void*)signature, strlen(signature));
MD5_Final(hash, &ctx); MD5_Final(hash, &ctx);
/* Start the request. */ /* Start the request. */
ret = rc_url_submit_lboard(buffer, sizeof(buffer), settings->arrays.cheevos_username, rcheevos_locals.token, lboard->info->id, lboard->last_value, hash); ret = rc_url_submit_lboard(buffer, sizeof(buffer), cheevos_username,
rcheevos_locals.token, lboard->info->id, lboard->last_value, hash);
if (ret != 0) if (ret != 0)
{ {
@ -992,13 +993,17 @@ const char* rcheevos_get_richpresence(void)
static void rcheevos_test_richpresence(void) static void rcheevos_test_richpresence(void)
{ {
settings_t* settings = config_get_ptr();
retro_time_t now = cpu_features_get_time_usec(); retro_time_t now = cpu_features_get_time_usec();
settings_t *settings = config_get_ptr();
const char *cheevos_username = settings->arrays.cheevos_username;
bool cheevos_richpresence_enable = settings->bools.cheevos_richpresence_enable;
bool discord_enable = settings->bools.discord_enable;
if (settings->bools.cheevos_richpresence_enable) if (cheevos_richpresence_enable)
{ {
/* update rich presence every two minutes */ /* Update rich presence every two minutes */
if (now < rcheevos_locals.richpresence.last_update + CHEEVOS_PING_FREQUENCY) if (now < rcheevos_locals.richpresence.last_update
+ CHEEVOS_PING_FREQUENCY)
return; return;
if (rcheevos_locals.richpresence.richpresence) if (rcheevos_locals.richpresence.richpresence)
@ -1010,8 +1015,9 @@ static void rcheevos_test_richpresence(void)
} }
else else
{ {
/* send ping every four minutes */ /* Send ping every four minutes */
if (now < rcheevos_locals.richpresence.last_update + CHEEVOS_PING_FREQUENCY * 2) if (now < rcheevos_locals.richpresence.last_update
+ CHEEVOS_PING_FREQUENCY * 2)
return; return;
} }
@ -1020,8 +1026,9 @@ static void rcheevos_test_richpresence(void)
{ {
char user_agent[256], url[256], post_data[1024]; char user_agent[256], url[256], post_data[1024];
snprintf(url, sizeof(url), "http://retroachievements.org/dorequest.php?r=ping&u=%s&t=%s", snprintf(url, sizeof(url),
settings->arrays.cheevos_username, rcheevos_locals.token); "http://retroachievements.org/dorequest.php?r=ping&u=%s&t=%s",
cheevos_username, rcheevos_locals.token);
if (rcheevos_locals.richpresence.evaluation[0]) if (rcheevos_locals.richpresence.evaluation[0])
{ {
@ -1031,14 +1038,12 @@ static void rcheevos_test_richpresence(void)
CHEEVOS_FREE(tmp); CHEEVOS_FREE(tmp);
#ifdef HAVE_DISCORD #ifdef HAVE_DISCORD
if (settings->bools.discord_enable) if (discord_enable)
discord_update(DISCORD_PRESENCE_RETROACHIEVEMENTS, false); discord_update(DISCORD_PRESENCE_RETROACHIEVEMENTS, false);
#endif #endif
} }
else else
{
snprintf(post_data, sizeof(post_data), "g=%u", rcheevos_locals.patchdata.game_id); snprintf(post_data, sizeof(post_data), "g=%u", rcheevos_locals.patchdata.game_id);
}
rcheevos_get_user_agent(user_agent); rcheevos_get_user_agent(user_agent);
task_push_http_post_transfer_with_user_agent(url, post_data, true, "POST", user_agent, NULL, NULL); task_push_http_post_transfer_with_user_agent(url, post_data, true, "POST", user_agent, NULL, NULL);
@ -1047,11 +1052,10 @@ static void rcheevos_test_richpresence(void)
void rcheevos_reset_game(void) void rcheevos_reset_game(void)
{ {
rcheevos_cheevo_t* cheevo;
rcheevos_lboard_t* lboard;
unsigned i; unsigned i;
rcheevos_lboard_t* lboard;
rcheevos_cheevo_t* cheevo = rcheevos_locals.core;
cheevo = rcheevos_locals.core;
for (i = 0; i < rcheevos_locals.patchdata.core_count; i++, cheevo++) for (i = 0; i < rcheevos_locals.patchdata.core_count; i++, cheevo++)
{ {
if (cheevo->trigger) if (cheevo->trigger)
@ -1060,7 +1064,9 @@ void rcheevos_reset_game(void)
} }
cheevo = rcheevos_locals.unofficial; cheevo = rcheevos_locals.unofficial;
for (i = 0; i < rcheevos_locals.patchdata.unofficial_count; i++, cheevo++)
for (i = 0; i < rcheevos_locals.patchdata.unofficial_count;
i++, cheevo++)
{ {
if (cheevo->trigger) if (cheevo->trigger)
rc_reset_trigger(cheevo->trigger); rc_reset_trigger(cheevo->trigger);
@ -1068,7 +1074,8 @@ void rcheevos_reset_game(void)
} }
lboard = rcheevos_locals.lboards; lboard = rcheevos_locals.lboards;
for (i = 0; i < rcheevos_locals.patchdata.lboard_count; i++, lboard++) for (i = 0; i < rcheevos_locals.patchdata.lboard_count;
i++, lboard++)
{ {
if (lboard->lboard) if (lboard->lboard)
rc_reset_lboard(lboard->lboard); rc_reset_lboard(lboard->lboard);
@ -1077,7 +1084,9 @@ void rcheevos_reset_game(void)
{ {
lboard->active = 0; lboard->active = 0;
/* this ensures the leaderboard won't restart until the start trigger is false for at least one frame */ /* This ensures the leaderboard won't restart
* until the start trigger is false for at
* least one frame */
if (lboard->lboard) if (lboard->lboard)
lboard->lboard->submitted = 1; lboard->lboard->submitted = 1;
} }
@ -1091,19 +1100,15 @@ static void rcheevos_append_menu_achievement(menu_displaylist_info_t* info, size
{ {
bool active = false; bool active = false;
if (cheevo->trigger == NULL) if (!cheevo->trigger)
{ {
enum_idx = MENU_ENUM_LABEL_CHEEVOS_UNSUPPORTED_ENTRY; enum_idx = MENU_ENUM_LABEL_CHEEVOS_UNSUPPORTED_ENTRY;
active = true; /* not really, but forces the badge to appear disabled */ active = true; /* not really, but forces the badge to appear disabled */
} }
else if (!(cheevo->active & RCHEEVOS_ACTIVE_HARDCORE)) else if (!(cheevo->active & RCHEEVOS_ACTIVE_HARDCORE))
{
enum_idx = MENU_ENUM_LABEL_CHEEVOS_UNLOCKED_ENTRY_HARDCORE; enum_idx = MENU_ENUM_LABEL_CHEEVOS_UNLOCKED_ENTRY_HARDCORE;
}
else if (!(cheevo->active & RCHEEVOS_ACTIVE_SOFTCORE)) else if (!(cheevo->active & RCHEEVOS_ACTIVE_SOFTCORE))
{
enum_idx = MENU_ENUM_LABEL_CHEEVOS_UNLOCKED_ENTRY; enum_idx = MENU_ENUM_LABEL_CHEEVOS_UNLOCKED_ENTRY;
}
else else
{ {
/* use enum passed in - either "Locked" for core or "Unofficial" for unofficial */ /* use enum passed in - either "Locked" for core or "Unofficial" for unofficial */
@ -1123,12 +1128,15 @@ void rcheevos_populate_menu(void* data)
#ifdef HAVE_MENU #ifdef HAVE_MENU
int i = 0; int i = 0;
int count = 0; int count = 0;
settings_t* settings = config_get_ptr();
menu_displaylist_info_t* info = (menu_displaylist_info_t*)data;
rcheevos_cheevo_t* cheevo = NULL; rcheevos_cheevo_t* cheevo = NULL;
menu_displaylist_info_t* info = (menu_displaylist_info_t*)data;
settings_t* settings = config_get_ptr();
bool cheevos_enable = settings->bools.cheevos_enable;
bool cheevos_hardcore_mode_enable = settings->bools.cheevos_hardcore_mode_enable;
bool cheevos_test_unofficial = settings->bools.cheevos_test_unofficial;
if ( settings->bools.cheevos_enable if ( cheevos_enable
&& settings->bools.cheevos_hardcore_mode_enable && cheevos_hardcore_mode_enable
&& rcheevos_loaded) && rcheevos_loaded)
{ {
if (!rcheevos_hardcore_paused) if (!rcheevos_hardcore_paused)
@ -1151,7 +1159,7 @@ void rcheevos_populate_menu(void* data)
rcheevos_append_menu_achievement(info, i++, MENU_ENUM_LABEL_CHEEVOS_LOCKED_ENTRY, cheevo++); rcheevos_append_menu_achievement(info, i++, MENU_ENUM_LABEL_CHEEVOS_LOCKED_ENTRY, cheevo++);
} }
if (settings->bools.cheevos_test_unofficial) if (cheevos_test_unofficial)
{ {
cheevo = rcheevos_locals.unofficial; cheevo = rcheevos_locals.unofficial;
for (count = rcheevos_locals.patchdata.unofficial_count; count > 0; count--) for (count = rcheevos_locals.patchdata.unofficial_count; count > 0; count--)
@ -1277,12 +1285,11 @@ bool rcheevos_unload(void)
bool rcheevos_toggle_hardcore_mode(void) bool rcheevos_toggle_hardcore_mode(void)
{ {
settings_t *settings = config_get_ptr(); settings_t *settings = config_get_ptr();
bool cheevos_hardcore_mode_enable = settings->bools.cheevos_hardcore_mode_enable;
if (!settings) bool rewind_enable = settings->bools.rewind_enable;
return false;
/* reset and deinit rewind to avoid cheat the score */ /* reset and deinit rewind to avoid cheat the score */
if ( settings->bools.cheevos_hardcore_mode_enable if (cheevos_hardcore_mode_enable
&& !rcheevos_hardcore_paused) && !rcheevos_hardcore_paused)
{ {
const char *msg = msg_hash_to_str( const char *msg = msg_hash_to_str(
@ -1295,7 +1302,7 @@ bool rcheevos_toggle_hardcore_mode(void)
* savestate previusly loaded. */ * savestate previusly loaded. */
command_event(CMD_EVENT_RESET, NULL); command_event(CMD_EVENT_RESET, NULL);
if (settings->bools.rewind_enable) if (rewind_enable)
command_event(CMD_EVENT_REWIND_DEINIT, NULL); command_event(CMD_EVENT_REWIND_DEINIT, NULL);
CHEEVOS_LOG("%s\n", msg); CHEEVOS_LOG("%s\n", msg);
@ -1303,7 +1310,7 @@ bool rcheevos_toggle_hardcore_mode(void)
} }
else else
{ {
if (settings->bools.rewind_enable) if (rewind_enable)
command_event(CMD_EVENT_REWIND_INIT, NULL); command_event(CMD_EVENT_REWIND_INIT, NULL);
} }
@ -1358,9 +1365,9 @@ const char* rcheevos_get_hash(void)
static void rcheevos_unlock_cb(unsigned id, void* userdata) static void rcheevos_unlock_cb(unsigned id, void* userdata)
{ {
rcheevos_cheevo_t* cheevo = NULL;
int i = 0; int i = 0;
unsigned j = 0, count = 0; unsigned j = 0, count = 0;
rcheevos_cheevo_t* cheevo = NULL;
for (i = 0; i < 2; i++) for (i = 0; i < 2; i++)
{ {
@ -1474,11 +1481,11 @@ enum
static int rcheevos_prepare_hash_psx(rcheevos_coro_t* coro) static int rcheevos_prepare_hash_psx(rcheevos_coro_t* coro)
{ {
char buffer[2048];
char exe_name_buffer[64]; char exe_name_buffer[64];
size_t exe_name_size; size_t exe_name_size;
char* exe_name = NULL; char* exe_name = NULL;
char* scan = NULL; char* scan = NULL;
char buffer[2048];
int success = 0; int success = 0;
size_t to_read = 0; size_t to_read = 0;
@ -1598,11 +1605,12 @@ static int rcheevos_prepare_hash_psx(rcheevos_coro_t* coro)
static int rcheevos_prepare_hash_nintendo_ds(rcheevos_coro_t* coro) static int rcheevos_prepare_hash_nintendo_ds(rcheevos_coro_t* coro)
{ {
intfstream_t* stream;
unsigned char header[512]; unsigned char header[512];
int success = 0; int success = 0;
intfstream_t *stream = intfstream_open_file(
coro->path, RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE);
stream = intfstream_open_file(coro->path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (stream) if (stream)
{ {
if (intfstream_read(stream, header, sizeof(header)) == 512) if (intfstream_read(stream, header, sizeof(header)) == 512)
@ -1674,13 +1682,13 @@ static int rcheevos_prepare_hash_nintendo_ds(rcheevos_coro_t* coro)
static int rcheevos_iterate(rcheevos_coro_t* coro) static int rcheevos_iterate(rcheevos_coro_t* coro)
{ {
char buffer[2048];
const int snes_header_len = 0x200; const int snes_header_len = 0x200;
const int lynx_header_len = 0x40; const int lynx_header_len = 0x40;
ssize_t num_read = 0; ssize_t num_read = 0;
size_t to_read = 4096; size_t to_read = 4096;
uint8_t* ptr = NULL; uint8_t* ptr = NULL;
const char* end = NULL; const char* end = NULL;
char buffer[2048];
static const uint32_t snes_exts[] = static const uint32_t snes_exts[] =
{ {
@ -2537,15 +2545,11 @@ found:
*************************************************************************/ *************************************************************************/
CORO_SUB(RCHEEVOS_LOGIN) CORO_SUB(RCHEEVOS_LOGIN)
{ {
const char* username;
const char* password;
const char* token;
int ret; int ret;
char tok[256]; char tok[256];
const char *username = coro->settings->arrays.cheevos_username;
username = coro->settings->arrays.cheevos_username; const char *password = coro->settings->arrays.cheevos_password;
password = coro->settings->arrays.cheevos_password; const char *token = coro->settings->arrays.cheevos_token;
token = coro->settings->arrays.cheevos_token;
if (rcheevos_locals.token[0]) if (rcheevos_locals.token[0])
CORO_RET(); CORO_RET();
@ -2738,7 +2742,9 @@ found:
/* Two calls - one for softcore and one for hardcore */ /* Two calls - one for softcore and one for hardcore */
for (coro->i = 0; coro->i < 2; coro->i++) for (coro->i = 0; coro->i < 2; coro->i++)
{ {
ret = rc_url_get_unlock_list(coro->url, sizeof(coro->url), coro->settings->arrays.cheevos_username, rcheevos_locals.token, coro->gameid, coro->i); ret = rc_url_get_unlock_list(coro->url, sizeof(coro->url),
coro->settings->arrays.cheevos_username,
rcheevos_locals.token, coro->gameid, coro->i);
if (ret < 0) if (ret < 0)
{ {
@ -2827,10 +2833,10 @@ static void rcheevos_task_handler(retro_task_t *task)
bool rcheevos_load(const void *data) bool rcheevos_load(const void *data)
{ {
retro_task_t *task; char buffer[32];
retro_task_t *task = NULL;
const struct retro_game_info *info = NULL; const struct retro_game_info *info = NULL;
rcheevos_coro_t *coro = NULL; rcheevos_coro_t *coro = NULL;
char buffer[32];
rcheevos_loaded = false; rcheevos_loaded = false;
rcheevos_hardcore_paused = false; rcheevos_hardcore_paused = false;
@ -2893,9 +2899,9 @@ bool rcheevos_load(const void *data)
char m3u_contents[1024]; char m3u_contents[1024];
char disc_path[PATH_MAX_LENGTH]; char disc_path[PATH_MAX_LENGTH];
char* tmp; char* tmp;
int64_t num_read; int64_t num_read = intfstream_read(
m3u_stream, m3u_contents, sizeof(m3u_contents) - 1);
num_read = intfstream_read(m3u_stream, m3u_contents, sizeof(m3u_contents) - 1);
intfstream_close(m3u_stream); intfstream_close(m3u_stream);
m3u_contents[num_read] = '\0'; m3u_contents[num_read] = '\0';
@ -2931,10 +2937,8 @@ bool rcheevos_load(const void *data)
task->title = NULL; task->title = NULL;
#ifdef HAVE_THREADS #ifdef HAVE_THREADS
if (rcheevos_locals.task_lock == NULL) if (!rcheevos_locals.task_lock)
{
rcheevos_locals.task_lock = slock_new(); rcheevos_locals.task_lock = slock_new();
}
#endif #endif
CHEEVOS_LOCK(rcheevos_locals.task_lock); CHEEVOS_LOCK(rcheevos_locals.task_lock);