Subversion Repositories spk

Rev

Rev 197 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed


#include "GameDirectories.h"
#include "GameExe.h"
#include "Packages.h"
#include "GameExe.h"

namespace SPK {

        CGameDirectories::CGameDirectories(const Utils::WString &mydoc) : _pSelected(NULL),
                _pCurrentItr(NULL),
                _iLanguage(0)
        {
                _pDirs = new std::vector<SGameDir *>();

                _updateControlledDirs(mydoc);
        }

        CGameDirectories::~CGameDirectories(void)
        {
                clear();
                delete _pDirs;
                _lControlledDirs.clear();
        }

        bool CGameDirectories::add(const Utils::WString &dir, const Utils::WString &name, int iGame, const Utils::WString &addon, bool bLoad)
        {
                for ( std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++ ) {
                        if ( dir.Compare((*itr)->dir) ) return false;
                }

                _add(dir, name, iGame, addon, bLoad);
                return true;
        }

        bool CGameDirectories::remove(const Utils::WString &dir)
        {
                for ( std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++ ) {
                        if ( dir.Compare((*itr)->dir) ) {
                                delete (*itr);
                                _pDirs->erase(itr);
                                return true;
                        }
                }

                return false;
        }

        bool CGameDirectories::findDir(const Utils::WString &dir)
        {
                SGameDir *d = _findGameDir(dir);
                if ( d ) {
                        _pCurrentItr = d;
                        return true;
                }

                return false;
        }

        bool CGameDirectories::parse(const Utils::WString &data, CPackages *pPackages)
        {
                int iGame = data.token(L";", 2).tokens(L" ", 1).toLong();
                SGameExe *exe = pPackages->GetGameExe()->game(iGame);
                _add(data.token(L";", 1), data.token(L";", 2).tokens(L" ", 2), iGame, (exe) ? exe->sAddon : Utils::WString::Null(), data.token(L";", 3).toBool());

                return true;
        }

        bool CGameDirectories::writeData(Utils::WStringList* lines)
        {
                bool writeAny = false;
                for (std::vector<SGameDir*>::const_iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
                        SGameDir* gd = *itr;
                        lines->pushBack(L"GameDir:" + gd->dir + L";" + Utils::WString::Number(gd->iGame) + L" " + gd->name + ((gd->bLoad) ? L";1" : L";0"));
                        writeAny = true;
                }

                return writeAny;
        }

        bool CGameDirectories::writeData(std::vector<Utils::WString> &lines)
        {
                bool writeAny = false;
                for (std::vector<SGameDir*>::const_iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
                        SGameDir* gd = *itr;
                        lines.push_back(L"GameDir:" + gd->dir + L";" + Utils::WString::Number(gd->iGame) + L" " + gd->name + ((gd->bLoad) ? L";1" : L";0"));
                        writeAny = true;
                }

                return writeAny;
        }

        void CGameDirectories::updateCurrentVFS(bool bReload)
        {
                if ( !_pCurrentItr ) return;

                // dont load text if its already been loaded, unless we need to reload them
                if ( !bReload && _pCurrentItr->pVfs->isTextUpdated() ) return;

                // update the texts for all pages
                _pCurrentItr->pVfs->updateTexts(0);
        }

        void CGameDirectories::clear()
        {
                for ( std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++ ) {
                        delete *itr;
                }
                _pDirs->clear();
        }

        bool CGameDirectories::setSelectedGameDirectory(int iGame, bool temp)
        {
                _pTemporary = NULL;
                if ( temp ) _pTemporary = _pSelected;
                _pSelected = _findGameDir(iGame);
                return hasSelected();
        }

        bool CGameDirectories::setSelectedDirectory(const Utils::WString &dir, bool temp)
        {
                _pTemporary = NULL;
                if ( temp ) _pTemporary = _pSelected;
                _pSelected = _findGameDir(dir);
                return hasSelected();
        }

        void CGameDirectories::reselectTemporaryDirectory()
        {
                if ( _pTemporary ) _pSelected = _pTemporary;
                _pTemporary = NULL;
        }

        void CGameDirectories::setLanguage(int iLang)
        {
                _iLanguage = iLang;
        }

        void CGameDirectories::setLoad(bool bLoad)
        {
                if ( _pCurrentItr ) _pCurrentItr->bLoad = bLoad;
        }

        bool CGameDirectories::isAllTextLoaded() const
        {
                for ( std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++ ) {
                        if ( !(*itr)->pVfs->isTextUpdated() ) return false;
                }

                return true;
        }

        bool CGameDirectories::isDir(const Utils::WString &dir) const
        {
                for ( std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++ ) {
                        if ( dir.Compare((*itr)->dir) ) return true;
                }

                return false;
        }

        bool CGameDirectories::hasSelected() const
        {
                return (_pSelected) ? true : false;
        }

        bool CGameDirectories::isEmpty() const
        {
                return _pDirs->empty();
        }

        int CGameDirectories::highestGame() const
        {
                int highest = 1;
                for(std::vector<SGameDir *>::const_iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
                        if ( (*itr)->iGame > highest )
                                highest = (*itr)->iGame;
                }

                return highest;
        }

        Utils::WString CGameDirectories::currentName() const
        {
                if ( !_pCurrentItr ) return Utils::WString::Null();
                return _pCurrentItr->name;
        }

        int CGameDirectories::currentGame() const
        {
                if ( !_pCurrentItr ) return -1;
                return _pCurrentItr->iGame;
        }

        bool CGameDirectories::currentLoad() const
        {
                if ( !_pCurrentItr ) return false;
                return _pCurrentItr->bLoad;
        }

        CVirtualFileSystem *CGameDirectories::selectedVFS() const
        {
                if ( _pSelected ) return _pSelected->pVfs;
                return NULL;
        }

        void CGameDirectories::syncWithControlled(CGameExe *exe)
        {
                for(Utils::WStringListIterator itr = _lControlledDirs.begin(); itr != _lControlledDirs.end(); itr++) {
                        add(exe->properDir((*itr)->str), (*itr)->data, exe->getGameType((*itr)->str), exe->addonDir((*itr)->str), true);
                }
        }

        Utils::WString CGameDirectories::findText(int iLanguage, int iPage, int iID, Utils::WString missing)
        {
                if ( iLanguage == 0 ) iLanguage = _iLanguage;
                if ( _pSelected ) {
                        if ( _pSelected->pVfs->textExists(iLanguage, iPage, iID) ) {
                                return _pSelected->pVfs->findText(iLanguage, iPage, iID);
                        }
                }

                return missing;
        }

        Utils::WString CGameDirectories::findText(int iGame, int iLanguage, int iPage, int iID)
        {
                if ( iLanguage == 0 ) iLanguage = _iLanguage;
                if ( iGame == -1 ) {
                        for ( int game = (GAME_MAX - 1); game >= 0; game-- ) {
                                SGameDir *gd = _findGameDir(game - 1);
                                if ( !gd ) continue;
                                if ( gd->pVfs->textExists(iLanguage, iPage, iID) ) {
                                        return gd->pVfs->findText(iLanguage, iPage, iID);
                                }
                        }
                }
                else {
                        SGameDir *gd = _findGameDir(iGame);
                        if ( gd ) {
                                if ( gd->pVfs->textExists(iLanguage, iPage, iID) ) {
                                        return gd->pVfs->findText(iLanguage, iPage, iID);
                                }
                        }
                }

                return Utils::WString::Null();
        }

        Utils::WString CGameDirectories::first(int iGame)
        {
                if ( _pDirs->empty() ) return Utils::WString::Null();

                _pCurrentItr = *_pDirs->begin();
                if ( iGame > -1 && _pCurrentItr->iGame != iGame ) return next(iGame);
                return (_pCurrentItr) ? _pCurrentItr->dir : Utils::WString::Null();
        }

        Utils::WString CGameDirectories::next(int iGame)
        {
                if ( !_pCurrentItr ) return Utils::WString::Null();

                bool found = false;
                for(std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
                        if ( (*itr) == _pCurrentItr ) found = true;
                        else if ( found && ((*itr)->iGame == iGame || iGame == -1) ) {
                                _pCurrentItr = *(itr);
                                return (_pCurrentItr) ? _pCurrentItr->dir : Utils::WString::Null();
                        }
                }

                return Utils::WString::Null();
        }

        Utils::WString CGameDirectories::firstShield()
        {
                if ( !_pSelected ) return Utils::WString::Null();
                return _pSelected->pVfs->firstShield();
        }

        Utils::WString CGameDirectories::nextShield()
        {
                if ( !_pSelected ) return Utils::WString::Null();
                return _pSelected->pVfs->nextShield();
        }

        Utils::WString CGameDirectories::firstComponentSection()
        {
                if ( !_pSelected ) return Utils::WString::Null();
                return _pSelected->pVfs->firstComponentSection();
        }

        Utils::WString CGameDirectories::nextComponentSection()
        {
                if ( !_pSelected ) return Utils::WString::Null();
                return _pSelected->pVfs->nextComponentSection();
        }

        Utils::WString CGameDirectories::firstDummySection()
        {
                if ( !_pSelected ) return Utils::WString::Null();
                return _pSelected->pVfs->firstDummySection();
        }

        Utils::WString CGameDirectories::nextDummySection()
        {
                if ( !_pSelected ) return Utils::WString::Null();
                return _pSelected->pVfs->nextDummySection();
        }

        Utils::WString CGameDirectories::firstBodiesSection()
        {
                if ( !_pSelected ) return Utils::WString::Null();
                return _pSelected->pVfs->firstBodiesSection();
        }

        Utils::WString CGameDirectories::nextBodiesSection()
        {
                if ( !_pSelected ) return Utils::WString::Null();
                return _pSelected->pVfs->nextBodiesSection();
        }

        Utils::WString CGameDirectories::firstCockpit()
        {
                if ( !_pSelected ) return Utils::WString::Null();
                return _pSelected->pVfs->firstCockpit();
        }

        Utils::WString CGameDirectories::nextCockpit()
        {
                if ( !_pSelected ) return Utils::WString::Null();
                return _pSelected->pVfs->nextCockpit();
        }

        std::pair<Utils::WString, Utils::WString> CGameDirectories::firstLaser() const
        {
                if ( !_pSelected ) return std::make_pair<Utils::WString, Utils::WString>(Utils::WString::Null(), Utils::WString::Null());
                return _pSelected->pVfs->firstLaser();
        }

        std::pair<Utils::WString, Utils::WString> CGameDirectories::nextLaser() const
        {
                if ( !_pSelected ) return std::make_pair<Utils::WString, Utils::WString>(Utils::WString::Null(), Utils::WString::Null());
                return _pSelected->pVfs->nextLaser();
        }

        std::pair<Utils::WString, Utils::WString> CGameDirectories::firstMissile() const
        {
                if ( !_pSelected ) return std::make_pair<Utils::WString, Utils::WString>(Utils::WString::Null(), Utils::WString::Null());
                return _pSelected->pVfs->firstMissile();
        }

        std::pair<Utils::WString, Utils::WString> CGameDirectories::nextMissile() const
        {
                if ( !_pSelected ) return std::make_pair<Utils::WString, Utils::WString>(Utils::WString::Null(), Utils::WString::Null());
                return _pSelected->pVfs->nextMissile();
        }

        SGameDir *CGameDirectories::_findGameDir(int iGame) const
        {
                if ( iGame == -1 ) {
                        for ( int game = (GAME_MAX - 1); game >= 0; game-- ) {
                                SGameDir *gd = _findGameDir(game);
                                if ( !gd ) continue;
                                return gd;
                        }
                }
                else {
                        for(std::vector<SGameDir *>::const_iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
                                if ( (*itr)->iGame == iGame ) return *itr;
                        }
                }
                return NULL;
        }

        SGameDir *CGameDirectories::_findGameDir(const Utils::WString &dir) const
        {
                for(std::vector<SGameDir *>::const_iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
                        if ( (*itr)->dir.Compare(dir) )
                                return *itr;
                }

                return NULL;
        }

        void CGameDirectories::_add(const Utils::WString &dir, const Utils::WString &name, int iGame, const Utils::WString &addon, bool bLoad)
        {
                SGameDir *gd = new SGameDir;
                
                gd->dir = dir;
                gd->name = name;
                gd->iGame = iGame;

                gd->bLoad = bLoad;
                gd->pVfs = new CVirtualFileSystem();
                gd->pVfs->setLanguage(44);
                gd->pVfs->setAddon(addon);
                gd->pVfs->LoadFilesystem(gd->dir);

                _pDirs->push_back(gd);
        }

        void CGameDirectories::_updateControlledDirs(const Utils::WString &mydoc)
        {
                _lControlledDirs.clear();

                CFileIO configFile(mydoc + L"/Egosoft/pluginmanager.dat");
                if ( configFile.startRead() ) {
                        while(!configFile.atEnd()) {
                                Utils::WString line = configFile.readEndOfLine();
                                if ( line.token(L":", 1).Compare(L"DirExe") ) {
                                        Utils::WString rest = line.tokens(L":", 2).removeFirstSpace();
                                        if ( rest.countToken(L"|" ) > 2 )
                                                _lControlledDirs.pushBack(rest.token(L"|", 3), rest.token(L"|", 2));
                                        else
                                                _lControlledDirs.pushBack(rest.token(L"|", 2), rest.token(L"|", 1));
                                }
                        }
                }
        }
}