dep: Update rcheevos to 0e9eb7c

This commit is contained in:
Connor McLaughlin 2022-03-27 01:01:32 +10:00
parent a55b5022c7
commit 4c4e62cee6
21 changed files with 2155 additions and 715 deletions

View File

@ -1,3 +1,24 @@
# v10.2.0
* add RC_MEMSIZE_16_BITS_BE, RC_MEMSIZE_24_BITS_BE, and RC_MEMSIZE_32_BITS_BE
* add secondary flag for RC_CONDITION_MEASURED that tells the UI when to show progress as raw vs. as a percentage
* add rapi calls for fetch_leaderboard_info, fetch_achievement_info and fetch_game_list
* add hash support for RC_CONSOLE_PSP
* add RCHEEVOS_URL_SSL compile flag to use https in rurl functions
* add space to "PC Engine" label
* update RC_CONSOLE_INTELLIVISION memory map to acknowledge non-8-bit addresses
* standardize to z64 format when hashing RC_CONSOLE_N64
* prevent generating hash for PSX disc when requesting RC_CONSOLE_PLAYSTATION2
* fix wrong error message being returned when a leaderboard was only slightly malformed
# v10.1.0
* add RC_RUNTIME_EVENT_ACHIEVEMENT_UNPRIMED
* add rc_runtime_validate_addresses
* add external memory to memory map for Magnavox Odyssey 2
* fix memory map base address for NeoGeo Pocket
* fix bitcount always returning 0 when used in rich presence
# v10.0.0 # v10.0.0
* add rapi sublibrary for communicating with server (eliminates need for client-side JSON parsing; client must still * add rapi sublibrary for communicating with server (eliminates need for client-side JSON parsing; client must still

View File

@ -150,7 +150,11 @@ enum {
RC_CONSOLE_SUPERVISION = 63, RC_CONSOLE_SUPERVISION = 63,
RC_CONSOLE_SHARPX1 = 64, RC_CONSOLE_SHARPX1 = 64,
RC_CONSOLE_TIC80 = 65, RC_CONSOLE_TIC80 = 65,
RC_CONSOLE_THOMSONTO8 = 66 RC_CONSOLE_THOMSONTO8 = 66,
RC_CONSOLE_PC6000 = 67,
RC_CONSOLE_PICO = 68,
RC_CONSOLE_MEGADUCK = 69,
RC_CONSOLE_ZEEBO = 70
}; };
``` ```

View File

@ -76,6 +76,11 @@ enum {
RC_CONSOLE_SHARPX1 = 64, RC_CONSOLE_SHARPX1 = 64,
RC_CONSOLE_TIC80 = 65, RC_CONSOLE_TIC80 = 65,
RC_CONSOLE_THOMSONTO8 = 66, RC_CONSOLE_THOMSONTO8 = 66,
RC_CONSOLE_PC6000 = 67,
RC_CONSOLE_PICO = 68,
RC_CONSOLE_MEGADUCK = 69,
RC_CONSOLE_ZEEBO = 70,
RC_CONSOLE_ARDUBOY = 71,
RC_CONSOLE_HUBS = 100, RC_CONSOLE_HUBS = 100,
RC_CONSOLE_EVENTS = 101 RC_CONSOLE_EVENTS = 101

View File

@ -101,7 +101,7 @@ extern "C" {
*/ */
typedef void* (*rc_hash_cdreader_open_track_handler)(const char* path, uint32_t track); 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. * 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); typedef size_t (*rc_hash_cdreader_read_sector_handler)(void* track_handle, uint32_t sector, void* buffer, size_t requested_bytes);
@ -109,17 +109,18 @@ extern "C" {
/* closes the track handle */ /* closes the track handle */
typedef void (*rc_hash_cdreader_close_track_handler)(void* track_handle); typedef void (*rc_hash_cdreader_close_track_handler)(void* track_handle);
/* convert absolute sector to track sector */ /* gets the absolute sector index for the first sector of a track */
typedef uint32_t(*rc_hash_cdreader_absolute_sector_to_track_sector)(void* track_handle, uint32_t sector); typedef uint32_t(*rc_hash_cdreader_first_track_sector_handler)(void* track_handle);
struct rc_hash_cdreader struct rc_hash_cdreader
{ {
rc_hash_cdreader_open_track_handler open_track; rc_hash_cdreader_open_track_handler open_track;
rc_hash_cdreader_read_sector_handler read_sector; rc_hash_cdreader_read_sector_handler read_sector;
rc_hash_cdreader_close_track_handler close_track; 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_get_default_cdreader(struct rc_hash_cdreader* cdreader);
void rc_hash_init_default_cdreader(void); void rc_hash_init_default_cdreader(void);
void rc_hash_init_custom_cdreader(struct rc_hash_cdreader* reader); void rc_hash_init_custom_cdreader(struct rc_hash_cdreader* reader);

View File

@ -58,6 +58,7 @@ typedef struct rc_runtime_lboard_t {
void* buffer; void* buffer;
rc_memref_t* invalid_memref; rc_memref_t* invalid_memref;
unsigned char md5[16]; unsigned char md5[16];
int serialized_size;
char owns_memrefs; char owns_memrefs;
} }
rc_runtime_lboard_t; rc_runtime_lboard_t;
@ -66,6 +67,7 @@ typedef struct rc_runtime_richpresence_t {
rc_richpresence_t* richpresence; rc_richpresence_t* richpresence;
void* buffer; void* buffer;
struct rc_runtime_richpresence_t* previous; struct rc_runtime_richpresence_t* previous;
unsigned char md5[16];
char owns_memrefs; char owns_memrefs;
} }
rc_runtime_richpresence_t; rc_runtime_richpresence_t;

View File

@ -54,6 +54,8 @@ enum {
RC_MEMSIZE_16_BITS_BE, RC_MEMSIZE_16_BITS_BE,
RC_MEMSIZE_24_BITS_BE, RC_MEMSIZE_24_BITS_BE,
RC_MEMSIZE_32_BITS_BE, RC_MEMSIZE_32_BITS_BE,
RC_MEMSIZE_FLOAT,
RC_MEMSIZE_MBF32,
RC_MEMSIZE_VARIABLE RC_MEMSIZE_VARIABLE
}; };
@ -67,6 +69,8 @@ typedef struct rc_memref_value_t {
char size; char size;
/* True if the value changed this frame. */ /* True if the value changed this frame. */
char changed; char changed;
/* The value type of the value (for variables) */
char type;
/* True if the reference will be used in indirection. /* 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 */ * NOTE: This is actually a property of the rc_memref_t, but we put it here to save space */
char is_indirect; char is_indirect;
@ -123,7 +127,7 @@ typedef struct rc_operand_t {
} }
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 | | Conditions |
@ -280,7 +284,7 @@ struct rc_value_t {
/* The list of conditions to evaluate. */ /* The list of conditions to evaluate. */
rc_condset_t* conditions; rc_condset_t* conditions;
/* The memory references required by the value. */ /* The memory references required by the variable. */
rc_memref_t* memrefs; rc_memref_t* memrefs;
/* The name of the variable. */ /* The name of the variable. */
@ -337,7 +341,13 @@ enum {
RC_FORMAT_SCORE, RC_FORMAT_SCORE,
RC_FORMAT_VALUE, RC_FORMAT_VALUE,
RC_FORMAT_MINUTES, 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); 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); 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); 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); 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 #ifdef __cplusplus
} }

View File

@ -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 = RC_ALLOC(rc_condition_t, parse);
self->current_hits = 0; self->current_hits = 0;
self->is_true = 0; self->is_true = 0;
self->pause = 0;
if (*aux != 0 && aux[1] == ':') { if (*aux != 0 && aux[1] == ':') {
switch (*aux) { switch (*aux) {
@ -106,11 +107,6 @@ rc_condition_t* rc_parse_condition(const char** memaddr, rc_parse_state_t* parse
return 0; 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); result = rc_parse_operator(&aux);
if (result < 0) { if (result < 0) {
parse->offset = result; 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; self->operand2.value.num = 0;
} }
if (!can_modify && self->operand2.type == RC_OPERAND_FP) {
parse->offset = RC_INVALID_COMPARISON;
return 0;
}
if (*aux == '(') { if (*aux == '(') {
char* end; char* end;
self->required_hits = (unsigned)strtoul(++aux, &end, 10); 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; return self;
} }
int rc_test_condition(rc_condition_t* self, rc_eval_state_t* eval_state) { int rc_condition_is_combining(const rc_condition_t* self) {
unsigned value1 = rc_evaluate_operand(&self->operand1, eval_state) + eval_state->add_value; switch (self->type) {
unsigned value2 = rc_evaluate_operand(&self->operand2, eval_state); 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) { default:
case RC_OPERATOR_EQ: return value1 == value2; return 1;
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;
} }
} }
int rc_evaluate_condition_value(rc_condition_t* self, rc_eval_state_t* eval_state) { int rc_test_condition(rc_condition_t* self, rc_eval_state_t* eval_state) {
unsigned value = rc_evaluate_operand(&self->operand1, 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) { switch (self->oper) {
case RC_OPERATOR_MULT: case RC_OPERATOR_MULT:
if (self->operand2.type == RC_OPERAND_FP) { rc_typed_value_multiply(value, &amount);
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);
}
break; break;
case RC_OPERATOR_DIV: case RC_OPERATOR_DIV:
if (self->operand2.type == RC_OPERAND_FP) rc_typed_value_divide(value, &amount);
{
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;
}
break; break;
case RC_OPERATOR_AND: 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; break;
} }
return value;
} }

View File

@ -1,36 +1,32 @@
#include "rc_internal.h" #include "rc_internal.h"
static void rc_update_condition_pause(rc_condition_t* condition, int* in_pause) { #include <string.h> /* memcpy */
if (condition->next != 0) {
rc_update_condition_pause(condition->next, in_pause); static void rc_update_condition_pause(rc_condition_t* condition) {
rc_condition_t* subclause = condition;
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;
} }
switch (condition->type) { if (!rc_condition_is_combining(condition))
case RC_CONDITION_PAUSE_IF: subclause = condition->next;
*in_pause = condition->pause = 1;
break;
case RC_CONDITION_ADD_SOURCE: condition = condition->next;
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;
default:
*in_pause = condition->pause = 0;
break;
} }
} }
rc_condset_t* rc_parse_condset(const char** memaddr, rc_parse_state_t* parse, int is_value) { rc_condset_t* rc_parse_condset(const char** memaddr, rc_parse_state_t* parse, int is_value) {
rc_condset_t* self; rc_condset_t* self;
rc_condition_t** next; rc_condition_t** next;
int in_pause;
int in_add_address; int in_add_address;
unsigned measured_target = 0; 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) { else if (is_value) {
measured_target = (unsigned)-1; measured_target = (unsigned)-1;
if ((*next)->oper != RC_OPERATOR_NONE) 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; (*next)->required_hits = measured_target;
break;
}
} }
else if ((*next)->required_hits != 0) { else if ((*next)->required_hits != 0) {
measured_target = (*next)->required_hits; 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) { else if ((*next)->operand2.type == RC_OPERAND_CONST) {
measured_target = (*next)->operand2.value.num; measured_target = (*next)->operand2.value.num;
} }
else if ((*next)->operand2.type == RC_OPERAND_FP) {
measured_target = (unsigned)(*next)->operand2.value.dbl;
}
else { else {
parse->offset = RC_INVALID_MEASURED_TARGET; parse->offset = RC_INVALID_MEASURED_TARGET;
return 0; return 0;
@ -132,10 +143,8 @@ rc_condset_t* rc_parse_condset(const char** memaddr, rc_parse_state_t* parse, in
*next = 0; *next = 0;
if (parse->buffer != 0) { if (parse->buffer != 0)
in_pause = 0; rc_update_condition_pause(self->conditions);
rc_update_condition_pause(self->conditions, &in_pause);
}
return self; return self;
} }
@ -146,7 +155,10 @@ static void rc_condset_update_indirect_memrefs(rc_condition_t* condition, int pr
continue; continue;
if (condition->type == RC_CONDITION_ADD_ADDRESS) { 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; 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) { static int rc_test_condset_internal(rc_condset_t* self, int processing_pause, rc_eval_state_t* eval_state) {
rc_condition_t* condition; rc_condition_t* condition;
int set_valid, cond_valid, and_next, or_next, reset_next; rc_typed_value_t value;
unsigned measured_value = 0; int set_valid, cond_valid, and_next, or_next, reset_next, measured_from_hits, can_measure;
unsigned total_hits = 0; rc_typed_value_t measured_value;
int can_measure = 1, measured_from_hits = 0; unsigned total_hits;
measured_value.type = RC_VALUE_TYPE_NONE;
measured_from_hits = 0;
can_measure = 1;
total_hits = 0;
eval_state->primed = 1; eval_state->primed = 1;
set_valid = 1; set_valid = 1;
and_next = 1; and_next = 1;
or_next = 0; or_next = 0;
reset_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) { for (condition = self->conditions; condition != 0; condition = condition->next) {
if (condition->pause != processing_pause) 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 */ /* STEP 1: process modifier conditions */
switch (condition->type) { switch (condition->type) {
case RC_CONDITION_ADD_SOURCE: 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; eval_state->add_address = 0;
continue; continue;
case RC_CONDITION_SUB_SOURCE: 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; eval_state->add_address = 0;
continue; continue;
case RC_CONDITION_ADD_ADDRESS: 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; continue;
case RC_CONDITION_MEASURED: 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 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; 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 */ /* STEP 2: evaluate the current condition */
condition->is_true = (char)rc_test_condition(condition, eval_state); 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; eval_state->add_address = 0;
/* apply logic flags and reset them for the next condition */ /* 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 (condition->required_hits != 0) {
/* if there's a hit target, capture the current hits for recording Measured value later */ /* if there's a hit target, capture the current hits for recording Measured value later */
measured_from_hits = 1; 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; break;
case RC_CONDITION_MEASURED_IF: 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; can_measure = 0;
}
break; break;
case RC_CONDITION_TRIGGER: case RC_CONDITION_TRIGGER:
@ -368,11 +398,14 @@ static int rc_test_condset_internal(rc_condset_t* self, int processing_pause, rc
set_valid &= cond_valid; set_valid &= cond_valid;
} }
/* if not suppressed, update the measured value */ if (measured_value.type != RC_VALUE_TYPE_NONE) {
if (measured_value > eval_state->measured_value && can_measure) { /* if no previous Measured value was captured, or the new one is greater, keep the new one */
eval_state->measured_value = measured_value; 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; eval_state->measured_from_hits = (char)measured_from_hits;
} }
}
return set_valid; return set_valid;
} }

View File

@ -21,6 +21,9 @@ const char* rc_console_name(int console_id)
case RC_CONSOLE_ARCADE: case RC_CONSOLE_ARCADE:
return "Arcade"; return "Arcade";
case RC_CONSOLE_ARDUBOY:
return "Arduboy";
case RC_CONSOLE_ATARI_2600: case RC_CONSOLE_ATARI_2600:
return "Atari 2600"; return "Atari 2600";
@ -96,6 +99,9 @@ const char* rc_console_name(int console_id)
case RC_CONSOLE_MEGA_DRIVE: case RC_CONSOLE_MEGA_DRIVE:
return "Sega Genesis"; return "Sega Genesis";
case RC_CONSOLE_MEGADUCK:
return "Mega Duck";
case RC_CONSOLE_MS_DOS: case RC_CONSOLE_MS_DOS:
return "MS-DOS"; return "MS-DOS";
@ -126,6 +132,9 @@ const char* rc_console_name(int console_id)
case RC_CONSOLE_ORIC: case RC_CONSOLE_ORIC:
return "Oric"; return "Oric";
case RC_CONSOLE_PC6000:
return "PC-6000";
case RC_CONSOLE_PC8800: case RC_CONSOLE_PC8800:
return "PC-8000/8800"; return "PC-8000/8800";
@ -156,6 +165,9 @@ const char* rc_console_name(int console_id)
case RC_CONSOLE_SEGA_CD: case RC_CONSOLE_SEGA_CD:
return "Sega CD"; return "Sega CD";
case RC_CONSOLE_PICO:
return "Sega Pico";
case RC_CONSOLE_SATURN: case RC_CONSOLE_SATURN:
return "Sega Saturn"; return "Sega Saturn";
@ -204,6 +216,9 @@ const char* rc_console_name(int console_id)
case RC_CONSOLE_XBOX: case RC_CONSOLE_XBOX:
return "XBOX"; return "XBOX";
case RC_CONSOLE_ZEEBO:
return "Zeebo";
case RC_CONSOLE_ZX81: case RC_CONSOLE_ZX81:
return "ZX-81"; return "ZX-81";
@ -227,6 +242,20 @@ static const rc_memory_region_t _rc_memory_regions_3do[] = {
}; };
static const rc_memory_regions_t rc_memory_regions_3do = { _rc_memory_regions_3do, 1 }; static const rc_memory_regions_t rc_memory_regions_3do = { _rc_memory_regions_3do, 1 };
/* ===== Amstrad CPC ===== */
/* http://www.cpcalive.com/docs/amstrad_cpc_6128_memory_map.html */
/* https://www.cpcwiki.eu/index.php/File:AWMG_page151.jpg */
/* The original CPC only had 64KB of memory, but the newer model has 128KB (expandable to 576KB) */
/* https://www.grimware.org/doku.php/documentations/devices/gatearraydo=export_xhtml#mmr */
static const rc_memory_region_t _rc_memory_regions_amstrad_pc[] = {
{ 0x000000U, 0x00003FU, 0x000000U, RC_MEMORY_TYPE_SYSTEM_RAM, "Firmware" },
{ 0x000040U, 0x00B0FFU, 0x000040U, RC_MEMORY_TYPE_SYSTEM_RAM, "System RAM" },
{ 0x00B100U, 0x00BFFFU, 0x00B100U, RC_MEMORY_TYPE_SYSTEM_RAM, "Stack and Firmware Data" },
{ 0x00C000U, 0x00FFFFU, 0x00C000U, RC_MEMORY_TYPE_SYSTEM_RAM, "Screen Memory" },
{ 0x010000U, 0x08FFFFU, 0x010000U, RC_MEMORY_TYPE_SYSTEM_RAM, "Extended RAM" },
};
static const rc_memory_regions_t rc_memory_regions_amstrad_pc = { _rc_memory_regions_amstrad_pc, 5 };
/* ===== Apple II ===== */ /* ===== Apple II ===== */
static const rc_memory_region_t _rc_memory_regions_appleii[] = { static const rc_memory_region_t _rc_memory_regions_appleii[] = {
{ 0x000000U, 0x00FFFFU, 0x000000U, RC_MEMORY_TYPE_SYSTEM_RAM, "Main RAM" }, { 0x000000U, 0x00FFFFU, 0x000000U, RC_MEMORY_TYPE_SYSTEM_RAM, "Main RAM" },
@ -234,6 +263,21 @@ static const rc_memory_region_t _rc_memory_regions_appleii[] = {
}; };
static const rc_memory_regions_t rc_memory_regions_appleii = { _rc_memory_regions_appleii, 2 }; static const rc_memory_regions_t rc_memory_regions_appleii = { _rc_memory_regions_appleii, 2 };
/* ===== Arduboy ===== */
/* https://scienceprog.com/avr-microcontroller-memory-map/ (Atmega32) */
static const rc_memory_region_t _rc_memory_regions_arduboy[] = {
{ 0x000000U, 0x0000FFU, 0x00000000U, RC_MEMORY_TYPE_SYSTEM_RAM, "Registers" },
/* https://www.dailydot.com/debug/arduboy-kickstarter/ 2.5KB of RAM */
/* https://github.com/buserror/simavr/blob/1d227277b3d0039f9faef9ea62880ca3051b14f8/simavr/cores/avr/iom32u4.h#L1444-L1445 */
{ 0x000100U, 0x000AFFU, 0x00000100U, RC_MEMORY_TYPE_SYSTEM_RAM, "System RAM" },
/* 1KB of EEPROM https://github.com/libretro/arduous/blob/93e1a6289b42ef48de1fcfb96443981725955ad0/src/arduous/arduous.cpp#L453-L455
* https://github.com/buserror/simavr/blob/1d227277b3d0039f9faef9ea62880ca3051b14f8/simavr/cores/avr/iom32u4.h#L1450 */
/* EEPROM has it's own addressing scheme starting at $0000. I've chosen to virtualize the address
* at $80000000 to avoid a conflict */
{ 0x000B00U, 0x000EFFU, 0x80000000U, RC_MEMORY_TYPE_SAVE_RAM, "EEPROM" }
};
static const rc_memory_regions_t rc_memory_regions_arduboy = { _rc_memory_regions_arduboy, 3 };
/* ===== Atari 2600 ===== */ /* ===== Atari 2600 ===== */
static const rc_memory_region_t _rc_memory_regions_atari2600[] = { static const rc_memory_region_t _rc_memory_regions_atari2600[] = {
{ 0x000000U, 0x00007FU, 0x000080U, RC_MEMORY_TYPE_SYSTEM_RAM, "System RAM" } { 0x000000U, 0x00007FU, 0x000080U, RC_MEMORY_TYPE_SYSTEM_RAM, "System RAM" }
@ -280,6 +324,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 }; 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 ===== */ /* ===== GameBoy / GameBoy Color ===== */
static const rc_memory_region_t _rc_memory_regions_gameboy[] = { static const rc_memory_region_t _rc_memory_regions_gameboy[] = {
{ 0x000000U, 0x0000FFU, 0x000000U, RC_MEMORY_TYPE_HARDWARE_CONTROLLER, "Interrupt vector" }, { 0x000000U, 0x0000FFU, 0x000000U, RC_MEMORY_TYPE_HARDWARE_CONTROLLER, "Interrupt vector" },
@ -506,6 +557,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 }; 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 ===== */ /* ===== Pokemon Mini ===== */
/* https://www.pokemon-mini.net/documentation/memory-map/ */ /* https://www.pokemon-mini.net/documentation/memory-map/ */
static const rc_memory_region_t _rc_memory_regions_pokemini[] = { static const rc_memory_region_t _rc_memory_regions_pokemini[] = {
@ -638,9 +697,15 @@ const rc_memory_regions_t* rc_console_memory_regions(int console_id)
case RC_CONSOLE_3DO: case RC_CONSOLE_3DO:
return &rc_memory_regions_3do; return &rc_memory_regions_3do;
case RC_CONSOLE_AMSTRAD_PC:
return &rc_memory_regions_amstrad_pc;
case RC_CONSOLE_APPLE_II: case RC_CONSOLE_APPLE_II:
return &rc_memory_regions_appleii; return &rc_memory_regions_appleii;
case RC_CONSOLE_ARDUBOY:
return &rc_memory_regions_arduboy;
case RC_CONSOLE_ATARI_2600: case RC_CONSOLE_ATARI_2600:
return &rc_memory_regions_atari2600; return &rc_memory_regions_atari2600;
@ -656,6 +721,10 @@ const rc_memory_regions_t* rc_console_memory_regions(int console_id)
case RC_CONSOLE_COLECOVISION: case RC_CONSOLE_COLECOVISION:
return &rc_memory_regions_colecovision; return &rc_memory_regions_colecovision;
case RC_CONSOLE_DREAMCAST:
return &rc_memory_regions_dreamcast;
case RC_CONSOLE_MEGADUCK:
case RC_CONSOLE_GAMEBOY: case RC_CONSOLE_GAMEBOY:
return &rc_memory_regions_gameboy; return &rc_memory_regions_gameboy;
@ -716,6 +785,9 @@ const rc_memory_regions_t* rc_console_memory_regions(int console_id)
case RC_CONSOLE_PLAYSTATION_2: case RC_CONSOLE_PLAYSTATION_2:
return &rc_memory_regions_playstation2; return &rc_memory_regions_playstation2;
case RC_CONSOLE_PSP:
return &rc_memory_regions_psp;
case RC_CONSOLE_POKEMON_MINI: case RC_CONSOLE_POKEMON_MINI:
return &rc_memory_regions_pokemini; return &rc_memory_regions_pokemini;

View File

@ -11,6 +11,9 @@ int rc_parse_format(const char* format_str) {
if (!strcmp(format_str, "RAMES")) { if (!strcmp(format_str, "RAMES")) {
return RC_FORMAT_FRAMES; 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; break;
@ -18,7 +21,7 @@ int rc_parse_format(const char* format_str) {
if (!strcmp(format_str, "IME")) { if (!strcmp(format_str, "IME")) {
return RC_FORMAT_FRAMES; return RC_FORMAT_FRAMES;
} }
else if (!strcmp(format_str, "IMESECS")) { if (!strcmp(format_str, "IMESECS")) {
return RC_FORMAT_SECONDS; return RC_FORMAT_SECONDS;
} }
@ -116,40 +119,88 @@ static int rc_format_value_centiseconds(char* buffer, int size, unsigned centise
return chars; 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; int chars;
rc_typed_value_t converted_value;
memcpy(&converted_value, value, sizeof(converted_value));
switch (format) { switch (format) {
case RC_FORMAT_FRAMES: default:
/* 60 frames per second = 100 centiseconds / 60 frames; multiply frames by 100 / 60 */ case RC_FORMAT_VALUE:
chars = rc_format_value_centiseconds(buffer, size, value * 10 / 6); rc_typed_value_convert(&converted_value, RC_VALUE_TYPE_SIGNED);
chars = snprintf(buffer, size, "%d", converted_value.value.i32);
break; break;
case RC_FORMAT_SECONDS: case RC_FORMAT_FRAMES:
chars = rc_format_value_seconds(buffer, size, value); /* 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; break;
case RC_FORMAT_CENTISECS: 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; break;
case RC_FORMAT_SECONDS_AS_MINUTES: 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; break;
case RC_FORMAT_MINUTES: 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; break;
case RC_FORMAT_SCORE: 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; break;
default: case RC_FORMAT_FLOAT1:
case RC_FORMAT_VALUE: rc_typed_value_convert(&converted_value, RC_VALUE_TYPE_FLOAT);
chars = snprintf(buffer, size, "%d", value); 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; break;
} }
return chars; 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);
}

View File

@ -25,11 +25,13 @@ void rc_parse_lboard_internal(rc_lboard_t* self, const char* memaddr, rc_parse_s
return; return;
} }
found |= RC_LBOARD_START;
memaddr += 4; memaddr += 4;
if (*memaddr && *memaddr != ':') {
found |= RC_LBOARD_START;
rc_parse_trigger_internal(&self->start, &memaddr, parse); rc_parse_trigger_internal(&self->start, &memaddr, parse);
self->start.memrefs = 0; self->start.memrefs = 0;
} }
}
else if ((memaddr[0] == 'c' || memaddr[0] == 'C') && else if ((memaddr[0] == 'c' || memaddr[0] == 'C') &&
(memaddr[1] == 'a' || memaddr[1] == 'A') && (memaddr[1] == 'a' || memaddr[1] == 'A') &&
(memaddr[2] == 'n' || memaddr[2] == 'N') && memaddr[3] == ':') { (memaddr[2] == 'n' || memaddr[2] == 'N') && memaddr[3] == ':') {
@ -38,11 +40,13 @@ void rc_parse_lboard_internal(rc_lboard_t* self, const char* memaddr, rc_parse_s
return; return;
} }
found |= RC_LBOARD_CANCEL;
memaddr += 4; memaddr += 4;
if (*memaddr && *memaddr != ':') {
found |= RC_LBOARD_CANCEL;
rc_parse_trigger_internal(&self->cancel, &memaddr, parse); rc_parse_trigger_internal(&self->cancel, &memaddr, parse);
self->cancel.memrefs = 0; self->cancel.memrefs = 0;
} }
}
else if ((memaddr[0] == 's' || memaddr[0] == 'S') && else if ((memaddr[0] == 's' || memaddr[0] == 'S') &&
(memaddr[1] == 'u' || memaddr[1] == 'U') && (memaddr[1] == 'u' || memaddr[1] == 'U') &&
(memaddr[2] == 'b' || memaddr[2] == 'B') && memaddr[3] == ':') { (memaddr[2] == 'b' || memaddr[2] == 'B') && memaddr[3] == ':') {
@ -51,11 +55,13 @@ void rc_parse_lboard_internal(rc_lboard_t* self, const char* memaddr, rc_parse_s
return; return;
} }
found |= RC_LBOARD_SUBMIT;
memaddr += 4; memaddr += 4;
if (*memaddr && *memaddr != ':') {
found |= RC_LBOARD_SUBMIT;
rc_parse_trigger_internal(&self->submit, &memaddr, parse); rc_parse_trigger_internal(&self->submit, &memaddr, parse);
self->submit.memrefs = 0; self->submit.memrefs = 0;
} }
}
else if ((memaddr[0] == 'v' || memaddr[0] == 'V') && else if ((memaddr[0] == 'v' || memaddr[0] == 'V') &&
(memaddr[1] == 'a' || memaddr[1] == 'A') && (memaddr[1] == 'a' || memaddr[1] == 'A') &&
(memaddr[2] == 'l' || memaddr[2] == 'L') && memaddr[3] == ':') { (memaddr[2] == 'l' || memaddr[2] == 'L') && memaddr[3] == ':') {
@ -64,11 +70,13 @@ void rc_parse_lboard_internal(rc_lboard_t* self, const char* memaddr, rc_parse_s
return; return;
} }
found |= RC_LBOARD_VALUE;
memaddr += 4; memaddr += 4;
if (*memaddr && *memaddr != ':') {
found |= RC_LBOARD_VALUE;
rc_parse_value_internal(&self->value, &memaddr, parse); rc_parse_value_internal(&self->value, &memaddr, parse);
self->value.memrefs = 0; self->value.memrefs = 0;
} }
}
else if ((memaddr[0] == 'p' || memaddr[0] == 'P') && else if ((memaddr[0] == 'p' || memaddr[0] == 'P') &&
(memaddr[1] == 'r' || memaddr[1] == 'R') && (memaddr[1] == 'r' || memaddr[1] == 'R') &&
(memaddr[2] == 'o' || memaddr[2] == 'O') && memaddr[3] == ':') { (memaddr[2] == 'o' || memaddr[2] == 'O') && memaddr[3] == ':') {
@ -77,13 +85,15 @@ void rc_parse_lboard_internal(rc_lboard_t* self, const char* memaddr, rc_parse_s
return; return;
} }
found |= RC_LBOARD_PROGRESS;
memaddr += 4; memaddr += 4;
if (*memaddr && *memaddr != ':') {
found |= RC_LBOARD_PROGRESS;
self->progress = RC_ALLOC(rc_value_t, parse); self->progress = RC_ALLOC(rc_value_t, parse);
rc_parse_value_internal(self->progress, &memaddr, parse); rc_parse_value_internal(self->progress, &memaddr, parse);
self->progress->memrefs = 0; self->progress->memrefs = 0;
} }
}
/* encountered an error parsing one of the parts */ /* encountered an error parsing one of the parts */
if (parse->offset < 0) if (parse->offset < 0)
@ -239,6 +249,18 @@ int rc_evaluate_lboard(rc_lboard_t* self, int* value, rc_peek_t peek, void* peek
return self->state; 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) { void rc_reset_lboard(rc_lboard_t* self) {
self->state = RC_LBOARD_STATE_WAITING; self->state = RC_LBOARD_STATE_WAITING;

View File

@ -2,6 +2,7 @@
#include <stdlib.h> /* malloc/realloc */ #include <stdlib.h> /* malloc/realloc */
#include <string.h> /* memcpy */ #include <string.h> /* memcpy */
#include <math.h> /* INFINITY/NAN */
#define MEMREF_PLACEHOLDER_ADDRESS 0xFFFFFFFF #define MEMREF_PLACEHOLDER_ADDRESS 0xFFFFFFFF
@ -45,13 +46,11 @@ int rc_parse_memref(const char** memaddr, char* size, unsigned* address) {
char* end; char* end;
unsigned long value; unsigned long value;
if (*aux++ != '0') if (aux[0] == '0') {
if (aux[1] != 'x' && aux[1] != 'X')
return RC_INVALID_MEMORY_OPERAND; return RC_INVALID_MEMORY_OPERAND;
if (*aux != 'x' && *aux != 'X') aux += 2;
return RC_INVALID_MEMORY_OPERAND;
aux++;
switch (*aux++) { switch (*aux++) {
/* ordered by estimated frequency in case compiler doesn't build a jump table */ /* ordered by estimated frequency in case compiler doesn't build a jump table */
case 'h': case 'H': *size = RC_MEMSIZE_8_BITS; break; case 'h': case 'H': *size = RC_MEMSIZE_8_BITS; break;
@ -90,6 +89,20 @@ int rc_parse_memref(const char** memaddr, char* size, unsigned* address) {
default: default:
return RC_INVALID_MEMORY_OPERAND; 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); value = strtoul(aux, &end, 16);
@ -104,94 +117,212 @@ int rc_parse_memref(const char** memaddr, char* size, unsigned* address) {
return RC_OK; 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 }; 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) switch (size)
{ {
case RC_MEMSIZE_8_BITS: case RC_MEMSIZE_8_BITS:
value = (value & 0x000000ff); value->value.u32 = (value->value.u32 & 0x000000ff);
break; break;
case RC_MEMSIZE_16_BITS: case RC_MEMSIZE_16_BITS:
value = (value & 0x0000ffff); value->value.u32 = (value->value.u32 & 0x0000ffff);
break; break;
case RC_MEMSIZE_24_BITS: case RC_MEMSIZE_24_BITS:
value = (value & 0x00ffffff); value->value.u32 = (value->value.u32 & 0x00ffffff);
break; break;
case RC_MEMSIZE_32_BITS: case RC_MEMSIZE_32_BITS:
break; break;
case RC_MEMSIZE_BIT_0: case RC_MEMSIZE_BIT_0:
value = (value >> 0) & 1; value->value.u32 = (value->value.u32 >> 0) & 1;
break; break;
case RC_MEMSIZE_BIT_1: case RC_MEMSIZE_BIT_1:
value = (value >> 1) & 1; value->value.u32 = (value->value.u32 >> 1) & 1;
break; break;
case RC_MEMSIZE_BIT_2: case RC_MEMSIZE_BIT_2:
value = (value >> 2) & 1; value->value.u32 = (value->value.u32 >> 2) & 1;
break; break;
case RC_MEMSIZE_BIT_3: case RC_MEMSIZE_BIT_3:
value = (value >> 3) & 1; value->value.u32 = (value->value.u32 >> 3) & 1;
break; break;
case RC_MEMSIZE_BIT_4: case RC_MEMSIZE_BIT_4:
value = (value >> 4) & 1; value->value.u32 = (value->value.u32 >> 4) & 1;
break; break;
case RC_MEMSIZE_BIT_5: case RC_MEMSIZE_BIT_5:
value = (value >> 5) & 1; value->value.u32 = (value->value.u32 >> 5) & 1;
break; break;
case RC_MEMSIZE_BIT_6: case RC_MEMSIZE_BIT_6:
value = (value >> 6) & 1; value->value.u32 = (value->value.u32 >> 6) & 1;
break; break;
case RC_MEMSIZE_BIT_7: case RC_MEMSIZE_BIT_7:
value = (value >> 7) & 1; value->value.u32 = (value->value.u32 >> 7) & 1;
break; break;
case RC_MEMSIZE_LOW: case RC_MEMSIZE_LOW:
value = value & 0x0f; value->value.u32 = value->value.u32 & 0x0f;
break; break;
case RC_MEMSIZE_HIGH: case RC_MEMSIZE_HIGH:
value = (value >> 4) & 0x0f; value->value.u32 = (value->value.u32 >> 4) & 0x0f;
break; break;
case RC_MEMSIZE_BITCOUNT: case RC_MEMSIZE_BITCOUNT:
value = rc_bits_set[(value & 0x0F)] value->value.u32 = rc_bits_set[(value->value.u32 & 0x0F)]
+ rc_bits_set[((value >> 4) & 0x0F)]; + rc_bits_set[((value->value.u32 >> 4) & 0x0F)];
break; break;
case RC_MEMSIZE_16_BITS_BE: case RC_MEMSIZE_16_BITS_BE:
value = ((value & 0xFF00) >> 8) | value->value.u32 = ((value->value.u32 & 0xFF00) >> 8) |
((value & 0x00FF) << 8); ((value->value.u32 & 0x00FF) << 8);
break; break;
case RC_MEMSIZE_24_BITS_BE: case RC_MEMSIZE_24_BITS_BE:
value = ((value & 0xFF0000) >> 16) | value->value.u32 = ((value->value.u32 & 0xFF0000) >> 16) |
(value & 0x00FF00) | (value->value.u32 & 0x00FF00) |
((value & 0x0000FF) << 16); ((value->value.u32 & 0x0000FF) << 16);
break; break;
case RC_MEMSIZE_32_BITS_BE: case RC_MEMSIZE_32_BITS_BE:
value = ((value & 0xFF000000) >> 24) | value->value.u32 = ((value->value.u32 & 0xFF000000) >> 24) |
((value & 0x00FF0000) >> 8) | ((value->value.u32 & 0x00FF0000) >> 8) |
((value & 0x0000FF00) << 8) | ((value->value.u32 & 0x0000FF00) << 8) |
((value & 0x000000FF) << 24); ((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; break;
default: default:
break; 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 /* 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_16_BITS, /* RC_MEMSIZE_16_BITS_BE */
RC_MEMSIZE_32_BITS, /* RC_MEMSIZE_24_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_32_BITS_BE */
RC_MEMSIZE_32_BITS, /* RC_MEMSIZE_FLOAT */
RC_MEMSIZE_32_BITS, /* RC_MEMSIZE_MBF32 */
RC_MEMSIZE_32_BITS /* RC_MEMSIZE_VARIABLE */ RC_MEMSIZE_32_BITS /* RC_MEMSIZE_VARIABLE */
}; };
@ -228,35 +361,34 @@ char rc_memref_shared_size(char size) {
} }
static unsigned rc_peek_value(unsigned address, char size, rc_peek_t peek, void* ud) { static unsigned rc_peek_value(unsigned address, char size, rc_peek_t peek, void* ud) {
unsigned value;
char shared_size;
if (!peek) if (!peek)
return 0; return 0;
shared_size = rc_memref_shared_size(size); switch (size)
switch (shared_size)
{ {
case RC_MEMSIZE_8_BITS: case RC_MEMSIZE_8_BITS:
value = peek(address, 1, ud); return peek(address, 1, ud);
break;
case RC_MEMSIZE_16_BITS: case RC_MEMSIZE_16_BITS:
value = peek(address, 2, ud); return peek(address, 2, ud);
break;
case RC_MEMSIZE_32_BITS: case RC_MEMSIZE_32_BITS:
value = peek(address, 4, ud); return peek(address, 4, ud);
break;
default: default:
{
unsigned value;
const size_t index = (size_t)size;
if (index >= sizeof(rc_memref_shared_sizes) / sizeof(rc_memref_shared_sizes[0]))
return 0; return 0;
/* fetch the larger value and mask off the bits associated to the specified size
* for correct deduction of prior value. non-prior memrefs should already be using
* shared size memrefs to minimize the total number of memory reads required. */
value = rc_peek_value(address, rc_memref_shared_sizes[index], peek, ud);
return value & rc_memref_masks[index];
}
} }
if (shared_size != size)
value = rc_transform_memref_value(value, size);
return value;
} }
void rc_update_memref_value(rc_memref_value_t* memref, unsigned new_value) { void rc_update_memref_value(rc_memref_value_t* memref, unsigned new_value) {

View File

@ -103,6 +103,16 @@ static int rc_parse_operand_memory(rc_operand_t* self, const char** memaddr, rc_
return ret; return ret;
size = rc_memref_shared_size(self->size); 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); self->value.memref = rc_alloc_memref(parse, address, size, (char)is_indirect);
if (parse->offset < 0) if (parse->offset < 0)
return parse->offset; return parse->offset;
@ -142,6 +152,14 @@ int rc_parse_operand(rc_operand_t* self, const char** memaddr, int is_indirect,
break; break;
case 'f': case 'F': /* floating point constant */ 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; allow_decimal = 1;
/* fall through */ /* fall through */
case 'v': case 'V': /* signed integer constant */ case 'v': case 'V': /* signed integer constant */
@ -169,13 +187,16 @@ int rc_parse_operand(rc_operand_t* self, const char** memaddr, int is_indirect,
return RC_INVALID_FP_OPERAND; return RC_INVALID_FP_OPERAND;
do { do {
/* only keep as many digits as will fit in a 32-bit value to prevent overflow.
* float only has around 7 digits of precision anyway. */
if (shift < 1000000000) {
fraction *= 10; fraction *= 10;
fraction += (*aux - '0'); fraction += (*aux - '0');
shift *= 10; shift *= 10;
}
++aux; ++aux;
} while (*aux >= '0' && *aux <= '9'); } while (*aux >= '0' && *aux <= '9');
/* if fractional part is 0, convert to an integer constant */
if (fraction != 0) { if (fraction != 0) {
/* non-zero fractional part, convert to double and merge in integer portion */ /* non-zero fractional part, convert to double and merge in integer portion */
const double dbl_fraction = ((double)fraction) / ((double)shift); const double dbl_fraction = ((double)fraction) / ((double)shift);
@ -183,17 +204,23 @@ int rc_parse_operand(rc_operand_t* self, const char** memaddr, int is_indirect,
self->value.dbl = ((double)(-((long)value))) - dbl_fraction; self->value.dbl = ((double)(-((long)value))) - dbl_fraction;
else else
self->value.dbl = (double)value + dbl_fraction; self->value.dbl = (double)value + dbl_fraction;
}
else {
/* fractional part is 0, just convert the integer portion */
if (negative)
self->value.dbl = (double)(-((long)value));
else
self->value.dbl = (double)value;
}
self->type = RC_OPERAND_FP; self->type = RC_OPERAND_FP;
break;
} }
} else { else {
/* not a floating point value, make sure something was read and advance the read pointer */ /* not a floating point value, make sure something was read and advance the read pointer */
if (end == aux) if (end == aux)
return allow_decimal ? RC_INVALID_FP_OPERAND : RC_INVALID_CONST_OPERAND; return allow_decimal ? RC_INVALID_FP_OPERAND : RC_INVALID_CONST_OPERAND;
aux = end; aux = end;
}
if (value > 0x7fffffffU) if (value > 0x7fffffffU)
value = 0x7fffffffU; value = 0x7fffffffU;
@ -204,7 +231,7 @@ int rc_parse_operand(rc_operand_t* self, const char** memaddr, int is_indirect,
self->value.num = (unsigned)(-((long)value)); self->value.num = (unsigned)(-((long)value));
else else
self->value.num = (unsigned)value; self->value.num = (unsigned)value;
}
break; break;
case '0': case '0':
@ -269,7 +296,18 @@ static int rc_luapeek(lua_State* L) {
#endif /* RC_DISABLE_LUA */ #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) { switch (self->type) {
case RC_OPERAND_CONST: case RC_OPERAND_CONST:
case RC_OPERAND_FP: case RC_OPERAND_FP:
@ -281,60 +319,7 @@ int rc_operand_is_memref(rc_operand_t* self) {
} }
} }
unsigned rc_evaluate_operand(rc_operand_t* self, rc_eval_state_t* eval_state) { unsigned rc_transform_operand_value(unsigned value, const rc_operand_t* self) {
#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 */
switch (self->type) switch (self->type)
{ {
case RC_OPERAND_BCD: case RC_OPERAND_BCD:
@ -421,3 +406,64 @@ unsigned rc_evaluate_operand(rc_operand_t* self, rc_eval_state_t* eval_state) {
return value; 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);
}

View File

@ -65,8 +65,26 @@ typedef struct {
} }
rc_scratch_t; rc_scratch_t;
enum {
RC_VALUE_TYPE_NONE,
RC_VALUE_TYPE_UNSIGNED,
RC_VALUE_TYPE_SIGNED,
RC_VALUE_TYPE_FLOAT
};
typedef struct { 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 */ int add_hits; /* AddHits */
unsigned add_address; /* AddAddress */ unsigned add_address; /* AddAddress */
@ -74,7 +92,7 @@ typedef struct {
void* peek_userdata; void* peek_userdata;
lua_State* L; lua_State* L;
unsigned measured_value; /* Measured */ rc_typed_value_t measured_value; /* Measured */
char was_reset; /* ResetIf triggered */ char was_reset; /* ResetIf triggered */
char has_hits; /* one of more hit counts is non-zero */ char has_hits; /* one of more hit counts is non-zero */
char primed; /* true if all non-Trigger conditions are true */ 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); 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); 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); 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); void rc_parse_trigger_internal(rc_trigger_t* self, const char** memaddr, rc_parse_state_t* parse);
int rc_trigger_state_active(int state); 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); 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_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); 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); 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); 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); 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_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); 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); void rc_parse_richpresence_internal(rc_richpresence_t* self, const char* script, rc_parse_state_t* parse);

View File

@ -8,7 +8,9 @@
enum { enum {
RC_FORMAT_STRING = 101, RC_FORMAT_STRING = 101,
RC_FORMAT_LOOKUP = 102, 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) { 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; 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) { 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_t* self;
rc_richpresence_display_part_t* part; rc_richpresence_display_part_t* part;
@ -132,6 +140,8 @@ static rc_richpresence_display_t* rc_parse_richpresence_display_internal(const c
if (*ptr == '@') { if (*ptr == '@') {
/* handle macro part */ /* handle macro part */
size_t macro_len;
line = ++ptr; line = ++ptr;
while (ptr < endline && *ptr != '(') while (ptr < endline && *ptr != '(')
++ptr; ++ptr;
@ -141,36 +151,22 @@ static rc_richpresence_display_t* rc_parse_richpresence_display_internal(const c
return 0; return 0;
} }
if (ptr > line) { macro_len = 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); part = RC_ALLOC(rc_richpresence_display_part_t, parse);
memset(part, 0, sizeof(rc_richpresence_display_part_t));
*next = part; *next = part;
next = &part->next; next = &part->next;
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->text = lookup->name;
part->lookup = lookup; part->lookup = lookup;
part->display_type = lookup->format; part->display_type = lookup->format;
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));
}
break; break;
} }
@ -178,21 +174,58 @@ static rc_richpresence_display_t* rc_parse_richpresence_display_internal(const c
} }
if (!lookup) { if (!lookup) {
part = RC_ALLOC(rc_richpresence_display_part_t, parse); static const rc_richpresence_builtin_macro_t builtin_macros[] = {
memset(part, 0, sizeof(rc_richpresence_display_part_t)); {"Number", 6, RC_FORMAT_VALUE},
*next = part; {"Score", 5, RC_FORMAT_SCORE},
next = &part->next; {"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;
}
}
}
/* find the closing parenthesis */ /* find the closing parenthesis */
in = line;
line = ++ptr;
while (ptr < endline && *ptr != ')') while (ptr < endline && *ptr != ')')
++ptr; ++ptr;
if (*ptr == ')')
++ptr;
/* assert: the allocated string is going to be smaller than the memory used for the parameter of the macro */ if (*ptr != ')') {
part->display_type = RC_FORMAT_UNKNOWN_MACRO; /* non-terminated macro, dump the macro and the remaining portion of the line */
part->text = rc_alloc_str(parse, line, (int)(ptr - 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;
++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) static int rc_evaluate_richpresence_display(rc_richpresence_display_part_t* part, char* buffer, unsigned buffersize)
{ {
rc_richpresence_lookup_item_t* item; rc_richpresence_lookup_item_t* item;
rc_typed_value_t value;
char tmp[256]; char tmp[256];
char* ptr = buffer; char* ptr = buffer;
const char* text; const char* text;
size_t chars; size_t chars;
unsigned value;
*ptr = '\0'; *ptr = '\0';
while (part) { while (part) {
@ -638,14 +671,16 @@ static int rc_evaluate_richpresence_display(rc_richpresence_display_part_t* part
break; break;
case RC_FORMAT_LOOKUP: 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; text = part->lookup->default_label;
item = part->lookup->root; item = part->lookup->root;
while (item) { while (item) {
if (value > item->last) { if (value.value.u32 > item->last) {
item = item->right; item = item->right;
} }
else if (value < item->first) { else if (value.value.u32 < item->first) {
item = item->left; item = item->left;
} }
else { else {
@ -657,14 +692,86 @@ static int rc_evaluate_richpresence_display(rc_richpresence_display_part_t* part
chars = strlen(text); chars = strlen(text);
break; 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: case RC_FORMAT_UNKNOWN_MACRO:
chars = snprintf(tmp, sizeof(tmp), "[Unknown macro]%s", part->text); chars = snprintf(tmp, sizeof(tmp), "[Unknown macro]%s", part->text);
text = tmp; text = tmp;
break; break;
default: default:
value = part->value->value; rc_typed_value_from_memref_value(&value, part->value);
chars = rc_format_value(tmp, sizeof(tmp), value, part->display_type); chars = rc_format_typed_value(tmp, sizeof(tmp), &value, part->display_type);
text = tmp; text = tmp;
break; break;
} }
@ -715,3 +822,14 @@ int rc_evaluate_richpresence(rc_richpresence_t* richpresence, char* buffer, unsi
rc_update_richpresence(richpresence, peek, peek_ud, L); rc_update_richpresence(richpresence, peek, peek_ud, L);
return rc_get_richpresence_display_string(richpresence, buffer, buffersize, 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);
}

View File

@ -300,6 +300,7 @@ int rc_runtime_activate_lboard(rc_runtime_t* self, unsigned id, const char* mema
unsigned char md5[16]; unsigned char md5[16];
rc_lboard_t* lboard; rc_lboard_t* lboard;
rc_parse_state_t parse; rc_parse_state_t parse;
rc_runtime_lboard_t* runtime_lboard;
int size; int size;
unsigned i; unsigned i;
@ -378,14 +379,15 @@ int rc_runtime_activate_lboard(rc_runtime_t* self, unsigned id, const char* mema
} }
/* assign the new lboard */ /* assign the new lboard */
self->lboards[self->lboard_count].id = id; runtime_lboard = &self->lboards[self->lboard_count++];
self->lboards[self->lboard_count].value = 0; runtime_lboard->id = id;
self->lboards[self->lboard_count].lboard = lboard; runtime_lboard->value = 0;
self->lboards[self->lboard_count].buffer = lboard_buffer; runtime_lboard->lboard = lboard;
self->lboards[self->lboard_count].invalid_memref = NULL; runtime_lboard->buffer = lboard_buffer;
memcpy(self->lboards[self->lboard_count].md5, md5, 16); runtime_lboard->invalid_memref = NULL;
self->lboards[self->lboard_count].owns_memrefs = rc_runtime_allocated_memrefs(self); memcpy(runtime_lboard->md5, md5, 16);
++self->lboard_count; runtime_lboard->serialized_size = 0;
runtime_lboard->owns_memrefs = rc_runtime_allocated_memrefs(self);
/* reset it, and return it */ /* reset it, and return it */
lboard->memrefs = NULL; 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) { int rc_runtime_activate_richpresence(rc_runtime_t* self, const char* script, lua_State* L, int funcs_idx) {
rc_richpresence_t* richpresence; rc_richpresence_t* richpresence;
rc_runtime_richpresence_t* previous; rc_runtime_richpresence_t* previous;
rc_richpresence_display_t* display; rc_runtime_richpresence_t** previous_ptr;
rc_parse_state_t parse; rc_parse_state_t parse;
unsigned char md5[16];
int size; int size;
if (script == NULL) if (script == NULL)
return RC_MISSING_DISPLAY_STRING; 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); size = rc_richpresence_size(script);
if (size < 0) if (size < 0)
return size; return size;
/* if the previous script doesn't have any memrefs, free it */
previous = self->richpresence; previous = self->richpresence;
if (previous) { if (previous) {
if (!previous->owns_memrefs) { 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)); self->richpresence = (rc_runtime_richpresence_t*)malloc(sizeof(rc_runtime_richpresence_t));
if (!self->richpresence) if (!self->richpresence)
return RC_OUT_OF_MEMORY; return RC_OUT_OF_MEMORY;
self->richpresence->previous = previous; self->richpresence->previous = previous;
self->richpresence->owns_memrefs = 0; self->richpresence->owns_memrefs = 0;
memcpy(self->richpresence->md5, md5, sizeof(md5));
self->richpresence->buffer = malloc(size); self->richpresence->buffer = malloc(size);
if (!self->richpresence->buffer) if (!self->richpresence->buffer)
@ -469,11 +508,7 @@ int rc_runtime_activate_richpresence(rc_runtime_t* self, const char* script, lua
} }
else { else {
/* reset all of the conditions */ /* reset all of the conditions */
display = richpresence->first_display; rc_reset_richpresence(richpresence);
while (display != NULL) {
rc_reset_trigger(&display->trigger);
display = display->next;
}
} }
return RC_OK; return RC_OK;

View File

@ -3,12 +3,16 @@
#include "../rhash/md5.h" #include "../rhash/md5.h"
#include <stdlib.h>
#include <string.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_MEMREFS 0x4645524D /* MREF */
#define RC_RUNTIME_CHUNK_VARIABLES 0x53524156 /* VARS */
#define RC_RUNTIME_CHUNK_ACHIEVEMENT 0x56484341 /* ACHV */ #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 */
@ -27,6 +31,8 @@ typedef struct rc_runtime_progress_t {
#define RC_MEMREF_FLAG_CHANGED_THIS_FRAME 0x00010000 #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_IS_TRUE 0x00000001
#define RC_COND_FLAG_OPERAND1_IS_INDIRECT_MEMREF 0x00010000 #define RC_COND_FLAG_OPERAND1_IS_INDIRECT_MEMREF 0x00010000
#define RC_COND_FLAG_OPERAND1_MEMREF_CHANGED_THIS_FRAME 0x00020000 #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; 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) static void rc_runtime_progress_start_chunk(rc_runtime_progress_t* progress, unsigned chunk_id)
{ {
rc_runtime_progress_write_uint(progress, 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; 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; rc_condset_t* condset;
int result; int result;
@ -394,6 +562,153 @@ static int rc_runtime_progress_read_achievement(rc_runtime_progress_t* progress)
return RC_OK; 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) static int rc_runtime_progress_serialize_internal(rc_runtime_progress_t* progress)
{ {
md5_state_t state; 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) if ((result = rc_runtime_progress_write_memrefs(progress)) != RC_OK)
return result; return result;
if ((result = rc_runtime_progress_write_variables(progress)) != RC_OK)
return result;
if ((result = rc_runtime_progress_write_achievements(progress)) != RC_OK) if ((result = rc_runtime_progress_write_achievements(progress)) != RC_OK)
return result; 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, RC_RUNTIME_CHUNK_DONE);
rc_runtime_progress_write_uint(progress, 16); 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 chunk_size;
unsigned next_chunk_offset; unsigned next_chunk_offset;
unsigned i; unsigned i;
int seen_rich_presence = 0;
int result = RC_OK; int result = RC_OK;
rc_runtime_progress_init(&progress, runtime, L); 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]; rc_runtime_trigger_t* runtime_trigger = &runtime->triggers[i];
if (runtime_trigger->trigger) { if (runtime_trigger->trigger) {
/* don't update state for inactive or triggered achievements */ /* 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 /* mark active achievements as unupdated. anything that's still unupdated
* after deserializing the progress will be reset to waiting */ * after deserializing the progress will be reset to waiting */
runtime_trigger->trigger->state = RC_TRIGGER_STATE_UNUPDATED; 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 { do {
chunk_id = rc_runtime_progress_read_uint(&progress); chunk_id = rc_runtime_progress_read_uint(&progress);
chunk_size = 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); result = rc_runtime_progress_read_memrefs(&progress);
break; break;
case RC_RUNTIME_CHUNK_VARIABLES:
result = rc_runtime_progress_read_variables(&progress);
break;
case RC_RUNTIME_CHUNK_ACHIEVEMENT: case RC_RUNTIME_CHUNK_ACHIEVEMENT:
result = rc_runtime_progress_read_achievement(&progress); result = rc_runtime_progress_read_achievement(&progress);
break; 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: case RC_RUNTIME_CHUNK_DONE:
md5_init(&state); md5_init(&state);
md5_append(&state, progress.buffer, progress.offset); 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) if (trigger && trigger->state == RC_TRIGGER_STATE_UNUPDATED)
rc_reset_trigger(trigger); 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; return result;

View File

@ -96,16 +96,29 @@ int rc_trigger_state_active(int state)
} }
} }
static int rc_condset_is_measured_from_hitcount(const rc_condset_t* condset, unsigned measured_value)
{
const rc_condition_t* condition;
for (condition = condset->conditions; condition; condition = condition->next) {
if (condition->type == RC_CONDITION_MEASURED && condition->required_hits &&
condition->current_hits == measured_value) {
return 1;
}
}
return 0;
}
static void rc_reset_trigger_hitcounts(rc_trigger_t* self) { static void rc_reset_trigger_hitcounts(rc_trigger_t* self) {
rc_condset_t* condset; rc_condset_t* condset;
if (self->requirement != 0) { if (self->requirement) {
rc_reset_condset(self->requirement); rc_reset_condset(self->requirement);
} }
condset = self->alternative; condset = self->alternative;
while (condset != 0) { while (condset) {
rc_reset_condset(condset); rc_reset_condset(condset);
condset = condset->next; condset = condset->next;
} }
@ -168,7 +181,7 @@ int rc_evaluate_trigger(rc_trigger_t* self, rc_peek_t peek, void* ud, lua_State*
sub_primed |= eval_state.primed; sub_primed |= eval_state.primed;
condset = condset->next; condset = condset->next;
} while (condset != 0); } while (condset);
/* to trigger, the core must be true and at least one alt must be true */ /* to trigger, the core must be true and at least one alt must be true */
ret &= sub; ret &= sub;
@ -179,8 +192,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 paused, the measured value may not be captured, keep the old value */
if (!is_paused) if (!is_paused) {
self->measured_value = eval_state.measured_value; 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 */ /* 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 */ /* otherwise, if the state is WAITING, proceed to activating the trigger */
@ -190,13 +205,31 @@ int rc_evaluate_trigger(rc_trigger_t* self, rc_peek_t peek, void* ud, lua_State*
return RC_TRIGGER_STATE_WAITING; return RC_TRIGGER_STATE_WAITING;
} }
if (eval_state.was_reset) {
/* if any ResetIf condition was true, reset the hit counts */ /* if any ResetIf condition was true, reset the hit counts */
rc_reset_trigger_hitcounts(self); if (eval_state.was_reset) {
/* if the measured value came from a hit count, reset it. do this before calling
/* if the measured value came from a hit count, reset it too */ * rc_reset_trigger_hitcounts in case we need to call rc_condset_is_measured_from_hitcount */
if (eval_state.measured_from_hits) if (eval_state.measured_from_hits) {
self->measured_value = 0; self->measured_value = 0;
}
else if (is_paused && self->measured_value) {
/* if the measured value is in a paused group, measured_from_hits won't have been set.
* attempt to determine if it should have been */
if (self->requirement && self->requirement->is_paused &&
rc_condset_is_measured_from_hitcount(self->requirement, self->measured_value)) {
self->measured_value = 0;
}
else {
for (condset = self->alternative; condset; condset = condset->next) {
if (condset->is_paused && rc_condset_is_measured_from_hitcount(condset, self->measured_value)) {
self->measured_value = 0;
break;
}
}
}
}
rc_reset_trigger_hitcounts(self);
/* if there were hit counts to clear, return RESET, but don't change the state */ /* if there were hit counts to clear, return RESET, but don't change the state */
if (self->has_hits) { if (self->has_hits) {

View File

@ -2,6 +2,7 @@
#include <string.h> /* memset */ #include <string.h> /* memset */
#include <ctype.h> /* isdigit */ #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) { static void rc_parse_cond_value(rc_value_t* self, const char** memaddr, rc_parse_state_t* parse) {
rc_condset_t** next_clause; 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:"; char buffer[64] = "A:";
const char* buffer_ptr; const char* buffer_ptr;
char* ptr; char* ptr;
int end_of_clause;
/* convert legacy format into condset */ /* convert legacy format into condset */
self->conditions = RC_ALLOC(rc_condset_t, parse); self->conditions = RC_ALLOC(rc_condset_t, parse);
self->conditions->has_pause = 0; memset(self->conditions, 0, sizeof(rc_condset_t));
self->conditions->is_paused = 0;
next = &self->conditions->conditions; next = &self->conditions->conditions;
next_clause = &self->conditions->next; next_clause = &self->conditions->next;
for (;;) { for (;; ++(*memaddr)) {
buffer[0] = 'A'; /* reset to AddSource */
ptr = &buffer[2]; ptr = &buffer[2];
end_of_clause = 0;
do { /* extract the next clause */
for (;; ++(*memaddr)) {
switch (**memaddr) { switch (**memaddr) {
case '_': /* add next */ case '_': /* add next */
case '$': /* maximum of */ case '$': /* maximum of */
case '\0': /* end of string */ case '\0': /* end of string */
case ':': /* end of leaderboard clause */ case ':': /* end of leaderboard clause */
case ')': /* end of rich presence macro */ case ')': /* end of rich presence macro */
end_of_clause = 1;
*ptr = '\0'; *ptr = '\0';
break; break;
@ -72,24 +71,31 @@ void rc_parse_legacy_value(rc_value_t* self, const char** memaddr, rc_parse_stat
*ptr++ = '*'; *ptr++ = '*';
buffer_ptr = *memaddr + 1; buffer_ptr = *memaddr + 1;
if (*buffer_ptr == '-' || *buffer_ptr == '+') if (*buffer_ptr == '-') {
++buffer_ptr; /* ignore sign */ 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 */ /* if it looks like a floating point number, add the 'f' prefix */
while (isdigit((unsigned char)*buffer_ptr)) while (isdigit((unsigned char)*buffer_ptr))
++buffer_ptr; ++buffer_ptr;
if (*buffer_ptr == '.') if (*buffer_ptr == '.')
*ptr++ = 'f'; *ptr++ = 'f';
break; continue;
default: default:
*ptr++ = **memaddr; *ptr++ = **memaddr;
continue;
}
break; break;
} }
++(*memaddr); /* process the clause */
} while (!end_of_clause);
buffer_ptr = buffer; buffer_ptr = buffer;
cond = rc_parse_condition(&buffer_ptr, parse, 0); cond = rc_parse_condition(&buffer_ptr, parse, 0);
if (parse->offset < 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; return;
} }
cond->pause = 0;
*next = cond; *next = cond;
switch ((*memaddr)[-1]) { if (**memaddr == '_') {
case '_': /* add next */ /* add next */
next = &cond->next; next = &cond->next;
break; continue;
}
case '$': /* max of */ 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->type = RC_CONDITION_MEASURED;
cond->next = 0; 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); *next_clause = RC_ALLOC(rc_condset_t, parse);
(*next_clause)->has_pause = 0;
(*next_clause)->is_paused = 0; 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 = &(*next_clause)->conditions;
next_clause = &(*next_clause)->next; 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;
}
} }
} }
@ -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; 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_eval_state_t eval_state;
rc_condset_t* condset; rc_condset_t* condset;
int result = 0; int valid = 0;
int paused = 1;
rc_update_memref_values(self->memrefs, peek, ud); 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) { for (condset = self->conditions; condset != NULL; condset = condset->next) {
memset(&eval_state, 0, sizeof(eval_state)); memset(&eval_state, 0, sizeof(eval_state));
eval_state.peek = peek; 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); rc_reset_condset(condset);
/* if the measured value came from a hit count, reset it too */ /* if the measured value came from a hit count, reset it too */
if (eval_state.measured_from_hits) if (eval_state.measured_from_hits) {
eval_state.measured_value = 0; 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 */ /* capture the first valid measurement */
result = (int)eval_state.measured_value; memcpy(value, &eval_state.measured_value, sizeof(*value));
paused = 0; valid = 1;
} }
else { else {
/* multiple condsets are currently only used for the MAX_OF operation. /* 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. * only keep the condset's value if it's higher than the current highest value.
*/ */
if ((int)eval_state.measured_value > result) if (rc_typed_value_compare(&eval_state.measured_value, value, RC_OPERATOR_GT))
result = (int)eval_state.measured_value; 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. */ /* 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 { else {
/* when paused, the Measured value will not be captured, use the last captured value. */ /* 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) { 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) { void rc_update_variables(rc_value_t* variable, rc_peek_t peek, void* ud, lua_State* L) {
rc_typed_value_t result;
while (variable) { 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; 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;
}
}

View File

@ -19,13 +19,29 @@ extern rc_hash_message_callback verbose_message_callback;
struct cdrom_t struct cdrom_t
{ {
void* file_handle; void* file_handle; /* the file handle for reading the track data */
int sector_size; int sector_size; /* the size of each sector in the track data */
int sector_header_size; int sector_header_size; /* the offset to the raw data within a sector block */
int first_sector; int64_t file_track_offset;/* the offset of the track data within the file */
int64_t first_sector_offset; 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) static void cdreader_determine_sector_size(struct cdrom_t* cdrom)
{ {
/* Attempt to determine the sector and header sizes. The CUE file may be lying. /* Attempt to determine the sector and header sizes. The CUE file may be lying.
@ -38,12 +54,12 @@ static void cdreader_determine_sector_size(struct cdrom_t* cdrom)
}; };
unsigned char header[32]; unsigned char header[32];
const int64_t toc_sector = 16; const int64_t toc_sector = 16 + cdrom->track_pregap_sectors;
cdrom->sector_size = 0; cdrom->sector_size = 0;
cdrom->sector_header_size = 0; cdrom->sector_header_size = 0;
rc_file_seek(cdrom->file_handle, toc_sector * 2352 + cdrom->first_sector_offset, SEEK_SET); 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)) if (rc_file_read(cdrom->file_handle, header, sizeof(header)) < sizeof(header))
return; return;
@ -55,10 +71,12 @@ static void cdreader_determine_sector_size(struct cdrom_t* cdrom)
cdrom->sector_header_size = 24; cdrom->sector_header_size = 24;
else else
cdrom->sector_header_size = 16; cdrom->sector_header_size = 16;
cdrom->track_first_sector = cdreader_get_sector(header) - (int)toc_sector;
} }
else else
{ {
rc_file_seek(cdrom->file_handle, toc_sector * 2336 + cdrom->first_sector_offset, SEEK_SET); rc_file_seek(cdrom->file_handle, toc_sector * 2336 + cdrom->file_track_offset, SEEK_SET);
rc_file_read(cdrom->file_handle, header, sizeof(header)); rc_file_read(cdrom->file_handle, header, sizeof(header));
if (memcmp(header, sync_pattern, 12) == 0) if (memcmp(header, sync_pattern, 12) == 0)
@ -69,10 +87,12 @@ static void cdreader_determine_sector_size(struct cdrom_t* cdrom)
cdrom->sector_header_size = 24; cdrom->sector_header_size = 24;
else else
cdrom->sector_header_size = 16; cdrom->sector_header_size = 16;
cdrom->track_first_sector = cdreader_get_sector(header) - (int)toc_sector;
} }
else else
{ {
rc_file_seek(cdrom->file_handle, toc_sector * 2048 + cdrom->first_sector_offset, SEEK_SET); rc_file_seek(cdrom->file_handle, toc_sector * 2048 + cdrom->file_track_offset, SEEK_SET);
rc_file_read(cdrom->file_handle, header, sizeof(header)); rc_file_read(cdrom->file_handle, header, sizeof(header));
if (memcmp(&header[1], "CD001", 5) == 0) if (memcmp(&header[1], "CD001", 5) == 0)
@ -103,15 +123,18 @@ static void* cdreader_open_bin_track(const char* path, uint32_t track)
cdrom = (struct cdrom_t*)calloc(1, sizeof(*cdrom)); cdrom = (struct cdrom_t*)calloc(1, sizeof(*cdrom));
cdrom->file_handle = file_handle; cdrom->file_handle = file_handle;
#ifndef NDEBUG
cdrom->track_id = track;
#endif
cdreader_determine_sector_size(cdrom); cdreader_determine_sector_size(cdrom);
if (cdrom->sector_size == 0) if (cdrom->sector_size == 0)
{ {
size_t size; int64_t size;
rc_file_seek(cdrom->file_handle, 0, SEEK_END); rc_file_seek(cdrom->file_handle, 0, SEEK_END);
size = ftell(cdrom->file_handle); size = rc_file_tell(cdrom->file_handle);
if ((size % 2352) == 0) if ((size % 2352) == 0)
{ {
@ -192,6 +215,11 @@ static int cdreader_open_bin(struct cdrom_t* cdrom, const char* path, const char
cdrom->sector_size = 2352; cdrom->sector_size = 2352;
cdrom->sector_header_size = 16; 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); return (cdrom->sector_size != 0);
@ -226,7 +254,12 @@ static int64_t cdreader_get_bin_size(const char* cue_path, const char* bin_name)
char* bin_filename = cdreader_get_bin_path(cue_path, bin_name); char* bin_filename = cdreader_get_bin_path(cue_path, bin_name);
if (bin_filename) if (bin_filename)
{ {
void* file_handle = rc_file_open(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) if (file_handle)
{ {
rc_file_seek(file_handle, 0, SEEK_END); rc_file_seek(file_handle, 0, SEEK_END);
@ -234,6 +267,7 @@ static int64_t cdreader_get_bin_size(const char* cue_path, const char* bin_name)
rc_file_close(file_handle); rc_file_close(file_handle);
} }
verbose_message_callback = old_verbose_message_callback;
free(bin_filename); free(bin_filename);
} }
@ -242,38 +276,40 @@ static int64_t cdreader_get_bin_size(const char* cue_path, const char* bin_name)
static void* cdreader_open_cue_track(const char* path, uint32_t track) static void* cdreader_open_cue_track(const char* path, uint32_t track)
{ {
void* file_handle; void* cue_handle;
int64_t file_offset = 0; int64_t cue_offset = 0;
char buffer[1024], mode[16]; char buffer[1024];
char* bin_filename; char* bin_filename = NULL;
char file[256];
char *ptr, *ptr2, *end; char *ptr, *ptr2, *end;
int current_track = 0;
int sector_size = 0;
int track_first_sector = 0;
int previous_sector_size = 0;
int previous_index_sector_offset = 0;
int previous_track_is_data = 0;
int64_t previous_track_sector_offset = 0;
char previous_track_mode[16];
int largest_track = 0;
int largest_track_sector_count = 0;
int64_t largest_track_offset = 0;
char largest_track_mode[16];
char largest_track_file[256];
int64_t offset = 0;
int done = 0; int done = 0;
size_t num_read = 0; size_t num_read = 0;
struct cdrom_t* cdrom = NULL; struct cdrom_t* cdrom = NULL;
file_handle = rc_file_open(path); struct track_t
if (!file_handle) {
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; return NULL;
file[0] = '\0'; memset(&current_track, 0, sizeof(current_track));
memset(&previous_track, 0, sizeof(previous_track));
memset(&largest_track, 0, sizeof(largest_track));
do do
{ {
num_read = rc_file_read(file_handle, buffer, sizeof(buffer) - 1); num_read = rc_file_read(cue_handle, buffer, sizeof(buffer) - 1);
if (num_read == 0) if (num_read == 0)
break; break;
@ -305,58 +341,51 @@ static void* cdreader_open_cue_track(const char* path, uint32_t track)
/* convert mm:ss:ff to sector count */ /* convert mm:ss:ff to sector count */
sscanf(ptr, "%d:%d:%d", &m, &s, &f); sscanf(ptr, "%d:%d:%d", &m, &s, &f);
sector_offset = ((m * 60) + s) * 75 + f; sector_offset = ((m * 60) + s) * 75 + f;
sector_offset -= previous_index_sector_offset;
if (index == 1) if (current_track.first_sector == -1)
track_first_sector += sector_offset; {
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 looking for the largest data track, determine previous track size */
if (index == 1 && track == RC_HASH_CDTRACK_LARGEST && previous_track_is_data) if (track == RC_HASH_CDTRACK_LARGEST && previous_track.sector_count > largest_track.sector_count &&
previous_track.is_data)
{ {
if (sector_offset > largest_track_sector_count) memcpy(&largest_track, &previous_track, sizeof(largest_track));
{
largest_track_sector_count = sector_offset;
largest_track_offset = previous_track_sector_offset;
largest_track = current_track - 1;
memcpy(largest_track_mode, previous_track_mode, sizeof(largest_track_mode));
strcpy(largest_track_file, file);
} }
} }
/* calculate the true offset and update the counters for the next INDEX marker */
offset += (int64_t)sector_offset * previous_sector_size;
previous_sector_size = sector_size;
previous_index_sector_offset += sector_offset;
if (index == 1) if (index == 1)
{ {
current_track.pregap_sectors = (sector_offset - current_track.first_sector);
if (verbose_message_callback) if (verbose_message_callback)
{ {
char message[128]; char message[128];
char* scan = mode; char* scan = current_track.mode;
while (*scan && !isspace((unsigned char)*scan)) while (*scan && !isspace((unsigned char)*scan))
++scan; ++scan;
*scan = '\0'; *scan = '\0';
/* it's undesirable to truncate offset to 32-bits, but %lld isn't defined in c89. */ /* it's undesirable to truncate offset to 32-bits, but %lld isn't defined in c89. */
snprintf(message, sizeof(message), "Found %s track %d (sector size %d, track starts at %d)", snprintf(message, sizeof(message), "Found %s track %d (first sector %d, sector size %d, %d pregap sectors)",
mode, current_track, sector_size, (int)offset); current_track.mode, current_track.id, current_track.first_sector, current_track.sector_size, current_track.pregap_sectors);
verbose_message_callback(message); verbose_message_callback(message);
} }
if (current_track == (int)track) if (current_track.id == track)
{ {
done = 1; done = 1;
break; break;
} }
memcpy(previous_track_mode, mode, sizeof(previous_track_mode)); if (track == RC_HASH_CDTRACK_FIRST_DATA && current_track.is_data)
previous_track_is_data = (memcmp(mode, "MODE", 4) == 0);
previous_track_sector_offset = offset;
if (previous_track_is_data && track == RC_HASH_CDTRACK_FIRST_DATA)
{ {
track = current_track; track = current_track.id;
done = 1; done = 1;
break; break;
} }
@ -364,49 +393,57 @@ static void* cdreader_open_cue_track(const char* path, uint32_t track)
} }
else if (strncasecmp(ptr, "TRACK ", 6) == 0) else if (strncasecmp(ptr, "TRACK ", 6) == 0)
{ {
if (current_track.sector_size)
memcpy(&previous_track, &current_track, sizeof(current_track));
ptr += 6; ptr += 6;
current_track = atoi(ptr); current_track.id = atoi(ptr);
current_track.pregap_sectors = -1;
current_track.first_sector = -1;
while (*ptr != ' ') while (*ptr != ' ')
++ptr; ++ptr;
while (*ptr == ' ') while (*ptr == ' ')
++ptr; ++ptr;
memcpy(mode, ptr, sizeof(mode)); memcpy(current_track.mode, ptr, sizeof(current_track.mode));
current_track.is_data = (memcmp(current_track.mode, "MODE", 4) == 0);
previous_sector_size = sector_size; if (current_track.is_data)
if (memcmp(mode, "MODE", 4) == 0)
{ {
sector_size = atoi(ptr + 6); current_track.sector_size = atoi(ptr + 6);
} }
else else
{ {
/* assume AUDIO */ /* assume AUDIO */
sector_size = 2352; current_track.sector_size = 2352;
} }
} }
else if (strncasecmp(ptr, "FILE ", 5) == 0) else if (strncasecmp(ptr, "FILE ", 5) == 0)
{ {
if (previous_sector_size > 0) if (current_track.sector_size)
{ {
/* determine previous track size */ memcpy(&previous_track, &current_track, sizeof(previous_track));
int sector_count = (int)cdreader_get_bin_size(path, file) / previous_sector_size;
track_first_sector += sector_count; 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 looking for the largest data track, check to see if this one is larger */
if (track == RC_HASH_CDTRACK_LARGEST && previous_track_is_data) if (track == RC_HASH_CDTRACK_LARGEST && previous_track.is_data &&
previous_track.sector_count > largest_track.sector_count)
{ {
if (sector_count > largest_track_sector_count) memcpy(&largest_track, &previous_track, sizeof(largest_track));
{
largest_track_sector_count = sector_count;
largest_track_offset = previous_track_sector_offset;
largest_track = current_track;
memcpy(largest_track_mode, previous_track_mode, sizeof(largest_track_mode));
strcpy(largest_track_file, file);
}
} }
} }
memset(&current_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; ptr += 5;
ptr2 = ptr; ptr2 = ptr;
if (*ptr == '"') if (*ptr == '"')
@ -425,20 +462,8 @@ static void* cdreader_open_cue_track(const char* path, uint32_t track)
} while (*ptr2 && *ptr2 != '\n' && *ptr2 != ' '); } while (*ptr2 && *ptr2 != '\n' && *ptr2 != ' ');
} }
if (ptr2 - ptr < (int)sizeof(file)) if (ptr2 - ptr < (int)sizeof(current_track.filename))
{ memcpy(current_track.filename, ptr, ptr2 - ptr);
memcpy(file, ptr, ptr2 - ptr);
file[ptr2 - ptr] = '\0';
}
else
{
file[0] = '\0';
}
current_track = 0;
previous_sector_size = 0;
previous_index_sector_offset = 0;
offset = 0;
} }
while (*ptr && *ptr != '\n') while (*ptr && *ptr != '\n')
@ -448,42 +473,36 @@ static void* cdreader_open_cue_track(const char* path, uint32_t track)
if (done) if (done)
break; break;
file_offset += (ptr - buffer); cue_offset += (ptr - buffer);
rc_file_seek(file_handle, file_offset, SEEK_SET); rc_file_seek(cue_handle, cue_offset, SEEK_SET);
} while (1); } while (1);
rc_file_close(file_handle); rc_file_close(cue_handle);
if (track == RC_HASH_CDTRACK_LARGEST) if (track == RC_HASH_CDTRACK_LARGEST)
{ {
previous_track_is_data = (memcmp(mode, "MODE", 4) == 0); if (current_track.sector_size && current_track.is_data)
if (previous_track_is_data)
{ {
int sector_count = (int)cdreader_get_bin_size(path, file) / previous_sector_size; const uint32_t file_sector_count = (uint32_t)cdreader_get_bin_size(path, current_track.filename) / current_track.sector_size;
sector_count -= previous_index_sector_offset; current_track.sector_count = file_sector_count - current_track.first_sector;
if (sector_count > largest_track_sector_count) if (largest_track.sector_count > current_track.sector_count)
{ memcpy(&current_track, &largest_track, sizeof(current_track));
largest_track_sector_count = sector_count;
largest_track_offset = previous_track_sector_offset;
largest_track = current_track;
memcpy(largest_track_mode, previous_track_mode, sizeof(largest_track_mode));
strcpy(largest_track_file, file);
} }
else
{
memcpy(&current_track, &largest_track, sizeof(current_track));
} }
if (largest_track > 0) track = current_track.id;
{
current_track = largest_track;
track = (uint32_t)largest_track;
offset = largest_track_offset;
memcpy(mode, largest_track_mode, sizeof(mode));
strcpy(file, largest_track_file);
} }
else if (track == RC_HASH_CDTRACK_LAST && !done)
{
track = current_track.id;
} }
if (current_track == (int)track) if (current_track.id == track)
{ {
cdrom = (struct cdrom_t*)calloc(1, sizeof(*cdrom)); cdrom = (struct cdrom_t*)calloc(1, sizeof(*cdrom));
if (!cdrom) if (!cdrom)
@ -493,20 +512,24 @@ static void* cdreader_open_cue_track(const char* path, uint32_t track)
return NULL; return NULL;
} }
cdrom->first_sector_offset = offset; cdrom->file_track_offset = current_track.file_track_offset;
cdrom->first_sector = track_first_sector; 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 */ /* verify existance of bin file */
bin_filename = cdreader_get_bin_path(path, file); bin_filename = cdreader_get_bin_path(path, current_track.filename);
if (bin_filename) if (bin_filename)
{ {
if (cdreader_open_bin(cdrom, bin_filename, mode)) if (cdreader_open_bin(cdrom, bin_filename, current_track.mode))
{ {
if (verbose_message_callback) if (verbose_message_callback)
{ {
if (cdrom->first_sector_offset) if (cdrom->track_pregap_sectors)
snprintf((char*)buffer, sizeof(buffer), "Opened track %d (sector size %d, track starts at %d)", snprintf((char*)buffer, sizeof(buffer), "Opened track %d (sector size %d, %d pregap sectors)",
track, cdrom->sector_size, (int)cdrom->first_sector_offset); track, cdrom->sector_size, cdrom->track_pregap_sectors);
else else
snprintf((char*)buffer, sizeof(buffer), "Opened track %d (sector size %d)", track, cdrom->sector_size); snprintf((char*)buffer, sizeof(buffer), "Opened track %d (sector size %d)", track, cdrom->sector_size);
@ -514,8 +537,17 @@ static void* cdreader_open_cue_track(const char* path, uint32_t track)
} }
} }
else 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); snprintf((char*)buffer, sizeof(buffer), "Could not open %s", bin_filename);
}
rc_hash_error((const char*)buffer); rc_hash_error((const char*)buffer);
free(cdrom); free(cdrom);
@ -593,7 +625,7 @@ static void* cdreader_open_gdi_track(const char* path, uint32_t track)
++ptr; ++ptr;
current_track = (uint32_t)atoi(ptr); current_track = (uint32_t)atoi(ptr);
if (track && current_track != track) if (track && current_track != track && track != RC_HASH_CDTRACK_FIRST_DATA)
continue; continue;
while (isdigit((unsigned char)*ptr)) while (isdigit((unsigned char)*ptr))
@ -643,11 +675,17 @@ static void* cdreader_open_gdi_track(const char* path, uint32_t track)
} }
*ptr2 = '\0'; *ptr2 = '\0';
if (track == current_track || (track == RC_HASH_CDTRACK_FIRST_DATA && track_type == 4)) if (track == current_track)
{ {
found = 1; found = 1;
break; 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) else if (track == RC_HASH_CDTRACK_LARGEST && track_type == 4)
{ {
track_size = cdreader_get_bin_size(path, file); track_size = cdreader_get_bin_size(path, file);
@ -700,12 +738,15 @@ static void* cdreader_open_gdi_track(const char* path, uint32_t track)
bin_path = cdreader_get_bin_path(path, file); bin_path = cdreader_get_bin_path(path, file);
if (cdreader_open_bin(cdrom, bin_path, mode)) if (cdreader_open_bin(cdrom, bin_path, mode))
{ {
cdrom->first_sector_offset = 0; cdrom->track_pregap_sectors = 0;
cdrom->first_sector = lba; cdrom->track_first_sector = lba;
#ifndef NDEBUG
cdrom->track_id = current_track;
#endif
if (verbose_message_callback) if (verbose_message_callback)
{ {
snprintf((char*)buffer, sizeof(buffer), "Opened track %d (sector size %d)", track, cdrom->sector_size); snprintf((char*)buffer, sizeof(buffer), "Opened track %d (sector size %d)", current_track, cdrom->sector_size);
verbose_message_callback((const char*)buffer); verbose_message_callback((const char*)buffer);
} }
} }
@ -747,7 +788,11 @@ static size_t cdreader_read_sector(void* track_handle, uint32_t sector, void* bu
if (!cdrom) if (!cdrom)
return 0; return 0;
sector_start = (int64_t)sector * cdrom->sector_size + cdrom->sector_header_size + cdrom->first_sector_offset; 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) while (requested_bytes > 2048)
{ {
@ -782,23 +827,26 @@ static void cdreader_close_track(void* track_handle)
} }
} }
static uint32_t cdreader_absolute_sector_to_track_sector(void* track_handle, uint32_t sector) static uint32_t cdreader_first_track_sector(void* track_handle)
{ {
struct cdrom_t* cdrom = (struct cdrom_t*)track_handle; struct cdrom_t* cdrom = (struct cdrom_t*)track_handle;
if (cdrom) if (cdrom)
return sector - cdrom->first_sector; return cdrom->track_first_sector + cdrom->track_pregap_sectors;
return 0; return 0;
} }
void rc_hash_get_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;
}
void rc_hash_init_default_cdreader() void rc_hash_init_default_cdreader()
{ {
struct rc_hash_cdreader cdreader; struct rc_hash_cdreader cdreader;
rc_hash_get_default_cdreader(&cdreader);
cdreader.open_track = cdreader_open_track;
cdreader.read_sector = cdreader_read_sector;
cdreader.close_track = cdreader_close_track;
cdreader.absolute_sector_to_track_sector = cdreader_absolute_sector_to_track_sector;
rc_hash_init_custom_cdreader(&cdreader); rc_hash_init_custom_cdreader(&cdreader);
} }

View File

@ -83,6 +83,12 @@ static void filereader_close(void* file_handle)
fclose((FILE*)file_handle); fclose((FILE*)file_handle);
} }
/* for unit tests - normally would call rc_hash_init_custom_filereader(NULL) */
void rc_hash_reset_filereader()
{
filereader = NULL;
}
void rc_hash_init_custom_filereader(struct rc_hash_filereader* reader) void rc_hash_init_custom_filereader(struct rc_hash_filereader* reader)
{ {
/* initialize with defaults first */ /* initialize with defaults first */
@ -193,13 +199,13 @@ static size_t rc_cd_read_sector(void* track_handle, uint32_t sector, void* buffe
return 0; 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) if (cdreader && cdreader->first_track_sector)
return cdreader->absolute_sector_to_track_sector(track_handle, sector); return cdreader->first_track_sector(track_handle);
rc_hash_error("no hook registered for cdreader_absolute_sector_to_track_sector"); rc_hash_error("no hook registered for cdreader_first_track_sector");
return sector; return 0;
} }
static void rc_cd_close_track(void* track_handle) static void rc_cd_close_track(void* track_handle)
@ -242,7 +248,7 @@ static uint32_t rc_cd_find_file_sector(void* track_handle, const char* path, uns
else else
{ {
/* find the cd information */ /* 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; return 0;
/* the directory_record starts at 156, the sector containing the table of contents is 2 bytes into that. /* the directory_record starts at 156, the sector containing the table of contents is 2 bytes into that.
@ -252,7 +258,6 @@ static uint32_t rc_cd_find_file_sector(void* track_handle, const char* path, uns
} }
/* fetch and process the directory record */ /* 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))) if (!rc_cd_read_sector(track_handle, sector, buffer, sizeof(buffer)))
return 0; return 0;
@ -405,7 +410,7 @@ static int rc_hash_cd_file(md5_state_t* md5, void* track_handle, uint32_t sector
if (name) if (name)
snprintf(message, sizeof(message), "Hashing %s title (%u bytes) and contents (%u bytes) ", name, (unsigned)strlen(name), size); snprintf(message, sizeof(message), "Hashing %s title (%u bytes) and contents (%u bytes) ", name, (unsigned)strlen(name), size);
else 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); verbose_message_callback(message);
} }
@ -648,6 +653,37 @@ static int rc_hash_arcade(char hash[33], const char* path)
return rc_hash_buffer(hash, (uint8_t*)filename, filename_length); return rc_hash_buffer(hash, (uint8_t*)filename, filename_length);
} }
static int rc_hash_text(char hash[33], const uint8_t* buffer, size_t buffer_size)
{
md5_state_t md5;
const uint8_t* scan = buffer;
const uint8_t* stop = buffer + buffer_size;
md5_init(&md5);
do {
/* find end of line */
while (scan < stop && *scan != '\r' && *scan != '\n')
++scan;
md5_append(&md5, buffer, (int)(scan - buffer));
/* include a normalized line ending */
/* NOTE: this causes a line ending to be hashed at the end of the file, even if one was not present */
md5_append(&md5, (const uint8_t*)"\n", 1);
/* skip newline */
if (scan < stop && *scan == '\r')
++scan;
if (scan < stop && *scan == '\n')
++scan;
buffer = scan;
} while (scan < stop);
return rc_hash_finalize(&md5, hash);
}
static int rc_hash_lynx(char hash[33], const uint8_t* buffer, size_t buffer_size) static int rc_hash_lynx(char hash[33], const uint8_t* buffer, size_t buffer_size)
{ {
/* if the file contains a header, ignore it */ /* if the file contains a header, ignore it */
@ -711,86 +747,7 @@ static void rc_hash_n64_to_z64(uint8_t* buffer, const uint8_t* stop)
} }
} }
static int rc_hash_n64(char hash[33], const uint8_t* buffer, size_t buffer_size) static int rc_hash_n64(char hash[33], const char* path)
{
uint8_t* swapbuffer;
uint8_t* stop;
const size_t swapbuffer_size = 65536;
md5_state_t md5;
size_t remaining;
int is_v64;
if (buffer[0] == 0x80) /* z64 format (big endian [native]) */
{
return rc_hash_buffer(hash, buffer, buffer_size);
}
else if (buffer[0] == 0x37) /* v64 format (byteswapped) */
{
rc_hash_verbose("converting v64 to z64");
is_v64 = 1;
}
else if (buffer[0] == 0x40) /* n64 format (little endian) */
{
rc_hash_verbose("converting n64 to z64");
is_v64 = 0;
}
else
{
rc_hash_verbose("Not a Nintendo 64 ROM");
return 0;
}
swapbuffer = (uint8_t*)malloc(swapbuffer_size);
if (!swapbuffer)
return rc_hash_error("Could not allocate temporary buffer");
stop = swapbuffer + swapbuffer_size;
md5_init(&md5);
if (buffer_size > MAX_BUFFER_SIZE)
remaining = MAX_BUFFER_SIZE;
else
remaining = (size_t)buffer_size;
if (verbose_message_callback)
{
char message[64];
snprintf(message, sizeof(message), "Hashing %u bytes", (unsigned)remaining);
verbose_message_callback(message);
}
while (remaining >= swapbuffer_size)
{
memcpy(swapbuffer, buffer, swapbuffer_size);
if (is_v64)
rc_hash_v64_to_z64(swapbuffer, stop);
else
rc_hash_n64_to_z64(swapbuffer, stop);
md5_append(&md5, swapbuffer, (int)swapbuffer_size);
buffer += swapbuffer_size;
remaining -= swapbuffer_size;
}
if (remaining > 0)
{
memcpy(swapbuffer, buffer, remaining);
stop = swapbuffer + remaining;
if (is_v64)
rc_hash_v64_to_z64(swapbuffer, stop);
else
rc_hash_n64_to_z64(swapbuffer, stop);
md5_append(&md5, swapbuffer, (int)remaining);
}
free(swapbuffer);
return rc_hash_finalize(&md5, hash);
}
static int rc_hash_n64_file(char hash[33], const char* path)
{ {
uint8_t* buffer; uint8_t* buffer;
uint8_t* stop; uint8_t* stop;
@ -1026,7 +983,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 * 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 * 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"); return rc_hash_error("Not a PC Engine CD");
} }
@ -1059,6 +1016,7 @@ static int rc_hash_pce_track(char hash[33], void* track_handle)
verbose_message_callback(message); verbose_message_callback(message);
} }
sector += rc_cd_first_track_sector(track_handle);
while (num_sectors > 0) while (num_sectors > 0)
{ {
rc_cd_read_sector(track_handle, sector, buffer, sizeof(buffer)); rc_cd_read_sector(track_handle, sector, buffer, sizeof(buffer));
@ -1122,7 +1080,8 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
return rc_hash_error("Could not open track"); return rc_hash_error("Could not open track");
/* PC-FX CD will have a header marker in sector 0 */ /* 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) if (memcmp("PC-FX:Hu_CD-ROM", &buffer[0], 15) != 0)
{ {
rc_cd_close_track(track_handle); rc_cd_close_track(track_handle);
@ -1132,7 +1091,8 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
if (!track_handle) if (!track_handle)
return rc_hash_error("Could not open track"); 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) if (memcmp("PC-FX:Hu_CD-ROM", &buffer[0], 15) == 0)
@ -1140,7 +1100,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 /* PC-FX boot header fills the first two sectors of the disc
* https://bitbucket.org/trap15/pcfxtools/src/master/pcfx-cdlink.c * 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) */ * 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_init(&md5);
md5_append(&md5, buffer, 128); md5_append(&md5, buffer, 128);
@ -1166,6 +1126,7 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
verbose_message_callback(message); verbose_message_callback(message);
} }
sector += rc_cd_first_track_sector(track_handle);
while (num_sectors > 0) while (num_sectors > 0)
{ {
rc_cd_read_sector(track_handle, sector, buffer, sizeof(buffer)); rc_cd_read_sector(track_handle, sector, buffer, sizeof(buffer));
@ -1178,7 +1139,7 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
else else
{ {
int result = 0; 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 */ /* some PC-FX CDs still identify as PCE CDs */
if (memcmp("PC Engine CD-ROM SYSTEM", &buffer[32], 23) == 0) if (memcmp("PC Engine CD-ROM SYSTEM", &buffer[32], 23) == 0)
@ -1198,31 +1159,42 @@ static int rc_hash_pcfx_cd(char hash[33], const char* path)
static int rc_hash_dreamcast(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* track_handle;
void* last_track_handle;
char exe_file[32] = ""; char exe_file[32] = "";
unsigned size; unsigned size;
uint32_t sector; uint32_t sector;
uint32_t track_sector;
int result = 0; int result = 0;
md5_state_t md5; md5_state_t md5;
int i = 0; int i = 0;
/* track 03 is the data track that contains the TOC and IP.BIN */ /* track 03 is the data track that contains the TOC and IP.BIN */
track_handle = rc_cd_open_track(path, 3); track_handle = rc_cd_open_track(path, 3);
if (!track_handle) 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 /* first 256 bytes from first sector should have IP.BIN structure that stores game meta information
* https://mc.pp.se/dc/ip.bin.html */ * https://mc.pp.se/dc/ip.bin.html */
rc_cd_read_sector(track_handle, 0, buffer, sizeof(buffer)); rc_cd_read_sector(track_handle, rc_cd_first_track_sector(track_handle), buffer, sizeof(buffer));
}
if (memcmp(&buffer[0], "SEGA SEGAKATANA ", 16) != 0) if (memcmp(&buffer[0], "SEGA SEGAKATANA ", 16) != 0)
{ {
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); rc_cd_close_track(track_handle);
return rc_hash_error("Not a Dreamcast CD"); return rc_hash_error("Not a Dreamcast CD");
} }
}
/* start the hash with the game meta information */ /* start the hash with the game meta information */
md5_init(&md5); md5_init(&md5);
@ -1258,30 +1230,26 @@ static int rc_hash_dreamcast(char hash[33], const char* path)
exe_file[i] = '\0'; exe_file[i] = '\0';
sector = rc_cd_find_file_sector(track_handle, exe_file, &size); sector = rc_cd_find_file_sector(track_handle, exe_file, &size);
rc_cd_close_track(track_handle);
if (sector == 0) 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. rc_cd_close_track(track_handle);
* There's only a handful of games that do this: Q*bert was the first identified. */ return rc_hash_error("Could not locate boot executable");
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);
} }
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); rc_hash_finalize(&md5, hash);
return result; return result;
@ -1547,6 +1515,84 @@ static int rc_hash_snes(char hash[33], const uint8_t* buffer, size_t buffer_size
return rc_hash_buffer(hash, buffer, buffer_size); return rc_hash_buffer(hash, buffer, buffer_size);
} }
struct rc_buffered_file
{
const uint8_t* read_ptr;
const uint8_t* data;
size_t data_size;
};
static struct rc_buffered_file rc_buffered_file;
static void* rc_file_open_buffered_file(const char* path)
{
struct rc_buffered_file* handle = (struct rc_buffered_file*)malloc(sizeof(struct rc_buffered_file));
memcpy(handle, &rc_buffered_file, sizeof(rc_buffered_file));
return handle;
}
void rc_file_seek_buffered_file(void* file_handle, int64_t offset, int origin)
{
struct rc_buffered_file* buffered_file = (struct rc_buffered_file*)file_handle;
switch (origin)
{
case SEEK_SET: buffered_file->read_ptr = buffered_file->data + offset; break;
case SEEK_CUR: buffered_file->read_ptr += offset; break;
case SEEK_END: buffered_file->read_ptr = buffered_file->data + buffered_file->data_size - offset; break;
}
if (buffered_file->read_ptr < buffered_file->data)
buffered_file->read_ptr = buffered_file->data;
else if (buffered_file->read_ptr > buffered_file->data + buffered_file->data_size)
buffered_file->read_ptr = buffered_file->data + buffered_file->data_size;
}
int64_t rc_file_tell_buffered_file(void* file_handle)
{
struct rc_buffered_file* buffered_file = (struct rc_buffered_file*)file_handle;
return (buffered_file->read_ptr - buffered_file->data);
}
size_t rc_file_read_buffered_file(void* file_handle, void* buffer, size_t requested_bytes)
{
struct rc_buffered_file* buffered_file = (struct rc_buffered_file*)file_handle;
const int64_t remaining = buffered_file->data_size - (buffered_file->read_ptr - buffered_file->data);
if ((int)requested_bytes > remaining)
requested_bytes = (int)remaining;
memcpy(buffer, buffered_file->read_ptr, requested_bytes);
buffered_file->read_ptr += requested_bytes;
return requested_bytes;
}
void rc_file_close_buffered_file(void* file_handle)
{
free(file_handle);
}
static int rc_hash_file_from_buffer(char hash[33], int console_id, const uint8_t* buffer, size_t buffer_size)
{
struct rc_hash_filereader buffered_filereader_funcs;
struct rc_hash_filereader* old_filereader = filereader;
int result;
memset(&buffered_filereader_funcs, 0, sizeof(buffered_filereader_funcs));
buffered_filereader_funcs.open = rc_file_open_buffered_file;
buffered_filereader_funcs.close = rc_file_close_buffered_file;
buffered_filereader_funcs.read = rc_file_read_buffered_file;
buffered_filereader_funcs.seek = rc_file_seek_buffered_file;
buffered_filereader_funcs.tell = rc_file_tell_buffered_file;
filereader = &buffered_filereader_funcs;
rc_buffered_file.data = rc_buffered_file.read_ptr = buffer;
rc_buffered_file.data_size = buffer_size;
result = rc_hash_generate_from_file(hash, console_id, "[buffered file]");
filereader = old_filereader;
return result;
}
int rc_hash_generate_from_buffer(char hash[33], int console_id, const uint8_t* buffer, size_t buffer_size) int rc_hash_generate_from_buffer(char hash[33], int console_id, const uint8_t* buffer, size_t buffer_size)
{ {
switch (console_id) switch (console_id)
@ -1558,6 +1604,7 @@ int rc_hash_generate_from_buffer(char hash[33], int console_id, const uint8_t* b
return rc_hash_error(message); return rc_hash_error(message);
} }
case RC_CONSOLE_AMSTRAD_PC:
case RC_CONSOLE_APPLE_II: case RC_CONSOLE_APPLE_II:
case RC_CONSOLE_ATARI_2600: case RC_CONSOLE_ATARI_2600:
case RC_CONSOLE_ATARI_JAGUAR: case RC_CONSOLE_ATARI_JAGUAR:
@ -1570,6 +1617,7 @@ int rc_hash_generate_from_buffer(char hash[33], int console_id, const uint8_t* b
case RC_CONSOLE_MAGNAVOX_ODYSSEY2: case RC_CONSOLE_MAGNAVOX_ODYSSEY2:
case RC_CONSOLE_MASTER_SYSTEM: case RC_CONSOLE_MASTER_SYSTEM:
case RC_CONSOLE_MEGA_DRIVE: case RC_CONSOLE_MEGA_DRIVE:
case RC_CONSOLE_MEGADUCK:
case RC_CONSOLE_MSX: case RC_CONSOLE_MSX:
case RC_CONSOLE_NEOGEO_POCKET: case RC_CONSOLE_NEOGEO_POCKET:
case RC_CONSOLE_ORIC: case RC_CONSOLE_ORIC:
@ -1584,6 +1632,10 @@ int rc_hash_generate_from_buffer(char hash[33], int console_id, const uint8_t* b
case RC_CONSOLE_WONDERSWAN: case RC_CONSOLE_WONDERSWAN:
return rc_hash_buffer(hash, buffer, buffer_size); return rc_hash_buffer(hash, buffer, buffer_size);
case RC_CONSOLE_ARDUBOY:
/* https://en.wikipedia.org/wiki/Intel_HEX */
return rc_hash_text(hash, buffer, buffer_size);
case RC_CONSOLE_ATARI_7800: case RC_CONSOLE_ATARI_7800:
return rc_hash_7800(hash, buffer, buffer_size); return rc_hash_7800(hash, buffer, buffer_size);
@ -1593,14 +1645,15 @@ int rc_hash_generate_from_buffer(char hash[33], int console_id, const uint8_t* b
case RC_CONSOLE_NINTENDO: case RC_CONSOLE_NINTENDO:
return rc_hash_nes(hash, buffer, buffer_size); return rc_hash_nes(hash, buffer, buffer_size);
case RC_CONSOLE_NINTENDO_64:
return rc_hash_n64(hash, buffer, buffer_size);
case RC_CONSOLE_PC_ENGINE: /* NOTE: does not support PCEngine CD */ case RC_CONSOLE_PC_ENGINE: /* NOTE: does not support PCEngine CD */
return rc_hash_pce(hash, buffer, buffer_size); return rc_hash_pce(hash, buffer, buffer_size);
case RC_CONSOLE_SUPER_NINTENDO: case RC_CONSOLE_SUPER_NINTENDO:
return rc_hash_snes(hash, buffer, buffer_size); return rc_hash_snes(hash, buffer, buffer_size);
case RC_CONSOLE_NINTENDO_64:
case RC_CONSOLE_NINTENDO_DS:
return rc_hash_file_from_buffer(hash, console_id, buffer, buffer_size);
} }
} }
@ -1842,7 +1895,6 @@ int rc_hash_generate_from_file(char hash[33], int console_id, const char* path)
return rc_hash_error(buffer); return rc_hash_error(buffer);
} }
case RC_CONSOLE_APPLE_II:
case RC_CONSOLE_ATARI_2600: case RC_CONSOLE_ATARI_2600:
case RC_CONSOLE_ATARI_JAGUAR: case RC_CONSOLE_ATARI_JAGUAR:
case RC_CONSOLE_COLECOVISION: case RC_CONSOLE_COLECOVISION:
@ -1854,6 +1906,7 @@ int rc_hash_generate_from_file(char hash[33], int console_id, const char* path)
case RC_CONSOLE_MAGNAVOX_ODYSSEY2: case RC_CONSOLE_MAGNAVOX_ODYSSEY2:
case RC_CONSOLE_MASTER_SYSTEM: case RC_CONSOLE_MASTER_SYSTEM:
case RC_CONSOLE_MEGA_DRIVE: case RC_CONSOLE_MEGA_DRIVE:
case RC_CONSOLE_MEGADUCK:
case RC_CONSOLE_NEOGEO_POCKET: case RC_CONSOLE_NEOGEO_POCKET:
case RC_CONSOLE_ORIC: case RC_CONSOLE_ORIC:
case RC_CONSOLE_POKEMON_MINI: case RC_CONSOLE_POKEMON_MINI:
@ -1867,6 +1920,8 @@ int rc_hash_generate_from_file(char hash[33], int console_id, const char* path)
/* generic whole-file hash - don't buffer */ /* generic whole-file hash - don't buffer */
return rc_hash_whole_file(hash, path); return rc_hash_whole_file(hash, path);
case RC_CONSOLE_AMSTRAD_PC:
case RC_CONSOLE_APPLE_II:
case RC_CONSOLE_MSX: case RC_CONSOLE_MSX:
case RC_CONSOLE_PC8800: case RC_CONSOLE_PC8800:
/* generic whole-file hash with m3u support - don't buffer */ /* generic whole-file hash with m3u support - don't buffer */
@ -1875,6 +1930,7 @@ int rc_hash_generate_from_file(char hash[33], int console_id, const char* path)
return rc_hash_whole_file(hash, path); return rc_hash_whole_file(hash, path);
case RC_CONSOLE_ARDUBOY:
case RC_CONSOLE_ATARI_7800: case RC_CONSOLE_ATARI_7800:
case RC_CONSOLE_ATARI_LYNX: case RC_CONSOLE_ATARI_LYNX:
case RC_CONSOLE_NINTENDO: case RC_CONSOLE_NINTENDO:
@ -1892,7 +1948,7 @@ int rc_hash_generate_from_file(char hash[33], int console_id, const char* path)
return rc_hash_arcade(hash, path); return rc_hash_arcade(hash, path);
case RC_CONSOLE_NINTENDO_64: case RC_CONSOLE_NINTENDO_64:
return rc_hash_n64_file(hash, path); return rc_hash_n64(hash, path);
case RC_CONSOLE_NINTENDO_DS: case RC_CONSOLE_NINTENDO_DS:
return rc_hash_nintendo_ds(hash, path); return rc_hash_nintendo_ds(hash, path);
@ -1986,6 +2042,9 @@ static void rc_hash_initialize_dsk_iterator(struct rc_hash_iterator* iterator, c
{ {
/* FAT-12 5.25" DD (512 byte sectors, 9 sectors per track, 40 tracks per side */ /* FAT-12 5.25" DD (512 byte sectors, 9 sectors per track, 40 tracks per side */
iterator->consoles[0] = RC_CONSOLE_MSX; iterator->consoles[0] = RC_CONSOLE_MSX;
/* AMSDOS 3" - 40 tracks */
iterator->consoles[1] = RC_CONSOLE_AMSTRAD_PC;
} }
else if (size == 256 * 16 * 35) /* 140KB */ else if (size == 256 * 16 * 35) /* 140KB */
{ {
@ -2002,6 +2061,7 @@ static void rc_hash_initialize_dsk_iterator(struct rc_hash_iterator* iterator, c
/* check MSX first, as Apple II isn't supported by RetroArch, and RAppleWin won't use the iterator */ /* check MSX first, as Apple II isn't supported by RetroArch, and RAppleWin won't use the iterator */
rc_hash_iterator_append_console(iterator, RC_CONSOLE_MSX); rc_hash_iterator_append_console(iterator, RC_CONSOLE_MSX);
rc_hash_iterator_append_console(iterator, RC_CONSOLE_AMSTRAD_PC);
rc_hash_iterator_append_console(iterator, RC_CONSOLE_APPLE_II); rc_hash_iterator_append_console(iterator, RC_CONSOLE_APPLE_II);
} }
@ -2072,7 +2132,7 @@ void rc_hash_initialize_iterator(struct rc_hash_iterator* iterator, const char*
} }
} }
/* bin is associated with MegaDrive, Sega32X, Atari 2600, and Watara Supervision. /* bin is associated with MegaDrive, Sega32X, Atari 2600, Watara Supervision, and MegaDuck.
* Since they all use the same hashing algorithm, only specify one of them */ * Since they all use the same hashing algorithm, only specify one of them */
iterator->consoles[0] = RC_CONSOLE_MEGA_DRIVE; iterator->consoles[0] = RC_CONSOLE_MEGA_DRIVE;
} }
@ -2087,10 +2147,11 @@ void rc_hash_initialize_iterator(struct rc_hash_iterator* iterator, const char*
{ {
iterator->consoles[0] = RC_CONSOLE_PLAYSTATION; iterator->consoles[0] = RC_CONSOLE_PLAYSTATION;
iterator->consoles[1] = RC_CONSOLE_PLAYSTATION_2; iterator->consoles[1] = RC_CONSOLE_PLAYSTATION_2;
iterator->consoles[2] = RC_CONSOLE_PC_ENGINE; iterator->consoles[2] = RC_CONSOLE_DREAMCAST;
iterator->consoles[3] = RC_CONSOLE_3DO; iterator->consoles[3] = RC_CONSOLE_SEGA_CD; /* ASSERT: handles both Sega CD and Saturn */
iterator->consoles[4] = RC_CONSOLE_PCFX; iterator->consoles[4] = RC_CONSOLE_PC_ENGINE;
iterator->consoles[5] = RC_CONSOLE_SEGA_CD; /* ASSERT: handles both Sega CD and Saturn */ iterator->consoles[5] = RC_CONSOLE_3DO;
iterator->consoles[6] = RC_CONSOLE_PCFX;
need_path = 1; need_path = 1;
} }
else if (rc_path_compare_extension(ext, "chd")) else if (rc_path_compare_extension(ext, "chd"))
@ -2098,10 +2159,10 @@ void rc_hash_initialize_iterator(struct rc_hash_iterator* iterator, const char*
iterator->consoles[0] = RC_CONSOLE_PLAYSTATION; iterator->consoles[0] = RC_CONSOLE_PLAYSTATION;
iterator->consoles[1] = RC_CONSOLE_PLAYSTATION_2; iterator->consoles[1] = RC_CONSOLE_PLAYSTATION_2;
iterator->consoles[2] = RC_CONSOLE_DREAMCAST; iterator->consoles[2] = RC_CONSOLE_DREAMCAST;
iterator->consoles[3] = RC_CONSOLE_PC_ENGINE; iterator->consoles[3] = RC_CONSOLE_SEGA_CD; /* ASSERT: handles both Sega CD and Saturn */
iterator->consoles[4] = RC_CONSOLE_3DO; iterator->consoles[4] = RC_CONSOLE_PC_ENGINE;
iterator->consoles[5] = RC_CONSOLE_PCFX; iterator->consoles[5] = RC_CONSOLE_3DO;
iterator->consoles[6] = RC_CONSOLE_SEGA_CD; /* ASSERT: handles both Sega CD and Saturn */ iterator->consoles[6] = RC_CONSOLE_PCFX;
need_path = 1; need_path = 1;
} }
else if (rc_path_compare_extension(ext, "col")) else if (rc_path_compare_extension(ext, "col"))
@ -2164,6 +2225,13 @@ void rc_hash_initialize_iterator(struct rc_hash_iterator* iterator, const char*
} }
break; break;
case 'h':
if (rc_path_compare_extension(ext, "hex"))
{
iterator->consoles[0] = RC_CONSOLE_ARDUBOY;
}
break;
case 'i': case 'i':
if (rc_path_compare_extension(ext, "iso")) if (rc_path_compare_extension(ext, "iso"))
{ {