From ce87d368bd3e9b6c05ed9f6fdbb97580f105bbcf Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Wed, 3 Jan 2024 05:18:11 -0600 Subject: refactor and download asset index --- src/assets.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/assets.c (limited to 'src/assets.c') diff --git a/src/assets.c b/src/assets.c new file mode 100644 index 0000000..57624ae --- /dev/null +++ b/src/assets.c @@ -0,0 +1,64 @@ +#include "assets.h" +#include "version.h" +#include "l2su.h" + +#include + +int l2_assets__download_index(json_t *version, char **path) +{ + const char *url = NULL; + const char *id = NULL; + const char *digeststr = NULL; + json_int_t jsize = 0; + + l2_sha1_digest_t expect_digest; + + if (json_unpack(version, "{s:{s?:s, s:s, s:I, s:s}}", "assetIndex", "url", &url, "id", &id, "size", &jsize, "sha1", &digeststr) < 0) { + return -1; + } + + if (jsize < 0) return -1; + if (l2_sha1_digest_from_hex(&expect_digest, digeststr) < 0) return -1; + + char *pathstr = l2_launcher_sprintf_alloc("%s/assets/indexes/%s.json", l2_state.paths.data, id); + if (!pathstr) return -1; + + int res = l2_launcher_download_checksummed(url, pathstr, &expect_digest, (size_t)jsize); + + if (res < 0) { + free(pathstr); + return res; + } + + *path = pathstr; + return res; +} + +int l2_assets__load_index(json_t *version, json_t **asset_index) +{ + char *path = NULL; + int res; + + res = l2_assets__download_index(version, &path); + + if (res < 0) { + goto cleanup; + } + + json_error_t jerr; + json_t *js; + if (!(js = json_load_file(path, JSON_REJECT_DUPLICATES, &jerr))) { + CMD_ERROR("JSON error parsing asset index: %s", jerr.text); + res = -1; + goto cleanup; + } + + free(path); + + *asset_index = js; + return res; + +cleanup: + free(path); + return res; +} -- cgit v1.2.3-70-g09d2