Skip to content

Commit

Permalink
improve: backup organization in a folder (#73)
Browse files Browse the repository at this point in the history
It will create a backup folder to put the backups inside it.
  • Loading branch information
lamonato29 authored Jun 3, 2024
1 parent e86141b commit 0a0acba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
12 changes: 7 additions & 5 deletions source/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ void Editor::saveMap(FileName filename, bool showdialog) {

// Move to permanent backup
if (!save_as && g_settings.getInteger(Config::ALWAYS_MAKE_BACKUP)) {
std::string backup_path = map_path + "backups/";
ensureBackupDirectoryExists(backup_path);
// Move temporary backups to their proper files
time_t t = time(nullptr);
tm* current_time = localtime(&t);
Expand All @@ -437,31 +439,31 @@ void Editor::saveMap(FileName filename, bool showdialog) {

if (!backup_otbm.empty()) {
converter.SetFullName(wxstr(savefile));
std::string otbm_filename = map_path + nstr(converter.GetName());
std::string otbm_filename = backup_path + nstr(converter.GetName());
std::rename(backup_otbm.c_str(), std::string(otbm_filename + "." + date.str() + (save_otgz ? ".otgz" : ".otbm")).c_str());
}

if (!backup_house.empty()) {
converter.SetFullName(wxstr(map.housefile));
std::string house_filename = map_path + nstr(converter.GetName());
std::string house_filename = backup_path + nstr(converter.GetName());
std::rename(backup_house.c_str(), std::string(house_filename + "." + date.str() + ".xml").c_str());
}

if (!backup_spawn.empty()) {
converter.SetFullName(wxstr(map.spawnmonsterfile));
std::string spawn_filename = map_path + nstr(converter.GetName());
std::string spawn_filename = backup_path + nstr(converter.GetName());
std::rename(backup_spawn.c_str(), std::string(spawn_filename + "." + date.str() + ".xml").c_str());
}

if (!backup_spawn_npc.empty()) {
converter.SetFullName(wxstr(map.spawnnpcfile));
std::string spawnnpc_filename = map_path + nstr(converter.GetName());
std::string spawnnpc_filename = backup_path + nstr(converter.GetName());
std::rename(backup_spawn_npc.c_str(), std::string(spawnnpc_filename + "." + date.str() + ".xml").c_str());
}

if (!backup_zones.empty()) {
converter.SetFullName(wxstr(map.zonefile));
std::string zones_filename = map_path + nstr(converter.GetName());
std::string zones_filename = backup_path + nstr(converter.GetName());
std::rename(backup_zones.c_str(), std::string(zones_filename + "." + date.str() + ".xml").c_str());
}
} else {
Expand Down
8 changes: 8 additions & 0 deletions source/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ class Editor {
void undraw(const PositionVector &posvec, bool alt);
void undraw(const PositionVector &todraw, PositionVector &toborder, bool alt);

void ensureBackupDirectoryExists(const std::string &backup_path);

protected:
void drawInternal(const Position offset, bool alt, bool dodraw);
void drawInternal(const PositionVector &posvec, bool alt, bool dodraw);
Expand Down Expand Up @@ -182,4 +184,10 @@ inline void Editor::undraw(const PositionVector &todraw, PositionVector &toborde
drawInternal(todraw, toborder, alt, false);
}

inline void Editor::ensureBackupDirectoryExists(const std::string &backup_path) {
if (!std::filesystem::exists(backup_path)) {
std::filesystem::create_directory(backup_path);
}
}

#endif

0 comments on commit 0a0acba

Please sign in to comment.