(Cheevos) Cleanups

This commit is contained in:
twinaphex 2020-06-07 21:34:43 +02:00
parent 2baa3be2da
commit 55a8404d01
4 changed files with 128 additions and 122 deletions

View File

@ -24,6 +24,7 @@
#ifdef HAVE_MENU #ifdef HAVE_MENU
#define CHEEVOS_MENU_BADGE_LIMIT 256 #define CHEEVOS_MENU_BADGE_LIMIT 256
/* TODO/FIXME - public global variables */
static uintptr_t cheevos_badge_menu_texture_list[CHEEVOS_MENU_BADGE_LIMIT] = { 0 }; static uintptr_t cheevos_badge_menu_texture_list[CHEEVOS_MENU_BADGE_LIMIT] = { 0 };
void cheevos_reset_menu_badges(void) void cheevos_reset_menu_badges(void)
@ -70,7 +71,10 @@ uintptr_t cheevos_get_badge_texture(const char *badge, bool locked)
if (!badge) if (!badge)
return 0; return 0;
snprintf(badge_file, sizeof(badge_file), "%s%s.png", badge, locked ? "_lock" : ""); strlcpy(badge_file, badge, sizeof(badge_file));
if (locked)
strlcat(badge_file, "_lock", sizeof(badge_file));
strlcat(badge_file, ".png", sizeof(badge_file));
fill_pathname_application_special(fullpath, fill_pathname_application_special(fullpath,
PATH_MAX_LENGTH * sizeof(char), PATH_MAX_LENGTH * sizeof(char),

View File

@ -29,17 +29,10 @@ static int rcheevos_cmpaddr(const void* e1, const void* e2)
const rcheevos_fixup_t* f2 = (const rcheevos_fixup_t*)e2; const rcheevos_fixup_t* f2 = (const rcheevos_fixup_t*)e2;
if (f1->address < f2->address) if (f1->address < f2->address)
{
return -1; return -1;
}
else if (f1->address > f2->address) else if (f1->address > f2->address)
{
return 1; return 1;
} return 0;
else
{
return 0;
}
} }
static size_t rcheevos_var_reduce(size_t addr, size_t mask) static size_t rcheevos_var_reduce(size_t addr, size_t mask)
@ -47,8 +40,8 @@ static size_t rcheevos_var_reduce(size_t addr, size_t mask)
while (mask) while (mask)
{ {
size_t tmp = (mask - 1) & ~mask; size_t tmp = (mask - 1) & ~mask;
addr = (addr & tmp) | ((addr >> 1) & ~tmp); addr = (addr & tmp) | ((addr >> 1) & ~tmp);
mask = (mask & (mask - 1)) >> 1; mask = (mask & (mask - 1)) >> 1;
} }
return addr; return addr;
@ -69,7 +62,7 @@ void rcheevos_fixup_init(rcheevos_fixups_t* fixups)
{ {
fixups->elements = NULL; fixups->elements = NULL;
fixups->capacity = fixups->count = 0; fixups->capacity = fixups->count = 0;
fixups->dirty = false; fixups->dirty = false;
} }
void rcheevos_fixup_destroy(rcheevos_fixups_t* fixups) void rcheevos_fixup_destroy(rcheevos_fixups_t* fixups)
@ -78,7 +71,8 @@ void rcheevos_fixup_destroy(rcheevos_fixups_t* fixups)
rcheevos_fixup_init(fixups); rcheevos_fixup_init(fixups);
} }
const uint8_t* rcheevos_fixup_find(rcheevos_fixups_t* fixups, unsigned address, int console) const uint8_t* rcheevos_fixup_find(
rcheevos_fixups_t* fixups, unsigned address, int console)
{ {
rcheevos_fixup_t key; rcheevos_fixup_t key;
rcheevos_fixup_t* found; rcheevos_fixup_t* found;
@ -86,17 +80,18 @@ const uint8_t* rcheevos_fixup_find(rcheevos_fixups_t* fixups, unsigned address,
if (fixups->dirty) if (fixups->dirty)
{ {
qsort(fixups->elements, fixups->count, sizeof(rcheevos_fixup_t), rcheevos_cmpaddr); qsort(fixups->elements, fixups->count,
sizeof(rcheevos_fixup_t), rcheevos_cmpaddr);
fixups->dirty = false; fixups->dirty = false;
} }
key.address = address; key.address = address;
found = (rcheevos_fixup_t*)bsearch(&key, fixups->elements, fixups->count, sizeof(rcheevos_fixup_t), rcheevos_cmpaddr); found = (rcheevos_fixup_t*)bsearch(&key,
fixups->elements, fixups->count,
sizeof(rcheevos_fixup_t), rcheevos_cmpaddr);
if (found != NULL) if (found)
{
return found->location; return found->location;
}
if (fixups->count == fixups->capacity) if (fixups->count == fixups->capacity)
{ {
@ -104,19 +99,17 @@ const uint8_t* rcheevos_fixup_find(rcheevos_fixups_t* fixups, unsigned address,
rcheevos_fixup_t* new_elements = (rcheevos_fixup_t*) rcheevos_fixup_t* new_elements = (rcheevos_fixup_t*)
realloc(fixups->elements, new_capacity * sizeof(rcheevos_fixup_t)); realloc(fixups->elements, new_capacity * sizeof(rcheevos_fixup_t));
if (new_elements == NULL) if (!new_elements)
{
return NULL; return NULL;
}
fixups->elements = new_elements; fixups->elements = new_elements;
fixups->capacity = new_capacity; fixups->capacity = new_capacity;
} }
fixups->elements[fixups->count].address = address; fixups->elements[fixups->count].address = address;
fixups->elements[fixups->count++].location = location = fixups->elements[fixups->count++].location = location =
rcheevos_patch_address(address, console); rcheevos_patch_address(address, console);
fixups->dirty = true; fixups->dirty = true;
return location; return location;
} }
@ -124,26 +117,30 @@ const uint8_t* rcheevos_fixup_find(rcheevos_fixups_t* fixups, unsigned address,
const uint8_t* rcheevos_patch_address(unsigned address, int console) const uint8_t* rcheevos_patch_address(unsigned address, int console)
{ {
rarch_system_info_t* system = runloop_get_system_info(); rarch_system_info_t* system = runloop_get_system_info();
const void* pointer = NULL; const void* pointer = NULL;
unsigned original_address = address; unsigned original_address = address;
if (console == RC_CONSOLE_NINTENDO) switch (console)
{ {
if (address >= 0x0800 && address < 0x2000) case RC_CONSOLE_NINTENDO:
{ if (address >= 0x0800 && address < 0x2000)
/* Address in the mirrorred RAM, adjust to real RAM. */ {
CHEEVOS_LOG(RCHEEVOS_TAG "NES memory address in mirrorred RAM %X, adjusted to %X\n", address, address & 0x07ff); /* Address in the mirrorred RAM,
address &= 0x07ff; * adjust to real RAM. */
} CHEEVOS_LOG(RCHEEVOS_TAG "NES memory address in mirrorred RAM %X, adjusted to %X\n", address, address & 0x07ff);
} address &= 0x07ff;
else if (console == RC_CONSOLE_GAMEBOY_COLOR) }
{ break;
if (address >= 0xe000 && address <= 0xfdff) case RC_CONSOLE_GAMEBOY_COLOR:
{ if (address >= 0xe000 && address <= 0xfdff)
/* Address in the echo RAM, adjust to real RAM. */ {
CHEEVOS_LOG(RCHEEVOS_TAG "GBC memory address in echo RAM %X, adjusted to %X\n", address, address - 0x2000); /* Address in the echo RAM, adjust to real RAM. */
address -= 0x2000; CHEEVOS_LOG(RCHEEVOS_TAG "GBC memory address in echo RAM %X, adjusted to %X\n", address, address - 0x2000);
} address -= 0x2000;
}
break;
default:
break;
} }
if (system->mmaps.num_descriptors != 0) if (system->mmaps.num_descriptors != 0)
@ -153,77 +150,78 @@ const uint8_t* rcheevos_patch_address(unsigned address, int console)
const rarch_memory_descriptor_t* end = NULL; const rarch_memory_descriptor_t* end = NULL;
/* Patch the address to correctly map it to the mmaps. */ /* Patch the address to correctly map it to the mmaps. */
if (console == RC_CONSOLE_GAMEBOY_ADVANCE) switch (console)
{ {
if (address < 0x8000) case RC_CONSOLE_GAMEBOY_ADVANCE:
{ if (address < 0x8000)
/* Internal RAM. */ {
CHEEVOS_LOG(RCHEEVOS_TAG "GBA memory address %X adjusted to %X\n", address, address + 0x3000000); /* Internal RAM. */
address += 0x3000000; CHEEVOS_LOG(RCHEEVOS_TAG "GBA memory address %X adjusted to %X\n", address, address + 0x3000000);
} address += 0x3000000;
else }
{ else
/* Work RAM. */ {
CHEEVOS_LOG(RCHEEVOS_TAG "GBA memory address %X adjusted to %X\n", address, address + 0x2000000 - 0x8000); /* Work RAM. */
address += 0x2000000 - 0x8000; CHEEVOS_LOG(RCHEEVOS_TAG "GBA memory address %X adjusted to %X\n", address, address + 0x2000000 - 0x8000);
} address += 0x2000000 - 0x8000;
} }
else if (console == RC_CONSOLE_PC_ENGINE) break;
{ case RC_CONSOLE_PC_ENGINE:
if (address < 0x002000) if (address < 0x002000)
{ {
/* RAM. */ /* RAM. */
CHEEVOS_LOG(RCHEEVOS_TAG "PCE memory address %X adjusted to %X\n", address, address + 0x1f0000); CHEEVOS_LOG(RCHEEVOS_TAG "PCE memory address %X adjusted to %X\n", address, address + 0x1f0000);
address += 0x1f0000; address += 0x1f0000;
} }
else if (address < 0x012000) else if (address < 0x012000)
{ {
/* CD-ROM RAM. */ /* CD-ROM RAM. */
CHEEVOS_LOG(RCHEEVOS_TAG "PCE memory address %X adjusted to %X\n", address, address + 0x100000 - 0x002000); CHEEVOS_LOG(RCHEEVOS_TAG "PCE memory address %X adjusted to %X\n", address, address + 0x100000 - 0x002000);
address += 0x100000 - 0x002000; address += 0x100000 - 0x002000;
} }
else if (address < 0x042000) else if (address < 0x042000)
{ {
/* Super System Card RAM. */ /* Super System Card RAM. */
CHEEVOS_LOG(RCHEEVOS_TAG "PCE memory address %X adjusted to %X\n", address, address + 0x0d0000 - 0x012000); CHEEVOS_LOG(RCHEEVOS_TAG "PCE memory address %X adjusted to %X\n", address, address + 0x0d0000 - 0x012000);
address += 0x0d0000 - 0x012000; address += 0x0d0000 - 0x012000;
} }
else else
{ {
/* CD-ROM battery backed RAM. */ /* CD-ROM battery backed RAM. */
CHEEVOS_LOG(RCHEEVOS_TAG "PCE memory address %X adjusted to %X\n", address, address + 0x1ee000 - 0x042000); CHEEVOS_LOG(RCHEEVOS_TAG "PCE memory address %X adjusted to %X\n", address, address + 0x1ee000 - 0x042000);
address += 0x1ee000 - 0x042000; address += 0x1ee000 - 0x042000;
} }
} break;
else if (console == RC_CONSOLE_SUPER_NINTENDO) case RC_CONSOLE_SUPER_NINTENDO:
{ if (address < 0x020000)
if (address < 0x020000) {
{ /* Work RAM. */
/* Work RAM. */ CHEEVOS_LOG(RCHEEVOS_TAG "SNES memory address %X adjusted to %X\n", address, address + 0x7e0000);
CHEEVOS_LOG(RCHEEVOS_TAG "SNES memory address %X adjusted to %X\n", address, address + 0x7e0000); address += 0x7e0000;
address += 0x7e0000; }
} else
else {
{ /* Save RAM. */
/* Save RAM. */ CHEEVOS_LOG(RCHEEVOS_TAG "SNES memory address %X adjusted to %X\n", address, address + 0x006000 - 0x020000);
CHEEVOS_LOG(RCHEEVOS_TAG "SNES memory address %X adjusted to %X\n", address, address + 0x006000 - 0x020000); address += 0x006000 - 0x020000;
address += 0x006000 - 0x020000; }
} break;
} case RC_CONSOLE_SEGA_CD:
else if (console == RC_CONSOLE_SEGA_CD) if (address < 0x010000)
{ {
if (address < 0x010000) /* Work RAM. */
{ address += 0xFF0000;
/* Work RAM. */ CHEEVOS_LOG(RCHEEVOS_TAG "Sega CD memory address %X adjusted to %X\n", original_address, address);
address += 0xFF0000; }
CHEEVOS_LOG(RCHEEVOS_TAG "Sega CD memory address %X adjusted to %X\n", original_address, address); else
} {
else /* CD-ROM peripheral RAM - exposed at virtual address to avoid banking */
{ address += 0x80020000 - 0x010000;
/* CD-ROM peripheral RAM - exposed at virtual address to avoid banking */ CHEEVOS_LOG(RCHEEVOS_TAG "Sega CD memory address %X adjusted to %X\n", original_address, address);
address += 0x80020000 - 0x010000; }
CHEEVOS_LOG(RCHEEVOS_TAG "Sega CD memory address %X adjusted to %X\n", original_address, address); break;
} default:
break;
} }
desc = system->mmaps.descriptors; desc = system->mmaps.descriptors;
@ -237,22 +235,27 @@ const uint8_t* rcheevos_patch_address(unsigned address, int console)
address -= desc->core.start; address -= desc->core.start;
if (desc->disconnect_mask) if (desc->disconnect_mask)
address = (unsigned)rcheevos_var_reduce(address & desc->disconnect_mask, desc->core.disconnect); address = (unsigned)rcheevos_var_reduce(
address & desc->disconnect_mask, desc->core.disconnect);
if (address >= desc->core.len) if (address >= desc->core.len)
address -= rcheevos_var_highest_bit(address); address -= rcheevos_var_highest_bit(address);
address += desc->core.offset; address += desc->core.offset;
CHEEVOS_LOG(RCHEEVOS_TAG "address %X set to descriptor %d at offset %X\n", original_address, (int)((desc - system->mmaps.descriptors) + 1), address); CHEEVOS_LOG(RCHEEVOS_TAG "address %X set to descriptor %d at offset %X\n", original_address,
(int)((desc - system->mmaps.descriptors) + 1), address);
break; break;
} }
} }
} }
else if (console == RC_CONSOLE_GAMEBOY_ADVANCE) else if (console == RC_CONSOLE_GAMEBOY_ADVANCE)
{ {
/* The RetroAchievements implementation of memory access for GBA puts the save RAM first, /* The RetroAchievements implementation of memory access
* so the default looping behavior below is backwards. If the core doesn't expose a * for GBA puts the save RAM first,
* so the default looping behavior below is backwards.
*
* If the core doesn't expose a
* memory map, say it isn't supported. * memory map, say it isn't supported.
*/ */
pointer = NULL; pointer = NULL;
@ -290,8 +293,9 @@ const uint8_t* rcheevos_patch_address(unsigned address, int console)
} }
/** /**
* HACK Subtract the correct amount of bytes to reach the save RAM as * HACK Subtract the correct amount of bytes to
* it's size is not always set correctly in the core. * reach the save RAM as its size is not always
* set correctly in the core.
*/ */
if (i == 0 && console == RC_CONSOLE_NINTENDO) if (i == 0 && console == RC_CONSOLE_NINTENDO)
address -= 0x6000; address -= 0x6000;
@ -300,7 +304,7 @@ const uint8_t* rcheevos_patch_address(unsigned address, int console)
} }
} }
if (pointer == NULL) if (!pointer)
{ {
CHEEVOS_LOG(RCHEEVOS_TAG "address %X not supported\n", original_address); CHEEVOS_LOG(RCHEEVOS_TAG "address %X not supported\n", original_address);
return NULL; return NULL;

View File

@ -349,12 +349,10 @@ static char* rcheevos_unescape_string(const char* string, size_t length)
*buffer_it++ = escaped_char; *buffer_it++ = escaped_char;
string += 2; string += 2;
break; break;
}; }
} }
else else
{
*buffer_it++ = *string++; *buffer_it++ = *string++;
}
} }
*buffer_it = '\0'; *buffer_it = '\0';

View File

@ -32,7 +32,7 @@ End of setup
*****************************************************************************/ *****************************************************************************/
#define RCHEEVOS_TAG "[RCHEEVOS]: " #define RCHEEVOS_TAG "[RCHEEVOS]: "
#define CHEEVOS_FREE(p) do { void* q = (void*)p; if (q != NULL) free(q); } while (0) #define CHEEVOS_FREE(p) do { void* q = (void*)p; if (q) free(q); } while (0)
#ifdef CHEEVOS_VERBOSE #ifdef CHEEVOS_VERBOSE