From fad2acf1ee496534cfae91c0b2d731e522e70f81 Mon Sep 17 00:00:00 2001
From: Andre Leiradella <andre@leiradella.com>
Date: Mon, 23 May 2016 23:35:57 -0300
Subject: [PATCH 1/2] added all console ids; added fixup for pce; fixed mmap
 matching

---
 cheevos.c | 47 ++++++++++++++++++++++++-----------------------
 1 file changed, 24 insertions(+), 23 deletions(-)

diff --git a/cheevos.c b/cheevos.c
index e574458d82..0c95a0525e 100644
--- a/cheevos.c
+++ b/cheevos.c
@@ -42,7 +42,7 @@
 #include "verbosity.h"
 
 /* Define this macro to deactivate awarded cheevos. */
-#define CHEEVOS_DEACTIVATE
+#undef CHEEVOS_DEACTIVATE
 
 /* Define this macro to log URLs (will log the user token). */
 #undef CHEEVOS_LOG_URLS
@@ -67,9 +67,17 @@
 
 enum
 {
-   CHEEVOS_CONSOLE_GAMEBOY = 4,
-   CHEEVOS_CONSOLE_GAMEBOY_ADVANCED = 5,
-   CHEEVOS_CONSOLE_GAMEBOY_COLOR = 6,
+   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,
 };
 
 enum
@@ -645,8 +653,7 @@ static void cheevos_parse_var(cheevos_var_t *var, const char **memaddr)
          const struct retro_memory_descriptor *desc;
          const struct retro_memory_descriptor *end;
          
-#if 0
-         if (cheevos_locals.console_id == CHEEVOS_CONSOLE_GAMEBOY_ADVANCED)
+         if (cheevos_locals.console_id == CHEEVOS_CONSOLE_GAMEBOY_ADVANCE)
          {
             /* Patch the address to correctly map it to the mmaps */
             if (var->value < 0x8000)
@@ -660,14 +667,17 @@ static void cheevos_parse_var(cheevos_var_t *var, const char **memaddr)
                var->value += 0x2000000 - 0x8000;
             }
          }
-#endif
+         else if (cheevos_locals.console_id == CHEEVOS_CONSOLE_PC_ENGINE)
+         {
+            var->value += 0x1f0000;
+         }
         
          desc = system->mmaps.descriptors;
          end = desc + system->mmaps.num_descriptors;
          
          for (; desc < end; desc++)
          {
-            if (var->value >= desc->start && var->value < (desc->start + desc->len))
+            if ((var->value & desc->select) == desc->start)
             {
                var->bank_id = desc - system->mmaps.descriptors;
                var->value = var->value - desc->start + desc->offset;
@@ -1955,28 +1965,20 @@ static unsigned cheevos_find_game_id_nes(
       {
          53, 198, 228
       };
-      bool round     = true;
-      RFILE *file    = NULL;
+      bool round  = true;
+      RFILE *file = filestream_open(info->path, RFILE_MODE_READ, 0);
       uint8_t * data = (uint8_t *) malloc(rom_size << 14);
       
-      if (!data)
+      if (!file || !data)
          return 0;
 
-      file = filestream_open(info->path, RFILE_MODE_READ, 0);
-
-      if (!file)
-      {
-         free(data);
-         filestream_close(file);
-         return 0;
-      }
-
       /* from fceu core - need it for a correctly md5 sum */
       memset(data, 0xFF, rom_size << 14);
 
       /* from fceu core - compute size using the cart mapper */
-      mapper_no  = (header.rom_type >> 4);
+      mapper_no = (header.rom_type >> 4);
 	   mapper_no |= (header.rom_type2 & 0xF0);
+      
 
       for (i = 0; i != sizeof(not_power2) / sizeof(not_power2[0]); ++i)
       {
@@ -1985,8 +1987,7 @@ static unsigned cheevos_find_game_id_nes(
           * since PRGCartMapping wants ROM_size to be to the power of 2
           * so instead if not to power of 2, we just use head.ROM_size when
           * we use FCEU_read. */
-         if (not_power2[i] == mapper_no)
-         {
+         if (not_power2[i] == mapper_no) {
             round = false;
             break;
          }

From d791da9507d582ae8e6d37667dd02904fbc78577 Mon Sep 17 00:00:00 2001
From: Andre Leiradella <andre@leiradella.com>
Date: Fri, 27 May 2016 00:06:03 -0300
Subject: [PATCH 2/2] undefined debug macros

---
 cheevos.c                                   | 146 +++++++++++---------
 libretro-common/include/queues/task_queue.h |   3 +
 menu/cbs/menu_cbs_ok.c                      |   4 +-
 retroarch.c                                 |  21 ---
 tasks/task_http.c                           |   3 +-
 tasks/tasks_internal.c                      |   2 +-
 tasks/tasks_internal.h                      |   2 +-
 7 files changed, 87 insertions(+), 94 deletions(-)

diff --git a/cheevos.c b/cheevos.c
index 0c95a0525e..c58e30f717 100644
--- a/cheevos.c
+++ b/cheevos.c
@@ -19,7 +19,6 @@
 #include <formats/jsonsax.h>
 #include <streams/file_stream.h>
 #include <rhash.h>
-#include <rthreads/async_job.h>
 #include <libretro.h>
 
 #include "cheevos.h"
@@ -28,6 +27,7 @@
 #include "libretro.h"
 #include "system.h"
 #include "network/net_http_special.h"
+#include "tasks/tasks_internal.h"
 #include "configuration.h"
 #include "performance_counters.h"
 #include "msg_hash.h"
@@ -41,8 +41,8 @@
 
 #include "verbosity.h"
 
-/* Define this macro to deactivate awarded cheevos. */
-#undef CHEEVOS_DEACTIVATE
+/* Define this macro to prevent cheevos from being deactivated. */
+#undef CHEEVOS_DONT_DEACTIVATE
 
 /* Define this macro to log URLs (will log the user token). */
 #undef CHEEVOS_LOG_URLS
@@ -50,6 +50,9 @@
 /* Define this macro to dump all cheevos' addresses. */
 #undef CHEEVOS_DUMP_ADDRS
 
+/* Define this macro to remove HTTP timeouts. */
+#undef CHEEVOS_NO_TIMEOUT
+
 #define JSON_KEY_GAMEID       0xb4960eecU
 #define JSON_KEY_ACHIEVEMENTS 0x69749ae1U
 #define JSON_KEY_ID           0x005973f2U
@@ -276,10 +279,6 @@ static cheevos_locals_t cheevos_locals =
 static int cheats_are_enabled  = 0;
 static int cheats_were_enabled = 0;
 
-/* forward declaration */
-
-int retroarch_async_job_add(async_task_t task, void *payload);
-
 /*****************************************************************************
 Supporting functions.
 *****************************************************************************/
@@ -300,7 +299,12 @@ static int cheevos_http_get(const char **result, size_t *size,
       const char *url, retro_time_t *timeout)
 {
    const char *msg = NULL;
+   
+#ifdef CHEEVOS_NO_TIMEOUT
+   int ret         = net_http_get(result, size, url, NULL);
+#else
    int ret         = net_http_get(result, size, url, timeout);
+#endif
    
    switch (ret)
    {
@@ -1424,38 +1428,39 @@ static int cheevos_login(retro_time_t *timeout)
    return -1;
 }
 
-static void cheevos_unlocker(void *payload)
+static void cheevos_make_unlock_url(const cheevo_t *cheevo, char* url, size_t url_size)
 {
-   char request[256];
-   const char *result;
    settings_t *settings = config_get_ptr();
-   unsigned cheevo_id   = (unsigned)(uintptr_t)payload;
 
-   if (!cheevos_login(NULL))
-   {
-      snprintf(
-         request, sizeof(request),
-         "http://retroachievements.org/dorequest.php?r=awardachievement&u=%s&t=%s&a=%u&h=%d",
-         settings->cheevos.username, cheevos_locals.token, cheevo_id, settings->cheevos.hardcore_mode_enable
-      );
+   snprintf(
+      url, url_size,
+      "http://retroachievements.org/dorequest.php?r=awardachievement&u=%s&t=%s&a=%u&h=%d",
+      settings->cheevos.username, cheevos_locals.token, cheevo->id, settings->cheevos.hardcore_mode_enable
+   );
+
+   url[url_size - 1] = 0;
 
-      request[sizeof(request) - 1] = 0;
-      
 #ifdef CHEEVOS_LOG_URLS
-      RARCH_LOG("CHEEVOS url to award the cheevo: %s\n", request);
+   RARCH_LOG("CHEEVOS url to award the cheevo: %s\n", url);
 #endif
-      
-      if (!cheevos_http_get(&result, NULL, request, NULL))
-      {
-         RARCH_LOG("CHEEVOS awarded achievement %u: %s\n", cheevo_id, result);
-         free((void*)result);
-      }
-      else
-      {
-         RARCH_ERR("CHEEVOS error awarding achievement %u, will retry...\n", cheevo_id);
-         /* re-schedule */
-         retroarch_async_job_add(cheevos_unlocker, (void*)(uintptr_t)cheevo_id);
-      }
+}
+
+static void cheevos_unlocked(void *task_data, void *user_data, const char *error)
+{
+   cheevo_t *cheevo = (cheevo_t *)user_data;
+
+   if (error == NULL)
+   {
+      RARCH_LOG("CHEEVOS awarded achievement %u\n", cheevo->id);
+   }
+   else
+   {
+      char url[256];
+
+      RARCH_ERR("CHEEVOS error awarding achievement %u, retrying\n", cheevo->id);
+
+      cheevos_make_unlock_url(cheevo, url, sizeof(url));
+      rarch_task_push_http_transfer(url, true, NULL, cheevos_unlocked, cheevo);
    }
 }
 
@@ -1468,15 +1473,18 @@ static void cheevos_test_cheevo_set(const cheevoset_t *set)
    {
       if (cheevo->active && cheevos_test_cheevo(cheevo))
       {
-         RARCH_LOG("CHEEVOS awarding cheevo %s (%s)\n", cheevo->title, cheevo->description);
+         settings_t *settings = config_get_ptr();
+         char url[256];
+
+         cheevo->active = 0;
+
+         RARCH_LOG("CHEEVOS awarding cheevo %u: %s (%s)\n", cheevo->id, cheevo->title, cheevo->description);
 
          runloop_msg_queue_push(cheevo->title, 0, 3 * 60, false);
          runloop_msg_queue_push(cheevo->description, 0, 5 * 60, false);
 
-         retroarch_async_job_add(cheevos_unlocker,
-               (void*)(uintptr_t)cheevo->id);
-
-         cheevo->active = 0;
+         cheevos_make_unlock_url(cheevo, url, sizeof(url));
+         rarch_task_push_http_transfer(url, true, NULL, cheevos_unlocked, cheevo);
       }
    }
 }
@@ -1598,43 +1606,43 @@ static unsigned cheevos_get_game_id(unsigned char *hash, retro_time_t *timeout)
    return 0;
 }
 
-static void cheevos_playing(void *payload)
+static void cheevos_make_playing_url(unsigned game_id, char* url, size_t url_size)
 {
-   char request[256];
-   const char* json;
-   unsigned game_id     = (unsigned)(uintptr_t)payload;
    settings_t *settings = config_get_ptr();
-   
-   if (!cheevos_login(NULL))
-   {
-      snprintf(
-         request, sizeof(request),
-         "http://retroachievements.org/dorequest.php?r=postactivity&u=%s&t=%s&a=3&m=%u",
-         settings->cheevos.username, cheevos_locals.token, game_id
-      );
 
-      request[sizeof(request) - 1] = 0;
-      
+   snprintf(
+      url, url_size,
+      "http://retroachievements.org/dorequest.php?r=postactivity&u=%s&t=%s&a=3&m=%u",
+      settings->cheevos.username, cheevos_locals.token, game_id
+   );
+
+   url[url_size - 1] = 0;
+
 #ifdef CHEEVOS_LOG_URLS
-      RARCH_LOG("CHEEVOS url to post the 'playing' activity: %s\n", request);
+   RARCH_LOG("CHEEVOS url to post the 'playing' activity: %s\n", url);
 #endif
+}
 
-      if (!cheevos_http_get(&json, NULL, request, NULL))
-      {
-         free((void*)json);
-         RARCH_LOG("CHEEVOS posted playing game %u activity\n", game_id);
-         return;
-      }
-      else
-      {
-         RARCH_ERR("CHEEVOS error posting playing game %u activity, will retry\n", game_id);
-         /* re-schedule */
-         retroarch_async_job_add(cheevos_playing, (void*)(uintptr_t)game_id);
-      }
+static void cheevos_playing(void *task_data, void *user_data, const char *error)
+{
+   unsigned game_id = (unsigned)(uintptr_t)user_data;
+
+   if (error == NULL)
+   {
+      RARCH_LOG("CHEEVOS posted playing game %u activity\n", game_id);
+   }
+   else
+   {
+      char url[256];
+
+      RARCH_ERR("CHEEVOS error posting playing game %u activity, will retry\n", game_id);
+
+      cheevos_make_playing_url(game_id, url, sizeof(url));
+      rarch_task_push_http_transfer(url, true, NULL, cheevos_playing, (void*)(uintptr_t)game_id);
    }
 }
 
-#ifdef CHEEVOS_DEACTIVATE
+#ifndef CHEEVOS_DONT_DEACTIVATE
 static int cheevos_deactivate__json_index(void *userdata, unsigned int index)
 {
    cheevos_deactivate_t *ud = (cheevos_deactivate_t*)userdata;
@@ -1697,7 +1705,7 @@ static int cheevos_deactivate_unlocks(unsigned game_id, retro_time_t *timeout)
 {
    /* Only call this function after the cheevos have been loaded. */
    
-#ifdef CHEEVOS_DEACTIVATE
+#ifndef CHEEVOS_DONT_DEACTIVATE
    static const jsonsax_handlers_t handlers =
    {
       NULL,
@@ -2061,6 +2069,7 @@ bool cheevos_load(const void *data)
    const char *json     = NULL;
    retro_time_t timeout = 5000000;
    unsigned game_id     = 0;
+   char url[256];
    settings_t *settings = config_get_ptr();
    const struct retro_game_info *info = (const struct retro_game_info*)data;
    
@@ -2156,7 +2165,8 @@ bool cheevos_load(const void *data)
          free((void*)json);
          cheevos_locals.loaded = 1;
          
-         retroarch_async_job_add(cheevos_playing, (void*)(uintptr_t)game_id);
+         cheevos_make_playing_url(game_id, url, sizeof(url));
+         rarch_task_push_http_transfer(url, true, NULL, cheevos_playing, (void*)(uintptr_t)game_id);
          return true;
       }
       
diff --git a/libretro-common/include/queues/task_queue.h b/libretro-common/include/queues/task_queue.h
index aedd8456c3..87cb0fa861 100644
--- a/libretro-common/include/queues/task_queue.h
+++ b/libretro-common/include/queues/task_queue.h
@@ -135,6 +135,9 @@ struct retro_task
     * to signal the task *must* end. */
    bool cancelled;
 
+   /* if true no OSD messages will be displayed. */
+   bool mute;
+
    /* created by the handler, destroyed by the user */
    void *task_data;
 
diff --git a/menu/cbs/menu_cbs_ok.c b/menu/cbs/menu_cbs_ok.c
index e2b890de50..bee11a2d40 100644
--- a/menu/cbs/menu_cbs_ok.c
+++ b/menu/cbs/menu_cbs_ok.c
@@ -1515,7 +1515,7 @@ static int action_ok_download_generic(const char *path,
    transf->type_hash = menu_hash_calculate(type_msg);
    strlcpy(transf->path, path, sizeof(transf->path));
 
-   rarch_task_push_http_transfer(s3, type_msg, cb_generic_download, transf);
+   rarch_task_push_http_transfer(s3, false, type_msg, cb_generic_download, transf);
 #endif
    return 0;
 }
@@ -1777,7 +1777,7 @@ static int generic_action_ok_network(const char *path,
 #endif
    }
 
-   rarch_task_push_http_transfer(url_path, url_label, callback, NULL);
+   rarch_task_push_http_transfer(url_path, false, url_label, callback, NULL);
 
    return generic_action_ok_displaylist_push(path,
          label, type, idx, entry_idx, type_id2);
diff --git a/retroarch.c b/retroarch.c
index 94cf15ce74..687961809c 100644
--- a/retroarch.c
+++ b/retroarch.c
@@ -26,9 +26,6 @@
 #include <boolean.h>
 #include <string/stdstring.h>
 #include <lists/string_list.h>
-#ifdef HAVE_THREADS
-#include <rthreads/async_job.h>
-#endif
 
 #ifdef _WIN32
 #ifdef _XBOX
@@ -115,10 +112,6 @@ static char current_savefile_dir[PATH_MAX_LENGTH];
 static char error_string[PATH_MAX_LENGTH];
 static jmp_buf error_sjlj_context;
 
-#ifdef HAVE_THREADS
-static async_job_t *async_jobs;
-#endif
-
 #define _PSUPP(var, name, desc) printf("  %s:\n\t\t%s: %s\n", name, desc, _##var##_supp ? "yes" : "no")
 
 static void retroarch_print_features(void)
@@ -178,13 +171,6 @@ static void retroarch_print_features(void)
 }
 #undef _PSUPP
 
-#ifdef HAVE_THREADS
-int retroarch_async_job_add(async_task_t task, void *payload)
-{
-   return async_job_add(async_jobs, task, payload);
-}
-#endif
-
 static void retroarch_print_version(void)
 {
    char str[PATH_MAX_LENGTH] = {0};
@@ -1462,10 +1448,6 @@ bool rarch_ctl(enum rarch_ctl_state state, void *data)
          command_event(CMD_EVENT_SAVEFILES_DEINIT, NULL);
 
          rarch_ctl(RARCH_CTL_UNSET_INITED, NULL);
-#ifdef HAVE_THREADS
-         async_job_free(async_jobs);
-         async_jobs = NULL;
-#endif
          break;
       case RARCH_CTL_INIT:
          rarch_ctl(RARCH_CTL_DEINIT, NULL);
@@ -1476,9 +1458,6 @@ bool rarch_ctl(enum rarch_ctl_state state, void *data)
                settings->input.libretro_device[i] = RETRO_DEVICE_JOYPAD;
          }
          runloop_ctl(RUNLOOP_CTL_MSG_QUEUE_INIT, NULL);
-#ifdef HAVE_THREADS
-         async_jobs = async_job_new();
-#endif
          break;
       case RARCH_CTL_SET_PATHS_REDIRECT:
          if(settings->sort_savestates_enable || settings->sort_savefiles_enable)
diff --git a/tasks/task_http.c b/tasks/task_http.c
index 3d9ff0e9b3..730d32144e 100644
--- a/tasks/task_http.c
+++ b/tasks/task_http.c
@@ -232,7 +232,7 @@ static bool rarch_task_http_retriever(retro_task_t *task, void *data)
    return true;
 }
 
-void *rarch_task_push_http_transfer(const char *url, const char *type,
+void *rarch_task_push_http_transfer(const char *url, bool mute, const char *type,
       retro_task_callback_t cb, void *user_data)
 {
    char tmp[PATH_MAX_LENGTH];
@@ -280,6 +280,7 @@ void *rarch_task_push_http_transfer(const char *url, const char *type,
 
    t->handler              = rarch_task_http_transfer_handler;
    t->state                = http;
+   t->mute                 = mute;
    t->callback             = cb;
    t->user_data            = user_data;
    t->progress             = -1;
diff --git a/tasks/tasks_internal.c b/tasks/tasks_internal.c
index 2ae5e44b7b..0ba68a28ba 100644
--- a/tasks/tasks_internal.c
+++ b/tasks/tasks_internal.c
@@ -37,7 +37,7 @@ static void task_queue_msg_push(unsigned prio, unsigned duration,
 
 void task_queue_push_progress(retro_task_t *task)
 {
-   if (task->title)
+   if (task->title && !task->mute)
    {
       if (task->finished)
       {
diff --git a/tasks/tasks_internal.h b/tasks/tasks_internal.h
index 097b671bb3..84b3e93bee 100644
--- a/tasks/tasks_internal.h
+++ b/tasks/tasks_internal.h
@@ -69,7 +69,7 @@ typedef struct nbio_handle
 } nbio_handle_t;
 
 #ifdef HAVE_NETWORKING
-void *rarch_task_push_http_transfer(const char *url, const char *type,
+void *rarch_task_push_http_transfer(const char *url, bool mute, const char *type,
       retro_task_callback_t cb, void *userdata);
 
 task_retriever_info_t *http_task_get_transfer_list(void);