diff options
Diffstat (limited to 'src/ui/maindialog.cpp')
-rw-r--r-- | src/ui/maindialog.cpp | 405 |
1 files changed, 193 insertions, 212 deletions
diff --git a/src/ui/maindialog.cpp b/src/ui/maindialog.cpp index ba8da9e..3acf1dd 100644 --- a/src/ui/maindialog.cpp +++ b/src/ui/maindialog.cpp @@ -33,6 +33,8 @@ #include "common/resources/inputstream.h" #include "common/resources/outputstream.h" +#include "object/level/parser.h" + #include "object/robotmain.h" #include "script/cmdtoken.h" @@ -58,6 +60,8 @@ #include <sstream> #include <iomanip> #include <vector> +#include <boost/filesystem.hpp> +#include <boost/foreach.hpp> //TODO Get rid of all sprintf's @@ -108,6 +112,8 @@ static int perso_color[3*10*3] = 0, 0, 0, // }; +namespace fs = boost::filesystem; + // Constructor of robot application. CMainDialog::CMainDialog() @@ -174,7 +180,6 @@ CMainDialog::CMainDialog() m_savegameDir = "savegame"; m_publicDir = "program"; - m_userDir = "user"; m_filesDir = m_savegameDir; m_setupFull = m_app->GetVideoConfig().fullScreen; @@ -306,7 +311,6 @@ void CMainDialog::ChangePhase(Phase phase) pb = pw->CreateButton(pos, ddim, -1, EVENT_INTERFACE_QUIT); pb->SetState(STATE_SHADOW); - #if DEV_BUILD if ( m_accessEnable && m_accessUser ) { pos.x = 447.0f/640.0f; @@ -315,7 +319,6 @@ void CMainDialog::ChangePhase(Phase phase) pb = pw->CreateButton(pos, ddim, -1, EVENT_INTERFACE_USER); pb->SetState(STATE_SHADOW); } - #endif /*pos.x = 139.0f/640.0f; pos.y = 313.0f/480.0f; @@ -713,7 +716,7 @@ void CMainDialog::ChangePhase(Phase phase) if ( m_phase == PHASE_DEFI ) strcpy(m_sceneName, "challenges" ); if ( m_phase == PHASE_MISSION ) strcpy(m_sceneName, "missions"); if ( m_phase == PHASE_FREE ) strcpy(m_sceneName, "freemissions"); - if ( m_phase == PHASE_USER ) strcpy(m_sceneName, "user"); + if ( m_phase == PHASE_USER ) strcpy(m_sceneName, "custom"); ReadGamerInfo(); @@ -3261,23 +3264,6 @@ void CMainDialog::NiceParticle(Math::Point mouse, bool bPress) -// Specifies the special user folder if needed. - -void CMainDialog::SetUserDir(char *base, int rank) -{ - std::string dir; - - if ( strcmp(base, "user") == 0 && rank >= 100 ) - { - dir = m_userDir + "/" + m_userList.at(rank/100-1); - GetProfile().SetUserDir(dir); - } - else - { - GetProfile().SetUserDir(""); - } -} - // Builds the file name of a mission. void CMainDialog::BuildSceneName(std::string &filename, char *base, int rank, bool sceneFile) @@ -3286,45 +3272,7 @@ void CMainDialog::BuildSceneName(std::string &filename, char *base, int rank, bo int chapter = rank/100; int new_rank = rank%100; - std::ostringstream outstream; - if ( strcmp(base, "user") == 0 ) - { - //TODO: Change this to point user dir according to operating system - /*rankStream << std::setfill('0') << std::setw(2) << rank%100; - filename = m_userDir + "/" + m_userList[rank/100-1] + "/" + rankStream.str() + ".txt";*/ - assert(false); //TODO: Userlevel support - } - else if ( strcmp(base, "perso") == 0 ) - { - filename = "levels/other/perso.txt"; - } - else if( strcmp(base, "win") == 0 || strcmp(base, "lost") == 0 ) - { - outstream << "levels/other/"; - outstream << base << std::setfill('0') << std::setw(3) << rank << ".txt"; - filename = outstream.str(); - } - else - { - outstream << "levels/" << base << "/"; - outstream << "chapter" << std::setfill('0') << std::setw(3) << chapter << "/"; - if(new_rank == 000) - { - if(sceneFile) - { - outstream << "chaptertitle.txt"; - } - } - else - { - outstream << "level" << std::setfill('0') << std::setw(3) << new_rank << "/"; - if(sceneFile) - { - outstream << "scene.txt"; - } - } - filename = outstream.str(); - } + filename = CLevelParser::BuildSceneName(std::string(base), chapter, new_rank, sceneFile); } // Built the default descriptive name of a mission. @@ -3346,16 +3294,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 + { + 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, userSaveDirs.at(i).c_str()); + pl->SetItemName(i, fileNames.at(i).c_str()); } } @@ -3538,6 +3520,7 @@ void CMainDialog::NameCreate() CWindow* pw; CEdit* pe; char name[100]; + std::string dir; char c; int len, i, j; @@ -3578,10 +3561,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 +3584,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]; - int sel = pl->GetSelect(); - if (sel == -1) + 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; + + 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 +3945,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 +4029,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 +4120,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 +4164,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 +4217,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 +4265,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 ) @@ -4326,7 +4361,6 @@ void CMainDialog::UpdateSceneChap(int &chap) { CWindow* pw; CList* pl; - FILE *file; std::string fileName; char op[100]; @@ -4351,53 +4385,25 @@ void CMainDialog::UpdateSceneChap(int &chap) if ( m_phase == PHASE_USER ) { j = 0; - - auto userSaveDirs = CResourceManager::ListDirectories(m_savegameDir); - std::sort(userSaveDirs.begin(), userSaveDirs.end()); - m_userList = userSaveDirs; + auto userLevelDirs = CResourceManager::ListDirectories("levels/custom/"); + std::sort(userLevelDirs.begin(), userLevelDirs.end()); + m_userList = userLevelDirs; m_userTotal = m_userList.size(); for ( j=0 ; j<m_userTotal ; j++ ) { - BuildSceneName(fileName, m_sceneName, (j+1)*100); - file = fopen(fileName.c_str(), "r"); - if ( file == NULL ) - { - strcpy(name, m_userList[j].c_str()); + try { + CLevelParser* level = new CLevelParser("custom", j+1, 0); + level->Load(); + pl->SetItemName(j, level->Get("Title")->GetParam("text")->AsString().c_str()); + pl->SetEnable(j, true); + delete level; } - else + catch(CLevelParserException& e) { - BuildResumeName(name, m_sceneName, j+1); // default name - sprintf(op, "Title.E"); - sprintf(op_i18n, "Title.%c", m_app->GetLanguageChar()); - - while ( fgets(line, 500, file) != NULL ) - { - for ( i=0 ; i<500 ; i++ ) - { - if ( line[i] == '\t' ) line[i] = ' '; // replaces tab by space - if ( line[i] == '/' && line[i+1] == '/' ) - { - line[i] = 0; - break; - } - } - - if ( Cmd(line, op) ) - { - OpString(line, "text", name); - } - if ( Cmd(line, op_i18n) ) - { - OpString(line, "text", name); - break; - } - } - fclose(file); + pl->SetItemName(j, (std::string("[ERROR]: ")+e.what()).c_str()); + pl->SetEnable(j, false); } - - pl->SetItemName(j, name); - pl->SetEnable(j, true); } } else @@ -4488,6 +4494,8 @@ void CMainDialog::UpdateSceneList(int chap, int &sel) if ( pl == 0 ) return; pl->Flush(); + + if(chap < 0) return; for ( j=0 ; j<99 ; j++ ) { @@ -4634,44 +4642,18 @@ void CMainDialog::UpdateSceneResume(int rank) m_bSceneSoluce = false; } } - - BuildSceneName(fileName, m_sceneName, rank); - sprintf(op, "Resume.E"); - sprintf(op_i18n, "Resume.%c", m_app->GetLanguageChar()); - - CInputStream stream; - stream.open(fileName); - if (!stream.is_open()) return; - - name[0] = 0; - while ( stream.getline(line, 500) ) + + if(rank<100) return; + + try { + CLevelParser* level = new CLevelParser(m_sceneName, rank/100, rank%100); + level->Load(); + pe->SetText(level->Get("Resume")->GetParam("text")->AsString().c_str()); + } + catch(CLevelParserException& e) { - for ( i=0 ; i<500 ; i++ ) - { - if (line[i] == 0) - break; - - if ( line[i] == '\t' ) line[i] = ' '; // replaces tab by space - if ( line[i] == '/' && line[i+1] == '/' ) - { - line[i] = 0; - break; - } - } - - if ( Cmd(line, op) ) - { - OpString(line, "text", name); - } - if ( Cmd(line, op_i18n) ) - { - OpString(line, "text", name); - break; - } + pe->SetText((std::string("[ERROR]: ")+e.what()).c_str()); } - stream.close(); - - pe->SetText(name); } // Updates the list of devices. @@ -5098,7 +5080,6 @@ void CMainDialog::SetupMemorize() { GetProfile().SetLocalProfileString("Directory", "savegame", m_savegameDir); GetProfile().SetLocalProfileString("Directory", "public", m_publicDir); - GetProfile().SetLocalProfileString("Directory", "user", m_userDir); GetProfile().SetLocalProfileString("Directory", "files", m_filesDir); GetProfile().SetLocalProfileInt("Setup", "Tooltips", m_bTooltip); GetProfile().SetLocalProfileInt("Setup", "InterfaceGlint", m_bGlint); @@ -5184,11 +5165,6 @@ void CMainDialog::SetupRecall() m_publicDir = key; } - if ( GetProfile().GetLocalProfileString("Directory", "user", key) ) - { - m_userDir = key; - } - if ( GetProfile().GetLocalProfileString("Directory", "files", key) ) { m_filesDir = key; @@ -6383,6 +6359,11 @@ bool CMainDialog::NextMission() return true; } +std::string& CMainDialog::GetUserLevelName(int id) +{ + return m_userList[id-1]; +} + } // namespace Ui |