minecraft-launcher-libs/make-libs-string.c
2023-06-25 21:25:18 +03:00

142 lines
4.7 KiB
C

/*
AUTHOR: Alexander Popov <iiiypuk {at} fastmail.fm>
LICENSE: MIT-0
REPO: https://git.a2s.su/iiiypuk/minecraft-launcher-libs
*/
#include <stdio.h>
#include <string.h>
#include <cjson/cJSON.h>
#define VERSION "1.0.1" // software version
void usage();
void info(cJSON * json);
void generate_library_list(cJSON * input_json, cJSON * output_json, char output_system);
int
main(int argc, char const * argv[])
{
if (argc != 3) {
usage();
} else {
FILE *fp;
char str[900000];
char buffer[100];
fp = fopen(argv[1], "r");
while (fgets(buffer, sizeof(buffer), fp)) {
strcat(str, buffer);
}
fclose(fp);
cJSON *json = cJSON_Parse(str);
cJSON *launcher_libraries = cJSON_CreateObject();
cJSON *libraries = cJSON_CreateArray();
cJSON_AddItemToObject(launcher_libraries, "libraries", libraries);
generate_library_list(json, launcher_libraries, argv[2][0]);
int libraries_array_size = cJSON_GetArraySize(libraries);
switch (argv[2][0]) {
default:
usage();
break;
case 'i':
info(json);
break;
case 'w':
for (int i = 0; i < libraries_array_size; ++i) {
cJSON *library = cJSON_GetArrayItem(libraries, i);
printf("%%MC_DIR%%/libraries/%s;", cJSON_GetStringValue(library));
}
printf("%s\n", "%MC_DIR%/versions/%GAME_VERSION%/%GAME_VERSION%.jar");
break;
case 'l':
case 'x':
for (int i = 0; i < libraries_array_size; ++i) {
cJSON *library = cJSON_GetArrayItem(libraries, i);
printf("$MC_DIR/libraries/%s:", cJSON_GetStringValue(library));
}
printf("%s\n", "$MC_DIR/versions/$GAME_VERSION/$GAME_VERSION.jar");
break;
}
}
return 0;
}
void
usage()
{
printf("%s\n%s: %s\n\n", "Minecraft Libraries List Generator",
"Version", VERSION);
printf("%s\n", "Usage:");
printf(" %s\n\n", "make-libs-string <version.json> <system>");
printf(" %s\n\n", "<system> - w|l|x (windows or linux or osx)");
printf("%s\n", "Other:");
printf(" %s\n", "make-libs-string <version.json> i - to get version info");
}
void
info(cJSON * json)
{
char *minecraft_version = cJSON_GetStringValue(cJSON_GetObjectItem(json, "id"));
printf("Minecraft Version: %s\n", minecraft_version);
char *assets_version = cJSON_GetStringValue(cJSON_GetObjectItem(json, "assets"));
printf("Minecraft Assets Version: %s\n", assets_version);
cJSON *libraries = cJSON_GetObjectItem(json, "libraries");
int libraries_array_size = cJSON_GetArraySize(libraries);
printf("Minecraft libraries count: %d\n", libraries_array_size);
}
void
generate_library_list(cJSON * input_json, cJSON * output_json, char output_system)
{
cJSON *input_libraries = cJSON_GetObjectItem(input_json, "libraries");
cJSON *output_libraries = cJSON_GetObjectItem(output_json, "libraries");
int libraries_array_size = cJSON_GetArraySize(input_libraries);
char *output_system_name;
switch (output_system) {
case 'w':
output_system_name = "windows";
break;
case 'l':
output_system_name = "linux";
break;
case 'x':
output_system_name = "osx";
break;
}
for (int i = 0; i < libraries_array_size; i++) {
cJSON *library = cJSON_GetArrayItem(input_libraries, i);
if (library != NULL) {
cJSON *downloads = cJSON_GetObjectItem(library, "downloads");
cJSON *artifact = cJSON_GetObjectItem(downloads, "artifact");
cJSON *rules = cJSON_GetObjectItem(library, "rules");
cJSON *rules_item = cJSON_GetArrayItem(rules, 0);
cJSON *os_type = cJSON_GetObjectItem(rules_item, "os");
char *os_name = cJSON_GetStringValue(cJSON_GetObjectItem(os_type, "name"));
if (os_name != 0) {
char *library_path = cJSON_GetStringValue(cJSON_GetObjectItem(artifact, "path"));
if (strcmp(output_system_name, os_name) == 0) {
cJSON_AddItemToArray(output_libraries, cJSON_CreateString(library_path));
}
} else {
char *library_path = cJSON_GetStringValue(cJSON_GetObjectItem(artifact, "path"));
cJSON_AddItemToArray(output_libraries, cJSON_CreateString(library_path));
}
}
}
}