From fa8aab99c2bf447a57ea1914ac98030455c49bad Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Wed, 28 Feb 2024 10:31:14 +0100 Subject: [PATCH] added asking before overwriting when saving native level files --- src/files.c | 30 ++++++++++++++++++++++-------- src/game_sp/export_sp.h | 2 +- src/game_sp/file.c | 6 ++++-- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/files.c b/src/files.c index 60d3ae0d..b1bbc43e 100644 --- a/src/files.c +++ b/src/files.c @@ -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); } diff --git a/src/game_sp/export_sp.h b/src/game_sp/export_sp.h index 0b9f6de4..06ade6d1 100644 --- a/src/game_sp/export_sp.h +++ b/src/game_sp/export_sp.h @@ -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); diff --git a/src/game_sp/file.c b/src/game_sp/file.c index a66187c5..677152ba 100644 --- a/src/game_sp/file.c +++ b/src/game_sp/file.c @@ -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; } -- 2.34.1