Merge pull request #789 from Erihel/dev-boost-filesystem-removal

Dev boost filesystem removal
master
krzys-h 2016-06-17 22:38:41 +02:00
commit 439a416a66
8 changed files with 27 additions and 122 deletions

View File

@ -47,7 +47,6 @@
#include "sound/oalsound/alsound.h"
#endif
#include <boost/filesystem.hpp>
#include <boost/tokenizer.hpp>
#include <SDL.h>

View File

@ -31,11 +31,8 @@
#include <physfs.h>
#include <boost/filesystem.hpp>
#include <boost/regex.hpp>
namespace fs = boost::filesystem;
CResourceManager::CResourceManager(const char *argv0)
{
@ -152,27 +149,17 @@ bool CResourceManager::CreateDirectory(const std::string& directory)
return false;
}
//TODO: Don't use boost::filesystem here
bool CResourceManager::RemoveDirectory(const std::string& directory)
{
if (PHYSFS_isInit())
{
bool success = true;
std::string writeDir = PHYSFS_getWriteDir();
try
std::string path = CleanPath(directory);
for (auto file : ListFiles(path))
{
std::string path = writeDir + "/" + CleanPath(directory);
#if PLATFORM_WINDOWS
fs::remove_all(CSystemUtilsWindows::UTF8_Decode(path));
#else
fs::remove_all(path);
#endif
if (PHYSFS_delete((path + "/" + file).c_str()) == 0)
return false;
}
catch (std::exception&)
{
success = false;
}
return success;
return PHYSFS_delete(path.c_str()) != 0;
}
return false;
}
@ -241,32 +228,6 @@ long long CResourceManager::GetLastModificationTime(const std::string& filename)
return -1;
}
//TODO: Don't use boost::filesystem. Why doesn't PHYSFS have this?
bool CResourceManager::Move(const std::string& from, const std::string& to)
{
if (PHYSFS_isInit())
{
bool success = true;
std::string writeDir = PHYSFS_getWriteDir();
try
{
std::string path_from = writeDir + "/" + CleanPath(from);
std::string path_to = writeDir + "/" + CleanPath(to);
#if PLATFORM_WINDOWS
fs::rename(CSystemUtilsWindows::UTF8_Decode(path_from), CSystemUtilsWindows::UTF8_Decode(path_to));
#else
fs::rename(path_from, path_to);
#endif
}
catch (std::exception&)
{
success = false;
}
return success;
}
return false;
}
bool CResourceManager::Remove(const std::string& filename)
{
if (PHYSFS_isInit())

View File

@ -66,8 +66,6 @@ public:
//! Returns last modification date as timestamp
static long long GetLastModificationTime(const std::string &filename);
//! Move file/directory
static bool Move(const std::string &from, const std::string &to);
//! Remove file
static bool Remove(const std::string& filename);
};

View File

@ -105,6 +105,7 @@
#include "ui/screen/screen_loading.h"
#include <algorithm>
#include <iomanip>
#include <stdexcept>
#include <ctime>
@ -5456,7 +5457,7 @@ void CRobotMain::SetAutosave(bool enable)
m_autosave = enable;
m_autosaveLast = m_gameTimeAbsolute;
AutosaveRotate(false);
AutosaveRotate();
}
bool CRobotMain::GetAutosave()
@ -5482,7 +5483,7 @@ void CRobotMain::SetAutosaveSlots(int slots)
if (m_autosaveSlots == slots) return;
m_autosaveSlots = slots;
AutosaveRotate(false);
AutosaveRotate();
}
int CRobotMain::GetAutosaveSlots()
@ -5490,85 +5491,39 @@ int CRobotMain::GetAutosaveSlots()
return m_autosaveSlots;
}
int CRobotMain::AutosaveRotate(bool freeOne)
// Remove oldest saves with autosave prefix
void CRobotMain::AutosaveRotate()
{
if (m_playerProfile == nullptr)
return 0;
return;
GetLogger()->Debug("Rotate autosaves...\n");
// Find autosave dirs
auto saveDirs = CResourceManager::ListDirectories(m_playerProfile->GetSaveDir());
std::map<int, std::string> autosaveDirs;
for (auto& dir : saveDirs)
{
try
{
const std::string autosavePrefix = "autosave";
if (dir.substr(0, autosavePrefix.length()) == autosavePrefix)
{
int id = boost::lexical_cast<int>(dir.substr(autosavePrefix.length()));
autosaveDirs[id] = m_playerProfile->GetSaveFile(dir);
}
}
catch (...)
{
GetLogger()->Info("Bad autosave found: %s\n", dir.c_str());
// skip
}
}
if (autosaveDirs.size() == 0) return 1;
const std::string autosavePrefix = "autosave";
std::vector<std::string> autosaves;
std::copy_if(saveDirs.begin(), saveDirs.end(), std::back_inserter(autosaves), [&](const std::string &save) {
return save.substr(0, autosavePrefix.length()) == autosavePrefix;
});
// Remove all but last m_autosaveSlots
std::map<int, std::string> autosavesToKeep;
int last_id = autosaveDirs.rbegin()->first;
int count = 0;
int to_keep = m_autosaveSlots-(freeOne ? 1 : 0);
int new_last_id = Math::Min(autosaveDirs.size(), to_keep);
bool rotate = false;
for (int i = last_id; i > 0; i--)
std::sort(autosaves.begin(), autosaves.end(), std::less<std::string>());
for (int i = 0; i < static_cast<int>(autosaves.size()) - m_autosaveSlots + 1; i++)
{
if (autosaveDirs.count(i) > 0)
{
count++;
if (count > m_autosaveSlots-(freeOne ? 1 : 0) || !m_autosave)
{
GetLogger()->Trace("Remove %s\n", autosaveDirs[i].c_str());
CResourceManager::RemoveDirectory(autosaveDirs[i]);
rotate = true;
}
else
{
GetLogger()->Trace("Keep %s\n", autosaveDirs[i].c_str());
autosavesToKeep[new_last_id-count+1] = autosaveDirs[i];
}
}
CResourceManager::RemoveDirectory(m_playerProfile->GetSaveDir() + "/" + autosaves[i]);
}
// Rename autosaves that we kept
if (rotate)
{
for (auto& save : autosavesToKeep)
{
std::string newDir = m_playerProfile->GetSaveFile("autosave" + boost::lexical_cast<std::string>(save.first));
GetLogger()->Trace("Rename %s -> %s\n", save.second.c_str(), newDir.c_str());
CResourceManager::Move(save.second, newDir);
}
}
return rotate ? count : count+1;
}
void CRobotMain::Autosave()
{
int id = AutosaveRotate(true);
AutosaveRotate();
GetLogger()->Info("Autosave!\n");
std::string dir = m_playerProfile->GetSaveFile("autosave" + boost::lexical_cast<std::string>(id));
char timestr[100];
char infostr[100];
time_t now = time(nullptr);
strftime(timestr, 99, "%x %X", localtime(&now));
std::string info = std::string("[AUTOSAVE] ")+timestr;
strftime(timestr, 99, "%y%m%d%H%M%S", localtime(&now));
strftime(infostr, 99, "%y.%m.%d %H:%M", localtime(&now));
std::string info = std::string("[AUTOSAVE] ") + infostr;
std::string dir = m_playerProfile->GetSaveFile(std::string("autosave") + timestr);
m_playerProfile->SaveScene(dir, info);
}

View File

@ -402,7 +402,7 @@ protected:
void ExecuteCmd(const std::string& cmd);
void UpdateSpeedLabel();
int AutosaveRotate(bool freeOne);
void AutosaveRotate();
void Autosave();
bool DestroySelectedObject();
void PushToSelectionHistory(CObject* obj);

View File

@ -25,7 +25,6 @@
#include <algorithm>
#include <iomanip>
#include <boost/filesystem.hpp>
CALSound::CALSound()
: m_enabled(false),
@ -586,11 +585,6 @@ bool CALSound::PlayMusic(const std::string &filename, bool repeat, float fadeTim
if (m_music.find(filename) == m_music.end())
{
GetLogger()->Debug("Music %s was not cached!\n", filename.c_str());
/* TODO: if (!boost::filesystem::exists(filename))
{
GetLogger()->Debug("Requested music %s was not found.\n", filename.c_str());
return false;
} */
auto newBuffer = MakeUnique<CBuffer>();
buffer = newBuffer.get();

View File

@ -27,8 +27,6 @@
#include <iomanip>
#include <sstream>
#include <boost/filesystem.hpp>
CSoundInterface::CSoundInterface()
{

View File

@ -80,7 +80,7 @@ void CScreenIO::IOReadName()
}
time(&now);
strftime(line, 99, "%y%m%d%H%M", localtime(&now));
strftime(line, 99, "%y.%m.%d %H:%M", localtime(&now));
sprintf(name, "%s - %s %d", line, resume.c_str(), m_main->GetLevelRank());
pe->SetText(name);