From 511a4ae3527a2b479068a0543d30657db570a360 Mon Sep 17 00:00:00 2001 From: Marc Date: Sun, 2 Oct 2022 14:24:42 +0200 Subject: [PATCH] Changed arguments and added option to delete a mod --- README.md | 5 +++-- src/main.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index fba77d3..1b4e6dc 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,10 @@ Use --list-games or -l to list compatible games Use --add or -a to add a mod to a game Use --list-mods or -m to list all mods for a game Use --install or -i to add a mod to a game -Use --remove or -r to remove a mod from a game +Use --uninstall or -u to uninstall a mod from a game +Use --remove or -r to remove a mod Use --deploy or -d to deploy the mods for the game -Use --unbind or -u to rollback a deployment +Use --unbind to rollback a deployment Use --fomod to create a new mod using the result from the FOMod ``` diff --git a/src/main.c b/src/main.c index 0f3c7ec..e1d6e23 100644 --- a/src/main.c +++ b/src/main.c @@ -56,9 +56,10 @@ int usage() { printf("Use --add or -a to add a mod to a game\n"); printf("Use --list-mods or -m to list all mods for a game\n"); printf("Use --install or -i to add a mod to a game\n"); - printf("Use --remove or -r to remove a mod from a game\n"); + printf("Use --uninstall or -u to uninstall a mod from a game\n"); + printf("Use --remove or -r to remove a mod\n"); printf("Use --deploy or -d to deploy the mods for the game\n"); - printf("Use --unbind or -u to rollback a deployment\n"); + printf("Use --unbind to rollback a deployment\n"); printf("Use --fomod to create a new mod using the result from the FOMod\n"); //TODO: as bonus //printf("Use --start-game to deploy the mods and launch the game through steam\n"); @@ -159,7 +160,7 @@ int listMods(int argc, char ** argv) { * this will just flag the mod as needed to be deployed * it's the deploying process that handle the rest */ -int installAndRemoveMod(int argc, char ** argv, bool install) { +int installAndUninstallMod(int argc, char ** argv, bool install) { if(argc != 4) return usage(); char * appIdStr = argv[2]; u_int32_t appid = validateAppId(appIdStr); @@ -371,10 +372,51 @@ int unbind(int argc, char ** argv) { return EXIT_SUCCESS; } +int removeMod(int argc, char ** argv) { + if(argc != 4) return usage(); + char * appIdStr = argv[2]; + int appid = validateAppId(appIdStr); + if(appid < 0) { + printf("Invalid appid"); + return EXIT_FAILURE; + } + + //strtoul set EINVAL if the string is invalid + u_int32_t modId = strtoul(argv[3], NULL, 10); + if(errno == EINVAL) { + printf("Modid has to be a valid number\n"); + return EXIT_FAILURE; + } + + //might crash if no mods were installed + char * home = getHome(); + char * modFolder = g_build_filename(home, MANAGER_FILES, MOD_FOLDER_NAME, appIdStr, NULL); + free(home); + GList * mods = listFilesInFolder(modFolder); + GList * modsFirstPointer = mods; + + for(int i = 0; i < modId; i++) { + mods = g_list_next(mods); + } + + if(mods == NULL) { + printf("Mod not found\n"); + return EXIT_FAILURE; + } + + gchar * filename = g_build_filename(modFolder, mods->data, NULL); + + delete(filename, true); + + g_free(filename); + g_list_free_full(modsFirstPointer, free); + return EXIT_SUCCESS; +} + int fomod(int argc, char ** argv) { if(argc != 4) return usage(); char * appIdStr = argv[2]; - u_int32_t appid = validateAppId(appIdStr); + int appid = validateAppId(appIdStr); if(appid < 0) { printf("Invalid appid"); return EXIT_FAILURE; @@ -411,7 +453,7 @@ int fomod(int argc, char ** argv) { char * modDestination = g_build_filename(modFolder, destination, NULL); char * modPath = g_build_filename(modFolder, mods->data, NULL); -//TODO: add error handling + //TODO: add error handling int returnValue = installFOMod(modPath, modDestination); free(destination); @@ -487,13 +529,13 @@ int main(int argc, char ** argv) { if(isRoot()) returnValue = noRoot(); else - returnValue = installAndRemoveMod(argc, argv, true); + returnValue = installAndUninstallMod(argc, argv, true); - } else if(strcmp(argv[1], "--remove") == 0 || strcmp(argv[1], "-r") == 0) { + } else if(strcmp(argv[1], "--uninstall") == 0 || strcmp(argv[1], "-u") == 0) { if(isRoot()) returnValue = noRoot(); else - returnValue = installAndRemoveMod(argc, argv, false); + returnValue = installAndUninstallMod(argc, argv, false); } else if(strcmp(argv[1], "--deploy") == 0 || strcmp(argv[1], "-d") == 0) { if(!isRoot()) @@ -501,7 +543,7 @@ int main(int argc, char ** argv) { else returnValue = deploy(argc, argv); - } else if(strcmp(argv[1], "--unbind") == 0 || strcmp(argv[1], "-u") == 0){ + } else if(strcmp(argv[1], "--unbind") == 0){ if(!isRoot()) returnValue = needRoot(); else @@ -518,6 +560,12 @@ int main(int argc, char ** argv) { else returnValue = fomod(argc, argv); + } else if(strcmp(argv[1], "--remove") == 0 || strcmp(argv[1], "-r") == 0) { + if(isRoot()) + returnValue = noRoot(); + else + returnValue = removeMod(argc, argv); + } else { usage(); returnValue = EXIT_FAILURE;