mirror of
https://github.com/libretro/RetroArch.git
synced 2025-02-12 20:58:18 +00:00
(cheevos) upgrade to rcheevos 10.3 (#13546)
* upgrade to rcheevos 10.3 * use rcheevos cdreader for gdi/cue processing * update widgets when loading state
This commit is contained in:
parent
3a67162ae2
commit
2ecb253ed4
@ -2175,6 +2175,7 @@ ifeq ($(HAVE_NETWORKING), 1)
|
||||
deps/rcheevos/src/rcheevos/runtime_progress.o \
|
||||
deps/rcheevos/src/rcheevos/trigger.o \
|
||||
deps/rcheevos/src/rcheevos/value.o \
|
||||
deps/rcheevos/src/rhash/cdreader.o \
|
||||
deps/rcheevos/src/rhash/hash.o \
|
||||
deps/rcheevos/src/rapi/rc_api_common.o \
|
||||
deps/rcheevos/src/rapi/rc_api_runtime.o \
|
||||
|
@ -71,6 +71,7 @@
|
||||
#include "../tasks/tasks_internal.h"
|
||||
|
||||
#include "../deps/rcheevos/include/rc_runtime.h"
|
||||
#include "../deps/rcheevos/include/rc_runtime_types.h"
|
||||
#include "../deps/rcheevos/include/rc_hash.h"
|
||||
#include "../deps/rcheevos/src/rcheevos/rc_libretro.h"
|
||||
|
||||
@ -1144,14 +1145,67 @@ bool rcheevos_get_serialized_data(void* buffer)
|
||||
|
||||
bool rcheevos_set_serialized_data(void* buffer)
|
||||
{
|
||||
if (rcheevos_locals.loaded)
|
||||
if (rcheevos_locals.loaded && buffer)
|
||||
{
|
||||
if (buffer && rc_runtime_deserialize_progress(
|
||||
&rcheevos_locals.runtime,
|
||||
(const unsigned char*)buffer, NULL) == RC_OK)
|
||||
return true;
|
||||
const int result = rc_runtime_deserialize_progress(
|
||||
&rcheevos_locals.runtime, (const unsigned char*)buffer, NULL);
|
||||
|
||||
rc_runtime_reset(&rcheevos_locals.runtime);
|
||||
#if defined(HAVE_GFX_WIDGETS)
|
||||
if (gfx_widgets_ready() && rcheevos_is_player_active())
|
||||
{
|
||||
settings_t* settings = config_get_ptr();
|
||||
|
||||
if (rcheevos_locals.leaderboard_trackers)
|
||||
{
|
||||
unsigned i;
|
||||
rc_runtime_lboard_t* lboard = rcheevos_locals.runtime.lboards;
|
||||
for (i = 0; i < rcheevos_locals.runtime.lboard_count; ++i, ++lboard)
|
||||
{
|
||||
if (!lboard->lboard)
|
||||
continue;
|
||||
|
||||
if (lboard->lboard->state == RC_LBOARD_STATE_STARTED)
|
||||
{
|
||||
rcheevos_ralboard_t* ralboard = rcheevos_find_lboard(lboard->id);
|
||||
if (ralboard != NULL)
|
||||
{
|
||||
char value[32];
|
||||
rc_runtime_format_lboard_value(value, sizeof(value), lboard->value, ralboard->format);
|
||||
gfx_widgets_set_leaderboard_display(lboard->id, value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
gfx_widgets_set_leaderboard_display(lboard->id, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (settings->bools.cheevos_challenge_indicators)
|
||||
{
|
||||
unsigned i;
|
||||
rc_runtime_trigger_t* cheevo = rcheevos_locals.runtime.triggers;
|
||||
for (i = 0; i < rcheevos_locals.runtime.trigger_count; ++i, ++cheevo)
|
||||
{
|
||||
if (!cheevo->trigger)
|
||||
continue;
|
||||
|
||||
if (cheevo->trigger->state == RC_TRIGGER_STATE_PRIMED)
|
||||
{
|
||||
rcheevos_racheevo_t* racheevo = rcheevos_find_cheevo(cheevo->id);
|
||||
if (racheevo != NULL)
|
||||
gfx_widgets_set_challenge_display(racheevo->id, racheevo->badge);
|
||||
}
|
||||
else
|
||||
{
|
||||
gfx_widgets_set_challenge_display(cheevo->id, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return (result == RC_OK);
|
||||
}
|
||||
|
||||
return false;
|
||||
@ -1206,6 +1260,7 @@ static void rc_hash_handle_file_close(void* file_handle)
|
||||
CHEEVOS_FREE(file_handle);
|
||||
}
|
||||
|
||||
#ifdef HAVE_CHD
|
||||
static void* rc_hash_handle_cd_open_track(
|
||||
const char* path, uint32_t track)
|
||||
{
|
||||
@ -1218,27 +1273,11 @@ static void* rc_hash_handle_cd_open_track(
|
||||
break;
|
||||
|
||||
case RC_HASH_CDTRACK_LAST:
|
||||
#ifdef HAVE_CHD
|
||||
if (string_is_equal_noncase(path_get_extension(path), "chd"))
|
||||
{
|
||||
cdfs_track = cdfs_open_track(path, CHDSTREAM_TRACK_LAST);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
CHEEVOS_LOG(RCHEEVOS_TAG "Last track only supported for CHD\n");
|
||||
cdfs_track = NULL;
|
||||
cdfs_track = cdfs_open_track(path, CHDSTREAM_TRACK_LAST);
|
||||
break;
|
||||
|
||||
case RC_HASH_CDTRACK_LARGEST:
|
||||
#ifdef HAVE_CHD
|
||||
if (string_is_equal_noncase(path_get_extension(path), "chd"))
|
||||
{
|
||||
cdfs_track = cdfs_open_track(path, CHDSTREAM_TRACK_PRIMARY);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
CHEEVOS_LOG(RCHEEVOS_TAG "Largest track only supported for CHD, using first data track\n");
|
||||
cdfs_track = cdfs_open_data_track(path);
|
||||
cdfs_track = cdfs_open_track(path, CHDSTREAM_TRACK_LAST);
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -1279,6 +1318,7 @@ static void rc_hash_handle_cd_close_track(void* track_handle)
|
||||
CHEEVOS_FREE(file);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* end hooks */
|
||||
|
||||
@ -1577,18 +1617,54 @@ static void rcheevos_identify_game_callback(void* userdata)
|
||||
static bool rcheevos_identify_game(const struct retro_game_info* info)
|
||||
{
|
||||
struct rcheevos_identify_game_data* data;
|
||||
struct rc_hash_filereader filereader;
|
||||
struct rc_hash_iterator iterator;
|
||||
size_t len;
|
||||
char hash[33];
|
||||
|
||||
#ifndef HAVE_CHD
|
||||
if (string_is_equal_noncase(path_get_extension(info->path), "chd"))
|
||||
{
|
||||
CHEEVOS_LOG(RCHEEVOS_TAG "CHD not supported without HAVE_CHD compile flag\n");
|
||||
return false;
|
||||
}
|
||||
#ifndef DEBUG
|
||||
settings_t* settings = config_get_ptr();
|
||||
#endif
|
||||
|
||||
/* provide hooks for reading files */
|
||||
memset(&filereader, 0, sizeof(filereader));
|
||||
filereader.open = rc_hash_handle_file_open;
|
||||
filereader.seek = rc_hash_handle_file_seek;
|
||||
filereader.tell = rc_hash_handle_file_tell;
|
||||
filereader.read = rc_hash_handle_file_read;
|
||||
filereader.close = rc_hash_handle_file_close;
|
||||
rc_hash_init_custom_filereader(&filereader);
|
||||
|
||||
rc_hash_init_error_message_callback(rcheevos_handle_log_message);
|
||||
|
||||
#ifndef DEBUG
|
||||
/* in DEBUG mode, always initialize the verbose message handler */
|
||||
if (settings->bools.cheevos_verbose_enable)
|
||||
#endif
|
||||
{
|
||||
rc_hash_init_verbose_message_callback(rcheevos_handle_log_message);
|
||||
}
|
||||
|
||||
if (string_is_equal_noncase(path_get_extension(info->path), "chd"))
|
||||
{
|
||||
#ifdef HAVE_CHD
|
||||
struct rc_hash_cdreader cdreader;
|
||||
memset(&cdreader, 0, sizeof(cdreader));
|
||||
cdreader.open_track = rc_hash_handle_cd_open_track;
|
||||
cdreader.read_sector = rc_hash_handle_cd_read_sector;
|
||||
cdreader.close_track = rc_hash_handle_cd_close_track;
|
||||
rc_hash_init_custom_cdreader(&cdreader);
|
||||
#else
|
||||
CHEEVOS_LOG(RCHEEVOS_TAG "Cannot generate hash from CHD without HAVE_CHD compile flag\n");
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
/* cdfs_ functions don't support gdi files or first track sector calculations */
|
||||
rc_hash_init_default_cdreader();
|
||||
}
|
||||
|
||||
/* fetch the first hash */
|
||||
rc_hash_initialize_iterator(&iterator,
|
||||
info->path, (uint8_t*)info->data, info->size);
|
||||
if (!rc_hash_iterate(hash, &iterator))
|
||||
@ -1706,8 +1782,6 @@ bool rcheevos_load_aborted(void)
|
||||
|
||||
bool rcheevos_load(const void *data)
|
||||
{
|
||||
struct rc_hash_cdreader cdreader;
|
||||
struct rc_hash_filereader filereader;
|
||||
const struct retro_game_info *info = (const struct retro_game_info*)
|
||||
data;
|
||||
settings_t *settings = config_get_ptr();
|
||||
@ -1747,31 +1821,6 @@ bool rcheevos_load(const void *data)
|
||||
rcheevos_validate_config_settings();
|
||||
rcheevos_leaderboards_enabled_changed();
|
||||
|
||||
/* provide hooks for reading files */
|
||||
memset(&filereader, 0, sizeof(filereader));
|
||||
filereader.open = rc_hash_handle_file_open;
|
||||
filereader.seek = rc_hash_handle_file_seek;
|
||||
filereader.tell = rc_hash_handle_file_tell;
|
||||
filereader.read = rc_hash_handle_file_read;
|
||||
filereader.close = rc_hash_handle_file_close;
|
||||
rc_hash_init_custom_filereader(&filereader);
|
||||
|
||||
memset(&cdreader, 0, sizeof(cdreader));
|
||||
cdreader.open_track = rc_hash_handle_cd_open_track;
|
||||
cdreader.read_sector = rc_hash_handle_cd_read_sector;
|
||||
cdreader.close_track = rc_hash_handle_cd_close_track;
|
||||
rc_hash_init_custom_cdreader(&cdreader);
|
||||
|
||||
rc_hash_init_error_message_callback(rcheevos_handle_log_message);
|
||||
|
||||
#ifndef DEBUG
|
||||
/* in DEBUG mode, always initialize the verbose message handler */
|
||||
if (settings->bools.cheevos_verbose_enable)
|
||||
#endif
|
||||
{
|
||||
rc_hash_init_verbose_message_callback(rcheevos_handle_log_message);
|
||||
}
|
||||
|
||||
/* Refresh the user agent in case it's not set or has changed */
|
||||
rcheevos_client_initialize();
|
||||
rcheevos_get_user_agent(&rcheevos_locals,
|
||||
|
@ -116,9 +116,12 @@ bool rcheevos_menu_get_state(unsigned menu_offset, char *buffer, size_t len)
|
||||
if (cheevo)
|
||||
{
|
||||
if (cheevo->menu_progress)
|
||||
snprintf(buffer, len, "%s - %d%%",
|
||||
msg_hash_to_str(menuitem->state_label_idx),
|
||||
cheevo->menu_progress);
|
||||
{
|
||||
const int written = snprintf(buffer, len, "%s - ",
|
||||
msg_hash_to_str(menuitem->state_label_idx));
|
||||
if (len - written > 0)
|
||||
rc_runtime_format_achievement_measured(&rcheevos_locals->runtime, cheevo->id, buffer + written, len - written);
|
||||
}
|
||||
else
|
||||
strlcpy(buffer, msg_hash_to_str(menuitem->state_label_idx), len);
|
||||
|
||||
|
7
deps/rcheevos/include/rc_api_runtime.h
vendored
7
deps/rcheevos/include/rc_api_runtime.h
vendored
@ -88,6 +88,10 @@ typedef struct rc_api_leaderboard_definition_t {
|
||||
const char* description;
|
||||
/* The definition of the leaderboard to be passed to rc_runtime_activate_lboard */
|
||||
const char* definition;
|
||||
/* Non-zero if lower values are better for this leaderboard */
|
||||
int lower_is_better;
|
||||
/* Non-zero if the leaderboard should not be displayed in a list of leaderboards */
|
||||
int hidden;
|
||||
}
|
||||
rc_api_leaderboard_definition_t;
|
||||
|
||||
@ -210,6 +214,9 @@ typedef struct rc_api_award_achievement_response_t {
|
||||
unsigned awarded_achievement_id;
|
||||
/* The updated player score */
|
||||
unsigned new_player_score;
|
||||
/* The number of achievements the user has not yet unlocked for this game
|
||||
* (in hardcore/non-hardcore per hardcore flag in request) */
|
||||
unsigned achievements_remaining;
|
||||
|
||||
/* Common server-provided response information */
|
||||
rc_api_response_t response;
|
||||
|
4
deps/rcheevos/include/rc_consoles.h
vendored
4
deps/rcheevos/include/rc_consoles.h
vendored
@ -76,6 +76,10 @@ enum {
|
||||
RC_CONSOLE_SHARPX1 = 64,
|
||||
RC_CONSOLE_TIC80 = 65,
|
||||
RC_CONSOLE_THOMSONTO8 = 66,
|
||||
RC_CONSOLE_PC6000 = 67,
|
||||
RC_CONSOLE_PICO = 68,
|
||||
RC_CONSOLE_MEGADUCK = 69,
|
||||
RC_CONSOLE_ZEEBO = 70,
|
||||
|
||||
RC_CONSOLE_HUBS = 100,
|
||||
RC_CONSOLE_EVENTS = 101
|
||||
|
8
deps/rcheevos/include/rc_hash.h
vendored
8
deps/rcheevos/include/rc_hash.h
vendored
@ -101,7 +101,7 @@ extern "C" {
|
||||
*/
|
||||
typedef void* (*rc_hash_cdreader_open_track_handler)(const char* path, uint32_t track);
|
||||
|
||||
/* attempts to read the specified number of bytes from the file starting at the read pointer.
|
||||
/* attempts to read the specified number of bytes from the file starting at the specified absolute sector.
|
||||
* returns the number of bytes actually read.
|
||||
*/
|
||||
typedef size_t (*rc_hash_cdreader_read_sector_handler)(void* track_handle, uint32_t sector, void* buffer, size_t requested_bytes);
|
||||
@ -109,15 +109,15 @@ extern "C" {
|
||||
/* closes the track handle */
|
||||
typedef void (*rc_hash_cdreader_close_track_handler)(void* track_handle);
|
||||
|
||||
/* convert absolute sector to track sector */
|
||||
typedef uint32_t(*rc_hash_cdreader_absolute_sector_to_track_sector)(void* track_handle, uint32_t sector);
|
||||
/* gets the absolute sector index for the first sector of a track */
|
||||
typedef uint32_t(*rc_hash_cdreader_first_track_sector_handler)(void* track_handle);
|
||||
|
||||
struct rc_hash_cdreader
|
||||
{
|
||||
rc_hash_cdreader_open_track_handler open_track;
|
||||
rc_hash_cdreader_read_sector_handler read_sector;
|
||||
rc_hash_cdreader_close_track_handler close_track;
|
||||
rc_hash_cdreader_absolute_sector_to_track_sector absolute_sector_to_track_sector;
|
||||
rc_hash_cdreader_first_track_sector_handler first_track_sector;
|
||||
};
|
||||
|
||||
void rc_hash_init_default_cdreader(void);
|
||||
|
2
deps/rcheevos/include/rc_runtime.h
vendored
2
deps/rcheevos/include/rc_runtime.h
vendored
@ -58,6 +58,7 @@ typedef struct rc_runtime_lboard_t {
|
||||
void* buffer;
|
||||
rc_memref_t* invalid_memref;
|
||||
unsigned char md5[16];
|
||||
int serialized_size;
|
||||
char owns_memrefs;
|
||||
}
|
||||
rc_runtime_lboard_t;
|
||||
@ -66,6 +67,7 @@ typedef struct rc_runtime_richpresence_t {
|
||||
rc_richpresence_t* richpresence;
|
||||
void* buffer;
|
||||
struct rc_runtime_richpresence_t* previous;
|
||||
unsigned char md5[16];
|
||||
char owns_memrefs;
|
||||
}
|
||||
rc_runtime_richpresence_t;
|
||||
|
17
deps/rcheevos/include/rc_runtime_types.h
vendored
17
deps/rcheevos/include/rc_runtime_types.h
vendored
@ -54,6 +54,8 @@ enum {
|
||||
RC_MEMSIZE_16_BITS_BE,
|
||||
RC_MEMSIZE_24_BITS_BE,
|
||||
RC_MEMSIZE_32_BITS_BE,
|
||||
RC_MEMSIZE_FLOAT,
|
||||
RC_MEMSIZE_MBF32,
|
||||
RC_MEMSIZE_VARIABLE
|
||||
};
|
||||
|
||||
@ -67,6 +69,8 @@ typedef struct rc_memref_value_t {
|
||||
char size;
|
||||
/* True if the value changed this frame. */
|
||||
char changed;
|
||||
/* The value type of the value (for variables) */
|
||||
char type;
|
||||
/* True if the reference will be used in indirection.
|
||||
* NOTE: This is actually a property of the rc_memref_t, but we put it here to save space */
|
||||
char is_indirect;
|
||||
@ -123,7 +127,7 @@ typedef struct rc_operand_t {
|
||||
}
|
||||
rc_operand_t;
|
||||
|
||||
int rc_operand_is_memref(rc_operand_t* operand);
|
||||
int rc_operand_is_memref(const rc_operand_t* operand);
|
||||
|
||||
/*****************************************************************************\
|
||||
| Conditions |
|
||||
@ -280,7 +284,7 @@ struct rc_value_t {
|
||||
/* The list of conditions to evaluate. */
|
||||
rc_condset_t* conditions;
|
||||
|
||||
/* The memory references required by the value. */
|
||||
/* The memory references required by the variable. */
|
||||
rc_memref_t* memrefs;
|
||||
|
||||
/* The name of the variable. */
|
||||
@ -337,7 +341,13 @@ enum {
|
||||
RC_FORMAT_SCORE,
|
||||
RC_FORMAT_VALUE,
|
||||
RC_FORMAT_MINUTES,
|
||||
RC_FORMAT_SECONDS_AS_MINUTES
|
||||
RC_FORMAT_SECONDS_AS_MINUTES,
|
||||
RC_FORMAT_FLOAT1,
|
||||
RC_FORMAT_FLOAT2,
|
||||
RC_FORMAT_FLOAT3,
|
||||
RC_FORMAT_FLOAT4,
|
||||
RC_FORMAT_FLOAT5,
|
||||
RC_FORMAT_FLOAT6
|
||||
};
|
||||
|
||||
int rc_parse_format(const char* format_str);
|
||||
@ -398,6 +408,7 @@ rc_richpresence_t* rc_parse_richpresence(void* buffer, const char* script, lua_S
|
||||
int rc_evaluate_richpresence(rc_richpresence_t* richpresence, char* buffer, unsigned buffersize, rc_peek_t peek, void* peek_ud, lua_State* L);
|
||||
void rc_update_richpresence(rc_richpresence_t* richpresence, rc_peek_t peek, void* peek_ud, lua_State* L);
|
||||
int rc_get_richpresence_display_string(rc_richpresence_t* richpresence, char* buffer, unsigned buffersize, rc_peek_t peek, void* peek_ud, lua_State* L);
|
||||
void rc_reset_richpresence(rc_richpresence_t* self);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
12
deps/rcheevos/src/rapi/rc_api_common.c
vendored
12
deps/rcheevos/src/rapi/rc_api_common.c
vendored
@ -4,8 +4,6 @@
|
||||
|
||||
#include "../rcheevos/rc_compat.h"
|
||||
|
||||
#include "../rhash/md5.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -631,7 +629,7 @@ int rc_json_get_unum(unsigned* out, const rc_json_field_t* field, const char* fi
|
||||
return 1;
|
||||
}
|
||||
|
||||
void rc_json_get_optional_unum(unsigned* out, const rc_json_field_t* field, const char* field_name, int default_value) {
|
||||
void rc_json_get_optional_unum(unsigned* out, const rc_json_field_t* field, const char* field_name, unsigned default_value) {
|
||||
if (!rc_json_get_unum(out, field, field_name))
|
||||
*out = default_value;
|
||||
}
|
||||
@ -813,13 +811,7 @@ void rc_api_destroy_request(rc_api_request_t* request) {
|
||||
rc_buf_destroy(&request->buffer);
|
||||
}
|
||||
|
||||
void rc_api_generate_checksum(char checksum[33], const char* data) {
|
||||
md5_state_t md5;
|
||||
md5_byte_t digest[16];
|
||||
|
||||
md5_init(&md5);
|
||||
md5_append(&md5, (unsigned char*)data, (int)strlen(data));
|
||||
md5_finish(&md5, digest);
|
||||
void rc_api_format_md5(char checksum[33], const unsigned char digest[16]) {
|
||||
snprintf(checksum, 33, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
|
||||
digest[0], digest[1], digest[2], digest[3], digest[4], digest[5], digest[6], digest[7],
|
||||
digest[8], digest[9], digest[10], digest[11], digest[12], digest[13], digest[14], digest[15]
|
||||
|
4
deps/rcheevos/src/rapi/rc_api_common.h
vendored
4
deps/rcheevos/src/rapi/rc_api_common.h
vendored
@ -46,7 +46,7 @@ int rc_json_get_bool(int* out, const rc_json_field_t* field, const char* field_n
|
||||
int rc_json_get_datetime(time_t* out, const rc_json_field_t* field, const char* field_name);
|
||||
void rc_json_get_optional_string(const char** out, rc_api_response_t* response, const rc_json_field_t* field, const char* field_name, const char* default_value);
|
||||
void rc_json_get_optional_num(int* out, const rc_json_field_t* field, const char* field_name, int default_value);
|
||||
void rc_json_get_optional_unum(unsigned* out, const rc_json_field_t* field, const char* field_name, int default_value);
|
||||
void rc_json_get_optional_unum(unsigned* out, const rc_json_field_t* field, const char* field_name, unsigned default_value);
|
||||
void rc_json_get_optional_bool(int* out, const rc_json_field_t* field, const char* field_name, int default_value);
|
||||
int rc_json_get_required_string(const char** out, rc_api_response_t* response, const rc_json_field_t* field, const char* field_name);
|
||||
int rc_json_get_required_num(int* out, rc_api_response_t* response, const rc_json_field_t* field, const char* field_name);
|
||||
@ -72,7 +72,7 @@ void rc_url_builder_append_str_param(rc_api_url_builder_t* builder, const char*
|
||||
|
||||
void rc_api_url_build_dorequest_url(rc_api_request_t* request);
|
||||
int rc_api_url_build_dorequest(rc_api_url_builder_t* builder, const char* api, const char* username, const char* api_token);
|
||||
void rc_api_generate_checksum(char checksum[33], const char* data);
|
||||
void rc_api_format_md5(char checksum[33], const unsigned char digest[16]);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
45
deps/rcheevos/src/rapi/rc_api_runtime.c
vendored
45
deps/rcheevos/src/rapi/rc_api_runtime.c
vendored
@ -4,6 +4,7 @@
|
||||
#include "rc_runtime.h"
|
||||
#include "rc_runtime_types.h"
|
||||
#include "../rcheevos/rc_compat.h"
|
||||
#include "../rhash/md5.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
@ -119,7 +120,9 @@ int rc_api_process_fetch_game_data_response(rc_api_fetch_game_data_response_t* r
|
||||
{"Title"},
|
||||
{"Description"},
|
||||
{"Mem"},
|
||||
{"Format"}
|
||||
{"Format"},
|
||||
{"LowerIsBetter"},
|
||||
{"Hidden"}
|
||||
};
|
||||
|
||||
memset(response, 0, sizeof(*response));
|
||||
@ -237,6 +240,8 @@ int rc_api_process_fetch_game_data_response(rc_api_fetch_game_data_response_t* r
|
||||
return RC_MISSING_VALUE;
|
||||
if (!rc_json_get_required_string(&leaderboard->definition, &response->response, &leaderboard_fields[3], "Mem"))
|
||||
return RC_MISSING_VALUE;
|
||||
rc_json_get_optional_bool(&leaderboard->lower_is_better, &leaderboard_fields[5], "LowerIsBetter", 0);
|
||||
rc_json_get_optional_bool(&leaderboard->hidden, &leaderboard_fields[6], "Hidden", 0);
|
||||
|
||||
if (!leaderboard_fields[4].value_end)
|
||||
return RC_MISSING_VALUE;
|
||||
@ -304,8 +309,9 @@ void rc_api_destroy_ping_response(rc_api_ping_response_t* response) {
|
||||
|
||||
int rc_api_init_award_achievement_request(rc_api_request_t* request, const rc_api_award_achievement_request_t* api_params) {
|
||||
rc_api_url_builder_t builder;
|
||||
char signature[128];
|
||||
char checksum[33];
|
||||
char buffer[33];
|
||||
md5_state_t md5;
|
||||
md5_byte_t digest[16];
|
||||
|
||||
rc_api_url_build_dorequest_url(request);
|
||||
|
||||
@ -320,9 +326,15 @@ int rc_api_init_award_achievement_request(rc_api_request_t* request, const rc_ap
|
||||
rc_url_builder_append_str_param(&builder, "m", api_params->game_hash);
|
||||
|
||||
/* Evaluate the signature. */
|
||||
snprintf(signature, sizeof(signature), "%u%s%u", api_params->achievement_id, api_params->username, api_params->hardcore ? 1 : 0);
|
||||
rc_api_generate_checksum(checksum, signature);
|
||||
rc_url_builder_append_str_param(&builder, "v", checksum);
|
||||
md5_init(&md5);
|
||||
snprintf(buffer, sizeof(buffer), "%u", api_params->achievement_id);
|
||||
md5_append(&md5, (md5_byte_t*)buffer, (int)strlen(buffer));
|
||||
md5_append(&md5, (md5_byte_t*)api_params->username, (int)strlen(api_params->username));
|
||||
snprintf(buffer, sizeof(buffer), "%d", api_params->hardcore ? 1 : 0);
|
||||
md5_append(&md5, (md5_byte_t*)buffer, (int)strlen(buffer));
|
||||
md5_finish(&md5, digest);
|
||||
rc_api_format_md5(buffer, digest);
|
||||
rc_url_builder_append_str_param(&builder, "v", buffer);
|
||||
|
||||
request->post_data = rc_url_builder_finalize(&builder);
|
||||
}
|
||||
@ -336,7 +348,8 @@ int rc_api_process_award_achievement_response(rc_api_award_achievement_response_
|
||||
{"Success"},
|
||||
{"Error"},
|
||||
{"Score"},
|
||||
{"AchievementID"}
|
||||
{"AchievementID"},
|
||||
{"AchievementsRemaining"}
|
||||
};
|
||||
|
||||
memset(response, 0, sizeof(*response));
|
||||
@ -361,6 +374,7 @@ int rc_api_process_award_achievement_response(rc_api_award_achievement_response_
|
||||
|
||||
rc_json_get_optional_unum(&response->new_player_score, &fields[2], "Score", 0);
|
||||
rc_json_get_optional_unum(&response->awarded_achievement_id, &fields[3], "AchievementID", 0);
|
||||
rc_json_get_optional_unum(&response->achievements_remaining, &fields[4], "AchievementsRemaining", (unsigned)-1);
|
||||
|
||||
return RC_OK;
|
||||
}
|
||||
@ -373,8 +387,9 @@ void rc_api_destroy_award_achievement_response(rc_api_award_achievement_response
|
||||
|
||||
int rc_api_init_submit_lboard_entry_request(rc_api_request_t* request, const rc_api_submit_lboard_entry_request_t* api_params) {
|
||||
rc_api_url_builder_t builder;
|
||||
char signature[128];
|
||||
char checksum[33];
|
||||
char buffer[33];
|
||||
md5_state_t md5;
|
||||
md5_byte_t digest[16];
|
||||
|
||||
rc_api_url_build_dorequest_url(request);
|
||||
|
||||
@ -390,9 +405,15 @@ int rc_api_init_submit_lboard_entry_request(rc_api_request_t* request, const rc_
|
||||
rc_url_builder_append_str_param(&builder, "m", api_params->game_hash);
|
||||
|
||||
/* Evaluate the signature. */
|
||||
snprintf(signature, sizeof(signature), "%u%s%d", api_params->leaderboard_id, api_params->username, api_params->score);
|
||||
rc_api_generate_checksum(checksum, signature);
|
||||
rc_url_builder_append_str_param(&builder, "v", checksum);
|
||||
md5_init(&md5);
|
||||
snprintf(buffer, sizeof(buffer), "%u", api_params->leaderboard_id);
|
||||
md5_append(&md5, (md5_byte_t*)buffer, (int)strlen(buffer));
|
||||
md5_append(&md5, (md5_byte_t*)api_params->username, (int)strlen(api_params->username));
|
||||
snprintf(buffer, sizeof(buffer), "%d", api_params->score);
|
||||
md5_append(&md5, (md5_byte_t*)buffer, (int)strlen(buffer));
|
||||
md5_finish(&md5, digest);
|
||||
rc_api_format_md5(buffer, digest);
|
||||
rc_url_builder_append_str_param(&builder, "v", buffer);
|
||||
|
||||
request->post_data = rc_url_builder_finalize(&builder);
|
||||
}
|
||||
|
88
deps/rcheevos/src/rcheevos/condition.c
vendored
88
deps/rcheevos/src/rcheevos/condition.c
vendored
@ -70,6 +70,7 @@ rc_condition_t* rc_parse_condition(const char** memaddr, rc_parse_state_t* parse
|
||||
self = RC_ALLOC(rc_condition_t, parse);
|
||||
self->current_hits = 0;
|
||||
self->is_true = 0;
|
||||
self->pause = 0;
|
||||
|
||||
if (*aux != 0 && aux[1] == ':') {
|
||||
switch (*aux) {
|
||||
@ -106,11 +107,6 @@ rc_condition_t* rc_parse_condition(const char** memaddr, rc_parse_state_t* parse
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (self->operand1.type == RC_OPERAND_FP) {
|
||||
parse->offset = can_modify ? RC_INVALID_FP_OPERAND : RC_INVALID_COMPARISON;
|
||||
return 0;
|
||||
}
|
||||
|
||||
result = rc_parse_operator(&aux);
|
||||
if (result < 0) {
|
||||
parse->offset = result;
|
||||
@ -175,11 +171,6 @@ rc_condition_t* rc_parse_condition(const char** memaddr, rc_parse_state_t* parse
|
||||
self->operand2.value.num = 0;
|
||||
}
|
||||
|
||||
if (!can_modify && self->operand2.type == RC_OPERAND_FP) {
|
||||
parse->offset = RC_INVALID_COMPARISON;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (*aux == '(') {
|
||||
char* end;
|
||||
self->required_hits = (unsigned)strtoul(++aux, &end, 10);
|
||||
@ -222,63 +213,52 @@ rc_condition_t* rc_parse_condition(const char** memaddr, rc_parse_state_t* parse
|
||||
return self;
|
||||
}
|
||||
|
||||
int rc_test_condition(rc_condition_t* self, rc_eval_state_t* eval_state) {
|
||||
unsigned value1 = rc_evaluate_operand(&self->operand1, eval_state) + eval_state->add_value;
|
||||
unsigned value2 = rc_evaluate_operand(&self->operand2, eval_state);
|
||||
int rc_condition_is_combining(const rc_condition_t* self) {
|
||||
switch (self->type) {
|
||||
case RC_CONDITION_STANDARD:
|
||||
case RC_CONDITION_PAUSE_IF:
|
||||
case RC_CONDITION_RESET_IF:
|
||||
case RC_CONDITION_MEASURED_IF:
|
||||
case RC_CONDITION_TRIGGER:
|
||||
case RC_CONDITION_MEASURED:
|
||||
return 0;
|
||||
|
||||
switch (self->oper) {
|
||||
case RC_OPERATOR_EQ: return value1 == value2;
|
||||
case RC_OPERATOR_NE: return value1 != value2;
|
||||
case RC_OPERATOR_LT: return value1 < value2;
|
||||
case RC_OPERATOR_LE: return value1 <= value2;
|
||||
case RC_OPERATOR_GT: return value1 > value2;
|
||||
case RC_OPERATOR_GE: return value1 >= value2;
|
||||
case RC_OPERATOR_NONE: return 1;
|
||||
default: return 1;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int rc_evaluate_condition_value(rc_condition_t* self, rc_eval_state_t* eval_state) {
|
||||
unsigned value = rc_evaluate_operand(&self->operand1, eval_state);
|
||||
int rc_test_condition(rc_condition_t* self, rc_eval_state_t* eval_state) {
|
||||
rc_typed_value_t value1, value2;
|
||||
|
||||
rc_evaluate_operand(&value1, &self->operand1, eval_state);
|
||||
if (eval_state->add_value.type != RC_VALUE_TYPE_NONE)
|
||||
rc_typed_value_add(&value1, &eval_state->add_value);
|
||||
|
||||
rc_evaluate_operand(&value2, &self->operand2, eval_state);
|
||||
|
||||
return rc_typed_value_compare(&value1, &value2, self->oper);
|
||||
}
|
||||
|
||||
void rc_evaluate_condition_value(rc_typed_value_t* value, rc_condition_t* self, rc_eval_state_t* eval_state) {
|
||||
rc_typed_value_t amount;
|
||||
|
||||
rc_evaluate_operand(value, &self->operand1, eval_state);
|
||||
rc_evaluate_operand(&amount, &self->operand2, eval_state);
|
||||
|
||||
switch (self->oper) {
|
||||
case RC_OPERATOR_MULT:
|
||||
if (self->operand2.type == RC_OPERAND_FP) {
|
||||
value = (int)((double)value * self->operand2.value.dbl);
|
||||
}
|
||||
else {
|
||||
/* the c standard for unsigned multiplication is well defined as non-overflowing truncation
|
||||
* to the type's size. this allows negative multiplication through twos-complements. i.e.
|
||||
* 1 * -1 (0xFFFFFFFF) = 0xFFFFFFFF = -1
|
||||
* 3 * -2 (0xFFFFFFFE) = 0x2FFFFFFFA & 0xFFFFFFFF = 0xFFFFFFFA = -6
|
||||
* 10 * -5 (0xFFFFFFFB) = 0x9FFFFFFCE & 0xFFFFFFFF = 0xFFFFFFCE = -50
|
||||
*/
|
||||
value *= rc_evaluate_operand(&self->operand2, eval_state);
|
||||
}
|
||||
rc_typed_value_multiply(value, &amount);
|
||||
break;
|
||||
|
||||
case RC_OPERATOR_DIV:
|
||||
if (self->operand2.type == RC_OPERAND_FP)
|
||||
{
|
||||
if (self->operand2.value.dbl == 0.0)
|
||||
value = 0;
|
||||
else
|
||||
value = (int)((double)value / self->operand2.value.dbl);
|
||||
}
|
||||
else
|
||||
{
|
||||
unsigned value2 = rc_evaluate_operand(&self->operand2, eval_state);
|
||||
if (value2 == 0)
|
||||
value = 0;
|
||||
else
|
||||
value /= value2;
|
||||
}
|
||||
rc_typed_value_divide(value, &amount);
|
||||
break;
|
||||
|
||||
case RC_OPERATOR_AND:
|
||||
value &= rc_evaluate_operand(&self->operand2, eval_state);
|
||||
rc_typed_value_convert(value, RC_VALUE_TYPE_UNSIGNED);
|
||||
rc_typed_value_convert(&amount, RC_VALUE_TYPE_UNSIGNED);
|
||||
value->value.u32 &= amount.value.u32;
|
||||
break;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
127
deps/rcheevos/src/rcheevos/condset.c
vendored
127
deps/rcheevos/src/rcheevos/condset.c
vendored
@ -1,36 +1,32 @@
|
||||
#include "rc_internal.h"
|
||||
|
||||
static void rc_update_condition_pause(rc_condition_t* condition, int* in_pause) {
|
||||
if (condition->next != 0) {
|
||||
rc_update_condition_pause(condition->next, in_pause);
|
||||
}
|
||||
#include <string.h> /* memcpy */
|
||||
|
||||
switch (condition->type) {
|
||||
case RC_CONDITION_PAUSE_IF:
|
||||
*in_pause = condition->pause = 1;
|
||||
break;
|
||||
static void rc_update_condition_pause(rc_condition_t* condition) {
|
||||
rc_condition_t* subclause = condition;
|
||||
|
||||
case RC_CONDITION_ADD_SOURCE:
|
||||
case RC_CONDITION_SUB_SOURCE:
|
||||
case RC_CONDITION_ADD_HITS:
|
||||
case RC_CONDITION_SUB_HITS:
|
||||
case RC_CONDITION_AND_NEXT:
|
||||
case RC_CONDITION_OR_NEXT:
|
||||
case RC_CONDITION_ADD_ADDRESS:
|
||||
case RC_CONDITION_RESET_NEXT_IF:
|
||||
condition->pause = (char)*in_pause;
|
||||
break;
|
||||
while (condition) {
|
||||
if (condition->type == RC_CONDITION_PAUSE_IF) {
|
||||
while (subclause != condition) {
|
||||
subclause->pause = 1;
|
||||
subclause = subclause->next;
|
||||
}
|
||||
condition->pause = 1;
|
||||
}
|
||||
else {
|
||||
condition->pause = 0;
|
||||
}
|
||||
|
||||
default:
|
||||
*in_pause = condition->pause = 0;
|
||||
break;
|
||||
if (!rc_condition_is_combining(condition))
|
||||
subclause = condition->next;
|
||||
|
||||
condition = condition->next;
|
||||
}
|
||||
}
|
||||
|
||||
rc_condset_t* rc_parse_condset(const char** memaddr, rc_parse_state_t* parse, int is_value) {
|
||||
rc_condset_t* self;
|
||||
rc_condition_t** next;
|
||||
int in_pause;
|
||||
int in_add_address;
|
||||
unsigned measured_target = 0;
|
||||
|
||||
@ -85,8 +81,20 @@ rc_condset_t* rc_parse_condset(const char** memaddr, rc_parse_state_t* parse, in
|
||||
}
|
||||
else if (is_value) {
|
||||
measured_target = (unsigned)-1;
|
||||
if ((*next)->oper != RC_OPERATOR_NONE)
|
||||
(*next)->required_hits = measured_target;
|
||||
switch ((*next)->oper)
|
||||
{
|
||||
case RC_OPERATOR_AND:
|
||||
case RC_OPERATOR_DIV:
|
||||
case RC_OPERATOR_MULT:
|
||||
case RC_OPERATOR_NONE:
|
||||
/* measuring value. leave required_hits at 0 */
|
||||
break;
|
||||
|
||||
default:
|
||||
/* comparison operator, measuring hits. set required_hits to MAX_INT */
|
||||
(*next)->required_hits = measured_target;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if ((*next)->required_hits != 0) {
|
||||
measured_target = (*next)->required_hits;
|
||||
@ -94,6 +102,9 @@ rc_condset_t* rc_parse_condset(const char** memaddr, rc_parse_state_t* parse, in
|
||||
else if ((*next)->operand2.type == RC_OPERAND_CONST) {
|
||||
measured_target = (*next)->operand2.value.num;
|
||||
}
|
||||
else if ((*next)->operand2.type == RC_OPERAND_FP) {
|
||||
measured_target = (unsigned)(*next)->operand2.value.dbl;
|
||||
}
|
||||
else {
|
||||
parse->offset = RC_INVALID_MEASURED_TARGET;
|
||||
return 0;
|
||||
@ -132,10 +143,8 @@ rc_condset_t* rc_parse_condset(const char** memaddr, rc_parse_state_t* parse, in
|
||||
|
||||
*next = 0;
|
||||
|
||||
if (parse->buffer != 0) {
|
||||
in_pause = 0;
|
||||
rc_update_condition_pause(self->conditions, &in_pause);
|
||||
}
|
||||
if (parse->buffer != 0)
|
||||
rc_update_condition_pause(self->conditions);
|
||||
|
||||
return self;
|
||||
}
|
||||
@ -146,7 +155,10 @@ static void rc_condset_update_indirect_memrefs(rc_condition_t* condition, int pr
|
||||
continue;
|
||||
|
||||
if (condition->type == RC_CONDITION_ADD_ADDRESS) {
|
||||
eval_state->add_address = rc_evaluate_condition_value(condition, eval_state);
|
||||
rc_typed_value_t value;
|
||||
rc_evaluate_condition_value(&value, condition, eval_state);
|
||||
rc_typed_value_convert(&value, RC_VALUE_TYPE_UNSIGNED);
|
||||
eval_state->add_address = value.value.u32;
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -163,20 +175,25 @@ static void rc_condset_update_indirect_memrefs(rc_condition_t* condition, int pr
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int rc_test_condset_internal(rc_condset_t* self, int processing_pause, rc_eval_state_t* eval_state) {
|
||||
rc_condition_t* condition;
|
||||
int set_valid, cond_valid, and_next, or_next, reset_next;
|
||||
unsigned measured_value = 0;
|
||||
unsigned total_hits = 0;
|
||||
int can_measure = 1, measured_from_hits = 0;
|
||||
rc_typed_value_t value;
|
||||
int set_valid, cond_valid, and_next, or_next, reset_next, measured_from_hits, can_measure;
|
||||
rc_typed_value_t measured_value;
|
||||
unsigned total_hits;
|
||||
|
||||
measured_value.type = RC_VALUE_TYPE_NONE;
|
||||
measured_from_hits = 0;
|
||||
can_measure = 1;
|
||||
total_hits = 0;
|
||||
|
||||
eval_state->primed = 1;
|
||||
set_valid = 1;
|
||||
and_next = 1;
|
||||
or_next = 0;
|
||||
reset_next = 0;
|
||||
eval_state->add_value = eval_state->add_hits = eval_state->add_address = 0;
|
||||
eval_state->add_value.type = RC_VALUE_TYPE_NONE;
|
||||
eval_state->add_hits = eval_state->add_address = 0;
|
||||
|
||||
for (condition = self->conditions; condition != 0; condition = condition->next) {
|
||||
if (condition->pause != processing_pause)
|
||||
@ -185,23 +202,30 @@ static int rc_test_condset_internal(rc_condset_t* self, int processing_pause, rc
|
||||
/* STEP 1: process modifier conditions */
|
||||
switch (condition->type) {
|
||||
case RC_CONDITION_ADD_SOURCE:
|
||||
eval_state->add_value += rc_evaluate_condition_value(condition, eval_state);
|
||||
rc_evaluate_condition_value(&value, condition, eval_state);
|
||||
rc_typed_value_add(&eval_state->add_value, &value);
|
||||
eval_state->add_address = 0;
|
||||
continue;
|
||||
|
||||
case RC_CONDITION_SUB_SOURCE:
|
||||
eval_state->add_value -= rc_evaluate_condition_value(condition, eval_state);
|
||||
rc_evaluate_condition_value(&value, condition, eval_state);
|
||||
rc_typed_value_convert(&value, RC_VALUE_TYPE_SIGNED);
|
||||
value.value.i32 = -value.value.i32;
|
||||
rc_typed_value_add(&eval_state->add_value, &value);
|
||||
eval_state->add_address = 0;
|
||||
continue;
|
||||
|
||||
case RC_CONDITION_ADD_ADDRESS:
|
||||
eval_state->add_address = rc_evaluate_condition_value(condition, eval_state);
|
||||
rc_evaluate_condition_value(&value, condition, eval_state);
|
||||
rc_typed_value_convert(&value, RC_VALUE_TYPE_UNSIGNED);
|
||||
eval_state->add_address = value.value.u32;
|
||||
continue;
|
||||
|
||||
case RC_CONDITION_MEASURED:
|
||||
if (condition->required_hits == 0) {
|
||||
if (condition->required_hits == 0 && can_measure) {
|
||||
/* Measured condition without a hit target measures the value of the left operand */
|
||||
measured_value = rc_evaluate_condition_value(condition, eval_state) + eval_state->add_value;
|
||||
rc_evaluate_condition_value(&measured_value, condition, eval_state);
|
||||
rc_typed_value_add(&measured_value, &eval_state->add_value);
|
||||
}
|
||||
break;
|
||||
|
||||
@ -211,7 +235,7 @@ static int rc_test_condset_internal(rc_condset_t* self, int processing_pause, rc
|
||||
|
||||
/* STEP 2: evaluate the current condition */
|
||||
condition->is_true = (char)rc_test_condition(condition, eval_state);
|
||||
eval_state->add_value = 0;
|
||||
eval_state->add_value.type = RC_VALUE_TYPE_NONE;
|
||||
eval_state->add_address = 0;
|
||||
|
||||
/* apply logic flags and reset them for the next condition */
|
||||
@ -345,13 +369,19 @@ static int rc_test_condset_internal(rc_condset_t* self, int processing_pause, rc
|
||||
if (condition->required_hits != 0) {
|
||||
/* if there's a hit target, capture the current hits for recording Measured value later */
|
||||
measured_from_hits = 1;
|
||||
measured_value = total_hits;
|
||||
if (can_measure) {
|
||||
measured_value.value.u32 = total_hits;
|
||||
measured_value.type = RC_VALUE_TYPE_UNSIGNED;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case RC_CONDITION_MEASURED_IF:
|
||||
if (!cond_valid)
|
||||
if (!cond_valid) {
|
||||
measured_value.value.u32 = 0;
|
||||
measured_value.type = RC_VALUE_TYPE_UNSIGNED;
|
||||
can_measure = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case RC_CONDITION_TRIGGER:
|
||||
@ -368,10 +398,13 @@ static int rc_test_condset_internal(rc_condset_t* self, int processing_pause, rc
|
||||
set_valid &= cond_valid;
|
||||
}
|
||||
|
||||
/* if not suppressed, update the measured value */
|
||||
if (measured_value > eval_state->measured_value && can_measure) {
|
||||
eval_state->measured_value = measured_value;
|
||||
eval_state->measured_from_hits = (char)measured_from_hits;
|
||||
if (measured_value.type != RC_VALUE_TYPE_NONE) {
|
||||
/* if no previous Measured value was captured, or the new one is greater, keep the new one */
|
||||
if (eval_state->measured_value.type == RC_VALUE_TYPE_NONE ||
|
||||
rc_typed_value_compare(&measured_value, &eval_state->measured_value, RC_OPERATOR_GT)) {
|
||||
memcpy(&eval_state->measured_value, &measured_value, sizeof(measured_value));
|
||||
eval_state->measured_from_hits = (char)measured_from_hits;
|
||||
}
|
||||
}
|
||||
|
||||
return set_valid;
|
||||
|
33
deps/rcheevos/src/rcheevos/consoleinfo.c
vendored
33
deps/rcheevos/src/rcheevos/consoleinfo.c
vendored
@ -96,6 +96,9 @@ const char* rc_console_name(int console_id)
|
||||
case RC_CONSOLE_MEGA_DRIVE:
|
||||
return "Sega Genesis";
|
||||
|
||||
case RC_CONSOLE_MEGADUCK:
|
||||
return "Mega Duck";
|
||||
|
||||
case RC_CONSOLE_MS_DOS:
|
||||
return "MS-DOS";
|
||||
|
||||
@ -126,6 +129,9 @@ const char* rc_console_name(int console_id)
|
||||
case RC_CONSOLE_ORIC:
|
||||
return "Oric";
|
||||
|
||||
case RC_CONSOLE_PC6000:
|
||||
return "PC-6000";
|
||||
|
||||
case RC_CONSOLE_PC8800:
|
||||
return "PC-8000/8800";
|
||||
|
||||
@ -155,6 +161,9 @@ const char* rc_console_name(int console_id)
|
||||
|
||||
case RC_CONSOLE_SEGA_CD:
|
||||
return "Sega CD";
|
||||
|
||||
case RC_CONSOLE_PICO:
|
||||
return "Sega Pico";
|
||||
|
||||
case RC_CONSOLE_SATURN:
|
||||
return "Sega Saturn";
|
||||
@ -204,6 +213,9 @@ const char* rc_console_name(int console_id)
|
||||
case RC_CONSOLE_XBOX:
|
||||
return "XBOX";
|
||||
|
||||
case RC_CONSOLE_ZEEBO:
|
||||
return "Zeebo";
|
||||
|
||||
case RC_CONSOLE_ZX81:
|
||||
return "ZX-81";
|
||||
|
||||
@ -280,6 +292,13 @@ static const rc_memory_region_t _rc_memory_regions_colecovision[] = {
|
||||
};
|
||||
static const rc_memory_regions_t rc_memory_regions_colecovision = { _rc_memory_regions_colecovision, 1 };
|
||||
|
||||
/* ===== Dreamcast ===== */
|
||||
/* http://archiv.sega-dc.de/munkeechuff/hardware/Memory.html */
|
||||
static const rc_memory_region_t _rc_memory_regions_dreamcast[] = {
|
||||
{ 0x00000000U, 0x00FFFFFFU, 0x0C000000U, RC_MEMORY_TYPE_SYSTEM_RAM, "System RAM" }
|
||||
};
|
||||
static const rc_memory_regions_t rc_memory_regions_dreamcast = { _rc_memory_regions_dreamcast, 1 };
|
||||
|
||||
/* ===== GameBoy / GameBoy Color ===== */
|
||||
static const rc_memory_region_t _rc_memory_regions_gameboy[] = {
|
||||
{ 0x000000U, 0x0000FFU, 0x000000U, RC_MEMORY_TYPE_HARDWARE_CONTROLLER, "Interrupt vector" },
|
||||
@ -506,6 +525,14 @@ static const rc_memory_region_t _rc_memory_regions_playstation2[] = {
|
||||
};
|
||||
static const rc_memory_regions_t rc_memory_regions_playstation2 = { _rc_memory_regions_playstation2, 2 };
|
||||
|
||||
/* ===== PlayStation Portable ===== */
|
||||
/* https://github.com/uofw/upspd/wiki/Memory-map */
|
||||
static const rc_memory_region_t _rc_memory_regions_psp[] = {
|
||||
{ 0x00000000U, 0x007FFFFFU, 0x08000000U, RC_MEMORY_TYPE_SYSTEM_RAM, "Kernel RAM" },
|
||||
{ 0x00800000U, 0x01FFFFFFU, 0x08800000U, RC_MEMORY_TYPE_SYSTEM_RAM, "System RAM" },
|
||||
};
|
||||
static const rc_memory_regions_t rc_memory_regions_psp = { _rc_memory_regions_psp, 2 };
|
||||
|
||||
/* ===== Pokemon Mini ===== */
|
||||
/* https://www.pokemon-mini.net/documentation/memory-map/ */
|
||||
static const rc_memory_region_t _rc_memory_regions_pokemini[] = {
|
||||
@ -656,6 +683,9 @@ const rc_memory_regions_t* rc_console_memory_regions(int console_id)
|
||||
case RC_CONSOLE_COLECOVISION:
|
||||
return &rc_memory_regions_colecovision;
|
||||
|
||||
case RC_CONSOLE_DREAMCAST:
|
||||
return &rc_memory_regions_dreamcast;
|
||||
|
||||
case RC_CONSOLE_GAMEBOY:
|
||||
return &rc_memory_regions_gameboy;
|
||||
|
||||
@ -716,6 +746,9 @@ const rc_memory_regions_t* rc_console_memory_regions(int console_id)
|
||||
case RC_CONSOLE_PLAYSTATION_2:
|
||||
return &rc_memory_regions_playstation2;
|
||||
|
||||
case RC_CONSOLE_PSP:
|
||||
return &rc_memory_regions_psp;
|
||||
|
||||
case RC_CONSOLE_POKEMON_MINI:
|
||||
return &rc_memory_regions_pokemini;
|
||||
|
||||
|
79
deps/rcheevos/src/rcheevos/format.c
vendored
79
deps/rcheevos/src/rcheevos/format.c
vendored
@ -11,6 +11,9 @@ int rc_parse_format(const char* format_str) {
|
||||
if (!strcmp(format_str, "RAMES")) {
|
||||
return RC_FORMAT_FRAMES;
|
||||
}
|
||||
if (!strncmp(format_str, "LOAT", 4) && format_str[4] >= '1' && format_str[4] <= '6' && format_str[5] == '\0') {
|
||||
return RC_FORMAT_FLOAT1 + (format_str[4] - '1');
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
@ -18,7 +21,7 @@ int rc_parse_format(const char* format_str) {
|
||||
if (!strcmp(format_str, "IME")) {
|
||||
return RC_FORMAT_FRAMES;
|
||||
}
|
||||
else if (!strcmp(format_str, "IMESECS")) {
|
||||
if (!strcmp(format_str, "IMESECS")) {
|
||||
return RC_FORMAT_SECONDS;
|
||||
}
|
||||
|
||||
@ -116,40 +119,88 @@ static int rc_format_value_centiseconds(char* buffer, int size, unsigned centise
|
||||
return chars;
|
||||
}
|
||||
|
||||
int rc_format_value(char* buffer, int size, int value, int format) {
|
||||
int rc_format_typed_value(char* buffer, int size, const rc_typed_value_t* value, int format) {
|
||||
int chars;
|
||||
rc_typed_value_t converted_value;
|
||||
|
||||
memcpy(&converted_value, value, sizeof(converted_value));
|
||||
|
||||
switch (format) {
|
||||
case RC_FORMAT_FRAMES:
|
||||
/* 60 frames per second = 100 centiseconds / 60 frames; multiply frames by 100 / 60 */
|
||||
chars = rc_format_value_centiseconds(buffer, size, value * 10 / 6);
|
||||
default:
|
||||
case RC_FORMAT_VALUE:
|
||||
rc_typed_value_convert(&converted_value, RC_VALUE_TYPE_SIGNED);
|
||||
chars = snprintf(buffer, size, "%d", converted_value.value.i32);
|
||||
break;
|
||||
|
||||
case RC_FORMAT_SECONDS:
|
||||
chars = rc_format_value_seconds(buffer, size, value);
|
||||
case RC_FORMAT_FRAMES:
|
||||
/* 60 frames per second = 100 centiseconds / 60 frames; multiply frames by 100 / 60 */
|
||||
rc_typed_value_convert(&converted_value, RC_VALUE_TYPE_UNSIGNED);
|
||||
chars = rc_format_value_centiseconds(buffer, size, converted_value.value.u32 * 10 / 6);
|
||||
break;
|
||||
|
||||
case RC_FORMAT_CENTISECS:
|
||||
chars = rc_format_value_centiseconds(buffer, size, value);
|
||||
rc_typed_value_convert(&converted_value, RC_VALUE_TYPE_UNSIGNED);
|
||||
chars = rc_format_value_centiseconds(buffer, size, converted_value.value.u32);
|
||||
break;
|
||||
|
||||
case RC_FORMAT_SECONDS:
|
||||
rc_typed_value_convert(&converted_value, RC_VALUE_TYPE_UNSIGNED);
|
||||
chars = rc_format_value_seconds(buffer, size, converted_value.value.u32);
|
||||
break;
|
||||
|
||||
case RC_FORMAT_SECONDS_AS_MINUTES:
|
||||
chars = rc_format_value_minutes(buffer, size, value / 60);
|
||||
rc_typed_value_convert(&converted_value, RC_VALUE_TYPE_UNSIGNED);
|
||||
chars = rc_format_value_minutes(buffer, size, converted_value.value.u32 / 60);
|
||||
break;
|
||||
|
||||
case RC_FORMAT_MINUTES:
|
||||
chars = rc_format_value_minutes(buffer, size, value);
|
||||
rc_typed_value_convert(&converted_value, RC_VALUE_TYPE_UNSIGNED);
|
||||
chars = rc_format_value_minutes(buffer, size, converted_value.value.u32);
|
||||
break;
|
||||
|
||||
case RC_FORMAT_SCORE:
|
||||
chars = snprintf(buffer, size, "%06d", value);
|
||||
rc_typed_value_convert(&converted_value, RC_VALUE_TYPE_SIGNED);
|
||||
chars = snprintf(buffer, size, "%06d", converted_value.value.i32);
|
||||
break;
|
||||
|
||||
default:
|
||||
case RC_FORMAT_VALUE:
|
||||
chars = snprintf(buffer, size, "%d", value);
|
||||
case RC_FORMAT_FLOAT1:
|
||||
rc_typed_value_convert(&converted_value, RC_VALUE_TYPE_FLOAT);
|
||||
chars = snprintf(buffer, size, "%.1f", converted_value.value.f32);
|
||||
break;
|
||||
|
||||
case RC_FORMAT_FLOAT2:
|
||||
rc_typed_value_convert(&converted_value, RC_VALUE_TYPE_FLOAT);
|
||||
chars = snprintf(buffer, size, "%.2f", converted_value.value.f32);
|
||||
break;
|
||||
|
||||
case RC_FORMAT_FLOAT3:
|
||||
rc_typed_value_convert(&converted_value, RC_VALUE_TYPE_FLOAT);
|
||||
chars = snprintf(buffer, size, "%.3f", converted_value.value.f32);
|
||||
break;
|
||||
|
||||
case RC_FORMAT_FLOAT4:
|
||||
rc_typed_value_convert(&converted_value, RC_VALUE_TYPE_FLOAT);
|
||||
chars = snprintf(buffer, size, "%.4f", converted_value.value.f32);
|
||||
break;
|
||||
|
||||
case RC_FORMAT_FLOAT5:
|
||||
rc_typed_value_convert(&converted_value, RC_VALUE_TYPE_FLOAT);
|
||||
chars = snprintf(buffer, size, "%.5f", converted_value.value.f32);
|
||||
break;
|
||||
|
||||
case RC_FORMAT_FLOAT6:
|
||||
rc_typed_value_convert(&converted_value, RC_VALUE_TYPE_FLOAT);
|
||||
chars = snprintf(buffer, size, "%.6f", converted_value.value.f32);
|
||||
break;
|
||||
}
|
||||
|
||||
return chars;
|
||||
}
|
||||
|
||||
int rc_format_value(char* buffer, int size, int value, int format) {
|
||||
rc_typed_value_t typed_value;
|
||||
|
||||
typed_value.value.i32 = value;
|
||||
typed_value.type = RC_VALUE_TYPE_SIGNED;
|
||||
return rc_format_typed_value(buffer, size, &typed_value, format);
|
||||
}
|
||||
|
54
deps/rcheevos/src/rcheevos/lboard.c
vendored
54
deps/rcheevos/src/rcheevos/lboard.c
vendored
@ -25,10 +25,12 @@ void rc_parse_lboard_internal(rc_lboard_t* self, const char* memaddr, rc_parse_s
|
||||
return;
|
||||
}
|
||||
|
||||
found |= RC_LBOARD_START;
|
||||
memaddr += 4;
|
||||
rc_parse_trigger_internal(&self->start, &memaddr, parse);
|
||||
self->start.memrefs = 0;
|
||||
if (*memaddr && *memaddr != ':') {
|
||||
found |= RC_LBOARD_START;
|
||||
rc_parse_trigger_internal(&self->start, &memaddr, parse);
|
||||
self->start.memrefs = 0;
|
||||
}
|
||||
}
|
||||
else if ((memaddr[0] == 'c' || memaddr[0] == 'C') &&
|
||||
(memaddr[1] == 'a' || memaddr[1] == 'A') &&
|
||||
@ -38,10 +40,12 @@ void rc_parse_lboard_internal(rc_lboard_t* self, const char* memaddr, rc_parse_s
|
||||
return;
|
||||
}
|
||||
|
||||
found |= RC_LBOARD_CANCEL;
|
||||
memaddr += 4;
|
||||
rc_parse_trigger_internal(&self->cancel, &memaddr, parse);
|
||||
self->cancel.memrefs = 0;
|
||||
if (*memaddr && *memaddr != ':') {
|
||||
found |= RC_LBOARD_CANCEL;
|
||||
rc_parse_trigger_internal(&self->cancel, &memaddr, parse);
|
||||
self->cancel.memrefs = 0;
|
||||
}
|
||||
}
|
||||
else if ((memaddr[0] == 's' || memaddr[0] == 'S') &&
|
||||
(memaddr[1] == 'u' || memaddr[1] == 'U') &&
|
||||
@ -51,10 +55,12 @@ void rc_parse_lboard_internal(rc_lboard_t* self, const char* memaddr, rc_parse_s
|
||||
return;
|
||||
}
|
||||
|
||||
found |= RC_LBOARD_SUBMIT;
|
||||
memaddr += 4;
|
||||
rc_parse_trigger_internal(&self->submit, &memaddr, parse);
|
||||
self->submit.memrefs = 0;
|
||||
if (*memaddr && *memaddr != ':') {
|
||||
found |= RC_LBOARD_SUBMIT;
|
||||
rc_parse_trigger_internal(&self->submit, &memaddr, parse);
|
||||
self->submit.memrefs = 0;
|
||||
}
|
||||
}
|
||||
else if ((memaddr[0] == 'v' || memaddr[0] == 'V') &&
|
||||
(memaddr[1] == 'a' || memaddr[1] == 'A') &&
|
||||
@ -64,10 +70,12 @@ void rc_parse_lboard_internal(rc_lboard_t* self, const char* memaddr, rc_parse_s
|
||||
return;
|
||||
}
|
||||
|
||||
found |= RC_LBOARD_VALUE;
|
||||
memaddr += 4;
|
||||
rc_parse_value_internal(&self->value, &memaddr, parse);
|
||||
self->value.memrefs = 0;
|
||||
if (*memaddr && *memaddr != ':') {
|
||||
found |= RC_LBOARD_VALUE;
|
||||
rc_parse_value_internal(&self->value, &memaddr, parse);
|
||||
self->value.memrefs = 0;
|
||||
}
|
||||
}
|
||||
else if ((memaddr[0] == 'p' || memaddr[0] == 'P') &&
|
||||
(memaddr[1] == 'r' || memaddr[1] == 'R') &&
|
||||
@ -77,12 +85,14 @@ void rc_parse_lboard_internal(rc_lboard_t* self, const char* memaddr, rc_parse_s
|
||||
return;
|
||||
}
|
||||
|
||||
found |= RC_LBOARD_PROGRESS;
|
||||
memaddr += 4;
|
||||
if (*memaddr && *memaddr != ':') {
|
||||
found |= RC_LBOARD_PROGRESS;
|
||||
|
||||
self->progress = RC_ALLOC(rc_value_t, parse);
|
||||
rc_parse_value_internal(self->progress, &memaddr, parse);
|
||||
self->progress->memrefs = 0;
|
||||
self->progress = RC_ALLOC(rc_value_t, parse);
|
||||
rc_parse_value_internal(self->progress, &memaddr, parse);
|
||||
self->progress->memrefs = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* encountered an error parsing one of the parts */
|
||||
@ -239,6 +249,18 @@ int rc_evaluate_lboard(rc_lboard_t* self, int* value, rc_peek_t peek, void* peek
|
||||
return self->state;
|
||||
}
|
||||
|
||||
int rc_lboard_state_active(int state) {
|
||||
switch (state)
|
||||
{
|
||||
case RC_LBOARD_STATE_DISABLED:
|
||||
case RC_LBOARD_STATE_INACTIVE:
|
||||
return 0;
|
||||
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void rc_reset_lboard(rc_lboard_t* self) {
|
||||
self->state = RC_LBOARD_STATE_WAITING;
|
||||
|
||||
|
287
deps/rcheevos/src/rcheevos/memref.c
vendored
287
deps/rcheevos/src/rcheevos/memref.c
vendored
@ -2,6 +2,7 @@
|
||||
|
||||
#include <stdlib.h> /* malloc/realloc */
|
||||
#include <string.h> /* memcpy */
|
||||
#include <math.h> /* INFINITY/NAN */
|
||||
|
||||
#define MEMREF_PLACEHOLDER_ADDRESS 0xFFFFFFFF
|
||||
|
||||
@ -45,50 +46,62 @@ int rc_parse_memref(const char** memaddr, char* size, unsigned* address) {
|
||||
char* end;
|
||||
unsigned long value;
|
||||
|
||||
if (*aux++ != '0')
|
||||
return RC_INVALID_MEMORY_OPERAND;
|
||||
|
||||
if (*aux != 'x' && *aux != 'X')
|
||||
return RC_INVALID_MEMORY_OPERAND;
|
||||
aux++;
|
||||
|
||||
switch (*aux++) {
|
||||
/* ordered by estimated frequency in case compiler doesn't build a jump table */
|
||||
case 'h': case 'H': *size = RC_MEMSIZE_8_BITS; break;
|
||||
case ' ': *size = RC_MEMSIZE_16_BITS; break;
|
||||
case 'x': case 'X': *size = RC_MEMSIZE_32_BITS; break;
|
||||
|
||||
case 'm': case 'M': *size = RC_MEMSIZE_BIT_0; break;
|
||||
case 'n': case 'N': *size = RC_MEMSIZE_BIT_1; break;
|
||||
case 'o': case 'O': *size = RC_MEMSIZE_BIT_2; break;
|
||||
case 'p': case 'P': *size = RC_MEMSIZE_BIT_3; break;
|
||||
case 'q': case 'Q': *size = RC_MEMSIZE_BIT_4; break;
|
||||
case 'r': case 'R': *size = RC_MEMSIZE_BIT_5; break;
|
||||
case 's': case 'S': *size = RC_MEMSIZE_BIT_6; break;
|
||||
case 't': case 'T': *size = RC_MEMSIZE_BIT_7; break;
|
||||
case 'l': case 'L': *size = RC_MEMSIZE_LOW; break;
|
||||
case 'u': case 'U': *size = RC_MEMSIZE_HIGH; break;
|
||||
case 'k': case 'K': *size = RC_MEMSIZE_BITCOUNT; break;
|
||||
case 'w': case 'W': *size = RC_MEMSIZE_24_BITS; break;
|
||||
case 'g': case 'G': *size = RC_MEMSIZE_32_BITS_BE; break;
|
||||
case 'i': case 'I': *size = RC_MEMSIZE_16_BITS_BE; break;
|
||||
case 'j': case 'J': *size = RC_MEMSIZE_24_BITS_BE; break;
|
||||
|
||||
/* case 'v': case 'V': */
|
||||
/* case 'y': case 'Y': 64 bit? */
|
||||
/* case 'z': case 'Z': 128 bit? */
|
||||
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
||||
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
||||
/* legacy support - addresses without a size prefix are assumed to be 16-bit */
|
||||
aux--;
|
||||
*size = RC_MEMSIZE_16_BITS;
|
||||
break;
|
||||
|
||||
default:
|
||||
if (aux[0] == '0') {
|
||||
if (aux[1] != 'x' && aux[1] != 'X')
|
||||
return RC_INVALID_MEMORY_OPERAND;
|
||||
|
||||
aux += 2;
|
||||
switch (*aux++) {
|
||||
/* ordered by estimated frequency in case compiler doesn't build a jump table */
|
||||
case 'h': case 'H': *size = RC_MEMSIZE_8_BITS; break;
|
||||
case ' ': *size = RC_MEMSIZE_16_BITS; break;
|
||||
case 'x': case 'X': *size = RC_MEMSIZE_32_BITS; break;
|
||||
|
||||
case 'm': case 'M': *size = RC_MEMSIZE_BIT_0; break;
|
||||
case 'n': case 'N': *size = RC_MEMSIZE_BIT_1; break;
|
||||
case 'o': case 'O': *size = RC_MEMSIZE_BIT_2; break;
|
||||
case 'p': case 'P': *size = RC_MEMSIZE_BIT_3; break;
|
||||
case 'q': case 'Q': *size = RC_MEMSIZE_BIT_4; break;
|
||||
case 'r': case 'R': *size = RC_MEMSIZE_BIT_5; break;
|
||||
case 's': case 'S': *size = RC_MEMSIZE_BIT_6; break;
|
||||
case 't': case 'T': *size = RC_MEMSIZE_BIT_7; break;
|
||||
case 'l': case 'L': *size = RC_MEMSIZE_LOW; break;
|
||||
case 'u': case 'U': *size = RC_MEMSIZE_HIGH; break;
|
||||
case 'k': case 'K': *size = RC_MEMSIZE_BITCOUNT; break;
|
||||
case 'w': case 'W': *size = RC_MEMSIZE_24_BITS; break;
|
||||
case 'g': case 'G': *size = RC_MEMSIZE_32_BITS_BE; break;
|
||||
case 'i': case 'I': *size = RC_MEMSIZE_16_BITS_BE; break;
|
||||
case 'j': case 'J': *size = RC_MEMSIZE_24_BITS_BE; break;
|
||||
|
||||
/* case 'v': case 'V': */
|
||||
/* case 'y': case 'Y': 64 bit? */
|
||||
/* case 'z': case 'Z': 128 bit? */
|
||||
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
||||
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
||||
/* legacy support - addresses without a size prefix are assumed to be 16-bit */
|
||||
aux--;
|
||||
*size = RC_MEMSIZE_16_BITS;
|
||||
break;
|
||||
|
||||
default:
|
||||
return RC_INVALID_MEMORY_OPERAND;
|
||||
}
|
||||
}
|
||||
else if (aux[0] == 'f' || aux[0] == 'F') {
|
||||
++aux;
|
||||
switch (*aux++) {
|
||||
case 'f': case 'F': *size = RC_MEMSIZE_FLOAT; break;
|
||||
case 'm': case 'M': *size = RC_MEMSIZE_MBF32; break;
|
||||
|
||||
default:
|
||||
return RC_INVALID_FP_OPERAND;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return RC_INVALID_MEMORY_OPERAND;
|
||||
}
|
||||
|
||||
value = strtoul(aux, &end, 16);
|
||||
@ -104,94 +117,212 @@ int rc_parse_memref(const char** memaddr, char* size, unsigned* address) {
|
||||
return RC_OK;
|
||||
}
|
||||
|
||||
static float rc_build_float(unsigned mantissa_bits, int exponent, int sign) {
|
||||
/* 32-bit float has a 23-bit mantissa and 8-bit exponent */
|
||||
const unsigned mantissa = mantissa_bits | 0x800000;
|
||||
double dbl = ((double)mantissa) / ((double)0x800000);
|
||||
|
||||
if (exponent > 127) {
|
||||
/* exponent above 127 is a special number */
|
||||
if (mantissa_bits == 0) {
|
||||
/* infinity */
|
||||
#ifdef INFINITY /* INFINITY and NAN #defines require C99 */
|
||||
dbl = INFINITY;
|
||||
#else
|
||||
dbl = -log(0.0);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
/* NaN */
|
||||
#ifdef NAN
|
||||
dbl = NAN;
|
||||
#else
|
||||
dbl = -sqrt(-1);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else if (exponent > 0) {
|
||||
/* exponent from 1 to 127 is a number greater than 1 */
|
||||
while (exponent > 30) {
|
||||
dbl *= (double)(1 << 30);
|
||||
exponent -= 30;
|
||||
}
|
||||
dbl *= (double)((long long)1 << exponent);
|
||||
}
|
||||
else if (exponent < 0) {
|
||||
/* exponent from -1 to -127 is a number less than 1 */
|
||||
exponent = -exponent;
|
||||
while (exponent > 30) {
|
||||
dbl /= (double)(1 << 30);
|
||||
exponent -= 30;
|
||||
}
|
||||
dbl /= (double)((long long)1 << exponent);
|
||||
}
|
||||
else {
|
||||
/* exponent of 0 requires no adjustment */
|
||||
}
|
||||
|
||||
return (sign) ? (float)-dbl : (float)dbl;
|
||||
}
|
||||
|
||||
static void rc_transform_memref_float(rc_typed_value_t* value) {
|
||||
/* decodes an IEEE 754 float */
|
||||
const unsigned mantissa = (value->value.u32 & 0x7FFFFF);
|
||||
const int exponent = (int)((value->value.u32 >> 23) & 0xFF) - 127;
|
||||
const int sign = (value->value.u32 & 0x80000000);
|
||||
|
||||
if (mantissa == 0 && exponent == -127)
|
||||
value->value.f32 = (sign) ? -0.0f : 0.0f;
|
||||
else
|
||||
value->value.f32 = rc_build_float(mantissa, exponent, sign);
|
||||
|
||||
value->type = RC_VALUE_TYPE_FLOAT;
|
||||
}
|
||||
|
||||
static void rc_transform_memref_mbf32(rc_typed_value_t* value) {
|
||||
/* decodes a Microsoft Binary Format float */
|
||||
/* NOTE: 32-bit MBF is stored in memory as big endian (at least for Apple II) */
|
||||
const unsigned mantissa = ((value->value.u32 & 0xFF000000) >> 24) |
|
||||
((value->value.u32 & 0x00FF0000) >> 8) |
|
||||
((value->value.u32 & 0x00007F00) << 8);
|
||||
const int exponent = (int)(value->value.u32 & 0xFF) - 129;
|
||||
const int sign = (value->value.u32 & 0x00008000);
|
||||
|
||||
if (mantissa == 0 && exponent == -129)
|
||||
value->value.f32 = (sign) ? -0.0f : 0.0f;
|
||||
else
|
||||
value->value.f32 = rc_build_float(mantissa, exponent, sign);
|
||||
|
||||
value->type = RC_VALUE_TYPE_FLOAT;
|
||||
}
|
||||
|
||||
static const unsigned char rc_bits_set[16] = { 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4 };
|
||||
|
||||
unsigned rc_transform_memref_value(unsigned value, char size) {
|
||||
void rc_transform_memref_value(rc_typed_value_t* value, char size) {
|
||||
/* ASSERT: value->type == RC_VALUE_TYPE_UNSIGNED */
|
||||
switch (size)
|
||||
{
|
||||
case RC_MEMSIZE_8_BITS:
|
||||
value = (value & 0x000000ff);
|
||||
value->value.u32 = (value->value.u32 & 0x000000ff);
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_16_BITS:
|
||||
value = (value & 0x0000ffff);
|
||||
value->value.u32 = (value->value.u32 & 0x0000ffff);
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_24_BITS:
|
||||
value = (value & 0x00ffffff);
|
||||
value->value.u32 = (value->value.u32 & 0x00ffffff);
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_32_BITS:
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_BIT_0:
|
||||
value = (value >> 0) & 1;
|
||||
value->value.u32 = (value->value.u32 >> 0) & 1;
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_BIT_1:
|
||||
value = (value >> 1) & 1;
|
||||
value->value.u32 = (value->value.u32 >> 1) & 1;
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_BIT_2:
|
||||
value = (value >> 2) & 1;
|
||||
value->value.u32 = (value->value.u32 >> 2) & 1;
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_BIT_3:
|
||||
value = (value >> 3) & 1;
|
||||
value->value.u32 = (value->value.u32 >> 3) & 1;
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_BIT_4:
|
||||
value = (value >> 4) & 1;
|
||||
value->value.u32 = (value->value.u32 >> 4) & 1;
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_BIT_5:
|
||||
value = (value >> 5) & 1;
|
||||
value->value.u32 = (value->value.u32 >> 5) & 1;
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_BIT_6:
|
||||
value = (value >> 6) & 1;
|
||||
value->value.u32 = (value->value.u32 >> 6) & 1;
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_BIT_7:
|
||||
value = (value >> 7) & 1;
|
||||
value->value.u32 = (value->value.u32 >> 7) & 1;
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_LOW:
|
||||
value = value & 0x0f;
|
||||
value->value.u32 = value->value.u32 & 0x0f;
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_HIGH:
|
||||
value = (value >> 4) & 0x0f;
|
||||
value->value.u32 = (value->value.u32 >> 4) & 0x0f;
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_BITCOUNT:
|
||||
value = rc_bits_set[(value & 0x0F)]
|
||||
+ rc_bits_set[((value >> 4) & 0x0F)];
|
||||
value->value.u32 = rc_bits_set[(value->value.u32 & 0x0F)]
|
||||
+ rc_bits_set[((value->value.u32 >> 4) & 0x0F)];
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_16_BITS_BE:
|
||||
value = ((value & 0xFF00) >> 8) |
|
||||
((value & 0x00FF) << 8);
|
||||
value->value.u32 = ((value->value.u32 & 0xFF00) >> 8) |
|
||||
((value->value.u32 & 0x00FF) << 8);
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_24_BITS_BE:
|
||||
value = ((value & 0xFF0000) >> 16) |
|
||||
(value & 0x00FF00) |
|
||||
((value & 0x0000FF) << 16);
|
||||
value->value.u32 = ((value->value.u32 & 0xFF0000) >> 16) |
|
||||
(value->value.u32 & 0x00FF00) |
|
||||
((value->value.u32 & 0x0000FF) << 16);
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_32_BITS_BE:
|
||||
value = ((value & 0xFF000000) >> 24) |
|
||||
((value & 0x00FF0000) >> 8) |
|
||||
((value & 0x0000FF00) << 8) |
|
||||
((value & 0x000000FF) << 24);
|
||||
value->value.u32 = ((value->value.u32 & 0xFF000000) >> 24) |
|
||||
((value->value.u32 & 0x00FF0000) >> 8) |
|
||||
((value->value.u32 & 0x0000FF00) << 8) |
|
||||
((value->value.u32 & 0x000000FF) << 24);
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_FLOAT:
|
||||
rc_transform_memref_float(value);
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_MBF32:
|
||||
rc_transform_memref_mbf32(value);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
static const unsigned rc_memref_masks[] = {
|
||||
0x000000ff, /* RC_MEMSIZE_8_BITS */
|
||||
0x0000ffff, /* RC_MEMSIZE_16_BITS */
|
||||
0x00ffffff, /* RC_MEMSIZE_24_BITS */
|
||||
0xffffffff, /* RC_MEMSIZE_32_BITS */
|
||||
0x0000000f, /* RC_MEMSIZE_LOW */
|
||||
0x000000f0, /* RC_MEMSIZE_HIGH */
|
||||
0x00000001, /* RC_MEMSIZE_BIT_0 */
|
||||
0x00000002, /* RC_MEMSIZE_BIT_1 */
|
||||
0x00000004, /* RC_MEMSIZE_BIT_2 */
|
||||
0x00000008, /* RC_MEMSIZE_BIT_3 */
|
||||
0x00000010, /* RC_MEMSIZE_BIT_4 */
|
||||
0x00000020, /* RC_MEMSIZE_BIT_5 */
|
||||
0x00000040, /* RC_MEMSIZE_BIT_6 */
|
||||
0x00000080, /* RC_MEMSIZE_BIT_7 */
|
||||
0x000000ff, /* RC_MEMSIZE_BITCOUNT */
|
||||
0x0000ffff, /* RC_MEMSIZE_16_BITS_BE */
|
||||
0x00ffffff, /* RC_MEMSIZE_24_BITS_BE */
|
||||
0xffffffff, /* RC_MEMSIZE_32_BITS_BE */
|
||||
0xffffffff, /* RC_MEMSIZE_FLOAT */
|
||||
0xffffffff, /* RC_MEMSIZE_MBF32 */
|
||||
0xffffffff /* RC_MEMSIZE_VARIABLE */
|
||||
};
|
||||
|
||||
unsigned rc_memref_mask(char size) {
|
||||
const size_t index = (size_t)size;
|
||||
if (index >= sizeof(rc_memref_masks) / sizeof(rc_memref_masks[0]))
|
||||
return 0xffffffff;
|
||||
|
||||
return rc_memref_masks[index];
|
||||
}
|
||||
|
||||
/* all sizes less than 8-bits (1 byte) are mapped to 8-bits. 24-bit is mapped to 32-bit
|
||||
@ -216,6 +347,8 @@ static const char rc_memref_shared_sizes[] = {
|
||||
RC_MEMSIZE_16_BITS, /* RC_MEMSIZE_16_BITS_BE */
|
||||
RC_MEMSIZE_32_BITS, /* RC_MEMSIZE_24_BITS_BE */
|
||||
RC_MEMSIZE_32_BITS, /* RC_MEMSIZE_32_BITS_BE */
|
||||
RC_MEMSIZE_32_BITS, /* RC_MEMSIZE_FLOAT */
|
||||
RC_MEMSIZE_32_BITS, /* RC_MEMSIZE_MBF32 */
|
||||
RC_MEMSIZE_32_BITS /* RC_MEMSIZE_VARIABLE */
|
||||
};
|
||||
|
||||
@ -228,7 +361,7 @@ char rc_memref_shared_size(char size) {
|
||||
}
|
||||
|
||||
static unsigned rc_peek_value(unsigned address, char size, rc_peek_t peek, void* ud) {
|
||||
unsigned value;
|
||||
rc_typed_value_t value;
|
||||
char shared_size;
|
||||
|
||||
if (!peek)
|
||||
@ -238,25 +371,27 @@ static unsigned rc_peek_value(unsigned address, char size, rc_peek_t peek, void*
|
||||
switch (shared_size)
|
||||
{
|
||||
case RC_MEMSIZE_8_BITS:
|
||||
value = peek(address, 1, ud);
|
||||
value.value.u32 = peek(address, 1, ud);
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_16_BITS:
|
||||
value = peek(address, 2, ud);
|
||||
value.value.u32 = peek(address, 2, ud);
|
||||
break;
|
||||
|
||||
case RC_MEMSIZE_32_BITS:
|
||||
value = peek(address, 4, ud);
|
||||
value.value.u32 = peek(address, 4, ud);
|
||||
break;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (shared_size != size)
|
||||
value = rc_transform_memref_value(value, size);
|
||||
if (shared_size != size) {
|
||||
value.type = RC_VALUE_TYPE_UNSIGNED;
|
||||
rc_transform_memref_value(&value, size);
|
||||
}
|
||||
|
||||
return value;
|
||||
return value.value.u32;
|
||||
}
|
||||
|
||||
void rc_update_memref_value(rc_memref_value_t* memref, unsigned new_value) {
|
||||
|
182
deps/rcheevos/src/rcheevos/operand.c
vendored
182
deps/rcheevos/src/rcheevos/operand.c
vendored
@ -103,6 +103,16 @@ static int rc_parse_operand_memory(rc_operand_t* self, const char** memaddr, rc_
|
||||
return ret;
|
||||
|
||||
size = rc_memref_shared_size(self->size);
|
||||
if (size != self->size && self->type == RC_OPERAND_PRIOR) {
|
||||
/* if the shared size differs from the requested size and it's a prior operation, we
|
||||
* have to check to make sure both sizes use the same mask, or the prior value may be
|
||||
* updated when bits outside the mask are modified, which would make it look like the
|
||||
* current value once the mask is applied. if the mask differs, create a new
|
||||
* non-shared record for tracking the prior data. */
|
||||
if (rc_memref_mask(size) != rc_memref_mask(self->size))
|
||||
size = self->size;
|
||||
}
|
||||
|
||||
self->value.memref = rc_alloc_memref(parse, address, size, (char)is_indirect);
|
||||
if (parse->offset < 0)
|
||||
return parse->offset;
|
||||
@ -142,6 +152,14 @@ int rc_parse_operand(rc_operand_t* self, const char** memaddr, int is_indirect,
|
||||
break;
|
||||
|
||||
case 'f': case 'F': /* floating point constant */
|
||||
if (isalpha((unsigned char)aux[1])) {
|
||||
ret = rc_parse_operand_memory(self, &aux, parse, is_indirect);
|
||||
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
break;
|
||||
}
|
||||
allow_decimal = 1;
|
||||
/* fall through */
|
||||
case 'v': case 'V': /* signed integer constant */
|
||||
@ -183,28 +201,33 @@ int rc_parse_operand(rc_operand_t* self, const char** memaddr, int is_indirect,
|
||||
self->value.dbl = ((double)(-((long)value))) - dbl_fraction;
|
||||
else
|
||||
self->value.dbl = (double)value + dbl_fraction;
|
||||
|
||||
self->type = RC_OPERAND_FP;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
else {
|
||||
if (negative)
|
||||
self->value.dbl = (double)(-((long)value));
|
||||
else
|
||||
self->value.dbl = (double)value;
|
||||
}
|
||||
|
||||
self->type = RC_OPERAND_FP;
|
||||
}
|
||||
else {
|
||||
/* not a floating point value, make sure something was read and advance the read pointer */
|
||||
if (end == aux)
|
||||
return allow_decimal ? RC_INVALID_FP_OPERAND : RC_INVALID_CONST_OPERAND;
|
||||
|
||||
aux = end;
|
||||
|
||||
if (value > 0x7fffffffU)
|
||||
value = 0x7fffffffU;
|
||||
|
||||
self->type = RC_OPERAND_CONST;
|
||||
|
||||
if (negative)
|
||||
self->value.num = (unsigned)(-((long)value));
|
||||
else
|
||||
self->value.num = (unsigned)value;
|
||||
}
|
||||
|
||||
if (value > 0x7fffffffU)
|
||||
value = 0x7fffffffU;
|
||||
|
||||
self->type = RC_OPERAND_CONST;
|
||||
|
||||
if (negative)
|
||||
self->value.num = (unsigned)(-((long)value));
|
||||
else
|
||||
self->value.num = (unsigned)value;
|
||||
|
||||
break;
|
||||
|
||||
case '0':
|
||||
@ -269,7 +292,18 @@ static int rc_luapeek(lua_State* L) {
|
||||
|
||||
#endif /* RC_DISABLE_LUA */
|
||||
|
||||
int rc_operand_is_memref(rc_operand_t* self) {
|
||||
int rc_operand_is_float_memref(const rc_operand_t* self) {
|
||||
switch (self->size) {
|
||||
case RC_MEMSIZE_FLOAT:
|
||||
case RC_MEMSIZE_MBF32:
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int rc_operand_is_memref(const rc_operand_t* self) {
|
||||
switch (self->type) {
|
||||
case RC_OPERAND_CONST:
|
||||
case RC_OPERAND_FP:
|
||||
@ -281,60 +315,7 @@ int rc_operand_is_memref(rc_operand_t* self) {
|
||||
}
|
||||
}
|
||||
|
||||
unsigned rc_evaluate_operand(rc_operand_t* self, rc_eval_state_t* eval_state) {
|
||||
#ifndef RC_DISABLE_LUA
|
||||
rc_luapeek_t luapeek;
|
||||
#endif /* RC_DISABLE_LUA */
|
||||
|
||||
unsigned value;
|
||||
|
||||
/* step 1: read memory */
|
||||
switch (self->type) {
|
||||
case RC_OPERAND_CONST:
|
||||
return self->value.num;
|
||||
|
||||
case RC_OPERAND_FP:
|
||||
/* This is handled by rc_evaluate_condition_value. */
|
||||
return 0;
|
||||
|
||||
case RC_OPERAND_LUA:
|
||||
value = 0;
|
||||
|
||||
#ifndef RC_DISABLE_LUA
|
||||
if (eval_state->L != 0) {
|
||||
lua_rawgeti(eval_state->L, LUA_REGISTRYINDEX, self->value.luafunc);
|
||||
lua_pushcfunction(eval_state->L, rc_luapeek);
|
||||
|
||||
luapeek.peek = eval_state->peek;
|
||||
luapeek.ud = eval_state->peek_userdata;
|
||||
|
||||
lua_pushlightuserdata(eval_state->L, &luapeek);
|
||||
|
||||
if (lua_pcall(eval_state->L, 2, 1, 0) == LUA_OK) {
|
||||
if (lua_isboolean(eval_state->L, -1)) {
|
||||
value = lua_toboolean(eval_state->L, -1);
|
||||
}
|
||||
else {
|
||||
value = (unsigned)lua_tonumber(eval_state->L, -1);
|
||||
}
|
||||
}
|
||||
|
||||
lua_pop(eval_state->L, 1);
|
||||
}
|
||||
|
||||
#endif /* RC_DISABLE_LUA */
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
value = rc_get_memref_value(self->value.memref, self->type, eval_state);
|
||||
break;
|
||||
}
|
||||
|
||||
/* step 2: mask off appropriate bits */
|
||||
value = rc_transform_memref_value(value, self->size);
|
||||
|
||||
/* step 3: apply logic */
|
||||
unsigned rc_transform_operand_value(unsigned value, const rc_operand_t* self) {
|
||||
switch (self->type)
|
||||
{
|
||||
case RC_OPERAND_BCD:
|
||||
@ -421,3 +402,64 @@ unsigned rc_evaluate_operand(rc_operand_t* self, rc_eval_state_t* eval_state) {
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
void rc_evaluate_operand(rc_typed_value_t* result, rc_operand_t* self, rc_eval_state_t* eval_state) {
|
||||
#ifndef RC_DISABLE_LUA
|
||||
rc_luapeek_t luapeek;
|
||||
#endif /* RC_DISABLE_LUA */
|
||||
|
||||
/* step 1: read memory */
|
||||
switch (self->type) {
|
||||
case RC_OPERAND_CONST:
|
||||
result->type = RC_VALUE_TYPE_UNSIGNED;
|
||||
result->value.u32 = self->value.num;
|
||||
return;
|
||||
|
||||
case RC_OPERAND_FP:
|
||||
result->type = RC_VALUE_TYPE_FLOAT;
|
||||
result->value.f32 = (float)self->value.dbl;
|
||||
return;
|
||||
|
||||
case RC_OPERAND_LUA:
|
||||
result->type = RC_VALUE_TYPE_UNSIGNED;
|
||||
result->value.u32 = 0;
|
||||
|
||||
#ifndef RC_DISABLE_LUA
|
||||
if (eval_state->L != 0) {
|
||||
lua_rawgeti(eval_state->L, LUA_REGISTRYINDEX, self->value.luafunc);
|
||||
lua_pushcfunction(eval_state->L, rc_luapeek);
|
||||
|
||||
luapeek.peek = eval_state->peek;
|
||||
luapeek.ud = eval_state->peek_userdata;
|
||||
|
||||
lua_pushlightuserdata(eval_state->L, &luapeek);
|
||||
|
||||
if (lua_pcall(eval_state->L, 2, 1, 0) == LUA_OK) {
|
||||
if (lua_isboolean(eval_state->L, -1)) {
|
||||
result->value.u32 = (unsigned)lua_toboolean(eval_state->L, -1);
|
||||
}
|
||||
else {
|
||||
result->value.u32 = (unsigned)lua_tonumber(eval_state->L, -1);
|
||||
}
|
||||
}
|
||||
|
||||
lua_pop(eval_state->L, 1);
|
||||
}
|
||||
|
||||
#endif /* RC_DISABLE_LUA */
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
result->type = RC_VALUE_TYPE_UNSIGNED;
|
||||
result->value.u32 = rc_get_memref_value(self->value.memref, self->type, eval_state);
|
||||
break;
|
||||
}
|
||||
|
||||
/* step 2: convert read memory to desired format */
|
||||
rc_transform_memref_value(result, self->size);
|
||||
|
||||
/* step 3: apply logic (BCD/invert) */
|
||||
if (result->type == RC_VALUE_TYPE_UNSIGNED)
|
||||
result->value.u32 = rc_transform_operand_value(result->value.u32, self);
|
||||
}
|
||||
|
42
deps/rcheevos/src/rcheevos/rc_internal.h
vendored
42
deps/rcheevos/src/rcheevos/rc_internal.h
vendored
@ -65,8 +65,26 @@ typedef struct {
|
||||
}
|
||||
rc_scratch_t;
|
||||
|
||||
enum {
|
||||
RC_VALUE_TYPE_NONE,
|
||||
RC_VALUE_TYPE_UNSIGNED,
|
||||
RC_VALUE_TYPE_SIGNED,
|
||||
RC_VALUE_TYPE_FLOAT
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
unsigned add_value; /* AddSource/SubSource */
|
||||
union {
|
||||
unsigned u32;
|
||||
int i32;
|
||||
float f32;
|
||||
} value;
|
||||
|
||||
char type;
|
||||
}
|
||||
rc_typed_value_t;
|
||||
|
||||
typedef struct {
|
||||
rc_typed_value_t add_value;/* AddSource/SubSource */
|
||||
int add_hits; /* AddHits */
|
||||
unsigned add_address; /* AddAddress */
|
||||
|
||||
@ -74,7 +92,7 @@ typedef struct {
|
||||
void* peek_userdata;
|
||||
lua_State* L;
|
||||
|
||||
unsigned measured_value; /* Measured */
|
||||
rc_typed_value_t measured_value; /* Measured */
|
||||
char was_reset; /* ResetIf triggered */
|
||||
char has_hits; /* one of more hit counts is non-zero */
|
||||
char primed; /* true if all non-Trigger conditions are true */
|
||||
@ -118,7 +136,8 @@ void rc_update_memref_values(rc_memref_t* memref, rc_peek_t peek, void* ud);
|
||||
void rc_update_memref_value(rc_memref_value_t* memref, unsigned value);
|
||||
unsigned rc_get_memref_value(rc_memref_t* memref, int operand_type, rc_eval_state_t* eval_state);
|
||||
char rc_memref_shared_size(char size);
|
||||
unsigned rc_transform_memref_value(unsigned value, char size);
|
||||
unsigned rc_memref_mask(char size);
|
||||
void rc_transform_memref_value(rc_typed_value_t* value, char size);
|
||||
|
||||
void rc_parse_trigger_internal(rc_trigger_t* self, const char** memaddr, rc_parse_state_t* parse);
|
||||
int rc_trigger_state_active(int state);
|
||||
@ -129,17 +148,30 @@ void rc_reset_condset(rc_condset_t* self);
|
||||
|
||||
rc_condition_t* rc_parse_condition(const char** memaddr, rc_parse_state_t* parse, int is_indirect);
|
||||
int rc_test_condition(rc_condition_t* self, rc_eval_state_t* eval_state);
|
||||
int rc_evaluate_condition_value(rc_condition_t* self, rc_eval_state_t* eval_state);
|
||||
void rc_evaluate_condition_value(rc_typed_value_t* value, rc_condition_t* self, rc_eval_state_t* eval_state);
|
||||
int rc_condition_is_combining(const rc_condition_t* self);
|
||||
|
||||
int rc_parse_operand(rc_operand_t* self, const char** memaddr, int is_indirect, rc_parse_state_t* parse);
|
||||
unsigned rc_evaluate_operand(rc_operand_t* self, rc_eval_state_t* eval_state);
|
||||
void rc_evaluate_operand(rc_typed_value_t* value, rc_operand_t* self, rc_eval_state_t* eval_state);
|
||||
int rc_operand_is_float_memref(const rc_operand_t* self);
|
||||
|
||||
void rc_parse_value_internal(rc_value_t* self, const char** memaddr, rc_parse_state_t* parse);
|
||||
int rc_evaluate_value_typed(rc_value_t* self, rc_typed_value_t* value, rc_peek_t peek, void* ud, lua_State* L);
|
||||
void rc_reset_value(rc_value_t* self);
|
||||
rc_value_t* rc_alloc_helper_variable(const char* memaddr, int memaddr_len, rc_parse_state_t* parse);
|
||||
void rc_update_variables(rc_value_t* variable, rc_peek_t peek, void* ud, lua_State* L);
|
||||
|
||||
void rc_typed_value_convert(rc_typed_value_t* value, char new_type);
|
||||
void rc_typed_value_add(rc_typed_value_t* value, const rc_typed_value_t* amount);
|
||||
void rc_typed_value_multiply(rc_typed_value_t* value, const rc_typed_value_t* amount);
|
||||
void rc_typed_value_divide(rc_typed_value_t* value, const rc_typed_value_t* amount);
|
||||
int rc_typed_value_compare(const rc_typed_value_t* value1, const rc_typed_value_t* value2, char oper);
|
||||
void rc_typed_value_from_memref_value(rc_typed_value_t* value, const rc_memref_value_t* memref);
|
||||
|
||||
int rc_format_typed_value(char* buffer, int size, const rc_typed_value_t* value, int format);
|
||||
|
||||
void rc_parse_lboard_internal(rc_lboard_t* self, const char* memaddr, rc_parse_state_t* parse);
|
||||
int rc_lboard_state_active(int state);
|
||||
|
||||
void rc_parse_richpresence_internal(rc_richpresence_t* self, const char* script, rc_parse_state_t* parse);
|
||||
|
||||
|
24
deps/rcheevos/src/rcheevos/rc_libretro.c
vendored
24
deps/rcheevos/src/rcheevos/rc_libretro.c
vendored
@ -36,6 +36,11 @@ static const rc_disallowed_setting_t _rc_disallowed_dolphin_settings[] = {
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
static const rc_disallowed_setting_t _rc_disallowed_duckstation_settings[] = {
|
||||
{ "duckstation_CDROM.LoadImagePatches", "true" },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
static const rc_disallowed_setting_t _rc_disallowed_ecwolf_settings[] = {
|
||||
{ "ecwolf-invulnerability", "enabled" },
|
||||
{ NULL, NULL }
|
||||
@ -44,6 +49,8 @@ static const rc_disallowed_setting_t _rc_disallowed_ecwolf_settings[] = {
|
||||
static const rc_disallowed_setting_t _rc_disallowed_fbneo_settings[] = {
|
||||
{ "fbneo-allow-patched-romsets", "enabled" },
|
||||
{ "fbneo-cheat-*", "!,Disabled,0 - Disabled" },
|
||||
{ "fbneo-dipswitch-*", "Universe BIOS*" },
|
||||
{ "fbneo-neogeo-mode", "UNIBIOS" },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
@ -95,6 +102,9 @@ static const rc_disallowed_setting_t _rc_disallowed_smsplus_settings[] = {
|
||||
};
|
||||
|
||||
static const rc_disallowed_setting_t _rc_disallowed_snes9x_settings[] = {
|
||||
{ "snes9x_gfx_clip", "disabled" },
|
||||
{ "snes9x_gfx_transp", "disabled" },
|
||||
{ "snes9x_layer_*", "disabled" },
|
||||
{ "snes9x_region", "pal" },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
@ -107,6 +117,7 @@ static const rc_disallowed_setting_t _rc_disallowed_virtual_jaguar_settings[] =
|
||||
static const rc_disallowed_core_settings_t rc_disallowed_core_settings[] = {
|
||||
{ "bsnes-mercury", _rc_disallowed_bsnes_settings },
|
||||
{ "dolphin-emu", _rc_disallowed_dolphin_settings },
|
||||
{ "DuckStation", _rc_disallowed_duckstation_settings },
|
||||
{ "ecwolf", _rc_disallowed_ecwolf_settings },
|
||||
{ "FCEUmm", _rc_disallowed_fceumm_settings },
|
||||
{ "FinalBurn Neo", _rc_disallowed_fbneo_settings },
|
||||
@ -123,10 +134,11 @@ static const rc_disallowed_core_settings_t rc_disallowed_core_settings[] = {
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
static int rc_libretro_string_equal_nocase(const char* test, const char* value) {
|
||||
while (*test) {
|
||||
if (tolower(*test++) != tolower(*value++))
|
||||
return 0;
|
||||
static int rc_libretro_string_equal_nocase_wildcard(const char* test, const char* value) {
|
||||
char c1, c2;
|
||||
while ((c1 = *test++)) {
|
||||
if (tolower(c1) != tolower(c2 = *value++))
|
||||
return (c2 == '*');
|
||||
}
|
||||
|
||||
return (*value == '\0');
|
||||
@ -151,7 +163,7 @@ static int rc_libretro_match_value(const char* val, const char* match) {
|
||||
char buffer[128];
|
||||
memcpy(buffer, ptr, size);
|
||||
buffer[size] = '\0';
|
||||
if (rc_libretro_string_equal_nocase(buffer, val))
|
||||
if (rc_libretro_string_equal_nocase_wildcard(buffer, val))
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@ -165,7 +177,7 @@ static int rc_libretro_match_value(const char* val, const char* match) {
|
||||
return !rc_libretro_match_value(val, &match[1]);
|
||||
|
||||
/* just a single value, attempt to match it */
|
||||
return rc_libretro_string_equal_nocase(val, match);
|
||||
return rc_libretro_string_equal_nocase_wildcard(val, match);
|
||||
}
|
||||
|
||||
int rc_libretro_is_setting_allowed(const rc_disallowed_setting_t* disallowed_settings, const char* setting, const char* value) {
|
||||
|
218
deps/rcheevos/src/rcheevos/richpresence.c
vendored
218
deps/rcheevos/src/rcheevos/richpresence.c
vendored
@ -8,7 +8,9 @@
|
||||
enum {
|
||||
RC_FORMAT_STRING = 101,
|
||||
RC_FORMAT_LOOKUP = 102,
|
||||
RC_FORMAT_UNKNOWN_MACRO = 103
|
||||
RC_FORMAT_UNKNOWN_MACRO = 103,
|
||||
RC_FORMAT_ASCIICHAR = 104,
|
||||
RC_FORMAT_UNICODECHAR = 105
|
||||
};
|
||||
|
||||
static rc_memref_value_t* rc_alloc_helper_variable_memref_value(const char* memaddr, int memaddr_len, rc_parse_state_t* parse) {
|
||||
@ -73,6 +75,12 @@ static const char* rc_parse_line(const char* line, const char** end, rc_parse_st
|
||||
return nextline;
|
||||
}
|
||||
|
||||
typedef struct rc_richpresence_builtin_macro_t {
|
||||
const char* name;
|
||||
size_t name_len;
|
||||
unsigned short display_type;
|
||||
} rc_richpresence_builtin_macro_t;
|
||||
|
||||
static rc_richpresence_display_t* rc_parse_richpresence_display_internal(const char* line, const char* endline, rc_parse_state_t* parse, rc_richpresence_lookup_t* first_lookup) {
|
||||
rc_richpresence_display_t* self;
|
||||
rc_richpresence_display_part_t* part;
|
||||
@ -132,6 +140,8 @@ static rc_richpresence_display_t* rc_parse_richpresence_display_internal(const c
|
||||
|
||||
if (*ptr == '@') {
|
||||
/* handle macro part */
|
||||
size_t macro_len;
|
||||
|
||||
line = ++ptr;
|
||||
while (ptr < endline && *ptr != '(')
|
||||
++ptr;
|
||||
@ -141,58 +151,81 @@ static rc_richpresence_display_t* rc_parse_richpresence_display_internal(const c
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ptr > line) {
|
||||
/* find the lookup and hook it up */
|
||||
lookup = first_lookup;
|
||||
while (lookup) {
|
||||
if (strncmp(lookup->name, line, ptr - line) == 0 && lookup->name[ptr - line] == '\0') {
|
||||
part = RC_ALLOC(rc_richpresence_display_part_t, parse);
|
||||
*next = part;
|
||||
next = &part->next;
|
||||
macro_len = ptr - line;
|
||||
|
||||
part->text = lookup->name;
|
||||
part->lookup = lookup;
|
||||
part->display_type = lookup->format;
|
||||
part = RC_ALLOC(rc_richpresence_display_part_t, parse);
|
||||
memset(part, 0, sizeof(rc_richpresence_display_part_t));
|
||||
*next = part;
|
||||
next = &part->next;
|
||||
|
||||
in = line;
|
||||
line = ++ptr;
|
||||
while (ptr < endline && *ptr != ')')
|
||||
++ptr;
|
||||
if (*ptr == ')') {
|
||||
part->value = rc_alloc_helper_variable_memref_value(line, (int)(ptr-line), parse);
|
||||
if (parse->offset < 0)
|
||||
return 0;
|
||||
++ptr;
|
||||
}
|
||||
else {
|
||||
/* non-terminated macro, dump the macro and the remaining portion of the line */
|
||||
--in; /* already skipped over @ */
|
||||
part->display_type = RC_FORMAT_STRING;
|
||||
part->text = rc_alloc_str(parse, in, (int)(ptr - in));
|
||||
}
|
||||
part->display_type = RC_FORMAT_UNKNOWN_MACRO;
|
||||
|
||||
/* find the lookup and hook it up */
|
||||
lookup = first_lookup;
|
||||
while (lookup) {
|
||||
if (strncmp(lookup->name, line, macro_len) == 0 && lookup->name[macro_len] == '\0') {
|
||||
part->text = lookup->name;
|
||||
part->lookup = lookup;
|
||||
part->display_type = lookup->format;
|
||||
break;
|
||||
}
|
||||
|
||||
lookup = lookup->next;
|
||||
}
|
||||
|
||||
if (!lookup) {
|
||||
static const rc_richpresence_builtin_macro_t builtin_macros[] = {
|
||||
{"Number", 6, RC_FORMAT_VALUE},
|
||||
{"Score", 5, RC_FORMAT_SCORE},
|
||||
{"Centiseconds", 12, RC_FORMAT_CENTISECS},
|
||||
{"Seconds", 7, RC_FORMAT_SECONDS},
|
||||
{"Minutes", 7, RC_FORMAT_MINUTES},
|
||||
{"SecondsAsMinutes", 16, RC_FORMAT_SECONDS_AS_MINUTES},
|
||||
{"ASCIIChar", 9, RC_FORMAT_ASCIICHAR},
|
||||
{"UnicodeChar", 11, RC_FORMAT_UNICODECHAR},
|
||||
{"Float1", 6, RC_FORMAT_FLOAT1},
|
||||
{"Float2", 6, RC_FORMAT_FLOAT2},
|
||||
{"Float3", 6, RC_FORMAT_FLOAT3},
|
||||
{"Float4", 6, RC_FORMAT_FLOAT4},
|
||||
{"Float5", 6, RC_FORMAT_FLOAT5},
|
||||
{"Float6", 6, RC_FORMAT_FLOAT6},
|
||||
};
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(builtin_macros) / sizeof(builtin_macros[0]); ++i) {
|
||||
if (macro_len == builtin_macros[i].name_len &&
|
||||
memcmp(builtin_macros[i].name, line, builtin_macros[i].name_len) == 0) {
|
||||
part->text = builtin_macros[i].name;
|
||||
part->lookup = NULL;
|
||||
part->display_type = builtin_macros[i].display_type;
|
||||
break;
|
||||
}
|
||||
|
||||
lookup = lookup->next;
|
||||
}
|
||||
}
|
||||
|
||||
if (!lookup) {
|
||||
part = RC_ALLOC(rc_richpresence_display_part_t, parse);
|
||||
memset(part, 0, sizeof(rc_richpresence_display_part_t));
|
||||
*next = part;
|
||||
next = &part->next;
|
||||
/* find the closing parenthesis */
|
||||
in = line;
|
||||
line = ++ptr;
|
||||
while (ptr < endline && *ptr != ')')
|
||||
++ptr;
|
||||
|
||||
/* find the closing parenthesis */
|
||||
while (ptr < endline && *ptr != ')')
|
||||
++ptr;
|
||||
if (*ptr == ')')
|
||||
++ptr;
|
||||
if (*ptr != ')') {
|
||||
/* non-terminated macro, dump the macro and the remaining portion of the line */
|
||||
--in; /* already skipped over @ */
|
||||
part->display_type = RC_FORMAT_STRING;
|
||||
part->text = rc_alloc_str(parse, in, (int)(ptr - in));
|
||||
}
|
||||
else if (part->display_type != RC_FORMAT_UNKNOWN_MACRO) {
|
||||
part->value = rc_alloc_helper_variable_memref_value(line, (int)(ptr - line), parse);
|
||||
if (parse->offset < 0)
|
||||
return 0;
|
||||
|
||||
/* assert: the allocated string is going to be smaller than the memory used for the parameter of the macro */
|
||||
part->display_type = RC_FORMAT_UNKNOWN_MACRO;
|
||||
part->text = rc_alloc_str(parse, line, (int)(ptr - line));
|
||||
}
|
||||
++ptr;
|
||||
}
|
||||
else {
|
||||
/* assert: the allocated string is going to be smaller than the memory used for the parameter of the macro */
|
||||
++ptr;
|
||||
part->text = rc_alloc_str(parse, in, (int)(ptr - in));
|
||||
}
|
||||
}
|
||||
|
||||
@ -623,11 +656,11 @@ void rc_update_richpresence(rc_richpresence_t* richpresence, rc_peek_t peek, voi
|
||||
static int rc_evaluate_richpresence_display(rc_richpresence_display_part_t* part, char* buffer, unsigned buffersize)
|
||||
{
|
||||
rc_richpresence_lookup_item_t* item;
|
||||
rc_typed_value_t value;
|
||||
char tmp[256];
|
||||
char* ptr = buffer;
|
||||
const char* text;
|
||||
size_t chars;
|
||||
unsigned value;
|
||||
|
||||
*ptr = '\0';
|
||||
while (part) {
|
||||
@ -638,14 +671,16 @@ static int rc_evaluate_richpresence_display(rc_richpresence_display_part_t* part
|
||||
break;
|
||||
|
||||
case RC_FORMAT_LOOKUP:
|
||||
value = part->value->value;
|
||||
rc_typed_value_from_memref_value(&value, part->value);
|
||||
rc_typed_value_convert(&value, RC_VALUE_TYPE_UNSIGNED);
|
||||
|
||||
text = part->lookup->default_label;
|
||||
item = part->lookup->root;
|
||||
while (item) {
|
||||
if (value > item->last) {
|
||||
if (value.value.u32 > item->last) {
|
||||
item = item->right;
|
||||
}
|
||||
else if (value < item->first) {
|
||||
else if (value.value.u32 < item->first) {
|
||||
item = item->left;
|
||||
}
|
||||
else {
|
||||
@ -657,14 +692,86 @@ static int rc_evaluate_richpresence_display(rc_richpresence_display_part_t* part
|
||||
chars = strlen(text);
|
||||
break;
|
||||
|
||||
case RC_FORMAT_ASCIICHAR:
|
||||
chars = 0;
|
||||
text = tmp;
|
||||
value.type = RC_VALUE_TYPE_UNSIGNED;
|
||||
|
||||
do {
|
||||
value.value.u32 = part->value->value;
|
||||
if (value.value.u32 == 0) {
|
||||
/* null terminator - skip over remaining character macros */
|
||||
while (part->next && part->next->display_type == RC_FORMAT_ASCIICHAR)
|
||||
part = part->next;
|
||||
break;
|
||||
}
|
||||
|
||||
if (value.value.u32 < 32 || value.value.u32 >= 127)
|
||||
value.value.u32 = '?';
|
||||
|
||||
tmp[chars++] = (char)value.value.u32;
|
||||
if (chars == sizeof(tmp) || !part->next || part->next->display_type != RC_FORMAT_ASCIICHAR)
|
||||
break;
|
||||
|
||||
part = part->next;
|
||||
} while (1);
|
||||
|
||||
tmp[chars] = '\0';
|
||||
break;
|
||||
|
||||
case RC_FORMAT_UNICODECHAR:
|
||||
chars = 0;
|
||||
text = tmp;
|
||||
value.type = RC_VALUE_TYPE_UNSIGNED;
|
||||
|
||||
do {
|
||||
value.value.u32 = part->value->value;
|
||||
if (value.value.u32 == 0) {
|
||||
/* null terminator - skip over remaining character macros */
|
||||
while (part->next && part->next->display_type == RC_FORMAT_UNICODECHAR)
|
||||
part = part->next;
|
||||
break;
|
||||
}
|
||||
|
||||
if (value.value.u32 < 32 || value.value.u32 > 65535)
|
||||
value.value.u32 = 0xFFFD; /* unicode replacement char */
|
||||
|
||||
if (value.value.u32 < 0x80) {
|
||||
tmp[chars++] = (char)value.value.u32;
|
||||
}
|
||||
else if (value.value.u32 < 0x0800) {
|
||||
tmp[chars + 1] = (char)(0x80 | (value.value.u32 & 0x3F)); value.value.u32 >>= 6;
|
||||
tmp[chars] = (char)(0xC0 | (value.value.u32 & 0x1F));
|
||||
chars += 2;
|
||||
}
|
||||
else {
|
||||
/* surrogate pair not supported, convert to replacement char */
|
||||
if (value.value.u32 >= 0xD800 && value.value.u32 < 0xE000)
|
||||
value.value.u32 = 0xFFFD;
|
||||
|
||||
tmp[chars + 2] = (char)(0x80 | (value.value.u32 & 0x3F)); value.value.u32 >>= 6;
|
||||
tmp[chars + 1] = (char)(0x80 | (value.value.u32 & 0x3F)); value.value.u32 >>= 6;
|
||||
tmp[chars] = (char)(0xE0 | (value.value.u32 & 0x1F));
|
||||
chars += 3;
|
||||
}
|
||||
|
||||
if (chars >= sizeof(tmp) - 3 || !part->next || part->next->display_type != RC_FORMAT_UNICODECHAR)
|
||||
break;
|
||||
|
||||
part = part->next;
|
||||
} while (1);
|
||||
|
||||
tmp[chars] = '\0';
|
||||
break;
|
||||
|
||||
case RC_FORMAT_UNKNOWN_MACRO:
|
||||
chars = snprintf(tmp, sizeof(tmp), "[Unknown macro]%s", part->text);
|
||||
text = tmp;
|
||||
break;
|
||||
|
||||
default:
|
||||
value = part->value->value;
|
||||
chars = rc_format_value(tmp, sizeof(tmp), value, part->display_type);
|
||||
rc_typed_value_from_memref_value(&value, part->value);
|
||||
chars = rc_format_typed_value(tmp, sizeof(tmp), &value, part->display_type);
|
||||
text = tmp;
|
||||
break;
|
||||
}
|
||||
@ -715,3 +822,14 @@ int rc_evaluate_richpresence(rc_richpresence_t* richpresence, char* buffer, unsi
|
||||
rc_update_richpresence(richpresence, peek, peek_ud, L);
|
||||
return rc_get_richpresence_display_string(richpresence, buffer, buffersize, peek, peek_ud, L);
|
||||
}
|
||||
|
||||
void rc_reset_richpresence(rc_richpresence_t* self) {
|
||||
rc_richpresence_display_t* display;
|
||||
rc_value_t* variable;
|
||||
|
||||
for (display = self->first_display; display; display = display->next)
|
||||
rc_reset_trigger(&display->trigger);
|
||||
|
||||
for (variable = self->variables; variable; variable = variable->next)
|
||||
rc_reset_value(variable);
|
||||
}
|
||||
|
63
deps/rcheevos/src/rcheevos/runtime.c
vendored
63
deps/rcheevos/src/rcheevos/runtime.c
vendored
@ -300,6 +300,7 @@ int rc_runtime_activate_lboard(rc_runtime_t* self, unsigned id, const char* mema
|
||||
unsigned char md5[16];
|
||||
rc_lboard_t* lboard;
|
||||
rc_parse_state_t parse;
|
||||
rc_runtime_lboard_t* runtime_lboard;
|
||||
int size;
|
||||
unsigned i;
|
||||
|
||||
@ -378,14 +379,15 @@ int rc_runtime_activate_lboard(rc_runtime_t* self, unsigned id, const char* mema
|
||||
}
|
||||
|
||||
/* assign the new lboard */
|
||||
self->lboards[self->lboard_count].id = id;
|
||||
self->lboards[self->lboard_count].value = 0;
|
||||
self->lboards[self->lboard_count].lboard = lboard;
|
||||
self->lboards[self->lboard_count].buffer = lboard_buffer;
|
||||
self->lboards[self->lboard_count].invalid_memref = NULL;
|
||||
memcpy(self->lboards[self->lboard_count].md5, md5, 16);
|
||||
self->lboards[self->lboard_count].owns_memrefs = rc_runtime_allocated_memrefs(self);
|
||||
++self->lboard_count;
|
||||
runtime_lboard = &self->lboards[self->lboard_count++];
|
||||
runtime_lboard->id = id;
|
||||
runtime_lboard->value = 0;
|
||||
runtime_lboard->lboard = lboard;
|
||||
runtime_lboard->buffer = lboard_buffer;
|
||||
runtime_lboard->invalid_memref = NULL;
|
||||
memcpy(runtime_lboard->md5, md5, 16);
|
||||
runtime_lboard->serialized_size = 0;
|
||||
runtime_lboard->owns_memrefs = rc_runtime_allocated_memrefs(self);
|
||||
|
||||
/* reset it, and return it */
|
||||
lboard->memrefs = NULL;
|
||||
@ -413,17 +415,52 @@ int rc_runtime_format_lboard_value(char* buffer, int size, int value, int format
|
||||
int rc_runtime_activate_richpresence(rc_runtime_t* self, const char* script, lua_State* L, int funcs_idx) {
|
||||
rc_richpresence_t* richpresence;
|
||||
rc_runtime_richpresence_t* previous;
|
||||
rc_richpresence_display_t* display;
|
||||
rc_runtime_richpresence_t** previous_ptr;
|
||||
rc_parse_state_t parse;
|
||||
unsigned char md5[16];
|
||||
int size;
|
||||
|
||||
if (script == NULL)
|
||||
return RC_MISSING_DISPLAY_STRING;
|
||||
|
||||
rc_runtime_checksum(script, md5);
|
||||
|
||||
/* look for existing match */
|
||||
previous_ptr = NULL;
|
||||
previous = self->richpresence;
|
||||
while (previous) {
|
||||
if (previous && memcmp(self->richpresence->md5, md5, 16) == 0) {
|
||||
/* unchanged. reset all of the conditions */
|
||||
rc_reset_richpresence(self->richpresence->richpresence);
|
||||
|
||||
/* move to front of linked list*/
|
||||
if (previous_ptr) {
|
||||
*previous_ptr = previous->previous;
|
||||
if (!self->richpresence->owns_memrefs) {
|
||||
free(self->richpresence->buffer);
|
||||
previous->previous = self->richpresence->previous;
|
||||
}
|
||||
else {
|
||||
previous->previous = self->richpresence;
|
||||
}
|
||||
|
||||
self->richpresence = previous;
|
||||
}
|
||||
|
||||
/* return success*/
|
||||
return RC_OK;
|
||||
}
|
||||
|
||||
previous_ptr = &previous->previous;
|
||||
previous = previous->previous;
|
||||
}
|
||||
|
||||
/* no existing match found, parse script */
|
||||
size = rc_richpresence_size(script);
|
||||
if (size < 0)
|
||||
return size;
|
||||
|
||||
/* if the previous script doesn't have any memrefs, free it */
|
||||
previous = self->richpresence;
|
||||
if (previous) {
|
||||
if (!previous->owns_memrefs) {
|
||||
@ -432,12 +469,14 @@ int rc_runtime_activate_richpresence(rc_runtime_t* self, const char* script, lua
|
||||
}
|
||||
}
|
||||
|
||||
/* allocate and process the new script */
|
||||
self->richpresence = (rc_runtime_richpresence_t*)malloc(sizeof(rc_runtime_richpresence_t));
|
||||
if (!self->richpresence)
|
||||
return RC_OUT_OF_MEMORY;
|
||||
|
||||
self->richpresence->previous = previous;
|
||||
self->richpresence->owns_memrefs = 0;
|
||||
memcpy(self->richpresence->md5, md5, sizeof(md5));
|
||||
self->richpresence->buffer = malloc(size);
|
||||
|
||||
if (!self->richpresence->buffer)
|
||||
@ -469,11 +508,7 @@ int rc_runtime_activate_richpresence(rc_runtime_t* self, const char* script, lua
|
||||
}
|
||||
else {
|
||||
/* reset all of the conditions */
|
||||
display = richpresence->first_display;
|
||||
while (display != NULL) {
|
||||
rc_reset_trigger(&display->trigger);
|
||||
display = display->next;
|
||||
}
|
||||
rc_reset_richpresence(richpresence);
|
||||
}
|
||||
|
||||
return RC_OK;
|
||||
|
372
deps/rcheevos/src/rcheevos/runtime_progress.c
vendored
372
deps/rcheevos/src/rcheevos/runtime_progress.c
vendored
@ -3,14 +3,18 @@
|
||||
|
||||
#include "../rhash/md5.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define RC_RUNTIME_MARKER 0x0A504152 /* RAP\n */
|
||||
#define RC_RUNTIME_MARKER 0x0A504152 /* RAP\n */
|
||||
|
||||
#define RC_RUNTIME_CHUNK_MEMREFS 0x4645524D /* MREF */
|
||||
#define RC_RUNTIME_CHUNK_ACHIEVEMENT 0x56484341 /* ACHV */
|
||||
#define RC_RUNTIME_CHUNK_MEMREFS 0x4645524D /* MREF */
|
||||
#define RC_RUNTIME_CHUNK_VARIABLES 0x53524156 /* VARS */
|
||||
#define RC_RUNTIME_CHUNK_ACHIEVEMENT 0x56484341 /* ACHV */
|
||||
#define RC_RUNTIME_CHUNK_LEADERBOARD 0x4452424C /* LBRD */
|
||||
#define RC_RUNTIME_CHUNK_RICHPRESENCE 0x48434952 /* RICH */
|
||||
|
||||
#define RC_RUNTIME_CHUNK_DONE 0x454E4F44 /* DONE */
|
||||
#define RC_RUNTIME_CHUNK_DONE 0x454E4F44 /* DONE */
|
||||
|
||||
typedef struct rc_runtime_progress_t {
|
||||
rc_runtime_t* runtime;
|
||||
@ -27,6 +31,8 @@ typedef struct rc_runtime_progress_t {
|
||||
|
||||
#define RC_MEMREF_FLAG_CHANGED_THIS_FRAME 0x00010000
|
||||
|
||||
#define RC_VAR_FLAG_HAS_COND_DATA 0x01000000
|
||||
|
||||
#define RC_COND_FLAG_IS_TRUE 0x00000001
|
||||
#define RC_COND_FLAG_OPERAND1_IS_INDIRECT_MEMREF 0x00010000
|
||||
#define RC_COND_FLAG_OPERAND1_MEMREF_CHANGED_THIS_FRAME 0x00020000
|
||||
@ -75,6 +81,17 @@ static int rc_runtime_progress_match_md5(rc_runtime_progress_t* progress, unsign
|
||||
return result;
|
||||
}
|
||||
|
||||
static unsigned rc_runtime_progress_djb2(const char* input)
|
||||
{
|
||||
unsigned result = 5381;
|
||||
char c;
|
||||
|
||||
while ((c = *input++) != '\0')
|
||||
result = ((result << 5) + result) + c; /* result = result * 33 + c */
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void rc_runtime_progress_start_chunk(rc_runtime_progress_t* progress, unsigned chunk_id)
|
||||
{
|
||||
rc_runtime_progress_write_uint(progress, chunk_id);
|
||||
@ -280,7 +297,158 @@ static int rc_runtime_progress_read_condset(rc_runtime_progress_t* progress, rc_
|
||||
return RC_OK;
|
||||
}
|
||||
|
||||
static int rc_runtime_progress_write_trigger(rc_runtime_progress_t* progress, rc_trigger_t* trigger)
|
||||
static unsigned rc_runtime_progress_should_serialize_variable_condset(const rc_condset_t* conditions)
|
||||
{
|
||||
const rc_condition_t* condition;
|
||||
|
||||
/* predetermined presence of pause flag or indirect memrefs - must serialize */
|
||||
if (conditions->has_pause || conditions->has_indirect_memrefs)
|
||||
return RC_VAR_FLAG_HAS_COND_DATA;
|
||||
|
||||
/* if any conditions has required hits, must serialize */
|
||||
/* ASSERT: Measured with comparison and no explicit target will set hit target to 0xFFFFFFFF */
|
||||
for (condition = conditions->conditions; condition; condition = condition->next) {
|
||||
if (condition->required_hits > 0)
|
||||
return RC_VAR_FLAG_HAS_COND_DATA;
|
||||
}
|
||||
|
||||
/* can safely be reset without affecting behavior */
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int rc_runtime_progress_write_variable(rc_runtime_progress_t* progress, const rc_value_t* variable)
|
||||
{
|
||||
unsigned flags;
|
||||
|
||||
flags = rc_runtime_progress_should_serialize_variable_condset(variable->conditions);
|
||||
if (variable->value.changed)
|
||||
flags |= RC_MEMREF_FLAG_CHANGED_THIS_FRAME;
|
||||
|
||||
rc_runtime_progress_write_uint(progress, flags);
|
||||
rc_runtime_progress_write_uint(progress, variable->value.value);
|
||||
rc_runtime_progress_write_uint(progress, variable->value.prior);
|
||||
|
||||
if (flags & RC_VAR_FLAG_HAS_COND_DATA) {
|
||||
int result = rc_runtime_progress_write_condset(progress, variable->conditions);
|
||||
if (result != RC_OK)
|
||||
return result;
|
||||
}
|
||||
|
||||
return RC_OK;
|
||||
}
|
||||
|
||||
static int rc_runtime_progress_write_variables(rc_runtime_progress_t* progress)
|
||||
{
|
||||
unsigned count = 0;
|
||||
const rc_value_t* variable;
|
||||
|
||||
for (variable = progress->runtime->variables; variable; variable = variable->next)
|
||||
++count;
|
||||
if (count == 0)
|
||||
return RC_OK;
|
||||
|
||||
rc_runtime_progress_start_chunk(progress, RC_RUNTIME_CHUNK_VARIABLES);
|
||||
rc_runtime_progress_write_uint(progress, count);
|
||||
|
||||
for (variable = progress->runtime->variables; variable; variable = variable->next)
|
||||
{
|
||||
unsigned djb2 = rc_runtime_progress_djb2(variable->name);
|
||||
rc_runtime_progress_write_uint(progress, djb2);
|
||||
|
||||
rc_runtime_progress_write_variable(progress, variable);
|
||||
}
|
||||
|
||||
rc_runtime_progress_end_chunk(progress);
|
||||
return RC_OK;
|
||||
}
|
||||
|
||||
static int rc_runtime_progress_read_variable(rc_runtime_progress_t* progress, rc_value_t* variable)
|
||||
{
|
||||
unsigned flags = rc_runtime_progress_read_uint(progress);
|
||||
variable->value.changed = (flags & RC_MEMREF_FLAG_CHANGED_THIS_FRAME) ? 1 : 0;
|
||||
variable->value.value = rc_runtime_progress_read_uint(progress);
|
||||
variable->value.prior = rc_runtime_progress_read_uint(progress);
|
||||
|
||||
if (flags & RC_VAR_FLAG_HAS_COND_DATA) {
|
||||
int result = rc_runtime_progress_read_condset(progress, variable->conditions);
|
||||
if (result != RC_OK)
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
rc_reset_condset(variable->conditions);
|
||||
}
|
||||
|
||||
return RC_OK;
|
||||
}
|
||||
|
||||
static int rc_runtime_progress_read_variables(rc_runtime_progress_t* progress)
|
||||
{
|
||||
struct rc_pending_value_t
|
||||
{
|
||||
rc_value_t* variable;
|
||||
unsigned djb2;
|
||||
};
|
||||
struct rc_pending_value_t local_pending_variables[32];
|
||||
struct rc_pending_value_t* pending_variables;
|
||||
rc_value_t* variable;
|
||||
unsigned count, serialized_count;
|
||||
int result;
|
||||
unsigned i;
|
||||
|
||||
serialized_count = rc_runtime_progress_read_uint(progress);
|
||||
if (serialized_count == 0)
|
||||
return RC_OK;
|
||||
|
||||
count = 0;
|
||||
for (variable = progress->runtime->variables; variable; variable = variable->next)
|
||||
++count;
|
||||
|
||||
if (count == 0)
|
||||
return RC_OK;
|
||||
|
||||
if (count <= sizeof(local_pending_variables) / sizeof(local_pending_variables[0])) {
|
||||
pending_variables = local_pending_variables;
|
||||
}
|
||||
else {
|
||||
pending_variables = (struct rc_pending_value_t*)malloc(count * sizeof(struct rc_pending_value_t));
|
||||
if (pending_variables == NULL)
|
||||
return RC_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
count = 0;
|
||||
for (variable = progress->runtime->variables; variable; variable = variable->next) {
|
||||
pending_variables[count].variable = variable;
|
||||
pending_variables[count].djb2 = rc_runtime_progress_djb2(variable->name);
|
||||
++count;
|
||||
}
|
||||
|
||||
result = RC_OK;
|
||||
for (; serialized_count > 0 && result == RC_OK; --serialized_count) {
|
||||
unsigned djb2 = rc_runtime_progress_read_uint(progress);
|
||||
for (i = 0; i < count; ++i) {
|
||||
if (pending_variables[i].djb2 == djb2) {
|
||||
variable = pending_variables[i].variable;
|
||||
result = rc_runtime_progress_read_variable(progress, variable);
|
||||
if (result == RC_OK) {
|
||||
if (i < count - 1)
|
||||
memcpy(&pending_variables[i], &pending_variables[count - 1], sizeof(struct rc_pending_value_t));
|
||||
count--;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (count > 0)
|
||||
rc_reset_value(pending_variables[--count].variable);
|
||||
|
||||
if (pending_variables != local_pending_variables)
|
||||
free(pending_variables);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int rc_runtime_progress_write_trigger(rc_runtime_progress_t* progress, const rc_trigger_t* trigger)
|
||||
{
|
||||
rc_condset_t* condset;
|
||||
int result;
|
||||
@ -394,6 +562,153 @@ static int rc_runtime_progress_read_achievement(rc_runtime_progress_t* progress)
|
||||
return RC_OK;
|
||||
}
|
||||
|
||||
static int rc_runtime_progress_write_leaderboards(rc_runtime_progress_t* progress)
|
||||
{
|
||||
unsigned i;
|
||||
unsigned flags;
|
||||
int offset = 0;
|
||||
int result;
|
||||
|
||||
for (i = 0; i < progress->runtime->lboard_count; ++i) {
|
||||
rc_runtime_lboard_t* runtime_lboard = &progress->runtime->lboards[i];
|
||||
if (!runtime_lboard->lboard)
|
||||
continue;
|
||||
|
||||
/* don't store state for inactive leaderboards */
|
||||
if (!rc_lboard_state_active(runtime_lboard->lboard->state))
|
||||
continue;
|
||||
|
||||
if (!progress->buffer) {
|
||||
if (runtime_lboard->serialized_size) {
|
||||
progress->offset += runtime_lboard->serialized_size;
|
||||
continue;
|
||||
}
|
||||
|
||||
offset = progress->offset;
|
||||
}
|
||||
|
||||
rc_runtime_progress_start_chunk(progress, RC_RUNTIME_CHUNK_LEADERBOARD);
|
||||
rc_runtime_progress_write_uint(progress, runtime_lboard->id);
|
||||
rc_runtime_progress_write_md5(progress, runtime_lboard->md5);
|
||||
|
||||
flags = runtime_lboard->lboard->state;
|
||||
rc_runtime_progress_write_uint(progress, flags);
|
||||
|
||||
result = rc_runtime_progress_write_trigger(progress, &runtime_lboard->lboard->start);
|
||||
if (result != RC_OK)
|
||||
return result;
|
||||
|
||||
result = rc_runtime_progress_write_trigger(progress, &runtime_lboard->lboard->submit);
|
||||
if (result != RC_OK)
|
||||
return result;
|
||||
|
||||
result = rc_runtime_progress_write_trigger(progress, &runtime_lboard->lboard->cancel);
|
||||
if (result != RC_OK)
|
||||
return result;
|
||||
|
||||
result = rc_runtime_progress_write_variable(progress, &runtime_lboard->lboard->value);
|
||||
if (result != RC_OK)
|
||||
return result;
|
||||
|
||||
rc_runtime_progress_end_chunk(progress);
|
||||
|
||||
if (!progress->buffer)
|
||||
runtime_lboard->serialized_size = progress->offset - offset;
|
||||
}
|
||||
|
||||
return RC_OK;
|
||||
}
|
||||
|
||||
static int rc_runtime_progress_read_leaderboard(rc_runtime_progress_t* progress)
|
||||
{
|
||||
unsigned id = rc_runtime_progress_read_uint(progress);
|
||||
unsigned i;
|
||||
int result;
|
||||
|
||||
for (i = 0; i < progress->runtime->lboard_count; ++i) {
|
||||
rc_runtime_lboard_t* runtime_lboard = &progress->runtime->lboards[i];
|
||||
if (runtime_lboard->id == id && runtime_lboard->lboard != NULL) {
|
||||
/* ignore triggered and waiting achievements */
|
||||
if (runtime_lboard->lboard->state == RC_TRIGGER_STATE_UNUPDATED) {
|
||||
/* only update state if definition hasn't changed (md5 matches) */
|
||||
if (rc_runtime_progress_match_md5(progress, runtime_lboard->md5)) {
|
||||
unsigned flags = rc_runtime_progress_read_uint(progress);
|
||||
|
||||
result = rc_runtime_progress_read_trigger(progress, &runtime_lboard->lboard->start);
|
||||
if (result != RC_OK)
|
||||
return result;
|
||||
|
||||
result = rc_runtime_progress_read_trigger(progress, &runtime_lboard->lboard->submit);
|
||||
if (result != RC_OK)
|
||||
return result;
|
||||
|
||||
result = rc_runtime_progress_read_trigger(progress, &runtime_lboard->lboard->cancel);
|
||||
if (result != RC_OK)
|
||||
return result;
|
||||
|
||||
result = rc_runtime_progress_read_variable(progress, &runtime_lboard->lboard->value);
|
||||
if (result != RC_OK)
|
||||
return result;
|
||||
|
||||
runtime_lboard->lboard->state = (char)(flags & 0x7F);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return RC_OK;
|
||||
}
|
||||
|
||||
static int rc_runtime_progress_write_rich_presence(rc_runtime_progress_t* progress)
|
||||
{
|
||||
const rc_richpresence_display_t* display;
|
||||
int result;
|
||||
|
||||
if (!progress->runtime->richpresence || !progress->runtime->richpresence->richpresence)
|
||||
return RC_OK;
|
||||
|
||||
/* if there are no conditional display strings, there's nothing to capture */
|
||||
display = progress->runtime->richpresence->richpresence->first_display;
|
||||
if (!display->next)
|
||||
return RC_OK;
|
||||
|
||||
rc_runtime_progress_start_chunk(progress, RC_RUNTIME_CHUNK_RICHPRESENCE);
|
||||
rc_runtime_progress_write_md5(progress, progress->runtime->richpresence->md5);
|
||||
|
||||
for (; display->next; display = display->next) {
|
||||
result = rc_runtime_progress_write_trigger(progress, &display->trigger);
|
||||
if (result != RC_OK)
|
||||
return result;
|
||||
}
|
||||
|
||||
rc_runtime_progress_end_chunk(progress);
|
||||
return RC_OK;
|
||||
}
|
||||
|
||||
static int rc_runtime_progress_read_rich_presence(rc_runtime_progress_t* progress)
|
||||
{
|
||||
rc_richpresence_display_t* display;
|
||||
int result;
|
||||
|
||||
if (!progress->runtime->richpresence || !progress->runtime->richpresence->richpresence)
|
||||
return RC_OK;
|
||||
|
||||
if (!rc_runtime_progress_match_md5(progress, progress->runtime->richpresence->md5)) {
|
||||
rc_reset_richpresence(progress->runtime->richpresence->richpresence);
|
||||
return RC_OK;
|
||||
}
|
||||
|
||||
display = progress->runtime->richpresence->richpresence->first_display;
|
||||
for (; display->next; display = display->next) {
|
||||
result = rc_runtime_progress_read_trigger(progress, &display->trigger);
|
||||
if (result != RC_OK)
|
||||
return result;
|
||||
}
|
||||
|
||||
return RC_OK;
|
||||
}
|
||||
|
||||
static int rc_runtime_progress_serialize_internal(rc_runtime_progress_t* progress)
|
||||
{
|
||||
md5_state_t state;
|
||||
@ -405,9 +720,18 @@ static int rc_runtime_progress_serialize_internal(rc_runtime_progress_t* progres
|
||||
if ((result = rc_runtime_progress_write_memrefs(progress)) != RC_OK)
|
||||
return result;
|
||||
|
||||
if ((result = rc_runtime_progress_write_variables(progress)) != RC_OK)
|
||||
return result;
|
||||
|
||||
if ((result = rc_runtime_progress_write_achievements(progress)) != RC_OK)
|
||||
return result;
|
||||
|
||||
if ((result = rc_runtime_progress_write_leaderboards(progress)) != RC_OK)
|
||||
return result;
|
||||
|
||||
if ((result = rc_runtime_progress_write_rich_presence(progress)) != RC_OK)
|
||||
return result;
|
||||
|
||||
rc_runtime_progress_write_uint(progress, RC_RUNTIME_CHUNK_DONE);
|
||||
rc_runtime_progress_write_uint(progress, 16);
|
||||
|
||||
@ -455,6 +779,7 @@ int rc_runtime_deserialize_progress(rc_runtime_t* runtime, const unsigned char*
|
||||
unsigned chunk_size;
|
||||
unsigned next_chunk_offset;
|
||||
unsigned i;
|
||||
int seen_rich_presence = 0;
|
||||
int result = RC_OK;
|
||||
|
||||
rc_runtime_progress_init(&progress, runtime, L);
|
||||
@ -469,8 +794,7 @@ int rc_runtime_deserialize_progress(rc_runtime_t* runtime, const unsigned char*
|
||||
rc_runtime_trigger_t* runtime_trigger = &runtime->triggers[i];
|
||||
if (runtime_trigger->trigger) {
|
||||
/* don't update state for inactive or triggered achievements */
|
||||
if (rc_trigger_state_active(runtime_trigger->trigger->state))
|
||||
{
|
||||
if (rc_trigger_state_active(runtime_trigger->trigger->state)) {
|
||||
/* mark active achievements as unupdated. anything that's still unupdated
|
||||
* after deserializing the progress will be reset to waiting */
|
||||
runtime_trigger->trigger->state = RC_TRIGGER_STATE_UNUPDATED;
|
||||
@ -478,6 +802,18 @@ int rc_runtime_deserialize_progress(rc_runtime_t* runtime, const unsigned char*
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < runtime->lboard_count; ++i) {
|
||||
rc_runtime_lboard_t* runtime_lboard = &runtime->lboards[i];
|
||||
if (runtime_lboard->lboard) {
|
||||
/* don't update state for inactive or triggered achievements */
|
||||
if (rc_lboard_state_active(runtime_lboard->lboard->state)) {
|
||||
/* mark active achievements as unupdated. anything that's still unupdated
|
||||
* after deserializing the progress will be reset to waiting */
|
||||
runtime_lboard->lboard->state = RC_TRIGGER_STATE_UNUPDATED;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
chunk_id = rc_runtime_progress_read_uint(&progress);
|
||||
chunk_size = rc_runtime_progress_read_uint(&progress);
|
||||
@ -489,10 +825,23 @@ int rc_runtime_deserialize_progress(rc_runtime_t* runtime, const unsigned char*
|
||||
result = rc_runtime_progress_read_memrefs(&progress);
|
||||
break;
|
||||
|
||||
case RC_RUNTIME_CHUNK_VARIABLES:
|
||||
result = rc_runtime_progress_read_variables(&progress);
|
||||
break;
|
||||
|
||||
case RC_RUNTIME_CHUNK_ACHIEVEMENT:
|
||||
result = rc_runtime_progress_read_achievement(&progress);
|
||||
break;
|
||||
|
||||
case RC_RUNTIME_CHUNK_LEADERBOARD:
|
||||
result = rc_runtime_progress_read_leaderboard(&progress);
|
||||
break;
|
||||
|
||||
case RC_RUNTIME_CHUNK_RICHPRESENCE:
|
||||
seen_rich_presence = 1;
|
||||
result = rc_runtime_progress_read_rich_presence(&progress);
|
||||
break;
|
||||
|
||||
case RC_RUNTIME_CHUNK_DONE:
|
||||
md5_init(&state);
|
||||
md5_append(&state, progress.buffer, progress.offset);
|
||||
@ -519,6 +868,15 @@ int rc_runtime_deserialize_progress(rc_runtime_t* runtime, const unsigned char*
|
||||
if (trigger && trigger->state == RC_TRIGGER_STATE_UNUPDATED)
|
||||
rc_reset_trigger(trigger);
|
||||
}
|
||||
|
||||
for (i = 0; i < runtime->lboard_count; ++i) {
|
||||
rc_lboard_t* lboard = runtime->lboards[i].lboard;
|
||||
if (lboard && lboard->state == RC_TRIGGER_STATE_UNUPDATED)
|
||||
rc_reset_lboard(lboard);
|
||||
}
|
||||
|
||||
if (!seen_rich_presence && runtime->richpresence && runtime->richpresence->richpresence)
|
||||
rc_reset_richpresence(runtime->richpresence->richpresence);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
6
deps/rcheevos/src/rcheevos/trigger.c
vendored
6
deps/rcheevos/src/rcheevos/trigger.c
vendored
@ -179,8 +179,10 @@ int rc_evaluate_trigger(rc_trigger_t* self, rc_peek_t peek, void* ud, lua_State*
|
||||
}
|
||||
|
||||
/* if paused, the measured value may not be captured, keep the old value */
|
||||
if (!is_paused)
|
||||
self->measured_value = eval_state.measured_value;
|
||||
if (!is_paused) {
|
||||
rc_typed_value_convert(&eval_state.measured_value, RC_VALUE_TYPE_UNSIGNED);
|
||||
self->measured_value = eval_state.measured_value.value.u32;
|
||||
}
|
||||
|
||||
/* if the state is WAITING and the trigger is ready to fire, ignore it and reset the hit counts */
|
||||
/* otherwise, if the state is WAITING, proceed to activating the trigger */
|
||||
|
468
deps/rcheevos/src/rcheevos/value.c
vendored
468
deps/rcheevos/src/rcheevos/value.c
vendored
@ -2,6 +2,7 @@
|
||||
|
||||
#include <string.h> /* memset */
|
||||
#include <ctype.h> /* isdigit */
|
||||
#include <float.h> /* FLT_EPSILON */
|
||||
|
||||
static void rc_parse_cond_value(rc_value_t* self, const char** memaddr, rc_parse_state_t* parse) {
|
||||
rc_condset_t** next_clause;
|
||||
@ -43,28 +44,26 @@ void rc_parse_legacy_value(rc_value_t* self, const char** memaddr, rc_parse_stat
|
||||
char buffer[64] = "A:";
|
||||
const char* buffer_ptr;
|
||||
char* ptr;
|
||||
int end_of_clause;
|
||||
|
||||
/* convert legacy format into condset */
|
||||
self->conditions = RC_ALLOC(rc_condset_t, parse);
|
||||
self->conditions->has_pause = 0;
|
||||
self->conditions->is_paused = 0;
|
||||
memset(self->conditions, 0, sizeof(rc_condset_t));
|
||||
|
||||
next = &self->conditions->conditions;
|
||||
next_clause = &self->conditions->next;
|
||||
|
||||
for (;;) {
|
||||
for (;; ++(*memaddr)) {
|
||||
buffer[0] = 'A'; /* reset to AddSource */
|
||||
ptr = &buffer[2];
|
||||
end_of_clause = 0;
|
||||
|
||||
do {
|
||||
/* extract the next clause */
|
||||
for (;; ++(*memaddr)) {
|
||||
switch (**memaddr) {
|
||||
case '_': /* add next */
|
||||
case '$': /* maximum of */
|
||||
case '\0': /* end of string */
|
||||
case ':': /* end of leaderboard clause */
|
||||
case ')': /* end of rich presence macro */
|
||||
end_of_clause = 1;
|
||||
*ptr = '\0';
|
||||
break;
|
||||
|
||||
@ -72,24 +71,31 @@ void rc_parse_legacy_value(rc_value_t* self, const char** memaddr, rc_parse_stat
|
||||
*ptr++ = '*';
|
||||
|
||||
buffer_ptr = *memaddr + 1;
|
||||
if (*buffer_ptr == '-' || *buffer_ptr == '+')
|
||||
++buffer_ptr; /* ignore sign */
|
||||
if (*buffer_ptr == '-') {
|
||||
buffer[0] = 'B'; /* change to SubSource */
|
||||
++(*memaddr); /* don't copy sign */
|
||||
++buffer_ptr; /* ignore sign when doing floating point check */
|
||||
}
|
||||
else if (*buffer_ptr == '+') {
|
||||
++buffer_ptr; /* ignore sign when doing floating point check */
|
||||
}
|
||||
|
||||
/* if it looks like a floating point number, add the 'f' prefix */
|
||||
while (isdigit((unsigned char)*buffer_ptr))
|
||||
++buffer_ptr;
|
||||
if (*buffer_ptr == '.')
|
||||
*ptr++ = 'f';
|
||||
break;
|
||||
continue;
|
||||
|
||||
default:
|
||||
*ptr++ = **memaddr;
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
||||
++(*memaddr);
|
||||
} while (!end_of_clause);
|
||||
break;
|
||||
}
|
||||
|
||||
/* process the clause */
|
||||
buffer_ptr = buffer;
|
||||
cond = rc_parse_condition(&buffer_ptr, parse, 0);
|
||||
if (parse->offset < 0)
|
||||
@ -113,31 +119,40 @@ void rc_parse_legacy_value(rc_value_t* self, const char** memaddr, rc_parse_stat
|
||||
return;
|
||||
}
|
||||
|
||||
cond->pause = 0;
|
||||
*next = cond;
|
||||
|
||||
switch ((*memaddr)[-1]) {
|
||||
case '_': /* add next */
|
||||
next = &cond->next;
|
||||
break;
|
||||
|
||||
case '$': /* max of */
|
||||
cond->type = RC_CONDITION_MEASURED;
|
||||
cond->next = 0;
|
||||
*next_clause = RC_ALLOC(rc_condset_t, parse);
|
||||
(*next_clause)->has_pause = 0;
|
||||
(*next_clause)->is_paused = 0;
|
||||
next = &(*next_clause)->conditions;
|
||||
next_clause = &(*next_clause)->next;
|
||||
break;
|
||||
|
||||
default: /* end of valid string */
|
||||
--(*memaddr); /* undo the increment we performed when copying the string */
|
||||
cond->type = RC_CONDITION_MEASURED;
|
||||
cond->next = 0;
|
||||
*next_clause = 0;
|
||||
return;
|
||||
if (**memaddr == '_') {
|
||||
/* add next */
|
||||
next = &cond->next;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (cond->type == RC_CONDITION_SUB_SOURCE) {
|
||||
/* cannot change SubSource to Measured. add a dummy condition */
|
||||
next = &cond->next;
|
||||
buffer_ptr = "A:0";
|
||||
cond = rc_parse_condition(&buffer_ptr, parse, 0);
|
||||
*next = cond;
|
||||
}
|
||||
|
||||
/* convert final AddSource condition to Measured */
|
||||
cond->type = RC_CONDITION_MEASURED;
|
||||
cond->next = 0;
|
||||
|
||||
if (**memaddr != '$') {
|
||||
/* end of valid string */
|
||||
*next_clause = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
/* max of ($), start a new clause */
|
||||
*next_clause = RC_ALLOC(rc_condset_t, parse);
|
||||
|
||||
if (parse->buffer) /* don't clear in sizing mode or pointer will break */
|
||||
memset(*next_clause, 0, sizeof(rc_condset_t));
|
||||
|
||||
next = &(*next_clause)->conditions;
|
||||
next_clause = &(*next_clause)->next;
|
||||
}
|
||||
}
|
||||
|
||||
@ -188,14 +203,16 @@ rc_value_t* rc_parse_value(void* buffer, const char* memaddr, lua_State* L, int
|
||||
return (parse.offset >= 0) ? self : NULL;
|
||||
}
|
||||
|
||||
int rc_evaluate_value(rc_value_t* self, rc_peek_t peek, void* ud, lua_State* L) {
|
||||
int rc_evaluate_value_typed(rc_value_t* self, rc_typed_value_t* value, rc_peek_t peek, void* ud, lua_State* L) {
|
||||
rc_eval_state_t eval_state;
|
||||
rc_condset_t* condset;
|
||||
int result = 0;
|
||||
int paused = 1;
|
||||
int valid = 0;
|
||||
|
||||
rc_update_memref_values(self->memrefs, peek, ud);
|
||||
|
||||
value->value.i32 = 0;
|
||||
value->type = RC_VALUE_TYPE_SIGNED;
|
||||
|
||||
for (condset = self->conditions; condset != NULL; condset = condset->next) {
|
||||
memset(&eval_state, 0, sizeof(eval_state));
|
||||
eval_state.peek = peek;
|
||||
@ -214,34 +231,46 @@ int rc_evaluate_value(rc_value_t* self, rc_peek_t peek, void* ud, lua_State* L)
|
||||
rc_reset_condset(condset);
|
||||
|
||||
/* if the measured value came from a hit count, reset it too */
|
||||
if (eval_state.measured_from_hits)
|
||||
eval_state.measured_value = 0;
|
||||
if (eval_state.measured_from_hits) {
|
||||
eval_state.measured_value.value.u32 = 0;
|
||||
eval_state.measured_value.type = RC_VALUE_TYPE_UNSIGNED;
|
||||
}
|
||||
}
|
||||
|
||||
if (paused) {
|
||||
if (!valid) {
|
||||
/* capture the first valid measurement */
|
||||
result = (int)eval_state.measured_value;
|
||||
paused = 0;
|
||||
memcpy(value, &eval_state.measured_value, sizeof(*value));
|
||||
valid = 1;
|
||||
}
|
||||
else {
|
||||
/* multiple condsets are currently only used for the MAX_OF operation.
|
||||
* only keep the condset's value if it's higher than the current highest value.
|
||||
*/
|
||||
if ((int)eval_state.measured_value > result)
|
||||
result = (int)eval_state.measured_value;
|
||||
if (rc_typed_value_compare(&eval_state.measured_value, value, RC_OPERATOR_GT))
|
||||
memcpy(value, &eval_state.measured_value, sizeof(*value));
|
||||
}
|
||||
}
|
||||
|
||||
if (!paused) {
|
||||
return valid;
|
||||
}
|
||||
|
||||
int rc_evaluate_value(rc_value_t* self, rc_peek_t peek, void* ud, lua_State* L) {
|
||||
rc_typed_value_t result;
|
||||
int valid = rc_evaluate_value_typed(self, &result, peek, ud, L);
|
||||
|
||||
if (valid) {
|
||||
/* if not paused, store the value so that it's available when paused. */
|
||||
rc_update_memref_value(&self->value, result);
|
||||
rc_typed_value_convert(&result, RC_VALUE_TYPE_UNSIGNED);
|
||||
rc_update_memref_value(&self->value, result.value.u32);
|
||||
}
|
||||
else {
|
||||
/* when paused, the Measured value will not be captured, use the last captured value. */
|
||||
result = self->value.value;
|
||||
result.value.u32 = self->value.value;
|
||||
result.type = RC_VALUE_TYPE_UNSIGNED;
|
||||
}
|
||||
|
||||
return result;
|
||||
rc_typed_value_convert(&result, RC_VALUE_TYPE_SIGNED);
|
||||
return result.value.i32;
|
||||
}
|
||||
|
||||
void rc_reset_value(rc_value_t* self) {
|
||||
@ -309,8 +338,347 @@ rc_value_t* rc_alloc_helper_variable(const char* memaddr, int memaddr_len, rc_pa
|
||||
}
|
||||
|
||||
void rc_update_variables(rc_value_t* variable, rc_peek_t peek, void* ud, lua_State* L) {
|
||||
rc_typed_value_t result;
|
||||
|
||||
while (variable) {
|
||||
rc_evaluate_value(variable, peek, ud, L);
|
||||
if (rc_evaluate_value_typed(variable, &result, peek, ud, L)) {
|
||||
/* store the raw bytes and type to be restored by rc_typed_value_from_memref_value */
|
||||
rc_update_memref_value(&variable->value, result.value.u32);
|
||||
variable->value.type = result.type;
|
||||
}
|
||||
|
||||
variable = variable->next;
|
||||
}
|
||||
}
|
||||
|
||||
void rc_typed_value_from_memref_value(rc_typed_value_t* value, const rc_memref_value_t* memref) {
|
||||
value->value.u32 = memref->value;
|
||||
|
||||
if (memref->size == RC_MEMSIZE_VARIABLE) {
|
||||
/* a variable can be any of the supported types, but the raw data was copied into u32 */
|
||||
value->type = memref->type;
|
||||
}
|
||||
else {
|
||||
/* not a variable, only u32 is supported */
|
||||
value->type = RC_VALUE_TYPE_UNSIGNED;
|
||||
}
|
||||
}
|
||||
|
||||
void rc_typed_value_convert(rc_typed_value_t* value, char new_type) {
|
||||
switch (new_type) {
|
||||
case RC_VALUE_TYPE_UNSIGNED:
|
||||
switch (value->type) {
|
||||
case RC_VALUE_TYPE_UNSIGNED:
|
||||
return;
|
||||
case RC_VALUE_TYPE_SIGNED:
|
||||
value->value.u32 = (unsigned)value->value.i32;
|
||||
break;
|
||||
case RC_VALUE_TYPE_FLOAT:
|
||||
value->value.u32 = (unsigned)value->value.f32;
|
||||
break;
|
||||
default:
|
||||
value->value.u32 = 0;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case RC_VALUE_TYPE_SIGNED:
|
||||
switch (value->type) {
|
||||
case RC_VALUE_TYPE_SIGNED:
|
||||
return;
|
||||
case RC_VALUE_TYPE_UNSIGNED:
|
||||
value->value.i32 = (int)value->value.u32;
|
||||
break;
|
||||
case RC_VALUE_TYPE_FLOAT:
|
||||
value->value.i32 = (int)value->value.f32;
|
||||
break;
|
||||
default:
|
||||
value->value.i32 = 0;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case RC_VALUE_TYPE_FLOAT:
|
||||
switch (value->type) {
|
||||
case RC_VALUE_TYPE_FLOAT:
|
||||
return;
|
||||
case RC_VALUE_TYPE_UNSIGNED:
|
||||
value->value.f32 = (float)value->value.u32;
|
||||
break;
|
||||
case RC_VALUE_TYPE_SIGNED:
|
||||
value->value.f32 = (float)value->value.i32;
|
||||
break;
|
||||
default:
|
||||
value->value.f32 = 0.0;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
value->type = new_type;
|
||||
}
|
||||
|
||||
static rc_typed_value_t* rc_typed_value_convert_into(rc_typed_value_t* dest, const rc_typed_value_t* source, char new_type) {
|
||||
memcpy(dest, source, sizeof(rc_typed_value_t));
|
||||
rc_typed_value_convert(dest, new_type);
|
||||
return dest;
|
||||
}
|
||||
|
||||
void rc_typed_value_add(rc_typed_value_t* value, const rc_typed_value_t* amount) {
|
||||
rc_typed_value_t converted;
|
||||
|
||||
if (amount->type != value->type && value->type != RC_VALUE_TYPE_NONE)
|
||||
amount = rc_typed_value_convert_into(&converted, amount, value->type);
|
||||
|
||||
switch (value->type)
|
||||
{
|
||||
case RC_VALUE_TYPE_UNSIGNED:
|
||||
value->value.u32 += amount->value.u32;
|
||||
break;
|
||||
|
||||
case RC_VALUE_TYPE_SIGNED:
|
||||
value->value.i32 += amount->value.i32;
|
||||
break;
|
||||
|
||||
case RC_VALUE_TYPE_FLOAT:
|
||||
value->value.f32 += amount->value.f32;
|
||||
break;
|
||||
|
||||
case RC_VALUE_TYPE_NONE:
|
||||
memcpy(value, amount, sizeof(rc_typed_value_t));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void rc_typed_value_multiply(rc_typed_value_t* value, const rc_typed_value_t* amount) {
|
||||
rc_typed_value_t converted;
|
||||
|
||||
switch (value->type)
|
||||
{
|
||||
case RC_VALUE_TYPE_UNSIGNED:
|
||||
switch (amount->type)
|
||||
{
|
||||
case RC_VALUE_TYPE_UNSIGNED:
|
||||
/* the c standard for unsigned multiplication is well defined as non-overflowing truncation
|
||||
* to the type's size. this allows negative multiplication through twos-complements. i.e.
|
||||
* 1 * -1 (0xFFFFFFFF) = 0xFFFFFFFF = -1
|
||||
* 3 * -2 (0xFFFFFFFE) = 0x2FFFFFFFA & 0xFFFFFFFF = 0xFFFFFFFA = -6
|
||||
* 10 * -5 (0xFFFFFFFB) = 0x9FFFFFFCE & 0xFFFFFFFF = 0xFFFFFFCE = -50
|
||||
*/
|
||||
value->value.u32 *= amount->value.u32;
|
||||
break;
|
||||
|
||||
case RC_VALUE_TYPE_SIGNED:
|
||||
value->value.u32 *= (unsigned)amount->value.i32;
|
||||
break;
|
||||
|
||||
case RC_VALUE_TYPE_FLOAT:
|
||||
rc_typed_value_convert(value, RC_VALUE_TYPE_FLOAT);
|
||||
value->value.f32 *= amount->value.f32;
|
||||
break;
|
||||
|
||||
default:
|
||||
value->type = RC_VALUE_TYPE_NONE;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case RC_VALUE_TYPE_SIGNED:
|
||||
switch (amount->type)
|
||||
{
|
||||
case RC_VALUE_TYPE_SIGNED:
|
||||
value->value.i32 *= amount->value.i32;
|
||||
break;
|
||||
|
||||
case RC_VALUE_TYPE_UNSIGNED:
|
||||
value->value.i32 *= (int)amount->value.u32;
|
||||
break;
|
||||
|
||||
case RC_VALUE_TYPE_FLOAT:
|
||||
rc_typed_value_convert(value, RC_VALUE_TYPE_FLOAT);
|
||||
value->value.f32 *= amount->value.f32;
|
||||
break;
|
||||
|
||||
default:
|
||||
value->type = RC_VALUE_TYPE_NONE;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case RC_VALUE_TYPE_FLOAT:
|
||||
if (amount->type == RC_VALUE_TYPE_NONE) {
|
||||
value->type = RC_VALUE_TYPE_NONE;
|
||||
}
|
||||
else {
|
||||
amount = rc_typed_value_convert_into(&converted, amount, RC_VALUE_TYPE_FLOAT);
|
||||
value->value.f32 *= amount->value.f32;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
value->type = RC_VALUE_TYPE_NONE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void rc_typed_value_divide(rc_typed_value_t* value, const rc_typed_value_t* amount) {
|
||||
rc_typed_value_t converted;
|
||||
|
||||
switch (amount->type)
|
||||
{
|
||||
case RC_VALUE_TYPE_UNSIGNED:
|
||||
if (amount->value.u32 == 0) { /* divide by zero */
|
||||
value->type = RC_VALUE_TYPE_NONE;
|
||||
return;
|
||||
}
|
||||
|
||||
switch (value->type) {
|
||||
case RC_VALUE_TYPE_UNSIGNED: /* integer math */
|
||||
value->value.u32 /= amount->value.u32;
|
||||
return;
|
||||
case RC_VALUE_TYPE_SIGNED: /* integer math */
|
||||
value->value.i32 /= (int)amount->value.u32;
|
||||
return;
|
||||
case RC_VALUE_TYPE_FLOAT:
|
||||
amount = rc_typed_value_convert_into(&converted, amount, RC_VALUE_TYPE_FLOAT);
|
||||
break;
|
||||
default:
|
||||
value->type = RC_VALUE_TYPE_NONE;
|
||||
return;
|
||||
}
|
||||
break;
|
||||
|
||||
case RC_VALUE_TYPE_SIGNED:
|
||||
if (amount->value.i32 == 0) { /* divide by zero */
|
||||
value->type = RC_VALUE_TYPE_NONE;
|
||||
return;
|
||||
}
|
||||
|
||||
switch (value->type) {
|
||||
case RC_VALUE_TYPE_SIGNED: /* integer math */
|
||||
value->value.i32 /= amount->value.i32;
|
||||
return;
|
||||
case RC_VALUE_TYPE_UNSIGNED: /* integer math */
|
||||
value->value.u32 /= (unsigned)amount->value.i32;
|
||||
return;
|
||||
case RC_VALUE_TYPE_FLOAT:
|
||||
amount = rc_typed_value_convert_into(&converted, amount, RC_VALUE_TYPE_FLOAT);
|
||||
break;
|
||||
default:
|
||||
value->type = RC_VALUE_TYPE_NONE;
|
||||
return;
|
||||
}
|
||||
break;
|
||||
|
||||
case RC_VALUE_TYPE_FLOAT:
|
||||
break;
|
||||
|
||||
default:
|
||||
value->type = RC_VALUE_TYPE_NONE;
|
||||
return;
|
||||
}
|
||||
|
||||
if (amount->value.f32 == 0.0) { /* divide by zero */
|
||||
value->type = RC_VALUE_TYPE_NONE;
|
||||
return;
|
||||
}
|
||||
|
||||
rc_typed_value_convert(value, RC_VALUE_TYPE_FLOAT);
|
||||
value->value.f32 /= amount->value.f32;
|
||||
}
|
||||
|
||||
static int rc_typed_value_compare_floats(float f1, float f2, char oper) {
|
||||
if (f1 == f2) {
|
||||
/* exactly equal */
|
||||
}
|
||||
else {
|
||||
/* attempt to match 7 significant digits (24-bit mantissa supports just over 7 significant decimal digits) */
|
||||
/* https://stackoverflow.com/questions/17333/what-is-the-most-effective-way-for-float-and-double-comparison */
|
||||
const float abs1 = (f1 < 0) ? -f1 : f1;
|
||||
const float abs2 = (f2 < 0) ? -f2 : f2;
|
||||
const float threshold = ((abs1 < abs2) ? abs1 : abs2) * FLT_EPSILON;
|
||||
const float diff = f1 - f2;
|
||||
const float abs_diff = (diff < 0) ? -diff : diff;
|
||||
|
||||
if (abs_diff <= threshold) {
|
||||
/* approximately equal */
|
||||
}
|
||||
else if (diff > threshold) {
|
||||
/* greater */
|
||||
switch (oper) {
|
||||
case RC_OPERATOR_NE:
|
||||
case RC_OPERATOR_GT:
|
||||
case RC_OPERATOR_GE:
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* lesser */
|
||||
switch (oper) {
|
||||
case RC_OPERATOR_NE:
|
||||
case RC_OPERATOR_LT:
|
||||
case RC_OPERATOR_LE:
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* exactly or approximately equal */
|
||||
switch (oper) {
|
||||
case RC_OPERATOR_EQ:
|
||||
case RC_OPERATOR_GE:
|
||||
case RC_OPERATOR_LE:
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int rc_typed_value_compare(const rc_typed_value_t* value1, const rc_typed_value_t* value2, char oper) {
|
||||
rc_typed_value_t converted_value2;
|
||||
if (value2->type != value1->type)
|
||||
value2 = rc_typed_value_convert_into(&converted_value2, value2, value1->type);
|
||||
|
||||
switch (value1->type) {
|
||||
case RC_VALUE_TYPE_UNSIGNED:
|
||||
switch (oper) {
|
||||
case RC_OPERATOR_EQ: return value1->value.u32 == value2->value.u32;
|
||||
case RC_OPERATOR_NE: return value1->value.u32 != value2->value.u32;
|
||||
case RC_OPERATOR_LT: return value1->value.u32 < value2->value.u32;
|
||||
case RC_OPERATOR_LE: return value1->value.u32 <= value2->value.u32;
|
||||
case RC_OPERATOR_GT: return value1->value.u32 > value2->value.u32;
|
||||
case RC_OPERATOR_GE: return value1->value.u32 >= value2->value.u32;
|
||||
default: return 1;
|
||||
}
|
||||
|
||||
case RC_VALUE_TYPE_SIGNED:
|
||||
switch (oper) {
|
||||
case RC_OPERATOR_EQ: return value1->value.i32 == value2->value.i32;
|
||||
case RC_OPERATOR_NE: return value1->value.i32 != value2->value.i32;
|
||||
case RC_OPERATOR_LT: return value1->value.i32 < value2->value.i32;
|
||||
case RC_OPERATOR_LE: return value1->value.i32 <= value2->value.i32;
|
||||
case RC_OPERATOR_GT: return value1->value.i32 > value2->value.i32;
|
||||
case RC_OPERATOR_GE: return value1->value.i32 >= value2->value.i32;
|
||||
default: return 1;
|
||||
}
|
||||
|
||||
case RC_VALUE_TYPE_FLOAT:
|
||||
return rc_typed_value_compare_floats(value1->value.f32, value2->value.f32, oper);
|
||||
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
849
deps/rcheevos/src/rhash/cdreader.c
vendored
Normal file
849
deps/rcheevos/src/rhash/cdreader.c
vendored
Normal file
@ -0,0 +1,849 @@
|
||||
#include "rc_hash.h"
|
||||
|
||||
#include "../rcheevos/rc_compat.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* internal helper functions in hash.c */
|
||||
extern void* rc_file_open(const char* path);
|
||||
extern void rc_file_seek(void* file_handle, int64_t offset, int origin);
|
||||
extern int64_t rc_file_tell(void* file_handle);
|
||||
extern size_t rc_file_read(void* file_handle, void* buffer, int requested_bytes);
|
||||
extern void rc_file_close(void* file_handle);
|
||||
extern int rc_hash_error(const char* message);
|
||||
extern const char* rc_path_get_filename(const char* path);
|
||||
extern int rc_path_compare_extension(const char* path, const char* ext);
|
||||
extern rc_hash_message_callback verbose_message_callback;
|
||||
|
||||
struct cdrom_t
|
||||
{
|
||||
void* file_handle; /* the file handle for reading the track data */
|
||||
int sector_size; /* the size of each sector in the track data */
|
||||
int sector_header_size; /* the offset to the raw data within a sector block */
|
||||
int64_t file_track_offset;/* the offset of the track data within the file */
|
||||
int track_first_sector; /* the first absolute sector associated to the track (includes pregap) */
|
||||
int track_pregap_sectors; /* the number of pregap sectors */
|
||||
#ifndef NDEBUG
|
||||
uint32_t track_id; /* the index of the track */
|
||||
#endif
|
||||
};
|
||||
|
||||
static int cdreader_get_sector(unsigned char header[16])
|
||||
{
|
||||
int minutes = (header[12] >> 4) * 10 + (header[12] & 0x0F);
|
||||
int seconds = (header[13] >> 4) * 10 + (header[13] & 0x0F);
|
||||
int frames = (header[14] >> 4) * 10 + (header[14] & 0x0F);
|
||||
|
||||
/* convert the MSF value to a sector index, and subtract 150 (2 seconds) per:
|
||||
* For data and mixed mode media (those conforming to ISO/IEC 10149), logical block address
|
||||
* zero shall be assigned to the block at MSF address 00/02/00 */
|
||||
return ((minutes * 60) + seconds) * 75 + frames - 150;
|
||||
}
|
||||
|
||||
static void cdreader_determine_sector_size(struct cdrom_t* cdrom)
|
||||
{
|
||||
/* Attempt to determine the sector and header sizes. The CUE file may be lying.
|
||||
* Look for the sync pattern using each of the supported sector sizes.
|
||||
* Then check for the presence of "CD001", which is gauranteed to be in either the
|
||||
* boot record or primary volume descriptor, one of which is always at sector 16.
|
||||
*/
|
||||
const unsigned char sync_pattern[] = {
|
||||
0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00
|
||||
};
|
||||
|
||||
unsigned char header[32];
|
||||
const int64_t toc_sector = 16 + cdrom->track_pregap_sectors;
|
||||
|
||||
cdrom->sector_size = 0;
|
||||
cdrom->sector_header_size = 0;
|
||||
|
||||
rc_file_seek(cdrom->file_handle, toc_sector * 2352 + cdrom->file_track_offset, SEEK_SET);
|
||||
if (rc_file_read(cdrom->file_handle, header, sizeof(header)) < sizeof(header))
|
||||
return;
|
||||
|
||||
if (memcmp(header, sync_pattern, 12) == 0)
|
||||
{
|
||||
cdrom->sector_size = 2352;
|
||||
|
||||
if (memcmp(&header[25], "CD001", 5) == 0)
|
||||
cdrom->sector_header_size = 24;
|
||||
else
|
||||
cdrom->sector_header_size = 16;
|
||||
|
||||
cdrom->track_first_sector = cdreader_get_sector(header) - (int)toc_sector;
|
||||
}
|
||||
else
|
||||
{
|
||||
rc_file_seek(cdrom->file_handle, toc_sector * 2336 + cdrom->file_track_offset, SEEK_SET);
|
||||
rc_file_read(cdrom->file_handle, header, sizeof(header));
|
||||
|
||||
if (memcmp(header, sync_pattern, 12) == 0)
|
||||
{
|
||||
cdrom->sector_size = 2336;
|
||||
|
||||
if (memcmp(&header[25], "CD001", 5) == 0)
|
||||
cdrom->sector_header_size = 24;
|
||||
else
|
||||
cdrom->sector_header_size = 16;
|
||||
|
||||
cdrom->track_first_sector = cdreader_get_sector(header) - (int)toc_sector;
|
||||
}
|
||||
else
|
||||
{
|
||||
rc_file_seek(cdrom->file_handle, toc_sector * 2048 + cdrom->file_track_offset, SEEK_SET);
|
||||
rc_file_read(cdrom->file_handle, header, sizeof(header));
|
||||
|
||||
if (memcmp(&header[1], "CD001", 5) == 0)
|
||||
{
|
||||
cdrom->sector_size = 2048;
|
||||
cdrom->sector_header_size = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void* cdreader_open_bin_track(const char* path, uint32_t track)
|
||||
{
|
||||
void* file_handle;
|
||||
struct cdrom_t* cdrom;
|
||||
|
||||
if (track > 1)
|
||||
{
|
||||
if (verbose_message_callback)
|
||||
verbose_message_callback("Cannot locate secondary tracks without a cue sheet");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
file_handle = rc_file_open(path);
|
||||
if (!file_handle)
|
||||
return NULL;
|
||||
|
||||
cdrom = (struct cdrom_t*)calloc(1, sizeof(*cdrom));
|
||||
cdrom->file_handle = file_handle;
|
||||
#ifndef NDEBUG
|
||||
cdrom->track_id = track;
|
||||
#endif
|
||||
|
||||
cdreader_determine_sector_size(cdrom);
|
||||
|
||||
if (cdrom->sector_size == 0)
|
||||
{
|
||||
size_t size;
|
||||
|
||||
rc_file_seek(cdrom->file_handle, 0, SEEK_END);
|
||||
size = ftell(cdrom->file_handle);
|
||||
|
||||
if ((size % 2352) == 0)
|
||||
{
|
||||
/* raw tracks use all 2352 bytes and have a 24 byte header */
|
||||
cdrom->sector_size = 2352;
|
||||
cdrom->sector_header_size = 24;
|
||||
}
|
||||
else if ((size % 2048) == 0)
|
||||
{
|
||||
/* cooked tracks eliminate all header/footer data */
|
||||
cdrom->sector_size = 2048;
|
||||
cdrom->sector_header_size = 0;
|
||||
}
|
||||
else if ((size % 2336) == 0)
|
||||
{
|
||||
/* MODE 2 format without 16-byte sync data */
|
||||
cdrom->sector_size = 2336;
|
||||
cdrom->sector_header_size = 8;
|
||||
}
|
||||
else
|
||||
{
|
||||
free(cdrom);
|
||||
|
||||
if (verbose_message_callback)
|
||||
verbose_message_callback("Could not determine sector size");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return cdrom;
|
||||
}
|
||||
|
||||
static int cdreader_open_bin(struct cdrom_t* cdrom, const char* path, const char* mode)
|
||||
{
|
||||
cdrom->file_handle = rc_file_open(path);
|
||||
if (!cdrom->file_handle)
|
||||
return 0;
|
||||
|
||||
/* determine sector size */
|
||||
cdreader_determine_sector_size(cdrom);
|
||||
|
||||
/* could not determine, which means we'll probably have more issues later
|
||||
* but use the CUE provided information anyway
|
||||
*/
|
||||
if (cdrom->sector_size == 0)
|
||||
{
|
||||
/* All of these modes have 2048 byte payloads. In MODE1/2352 and MODE2/2352
|
||||
* modes, the mode can actually be specified per sector to change the payload
|
||||
* size, but that reduces the ability to recover from errors when the disc
|
||||
* is damaged, so it's seldomly used, and when it is, it's mostly for audio
|
||||
* or video data where a blip or two probably won't be noticed by the user.
|
||||
* So, while we techincally support all of the following modes, we only do
|
||||
* so with 2048 byte payloads.
|
||||
* http://totalsonicmastering.com/cuesheetsyntax.htm
|
||||
* MODE1/2048 ? CDROM Mode1 Data (cooked) [no header, no footer]
|
||||
* MODE1/2352 ? CDROM Mode1 Data (raw) [16 byte header, 288 byte footer]
|
||||
* MODE2/2336 ? CDROM-XA Mode2 Data [8 byte header, 280 byte footer]
|
||||
* MODE2/2352 ? CDROM-XA Mode2 Data [24 byte header, 280 byte footer]
|
||||
*/
|
||||
if (memcmp(mode, "MODE2/2352", 10) == 0)
|
||||
{
|
||||
cdrom->sector_size = 2352;
|
||||
cdrom->sector_header_size = 24;
|
||||
}
|
||||
else if (memcmp(mode, "MODE1/2048", 10) == 0)
|
||||
{
|
||||
cdrom->sector_size = 2048;
|
||||
cdrom->sector_header_size = 0;
|
||||
}
|
||||
else if (memcmp(mode, "MODE2/2336", 10) == 0)
|
||||
{
|
||||
cdrom->sector_size = 2336;
|
||||
cdrom->sector_header_size = 8;
|
||||
}
|
||||
else if (memcmp(mode, "MODE1/2352", 10) == 0)
|
||||
{
|
||||
cdrom->sector_size = 2352;
|
||||
cdrom->sector_header_size = 16;
|
||||
}
|
||||
else if (memcmp(mode, "AUDIO", 5) == 0)
|
||||
{
|
||||
cdrom->sector_size = 2352;
|
||||
cdrom->sector_header_size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return (cdrom->sector_size != 0);
|
||||
}
|
||||
|
||||
static char* cdreader_get_bin_path(const char* cue_path, const char* bin_name)
|
||||
{
|
||||
const char* filename = rc_path_get_filename(cue_path);
|
||||
const size_t bin_name_len = strlen(bin_name);
|
||||
const size_t cue_path_len = filename - cue_path;
|
||||
const size_t needed = cue_path_len + bin_name_len + 1;
|
||||
|
||||
char* bin_filename = (char*)malloc(needed);
|
||||
if (!bin_filename)
|
||||
{
|
||||
char buffer[64];
|
||||
snprintf(buffer, sizeof(buffer), "Failed to allocate %u bytes", (unsigned)needed);
|
||||
rc_hash_error((const char*)buffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(bin_filename, cue_path, cue_path_len);
|
||||
memcpy(bin_filename + cue_path_len, bin_name, bin_name_len + 1);
|
||||
}
|
||||
|
||||
return bin_filename;
|
||||
}
|
||||
|
||||
static int64_t cdreader_get_bin_size(const char* cue_path, const char* bin_name)
|
||||
{
|
||||
int64_t size = 0;
|
||||
char* bin_filename = cdreader_get_bin_path(cue_path, bin_name);
|
||||
if (bin_filename)
|
||||
{
|
||||
/* disable verbose messaging while getting file size */
|
||||
rc_hash_message_callback old_verbose_message_callback = verbose_message_callback;
|
||||
void* file_handle;
|
||||
verbose_message_callback = NULL;
|
||||
|
||||
file_handle = rc_file_open(bin_filename);
|
||||
if (file_handle)
|
||||
{
|
||||
rc_file_seek(file_handle, 0, SEEK_END);
|
||||
size = rc_file_tell(file_handle);
|
||||
rc_file_close(file_handle);
|
||||
}
|
||||
|
||||
verbose_message_callback = old_verbose_message_callback;
|
||||
free(bin_filename);
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
static void* cdreader_open_cue_track(const char* path, uint32_t track)
|
||||
{
|
||||
void* cue_handle;
|
||||
int64_t cue_offset = 0;
|
||||
char buffer[1024];
|
||||
char* bin_filename = NULL;
|
||||
char *ptr, *ptr2, *end;
|
||||
int done = 0;
|
||||
size_t num_read = 0;
|
||||
struct cdrom_t* cdrom = NULL;
|
||||
|
||||
struct track_t
|
||||
{
|
||||
uint32_t id;
|
||||
int sector_size;
|
||||
int sector_count;
|
||||
int first_sector;
|
||||
int pregap_sectors;
|
||||
int is_data;
|
||||
int file_track_offset;
|
||||
int file_first_sector;
|
||||
char mode[16];
|
||||
char filename[256];
|
||||
} current_track, previous_track, largest_track;
|
||||
|
||||
cue_handle = rc_file_open(path);
|
||||
if (!cue_handle)
|
||||
return NULL;
|
||||
|
||||
memset(¤t_track, 0, sizeof(current_track));
|
||||
memset(&previous_track, 0, sizeof(previous_track));
|
||||
memset(&largest_track, 0, sizeof(largest_track));
|
||||
|
||||
do
|
||||
{
|
||||
num_read = rc_file_read(cue_handle, buffer, sizeof(buffer) - 1);
|
||||
if (num_read == 0)
|
||||
break;
|
||||
|
||||
buffer[num_read] = 0;
|
||||
if (num_read == sizeof(buffer) - 1)
|
||||
end = buffer + sizeof(buffer) * 3 / 4;
|
||||
else
|
||||
end = buffer + num_read;
|
||||
|
||||
for (ptr = buffer; ptr < end; ++ptr)
|
||||
{
|
||||
while (*ptr == ' ')
|
||||
++ptr;
|
||||
|
||||
if (strncasecmp(ptr, "INDEX ", 6) == 0)
|
||||
{
|
||||
int m = 0, s = 0, f = 0;
|
||||
int index;
|
||||
int sector_offset;
|
||||
|
||||
ptr += 6;
|
||||
index = atoi(ptr);
|
||||
|
||||
while (*ptr != ' ' && *ptr != '\n')
|
||||
++ptr;
|
||||
while (*ptr == ' ')
|
||||
++ptr;
|
||||
|
||||
/* convert mm:ss:ff to sector count */
|
||||
sscanf(ptr, "%d:%d:%d", &m, &s, &f);
|
||||
sector_offset = ((m * 60) + s) * 75 + f;
|
||||
|
||||
if (current_track.first_sector == -1)
|
||||
{
|
||||
current_track.first_sector = sector_offset;
|
||||
if (strcmp(current_track.filename, previous_track.filename) == 0)
|
||||
{
|
||||
previous_track.sector_count = current_track.first_sector - previous_track.first_sector;
|
||||
current_track.file_track_offset += previous_track.sector_count * previous_track.sector_size;
|
||||
}
|
||||
|
||||
/* if looking for the largest data track, determine previous track size */
|
||||
if (track == RC_HASH_CDTRACK_LARGEST && previous_track.sector_count > largest_track.sector_count &&
|
||||
previous_track.is_data)
|
||||
{
|
||||
memcpy(&largest_track, &previous_track, sizeof(largest_track));
|
||||
}
|
||||
}
|
||||
|
||||
if (index == 1)
|
||||
{
|
||||
current_track.pregap_sectors = (sector_offset - current_track.first_sector);
|
||||
|
||||
if (verbose_message_callback)
|
||||
{
|
||||
char message[128];
|
||||
char* scan = current_track.mode;
|
||||
while (*scan && !isspace((unsigned char)*scan))
|
||||
++scan;
|
||||
*scan = '\0';
|
||||
|
||||
/* it's undesirable to truncate offset to 32-bits, but %lld isn't defined in c89. */
|
||||
snprintf(message, sizeof(message), "Found %s track %d (first sector %d, sector size %d, %d pregap sectors)",
|
||||
current_track.mode, current_track.id, current_track.first_sector, current_track.sector_size, current_track.pregap_sectors);
|
||||
verbose_message_callback(message);
|
||||
}
|
||||
|
||||
if (current_track.id == track)
|
||||
{
|
||||
done = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (track == RC_HASH_CDTRACK_FIRST_DATA && current_track.is_data)
|
||||
{
|
||||
track = current_track.id;
|
||||
done = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (strncasecmp(ptr, "TRACK ", 6) == 0)
|
||||
{
|
||||
if (current_track.sector_size)
|
||||
memcpy(&previous_track, ¤t_track, sizeof(current_track));
|
||||
|
||||
ptr += 6;
|
||||
current_track.id = atoi(ptr);
|
||||
|
||||
current_track.pregap_sectors = -1;
|
||||
current_track.first_sector = -1;
|
||||
|
||||
while (*ptr != ' ')
|
||||
++ptr;
|
||||
while (*ptr == ' ')
|
||||
++ptr;
|
||||
memcpy(current_track.mode, ptr, sizeof(current_track.mode));
|
||||
current_track.is_data = (memcmp(current_track.mode, "MODE", 4) == 0);
|
||||
|
||||
if (current_track.is_data)
|
||||
{
|
||||
current_track.sector_size = atoi(ptr + 6);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* assume AUDIO */
|
||||
current_track.sector_size = 2352;
|
||||
}
|
||||
}
|
||||
else if (strncasecmp(ptr, "FILE ", 5) == 0)
|
||||
{
|
||||
if (current_track.sector_size)
|
||||
{
|
||||
memcpy(&previous_track, ¤t_track, sizeof(previous_track));
|
||||
|
||||
if (previous_track.sector_count == 0)
|
||||
{
|
||||
const uint32_t file_sector_count = (uint32_t)cdreader_get_bin_size(path, previous_track.filename) / previous_track.sector_size;
|
||||
previous_track.sector_count = file_sector_count - previous_track.first_sector;
|
||||
}
|
||||
|
||||
/* if looking for the largest data track, check to see if this one is larger */
|
||||
if (track == RC_HASH_CDTRACK_LARGEST && previous_track.is_data &&
|
||||
previous_track.sector_count > largest_track.sector_count)
|
||||
{
|
||||
memcpy(&largest_track, &previous_track, sizeof(largest_track));
|
||||
}
|
||||
}
|
||||
|
||||
memset(¤t_track, 0, sizeof(current_track));
|
||||
|
||||
current_track.file_first_sector = previous_track.file_first_sector +
|
||||
previous_track.first_sector + previous_track.sector_count;
|
||||
|
||||
ptr += 5;
|
||||
ptr2 = ptr;
|
||||
if (*ptr == '"')
|
||||
{
|
||||
++ptr;
|
||||
do
|
||||
{
|
||||
++ptr2;
|
||||
} while (*ptr2 && *ptr2 != '\n' && *ptr2 != '"');
|
||||
}
|
||||
else
|
||||
{
|
||||
do
|
||||
{
|
||||
++ptr2;
|
||||
} while (*ptr2 && *ptr2 != '\n' && *ptr2 != ' ');
|
||||
}
|
||||
|
||||
if (ptr2 - ptr < (int)sizeof(current_track.filename))
|
||||
memcpy(current_track.filename, ptr, ptr2 - ptr);
|
||||
}
|
||||
|
||||
while (*ptr && *ptr != '\n')
|
||||
++ptr;
|
||||
}
|
||||
|
||||
if (done)
|
||||
break;
|
||||
|
||||
cue_offset += (ptr - buffer);
|
||||
rc_file_seek(cue_handle, cue_offset, SEEK_SET);
|
||||
|
||||
} while (1);
|
||||
|
||||
rc_file_close(cue_handle);
|
||||
|
||||
if (track == RC_HASH_CDTRACK_LARGEST)
|
||||
{
|
||||
if (current_track.sector_size && current_track.is_data)
|
||||
{
|
||||
const uint32_t file_sector_count = (uint32_t)cdreader_get_bin_size(path, current_track.filename) / current_track.sector_size;
|
||||
current_track.sector_count = file_sector_count - current_track.first_sector;
|
||||
|
||||
if (largest_track.sector_count > current_track.sector_count)
|
||||
memcpy(¤t_track, &largest_track, sizeof(current_track));
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(¤t_track, &largest_track, sizeof(current_track));
|
||||
}
|
||||
|
||||
track = current_track.id;
|
||||
}
|
||||
else if (track == RC_HASH_CDTRACK_LAST && !done)
|
||||
{
|
||||
track = current_track.id;
|
||||
}
|
||||
|
||||
if (current_track.id == track)
|
||||
{
|
||||
cdrom = (struct cdrom_t*)calloc(1, sizeof(*cdrom));
|
||||
if (!cdrom)
|
||||
{
|
||||
snprintf((char*)buffer, sizeof(buffer), "Failed to allocate %u bytes", (unsigned)sizeof(*cdrom));
|
||||
rc_hash_error((const char*)buffer);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cdrom->file_track_offset = current_track.file_track_offset;
|
||||
cdrom->track_pregap_sectors = current_track.pregap_sectors;
|
||||
cdrom->track_first_sector = current_track.file_first_sector + current_track.first_sector;
|
||||
#ifndef NDEBUG
|
||||
cdrom->track_id = current_track.id;
|
||||
#endif
|
||||
|
||||
/* verify existance of bin file */
|
||||
bin_filename = cdreader_get_bin_path(path, current_track.filename);
|
||||
if (bin_filename)
|
||||
{
|
||||
if (cdreader_open_bin(cdrom, bin_filename, current_track.mode))
|
||||
{
|
||||
if (verbose_message_callback)
|
||||
{
|
||||
if (cdrom->track_pregap_sectors)
|
||||
snprintf((char*)buffer, sizeof(buffer), "Opened track %d (sector size %d, %d pregap sectors)",
|
||||
track, cdrom->sector_size, cdrom->track_pregap_sectors);
|
||||
else
|
||||
snprintf((char*)buffer, sizeof(buffer), "Opened track %d (sector size %d)", track, cdrom->sector_size);
|
||||
|
||||
verbose_message_callback((const char*)buffer);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cdrom->file_handle)
|
||||
{
|
||||
rc_file_close(cdrom->file_handle);
|
||||
snprintf((char*)buffer, sizeof(buffer), "Could not determine sector size for %s track", current_track.mode);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf((char*)buffer, sizeof(buffer), "Could not open %s", bin_filename);
|
||||
}
|
||||
|
||||
rc_hash_error((const char*)buffer);
|
||||
|
||||
free(cdrom);
|
||||
cdrom = NULL;
|
||||
}
|
||||
|
||||
free(bin_filename);
|
||||
}
|
||||
}
|
||||
|
||||
return cdrom;
|
||||
}
|
||||
|
||||
static void* cdreader_open_gdi_track(const char* path, uint32_t track)
|
||||
{
|
||||
void* file_handle;
|
||||
char buffer[1024];
|
||||
char mode[16] = "MODE1/";
|
||||
char sector_size[16];
|
||||
char file[256];
|
||||
int64_t track_size;
|
||||
int track_type;
|
||||
char* bin_path = "";
|
||||
uint32_t current_track = 0;
|
||||
char* ptr, *ptr2, *end;
|
||||
int lba = 0;
|
||||
|
||||
uint32_t largest_track = 0;
|
||||
int64_t largest_track_size = 0;
|
||||
char largest_track_file[256];
|
||||
char largest_track_sector_size[16];
|
||||
int largest_track_lba = 0;
|
||||
|
||||
int found = 0;
|
||||
size_t num_read = 0;
|
||||
int64_t file_offset = 0;
|
||||
struct cdrom_t* cdrom = NULL;
|
||||
|
||||
file_handle = rc_file_open(path);
|
||||
if (!file_handle)
|
||||
return NULL;
|
||||
|
||||
file[0] = '\0';
|
||||
do
|
||||
{
|
||||
num_read = rc_file_read(file_handle, buffer, sizeof(buffer) - 1);
|
||||
if (num_read == 0)
|
||||
break;
|
||||
|
||||
buffer[num_read] = 0;
|
||||
if (num_read == sizeof(buffer) - 1)
|
||||
end = buffer + sizeof(buffer) * 3 / 4;
|
||||
else
|
||||
end = buffer + num_read;
|
||||
|
||||
ptr = buffer;
|
||||
|
||||
/* the first line contains the number of tracks, so we can get the last track index from it */
|
||||
if (track == RC_HASH_CDTRACK_LAST)
|
||||
track = atoi(ptr);
|
||||
|
||||
/* first line contains the number of tracks and will be skipped */
|
||||
while (ptr < end)
|
||||
{
|
||||
/* skip until next newline */
|
||||
while (*ptr != '\n' && ptr < end)
|
||||
++ptr;
|
||||
|
||||
/* skip newlines */
|
||||
while ((*ptr == '\n' || *ptr == '\r') && ptr < end)
|
||||
++ptr;
|
||||
|
||||
/* line format: [trackid] [lba] [type] [sectorsize] [file] [?] */
|
||||
while (isspace((unsigned char)*ptr))
|
||||
++ptr;
|
||||
|
||||
current_track = (uint32_t)atoi(ptr);
|
||||
if (track && current_track != track && track != RC_HASH_CDTRACK_FIRST_DATA)
|
||||
continue;
|
||||
|
||||
while (isdigit((unsigned char)*ptr))
|
||||
++ptr;
|
||||
++ptr;
|
||||
|
||||
while (isspace((unsigned char)*ptr))
|
||||
++ptr;
|
||||
|
||||
lba = atoi(ptr);
|
||||
while (isdigit((unsigned char)*ptr))
|
||||
++ptr;
|
||||
++ptr;
|
||||
|
||||
while (isspace((unsigned char)*ptr))
|
||||
++ptr;
|
||||
|
||||
track_type = atoi(ptr);
|
||||
while (isdigit((unsigned char)*ptr))
|
||||
++ptr;
|
||||
++ptr;
|
||||
|
||||
while (isspace((unsigned char)*ptr))
|
||||
++ptr;
|
||||
|
||||
ptr2 = sector_size;
|
||||
while (isdigit((unsigned char)*ptr))
|
||||
*ptr2++ = *ptr++;
|
||||
*ptr2 = '\0';
|
||||
++ptr;
|
||||
|
||||
while (isspace((unsigned char)*ptr))
|
||||
++ptr;
|
||||
|
||||
ptr2 = file;
|
||||
if (*ptr == '\"')
|
||||
{
|
||||
++ptr;
|
||||
while (*ptr != '\"')
|
||||
*ptr2++ = *ptr++;
|
||||
++ptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (*ptr != ' ')
|
||||
*ptr2++ = *ptr++;
|
||||
}
|
||||
*ptr2 = '\0';
|
||||
|
||||
if (track == current_track)
|
||||
{
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
else if (track == RC_HASH_CDTRACK_FIRST_DATA && track_type == 4)
|
||||
{
|
||||
track = current_track;
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
else if (track == RC_HASH_CDTRACK_LARGEST && track_type == 4)
|
||||
{
|
||||
track_size = cdreader_get_bin_size(path, file);
|
||||
if (track_size > largest_track_size)
|
||||
{
|
||||
largest_track_size = track_size;
|
||||
largest_track = current_track;
|
||||
largest_track_lba = lba;
|
||||
strcpy(largest_track_file, file);
|
||||
strcpy(largest_track_sector_size, sector_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (found)
|
||||
break;
|
||||
|
||||
file_offset += (ptr - buffer);
|
||||
rc_file_seek(file_handle, file_offset, SEEK_SET);
|
||||
|
||||
} while (1);
|
||||
|
||||
rc_file_close(file_handle);
|
||||
|
||||
cdrom = (struct cdrom_t*)calloc(1, sizeof(*cdrom));
|
||||
if (!cdrom)
|
||||
{
|
||||
snprintf((char*)buffer, sizeof(buffer), "Failed to allocate %u bytes", (unsigned)sizeof(*cdrom));
|
||||
rc_hash_error((const char*)buffer);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* if we were tracking the largest track, make it the current track.
|
||||
* otherwise, current_track will be the requested track, or last track. */
|
||||
if (largest_track != 0 && largest_track != current_track)
|
||||
{
|
||||
current_track = largest_track;
|
||||
strcpy(file, largest_track_file);
|
||||
strcpy(sector_size, largest_track_sector_size);
|
||||
lba = largest_track_lba;
|
||||
}
|
||||
|
||||
/* open the bin file for the track - construct mode parameter from sector_size */
|
||||
ptr = &mode[6];
|
||||
ptr2 = sector_size;
|
||||
while (*ptr2 && *ptr2 != '\"')
|
||||
*ptr++ = *ptr2++;
|
||||
*ptr = '\0';
|
||||
|
||||
bin_path = cdreader_get_bin_path(path, file);
|
||||
if (cdreader_open_bin(cdrom, bin_path, mode))
|
||||
{
|
||||
cdrom->track_pregap_sectors = 0;
|
||||
cdrom->track_first_sector = lba;
|
||||
#ifndef NDEBUG
|
||||
cdrom->track_id = current_track;
|
||||
#endif
|
||||
|
||||
if (verbose_message_callback)
|
||||
{
|
||||
snprintf((char*)buffer, sizeof(buffer), "Opened track %d (sector size %d)", current_track, cdrom->sector_size);
|
||||
verbose_message_callback((const char*)buffer);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf((char*)buffer, sizeof(buffer), "Could not open %s", bin_path);
|
||||
rc_hash_error((const char*)buffer);
|
||||
|
||||
free(cdrom);
|
||||
cdrom = NULL;
|
||||
}
|
||||
|
||||
free(bin_path);
|
||||
|
||||
return cdrom;
|
||||
}
|
||||
|
||||
static void* cdreader_open_track(const char* path, uint32_t track)
|
||||
{
|
||||
/* backwards compatibility - 0 used to mean largest */
|
||||
if (track == 0)
|
||||
track = RC_HASH_CDTRACK_LARGEST;
|
||||
|
||||
if (rc_path_compare_extension(path, "cue"))
|
||||
return cdreader_open_cue_track(path, track);
|
||||
if (rc_path_compare_extension(path, "gdi"))
|
||||
return cdreader_open_gdi_track(path, track);
|
||||
|
||||
return cdreader_open_bin_track(path, track);
|
||||
}
|
||||
|
||||
static size_t cdreader_read_sector(void* track_handle, uint32_t sector, void* buffer, size_t requested_bytes)
|
||||
{
|
||||
int64_t sector_start;
|
||||
size_t num_read, total_read = 0;
|
||||
uint8_t* buffer_ptr = (uint8_t*)buffer;
|
||||
|
||||
struct cdrom_t* cdrom = (struct cdrom_t*)track_handle;
|
||||
if (!cdrom)
|
||||
return 0;
|
||||
|
||||
if (sector < (uint32_t)cdrom->track_first_sector)
|
||||
return 0;
|
||||
|
||||
sector_start = (int64_t)(sector - cdrom->track_first_sector) * cdrom->sector_size +
|
||||
cdrom->sector_header_size + cdrom->file_track_offset;
|
||||
|
||||
while (requested_bytes > 2048)
|
||||
{
|
||||
rc_file_seek(cdrom->file_handle, sector_start, SEEK_SET);
|
||||
num_read = rc_file_read(cdrom->file_handle, buffer_ptr, 2048);
|
||||
total_read += num_read;
|
||||
|
||||
if (num_read < 2048)
|
||||
return total_read;
|
||||
|
||||
buffer_ptr += 2048;
|
||||
sector_start += cdrom->sector_size;
|
||||
requested_bytes -= 2048;
|
||||
}
|
||||
|
||||
rc_file_seek(cdrom->file_handle, sector_start, SEEK_SET);
|
||||
num_read = rc_file_read(cdrom->file_handle, buffer_ptr, (int)requested_bytes);
|
||||
total_read += num_read;
|
||||
|
||||
return total_read;
|
||||
}
|
||||
|
||||
static void cdreader_close_track(void* track_handle)
|
||||
{
|
||||
struct cdrom_t* cdrom = (struct cdrom_t*)track_handle;
|
||||
if (cdrom)
|
||||
{
|
||||
if (cdrom->file_handle)
|
||||
rc_file_close(cdrom->file_handle);
|
||||
|
||||
free(track_handle);
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t cdreader_first_track_sector(void* track_handle)
|
||||
{
|
||||
struct cdrom_t* cdrom = (struct cdrom_t*)track_handle;
|
||||
if (cdrom)
|
||||
return cdrom->track_first_sector + cdrom->track_pregap_sectors;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void rc_hash_init_default_cdreader()
|
||||
{
|
||||
struct rc_hash_cdreader cdreader;
|
||||
|
||||
cdreader.open_track = cdreader_open_track;
|
||||
cdreader.read_sector = cdreader_read_sector;
|
||||
cdreader.close_track = cdreader_close_track;
|
||||
cdreader.first_track_sector = cdreader_first_track_sector;
|
||||
|
||||
rc_hash_init_custom_cdreader(&cdreader);
|
||||
}
|
113
deps/rcheevos/src/rhash/hash.c
vendored
113
deps/rcheevos/src/rhash/hash.c
vendored
@ -193,13 +193,13 @@ static size_t rc_cd_read_sector(void* track_handle, uint32_t sector, void* buffe
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint32_t rc_cd_absolute_sector_to_track_sector(void* track_handle, uint32_t sector)
|
||||
static uint32_t rc_cd_first_track_sector(void* track_handle)
|
||||
{
|
||||
if (cdreader && cdreader->absolute_sector_to_track_sector)
|
||||
return cdreader->absolute_sector_to_track_sector(track_handle, sector);
|
||||
if (cdreader && cdreader->first_track_sector)
|
||||
return cdreader->first_track_sector(track_handle);
|
||||
|
||||
rc_hash_error("no hook registered for cdreader_absolute_sector_to_track_sector");
|
||||
return sector;
|
||||
rc_hash_error("no hook registered for cdreader_first_track_sector");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void rc_cd_close_track(void* track_handle)
|
||||
@ -242,7 +242,7 @@ static uint32_t rc_cd_find_file_sector(void* track_handle, const char* path, uns
|
||||
else
|
||||
{
|
||||
/* find the cd information */
|
||||
if (!rc_cd_read_sector(track_handle, 16, buffer, 256))
|
||||
if (!rc_cd_read_sector(track_handle, rc_cd_first_track_sector(track_handle) + 16, buffer, 256))
|
||||
return 0;
|
||||
|
||||
/* the directory_record starts at 156, the sector containing the table of contents is 2 bytes into that.
|
||||
@ -252,7 +252,6 @@ static uint32_t rc_cd_find_file_sector(void* track_handle, const char* path, uns
|
||||
}
|
||||
|
||||
/* fetch and process the directory record */
|
||||
sector = rc_cd_absolute_sector_to_track_sector(track_handle, sector);
|
||||
if (!rc_cd_read_sector(track_handle, sector, buffer, sizeof(buffer)))
|
||||
return 0;
|
||||
|
||||
@ -405,7 +404,7 @@ static int rc_hash_cd_file(md5_state_t* md5, void* track_handle, uint32_t sector
|
||||
if (name)
|
||||
snprintf(message, sizeof(message), "Hashing %s title (%u bytes) and contents (%u bytes) ", name, (unsigned)strlen(name), size);
|
||||
else
|
||||
snprintf(message, sizeof(message), "Hashing %s contents (%u bytes)", description, size);
|
||||
snprintf(message, sizeof(message), "Hashing %s contents (%u bytes @ sector %u)", description, size, sector);
|
||||
|
||||
verbose_message_callback(message);
|
||||
}
|
||||
@ -947,7 +946,7 @@ static int rc_hash_pce_track(char hash[33], void* track_handle)
|
||||
* the string "PC Engine CD-ROM SYSTEM" should exist at 32 bytes into the sector
|
||||
* http://shu.sheldows.com/shu/download/pcedocs/pce_cdrom.html
|
||||
*/
|
||||
if (rc_cd_read_sector(track_handle, 1, buffer, 128) < 128)
|
||||
if (rc_cd_read_sector(track_handle, rc_cd_first_track_sector(track_handle) + 1, buffer, 128) < 128)
|
||||
{
|
||||
return rc_hash_error("Not a PC Engine CD");
|
||||
}
|
||||
@ -980,6 +979,7 @@ static int rc_hash_pce_track(char hash[33], void* track_handle)
|
||||
verbose_message_callback(message);
|
||||
}
|
||||
|
||||
sector += rc_cd_first_track_sector(track_handle);
|
||||
while (num_sectors > 0)
|
||||
{
|
||||
rc_cd_read_sector(track_handle, sector, buffer, sizeof(buffer));
|
||||
@ -1043,7 +1043,8 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
|
||||
return rc_hash_error("Could not open track");
|
||||
|
||||
/* PC-FX CD will have a header marker in sector 0 */
|
||||
rc_cd_read_sector(track_handle, 0, buffer, 32);
|
||||
sector = rc_cd_first_track_sector(track_handle);
|
||||
rc_cd_read_sector(track_handle, sector, buffer, 32);
|
||||
if (memcmp("PC-FX:Hu_CD-ROM", &buffer[0], 15) != 0)
|
||||
{
|
||||
rc_cd_close_track(track_handle);
|
||||
@ -1053,7 +1054,8 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
|
||||
if (!track_handle)
|
||||
return rc_hash_error("Could not open track");
|
||||
|
||||
rc_cd_read_sector(track_handle, 0, buffer, 32);
|
||||
sector = rc_cd_first_track_sector(track_handle);
|
||||
rc_cd_read_sector(track_handle, sector, buffer, 32);
|
||||
}
|
||||
|
||||
if (memcmp("PC-FX:Hu_CD-ROM", &buffer[0], 15) == 0)
|
||||
@ -1061,7 +1063,7 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
|
||||
/* PC-FX boot header fills the first two sectors of the disc
|
||||
* https://bitbucket.org/trap15/pcfxtools/src/master/pcfx-cdlink.c
|
||||
* the important stuff is the first 128 bytes of the second sector (title being the first 32) */
|
||||
rc_cd_read_sector(track_handle, 1, buffer, 128);
|
||||
rc_cd_read_sector(track_handle, sector + 1, buffer, 128);
|
||||
|
||||
md5_init(&md5);
|
||||
md5_append(&md5, buffer, 128);
|
||||
@ -1087,6 +1089,7 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
|
||||
verbose_message_callback(message);
|
||||
}
|
||||
|
||||
sector += rc_cd_first_track_sector(track_handle);
|
||||
while (num_sectors > 0)
|
||||
{
|
||||
rc_cd_read_sector(track_handle, sector, buffer, sizeof(buffer));
|
||||
@ -1099,7 +1102,7 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
|
||||
else
|
||||
{
|
||||
int result = 0;
|
||||
rc_cd_read_sector(track_handle, 1, buffer, 128);
|
||||
rc_cd_read_sector(track_handle, sector + 1, buffer, 128);
|
||||
|
||||
/* some PC-FX CDs still identify as PCE CDs */
|
||||
if (memcmp("PC Engine CD-ROM SYSTEM", &buffer[32], 23) == 0)
|
||||
@ -1119,30 +1122,41 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
|
||||
|
||||
static int rc_hash_dreamcast(char hash[33], const char* path)
|
||||
{
|
||||
uint8_t buffer[256];
|
||||
uint8_t buffer[256] = "";
|
||||
void* track_handle;
|
||||
void* last_track_handle;
|
||||
char exe_file[32] = "";
|
||||
unsigned size;
|
||||
uint32_t sector;
|
||||
uint32_t track_sector;
|
||||
int result = 0;
|
||||
md5_state_t md5;
|
||||
int i = 0;
|
||||
|
||||
/* track 03 is the data track that contains the TOC and IP.BIN */
|
||||
track_handle = rc_cd_open_track(path, 3);
|
||||
if (!track_handle)
|
||||
return rc_hash_error("Could not open track");
|
||||
|
||||
/* first 256 bytes from first sector should have IP.BIN structure that stores game meta information
|
||||
* https://mc.pp.se/dc/ip.bin.html */
|
||||
rc_cd_read_sector(track_handle, 0, buffer, sizeof(buffer));
|
||||
if (track_handle)
|
||||
{
|
||||
/* first 256 bytes from first sector should have IP.BIN structure that stores game meta information
|
||||
* https://mc.pp.se/dc/ip.bin.html */
|
||||
rc_cd_read_sector(track_handle, rc_cd_first_track_sector(track_handle), buffer, sizeof(buffer));
|
||||
}
|
||||
|
||||
if (memcmp(&buffer[0], "SEGA SEGAKATANA ", 16) != 0)
|
||||
{
|
||||
rc_cd_close_track(track_handle);
|
||||
return rc_hash_error("Not a Dreamcast CD");
|
||||
if (track_handle)
|
||||
rc_cd_close_track(track_handle);
|
||||
|
||||
/* not a gd-rom dreamcast file. check for mil-cd by looking for the marker in the first data track */
|
||||
track_handle = rc_cd_open_track(path, RC_HASH_CDTRACK_FIRST_DATA);
|
||||
if (!track_handle)
|
||||
return rc_hash_error("Could not open track");
|
||||
|
||||
rc_cd_read_sector(track_handle, rc_cd_first_track_sector(track_handle), buffer, sizeof(buffer));
|
||||
if (memcmp(&buffer[0], "SEGA SEGAKATANA ", 16) != 0)
|
||||
{
|
||||
/* did not find marker on track 3 or first data track */
|
||||
rc_cd_close_track(track_handle);
|
||||
return rc_hash_error("Not a Dreamcast CD");
|
||||
}
|
||||
}
|
||||
|
||||
/* start the hash with the game meta information */
|
||||
@ -1179,30 +1193,26 @@ static int rc_hash_dreamcast(char hash[33], const char* path)
|
||||
exe_file[i] = '\0';
|
||||
|
||||
sector = rc_cd_find_file_sector(track_handle, exe_file, &size);
|
||||
|
||||
rc_cd_close_track(track_handle);
|
||||
|
||||
if (sector == 0)
|
||||
return rc_hash_error("Could not locate boot executable");
|
||||
|
||||
/* last track contains the boot executable */
|
||||
last_track_handle = rc_cd_open_track(path, RC_HASH_CDTRACK_LAST);
|
||||
track_sector = rc_cd_absolute_sector_to_track_sector(last_track_handle, sector);
|
||||
|
||||
if ((int32_t)track_sector < 0)
|
||||
{
|
||||
/* boot executable is not in the last track; try the primary data track.
|
||||
* There's only a handful of games that do this: Q*bert was the first identified. */
|
||||
rc_cd_close_track(last_track_handle);
|
||||
|
||||
rc_hash_verbose("Boot executable not found in last track, trying primary track");
|
||||
last_track_handle = rc_cd_open_track(path, 3);
|
||||
track_sector = rc_cd_absolute_sector_to_track_sector(last_track_handle, sector);
|
||||
rc_cd_close_track(track_handle);
|
||||
return rc_hash_error("Could not locate boot executable");
|
||||
}
|
||||
|
||||
result = rc_hash_cd_file(&md5, last_track_handle, track_sector, NULL, size, "boot executable");
|
||||
if (rc_cd_read_sector(track_handle, sector, buffer, 1))
|
||||
{
|
||||
/* the boot executable is in the primary data track */
|
||||
}
|
||||
else
|
||||
{
|
||||
rc_cd_close_track(track_handle);
|
||||
|
||||
rc_cd_close_track(last_track_handle);
|
||||
/* the boot executable is normally in the last track */
|
||||
track_handle = rc_cd_open_track(path, RC_HASH_CDTRACK_LAST);
|
||||
}
|
||||
|
||||
result = rc_hash_cd_file(&md5, track_handle, sector, NULL, size, "boot executable");
|
||||
rc_cd_close_track(track_handle);
|
||||
|
||||
rc_hash_finalize(&md5, hash);
|
||||
return result;
|
||||
@ -2086,10 +2096,11 @@ void rc_hash_initialize_iterator(struct rc_hash_iterator* iterator, const char*
|
||||
{
|
||||
iterator->consoles[0] = RC_CONSOLE_PLAYSTATION;
|
||||
iterator->consoles[1] = RC_CONSOLE_PLAYSTATION_2;
|
||||
iterator->consoles[2] = RC_CONSOLE_PC_ENGINE;
|
||||
iterator->consoles[3] = RC_CONSOLE_3DO;
|
||||
iterator->consoles[4] = RC_CONSOLE_PCFX;
|
||||
iterator->consoles[5] = RC_CONSOLE_SEGA_CD; /* ASSERT: handles both Sega CD and Saturn */
|
||||
iterator->consoles[2] = RC_CONSOLE_DREAMCAST;
|
||||
iterator->consoles[3] = RC_CONSOLE_SEGA_CD; /* ASSERT: handles both Sega CD and Saturn */
|
||||
iterator->consoles[4] = RC_CONSOLE_PC_ENGINE;
|
||||
iterator->consoles[5] = RC_CONSOLE_3DO;
|
||||
iterator->consoles[6] = RC_CONSOLE_PCFX;
|
||||
need_path = 1;
|
||||
}
|
||||
else if (rc_path_compare_extension(ext, "chd"))
|
||||
@ -2097,10 +2108,10 @@ void rc_hash_initialize_iterator(struct rc_hash_iterator* iterator, const char*
|
||||
iterator->consoles[0] = RC_CONSOLE_PLAYSTATION;
|
||||
iterator->consoles[1] = RC_CONSOLE_PLAYSTATION_2;
|
||||
iterator->consoles[2] = RC_CONSOLE_DREAMCAST;
|
||||
iterator->consoles[3] = RC_CONSOLE_PC_ENGINE;
|
||||
iterator->consoles[4] = RC_CONSOLE_3DO;
|
||||
iterator->consoles[5] = RC_CONSOLE_PCFX;
|
||||
iterator->consoles[6] = RC_CONSOLE_SEGA_CD; /* ASSERT: handles both Sega CD and Saturn */
|
||||
iterator->consoles[3] = RC_CONSOLE_SEGA_CD; /* ASSERT: handles both Sega CD and Saturn */
|
||||
iterator->consoles[4] = RC_CONSOLE_PC_ENGINE;
|
||||
iterator->consoles[5] = RC_CONSOLE_3DO;
|
||||
iterator->consoles[6] = RC_CONSOLE_PCFX;
|
||||
need_path = 1;
|
||||
}
|
||||
else if (rc_path_compare_extension(ext, "col"))
|
||||
|
@ -212,6 +212,7 @@ ACHIEVEMENTS
|
||||
#include "../deps/rcheevos/src/rcheevos/runtime_progress.c"
|
||||
#include "../deps/rcheevos/src/rcheevos/trigger.c"
|
||||
#include "../deps/rcheevos/src/rcheevos/value.c"
|
||||
#include "../deps/rcheevos/src/rhash/cdreader.c"
|
||||
#include "../deps/rcheevos/src/rhash/hash.c"
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user