summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkrzys-h <krzys_h@interia.pl>2014-09-26 19:48:53 +0200
committerkrzys-h <krzys_h@interia.pl>2014-09-26 19:50:27 +0200
commit3b1cfdbfb4e57aff7c4d651ea2a14aceab36be14 (patch)
tree865d0e66a15d8aa4a7ebe85d209ae2202871a66d
parent740036e430d98afe487313bb5be8c5e72968eb94 (diff)
downloadcolobot-3b1cfdbfb4e57aff7c4d651ea2a14aceab36be14.tar.gz
colobot-3b1cfdbfb4e57aff7c4d651ea2a14aceab36be14.tar.bz2
colobot-3b1cfdbfb4e57aff7c4d651ea2a14aceab36be14.zip
Don't use physfs in saving code for now
This requires too much refactoring and we want to release 0.1.5 soon. We'll need to rewrite saving/loading code to fully implement new level parser anyway.
-rw-r--r--src/ui/maindialog.cpp248
-rw-r--r--src/ui/maindialog.h7
2 files changed, 182 insertions, 73 deletions
diff --git a/src/ui/maindialog.cpp b/src/ui/maindialog.cpp
index ba8da9e..068732d 100644
--- a/src/ui/maindialog.cpp
+++ b/src/ui/maindialog.cpp
@@ -52,12 +52,15 @@
#include "ui/window.h"
#include "ui/edit.h"
#include "ui/editvalue.h"
+#include <test/cbot/CBot_console/CClass.h>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <iomanip>
#include <vector>
+#include <boost/filesystem.hpp>
+#include <boost/foreach.hpp>
//TODO Get rid of all sprintf's
@@ -108,6 +111,8 @@ static int perso_color[3*10*3] =
0, 0, 0, //
};
+namespace fs = boost::filesystem;
+
// Constructor of robot application.
CMainDialog::CMainDialog()
@@ -172,7 +177,12 @@ CMainDialog::CMainDialog()
m_partiTime[i] = 0.0f;
}
+ #if DEV_BUILD
+ m_savegameDir = "savegame";
+ #else
m_savegameDir = "savegame";
+ #endif
+
m_publicDir = "program";
m_userDir = "user";
m_filesDir = m_savegameDir;
@@ -3346,16 +3356,50 @@ std::string & CMainDialog::GetFilesDir()
void CMainDialog::ReadNameList()
{
- CWindow* pw = static_cast<CWindow*>(m_interface->SearchControl(EVENT_WINDOW5));
- if (pw == nullptr) return;
- CList* pl = static_cast<CList*>(pw->SearchControl(EVENT_INTERFACE_NLIST));
- if (pl == nullptr) return;
+ CWindow* pw;
+ CList* pl;
+ //struct _finddata_t fBuffer;
+ char dir[MAX_FNAME];
+ // char filenames[MAX_FNAME][100];
+ std::vector<std::string> fileNames;
+
+ pw = static_cast<CWindow*>(m_interface->SearchControl(EVENT_WINDOW5));
+ if ( pw == 0 ) return;
+ pl = static_cast<CList*>(pw->SearchControl(EVENT_INTERFACE_NLIST));
+ if ( pl == 0 ) return;
pl->Flush();
- auto userSaveDirs = CResourceManager::ListDirectories(m_savegameDir);
- for (int i = 0; i < static_cast<int>(userSaveDirs.size()); ++i)
+
+ try
{
- pl->SetItemName(i, userSaveDirs.at(i).c_str());
+ if (! fs::exists(m_savegameDir) && fs::is_directory(m_savegameDir))
+ {
+ GetLogger()->Error("Savegame dir does not exist %s\n",dir);
+ }
+ else
+ {
+ fs::directory_iterator dirIt(m_savegameDir), dirEndIt;
+
+ for (; dirIt != dirEndIt; ++dirIt)
+ {
+ const fs::path& p = *dirIt;
+ if (fs::is_directory(p))
+ {
+ fileNames.push_back(p.leaf().string());
+ }
+ }
+ }
+ }
+ catch (std::exception & e)
+ {
+ GetLogger()->Error("Error on listing savegame directory : %s\n", e.what());
+ return;
+ }
+
+
+ for (size_t i=0 ; i<fileNames.size() ; ++i )
+ {
+ pl->SetItemName(i, fileNames.at(i).c_str());
}
}
@@ -3538,6 +3582,7 @@ void CMainDialog::NameCreate()
CWindow* pw;
CEdit* pe;
char name[100];
+ std::string dir;
char c;
int len, i, j;
@@ -3578,10 +3623,13 @@ void CMainDialog::NameCreate()
return;
}
- std::string userSaveDir = m_savegameDir + "/" + name;
- if (!CResourceManager::DirectoryExists(userSaveDir))
+ // TODO: _mkdir(m_savegameDir); // if does not exist yet!
+
+
+ dir = m_savegameDir + "/" + name;
+ if (!fs::exists(dir))
{
- CResourceManager::CreateDirectory(userSaveDir);
+ fs::create_directories(dir);
}
else
{
@@ -3598,26 +3646,58 @@ void CMainDialog::NameCreate()
m_main->ChangePhase(PHASE_INIT);
}
+// Deletes a folder and all its offspring.
+
+bool RemoveDir(char *dirName)
+{
+ try
+ {
+
+ if (!fs::exists(dirName) && fs::is_directory(dirName))
+ {
+ GetLogger()->Error("Directory does not exist %s\n",dirName);
+ return false;
+ }
+ else
+ {
+ fs::remove_all(dirName);
+ }
+
+ }
+ catch (std::exception & e)
+ {
+ GetLogger()->Error("Error on removing directory %s : %s\n", dirName, e.what());
+ return false;
+ }
+ return true;
+}
+
// Removes a player.
void CMainDialog::NameDelete()
{
- CWindow* pw = static_cast<CWindow*>(m_interface->SearchControl(EVENT_WINDOW5));
- if (pw == nullptr) return;
- CList* pl = static_cast<CList*>(pw->SearchControl(EVENT_INTERFACE_NLIST));
- if (pl == nullptr) return;
+ CWindow* pw;
+ CList* pl;
+ int sel;
+ char* gamer;
+ char dir[100];
+
+ pw = static_cast<CWindow*>(m_interface->SearchControl(EVENT_WINDOW5));
+ if ( pw == 0 ) return;
+ pl = static_cast<CList*>(pw->SearchControl(EVENT_INTERFACE_NLIST));
+ if ( pl == 0 ) return;
- int sel = pl->GetSelect();
- if (sel == -1)
+ sel = pl->GetSelect();
+ if ( sel == -1 )
{
m_sound->Play(SOUND_TZOING);
return;
}
+ gamer = pl->GetItemName(sel);
- char* gamer = pl->GetItemName(sel);
-
- std::string userSaveDir = m_savegameDir + "/" + gamer;
- if (!CResourceManager::RemoveDirectory(userSaveDir))
+ // Deletes all the contents of the file.
+ sprintf(dir, "%s/%s", m_savegameDir.c_str(), gamer);
+ if ( !RemoveDir(dir) )
{
m_sound->Play(SOUND_TZOING);
return;
@@ -3927,13 +4007,17 @@ void CMainDialog::DefPerso()
bool CMainDialog::IsIOReadScene()
{
- std::string userSaveDir = m_savegameDir + "/" + m_main->GetGamerName();
- auto saveDirs = CResourceManager::ListDirectories(userSaveDir);
- for (auto dir : saveDirs)
+ fs::directory_iterator end_iter;
+
+ fs::path saveDir(m_savegameDir + "/" + m_main->GetGamerName());
+ if (fs::exists(saveDir) && fs::is_directory(saveDir))
{
- if (CResourceManager::Exists(userSaveDir + "/" + dir + "/" + "data.sav"))
+ for( fs::directory_iterator dir_iter(saveDir) ; dir_iter != end_iter ; ++dir_iter)
{
- return true;
+ if ( fs::is_directory(dir_iter->status()) && fs::exists(dir_iter->path() / "data.sav") )
+ {
+ return true;
+ }
}
}
@@ -4007,53 +4091,59 @@ void CMainDialog::IOReadName()
void CMainDialog::IOReadList()
{
- CWindow* pw = static_cast<CWindow*>(m_interface->SearchControl(EVENT_WINDOW5));
- if (pw == nullptr) return;
- CList* pl = static_cast<CList*>(pw->SearchControl(EVENT_INTERFACE_IOLIST));
- if (pl == nullptr) return;
+ FILE* file = NULL;
+ CWindow* pw;
+ CList* pl;
+ char line[500];
+ char name[100];
+ int i;
+ std::vector<fs::path> v;
+
+ pw = static_cast<CWindow*>(m_interface->SearchControl(EVENT_WINDOW5));
+ if ( pw == 0 ) return;
+ pl = static_cast<CList*>(pw->SearchControl(EVENT_INTERFACE_IOLIST));
+ if ( pl == 0 ) return;
pl->Flush();
+ fs::path saveDir(m_savegameDir + "/" + m_main->GetGamerName());
m_saveList.clear();
- std::string userSaveDir = m_savegameDir + "/" + m_main->GetGamerName();
-
- auto saveDirs = CResourceManager::ListDirectories(userSaveDir);
- std::sort(saveDirs.begin(), saveDirs.end());
-
- for (auto dir : saveDirs)
+ if (fs::exists(saveDir) && fs::is_directory(saveDir))
{
- std::string savegameFile = userSaveDir + "/" + dir + "/" + "data.sav";
- if (CResourceManager::Exists(savegameFile))
+ copy(fs::directory_iterator(saveDir), fs::directory_iterator(), back_inserter(v));
+ std::sort(v.begin(), v.end());
+ for( std::vector<fs::path>::iterator iter = v.begin(); iter != v.end(); ++iter)
{
- char line[500];
- char name[100];
+ if ( fs::is_directory(*iter) && fs::exists(*iter / "data.sav") )
+ {
- FILE* file = fopen(savegameFile.c_str(), "r");
- if ( file == NULL ) continue;
+ file = fopen((*iter / "data.sav").make_preferred().string().c_str(), "r");
+ if ( file == NULL ) continue;
- while ( fgets(line, 500, file) != NULL )
- {
- for (int i=0 ; i<500 ; i++ )
+ while ( fgets(line, 500, file) != NULL )
{
- if ( line[i] == '\t' ) line[i] = ' '; // replaces tab by space
- if ( line[i] == '/' && line[i+1] == '/' )
+ for ( i=0 ; i<500 ; i++ )
{
- line[i] = 0;
+ if ( line[i] == '\t' ) line[i] = ' '; // replaces tab by space
+ if ( line[i] == '/' && line[i+1] == '/' )
+ {
+ line[i] = 0;
+ break;
+ }
+ }
+
+ if ( Cmd(line, "Title") )
+ {
+ OpString(line, "text", name);
break;
}
}
+ fclose(file);
- if ( Cmd(line, "Title") )
- {
- OpString(line, "text", name);
- break;
- }
+ pl->SetItemName(m_saveList.size(), name);
+ m_saveList.push_back(*iter);
}
- fclose(file);
-
- pl->SetItemName(m_saveList.size(), name);
- m_saveList.push_back(userSaveDir + "/" + dir);
}
}
@@ -4092,7 +4182,7 @@ void CMainDialog::IOUpdateList()
if (m_saveList.size() <= static_cast<unsigned int>(sel))
return;
- std::string filename = m_saveList.at(sel) + "/" + "screen.png";
+ std::string filename = (m_saveList.at(sel) / "screen.png").make_preferred().string();
if ( m_phase == PHASE_WRITE || m_phase == PHASE_WRITEs )
{
if ( sel < max-1 )
@@ -4136,9 +4226,16 @@ void CMainDialog::IODeleteScene()
return;
}
- if (CResourceManager::DirectoryExists(m_saveList.at(sel)))
+ try
+ {
+ if (fs::exists(m_saveList.at(sel)) && fs::is_directory(m_saveList.at(sel)))
+ {
+ fs::remove_all(m_saveList.at(sel));
+ }
+ }
+ catch (std::exception & e)
{
- CResourceManager::RemoveDirectory(m_saveList.at(sel));
+ GetLogger()->Error("Error removing save %s : %s\n", pl->GetItemName(sel), e.what());
}
IOReadList();
@@ -4182,28 +4279,28 @@ bool CMainDialog::IOWriteScene()
return false;
}
- std::string dir;
+ fs::path dir;
pe->GetText(info, 100);
if (static_cast<unsigned int>(sel) >= m_saveList.size())
{
- dir = m_savegameDir + "/" + m_main->GetGamerName() + "/" + "save" + clearName(info);
+ dir = fs::path(m_savegameDir) / m_main->GetGamerName() / ("save" + clearName(info));
}
else
{
dir = m_saveList.at(sel);
}
- if (!CResourceManager::DirectoryExists(dir))
+ if (!fs::exists(dir))
{
- CResourceManager::CreateDirectory(dir);
+ fs::create_directories(dir);
}
- std::string savegameFileName = dir + "/" + "data.sav";
- std::string fileCBot = dir + "/" + "cbot.run";
- m_main->IOWriteScene(savegameFileName.c_str(), fileCBot.c_str(), info);
+ std::string fileName = (dir / "data.sav").make_preferred().string();
+ std::string fileCBot = (dir / "cbot.run").make_preferred().string();
+ m_main->IOWriteScene(fileName.c_str(), fileCBot.c_str(), info);
m_shotDelay = 3;
- m_shotName = dir + "/" + "screen.png";
+ m_shotName = (dir / "screen.png").make_preferred().string();
return true;
}
@@ -4230,8 +4327,8 @@ bool CMainDialog::IOReadScene()
return false;
}
- std::string fileName = m_saveList.at(sel) + "/" + "data.sav";
- std::string fileCbot = m_saveList.at(sel) + "/" + "cbot.run";
+ std::string fileName = (m_saveList.at(sel) / "data.sav").make_preferred().string();
+ std::string fileCbot = (m_saveList.at(sel) / "cbot.run").make_preferred().string();
file = fopen(fileName.c_str(), "r");
if ( file == NULL )
@@ -4351,10 +4448,17 @@ void CMainDialog::UpdateSceneChap(int &chap)
if ( m_phase == PHASE_USER )
{
j = 0;
+ fs::directory_iterator dirIt(m_savegameDir), dirEndIt;
+ m_userList.clear();
- auto userSaveDirs = CResourceManager::ListDirectories(m_savegameDir);
- std::sort(userSaveDirs.begin(), userSaveDirs.end());
- m_userList = userSaveDirs;
+ for (; dirIt != dirEndIt; ++dirIt)
+ {
+ const fs::path& p = *dirIt;
+ if (fs::is_directory(p))
+ {
+ m_userList.push_back(p.leaf().string());
+ }
+ }
m_userTotal = m_userList.size();
for ( j=0 ; j<m_userTotal ; j++ )
diff --git a/src/ui/maindialog.h b/src/ui/maindialog.h
index 32c0e1d..cb26426 100644
--- a/src/ui/maindialog.h
+++ b/src/ui/maindialog.h
@@ -25,8 +25,13 @@
#include "app/pausemanager.h"
+#include <boost/filesystem.hpp>
+#include <boost/algorithm/string.hpp>
+
#include <vector>
+namespace fs = boost::filesystem;
+
class CEventQueue;
class CSoundInterface;
@@ -260,7 +265,7 @@ protected:
SceneInfo m_sceneInfo[MAXSCENE];
- std::vector<std::string> m_saveList;
+ std::vector<fs::path> m_saveList;
};
} // namespace Ui