static int cheevos_count_cheevos(const char *json, unsigned *core_count, unsigned *unofficial_count) { static const jsonsax_handlers_t handlers = { NULL, NULL, NULL, NULL, NULL, cheevos_count__json_end_array, cheevos_count__json_key, NULL, NULL, cheevos_count__json_number, NULL, NULL }; int res; cheevos_countud_t ud; ud.in_cheevos = 0; ud.core_count = 0; ud.unofficial_count = 0; res = jsonsax_parse(json, &handlers, (void*)&ud); *core_count = ud.core_count; *unofficial_count = ud.unofficial_count; return res; }
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__json_key, NULL, cheevos_getvalue__json_string, cheevos_getvalue__json_string, /* number */ cheevos_getvalue__json_boolean, cheevos_getvalue__json_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) { strncpy(value, ud.value, length); value[ud.length] = 0; return 0; } return -1; }
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); }
int cheevos_load( const char* json ) { static const jsonsax_handlers_t handlers = { NULL, NULL, NULL, read__json_end_object, NULL, read__json_end_array, read__json_key, NULL, read__json_string, read__json_number, NULL, NULL }; /* Count the number of achievements in the JSON file. */ unsigned core_count, unofficial_count; if ( count_cheevos( json, &core_count, &unofficial_count ) != JSONSAX_OK ) { return -1; } /* Allocate the achievements. */ core_cheevos.cheevos = (cheevo_t*)malloc( core_count * sizeof( cheevo_t ) ); core_cheevos.count = core_count; unofficial_cheevos.cheevos = (cheevo_t*)malloc( unofficial_count * sizeof( cheevo_t ) ); unofficial_cheevos.count = unofficial_count; if ( !core_cheevos.cheevos || !unofficial_cheevos.cheevos ) { free( (void*)core_cheevos.cheevos ); free( (void*)unofficial_cheevos.cheevos ); return -1; } /* Load the achievements. */ cheevos_readud_t ud; ud.in_cheevos = 0; ud.field = NULL; ud.core_count = 0; ud.unofficial_count = 0; if ( jsonsax_parse( json, &handlers, (void*)&ud ) == JSONSAX_OK ) { return 0; } cheevos_unload(); return -1; }
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); }
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; }