From e807329fe90d9d6d7c551f1fdb55197c1cb32366 Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Fri, 20 Dec 2024 22:14:37 +0100 Subject: [PATCH] added renaming already existing level directory when dropping zip file --- src/events.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/libgame/setup.c | 2 +- src/libgame/setup.h | 1 + 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/events.c b/src/events.c index 80c15c2e..16a84e92 100644 --- a/src/events.c +++ b/src/events.c @@ -1544,6 +1544,49 @@ static int HandleDropFileEvent(char *filename) directory = getUserLevelDir(NULL); } + // check if level or artwork set directory already exists in target directory + if (!CheckZipFileForDirectory(filename, directory, tree_type) && + zip_file.state == ZIP_FILE_ERROR_DIR_EXISTS) + { + char message[100]; + + sprintf(message, "%s set already exists! Replace with new set?", + (tree_type == TREE_TYPE_LEVEL_DIR ? "Level" : "Artwork")); + + // ask if level or artwork set directory should be replaced (and fail if not) + if (!Request(message, REQ_ASK)) + return TREE_TYPE_UNDEFINED; + + time_t epoch_seconds = time(NULL); + struct tm *now = localtime(&epoch_seconds); + char suffix[100]; + + sprintf(suffix, ".REPLACED-%04d%02d%02d-%02d%02d%02d", + now->tm_year + 1900, + now->tm_mon + 1, + now->tm_mday, + now->tm_hour, + now->tm_min, + now->tm_sec); + + // workaround: rename level or artwork set directory and topmost config file + // (change this so that the level or artwork set directory will be deleted) + char *conf_file_renamed = getStringCat2(zip_file.top_dir_conf_filename, suffix); + char *conf_path_renamed = getStringCat2(zip_file.top_dir_path, suffix); + boolean success = (rename(zip_file.top_dir_conf_filename, conf_file_renamed) == 0 && + rename(zip_file.top_dir_path, conf_path_renamed) == 0); + + checked_free(conf_file_renamed); + checked_free(conf_path_renamed); + + if (!success) + { + Request("Replacing old set failed!", REQ_CONFIRM); + + return TREE_TYPE_UNDEFINED; + } + } + // extract level or artwork set from zip file to target directory char *top_dir = ExtractZipFileIntoDirectory(filename, directory, tree_type); diff --git a/src/libgame/setup.c b/src/libgame/setup.c index c99298e5..acdba909 100644 --- a/src/libgame/setup.c +++ b/src/libgame/setup.c @@ -3812,7 +3812,7 @@ static int CheckZipFileForDirectoryExt(char *zip_filename, char *directory, int return ZIP_FILE_OK; } -static boolean CheckZipFileForDirectory(char *zip_filename, char *directory, int tree_type) +boolean CheckZipFileForDirectory(char *zip_filename, char *directory, int tree_type) { zip_file.state = CheckZipFileForDirectoryExt(zip_filename, directory, tree_type); diff --git a/src/libgame/setup.h b/src/libgame/setup.h index 028a1142..e63b416d 100644 --- a/src/libgame/setup.h +++ b/src/libgame/setup.h @@ -384,6 +384,7 @@ int hash_key_strings_are_equal(void *, void *); int hash_key_integers_are_equal(void *, void *); int GetZipFileTreeType(char *); +boolean CheckZipFileForDirectory(char *, char *, int); char *ExtractZipFileIntoDirectory(char *, char *, int); boolean AdjustGraphicsForEMC(void); -- 2.34.1