Integrated rcheevos

This commit is contained in:
Andre Leiradella 2018-09-02 14:23:37 +01:00
parent 3f72e1fd96
commit 613a3e8479
17 changed files with 1991 additions and 3237 deletions

View File

@ -1676,11 +1676,58 @@ ifeq ($(HAVE_NETWORKING), 1)
# Retro Achievements
ifeq ($(HAVE_CHEEVOS), 1)
DEFINES += -DHAVE_CHEEVOS
DEFINES += -DHAVE_CHEEVOS \
-Ideps/rcheevos/include \
-Ideps/lua/src
OBJ += cheevos/cheevos.o \
cheevos/var.o \
cheevos/cond.o \
cheevos/fixup.o \
cheevos/parser.o \
cheevos/badges.o \
cheevos/hash.o \
deps/rcheevos/src/rcheevos/trigger.o \
deps/rcheevos/src/rcheevos/condset.o \
deps/rcheevos/src/rcheevos/condition.o \
deps/rcheevos/src/rcheevos/operand.o \
deps/rcheevos/src/rcheevos/term.o \
deps/rcheevos/src/rcheevos/expression.o \
deps/rcheevos/src/rcheevos/value.o \
deps/rcheevos/src/rcheevos/lboard.o \
deps/rcheevos/src/rcheevos/alloc.o \
deps/rcheevos/src/rcheevos/format.o \
deps/rcheevos/src/rurl/url.o \
deps/lua/src/lapi.o \
deps/lua/src/lcode.o \
deps/lua/src/lctype.o \
deps/lua/src/ldebug.o \
deps/lua/src/ldo.o \
deps/lua/src/ldump.o \
deps/lua/src/lfunc.o \
deps/lua/src/lgc.o \
deps/lua/src/llex.o \
deps/lua/src/lmem.o \
deps/lua/src/lobject.o \
deps/lua/src/lopcodes.o \
deps/lua/src/lparser.o \
deps/lua/src/lstate.o \
deps/lua/src/lstring.o \
deps/lua/src/ltable.o \
deps/lua/src/ltm.o \
deps/lua/src/lundump.o \
deps/lua/src/lvm.o \
deps/lua/src/lzio.o \
deps/lua/src/lauxlib.o \
deps/lua/src/lbaselib.o \
deps/lua/src/lbitlib.o \
deps/lua/src/lcorolib.o \
deps/lua/src/ldblib.o \
deps/lua/src/liolib.o \
deps/lua/src/lmathlib.o \
deps/lua/src/loslib.o \
deps/lua/src/lstrlib.o \
deps/lua/src/ltablib.o \
deps/lua/src/lutf8lib.o \
deps/lua/src/loadlib.o \
deps/lua/src/linit.o \
$(LIBRETRO_COMM_DIR)/utils/md5.o
endif

View File

@ -1,5 +1,20 @@
#ifndef __RARCH_BADGE_H
#define __RARCH_BADGE_H
/* RetroArch - A frontend for libretro.
* Copyright (C) 2015-2018 - Andre Leiradella
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __RARCH_CHEEVOS_BADGE_H
#define __RARCH_CHEEVOS_BADGE_H
#include "../menu/menu_driver.h"

File diff suppressed because it is too large Load Diff

View File

@ -13,45 +13,20 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __RARCH_CHEEVOS_H
#define __RARCH_CHEEVOS_H
#ifndef __RARCH_CHEEVOS_CHEEVOS_H
#define __RARCH_CHEEVOS_CHEEVOS_H
#include <stdint.h>
#include <stdlib.h>
#include <boolean.h>
#include "../verbosity.h"
#include <retro_common_api.h>
RETRO_BEGIN_DECLS
/*****************************************************************************
Setup - mainly for debugging
*****************************************************************************/
/* Define this macro to get extra-verbose log for cheevos. */
#undef CHEEVOS_VERBOSE
/*****************************************************************************
End of setup
*****************************************************************************/
#define CHEEVOS_TAG "[CHEEVOS]: "
#ifdef CHEEVOS_VERBOSE
#define CHEEVOS_LOG RARCH_LOG
#define CHEEVOS_ERR RARCH_ERR
#else
void cheevos_log(const char *fmt, ...);
#define CHEEVOS_LOG cheevos_log
#define CHEEVOS_ERR cheevos_log
#endif
typedef struct cheevos_ctx_desc
{
unsigned idx;
@ -59,75 +34,12 @@ typedef struct cheevos_ctx_desc
size_t len;
} cheevos_ctx_desc_t;
typedef enum
{
CHEEVOS_CONSOLE_NONE = 0,
/* Don't change those, the values match the console IDs
* at retroachievements.org. */
CHEEVOS_CONSOLE_MEGA_DRIVE = 1,
CHEEVOS_CONSOLE_NINTENDO_64 = 2,
CHEEVOS_CONSOLE_SUPER_NINTENDO = 3,
CHEEVOS_CONSOLE_GAMEBOY = 4,
CHEEVOS_CONSOLE_GAMEBOY_ADVANCE = 5,
CHEEVOS_CONSOLE_GAMEBOY_COLOR = 6,
CHEEVOS_CONSOLE_NINTENDO = 7,
CHEEVOS_CONSOLE_PC_ENGINE = 8,
CHEEVOS_CONSOLE_SEGA_CD = 9,
CHEEVOS_CONSOLE_SEGA_32X = 10,
CHEEVOS_CONSOLE_MASTER_SYSTEM = 11,
CHEEVOS_CONSOLE_PLAYSTATION = 12,
CHEEVOS_CONSOLE_ATARI_LYNX = 13,
CHEEVOS_CONSOLE_NEOGEO_POCKET = 14,
CHEEVOS_CONSOLE_GAME_GEAR = 15,
CHEEVOS_CONSOLE_GAMECUBE = 16,
CHEEVOS_CONSOLE_ATARI_JAGUAR = 17,
CHEEVOS_CONSOLE_NINTENDO_DS = 18,
CHEEVOS_CONSOLE_WII = 19,
CHEEVOS_CONSOLE_WII_U = 20,
CHEEVOS_CONSOLE_PLAYSTATION_2 = 21,
CHEEVOS_CONSOLE_XBOX = 22,
CHEEVOS_CONSOLE_SKYNET = 23,
CHEEVOS_CONSOLE_XBOX_ONE = 24,
CHEEVOS_CONSOLE_ATARI_2600 = 25,
CHEEVOS_CONSOLE_MS_DOS = 26,
CHEEVOS_CONSOLE_ARCADE = 27,
CHEEVOS_CONSOLE_VIRTUAL_BOY = 28,
CHEEVOS_CONSOLE_MSX = 29,
CHEEVOS_CONSOLE_COMMODORE_64 = 30,
CHEEVOS_CONSOLE_ZX81 = 31
} cheevos_console_t;
enum
{
CHEEVOS_DIRTY_TITLE = 1 << 0,
CHEEVOS_DIRTY_DESC = 1 << 1,
CHEEVOS_DIRTY_POINTS = 1 << 2,
CHEEVOS_DIRTY_AUTHOR = 1 << 3,
CHEEVOS_DIRTY_ID = 1 << 4,
CHEEVOS_DIRTY_BADGE = 1 << 5,
CHEEVOS_DIRTY_CONDITIONS = 1 << 6,
CHEEVOS_DIRTY_VOTES = 1 << 7,
CHEEVOS_DIRTY_DESCRIPTION = 1 << 8,
CHEEVOS_DIRTY_ALL = (1 << 9) - 1
};
enum
{
CHEEVOS_ACTIVE_SOFTCORE = 1 << 0,
CHEEVOS_ACTIVE_HARDCORE = 1 << 1
};
enum
{
CHEEVOS_FORMAT_FRAMES = 0,
CHEEVOS_FORMAT_SECS,
CHEEVOS_FORMAT_MILLIS,
CHEEVOS_FORMAT_SCORE,
CHEEVOS_FORMAT_VALUE,
CHEEVOS_FORMAT_OTHER
};
bool cheevos_load(const void *data);
void cheevos_reset_game(void);
@ -150,7 +62,7 @@ void cheevos_set_support_cheevos(bool state);
bool cheevos_get_support_cheevos(void);
cheevos_console_t cheevos_get_console(void);
int cheevos_get_console(void);
extern bool cheevos_loaded;
extern bool cheevos_hardcore_active;
@ -160,4 +72,4 @@ extern int cheats_were_enabled;
RETRO_END_DECLS
#endif /* __RARCH_CHEEVOS_H */
#endif /* __RARCH_CHEEVOS_CHEEVOS_H */

View File

@ -1,189 +0,0 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2015-2017 - Andre Leiradella
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "cond.h"
#include "var.h"
#include "../retroarch.h"
#include "../verbosity.h"
/*****************************************************************************
Parsing
*****************************************************************************/
static cheevos_cond_op_t cheevos_cond_parse_operator(const char** memaddr)
{
const char *str = *memaddr;
cheevos_cond_op_t op;
if (*str == '=' && str[1] == '=')
{
op = CHEEVOS_COND_OP_EQUALS;
str += 2;
}
else if (*str == '=')
{
op = CHEEVOS_COND_OP_EQUALS;
str++;
}
else if (*str == '!' && str[1] == '=')
{
op = CHEEVOS_COND_OP_NOT_EQUAL_TO;
str += 2;
}
else if (*str == '<' && str[1] == '=')
{
op = CHEEVOS_COND_OP_LESS_THAN_OR_EQUAL;
str += 2;
}
else if (*str == '<')
{
op = CHEEVOS_COND_OP_LESS_THAN;
str++;
}
else if (*str == '>' && str[1] == '=')
{
op = CHEEVOS_COND_OP_GREATER_THAN_OR_EQUAL;
str += 2;
}
else if (*str == '>')
{
op = CHEEVOS_COND_OP_GREATER_THAN;
str++;
}
else
{
CHEEVOS_ERR(CHEEVOS_TAG "unknown operator %c\n.", *str);
op = CHEEVOS_COND_OP_EQUALS;
}
*memaddr = str;
return op;
}
void cheevos_cond_parse(cheevos_cond_t* cond, const char** memaddr)
{
const char* str = *memaddr;
cond->type = CHEEVOS_COND_TYPE_STANDARD;
if (*str != 0 && str[1] == ':')
{
int skip = 2;
switch (*str)
{
case 'R':
cond->type = CHEEVOS_COND_TYPE_RESET_IF;
break;
case 'P':
cond->type = CHEEVOS_COND_TYPE_PAUSE_IF;
break;
case 'A':
cond->type = CHEEVOS_COND_TYPE_ADD_SOURCE;
break;
case 'B':
cond->type = CHEEVOS_COND_TYPE_SUB_SOURCE;
break;
case 'C':
cond->type = CHEEVOS_COND_TYPE_ADD_HITS;
break;
default:
skip = 0;
break;
}
str += skip;
}
cheevos_var_parse(&cond->source, &str);
cond->op = cheevos_cond_parse_operator(&str);
cheevos_var_parse(&cond->target, &str);
cond->curr_hits = 0;
if (*str == '(' || *str == '.')
{
char* end;
cond->req_hits = (unsigned)strtol(str + 1, &end, 10);
str = end + (*end == ')' || *end == '.');
}
else
cond->req_hits = 0;
*memaddr = str;
}
unsigned cheevos_cond_count_in_set(const char* memaddr, unsigned which)
{
cheevos_cond_t dummy;
unsigned index = 0;
unsigned count = 0;
for (;;)
{
for (;;)
{
cheevos_cond_parse(&dummy, &memaddr);
if (index == which)
count++;
if (*memaddr != '_')
break;
memaddr++;
}
index++;
if (*memaddr != 'S')
break;
memaddr++;
}
return count;
}
void cheevos_cond_parse_in_set(cheevos_cond_t* cond, const char* memaddr, unsigned which)
{
cheevos_cond_t dummy;
unsigned index = 0;
for (;;)
{
for (;;)
{
if (index == which)
{
cheevos_cond_parse(cond, &memaddr);
cond++;
}
else
cheevos_cond_parse(&dummy, &memaddr);
if (*memaddr != '_')
break;
memaddr++;
}
index++;
if (*memaddr != 'S')
break;
memaddr++;
}
}

View File

@ -1,63 +0,0 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2015-2017 - Andre Leiradella
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __RARCH_CHEEVOS_COND_H
#define __RARCH_CHEEVOS_COND_H
#include "var.h"
#include <retro_common_api.h>
RETRO_BEGIN_DECLS
typedef enum
{
CHEEVOS_COND_TYPE_STANDARD,
CHEEVOS_COND_TYPE_PAUSE_IF,
CHEEVOS_COND_TYPE_RESET_IF,
CHEEVOS_COND_TYPE_ADD_SOURCE,
CHEEVOS_COND_TYPE_SUB_SOURCE,
CHEEVOS_COND_TYPE_ADD_HITS
} cheevos_cond_type_t;
typedef enum
{
CHEEVOS_COND_OP_EQUALS,
CHEEVOS_COND_OP_LESS_THAN,
CHEEVOS_COND_OP_LESS_THAN_OR_EQUAL,
CHEEVOS_COND_OP_GREATER_THAN,
CHEEVOS_COND_OP_GREATER_THAN_OR_EQUAL,
CHEEVOS_COND_OP_NOT_EQUAL_TO
} cheevos_cond_op_t;
typedef struct
{
cheevos_cond_type_t type;
unsigned req_hits;
unsigned curr_hits;
char pause;
cheevos_var_t source;
cheevos_cond_op_t op;
cheevos_var_t target;
} cheevos_cond_t;
void cheevos_cond_parse(cheevos_cond_t* cond, const char** memaddr);
unsigned cheevos_cond_count_in_set(const char* memaddr, unsigned which);
void cheevos_cond_parse_in_set(cheevos_cond_t* cond, const char* memaddr, unsigned which);
RETRO_END_DECLS
#endif /* __RARCH_CHEEVOS_COND_H */

View File

@ -1,5 +1,5 @@
#ifndef CORO_H
#define CORO_H
#ifndef __RARCH_CHEEVOS_CORO_H
#define __RARCH_CHEEVOS_CORO_H
/*
Released under the CC0: https://creativecommons.org/publicdomain/zero/1.0/
@ -72,4 +72,4 @@ Released under the CC0: https://creativecommons.org/publicdomain/zero/1.0/
int step, sp; \
int stack[ 8 ];
#endif /* CORO_H */
#endif /* __RARCH_CHEEVOS_CORO_H */

264
cheevos/fixup.c Normal file
View File

@ -0,0 +1,264 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2015-2018 - Andre Leiradella
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "fixup.h"
#include "cheevos.h"
#include "util.h"
#include "../retroarch.h"
#include "../core.h"
#include <rcheevos.h>
static int cheevos_cmpaddr(const void* e1, const void* e2)
{
const cheevos_fixup_t* f1 = (const cheevos_fixup_t*)e1;
const cheevos_fixup_t* f2 = (const cheevos_fixup_t*)e2;
if (f1->address < f2->address)
{
return -1;
}
else if (f1->address > f2->address)
{
return 1;
}
else
{
return 0;
}
}
static size_t cheevos_var_reduce(size_t addr, size_t mask)
{
while (mask)
{
size_t tmp = (mask - 1) & ~mask;
addr = (addr & tmp) | ((addr >> 1) & ~tmp);
mask = (mask & (mask - 1)) >> 1;
}
return addr;
}
static size_t cheevos_var_highest_bit(size_t n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return n ^ (n >> 1);
}
void cheevos_fixup_init(cheevos_fixups_t* fixups)
{
fixups->elements = NULL;
fixups->capacity = fixups->count = 0;
fixups->dirty = false;
}
void cheevos_fixup_destroy(cheevos_fixups_t* fixups)
{
CHEEVOS_FREE(fixups->elements);
cheevos_fixup_init(fixups);
}
const uint8_t* cheevos_fixup_find(cheevos_fixups_t* fixups, unsigned address, int console)
{
cheevos_fixup_t key;
cheevos_fixup_t* found;
const uint8_t* location;
if (fixups->dirty)
{
qsort(fixups->elements, fixups->count, sizeof(cheevos_fixup_t), cheevos_cmpaddr);
fixups->dirty = false;
}
key.address = address;
found = (cheevos_fixup_t*)bsearch(&key, fixups->elements, fixups->count, sizeof(cheevos_fixup_t), cheevos_cmpaddr);
if (found != NULL)
{
return found->location;
}
if (fixups->count == fixups->capacity)
{
unsigned new_capacity = fixups->capacity == 0 ? 16 : fixups->capacity * 2;
cheevos_fixup_t* new_elements = (cheevos_fixup_t*)
realloc(fixups->elements, new_capacity * sizeof(cheevos_fixup_t));
if (new_elements == NULL)
{
return NULL;
}
fixups->elements = new_elements;
fixups->capacity = new_capacity;
}
fixups->elements[fixups->count].address = address;
fixups->elements[fixups->count++].location = location =
cheevos_patch_address(address, console);
fixups->dirty = true;
return location;
}
const uint8_t* cheevos_patch_address(unsigned address, int console)
{
rarch_system_info_t* system = runloop_get_system_info();
const void* pointer = NULL;
if (console == RC_CONSOLE_NINTENDO)
{
if (address >= 0x0800 && address < 0x2000)
{
/* Address in the mirrorred RAM, adjust to real RAM. */
CHEEVOS_LOG(CHEEVOS_TAG "NES memory address in mirrorred RAM %X, adjusted to %X\n", address, address & 0x07ff);
address &= 0x07ff;
}
}
else if (console == RC_CONSOLE_GAMEBOY_COLOR)
{
if (address >= 0xe000 && address <= 0xfdff)
{
/* Address in the echo RAM, adjust to real RAM. */
CHEEVOS_LOG(CHEEVOS_TAG "GBC memory address in echo RAM %X, adjusted to %X\n", address, address - 0x2000);
address -= 0x2000;
}
}
if (system->mmaps.num_descriptors != 0)
{
/* We have memory descriptors, use it. */
const rarch_memory_descriptor_t* desc = NULL;
const rarch_memory_descriptor_t* end = NULL;
/* Patch the address to correctly map it to the mmaps. */
if (console == RC_CONSOLE_GAMEBOY_ADVANCE)
{
if (address < 0x8000)
{
/* Internal RAM. */
CHEEVOS_LOG(CHEEVOS_TAG "GBA memory address %X adjusted to %X\n", address, address + 0x3000000);
address += 0x3000000;
}
else
{
/* Work RAM. */
CHEEVOS_LOG(CHEEVOS_TAG "GBA memory address %X adjusted to %X\n", address, address + 0x2000000 - 0x8000);
address += 0x2000000 - 0x8000;
}
}
else if (console == RC_CONSOLE_PC_ENGINE)
{
/* RAM. */
CHEEVOS_LOG(CHEEVOS_TAG "PCE memory address %X adjusted to %X\n", address, address + 0x1f0000);
address += 0x1f0000;
}
else if (console == RC_CONSOLE_SUPER_NINTENDO)
{
if (address < 0x020000)
{
/* Work RAM. */
CHEEVOS_LOG(CHEEVOS_TAG "SNES memory address %X adjusted to %X\n", address, address + 0x7e0000);
address += 0x7e0000;
}
else
{
/* Save RAM. */
CHEEVOS_LOG(CHEEVOS_TAG "SNES memory address %X adjusted to %X\n", address, address + 0x006000 - 0x020000);
address += 0x006000 - 0x020000;
}
}
desc = system->mmaps.descriptors;
end = desc + system->mmaps.num_descriptors;
for (; desc < end; desc++)
{
if (((desc->core.start ^ address) & desc->core.select) == 0)
{
unsigned addr = address;
pointer = desc->core.ptr;
address = (unsigned)cheevos_var_reduce(
(addr - desc->core.start) & desc->disconnect_mask,
desc->core.disconnect);
if (address >= desc->core.len)
address -= cheevos_var_highest_bit(address);
address += desc->core.offset;
CHEEVOS_LOG(CHEEVOS_TAG "address %X set to descriptor %d at offset %X\n", addr, (int)((desc - system->mmaps.descriptors) + 1), address);
break;
}
}
}
else
{
unsigned i;
for (i = 0; i < 4; i++)
{
retro_ctx_memory_info_t meminfo;
switch (i)
{
case 0:
meminfo.id = RETRO_MEMORY_SYSTEM_RAM;
break;
case 1:
meminfo.id = RETRO_MEMORY_SAVE_RAM;
break;
case 2:
meminfo.id = RETRO_MEMORY_VIDEO_RAM;
break;
case 3:
meminfo.id = RETRO_MEMORY_RTC;
break;
}
core_get_memory(&meminfo);
if (address < meminfo.size)
{
pointer = meminfo.data;
break;
}
/**
* HACK Subtract the correct amount of bytes to reach the save RAM as
* it's size is not always set correctly in the core.
*/
if (i == 0 && console == RC_CONSOLE_NINTENDO)
address -= 0x6000;
else
address -= meminfo.size;
}
}
if (pointer == NULL)
{
return NULL;
}
return (const uint8_t*)pointer + address;
}

48
cheevos/fixup.h Normal file
View File

@ -0,0 +1,48 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2015-2018 - Andre Leiradella
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __RARCH_CHEEVOS_FIXUP_H
#define __RARCH_CHEEVOS_FIXUP_H
#include <stdint.h>
#include <boolean.h>
#include <retro_common_api.h>
RETRO_BEGIN_DECLS
typedef struct
{
unsigned address;
const uint8_t* location;
} cheevos_fixup_t;
typedef struct
{
cheevos_fixup_t* elements;
unsigned capacity, count;
bool dirty;
} cheevos_fixups_t;
void cheevos_fixup_init(cheevos_fixups_t* fixups);
void cheevos_fixup_destroy(cheevos_fixups_t* fixups);
const uint8_t* cheevos_fixup_find(cheevos_fixups_t* fixups, unsigned address, int console);
const uint8_t* cheevos_patch_address(unsigned address, int console);
RETRO_END_DECLS
#endif

12
cheevos/hash.c Normal file
View File

@ -0,0 +1,12 @@
#include "hash.h"
uint32_t cheevos_djb2(const char* str, size_t length)
{
const unsigned char* aux = (const unsigned char*)str;
uint32_t hash = 5381;
while (length--)
hash = ( hash << 5 ) + hash + *aux++;
return hash;
}

30
cheevos/hash.h Normal file
View File

@ -0,0 +1,30 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2015-2018 - Andre Leiradella
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __RARCH_CHEEVOS_HASH_H
#define __RARCH_CHEEVOS_HASH_H
#include <stdint.h>
#include <stddef.h>
#include <retro_common_api.h>
RETRO_BEGIN_DECLS
uint32_t cheevos_djb2(const char* str, size_t length);
RETRO_END_DECLS
#endif

674
cheevos/parser.c Normal file
View File

@ -0,0 +1,674 @@
#include "parser.h"
#include "hash.h"
#include "util.h"
#include <formats/jsonsax.h>
#include <string/stdstring.h>
#include <compat/strl.h>
/* C89 wants only int values in enums. */
#define CHEEVOS_JSON_KEY_GAMEID 0xb4960eecU
#define CHEEVOS_JSON_KEY_ACHIEVEMENTS 0x69749ae1U
#define CHEEVOS_JSON_KEY_ID 0x005973f2U
#define CHEEVOS_JSON_KEY_MEMADDR 0x1e76b53fU
#define CHEEVOS_JSON_KEY_TITLE 0x0e2a9a07U
#define CHEEVOS_JSON_KEY_DESCRIPTION 0xe61a1f69U
#define CHEEVOS_JSON_KEY_POINTS 0xca8fce22U
#define CHEEVOS_JSON_KEY_AUTHOR 0xa804edb8U
#define CHEEVOS_JSON_KEY_MODIFIED 0xdcea4fe6U
#define CHEEVOS_JSON_KEY_CREATED 0x3a84721dU
#define CHEEVOS_JSON_KEY_BADGENAME 0x887685d9U
#define CHEEVOS_JSON_KEY_CONSOLE_ID 0x071656e5U
#define CHEEVOS_JSON_KEY_TOKEN 0x0e2dbd26U
#define CHEEVOS_JSON_KEY_FLAGS 0x0d2e96b2U
#define CHEEVOS_JSON_KEY_LEADERBOARDS 0xf1247d2dU
#define CHEEVOS_JSON_KEY_MEM 0x0b8807e4U
#define CHEEVOS_JSON_KEY_FORMAT 0xb341208eU
#define CHEEVOS_JSON_KEY_SUCCESS 0x110461deU
#define CHEEVOS_JSON_KEY_ERROR 0x0d2011cfU
/*****************************************************************************
Gets a value in a JSON
*****************************************************************************/
typedef struct
{
unsigned key_hash;
int is_key;
const char* value;
size_t length;
} cheevos_getvalueud_t;
static int cheevos_getvalue_key(void* userdata,
const char* name, size_t length)
{
cheevos_getvalueud_t* ud = (cheevos_getvalueud_t*)userdata;
ud->is_key = cheevos_djb2(name, length) == ud->key_hash;
return 0;
}
static int cheevos_getvalue_string(void* userdata,
const char* string, size_t length)
{
cheevos_getvalueud_t* ud = (cheevos_getvalueud_t*)userdata;
if (ud->is_key)
{
ud->value = string;
ud->length = length;
ud->is_key = 0;
}
return 0;
}
static int cheevos_getvalue_boolean(void* userdata, int istrue)
{
cheevos_getvalueud_t* ud = (cheevos_getvalueud_t*)userdata;
if (ud->is_key)
{
if (istrue)
{
ud->value = "true";
ud->length = 4;
}
else
{
ud->value = "false";
ud->length = 5;
}
ud->is_key = 0;
}
return 0;
}
static int cheevos_getvalue_null(void* userdata)
{
cheevos_getvalueud_t* ud = (cheevos_getvalueud_t*)userdata;
if (ud->is_key )
{
ud->value = "null";
ud->length = 4;
ud->is_key = 0;
}
return 0;
}
static int cheevos_get_value(const char* json, unsigned key_hash,
char* value, size_t length)
{
static const jsonsax_handlers_t handlers =
{
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
cheevos_getvalue_key,
NULL,
cheevos_getvalue_string,
cheevos_getvalue_string, /* number */
cheevos_getvalue_boolean,
cheevos_getvalue_null
};
cheevos_getvalueud_t ud;
ud.key_hash = key_hash;
ud.is_key = 0;
ud.value = NULL;
ud.length = 0;
*value = 0;
if ((jsonsax_parse(json, &handlers, (void*)&ud) == JSONSAX_OK)
&& ud.value && ud.length < length)
{
strlcpy(value, ud.value, ud.length + 1);
return 0;
}
return -1;
}
/*****************************************************************************
Returns the token of the error message
*****************************************************************************/
int cheevos_get_token(const char* json, char* token, size_t length)
{
cheevos_get_value(json, CHEEVOS_JSON_KEY_ERROR, token, length);
if (!string_is_empty(token))
return -1;
return cheevos_get_value(json, CHEEVOS_JSON_KEY_TOKEN, token, length);
}
/*****************************************************************************
Count number of achievements in a JSON file
*****************************************************************************/
typedef struct
{
int in_cheevos;
int in_lboards;
uint32_t field_hash;
unsigned core_count;
unsigned unofficial_count;
unsigned lboard_count;
} cheevos_countud_t;
static int cheevos_count_end_array(void* userdata)
{
cheevos_countud_t* ud = (cheevos_countud_t*)userdata;
ud->in_cheevos = 0;
ud->in_lboards = 0;
return 0;
}
static int cheevos_count_key(void* userdata,
const char* name, size_t length)
{
cheevos_countud_t* ud = (cheevos_countud_t*)userdata;
ud->field_hash = cheevos_djb2(name, length);
if (ud->field_hash == CHEEVOS_JSON_KEY_ACHIEVEMENTS)
ud->in_cheevos = 1;
else if (ud->field_hash == CHEEVOS_JSON_KEY_LEADERBOARDS)
ud->in_lboards = 1;
return 0;
}
static int cheevos_count_number(void* userdata,
const char* number, size_t length)
{
cheevos_countud_t* ud = (cheevos_countud_t*)userdata;
if (ud->in_cheevos && ud->field_hash == CHEEVOS_JSON_KEY_FLAGS)
{
long flags = strtol(number, NULL, 10);
if (flags == 3)
ud->core_count++; /* Core achievements */
else if (flags == 5)
ud->unofficial_count++; /* Unofficial achievements */
}
else if (ud->in_lboards && ud->field_hash == CHEEVOS_JSON_KEY_ID)
ud->lboard_count++;
return 0;
}
static int cheevos_count_cheevos(const char* json,
unsigned* core_count, unsigned* unofficial_count,
unsigned* lboard_count)
{
static const jsonsax_handlers_t handlers =
{
NULL,
NULL,
NULL,
NULL,
NULL,
cheevos_count_end_array,
cheevos_count_key,
NULL,
NULL,
cheevos_count_number,
NULL,
NULL
};
int res;
cheevos_countud_t ud;
ud.in_cheevos = 0;
ud.in_lboards = 0;
ud.core_count = 0;
ud.unofficial_count = 0;
ud.lboard_count = 0;
res = jsonsax_parse(json, &handlers, (void*)&ud);
*core_count = ud.core_count;
*unofficial_count = ud.unofficial_count;
*lboard_count = ud.lboard_count;
return res;
}
/*****************************************************************************
Parses the cheevos in the JSON
*****************************************************************************/
typedef struct
{
const char* string;
size_t length;
} cheevos_field_t;
typedef struct
{
int in_cheevos;
int in_lboards;
int is_console_id;
unsigned core_count;
unsigned unofficial_count;
unsigned lboard_count;
cheevos_field_t* field;
cheevos_field_t id, memaddr, title, desc, points, author;
cheevos_field_t modified, created, badge, flags, format;
cheevos_rapatchdata_t* patchdata;
} cheevos_readud_t;
static const char* cheevos_dupstr(const cheevos_field_t* field)
{
char* string = (char*)malloc(field->length + 1);
if (!string)
return NULL;
memcpy((void*)string, (void*)field->string, field->length);
string[field->length] = 0;
return string;
}
static int cheevos_new_cheevo(cheevos_readud_t* ud)
{
cheevos_racheevo_t* cheevo = NULL;
unsigned flags = (unsigned)strtol(ud->flags.string, NULL, 10);
if (flags == 3)
cheevo = ud->patchdata->core + ud->core_count++;
else if (flags == 5)
cheevo = ud->patchdata->unofficial + ud->unofficial_count++;
else
return 0;
cheevo->title = cheevos_dupstr(&ud->title);
cheevo->description = cheevos_dupstr(&ud->desc);
cheevo->badge = cheevos_dupstr(&ud->badge);
cheevo->memaddr = cheevos_dupstr(&ud->memaddr);
cheevo->points = (unsigned)strtol(ud->points.string, NULL, 10);
cheevo->id = (unsigned)strtol(ud->id.string, NULL, 10);
if ( !cheevo->title
|| !cheevo->description
|| !cheevo->badge
|| !cheevo->memaddr)
{
CHEEVOS_FREE(cheevo->title);
CHEEVOS_FREE(cheevo->description);
CHEEVOS_FREE(cheevo->badge);
CHEEVOS_FREE(cheevo->memaddr);
return -1;
}
return 0;
}
static int cheevos_new_lboard(cheevos_readud_t* ud)
{
cheevos_ralboard_t* lboard = ud->patchdata->lboards + ud->lboard_count++;
lboard->title = cheevos_dupstr(&ud->title);
lboard->description = cheevos_dupstr(&ud->desc);
lboard->format = cheevos_dupstr(&ud->format);
lboard->mem = cheevos_dupstr(&ud->memaddr);
lboard->id = (unsigned)strtol(ud->id.string, NULL, 10);
if ( !lboard->title
|| !lboard->description
|| !lboard->format
|| !lboard->mem)
{
CHEEVOS_FREE(lboard->title);
CHEEVOS_FREE(lboard->description);
CHEEVOS_FREE(lboard->format);
CHEEVOS_FREE(lboard->mem);
return -1;
}
return 0;
}
static int cheevos_read_end_object(void* userdata)
{
cheevos_readud_t* ud = (cheevos_readud_t*)userdata;
if (ud->in_cheevos)
return cheevos_new_cheevo(ud);
if (ud->in_lboards)
return cheevos_new_lboard(ud);
return 0;
}
static int cheevos_read_end_array(void* userdata)
{
cheevos_readud_t* ud = (cheevos_readud_t*)userdata;
ud->in_cheevos = 0;
ud->in_lboards = 0;
return 0;
}
static int cheevos_read_key(void* userdata,
const char* name, size_t length)
{
cheevos_readud_t* ud = (cheevos_readud_t*)userdata;
int common = ud->in_cheevos || ud->in_lboards;
uint32_t hash = cheevos_djb2(name, length);
ud->field = NULL;
switch (hash)
{
case CHEEVOS_JSON_KEY_ACHIEVEMENTS:
ud->in_cheevos = 1;
break;
case CHEEVOS_JSON_KEY_LEADERBOARDS:
ud->in_lboards = 1;
break;
case CHEEVOS_JSON_KEY_CONSOLE_ID:
ud->is_console_id = 1;
break;
case CHEEVOS_JSON_KEY_ID:
if (common)
ud->field = &ud->id;
break;
case CHEEVOS_JSON_KEY_MEMADDR:
if (ud->in_cheevos)
ud->field = &ud->memaddr;
break;
case CHEEVOS_JSON_KEY_MEM:
if (ud->in_lboards)
ud->field = &ud->memaddr;
break;
case CHEEVOS_JSON_KEY_TITLE:
if (common)
ud->field = &ud->title;
break;
case CHEEVOS_JSON_KEY_DESCRIPTION:
if (common)
ud->field = &ud->desc;
break;
case CHEEVOS_JSON_KEY_POINTS:
if (ud->in_cheevos)
ud->field = &ud->points;
break;
case CHEEVOS_JSON_KEY_AUTHOR:
if (ud->in_cheevos)
ud->field = &ud->author;
break;
case CHEEVOS_JSON_KEY_MODIFIED:
if (ud->in_cheevos)
ud->field = &ud->modified;
break;
case CHEEVOS_JSON_KEY_CREATED:
if (ud->in_cheevos)
ud->field = &ud->created;
break;
case CHEEVOS_JSON_KEY_BADGENAME:
if (ud->in_cheevos)
ud->field = &ud->badge;
break;
case CHEEVOS_JSON_KEY_FLAGS:
if (ud->in_cheevos)
ud->field = &ud->flags;
break;
case CHEEVOS_JSON_KEY_FORMAT:
if (ud->in_lboards)
ud->field = &ud->format;
break;
default:
break;
}
return 0;
}
static int cheevos_read_string(void* userdata,
const char* string, size_t length)
{
cheevos_readud_t* ud = (cheevos_readud_t*)userdata;
if (ud->field)
{
ud->field->string = string;
ud->field->length = length;
}
return 0;
}
static int cheevos_read_number(void* userdata,
const char* number, size_t length)
{
cheevos_readud_t* ud = (cheevos_readud_t*)userdata;
if (ud->field)
{
ud->field->string = number;
ud->field->length = length;
}
else if (ud->is_console_id)
{
ud->patchdata->console_id = strtol(number, NULL, 10);
ud->is_console_id = 0;
}
return 0;
}
int cheevos_get_patchdata(const char* json, cheevos_rapatchdata_t* patchdata)
{
static const jsonsax_handlers_t handlers =
{
NULL,
NULL,
NULL,
cheevos_read_end_object,
NULL,
cheevos_read_end_array,
cheevos_read_key,
NULL,
cheevos_read_string,
cheevos_read_number,
NULL,
NULL
};
cheevos_readud_t ud;
int res;
/* Count the number of achievements in the JSON file. */
res = cheevos_count_cheevos(json, &patchdata->core_count,
&patchdata->unofficial_count, &patchdata->lboard_count);
if (res != JSONSAX_OK)
return -1;
/* Allocate the achievements. */
patchdata->core = (cheevos_racheevo_t*)
calloc(patchdata->core_count, sizeof(cheevos_racheevo_t));
patchdata->unofficial = (cheevos_racheevo_t*)
calloc(patchdata->unofficial_count, sizeof(cheevos_racheevo_t));
patchdata->lboards = (cheevos_ralboard_t*)
calloc(patchdata->lboard_count, sizeof(cheevos_ralboard_t));
if (!patchdata->core ||
!patchdata->unofficial ||
!patchdata->lboards)
{
CHEEVOS_FREE(patchdata->core);
CHEEVOS_FREE(patchdata->unofficial);
CHEEVOS_FREE(patchdata->lboards);
return -1;
}
/* Load the achievements. */
ud.in_cheevos = 0;
ud.in_lboards = 0;
ud.is_console_id = 0;
ud.field = NULL;
ud.core_count = 0;
ud.unofficial_count = 0;
ud.lboard_count = 0;
ud.patchdata = patchdata;
if (jsonsax_parse(json, &handlers, (void*)&ud) != JSONSAX_OK)
{
cheevos_free_patchdata(patchdata);
return -1;
}
return 0;
}
/*****************************************************************************
Frees the patchdata
*****************************************************************************/
void cheevos_free_patchdata(cheevos_rapatchdata_t* patchdata)
{
unsigned i = 0, count = 0;
const cheevos_racheevo_t* cheevo = NULL;
const cheevos_ralboard_t* lboard = NULL;
cheevo = patchdata->core;
for (i = 0, count = patchdata->core_count; i < count; i++, cheevo++)
{
CHEEVOS_FREE(cheevo->title);
CHEEVOS_FREE(cheevo->description);
CHEEVOS_FREE(cheevo->badge);
CHEEVOS_FREE(cheevo->memaddr);
}
cheevo = patchdata->unofficial;
for (i = 0, count = patchdata->unofficial_count; i < count; i++, cheevo++)
{
CHEEVOS_FREE(cheevo->title);
CHEEVOS_FREE(cheevo->description);
CHEEVOS_FREE(cheevo->badge);
CHEEVOS_FREE(cheevo->memaddr);
}
lboard = patchdata->lboards;
for (i = 0, count = patchdata->lboard_count; i < count; i++, lboard++)
{
CHEEVOS_FREE(lboard->title);
CHEEVOS_FREE(lboard->description);
CHEEVOS_FREE(lboard->format);
CHEEVOS_FREE(lboard->mem);
}
CHEEVOS_FREE(patchdata->core);
CHEEVOS_FREE(patchdata->unofficial);
CHEEVOS_FREE(patchdata->lboards);
patchdata->console_id = 0;
patchdata->core = NULL;
patchdata->unofficial = NULL;
patchdata->lboards = NULL;
patchdata->core_count = 0;
patchdata->unofficial_count = 0;
patchdata->lboard_count = 0;
}
/*****************************************************************************
Deactivates unlocked cheevos
*****************************************************************************/
typedef struct
{
int is_element;
cheevos_unlock_cb_t unlock_cb;
void* userdata;
} cheevos_deactivate_t;
static int cheevos_deactivate_index(void* userdata, unsigned int index)
{
cheevos_deactivate_t* ud = (cheevos_deactivate_t*)userdata;
ud->is_element = 1;
return 0;
}
static int cheevos_deactivate_number(void* userdata,
const char* number, size_t length)
{
cheevos_deactivate_t* ud = (cheevos_deactivate_t*)userdata;
unsigned id = 0;
if (ud->is_element)
{
ud->is_element = 0;
id = (unsigned)strtol(number, NULL, 10);
ud->unlock_cb(id, ud->userdata);
}
return 0;
}
void cheevos_deactivate_unlocks(const char* json, cheevos_unlock_cb_t unlock_cb, void* userdata)
{
static const jsonsax_handlers_t handlers =
{
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
cheevos_deactivate_index,
NULL,
cheevos_deactivate_number,
NULL,
NULL
};
cheevos_deactivate_t ud;
ud.is_element = 0;
ud.unlock_cb = unlock_cb;
ud.userdata = userdata;
jsonsax_parse(json, &handlers, (void*)&ud);
}
/*****************************************************************************
Returns the game ID
*****************************************************************************/
unsigned chevos_get_gameid(const char* json)
{
char gameid[32];
if (cheevos_get_value(json, CHEEVOS_JSON_KEY_GAMEID, gameid, sizeof(gameid)) != 0)
return 0;
return (unsigned)strtol(gameid, NULL, 10);
}

69
cheevos/parser.h Normal file
View File

@ -0,0 +1,69 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2015-2018 - Andre Leiradella
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __RARCH_CHEEVOS_PARSER_H
#define __RARCH_CHEEVOS_PARSER_H
#include <stdint.h>
#include <stddef.h>
#include <boolean.h>
#include <retro_common_api.h>
RETRO_BEGIN_DECLS
typedef struct {
const char* title;
const char* description;
const char* badge;
const char* memaddr;
unsigned points;
unsigned id;
} cheevos_racheevo_t;
typedef struct {
const char* title;
const char* description;
const char* format;
const char* mem;
unsigned id;
} cheevos_ralboard_t;
typedef struct {
unsigned console_id;
cheevos_racheevo_t* core;
cheevos_racheevo_t* unofficial;
cheevos_ralboard_t* lboards;
unsigned core_count;
unsigned unofficial_count;
unsigned lboard_count;
} cheevos_rapatchdata_t;
typedef void (*cheevos_unlock_cb_t)(unsigned id, void* userdata);
int cheevos_get_token(const char* json, char* token, size_t length);
int cheevos_get_patchdata(const char* json, cheevos_rapatchdata_t* patchdata);
void cheevos_free_patchdata(cheevos_rapatchdata_t* patchdata);
void cheevos_deactivate_unlocks(const char* json, cheevos_unlock_cb_t unlock_cb, void* userdata);
unsigned chevos_get_gameid(const char* json);
RETRO_END_DECLS
#endif

53
cheevos/util.h Normal file
View File

@ -0,0 +1,53 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2015-2016 - Andre Leiradella
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __RARCH_CHEEVOS_UTIL_H
#define __RARCH_CHEEVOS_UTIL_H
#include <retro_common_api.h>
RETRO_BEGIN_DECLS
/*****************************************************************************
Setup - mainly for debugging
*****************************************************************************/
/* Define this macro to get extra-verbose log for cheevos. */
#undef CHEEVOS_VERBOSE
/*****************************************************************************
End of setup
*****************************************************************************/
#define CHEEVOS_TAG "[CHEEVOS]: "
#define CHEEVOS_FREE(p) do { void* q = (void*)p; if (q != NULL) free(q); } while (0)
#ifdef CHEEVOS_VERBOSE
#define CHEEVOS_LOG RARCH_LOG
#define CHEEVOS_ERR RARCH_ERR
#else
#define CHEEVOS_LOG cheevos_log
#define CHEEVOS_ERR RARCH_ERR
void cheevos_log(const char *fmt, ...);
#endif
RETRO_END_DECLS
#endif /* __RARCH_CHEEVOS_UTIL_H */

View File

@ -1,416 +0,0 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2015-2017 - Andre Leiradella
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <stdint.h>
#include <libretro.h>
#include "var.h"
#include "../retroarch.h"
#include "../core.h"
#include "../verbosity.h"
/*****************************************************************************
Parsing
*****************************************************************************/
static cheevos_var_size_t cheevos_var_parse_prefix(const char** memaddr)
{
/* Careful not to use ABCDEF here, this denotes part of an actual variable! */
const char* str = *memaddr;
cheevos_var_size_t size;
switch (toupper((unsigned char)*str++))
{
case 'M':
size = CHEEVOS_VAR_SIZE_BIT_0;
break;
case 'N':
size = CHEEVOS_VAR_SIZE_BIT_1;
break;
case 'O':
size = CHEEVOS_VAR_SIZE_BIT_2;
break;
case 'P':
size = CHEEVOS_VAR_SIZE_BIT_3;
break;
case 'Q':
size = CHEEVOS_VAR_SIZE_BIT_4;
break;
case 'R':
size = CHEEVOS_VAR_SIZE_BIT_5;
break;
case 'S':
size = CHEEVOS_VAR_SIZE_BIT_6;
break;
case 'T':
size = CHEEVOS_VAR_SIZE_BIT_7;
break;
case 'L':
size = CHEEVOS_VAR_SIZE_NIBBLE_LOWER;
break;
case 'U':
size = CHEEVOS_VAR_SIZE_NIBBLE_UPPER;
break;
case 'H':
size = CHEEVOS_VAR_SIZE_EIGHT_BITS;
break;
case 'X':
size = CHEEVOS_VAR_SIZE_THIRTYTWO_BITS;
break;
default:
str--;
/* fall through */
case ' ':
size = CHEEVOS_VAR_SIZE_SIXTEEN_BITS;
break;
}
*memaddr = str;
return size;
}
static size_t cheevos_var_reduce(size_t addr, size_t mask)
{
while (mask)
{
size_t tmp = (mask - 1) & ~mask;
addr = (addr & tmp) | ((addr >> 1) & ~tmp);
mask = (mask & (mask - 1)) >> 1;
}
return addr;
}
static size_t cheevos_var_highest_bit(size_t n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return n ^ (n >> 1);
}
void cheevos_var_parse(cheevos_var_t* var, const char** memaddr)
{
char *end = NULL;
const char *str = *memaddr;
unsigned base = 16;
var->is_bcd = false;
if (toupper((unsigned char)*str) == 'D' && str[1] == '0' && toupper((unsigned char)str[2]) == 'X')
{
/* d0x + 4 hex digits */
str += 3;
var->type = CHEEVOS_VAR_TYPE_DELTA_MEM;
}
else if (toupper((unsigned char)*str) == 'B' && str[1] == '0' && toupper((unsigned char)str[2]) == 'X')
{
/* b0x (binary-coded decimal) */
str += 3;
var->is_bcd = true;
var->type = CHEEVOS_VAR_TYPE_ADDRESS;
}
else if (*str == '0' && toupper((unsigned char)str[1]) == 'X')
{
/* 0x + 4 hex digits */
str += 2;
var->type = CHEEVOS_VAR_TYPE_ADDRESS;
}
else
{
var->type = CHEEVOS_VAR_TYPE_VALUE_COMP;
if (toupper((unsigned char)*str) == 'H')
str++;
else
{
if (toupper((unsigned char)*str) == 'V')
str++;
base = 10;
}
}
if (var->type != CHEEVOS_VAR_TYPE_VALUE_COMP)
{
var->size = cheevos_var_parse_prefix(&str);
}
var->value = (unsigned)strtol(str, &end, base);
*memaddr = end;
}
void cheevos_var_patch_addr(cheevos_var_t* var, cheevos_console_t console)
{
rarch_system_info_t *system = runloop_get_system_info();
var->bank_id = -1;
if (console == CHEEVOS_CONSOLE_NINTENDO)
{
if (var->value >= 0x0800 && var->value < 0x2000)
{
CHEEVOS_LOG(CHEEVOS_TAG "NES memory address in mirrorred RAM %X, adjusted to %X\n", var->value, var->value & 0x07ff);
var->value &= 0x07ff;
}
}
else if (console == CHEEVOS_CONSOLE_GAMEBOY_COLOR)
{
if (var->value >= 0xe000 && var->value <= 0xfdff)
{
CHEEVOS_LOG(CHEEVOS_TAG "GBC memory address in echo RAM %X, adjusted to %X\n", var->value, var->value - 0x2000);
var->value -= 0x2000;
}
}
if (system->mmaps.num_descriptors != 0)
{
const rarch_memory_descriptor_t *desc = NULL;
const rarch_memory_descriptor_t *end = NULL;
/* Patch the address to correctly map it to the mmaps */
if (console == CHEEVOS_CONSOLE_GAMEBOY_ADVANCE)
{
if (var->value < 0x8000) /* Internal RAM */
{
CHEEVOS_LOG(CHEEVOS_TAG "GBA memory address %X adjusted to %X\n", var->value, var->value + 0x3000000);
var->value += 0x3000000;
}
else /* Work RAM */
{
CHEEVOS_LOG(CHEEVOS_TAG "GBA memory address %X adjusted to %X\n", var->value, var->value + 0x2000000 - 0x8000);
var->value += 0x2000000 - 0x8000;
}
}
else if (console == CHEEVOS_CONSOLE_PC_ENGINE)
{
CHEEVOS_LOG(CHEEVOS_TAG "PCE memory address %X adjusted to %X\n", var->value, var->value + 0x1f0000);
var->value += 0x1f0000;
}
else if (console == CHEEVOS_CONSOLE_SUPER_NINTENDO)
{
if (var->value < 0x020000) /* Work RAM */
{
CHEEVOS_LOG(CHEEVOS_TAG "SNES memory address %X adjusted to %X\n", var->value, var->value + 0x7e0000);
var->value += 0x7e0000;
}
else /* Save RAM */
{
CHEEVOS_LOG(CHEEVOS_TAG "SNES memory address %X adjusted to %X\n", var->value, var->value + 0x006000 - 0x020000);
var->value += 0x006000 - 0x020000;
}
}
desc = system->mmaps.descriptors;
end = desc + system->mmaps.num_descriptors;
for (; desc < end; desc++)
{
if (((desc->core.start ^ var->value) & desc->core.select) == 0)
{
unsigned addr = var->value;
var->bank_id = (int)(desc - system->mmaps.descriptors);
var->value = (unsigned)cheevos_var_reduce(
(addr - desc->core.start) & desc->disconnect_mask,
desc->core.disconnect);
if (var->value >= desc->core.len)
var->value -= cheevos_var_highest_bit(var->value);
var->value += desc->core.offset;
CHEEVOS_LOG(CHEEVOS_TAG "address %X set to descriptor %d at offset %X\n", addr, var->bank_id + 1, var->value);
break;
}
}
}
else
{
unsigned i;
for (i = 0; i < 4; i++)
{
retro_ctx_memory_info_t meminfo;
switch (i)
{
case 0:
meminfo.id = RETRO_MEMORY_SYSTEM_RAM;
break;
case 1:
meminfo.id = RETRO_MEMORY_SAVE_RAM;
break;
case 2:
meminfo.id = RETRO_MEMORY_VIDEO_RAM;
break;
case 3:
meminfo.id = RETRO_MEMORY_RTC;
break;
}
core_get_memory(&meminfo);
if (var->value < meminfo.size)
{
var->bank_id = i;
break;
}
/* HACK subtract the correct amount of bytes to reach the save RAM */
if (i == 0 && console == CHEEVOS_CONSOLE_NINTENDO)
var->value -= 0x6000;
else
var->value -= meminfo.size;
}
}
}
/*****************************************************************************
Testing
*****************************************************************************/
uint8_t* cheevos_var_get_memory(const cheevos_var_t* var)
{
uint8_t* memory = NULL;
if (var->bank_id >= 0)
{
rarch_system_info_t* system = runloop_get_system_info();
if (system->mmaps.num_descriptors != 0)
memory = (uint8_t*)system->mmaps.descriptors[var->bank_id].core.ptr;
else
{
retro_ctx_memory_info_t meminfo = {NULL, 0, 0};
switch (var->bank_id)
{
case 0:
meminfo.id = RETRO_MEMORY_SYSTEM_RAM;
break;
case 1:
meminfo.id = RETRO_MEMORY_SAVE_RAM;
break;
case 2:
meminfo.id = RETRO_MEMORY_VIDEO_RAM;
break;
case 3:
meminfo.id = RETRO_MEMORY_RTC;
break;
default:
CHEEVOS_ERR(CHEEVOS_TAG "invalid bank id: %s\n", var->bank_id);
break;
}
core_get_memory(&meminfo);
memory = (uint8_t*)meminfo.data;
}
if (memory)
memory += var->value;
}
return memory;
}
unsigned cheevos_var_get_value(cheevos_var_t* var)
{
const uint8_t* memory = NULL;
unsigned value = 0;
switch (var->type)
{
case CHEEVOS_VAR_TYPE_VALUE_COMP:
value = var->value;
break;
case CHEEVOS_VAR_TYPE_ADDRESS:
case CHEEVOS_VAR_TYPE_DELTA_MEM:
memory = cheevos_var_get_memory(var);
if (memory)
{
value = memory[0];
switch (var->size)
{
case CHEEVOS_VAR_SIZE_BIT_0:
value &= 1;
break;
case CHEEVOS_VAR_SIZE_BIT_1:
value = (value >> 1) & 1;
break;
case CHEEVOS_VAR_SIZE_BIT_2:
value = (value >> 2) & 1;
break;
case CHEEVOS_VAR_SIZE_BIT_3:
value = (value >> 3) & 1;
break;
case CHEEVOS_VAR_SIZE_BIT_4:
value = (value >> 4) & 1;
break;
case CHEEVOS_VAR_SIZE_BIT_5:
value = (value >> 5) & 1;
break;
case CHEEVOS_VAR_SIZE_BIT_6:
value = (value >> 6) & 1;
break;
case CHEEVOS_VAR_SIZE_BIT_7:
value = (value >> 7) & 1;
break;
case CHEEVOS_VAR_SIZE_NIBBLE_LOWER:
value &= 0x0f;
break;
case CHEEVOS_VAR_SIZE_NIBBLE_UPPER:
value = (value >> 4) & 0x0f;
break;
case CHEEVOS_VAR_SIZE_EIGHT_BITS:
break;
case CHEEVOS_VAR_SIZE_SIXTEEN_BITS:
value |= memory[1] << 8;
break;
case CHEEVOS_VAR_SIZE_THIRTYTWO_BITS:
value |= memory[1] << 8;
value |= memory[2] << 16;
value |= memory[3] << 24;
break;
}
}
if (var->type == CHEEVOS_VAR_TYPE_DELTA_MEM)
{
unsigned previous = var->previous;
var->previous = value;
value = previous;
}
break;
case CHEEVOS_VAR_TYPE_DYNAMIC_VAR:
/* We shouldn't get here... */
break;
}
if(var->is_bcd)
return (((value >> 4) & 0xf) * 10) + (value & 0xf);
else
return value;
}

View File

@ -1,78 +0,0 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2015-2017 - Andre Leiradella
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __RARCH_CHEEVOS_VAR_H
#define __RARCH_CHEEVOS_VAR_H
#include <stdint.h>
#include "cheevos.h"
#include <retro_common_api.h>
RETRO_BEGIN_DECLS
typedef enum
{
CHEEVOS_VAR_SIZE_BIT_0 = 0,
CHEEVOS_VAR_SIZE_BIT_1,
CHEEVOS_VAR_SIZE_BIT_2,
CHEEVOS_VAR_SIZE_BIT_3,
CHEEVOS_VAR_SIZE_BIT_4,
CHEEVOS_VAR_SIZE_BIT_5,
CHEEVOS_VAR_SIZE_BIT_6,
CHEEVOS_VAR_SIZE_BIT_7,
CHEEVOS_VAR_SIZE_NIBBLE_LOWER,
CHEEVOS_VAR_SIZE_NIBBLE_UPPER,
/* Byte, */
CHEEVOS_VAR_SIZE_EIGHT_BITS, /* =Byte, */
CHEEVOS_VAR_SIZE_SIXTEEN_BITS,
CHEEVOS_VAR_SIZE_THIRTYTWO_BITS
} cheevos_var_size_t;
typedef enum
{
/* compare to the value of a live address in RAM */
CHEEVOS_VAR_TYPE_ADDRESS = 0,
/* a number. assume 32 bit */
CHEEVOS_VAR_TYPE_VALUE_COMP,
/* the value last known at this address. */
CHEEVOS_VAR_TYPE_DELTA_MEM,
/* a custom user-set variable */
CHEEVOS_VAR_TYPE_DYNAMIC_VAR
} cheevos_var_type_t;
typedef struct
{
cheevos_var_size_t size;
cheevos_var_type_t type;
int bank_id;
bool is_bcd;
unsigned value;
unsigned previous;
} cheevos_var_t;
void cheevos_var_parse(cheevos_var_t* var, const char** memaddr);
void cheevos_var_patch_addr(cheevos_var_t* var, cheevos_console_t console);
uint8_t* cheevos_var_get_memory(const cheevos_var_t* var);
unsigned cheevos_var_get_value(cheevos_var_t* var);
RETRO_END_DECLS
#endif /* __RARCH_CHEEVOS_VAR_H */

View File

@ -41,7 +41,7 @@
#ifdef HAVE_CHEEVOS
#include "cheevos/cheevos.h"
#include "cheevos/var.h"
#include "cheevos/fixup.h"
#endif
#ifdef HAVE_DISCORD
@ -262,14 +262,13 @@ static bool command_version(const char* arg)
#define SMY_CMD_STR "READ_CORE_RAM"
static bool command_read_ram(const char *arg)
{
cheevos_var_t var;
unsigned i;
char *reply = NULL;
const uint8_t * data = NULL;
char *reply_at = NULL;
unsigned int nbytes = 0;
unsigned int alloc_size = 0;
int addr = -1;
unsigned int addr = -1;
if (sscanf(arg, "%x %d", &addr, &nbytes) != 2)
return true;
@ -278,9 +277,7 @@ static bool command_read_ram(const char *arg)
reply[0] = '\0';
reply_at = reply + sprintf(reply, SMY_CMD_STR " %x", addr);
var.value = addr;
cheevos_var_patch_addr(&var, cheevos_get_console());
data = cheevos_var_get_memory(&var);
data = cheevos_patch_address(addr, cheevos_get_console());
if (data)
{
@ -302,14 +299,9 @@ static bool command_read_ram(const char *arg)
static bool command_write_ram(const char *arg)
{
cheevos_var_t var;
unsigned nbytes = 0;
uint8_t *data = NULL;
var.value = strtoul(arg, (char**)&arg, 16);
cheevos_var_patch_addr(&var, cheevos_get_console());
data = cheevos_var_get_memory(&var);
unsigned int addr = strtoul(arg, (char**)&arg, 16);
uint8_t *data = (uint8_t *)cheevos_patch_address(addr, cheevos_get_console());
if (data)
{