From e505fd8cfc0034c7ca72da799d1d6a5a98bcbf86 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Sun, 5 Jun 2022 02:56:46 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .editorconfig | 17 ++++++++ LICENSE | 16 ++++++++ Makefile | 8 ++++ README.md | 3 ++ main.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 150 insertions(+) create mode 100644 .editorconfig create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 main.c diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..daa3bb9 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[{*.c,*.h}] +indent_style = space +indent_size = 4 + +[Makefile] +indent_style = tab +indent_size = 4 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..36f0add --- /dev/null +++ b/LICENSE @@ -0,0 +1,16 @@ +MIT No Attribution + +Copyright 2022 Alexander Popov + +Permission is hereby granted, free of charge, to any person obtaining a copy of this +software and associated documentation files (the "Software"), to deal in the Software +without restriction, including without limitation the rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3d276b9 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +CC = clang +CFLAGS = -O2 -lcjson + +all: + ${CC} -O2 -lcjson main.c -o make-libs-string + +clean: + rm -f *.o diff --git a/README.md b/README.md new file mode 100644 index 0000000..9cb9a69 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +Parse `version.json` file and return libs + +Паритет с версией на [Python](https://github.com/iiiypuk/minecraft-libs-parser) diff --git a/main.c b/main.c new file mode 100644 index 0000000..74c0259 --- /dev/null +++ b/main.c @@ -0,0 +1,106 @@ +#include +#include +#include + +void usage(); +void info(cJSON *json); +void generate_library_list(cJSON *input_json, cJSON *output_json); + +int main(int argc, char const *argv[]) +{ + if (argc < 2 || 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); + int libraries_array_size = cJSON_GetArraySize(libraries); + + switch (argv[2][0]) + { + default: + usage(); + break; + case 's': + 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", "%MC_DIR%/versions/%GAME_VERSION%/%GAME_VERSION%.jar"); + break; + case 'u': + 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", "$MC_DIR/versions/$GAME_VERSION/$GAME_VERSION.jar"); + break; + } + } + + return 0; +} + +void usage() +{ + printf("%s\n\n", "Minecraft Libraries List Generator"); + printf("%s\n", "Usage:"); + printf(" %s\n\n", "make-libs-string "); + printf(" %s\n\n", " - w|u (windows or unix)"); + printf("%s\n", "Other:"); + printf(" %s\n", "make-libs-string s - to get info"); +} + +void info(cJSON *json) +{ + char *assets_version = cJSON_GetStringValue(cJSON_GetObjectItem(json, "id")); + 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) +{ + cJSON *input_libraries = cJSON_GetObjectItem(input_json, "libraries"); + cJSON *output_libraries = cJSON_GetObjectItem(output_json, "libraries"); + int libraries_array_size = cJSON_GetArraySize(input_libraries); + + 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"); + + char *library_path = cJSON_GetStringValue(cJSON_GetObjectItem(artifact, "path")); + + cJSON_AddItemToArray(output_libraries, cJSON_CreateString(library_path)); + } + } +}