Example #1
0
static int cheevos_parse(const char *json)
{
   static const jsonsax_handlers_t handlers =
   {
      NULL,
      NULL,
      NULL,
      cheevos_read__json_end_object,
      NULL,
      cheevos_read__json_end_array,
      cheevos_read__json_key,
      NULL,
      cheevos_read__json_string,
      cheevos_read__json_number,
      NULL,
      NULL
   };

   static int initialize = 1;

   unsigned core_count, unofficial_count;
   cheevos_readud_t ud;
   settings_t *settings         = config_get_ptr();

   /* Just return OK if cheevos are disabled. */
   if (!settings->cheevos.enable)
      return 0;

   /* Count the number of achievements in the JSON file. */

   if (cheevos_count_cheevos(json, &core_count, &unofficial_count) != JSONSAX_OK)
      return -1;

   /* Allocate the achievements. */

   cheevos_locals.core.cheevos = (cheevo_t*)malloc(core_count * sizeof(cheevo_t));
   cheevos_locals.core.count = core_count;

   cheevos_locals.unofficial.cheevos = (cheevo_t*)malloc(unofficial_count * sizeof(cheevo_t));
   cheevos_locals.unofficial.count = unofficial_count;

   if (!cheevos_locals.core.cheevos || !cheevos_locals.unofficial.cheevos)
   {
      free((void*)cheevos_locals.core.cheevos);
      free((void*)cheevos_locals.unofficial.cheevos);
      cheevos_locals.core.count = cheevos_locals.unofficial.count = 0;

      return -1;
   }

   memset((void*)cheevos_locals.core.cheevos, 0, core_count * sizeof(cheevo_t));
   memset((void*)cheevos_locals.unofficial.cheevos, 0, unofficial_count * sizeof(cheevo_t));

   /* Load the achievements. */

   ud.in_cheevos = 0;
   ud.field = NULL;
   ud.core_count = 0;
   ud.unofficial_count = 0;

   if (!jsonsax_parse(json, &handlers, (void*)&ud) == JSONSAX_OK)
   {
      cheevos_unload();
      return -1;
   }
   
   if (initialize)
   {
      initialize = 0;
      cheevos_locals.jobs = async_job_new();
   }

   return -(cheevos_locals.jobs == NULL);
}
Example #2
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;
}