aboutsummaryrefslogtreecommitdiffstats
path: root/src/assets.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/assets.c')
-rw-r--r--src/assets.c64
1 files changed, 64 insertions, 0 deletions
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 <jansson.h>
+
+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;
+}