added renaming already existing level directory when dropping zip file
authorHolger Schemel <holger.schemel@virtion.de>
Fri, 20 Dec 2024 21:14:37 +0000 (22:14 +0100)
committerHolger Schemel <holger.schemel@virtion.de>
Fri, 20 Dec 2024 21:14:42 +0000 (22:14 +0100)
src/events.c
src/libgame/setup.c
src/libgame/setup.h

index 80c15c2e16c089ed71101312bca42fe99813b4a0..16a84e920db34dcc2a446938fa1d0a682c395974 100644 (file)
@@ -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);
 
index c99298e518bc802a22348f371f347a64010aa48e..acdba909ec513a9934f64b251bfd03e0c9750a69 100644 (file)
@@ -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);
 
index 028a11420b6cfd54986a153b38a875f14a8e407b..e63b416df0fb7c45f4d05b0cb809637b05534388 100644 (file)
@@ -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);