Subversion Repositories spk

Rev

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


#include "VirtualFileSystem.h"
#include "File_IO.h"
#include "CatFile.h"

CVirtualFileSystem::CVirtualFileSystem()
{
        m_bLoaded = false;
        m_pMap = NULL;
        m_sAddon = "";
}

CVirtualFileSystem::~CVirtualFileSystem(void)
{
        if ( m_pMap )
                delete m_pMap;
}

bool CVirtualFileSystem::LoadFilesystem(CyString &dir, CyString &mod, int maxPatch)
{
        m_sDir = dir;
        m_bLoaded = false;

        if ( m_pMap ) delete m_pMap;
        m_pMap = new MAP;

        int number = 1;
        while ( CFileIO(dir + "/" + CyString::Number(number).PadNumber(2) + ".cat").Exists() )
        {
                if ( maxPatch && maxPatch > number ) break;
                CyString file = dir + "/" + CyString::Number(number).PadNumber(2);
                if ( !CFileIO(file + ".dat").Exists() )
                        break;

                CCatFile cat;
                if ( cat.Open(file + ".cat", m_sAddon, CATREAD_JUSTCONTENTS, false) == CATERR_NONE )
                {
                        for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() )
                        {
                                (*m_pMap)[CFileIO(c->Data()->sFile).GetFullFilename().ToLower().c_str()] = CyString(file + ".cat").c_str();
                                m_bLoaded = true;
                        }
                }
                ++number;
        }

        if ( !mod.Empty() )
                LoadMod(mod);

        return m_bLoaded;
}

bool CVirtualFileSystem::LoadMod(CyString &mod)
{
        bool loaded = false;
        CCatFile cat;
        if ( CCatFile::Opened(cat.Open(mod, m_sAddon, CATREAD_JUSTCONTENTS, false), false) )
        {
                for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() )
                {
                        (*m_pMap)[CFileIO(c->Data()->sFile).GetFullFilename().ToLower().c_str()] = mod.c_str();
                        loaded = true;
                }
        }

        return loaded;
}

CyString CVirtualFileSystem::GetFile(CyString &file)
{
        if ( !m_pMap ) return NullString;
        MAP::iterator itr = m_pMap->find(CFileIO(file).GetFullFilename().ToLower().c_str());
        if ( itr == m_pMap->end() ) return NullString;
        return itr->second;
}

bool CVirtualFileSystem::ExtractGameFile(CyString &file, CyString &to)
{
        CyString sIn = GetFile(file);
        if ( sIn.Empty() ) return false;

        CCatFile catFile;
        if ( catFile.Open(sIn, m_sAddon, CATREAD_CATDECRYPT, false) == CATERR_NONE )
        {
                // check for the file
                if ( catFile.ExtractFile(file, to) )
                        return true;
                if ( catFile.Error() == CATERR_INVALIDDEST || catFile.Error() == CATERR_CANTCREATEDIR )
                {
                        if ( catFile.ExtractFile(file) )
                                return true;
                }
        }
        return false;
}