Subversion Repositories spk

Rev

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


#include "ModDiff.h"
#include "File_IO.h"
#include "CatFile.h"

CModDiff::CModDiff(const Utils::String &dir, const Utils::String &sAddon, int maxPatch) : m_pCatFile(NULL), m_sAddon(sAddon), m_sTempDir("."), m_iMaxPatch(maxPatch)
{
        m_bLoaded = this->LoadDirectory(dir);
}

CModDiff::~CModDiff(void)
{
        delete m_pCatFile;
}

bool CModDiff::LoadDirectory(const Utils::String &dir)
{
        m_sCurrentDir = dir;
        m_fileSystem.setAddon(m_sAddon);
        return m_fileSystem.LoadFilesystem(dir, m_iMaxPatch);
}

bool CModDiff::startDiff(const Utils::String &sModFile)
{
        ClearError();
        if ( !CFileIO::Exists(sModFile) ) { m_iError = MDERR_FILENOTFOUND; return false; }

        delete m_pCatFile;
        m_pCatFile = new CCatFile;

        if ( m_pCatFile->open(sModFile, m_sAddon, CATREAD_CATDECRYPT, false) != CATERR_NONE ) { 
                delete m_pCatFile;
                m_pCatFile = NULL;
                m_iError = MDERR_CANTOPENMOD; 
                return false; 
        }

        return true;
}

Utils::String CModDiff::_extractFile(const Utils::String &sFile, const Utils::String &sTo)
{
        SInCatFile *c = m_pCatFile->FindData(sFile);
        if ( !c ) c = m_pCatFile->FindData(m_sAddon + "/" + sFile);
        if ( !c ) return m_fileSystem.ExtractGameFile(sFile, sTo);
        if ( m_pCatFile->ExtractFile(c, sTo) ) return sTo;
        return "";
}

bool CModDiff::doDiff(const Utils::String &sModFile)
{
        // find the file in the loaded cat
        SInCatFile *c = m_pCatFile->FindData(sModFile);
        if ( !c ) c = m_pCatFile->FindData(m_sAddon + "/" + sModFile);
        if ( !c ) return false;

        // extract the matching file
        Utils::String sToFile = CFileIO(m_sTempDir + "/" + CFileIO(c->sFile).filename()).fullFilename();
        if ( !m_pCatFile->ExtractFile(sModFile, sToFile) ) return false;

        // create a diff
        Utils::String to = m_fileSystem.ExtractGameFile(c->sFile, sToFile + ".compare");
        if ( !to.empty() ) {
                SDiffFile *diff = diffFile(to, sToFile, c->sFile);
                if ( diff ) { 
                        this->_adjustFile(sModFile, diff, false);
                }
                CFileIO::Remove(to);
        }

        CFileIO::Remove(sToFile);

        return true;
}


bool CModDiff::_adjustTShips(SDiffFile *pDiff, bool bReverse)
{
        // need to read TCockpits
        Utils::String sTo = this->_extractFile("types/TCockpits.pck", "TCockpits.xml");
        if ( sTo.empty() ) return false;
        CFileIO TCockpits("TCockpits.xml");
        if ( !TCockpits.exists() ) return false;
        CyStringList *pFiles = TCockpits.ReadLinesStr();
        if ( !pFiles ) return false;

        // remove all the comments
        for ( SStringList *str = pFiles->Head(); str; str = str->next ) {
                if ( str->str[0] == '/' ) str->remove = true;
        }
        pFiles->RemoveMarked();
        pFiles->PopFront();

        std::map<Utils::String, int> fileSet;
        int iPos = 0;
        for ( SStringList *str = pFiles->Head(); str; str = str->next ) fileSet[Utils::String(str->str.ToString()).token(";", -2)] = iPos++;

        // now cycle through ships and adjust the cockpits
        int iCount = -1;
        for ( SDiffEntry *pEntry = pDiff->m_lEntries.First(); pEntry; pEntry = pDiff->m_lEntries.Next() ) {
                switch (pEntry->iType) {
                        case DIFFTYPE_ADDITION:
                                {
                                        SDiffEntryAddition *pAddition = static_cast<SDiffEntryAddition *>(pEntry);
                                        if ( pAddition ) {
                                                for ( int t = 0; t < 6; t++ ) {
                                                        Utils::String sE = pAddition->sEntry.token(";", 32 + (t * 2));
                                                        if ( !bReverse && sE.isNumber() ) {
                                                                int iTurret = sE;
                                                                if ( iTurret && iTurret < pFiles->Count() ) {
                                                                        Utils::String sCockpit = pFiles->GetAt(iTurret)->str.ToString();
                                                                        pAddition->sEntry = pAddition->sEntry.replaceToken(";", 32 + (t * 2), sCockpit.token(";", -2) + ":" + static_cast<long>(iTurret));
                                                                }
                                                        }
                                                        else if ( bReverse && !sE.isNumber() ) {
                                                                int iUnfound = 0;
                                                                if ( sE.isin(":") ) {
                                                                        iUnfound = sE.token(":", 2);
                                                                        sE = sE.token(":", 1);
                                                                }
                                                                int iEntry = (fileSet.find(sE) == fileSet.end()) ? iUnfound : fileSet[sE];
                                                                pAddition->sEntry = pAddition->sEntry.replaceToken(";", 32 + (t * 2), static_cast<long>(iEntry));
                                                        }
                                                }
                                        }
                                }
                                break;
                        case DIFFTYPE_CHANGE:
                                {
                                        SDiffEntryChange *pChange = static_cast<SDiffEntryChange *>(pEntry);
                                        if ( pChange->iPos >= 32 && pChange->iPos <= 42 && (pChange->iPos % 2) && pChange->sEntry.isNumber() ) {
                                                Utils::String sCockpit = pFiles->GetAt(pChange->sEntry)->str.ToString();
                                                pChange->sEntry = sCockpit.token(";", -2) + ":" + pChange->sEntry;
                                        }
                                }
                                break;
                }
        }

        delete pFiles;

        return true;
}

int CModDiff::_specialType(const Utils::String &sFile)
{
        if ( sFile.Compare("TShips") ) return MERGETYPE_TSHIPS;
        return MERGETYPE_NONE;
}

void CModDiff::_adjustFile(const Utils::String &sFile, SDiffFile *pDiff, bool bReverse)
{
        // check if we need to adjust the file
        CFileIO File(sFile);
        int iType = _specialType(File.baseName());
        if ( iType == MERGETYPE_NONE ) return;

        // read in the file data
        switch(iType) {
                case MERGETYPE_TSHIPS:
                        this->_adjustTShips(pDiff, bReverse);
                        break;
        }
}

bool CModDiff::CreateDiff(const Utils::String &modfile)
{
        ClearError();

        //check for valid parameters
        if ( !CFileIO(modfile).ExistsOld() ) { m_iError = MDERR_FILENOTFOUND; return false; }

        Utils::String addonDir = "";
        int addonSize = addonDir.length() + 1;

        // try and open the mod file
        CCatFile cat;
        if ( cat.open(modfile, addonDir, CATREAD_DAT, false) != CATERR_NONE ) { m_iError = MDERR_CANTOPENMOD; return false; }

        // we'll need to read in all the types/text files
        for (unsigned int i = 0; i < cat.GetNumFiles(); i++)
        {
                SInCatFile *f = cat.GetFile(i);
                Utils::String checkFile = f->sFile.findReplace("\\", "/");
                if ( (checkFile.left(6).Compare("types/") || checkFile.left(2).Compare("t/") || checkFile.left(6 + addonSize).Compare(addonDir + "/types/") || checkFile.left(2 + addonSize).Compare(addonDir + "/t/")) && _validFile(checkFile) )
                {
                        // extract the file to the temp dir
                        Utils::String toFile = CFileIO(m_sTempDir + "/" + CFileIO(f->sFile).filename()).fullFilename();
                        if ( cat.ExtractFile(f, toFile ) )
                        {
                                // now extract the matching file from the game dir
                                if ( m_fileSystem.ExtractGameFile(f->sFile, toFile + ".compare") )
                                {
                                        diffFile(toFile + ".compare", toFile, f->sFile);
                                        CFileIO::Remove(toFile + ".compare");
                                }
                                // make sure we clear up afterwards
                                CFileIO::Remove(toFile);
                        }
                }
        }

        return true;
}

SDiffFile *CModDiff::diffFile(const Utils::String &baseFile, const Utils::String &modFile, const Utils::String &fileType)
{
        int type = 0;

        SDiffFile *diffFile = new SDiffFile;
        diffFile->sFile = fileType;

        // read both files and compare them
        CFileIO Base(baseFile);
        CyStringList *baseLines = Base.ReadLinesStr();
        if ( baseLines )
        {
                CFileIO Mod(modFile);
                CyStringList *lines = Mod.ReadLinesStr();
                if ( lines )
                {
                        int id = -1;
                        SStringList *node[2];
                        node[0] = baseLines->Head();
                        node[1] = lines->Head();

                        Utils::String prev[2];

                        while ( node[0] || node[1] )
                        {
                                Utils::String str[2];

                                for ( int i = 0; i < 2; i++ )
                                {
                                        while (node[i])
                                        {
                                                Utils::String l = node[i]->str.ToString();
                                                node[i] = node[i]->next;

                                                l.removeFirstSpace();
                                                l.removeChar('\r');
                                                if ( !l.empty() && l.left(1) != "/") {
                                                        str[i] += l;
                                                        if ( _isLineComplete(str[i], fileType, (id == -1) ? true : false) ) 
                                                                break;
                                                }
                                        }
                                }

                                if ( id == -1 )
                                        id = 0;
                                else
                                {
                                        // first check for mismatch amount, one of the nodes will be empty
                                        if ( str[0].empty() && !str[1].empty() ) // mod file has more entries (these must be additions)
                                        {
                                                SDiffEntryAddition *entry = new SDiffEntryAddition;
                                                entry->iID = id;
                                                entry->sEntry = str[1];
                                                diffFile->m_lEntries.push_back(entry);
                                        }
                                        else if ( str[1].empty() && !str[0].empty() ) // mod file has less entries (must have removed some)
                                        {
                                                SDiffEntry *entry = new SDiffEntryRemoval;
                                                entry->iID = id;
                                                diffFile->m_lEntries.push_back(entry);
                                        }
                                        else // we have a line for both, we need to compare them
                                        {
                                                // we migth have multiple entries to add when changed
                                                if ( str[0].empty() || str[1].empty() ) continue;
                                                _compareLine(str[0], str[1], type, id, diffFile);                                               
                                        }

                                        ++id;
                                }
                        }

                        delete lines;
                }
                delete baseLines;
        }

        if ( diffFile->m_lEntries.empty() ) {
                delete diffFile;
                return NULL;
        }
        else {
                m_lFiles.push_back(diffFile);
                return diffFile;
        }
}

int CModDiff::_amountPosition(const Utils::String &fileType)
{
        return 2;
}

bool CModDiff::_isLineComplete(const Utils::String &line, const Utils::String &fileType, bool first)
{
        if ( first )
        {
                if ( line.countToken(";") > _amountPosition(fileType) ) return true;
                return false;
        }

        return true;
}

void CModDiff::_compareLine(const Utils::String &line1, const Utils::String &line2, int type, int id, SDiffFile *diffFile)
{
        int max1, max2;
        Utils::String *str1 = line1.tokenise(";", &max1);
        Utils::String *str2 = line2.tokenise(";", &max2);

        if ( !str1 || !str2 ) return;

        int max = ((max1 > max2) ? max2 : max1);
        for ( int i = 0; i < max; i++ )
        {
                if ( str1[i] == str2[i] ) continue;
                if ( str1[i].Compare(str2[i]) ) continue;
                if ( str1[i].empty() && str2[i].empty() ) continue;
                if ( str1[i].length() && str1[i][0] == '\0' && str2[i].length() && str2[i][0] == '\0' ) continue;
                SDiffEntryChange *diff = new SDiffEntryChange;
                diff->iID = id;
                diff->iPos = i;
                diff->sEntry = str2[i];
                diff->sFrom = str1[i];
                diffFile->m_lEntries.push_back(diff);
        }
}

void CModDiff::Clean()
{
        for ( CListNode<SDiffFile> *node = m_lFiles.Front(); node; node = node->next() )
        {
                node->Data()->m_lEntries.clear();
                node->DeleteData();
        }
        m_lFiles.clear();
}

bool CModDiff::WriteDiff(const Utils::String &file)
{
        if ( m_lFiles.empty() ) return false;

        CyStringList lines;
        
        for ( CListNode<SDiffFile> *node = m_lFiles.Front(); node; node = node->next() )
        {
                lines.PushBack(CyString("$$") + node->Data()->sFile);
                for ( CListNode<SDiffEntry> *node2 = node->Data()->m_lEntries.Front(); node2; node2 = node2->next() )
                {
                        switch(node2->Data()->iType)
                        {
                                case DIFFTYPE_ADDITION:
                                        lines.PushBack(CyString("+++:") + (long)node2->Data()->iID + ":" + ((SDiffEntryAddition *)node2->Data())->sEntry);
                                        break;
                                case DIFFTYPE_REMOVAL:
                                        lines.PushBack(CyString("---:") + (long)node2->Data()->iID);
                                        break;
                                case DIFFTYPE_CHANGE:
                                        lines.PushBack(CyString("///:") + (long)node2->Data()->iID + ":" + (long)((SDiffEntryChange *)node2->Data())->iPos + ":" + ((SDiffEntryChange *)node2->Data())->sEntry);
                                        break;
                        }
                }
        }
        

        CFileIO File(file);
        return File.WriteFile(&lines);
}

bool CModDiff::ReadDiff(const Utils::String &file)
{
        Clean();

        CFileIO File(file);
        if ( !File.exists() ) return false;

        CyStringList *lines = File.ReadLinesStr();

        if ( lines )
        {
                SDiffFile *diffFile = NULL;
                for ( SStringList *str = lines->Head(); str; str = str->next )
                {
                        if ( str->str.Left(2).Compare("$$") )
                        {
                                diffFile = new SDiffFile;
                                m_lFiles.push_back(diffFile);
                                diffFile->sFile = str->str.Right(-2).ToString();
                        }
                        else if ( diffFile )
                        {
                                if ( str->str.Left(4).Compare("+++:") )
                                {
                                        SDiffEntryAddition *addition = new SDiffEntryAddition;
                                        addition->iID = str->str.GetToken(":", 2, 2).ToInt();
                                        addition->sEntry = str->str.GetToken(":", 3).ToString();
                                        diffFile->m_lEntries.push_back(addition);
                                }
                                else if ( str->str.Left(4).Compare("---:") )
                                {
                                        SDiffEntryRemoval *entry = new SDiffEntryRemoval;
                                        entry->iID = str->str.GetToken(":", 2, 2).ToInt();
                                        diffFile->m_lEntries.push_back(entry);
                                }
                                else if ( str->str.Left(4).Compare("///:") )
                                {
                                        SDiffEntryChange *entry = new SDiffEntryChange;
                                        entry->iID = str->str.GetToken(":", 2, 2).ToInt();
                                        entry->iPos = str->str.GetToken(":", 3, 3).ToInt();
                                        entry->sEntry = str->str.GetToken(":", 4).ToString();
                                        diffFile->m_lEntries.push_back(entry);
                                }
                        }
                }

                delete lines;

                return true;
        }

        return false;
}

bool CModDiff::ApplyMod(const Utils::String &mod)
{
        return m_fileSystem.addMod(mod);
}

bool CModDiff::ApplyDiff(const Utils::String &mod)
{
        if ( m_lFiles.empty() ) return false;

        this->ApplyMod(mod);

        bool ret = false;

        Utils::String addonDir = "";

        CCatFile cat;
        if ( !CCatFile::Opened(cat.open(mod, addonDir, CATREAD_CATDECRYPT, true)) )
                return false;

        for ( CListNode<SDiffFile> *node = m_lFiles.Front(); node; node = node->next() )
        {
                // extract the file from the game
                SDiffFile *f = node->Data();

                CyStringList writeLines;
                int id;
                if ( _readGameFile(f->sFile, &writeLines, &id) )
                {
                        // now apply the diff
                        this->_adjustFile(f->sFile, f, true);
                        for ( CListNode<SDiffEntry> *eNode = f->m_lEntries.Front(); eNode; eNode = eNode->next() )
                        {
                                switch ( eNode->Data()->iType )
                                {
                                        case DIFFTYPE_ADDITION:
                                                writeLines.PushBack(CyString(((SDiffEntryAddition *)eNode->Data())->sEntry));
                                                break;
                                        case DIFFTYPE_REMOVAL:
                                                writeLines.GetAt(eNode->Data()->iID)->remove = true;
                                                break;
                                        case DIFFTYPE_CHANGE:
                                                {
                                                        SStringList *strAt = writeLines.GetAt(eNode->Data()->iID);
                                                        if ( strAt )
                                                                strAt->str.RepToken(";", ((SDiffEntryChange *)eNode->Data())->iPos, ((SDiffEntryChange *)eNode->Data())->sEntry);
                                                }
                                                break;
                                }
                        }

                        // add our comments and info
                        writeLines.PushFront(CyString((long)id) + ";" + (long)writeLines.Count() + ";");
                        writeLines.PushFront(CyString("// Generated by ModDiff (SPK Version: ") + CyString::CreateFromFloat(GetLibraryVersion(), 2) + ")");

                        // now write the file
                        CFileIO WriteFile(m_sTempDir + "/" + CFileIO(f->sFile).filename());
                        if ( WriteFile.WriteFile(&writeLines) )
                        {
                                if ( cat.AppendFile(m_sTempDir + "/" + CFileIO(f->sFile).filename(), f->sFile) )
                                {
                                        ret = true;
                                }
                                WriteFile.remove();
                        }
                }
        }

        if ( ret )
                cat.WriteCatFile();

        return ret;
}

bool CModDiff::_readGameFile(const Utils::String &file, CyStringList *writeLines, int *id)
{
        bool ret = false;

        Utils::String sTo = m_fileSystem.ExtractGameFile(file, m_sTempDir + "/" + CFileIO(file).filename());
        if ( !sTo.empty() ) {
                CFileIO File(sTo);

                CyStringList *lines = File.ReadLinesStr();
                if ( lines )
                {
                        int entries = -1;
                        for ( SStringList *str = lines->Head(); str; str = str->next )
                        {
                                CyString l = str->str;
                                l.RemoveFirstSpace();
                                if ( l.Empty() ) continue;
                                if ( l.Left(1) == "/" ) continue;

                                if ( entries == -1 )
                                {
                                        entries = l.GetToken(":", 2, 2).ToInt();
                                        if ( id )
                                                (*id) = l.GetToken(":", 1, 1).ToInt();
                                }
                                else
                                        writeLines->PushBack(l);
                        }
                        delete lines;
                        ret = true;
                }
                File.close();
                File.remove();
        }

        return ret;
}


bool CModDiff::_validFile(const Utils::String &file)
{
        return CModDiff::CanBeDiffed(file);
}

bool CModDiff::CanBeDiffed(const Utils::String &file)
{
        Utils::String checkFile = file;
        checkFile = CFileIO(file).dir() + "/" + CFileIO(file).baseName();

        // all t files are the same format
        if ( checkFile.left(7).Compare("types/T") )
                return true;

        return false;
}