Rev 114 | Rev 227 | 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::String &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::String &dir, const Utils::String &name, int iGame, const Utils::String &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::String &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::String &dir)
{
SGameDir *d = _findGameDir(dir);
if ( d ) {
_pCurrentItr = d;
return true;
}
return false;
}
bool CGameDirectories::parse(const Utils::String &data, CPackages *pPackages)
{
int iGame = data.token(";", 2).tokens(" ", 1).toLong();
SGameExe *exe = pPackages->GetGameExe()->GetGame(iGame);
_add(data.token(";", 1), data.token(";", 2).tokens(" ", 2), iGame, (exe) ? exe->sAddon : Utils::String::Null(), data.token(";", 3).toBool());
return true;
}
bool CGameDirectories::writeData(Utils::CStringList *lines)
{
bool writeAny = false;
for(std::vector<SGameDir *>::const_iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
SGameDir *gd = *itr;
lines->pushBack("GameDir:" + gd->dir + ";" + Utils::String::Number(gd->iGame) + " " + gd->name + ((gd->bLoad) ? ";1" : ";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::String &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::String &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::String CGameDirectories::currentName() const
{
if ( !_pCurrentItr ) return Utils::String::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::CStringListIterator itr = _lControlledDirs.begin(); itr != _lControlledDirs.end(); itr++) {
add(exe->GetProperDir((*itr)->str), (*itr)->data, exe->GetGameType((*itr)->str), exe->GetAddonDir((*itr)->str), true);
}
}
Utils::String CGameDirectories::findText(int iLanguage, int iPage, int iID, Utils::String 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::String 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::String::Null();
}
Utils::String CGameDirectories::first(int iGame)
{
if ( _pDirs->empty() ) return Utils::String::Null();
_pCurrentItr = *_pDirs->begin();
if ( iGame > -1 && _pCurrentItr->iGame != iGame ) return next(iGame);
return (_pCurrentItr) ? _pCurrentItr->dir : Utils::String::Null();
}
Utils::String CGameDirectories::next(int iGame)
{
if ( !_pCurrentItr ) return Utils::String::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::String::Null();
}
}
return Utils::String::Null();
}
Utils::String CGameDirectories::firstShield()
{
if ( !_pSelected ) return Utils::String::Null();
return _pSelected->pVfs->firstShield();
}
Utils::String CGameDirectories::nextShield()
{
if ( !_pSelected ) return Utils::String::Null();
return _pSelected->pVfs->nextShield();
}
Utils::String CGameDirectories::firstComponentSection()
{
if ( !_pSelected ) return Utils::String::Null();
return _pSelected->pVfs->firstComponentSection();
}
Utils::String CGameDirectories::nextComponentSection()
{
if ( !_pSelected ) return Utils::String::Null();
return _pSelected->pVfs->nextComponentSection();
}
Utils::String CGameDirectories::firstDummySection()
{
if ( !_pSelected ) return Utils::String::Null();
return _pSelected->pVfs->firstDummySection();
}
Utils::String CGameDirectories::nextDummySection()
{
if ( !_pSelected ) return Utils::String::Null();
return _pSelected->pVfs->nextDummySection();
}
Utils::String CGameDirectories::firstBodiesSection()
{
if ( !_pSelected ) return Utils::String::Null();
return _pSelected->pVfs->firstBodiesSection();
}
Utils::String CGameDirectories::nextBodiesSection()
{
if ( !_pSelected ) return Utils::String::Null();
return _pSelected->pVfs->nextBodiesSection();
}
Utils::String CGameDirectories::firstCockpit()
{
if ( !_pSelected ) return Utils::String::Null();
return _pSelected->pVfs->firstCockpit();
}
Utils::String CGameDirectories::nextCockpit()
{
if ( !_pSelected ) return Utils::String::Null();
return _pSelected->pVfs->nextCockpit();
}
std::pair<Utils::String, Utils::String> CGameDirectories::firstLaser()
{
if ( !_pSelected ) return std::make_pair<Utils::String, Utils::String>(Utils::String::Null(), Utils::String::Null());
return _pSelected->pVfs->firstLaser();
}
std::pair<Utils::String, Utils::String> CGameDirectories::nextLaser()
{
if ( !_pSelected ) return std::make_pair<Utils::String, Utils::String>(Utils::String::Null(), Utils::String::Null());
return _pSelected->pVfs->nextLaser();
}
std::pair<Utils::String, Utils::String> CGameDirectories::firstMissile()
{
if ( !_pSelected ) return std::make_pair<Utils::String, Utils::String>(Utils::String::Null(), Utils::String::Null());
return _pSelected->pVfs->firstMissile();
}
std::pair<Utils::String, Utils::String> CGameDirectories::nextMissile()
{
if ( !_pSelected ) return std::make_pair<Utils::String, Utils::String>(Utils::String::Null(), Utils::String::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::String &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::String &dir, const Utils::String &name, int iGame, const Utils::String &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::String &mydoc)
{
_lControlledDirs.clear();
CFileIO configFile(mydoc + "/Egosoft/pluginmanager.dat");
if ( configFile.startRead() ) {
while(!configFile.atEnd()) {
Utils::String line = configFile.readEndOfLine();
if ( line.token(":", 1).Compare("DirExe") ) {
Utils::String rest = line.tokens(":", 2).removeFirstSpace();
if ( rest.countToken("|" ) > 2 )
_lControlledDirs.pushBack(rest.token("|", 3), rest.token("|", 2));
else
_lControlledDirs.pushBack(rest.token("|", 2), rest.token("|", 1));
}
}
}
}
}