added asking before overwriting when saving native level files
authorHolger Schemel <info@artsoft.org>
Wed, 28 Feb 2024 09:31:14 +0000 (10:31 +0100)
committerHolger Schemel <info@artsoft.org>
Wed, 28 Feb 2024 09:31:43 +0000 (10:31 +0100)
src/files.c
src/game_sp/export_sp.h
src/game_sp/file.c

index 60d3ae0d23dc00f43aa474d72e796c93a210d438..b1bbc43ef9b1810ee19e6e098094fa688be6486d 100644 (file)
@@ -6491,26 +6491,40 @@ void CopyNativeLevel_Native_to_RND(struct LevelInfo *level)
 
 void SaveNativeLevel(struct LevelInfo *level)
 {
+  // saving native level files only supported for some game engines
+  if (level->game_engine_type != GAME_ENGINE_TYPE_BD &&
+      level->game_engine_type != GAME_ENGINE_TYPE_SP)
+    return;
+
+  char *file_ext = (level->game_engine_type == GAME_ENGINE_TYPE_BD ? "bd" :
+                   level->game_engine_type == GAME_ENGINE_TYPE_SP ? "sp" : "");
+  char *basename = getSingleLevelBasenameExt(level->file_info.nr, file_ext);
+  char *filename = getLevelFilenameFromBasename(basename);
+
+  if (fileExists(filename) && !Request("Native level file already exists! Overwrite it?", REQ_ASK))
+    return;
+
+  boolean success = FALSE;
+
   if (level->game_engine_type == GAME_ENGINE_TYPE_BD)
   {
-    char *basename = getSingleLevelBasenameExt(level->file_info.nr, "bd");
-    char *filename = getLevelFilenameFromBasename(basename);
-
     CopyNativeLevel_RND_to_BD(level);
     // CopyNativeTape_RND_to_BD(level);
 
-    SaveNativeLevel_BD(filename);
+    success = SaveNativeLevel_BD(filename);
   }
   else if (level->game_engine_type == GAME_ENGINE_TYPE_SP)
   {
-    char *basename = getSingleLevelBasenameExt(level->file_info.nr, "sp");
-    char *filename = getLevelFilenameFromBasename(basename);
-
     CopyNativeLevel_RND_to_SP(level);
     CopyNativeTape_RND_to_SP(level);
 
-    SaveNativeLevel_SP(filename);
+    success = SaveNativeLevel_SP(filename);
   }
+
+  if (success)
+    Request("Native level file saved!", REQ_CONFIRM);
+  else
+    Request("Failed to save native level file!", REQ_CONFIRM);
 }
 
 
index 0b9f6de4007a73fc02c71e962dcf87d3fd31981e..06ade6d18960ddefa3631032a16b09b2a9b5dd13 100644 (file)
@@ -190,7 +190,7 @@ unsigned int InitEngineRandom_SP(int);
 void setLevelInfoToDefaults_SP(void);
 void copyInternalEngineVars_SP(void);
 boolean LoadNativeLevel_SP(char *, int, boolean);
-void SaveNativeLevel_SP(char *);
+boolean SaveNativeLevel_SP(char *);
 
 int getFieldbufferOffsetX_SP(void);
 int getFieldbufferOffsetY_SP(void);
index a66187c582f5d04f6b13dcbaea72a6d8036eec63..677152ba4f7ae11b8053e2cda8dc8a2d39253a63 100644 (file)
@@ -512,7 +512,7 @@ boolean LoadNativeLevel_SP(char *filename, int level_pos,
   return TRUE;
 }
 
-void SaveNativeLevel_SP(char *filename)
+boolean SaveNativeLevel_SP(char *filename)
 {
   LevelInfoType *header = &native_sp_level.header;
   FILE *file;
@@ -522,7 +522,7 @@ void SaveNativeLevel_SP(char *filename)
   {
     Warn("cannot save native level file '%s'", filename);
 
-    return;
+    return FALSE;
   }
 
   // write level playfield (width * height == 60 * 24 tiles == 1440 bytes)
@@ -573,4 +573,6 @@ void SaveNativeLevel_SP(char *filename)
   }
 
   fclose(file);
+
+  return TRUE;
 }