aboutsummaryrefslogtreecommitdiffstats
path: root/src/assets.c
blob: 57624aeb5b7cf42e26e1f97f20ef5be0720cd53d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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;
}