Modify updater to pull from TitleDB.

This commit is contained in:
Steven Smith 2018-02-22 16:32:05 -08:00
parent 36c4ca784c
commit a466e3da13

View File

@ -18,42 +18,52 @@ static void update_check_update(ui_view* view, void* data, float* progress, char
Result res = 0; Result res = 0;
json_t* json = NULL; json_t* json = NULL;
if(R_SUCCEEDED(res = task_download_json_sync("https://api.github.com/repos/Steveice10/FBI/releases/latest", &json, 16 * 1024))) { if(R_SUCCEEDED(res = task_download_json_sync("https://api.titledb.com/v1/entry?nested=true&only=cia.id&only=cia.version&only=tdsx.id&only=tdsx.version&_filters=%7B%22name%22%3A%20%22FBI%22%7D", &json, 16 * 1024))) {
if(json_is_object(json)) { const char* type = fs_get_3dsx_path() != NULL ? "tdsx" : "cia";
json_t* name = json_object_get(json, "name");
json_t* assets = json_object_get(json, "assets");
if(json_is_string(name) && json_is_array(assets)) { json_t* entry = NULL;
char versionString[16]; json_t* objs = NULL;
snprintf(versionString, sizeof(versionString), "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_MICRO); if(json_is_array(json) && json_array_size(json) == 1
&& json_is_object(entry = json_array_get(json, 0))
&& json_is_array(objs = json_object_get(entry, type))) {
if(json_array_size(json) > 0) {
u32 latestId = 0;
u32 latestMajor = 0;
u32 latestMinor = 0;
u32 latestMicro = 0;
if(strncmp(json_string_value(name), versionString, json_string_length(name)) != 0) { for(u32 i = 0; i < json_array_size(objs); i++) {
const char* url = NULL; json_t* obj = json_array_get(objs, i);
if(json_is_object(obj)) {
json_t* idJson = json_object_get(obj, "id");
json_t* versionJson = json_object_get(obj, "version");
if(json_is_integer(idJson) && json_is_string(versionJson)) {
u32 id = (u32) json_integer_value(idJson);
const char* version = json_string_value(versionJson);
for(u32 i = 0; i < json_array_size(assets); i++) { u32 major = 0;
json_t* val = json_array_get(assets, i); u32 minor = 0;
if(json_is_object(val)) { u32 micro = 0;
json_t* assetName = json_object_get(val, "name"); sscanf(version, "%lu.%lu.%lu", &major, &minor, &micro);
json_t* assetUrl = json_object_get(val, "browser_download_url");
if(json_is_string(assetName) && json_is_string(assetUrl)) { if(major > latestMajor
if(strncmp(json_string_value(assetName), fs_get_3dsx_path() != NULL ? "FBI.3dsx" : "FBI.cia", json_string_length(assetName)) == 0) { || (major == latestMajor && minor > latestMinor)
url = json_string_value(assetUrl); || (major == latestMajor && minor == latestMinor && micro > latestMicro)) {
break; latestId = id;
} latestMajor = major;
latestMinor = minor;
latestMicro = micro;
} }
} }
} }
if(url != NULL) {
strncpy(updateURL, url, DOWNLOAD_URL_MAX);
hasUpdate = true;
} else {
res = R_APP_BAD_DATA;
}
} }
} else {
res = R_APP_BAD_DATA; if(latestMajor > VERSION_MAJOR
|| (latestMajor == VERSION_MAJOR && latestMinor > VERSION_MINOR)
|| (latestMajor == VERSION_MAJOR && latestMinor == VERSION_MINOR && latestMicro > VERSION_MICRO)) {
snprintf(updateURL, DOWNLOAD_URL_MAX, "https://3ds.titledb.com/v1/%s/%lu/download", type, latestId);
hasUpdate = true;
}
} }
} else { } else {
res = R_APP_BAD_DATA; res = R_APP_BAD_DATA;