aboutsummaryrefslogtreecommitdiff
path: root/gen.c
blob: 7449d15d434566f9a2b89db72c0174b9c9382e24 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/**
 * Author: YuutaW <YuutaW@YMC.MOE>
 *
 * CFLAGS=-std=c99 -I/usr/local/include/ -D_POSIX_C_SOURCE=200809L
 * LDFLAGS=-ljson-c -lcurl
 *
 * License: GPLv2
 */

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <err.h>
#include <sysexits.h>
#include <curl/curl.h>
#include <stdlib.h>
#include <json-c/json_object.h>
#include <json-c/json_tokener.h>
#include <json-c/json.h>
#include <assert.h>

enum source_type {
    jar,
    client,
    extract_jar,
    log4j,
    asset,
    asset_index,
    fabric
};

struct source {
    struct source *next;
    char *id;
    char *hash;
    char *url;
    enum source_type type;
};

static struct source *source_fist = NULL;

static char *out_pkgbuild = "PKGBUILD.gen";
static FILE *pkgbuild = NULL;
static char *out_launcher = "launcher.gen";
static FILE *launcher = NULL;
static char *out_fabric_launcher = "launcher.fabric.gen";
static char *version = NULL;
static CURL *curl = NULL;
static json_object *json = NULL;
static json_tokener *tok = NULL;
static char *all_version_manifest_url = "https://launchermeta.mojang.com/mc/game/version_manifest_v2.json";
static char *version_manifest_url = NULL;
static char *all_fabric_manifest_url = "https://meta.fabricmc.net/v2/versions/loader";
static char *fabric_manifest_url = NULL;
static char *assets_url = NULL;

static void cleanup(void) {
    if (pkgbuild) fclose(pkgbuild);
    if (launcher) fclose(launcher);
    if (curl) {
        curl_easy_cleanup(curl);
        curl_global_cleanup();
    }
    if (tok) json_tokener_free(tok);
    if (json) {
        json_object_put(json);
        json = NULL;
    }
    struct source *s = source_fist;
    while (s) {
        struct source *s1 = s->next;
        free(s->hash);
        free(s->id);
        free(s->url);
        free(s);
        s = s1;
    }
    if (version) free(version);
}

static char get(const char *url, const char fail_not_found) {
    /* The url may belong to the json. Use it first. */
    curl_easy_setopt(curl, CURLOPT_URL, url);
    if (json) {
        json_object_put(json);
        json = NULL;
    }
    CURLcode res = curl_easy_perform(curl);
    if (res == CURLE_WRITE_ERROR) {
        cleanup();
        exit(1);
    }
    if (res) {
        long http_code = 0;
        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
        if (!fail_not_found && (http_code == 400 || http_code == 404)) return 1;
        cleanup();
        errx(res, "Cannot GET %s: %s",
             url,
             curl_easy_strerror(res));
    }
    /* No worries for IDE condition warning: it will be set in the callback function. */
    if (!json) {
        cleanup();
        errx(1, "Cannot parse response JSON: "
                "The stream ends without a full JSON.");
    }
    return 0;
}

static FILE *try_fopen(const char *path) {
    FILE *f = fopen(path, "w+");
    if (!f) {
        cleanup();
        err(errno, "Cannot open %s", path);
    }
    return f;
}

static size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
    json = json_tokener_parse_ex(tok,
                                 buffer,
                                 (int) (size * nmemb));
    enum json_tokener_error e;
    if (!json && (e = json_tokener_get_error(tok)) != json_tokener_continue) {
        /* Not sure if it is safe to call curl_easy_cleanup() within the callback.
         * Thus, delegate exit to the caller.
         * We will lose the return code here. */
        fprintf(stderr, "Cannot parse response JSON: %s",
                json_tokener_error_desc(e));
        /* Not necessary, but just to satisfy me. */
        json_tokener_reset(tok);
        return 0;
    }
    return size * nmemb;
}

static void src(const char *hash, const char *url, const enum source_type type) {
    char *id = strrchr(url, '/');
    if (!id) {
        cleanup();
        errx(1, "URL %s is not valid.", url);
    }
    /* Remove leading / */
    id++;
    struct source *s = malloc(sizeof(struct source));
    if (!s) {
        cleanup();
        err(errno, "Cannot allocate memory. Requested %lu bytes.",
            sizeof(struct source));
    }
    s->next = source_fist;
    s->id = calloc(strlen(id) + 1, sizeof(char));
    if (!s->id) {
        free(s);
        cleanup();
        err(errno, "Cannot allocate memory");
    }
    strcpy(s->id, id);
    s->hash = calloc(strlen(hash) + 1, sizeof(char));
    if (!s->hash) {
        free(s->id);
        free(s);
        cleanup();
        err(errno, "Cannot allocate memory");
    }
    strcpy(s->hash, hash);
    s->url = calloc(strlen(url) + 1, sizeof(char));
    if (!s->url) {
        free(s->id);
        free(s->hash);
        free(s);
        cleanup();
        err(errno, "Cannot allocate memory");
    }
    strcpy(s->url, url);

    s->type = type;

    source_fist = s;
}

static void append(FILE *stream, const char *key, const char *value) {
    fprintf(stream, "%s=\"$%s %s\"\n",
            key,
            key,
            value);
}

static void arg(const char jvm, const char *value, const char allow_space) {
    if (!allow_space && strchr(value, ' ')) {
        fprintf(stderr, "Warn: Refusing argument with space: %s\n", value);
        return;
    }
    append(launcher, jvm ? (jvm == 2 ? "JVM_ARGS_X86" : "JVM_ARGS") : "MC_ARGS", value);
}

static void set(FILE *stream, const char *key, const char *value) {
    fprintf(stream, "%s=\"%s\"\n",
            key,
            value);
}

static void parse_artifact(json_object *artifact, const enum source_type type) {
    json_object *obj;
    if (!json_object_object_get_ex(artifact, "sha1", &obj)) {
        cleanup();
        errx(1, "Invalid version.json: library doesn't have sha1.");
    }
    const char *sha1 = json_object_get_string(obj);
    if (!json_object_object_get_ex(artifact, "url", &obj)) {
        cleanup();
        errx(1, "Invalid version.json: library doesn't have url.");
    }
    const char *url = json_object_get_string(obj);
    src(sha1, url, type);
}

static char check_rules(json_object *rules) {
    char allow = 0;
    unsigned int rules_len = json_object_array_length(rules);
    for (unsigned int j = 0; j < rules_len; j++) {
        const json_object *rule = json_object_array_get_idx(rules, j);
        json_object *obj2;
        if (json_object_object_get_ex(rule, "os", &obj2)) {
            json_object *obj3;
            if (json_object_object_get_ex(obj2, "name", &obj3)) {
                /* Definitely not Unix, bro? */
                continue;
            }
            if (json_object_object_get_ex(obj2, "arch", &obj3) &&
                !strcmp("x86", json_object_get_string(obj3))) {
                allow = 2;
                break;
            }
            continue;
        }
        if (!json_object_object_get_ex(rule, "action", &obj2)) {
            cleanup();
            errx(1, "Rule doesn't have an action.");
        }
        /* Although the value seems to be always allow,
         * a double check seems satisfying. */
        if (!strcmp("allow", json_object_get_string(obj2))) {
            allow = 1;
            break;
        }
    }
    return allow;
}

static const char *fetch_version_manifest(void) {
    get(all_version_manifest_url, 1);
    json_object *obj;
    if (!version) {
        if (!json_object_object_get_ex(json, "latest", &obj)) {
            cleanup();
            errx(1, "Invalid version_manifest_v2.json: No latest object.");
        }
        if (!json_object_object_get_ex(obj, "release", &obj)) {
            cleanup();
            errx(1, "Invalid version_manifest_v2.json: No release object.");
        }
        const char *ver = json_object_get_string(obj);
        unsigned int len = strlen(ver);
        version = calloc(len + 1, sizeof(char));
        if (!version) {
            cleanup();
            err(errno, "Cannot allocate memory");
        }
        memcpy(version, ver, len + 1);
    }
    if (!json_object_object_get_ex(json, "versions", &obj)) {
        cleanup();
        errx(1, "Invalid version_manifest_v2.json: No versions[] object.");
    }
    unsigned int len = json_object_array_length(obj);
    for (unsigned int i = 0; i < len; i++) {
        const json_object *item = json_object_array_get_idx(obj, i);
        json_object *obj1;
        if (!json_object_object_get_ex(item, "id", &obj1)) {
            cleanup();
            errx(1, "Version doesn't have an id object.");
        }
        if (strcmp(version, json_object_get_string(obj1)) != 0) continue;
        if (!json_object_object_get_ex(item, "url", &obj1)) {
            cleanup();
            errx(1, "Invalid version_manifest_v2.json: no URL.");
        }
        return json_object_get_string(obj1);
    }
    cleanup();
    errx(1, "Version %s is not found.", version);
}

static const char *fetch_fabric_manifest(void) {
    get(all_fabric_manifest_url, 1);
    unsigned int len = json_object_array_length(json);
    for (unsigned int i = 0; i < len; i++) {
        const json_object *item = json_object_array_get_idx(json, i);
        json_object *obj1;
        if (!json_object_object_get_ex(item, "stable", &obj1)) {
            cleanup();
            errx(1, "Invalid fabric version json: no stable.");
        }
        if (!json_object_get_boolean(obj1)) continue;
        if (!json_object_object_get_ex(item, "version", &obj1)) {
            cleanup();
            errx(1, "Fabric loader doesn't have the version object.");
        }
        return json_object_get_string(obj1);
    }
    cleanup();
    errx(1, "No stable Fabric versions found.");
}

static void parse_arguments(struct json_object *obj) {
    json_object *args;
    if (!json_object_object_get_ex(obj, "game", &args)) {
        cleanup();
        errx(1, "Invalid version.json: No game[] object.");
    }
    unsigned int args_len = json_object_array_length(args);
    for (unsigned int i = 0; i < args_len; i++) {
        json_object *item = json_object_array_get_idx(args, i);
        if (json_object_is_type(item, json_type_string)) {
            arg(0, json_object_get_string(item), 0);
            continue;
        }
        if (!json_object_is_type(item, json_type_object)) {
            cleanup();
            errx(1, "Unknown Argument type: it must be a string or object.");
        }
        /* Currently, we don't support --demo, --width and --height. */
        json_object *val;
        if (!json_object_object_get_ex(item, "value", &val)) {
            cleanup();
            errx(1, "Argument doesn't have a value object.");
        }
        if (json_object_is_type(val, json_type_string)) {
            fprintf(stderr, "Skipped unsupported game argument: %s\n",
                    json_object_get_string(val));
        } else if (json_object_is_type(val, json_type_array)) {
            fprintf(stderr, "Skipped unsupported game argument: ");
            unsigned int len = json_object_array_length(val);
            for (unsigned int j = 0; j < len; j++) {
                fprintf(stderr, "%s ",
                        json_object_get_string(
                                json_object_array_get_idx(val, j)
                        ));
            }
            fprintf(stderr, "\n");
        }
    }
    if (!json_object_object_get_ex(obj, "jvm", &args)) {
        cleanup();
        errx(1, "Invalid version.json: No jvm[] object.");
    }
    args_len = json_object_array_length(args);
    for (unsigned int i = 0; i < args_len; i++) {
        json_object *item = json_object_array_get_idx(args, i);
        if (json_object_is_type(item, json_type_string)) {
            arg(1, json_object_get_string(item), 0);
            continue;
        }
        if (!json_object_is_type(item, json_type_object)) {
            cleanup();
            errx(1, "Unknown JVM Argument type: it must be a string or object.");
        }
        json_object *value;
        unsigned int value_len = 1;
        if (!json_object_object_get_ex(item, "value", &value)) {
            cleanup();
            errx(1, "JVM Argument doesn't have a value object.");
        }
        if (json_object_is_type(value, json_type_array)) {
            value_len = json_object_array_length(value);
        }
        json_object *rules;
        if (json_object_object_get_ex(item, "rules", &rules)) {
            const char rule = check_rules(rules);
            if (!rule) continue;
            if (rule == 2) {
                if (json_object_is_type(value, json_type_string)) {
                    arg(2, json_object_get_string(value), 0);
                } else {
                    for (unsigned int j = 0; j < value_len; j++) {
                        arg(2,
                               json_object_get_string(
                                       json_object_array_get_idx(value, j)
                               ),
                               0);
                    }
                }
            } else {
                if (json_object_is_type(value, json_type_string)) {
                    arg(1, json_object_get_string(value), 0);
                } else {
                    for (unsigned int j = 0; j < value_len; j++) {
                        arg(1,
                               json_object_get_string(
                                       json_object_array_get_idx(value, j)
                               ), 0);
                    }
                }
            }
        }
    }
}

static char *parse_maven(const char *name, const char *maven_repo) {
    /* Download from Maven instead */
    unsigned short seg = 0;
    char *name_cpy = calloc(strlen(name) + 1, sizeof(char));
    if (!name_cpy) {
        cleanup();
        err(errno, "Cannot allocate memory");
    }
    memcpy(name_cpy, name, strlen(name) + 1);
    const unsigned int repo_len = strlen(maven_repo);
    unsigned int artifact_len = 0;
    const unsigned int len = repo_len + 1 /* / */ +
                             2 * strlen(name) + 1 /* / before jar name */ + 4 /* .jar */ + 1;
    char *url_jar = calloc(len, sizeof(char));
    if (!url_jar) {
        cleanup();
        err(errno, "Cannot allocate memory");
    }
    unsigned int url_jar_len = 0;
    unsigned int artifact_start = 0;
    memcpy(url_jar, maven_repo, repo_len);
    url_jar_len += repo_len;
    url_jar[url_jar_len ++] = '/';
    /* Highly inefficient code. */
    for (char *p = strtok(name_cpy, ":"); p != NULL; p = strtok(NULL, ":")) {
        switch (seg ++) {
            case 0: {
                unsigned int z = 0;
                char c;
                while ((c = p[z ++]) != '\0') {
                    if (c == '.') url_jar[url_jar_len ++] = '/';
                    else url_jar[url_jar_len ++] = c;
                }
                url_jar[url_jar_len ++] = '/';
                break;
            }
            case 1:
                artifact_len = strlen(p);
                artifact_start = url_jar_len;
                memcpy(&url_jar[url_jar_len], p, artifact_len);
                url_jar_len += artifact_len;
                url_jar[url_jar_len ++] = '/';
                break;
            case 2: {
                unsigned int version_len = strlen(p);
                memcpy(&url_jar[url_jar_len], p, version_len);
                url_jar_len += version_len;
                url_jar[url_jar_len ++] = '/';
                memcpy(&url_jar[url_jar_len], &url_jar[artifact_start], artifact_len);
                url_jar_len += artifact_len;
                url_jar[url_jar_len ++] = '-';
                memcpy(&url_jar[url_jar_len], p, version_len);
                url_jar_len += version_len;
                memcpy(&url_jar[url_jar_len], ".jar\0", 5);
                url_jar_len += 5;
                break;
            }
            default:
                free(url_jar);
                free(name_cpy);
                errx(1, "Illegal maven name: %s", name);
        }
    }
    free(name_cpy);
    return url_jar;
}

static void fetch_version(const char *url, const char is_fabric) {
    /* URL is no longer valid. */
    if (get(url, is_fabric ? 0 : 1)) {
        fprintf(stderr, "Warn: No Fabric available for this version.\n");
        return;
    }
    json_object *obj;
    if (!json_object_object_get_ex(json, "mainClass", &obj)) {
        cleanup();
        errx(1, "Invalid version.json: No mainClass object.");
    }
    set(launcher, "MAIN_CLASS", json_object_get_string(obj));
    if (!is_fabric) {
        if (!json_object_object_get_ex(json, "id", &obj)) {
            cleanup();
            errx(1, "Invalid version.json: No id object.");
        }
        set(launcher, "ID", json_object_get_string(obj));
        set(pkgbuild, "_MC_ID", json_object_get_string(obj));
    }
    if (json_object_object_get_ex(json, "javaVersion", &obj)) {
        if (!json_object_object_get_ex(obj, "majorVersion", &obj)) {
            cleanup();
            errx(1, "Invalid version.json: No majorVersion object.");
        }
        fprintf(pkgbuild, "_JAVA_VERSION=%d\n",
                json_object_get_int(obj));
    }
    if (!json_object_object_get_ex(json, "libraries", &obj)) {
        cleanup();
        errx(1, "Invalid version.json: No libraries[] object.");
    }
    unsigned int libs_len = json_object_array_length(obj);
    for (unsigned int i = 0; i < libs_len; i++) {
        const json_object *item = json_object_array_get_idx(obj, i);
        json_object *obj1;
        if (json_object_object_get_ex(item, "rules", &obj1)) {
            if (!check_rules(obj1)) continue;
        }
        if (!json_object_object_get_ex(item, "downloads", &obj1)) {
            json_object *maven;
            if (!json_object_object_get_ex(item, "name", &obj1) ||
                !json_object_object_get_ex(item, "url", &maven)) {
                cleanup();
                errx(1, "Library doesn't have downloads object.");
            }
            const char *name = json_object_get_string(obj1);
            const char *maven_repo = json_object_get_string(maven);
            char *maven_url = parse_maven(name, maven_repo);
            src("SKIP", maven_url, is_fabric ? fabric : jar);
            free(maven_url);
            continue;
        }
        json_object *artifact;
        json_object *natives;
        json_object *classifiers;
        /* Prefer natives in case they both exist.
         * It seems that if they both exist, there must be a standalone artifact before it.
         */
        if (json_object_object_get_ex(item, "natives", &natives) &&
            json_object_object_get_ex(obj1, "classifiers", &classifiers)) {
            if (!json_object_object_get_ex(natives, "linux", &obj1))
                continue;
            if (!json_object_object_get_ex(classifiers,
                                           json_object_get_string(obj1),
                                           &obj1))
                continue;
            parse_artifact(obj1, extract_jar);
        } else if (json_object_object_get_ex(obj1, "artifact", &artifact)) {
            parse_artifact(artifact,
                           json_object_object_get_ex(item, "extract", &obj1) ? extract_jar
                                                                             : jar);
        } else {
            cleanup();
            errx(1, "Library doesn't have an artifact or natives and classifiers object.");
        }
    }

    if (!is_fabric) {
        if (!json_object_object_get_ex(json, "downloads", &obj)) {
            cleanup();
            errx(1, "Invalid version.json: No downloads object.");
        }
        if (!json_object_object_get_ex(obj, "client", &obj)) {
            cleanup();
            errx(1, "Invalid version.json: No client object.");
        }
        parse_artifact(obj, client);

        if (!json_object_object_get_ex(json, "assetIndex", &obj)) {
            cleanup();
            errx(1, "Invalid version.json: No assetIndex object.");
        }
        json_object *obj1;
        if (!json_object_object_get_ex(obj, "url", &obj1)) {
            cleanup();
            errx(1, "Invalid version.json: assetIndex does not have the url object.");
        }
        assets_url = (char *) json_object_get_string(obj1);
        if (!json_object_object_get_ex(obj, "sha1", &obj1)) {
            cleanup();
            errx(1, "Invalid version.json: assetIndex does not have the sha1 object.");
        }
        src(json_object_get_string(obj1), assets_url, asset_index);
        if (!json_object_object_get_ex(obj, "id", &obj1)) {
            cleanup();
            errx(1, "Invalid version.json: assetIndex does not have the id object.");
        }
        set(pkgbuild, "_ASSET_ID", json_object_get_string(obj1));
        set(launcher, "ASSET_ID", json_object_get_string(obj1));
        set(launcher, "assets_index_name", json_object_get_string(obj1));
    }

    if (json_object_object_get_ex(json, "arguments", &obj)) {
        parse_arguments(obj);
    } else if (json_object_object_get_ex(json, "minecraftArguments", &obj)) {
        arg(0, json_object_get_string(obj), 1 /* Allow */);
    } else {
        cleanup();
        errx(1, "Invalid version.json: No arguments or minecraftArguments object.");
    }
    if (json_object_object_get_ex(json, "logging", &obj)) {
        if (json_object_object_get_ex(obj, "client", &obj)) {
            json_object *file;
            json_object *a;
            if (!json_object_object_get_ex(obj, "file", &file)) {
                cleanup();
                errx(1, "Logging doesn't have the file object.");
            }
            if (!json_object_object_get_ex(obj, "argument", &a)) {
                cleanup();
                errx(1, "Logging doesn't have the argument object.");
            }
            json_object *id;
            if (!json_object_object_get_ex(file, "id", &id)) {
                cleanup();
                errx(1, "Logging file doesn't have the id object.");
            }
            set(pkgbuild, "_LOG4J_FILE", json_object_get_string(id));
            set(launcher, "LOG4J_FILE", json_object_get_string(id));
            parse_artifact(file, log4j);
            /* Hope Mojang won't change this. */
            if (strcmp("-Dlog4j.configurationFile=${path}",
                       json_object_get_string(a)) != 0) {
                fprintf(stderr,
                        "Unsupported Log4J Configuration: %s",
                        (char *) json_object_get_string(a));
            } else {
                arg(1, "-Dlog4j.configurationFile=LOG4J_XML_PATH", 1 /* Disable check */);
            }
        }
    }
}

static void fetch_assets(void) {
    get(assets_url, 1);
    json_object *obj;
    if (!json_object_object_get_ex(json, "objects", &obj)) {
        cleanup();
        errx(1, "Invalid assets JSON: no objects object.");
    }
    char url[85];
    json_object_object_foreach(obj, key, val) {
        json_object *hash;
        if (!json_object_object_get_ex(val, "hash", &hash)) {
            cleanup();
            errx(1, "Invalid assets JSON: no hash object.");
        }
        const char *hash_str = json_object_get_string(hash);
        if (strlen(hash_str) != 40) {
            cleanup();
            errx(1, "Invalid length of hash.");
        }
        sprintf(url, "https://resources.download.minecraft.net/%c%c/%s",
                hash_str[0],
                hash_str[1],
                hash_str);
        src(hash_str, url, asset);
    }
}

static void out(const char *tag, enum source_type type) {
    char tag_fin[64];
    sprintf(tag_fin, "_MC_%s=\"", tag);
    struct source *s = source_fist;
    fprintf(pkgbuild, "%s", tag_fin);
    while (s) {
        if (s->type == type) fprintf(pkgbuild, "%s\n", s->id);
        s = s->next;
    }
    fprintf(pkgbuild, "\"\n");

    sprintf(tag_fin, "_MC_%s_SHA1=\"", tag);
    s = source_fist;
    fprintf(pkgbuild, "%s", tag_fin);
    while (s) {
        if (s->type == type) fprintf(pkgbuild, "%s\n", s->hash);
        s = s->next;
    }
    fprintf(pkgbuild, "\"\n");

    sprintf(tag_fin, "_MC_%s_URL=\"", tag);
    s = source_fist;
    fprintf(pkgbuild, "%s", tag_fin);
    while (s) {
        if (s->type == type) fprintf(pkgbuild, "%s\n", s->url);
        s = s->next;
    }
    fprintf(pkgbuild, "\"\n");
}

int main(int argc, char **argv) {
    int opt;
    while ((opt = getopt(argc, argv, "o:O:v:m:M:f:F:g:")) != -1) {
        switch (opt) {
            case 'o':
                out_pkgbuild = optarg;
                break;
            case 'O':
                out_launcher = optarg;
                break;
            case 'v': {
                unsigned int len = strlen(optarg);
                version = calloc(len + 1, sizeof(char));
                if (!version) {
                    cleanup();
                    err(errno, "Cannot allocate memory");
                }
                memcpy(version, optarg, len + 1);
                break;
            }
            case 'm':
                version_manifest_url = optarg;
                break;
            case 'M':
                all_version_manifest_url = optarg;
                break;
            case 'f':
                fabric_manifest_url = optarg;
                break;
            case 'F':
                all_fabric_manifest_url = optarg;
                break;
            case 'g':
                out_fabric_launcher = optarg;
                break;
            default:
                errx(EX_USAGE, "-o %s -O %s -v %s -g %s",
                     out_pkgbuild,
                     out_launcher,
                     version,
                     out_fabric_launcher);
        }
    }
    pkgbuild = try_fopen(out_pkgbuild);
    launcher = try_fopen(out_launcher);
    CURLcode res = curl_global_init(CURL_GLOBAL_SSL);
    if (res) {
        cleanup();
        errx(res, "Cannot initialize libcurl global: %s",
             curl_easy_strerror(res));
    }
    if (!(curl = curl_easy_init())) {
        curl_global_cleanup();
        cleanup();
        errx(1, "Cannot initialize libcurl.");
    }
    curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    tok = json_tokener_new();
    if (!tok) {
        cleanup();
        errx(1, "Cannot initialize JSON tok.");
    }
    fetch_version(version_manifest_url == NULL ? fetch_version_manifest() : version_manifest_url, 0);
    if (assets_url) fetch_assets();
    out("CLIENT", client);
    out("JAR", jar);
    out("EXTRACT_JAR", extract_jar);
    out("ASSET", asset);
    out("LOG4J", log4j);
    out("ASSET_INDEX", asset_index);
    fclose(launcher);

    launcher = try_fopen(out_fabric_launcher);
    char fabric_built_url[256];
    if (!fabric_manifest_url) {
        const char *ver = fetch_fabric_manifest();
        set(pkgbuild, "_FABRIC", ver);
        set(launcher, "FABRIC", ver);
        sprintf(fabric_built_url,
                "https://meta.fabricmc.net/v2/versions/loader/%s/%s/profile/json",
                version,
                ver);
        fabric_manifest_url = fabric_built_url;
    }
    fetch_version(fabric_manifest_url, 1);
    out("FABRIC_JAR", fabric);

    cleanup();
    return 0;
}