From 81dade9ed5fd80bed0fb0fcd4477bea0b160e915 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Wed, 1 May 2024 09:47:58 +0200 Subject: [PATCH] fixed: for deleting files and folders on Windows we have to call the Unicode variants of these functions. --- src/common/menu/savegamemanager.cpp | 7 +++---- src/common/utility/cmdlib.cpp | 20 ++++++++++++++++++++ src/common/utility/cmdlib.h | 2 ++ src/common/utility/writezip.cpp | 7 ++++--- src/g_game.cpp | 2 +- src/maploader/glnodes.cpp | 10 ++-------- 6 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/common/menu/savegamemanager.cpp b/src/common/menu/savegamemanager.cpp index 83865cd1d..06b014122 100644 --- a/src/common/menu/savegamemanager.cpp +++ b/src/common/menu/savegamemanager.cpp @@ -84,7 +84,7 @@ int FSavegameManagerBase::RemoveSaveSlot(int index) int listindex = SaveGames[0]->bNoDelete ? index - 1 : index; if (listindex < 0) return index; - remove(SaveGames[index]->Filename.GetChars()); + RemoveFile(SaveGames[index]->Filename.GetChars()); UnloadSaveData(); FSaveGameNode *file = SaveGames[index]; @@ -274,7 +274,7 @@ DEFINE_ACTION_FUNCTION(FSavegameManager, DoSave) unsigned FSavegameManagerBase::ExtractSaveData(int index) { - FResourceFile *resf; + std::unique_ptr resf; FSaveGameNode *node; if (index == -1) @@ -295,7 +295,7 @@ unsigned FSavegameManagerBase::ExtractSaveData(int index) (node = SaveGames[index]) && !node->Filename.IsEmpty() && !node->bOldVersion && - (resf = FResourceFile::OpenResourceFile(node->Filename.GetChars(), true)) != nullptr) + ( (resf.reset(FResourceFile::OpenResourceFile(node->Filename.GetChars(), true))), resf != nullptr)) { auto info = resf->FindEntry("info.json"); if (info < 0) @@ -329,7 +329,6 @@ unsigned FSavegameManagerBase::ExtractSaveData(int index) } } } - delete resf; } return index; } diff --git a/src/common/utility/cmdlib.cpp b/src/common/utility/cmdlib.cpp index 5933a91d5..3b8dc9981 100644 --- a/src/common/utility/cmdlib.cpp +++ b/src/common/utility/cmdlib.cpp @@ -593,6 +593,26 @@ void CreatePath(const char *fn) } #endif +void RemoveFile(const char* file) +{ +#ifndef _WIN32 + remove(file); +#else + auto wpath = WideString(file); + _wremove(wpath.c_str()); +#endif +} + +int RemoveDir(const char* file) +{ +#ifndef _WIN32 + return rmdir(file); +#else + auto wpath = WideString(file); + return _wrmdir(wpath.c_str()); +#endif +} + //========================================================================== // // strbin -- In-place version diff --git a/src/common/utility/cmdlib.h b/src/common/utility/cmdlib.h index df779a975..88027c156 100644 --- a/src/common/utility/cmdlib.h +++ b/src/common/utility/cmdlib.h @@ -71,6 +71,8 @@ int strbin (char *str); FString strbin1 (const char *start); void CreatePath(const char * fn); +void RemoveFile(const char* file); +int RemoveDir(const char* file); FString ExpandEnvVars(const char *searchpathstring); FString NicePath(const char *path); diff --git a/src/common/utility/writezip.cpp b/src/common/utility/writezip.cpp index 2f3507e6f..de89779c0 100644 --- a/src/common/utility/writezip.cpp +++ b/src/common/utility/writezip.cpp @@ -40,6 +40,7 @@ #include "m_swap.h" #include "w_zip.h" #include "fs_decompress.h" +#include "cmdlib.h" using FileSys::FCompressedBuffer; @@ -201,7 +202,7 @@ bool WriteZip(const char* filename, const FCompressedBuffer* content, size_t con if (pos == -1) { delete f; - remove(filename); + RemoveFile(filename); return false; } positions.Push(pos); @@ -213,7 +214,7 @@ bool WriteZip(const char* filename, const FCompressedBuffer* content, size_t con if (AppendCentralDirectory(f, content[i], dostime, positions[i]) < 0) { delete f; - remove(filename); + RemoveFile(filename); return false; } } @@ -230,7 +231,7 @@ bool WriteZip(const char* filename, const FCompressedBuffer* content, size_t con if (f->Write(&dirend, sizeof(dirend)) != sizeof(dirend)) { delete f; - remove(filename); + RemoveFile(filename); return false; } delete f; diff --git a/src/g_game.cpp b/src/g_game.cpp index 56a84d482..472b840a4 100644 --- a/src/g_game.cpp +++ b/src/g_game.cpp @@ -3079,7 +3079,7 @@ bool G_CheckDemoStatus (void) const size_t size = demo_p - demobuffer; saved = fw->Write(demobuffer, size) == size; delete fw; - if (!saved) remove(demoname.GetChars()); + if (!saved) RemoveFile(demoname.GetChars()); } M_Free (demobuffer); demorecording = false; diff --git a/src/maploader/glnodes.cpp b/src/maploader/glnodes.cpp index b41b22c0b..df569b1ae 100644 --- a/src/maploader/glnodes.cpp +++ b/src/maploader/glnodes.cpp @@ -34,12 +34,6 @@ #ifndef _WIN32 #include - -#else -#include - -#define rmdir _rmdir - #endif #include @@ -1215,11 +1209,11 @@ UNSAFE_CCMD(clearnodecache) { if (list[i].isDirectory) { - rmdir(list[i].FilePath.c_str()); + RemoveDir(list[i].FilePath.c_str()); } else { - remove(list[i].FilePath.c_str()); + RemoveFile(list[i].FilePath.c_str()); } }