diff --git a/command.c b/command.c index ffc761502e..dc4c5d2272 100644 --- a/command.c +++ b/command.c @@ -46,6 +46,7 @@ #include "audio/audio_driver.h" #include "command.h" +#include "core_info.h" #include "cheat_manager.h" #include "content.h" #include "dynamic.h" @@ -693,6 +694,123 @@ uint8_t *command_memory_get_pointer( *max_bytes = 0; return NULL; } + +bool command_get_status(command_t *cmd, const char* arg) +{ + char reply[4096] = {0}; + bool contentless = false; + bool is_inited = false; + runloop_state_t *runloop_st = runloop_state_get_ptr(); + + content_get_status(&contentless, &is_inited); + + if (!is_inited) + strcpy_literal(reply, "GET_STATUS CONTENTLESS"); + else + { + /* add some content info */ + const char *status = "PLAYING"; + const char *content_name = path_basename(path_get(RARCH_PATH_BASENAME)); /* filename only without ext */ + int content_crc32 = content_get_crc(); + const char* system_id = NULL; + core_info_t *core_info = NULL; + + core_info_get_current_core(&core_info); + + if (runloop_st->paused) + status = "PAUSED"; + if (core_info) + system_id = core_info->system_id; + if (!system_id) + system_id = runloop_st->system.info.library_name; + + snprintf(reply, sizeof(reply), "GET_STATUS %s %s,%s,crc32=%x\n", status, system_id, content_name, content_crc32); + } + + cmd->replier(cmd, reply, strlen(reply)); + + return true; +} + +bool command_read_memory(command_t *cmd, const char *arg) +{ + unsigned i; + char* reply = NULL; + char* reply_at = NULL; + const uint8_t* data = NULL; + unsigned int nbytes = 0; + unsigned int alloc_size = 0; + unsigned int address = -1; + size_t len = 0; + unsigned int max_bytes = 0; + runloop_state_t *runloop_st = runloop_state_get_ptr(); + const rarch_system_info_t* system = &runloop_st->system; + + if (sscanf(arg, "%x %u", &address, &nbytes) != 2) + return false; + + /* Ensure large enough to return all requested bytes or an error message */ + alloc_size = 64 + nbytes * 3; + reply = (char*)malloc(alloc_size); + reply_at = reply + snprintf(reply, alloc_size - 1, "READ_CORE_MEMORY %x", address); + + data = command_memory_get_pointer(system, address, &max_bytes, 0, reply_at, alloc_size - strlen(reply)); + + if (data) + { + if (nbytes > max_bytes) + nbytes = max_bytes; + + for (i = 0; i < nbytes; i++) + snprintf(reply_at + 3 * i, 4, " %02X", data[i]); + + reply_at[3 * nbytes] = '\n'; + len = reply_at + 3 * nbytes + 1 - reply; + } + else + len = strlen(reply); + + cmd->replier(cmd, reply, len); + free(reply); + return true; +} + +bool command_write_memory(command_t *cmd, const char *arg) +{ + unsigned int address = (unsigned int)strtoul(arg, (char**)&arg, 16); + unsigned int max_bytes = 0; + char reply[128] = ""; + runloop_state_t *runloop_st = runloop_state_get_ptr(); + const rarch_system_info_t + *system = &runloop_st->system; + char *reply_at = reply + snprintf(reply, sizeof(reply) - 1, "WRITE_CORE_MEMORY %x", address); + uint8_t *data = command_memory_get_pointer(system, address, &max_bytes, 1, reply_at, sizeof(reply) - strlen(reply) - 1); + + if (data) + { + uint8_t* start = data; + while (*arg && max_bytes > 0) + { + --max_bytes; + *data = strtoul(arg, (char**)&arg, 16); + data++; + } + + snprintf(reply_at, sizeof(reply) - strlen(reply) - 1, + " %u\n", (unsigned)(data - start)); + +#ifdef HAVE_CHEEVOS + if (rcheevos_hardcore_active()) + { + RARCH_LOG("Achievements hardcore mode disabled by WRITE_CORE_MEMORY\n"); + rcheevos_pause_hardcore(); + } +#endif + } + + cmd->replier(cmd, reply, strlen(reply)); + return true; +} #endif void command_event_set_volume( diff --git a/retroarch.c b/retroarch.c index 5c1b31a928..b2f12b93d2 100644 --- a/retroarch.c +++ b/retroarch.c @@ -4440,42 +4440,6 @@ static void runloop_msg_queue_init(void) /* COMMAND */ #ifdef HAVE_COMMAND -bool command_get_status(command_t *cmd, const char* arg) -{ - char reply[4096] = {0}; - bool contentless = false; - bool is_inited = false; - - content_get_status(&contentless, &is_inited); - - if (!is_inited) - strcpy_literal(reply, "GET_STATUS CONTENTLESS"); - else - { - /* add some content info */ - const char *status = "PLAYING"; - const char *content_name = path_basename(path_get(RARCH_PATH_BASENAME)); /* filename only without ext */ - int content_crc32 = content_get_crc(); - const char* system_id = NULL; - core_info_t *core_info = NULL; - - core_info_get_current_core(&core_info); - - if (runloop_state.paused) - status = "PAUSED"; - if (core_info) - system_id = core_info->system_id; - if (!system_id) - system_id = runloop_state.system.info.library_name; - - snprintf(reply, sizeof(reply), "GET_STATUS %s %s,%s,crc32=%x\n", status, system_id, content_name, content_crc32); - } - - cmd->replier(cmd, reply, strlen(reply)); - - return true; -} - bool command_get_config_param(command_t *cmd, const char* arg) { char reply[8192] = {0}; @@ -4516,84 +4480,6 @@ bool command_get_config_param(command_t *cmd, const char* arg) cmd->replier(cmd, reply, strlen(reply)); return true; } - -bool command_read_memory(command_t *cmd, const char *arg) -{ - unsigned i; - char* reply = NULL; - char* reply_at = NULL; - const uint8_t* data = NULL; - unsigned int nbytes = 0; - unsigned int alloc_size = 0; - unsigned int address = -1; - size_t len = 0; - unsigned int max_bytes = 0; - const rarch_system_info_t* system = &runloop_state.system; - - if (sscanf(arg, "%x %u", &address, &nbytes) != 2) - return false; - - /* Ensure large enough to return all requested bytes or an error message */ - alloc_size = 64 + nbytes * 3; - reply = (char*)malloc(alloc_size); - reply_at = reply + snprintf(reply, alloc_size - 1, "READ_CORE_MEMORY %x", address); - - data = command_memory_get_pointer(system, address, &max_bytes, 0, reply_at, alloc_size - strlen(reply)); - - if (data) - { - if (nbytes > max_bytes) - nbytes = max_bytes; - - for (i = 0; i < nbytes; i++) - snprintf(reply_at + 3 * i, 4, " %02X", data[i]); - - reply_at[3 * nbytes] = '\n'; - len = reply_at + 3 * nbytes + 1 - reply; - } - else - len = strlen(reply); - - cmd->replier(cmd, reply, len); - free(reply); - return true; -} - -bool command_write_memory(command_t *cmd, const char *arg) -{ - unsigned int address = (unsigned int)strtoul(arg, (char**)&arg, 16); - unsigned int max_bytes = 0; - char reply[128] = ""; - const rarch_system_info_t - *system = &runloop_state.system; - char *reply_at = reply + snprintf(reply, sizeof(reply) - 1, "WRITE_CORE_MEMORY %x", address); - uint8_t *data = command_memory_get_pointer(system, address, &max_bytes, 1, reply_at, sizeof(reply) - strlen(reply) - 1); - - if (data) - { - uint8_t* start = data; - while (*arg && max_bytes > 0) - { - --max_bytes; - *data = strtoul(arg, (char**)&arg, 16); - data++; - } - - snprintf(reply_at, sizeof(reply) - strlen(reply) - 1, - " %u\n", (unsigned)(data - start)); - -#ifdef HAVE_CHEEVOS - if (rcheevos_hardcore_active()) - { - RARCH_LOG("Achievements hardcore mode disabled by WRITE_CORE_MEMORY\n"); - rcheevos_pause_hardcore(); - } -#endif - } - - cmd->replier(cmd, reply, strlen(reply)); - return true; -} #endif #if defined(HAVE_CG) || defined(HAVE_GLSL) || defined(HAVE_SLANG) || defined(HAVE_HLSL) @@ -4606,7 +4492,8 @@ static bool retroarch_apply_shader( char msg[256]; video_driver_state_t *video_st = video_state_get_ptr(); - const char *core_name = runloop_state.system.info.library_name; + runloop_state_t *runloop_st = runloop_state_get_ptr(); + const char *core_name = runloop_st->system.info.library_name; const char *preset_file = NULL; #ifdef HAVE_MENU struct video_shader *shader = menu_shader_get();