Subversion Repositories spk

Rev

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

//header
#include "SpkFile.h"

// debug logging
#include "Logging/Log.h"

// old style compression (needed to convert old formats)
#include "../../HiP/HiP.h"

//TODO: remove this
unsigned char *LineByLineRead ( unsigned char *data, const Utils::String &end, Utils::String *readData )
{
        Utils::String line;
        while ( true )
        {
                data = line.readToEndOfLine(data);

                if ( (*readData).empty() && line.empty() ) continue;
                if ( line == end )
                        break;
                *readData += (line + "\r\n");
        }

        return data;
}


CSpkFile::CSpkFile() : CBaseFile ()
{
        SetDefaults ();

        m_iType = TYPE_SPK;
}

CSpkFile::~CSpkFile()
{
        Delete ();
}
/*
        Func:   SetDefaults
        Desc:   Sets the  default values when class is created
*/
void CSpkFile::SetDefaults ()
{
        m_pLastWare = NULL;
        m_iPackageType = PACKAGETYPE_NORMAL;
        m_bForceProfile = false;
        m_iScriptType = SCRIPTTYPE_CUSTOM;

        CBaseFile::SetDefaults ();
}

void CSpkFile::Delete ()
{
        m_lWares.clear(true);
        m_lSettings.clear(true);

        CBaseFile::Delete ();
}


bool CSpkFile::CheckHeader(const Utils::String header) const
{
        if ( header.Compare("SPKCycrow") )
                return true;
        return false;
}
/*
        Func:   ParseValueLine
        Input:  String - single line from a file to set
        Return: Boolean - returns true if value exists
        Desc:   Reads the line and assigns the parameters for the file
*/
bool CSpkFile::ParseValueLine(const Utils::String &line)
{
        Utils::String first = line.token(" ", 1);
        Utils::String rest  = line.tokens(" ", 2);

        if ( first == "AnotherMod:" )
        {
                m_sOtherAuthor = rest.token("|", 1);
                m_sOtherName = rest.tokens("|", 2);
        }
        else if ( line == "CustomStart" )
                m_iPackageType = PACKAGETYPE_CUSTOMSTART;
        else if ( line == "PackageUpdate" )
                m_iPackageType = PACKAGETYPE_UPDATE;
        else if ( line == "Patch" )
                m_iPackageType = PACKAGETYPE_PATCH;
        else if ( line == "ForceProfile" )
                m_bForceProfile = true;
        else if ( line == "Signed" )
                m_bSigned = true;
        else if ( first == "ScriptType:" )
                m_sScriptType = rest;
        else if ( first == "ScriptTypeNew:" )
                m_iScriptType = rest;
        else if ( first == "PackageType:" )
                m_iPackageType = rest;
        else if ( first == "Ware:" )
                AddWare ( rest );
        else if ( (first == "WareText:") && (m_pLastWare) )
                AddWareText ( rest );
        else if ( first == "Setting:" )
        {
                SSettingType *t = AddSetting ( rest.token("|", 2), rest.token("|", 1) );
                ConvertSetting ( t, rest.tokens("|", 3));
        }
        else
                return CBaseFile::ParseValueLine ( line );

        return true;
}

/*
        Func:   CreateValuesLine
        Return: String - returns the full string for values
        Desc:   Creates a single string for all values, this is used when compressing to write to the spk file
*/
Utils::String CSpkFile::CreateValuesLine () const
{
        Utils::String values = CBaseFile::CreateValuesLine ();
        // combine all values together
        if ( (!m_sOtherAuthor.empty()) && (!m_sOtherName.empty()) )
                values += "AnotherMod: " + m_sOtherAuthor + "|" + m_sOtherName + "\n";
        if ( m_bForceProfile )
                values += "ForceProfile\n";
        if ( !m_sScriptType.empty() )
                values += "ScriptType: " + m_sScriptType + "\n";
        values += Utils::String("PackageType: ") + (long)m_iPackageType + "\n";
        values += Utils::String("ScriptTypeNew: ") + (long)m_iScriptType + "\n";

        for ( CListNode<SSettingType> *node = m_lSettings.Front(); node; node = node->next() )
                values += Utils::String("Setting: ") + (long)node->Data()->iType + "|" + node->Data()->sKey + "|" + GetSetting(node->Data()) + "\n";

        for ( CListNode<SWares> *wNode = m_lWares.Front(); wNode; wNode = wNode->next() ) {
                SWares *ware = wNode->Data();
                if ( wNode->Data()->iTextID > 0 )
                        values += Utils::String("Ware: ") + ware->cType + ":" + ware->iPrice + ":" + (long)ware->iSize + ":" + (long)ware->iVolumn + ":" + ware->sID + ":" + (long)ware->iNotority + ":" + (long)ware->iTextID + "," + (long)ware->iTextPage + "\n";
                else
                        values += Utils::String("Ware: ") + ware->cType + ":" + ware->iPrice + ":" + (long)ware->iSize + ":" + (long)ware->iVolumn + ":" + ware->sID + ":" + (long)ware->iNotority + "\n";
                for ( CListNode<SWaresText> *wtNode = ware->lText.Front(); wtNode; wtNode = wtNode->next() )
                        values += Utils::String("WareText: ") + (long)wtNode->Data()->iLang + " " + wtNode->Data()->sName + "|" + wtNode->Data()->sDesc + "\n";
        }

        return values;
}

Utils::String CSpkFile::GetCustomScriptType (int lang) const
{
        if ( !m_sScriptType.empty() )
        {
                int max;
                Utils::String *split = m_sScriptType.tokenise("<br>", &max);
                if ( max && split )
                {
                        for ( int i = 1; i < max; i++ )
                        {
                                Utils::String str = split[i];
                                int num = str.token(":", 1);
                                Utils::String name = str.tokens(":", 2);

                                if ( num == lang )
                                {
                                        CLEANSPLIT(split, max)
                                        return name;
                                }
                        }

                        Utils::String ret = split[0];
                        CLEANSPLIT(split, max)

                        return ret;
                }
                CLEANSPLIT(split, max)
        }
        return m_sScriptType;
}

bool CSpkFile::WriteHeader(FILE *id, int valueheader, int valueComprLen)
{
        fprintf ( id, "SPKCycrow;%.2f;%d;%d\n", FILEVERSION, valueheader, valueComprLen );
        if ( ferror(id) )
                return false;
        return true;
}

void CSpkFile::AddWareText(const Utils::String &sData)
{
        if ( !m_pLastWare )
                return;

        SWaresText *wt = new SWaresText;
        wt->iLang = sData.token(" ", 1);
        wt->sName = sData.tokens(" ", 2).token("|", 1);
        wt->sDesc = sData.tokens(" ", 2).tokens("|", 2);
        m_pLastWare->lText.push_back ( wt );

        m_bChanged = true;
}

void CSpkFile::AddWare(const Utils::String &sData)
{
        SWares *ware = new SWares;
        ware->iTextID = -1;
        ware->iTextPage = 0;
        ware->cType = sData.token(":", 1)[0];
        ware->iPrice = sData.token(":", 2);
        ware->iSize = sData.token(":", 3);
        ware->iVolumn = sData.token(":", 4);
        ware->sID = sData.token(":", 5);
        ware->iNotority = sData.token(":", 6);
        if ( !sData.token(":", 7).empty() )
        {
                Utils::String r = sData.token(":", 7);
                ware->iTextID = r.token(",", 1);
                ware->iTextPage = r.token(",", 2);
        }
        m_lWares.push_back ( ware );
        m_pLastWare = ware;

        m_bChanged = true;
}



bool CSpkFile::ReadFileToMemory ( C_File *file )
{
        if ( !file )
                return false;

        // check if data is already extracted
        if ( (file->GetDataSize ()) && (file->GetData()) )
                return true;

        // no file to read from
        if ( m_sFilename.Empty() )
                return false;

        // now open the file
        FILE *id = fopen ( m_sFilename.c_str(), "rb" );
        if ( !id )
                return false;

        // read the header
        GetEndOfLine ( id, NULL, false );
        // skip past values
        fseek ( id, m_SHeader.lValueCompressSize, SEEK_CUR );

        // read the next header
        GetEndOfLine ( id, NULL, false );
        // skip past files
        fseek ( id, 4, SEEK_CUR );
        fseek ( id, m_SHeader2.lSize, SEEK_CUR );

        // skip the icon file
        if ( m_pIconFile )
        {
                fseek ( id, 4, SEEK_CUR );
                fseek ( id, m_pIconFile->GetDataSize (), SEEK_CUR );
        }

        // now were in the file section
        // skip past each one
        for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
        {
                C_File *fit = node->Data();
                if ( fit == file )
                        break;

                fseek ( id, 4, SEEK_CUR );
                fseek ( id, fit->GetDataSize(), SEEK_CUR );
        }

        // now we should be at the start of the file
        // read the data into memory
        if ( !file->ReadFromFile ( id, file->GetDataSize() ) )
        {
                fclose ( id );
                return false;
        }
        fclose ( id );

        return true;
}

bool CSpkFile::CheckValidReadmes () const
{
        for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
        {
                C_File *file = node->Data();
                if ( file->GetFileType() != FILETYPE_README )
                        continue;
                if ( !file->CheckValidFilePointer() )
                        continue;
                return true;
        }

        return false;
}



void CSpkFile::ClearWares()
{
        for ( CListNode<SWares> *node = m_lWares.Front(); node; node = node->next() ) {
                node->Data()->lText.clear(true);
                node->DeleteData();
        }
        m_bChanged = true;
        m_lWares.clear(true);
}

SWares *CSpkFile::FindWare(const Utils::String &id) const
{
        for ( CListNode<SWares> *node = m_lWares.Front(); node; node = node->next() ) {
                SWares *w = node->Data();
                if ( w->sID.Compare(id) ) {
                        return w;
                }
        }
        return NULL;
}

void CSpkFile::RemoveWare(const Utils::String &id )
{
        for ( CListNode<SWares> *node = m_lWares.Front(); node; node = node->next() ) {
                SWares *w = node->Data();
                if ( w->sID.Compare(id) ) {
                        m_lWares.RemoveCurrent ();
                        delete w;
                        m_bChanged = true;
                        return;
                }
        }
}

void CSpkFile::AddWare(SWares *ware)
{
        ware->sID.remove(' ');

        SWares *newware = this->FindWare(ware->sID);
        if ( newware ) {
                m_lWares.remove(newware);
        }

        m_lWares.push_back(ware);
        m_bChanged = true;
}

void CSpkFile::AddWareText(SWares *pWare, int iLang, const Utils::String &sName, const Utils::String &sDesc)
{
        SWaresText *wt;
        for ( CListNode<SWaresText> *node = pWare->lText.Front(); node; node = node->next() ) {
                wt = node->Data();
                if ( wt->iLang == iLang ) {
                        wt->sDesc = sDesc;
                        wt->sName = sName;
                        return;
                }
        }

        wt = new SWaresText;
        wt->iLang = iLang;
        wt->sName = sName;
        wt->sDesc = sDesc;

        pWare->lText.push_back(wt);
        m_bChanged = true;
}


void CSpkFile::ClearWareText(const Utils::String &id)
{
        SWares *w = FindWare(id);
        ClearWareText(w);
}

void CSpkFile::ClearWareText(SWares *w)
{
        if ( !w ) return;

        w->lText.clear(true);
        m_bChanged = true;
}

void CSpkFile::RemoveWareText(const Utils::String &wid, int lang)
{
        SWares *w = FindWare(wid);
        if ( w )
        {
                for ( CListNode<SWaresText> *node = w->lText.Front(); node; node = node->next() ) {
                        SWaresText *wt = node->Data();
                        if ( wt->iLang == lang ) {
                                w->lText.RemoveCurrent();
                                m_bChanged = true;
                                delete wt;
                                break;
                        }
                }
        }
}

int CSpkFile::CheckValidCustomStart () const
{
        for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
        {
                C_File *file = node->Data();
                if ( file->GetFileType() != FILETYPE_SCRIPT )
                        continue;

                Utils::String basename = file->GetName().GetToken ( 1, file->GetName().NumToken('.') - 1, '.' ).ToString();
                if ( basename.right(15).Compare(".initplayership") )
                        return 0;
        }
        if ( !IsAnotherMod() )
                return 1;
        else
                return 2;
}

bool CSpkFile::UpdateSigned (bool updateFiles)
{
        // check for any custom wares
        // patch mods and custom starts are also not signed
        if ( (!m_lWares.empty()) || (this->IsPatch()) || (this->IsCustomStart()) )
        {
                m_bSigned = false;
                return false;
        }

        return CBaseFile::UpdateSigned(updateFiles);
}

SSettingType *CSpkFile::AddSetting(const Utils::String &key, int type )
{
        Utils::String sKey = key.removeChar('|');

        SSettingType *t;
        for ( t = m_lSettings.First(); t; t = m_lSettings.Next() )
        {
                if ( t->sKey.Compare(sKey) )
                        return NULL;
        }

        switch ( type )
        {
                case SETTING_STRING:
                        t = new SSettingString;
                        break;
                case SETTING_INTEGER:
                        t = new SSettingInteger;
                        break;
                case SETTING_CHECK:
                        t = new SSettingCheck;
                        break;
        }

        if ( !t )
                return NULL;

        t->sKey = sKey;
        t->iType = type;

        m_lSettings.push_back ( t );
        m_bChanged = true;

        return t;
}

void CSpkFile::ConvertSetting ( SSettingType *t, const Utils::String &set ) const
{
        if ( !t )
                return;

        switch ( t->iType )
        {
                case SETTING_STRING:
                        ((SSettingString *)t)->sValue = set;
                        break;
                case SETTING_INTEGER:
                        ((SSettingInteger *)t)->iValue = set;
                        break;
                case SETTING_CHECK:
                        ((SSettingCheck *)t)->bValue = set;
                        break;
        }
}
Utils::String CSpkFile::GetSetting ( SSettingType *t ) const
{
        if ( !t )
                return "";

        switch ( t->iType )
        {
                case SETTING_STRING:
                        return ((SSettingString *)t)->sValue;
                case SETTING_INTEGER:
                        return Utils::String::number(((SSettingInteger *)t)->iValue);
                case SETTING_CHECK:
                        return (((SSettingInteger *)t)->iValue) ? "1" : "0";
        }

        return "";
}

void CSpkFile::ClearSettings ()
{
        m_lSettings.clear(true);
        m_bChanged = true;
}


bool CSpkFile::IsMatchingMod(const Utils::String &mod) const
{
        for ( CListNode<C_File> *node = m_lFiles.Front(); node; node = node->next() )
        {
                C_File *file = node->Data();
                if ( file->GetFileType() != FILETYPE_MOD )
                        continue;

                if ( file->IsFakePatch() )
                        continue;

                Utils::String filename = file->GetBaseName().ToString();
                if ( filename.Compare(mod) )
                        return true;
        }
        return false;
}

Utils::String CSpkFile::GetScriptTypeString(int lang) const
{
        int iType = m_iScriptType;

        if ( this->IsLibrary() )
                return "Library";
        else if ( this->IsPackageUpdate() )
                return "Package Update";
        else if ( this->IsCustomStart() )
                return "Custom Start";
        else if ( this->IsPatch() )
                return "Patch";
        else if ( m_iScriptType == SCRIPTTYPE_CUSTOM )
        {
                Utils::String type = this->GetCustomScriptType(lang);
                if ( !type.empty() )
                        return type;
                iType = -1;
        }

        if (iType == -1)  // no script type
        {
                if ( this->IsFakePatch() )
                        return "Fake Patch";
        }

        Utils::String sType = CSpkFile::GetScriptTypeStringStatic(m_iScriptType);

        if ( sType.empty() )
                return "Other";

        return sType;   
}

int CSpkFile::ConvertScriptType(const Utils::String &sType)
{
        for ( int i = 0; i < SCRIPTTYPE_MAX; i++ )
        {
                if ( sType.Compare(CSpkFile::GetScriptTypeStringStatic(i)) )
                        return i;
        }

        return -1;
}

Utils::String CSpkFile::GetScriptTypeStringStatic(int type)
{
        switch ( type )
        {
                case SCRIPTTYPE_CUSTOM:
                        return "Custom";
                case SCRIPTTYPE_NAVIGATION:
                        return "Navigation";
                case SCRIPTTYPE_COMBAT:
                        return "Combat";
                case SCRIPTTYPE_MISSION:
                        return "Mission";
                case SCRIPTTYPE_ALPLUGIN:
                        return "AL Plugin";
                case SCRIPTTYPE_HOTKEY:
                        return "Hotkey";
                case SCRIPTTYPE_SHIPUPGRADE:
                        return "Ship Upgrade";
                case SCRIPTTYPE_SHIPCOMMAND:
                        return "Ship Command";
                case SCRIPTTYPE_STATIONCOMMAND:
                        return "Station Command";
                case SCRIPTTYPE_FLEET:
                        return "Fleet Management";
                case SCRIPTTYPE_TRADE:
                        return "Trade";
                case SCRIPTTYPE_PIRACY:
                        return "Piracy";
                case SCRIPTTYPE_CHEAT:
                        return "Cheat";
                case SCRIPTTYPE_EXTENSION:
                        return "Extension Mods";
                case SCRIPTTYPE_REBALANCE:
                        return "Rebalance";
                case SCRIPTTYPE_FIX:
                        return "Vanilla Fix";
                case SCRIPTTYPE_GENERALMOD:
                        return "General Mod";
                case SCRIPTTYPE_TOTAL:
                        return "Totel Conversion";
                case SCRIPTTYPE_WINGCOMMAND:
                        return "Wing Command";
        }

        return "Other";
}

bool CSpkFile::LoadPackageData(const Utils::String &sFirst, const Utils::String &sRest)
{
        if ( sFirst.Compare("ScriptType") )
        {
                if ( sRest.Compare("Library") || sRest.Compare("Library Script") )
                        this->SetLibrary();
                else if ( sRest.Compare("Update") || sRest.Compare("Package Update") || sRest.Compare("Mod Update") )
                        this->SetPackageUpdate();
                else if ( sRest.Compare("Start") || sRest.Compare("Custom Start") )
                        this->SetCustomStart();
                else if ( sRest.Compare("Patch") || sRest.Compare("Patch Mod") )
                        this->SetPatch();
                else
                {
                        int check = sRest;
                        if ( check || sRest == "0" )
                                m_iScriptType = check;
                        else
                        {
                                m_iScriptType = CSpkFile::ConvertScriptType(sRest);
                                if ( m_iScriptType == -1 )
                                        m_sScriptType = sRest;
                        }
                }
        }
        else if ( sFirst.Compare("AnotherMod") )
        {
                m_sOtherName = sRest.token("|", 1);
                m_sOtherAuthor = sRest.tokens("|", 2);
        }
        else if ( sFirst.Compare("WareName") || sFirst.Compare("WareDesc") )
        {
                // find the ware to use
                SWares *useWare = m_pLastWare;
                Utils::String id = sRest.token(" ", 1);
                for ( CListNode<SWares> *wNode = m_lWares.Front(); wNode; wNode = wNode->next() )
                {
                        if ( wNode->Data()->sID.Compare(id.c_str()) )
                        {
                                useWare = wNode->Data();
                                break;
                        }
                }

                // check if we have the id already
                if ( useWare )
                {
                        int lang = sRest.token(" ", 2);
                        SWaresText *wt = NULL;
                        for ( CListNode<SWaresText> *tNode = useWare->lText.Front(); tNode; tNode = tNode->next() )
                        {
                                if ( tNode->Data()->iLang == lang )
                                {
                                        wt = tNode->Data();
                                        break;
                                }
                        }

                        if ( !wt )
                        {
                                wt = new SWaresText;
                                wt->iLang = lang;
                                useWare->lText.push_back(wt);
                        }

                        if ( sFirst.Compare("WareName") )
                                wt->sName = sRest.tokens(" ", 3);
                        else
                                wt->sDesc = sRest.tokens(" ", 3);
                }
        }
        else if ( sFirst.left(4).Compare("Ware") )
        {
                SWares *ware = new SWares;
                ware->iTextID = -1;
                ware->iTextPage = 0;
                ware->cType = sFirst[4];
                ware->iPrice = sRest.token(" ", 2);
                ware->iSize = sRest.token(" ", 3);
                ware->iVolumn = sRest.token(" ", 4);
                ware->sID = sRest.token(" ", 1);
                ware->iNotority = sRest.token(" ", 5);
                if ( !sRest.token(" ", 6).empty() )
                {
                        ware->iTextID = sRest.token(" ", 6).token(",", 2);
                        ware->iTextPage = sRest.token(" ", 6).token(",", 1);
                }
                m_lWares.push_back ( ware );
                m_pLastWare = ware;
        }

        else if ( CBaseFile::LoadPackageData(sFirst, sRest) )
                return true;
        else
                return false;

        return true;
}

bool CSpkFile::GeneratePackagerScript(bool wildcard, CyStringList *list, bool datafile)
{
        if ( !CBaseFile::GeneratePackagerScript(wildcard, list, datafile) )
                return false;

        list->PushBack("# Script Type, the type of package file, some are special types, others are just for show");
        if ( this->IsLibrary() )
                list->PushBack("ScriptType: Library");
        else if ( this->IsPackageUpdate() )
                list->PushBack("ScriptType: Package Update");
        else if ( this->IsCustomStart() )
                list->PushBack("ScriptType: Custom Start");
        else if ( this->IsPatch() )
                list->PushBack("ScriptType: Patch");
        else
                list->PushBack(CyString("ScriptType: ") + this->GetScriptTypeString(44));
        list->PushBack("");

        if ( this->IsAnotherMod() )
        {
                list->PushBack("# For another mod/package, this is a child package");
                list->PushBack(CyString("AnotherMod: ") + m_sOtherName + "|" + m_sOtherAuthor);
                list->PushBack("");
        }

        if ( !m_lWares.empty() )
        {
                list->PushBack("# Custom Wares, Ware<type>: <id> <price> <size> <volumn> <notority>");
                for ( CListNode<SWares> *node = m_lWares.Front(); node; node = node->next() )
                {
                        SWares *w = node->Data();
                        if ( w->iTextID > 0 )
                                list->PushBack(CyString("Ware") + (char)w->cType + ": " + w->sID + " " + (long)w->iPrice + " " + (long)w->iSize + " " + (long)w->iVolumn + " " + (long)w->iNotority + " " + (long)w->iTextPage + "," + (long)w->iTextID);
                        else
                                list->PushBack(CyString("Ware") + (char)w->cType + ": " + w->sID + " " + (long)w->iPrice + " " + (long)w->iSize + " " + (long)w->iVolumn + " " + (long)w->iNotority);
                        for ( CListNode<SWaresText> *wNode = w->lText.Front(); wNode; wNode = wNode->next() )
                        {
                                SWaresText *wt = wNode->Data();
                                if ( !wt->sName.empty() )
                                        list->PushBack(CyString("WareName: ") + w->sID + " " + (long)wt->iLang + " " + wt->sName);
                                if ( !wt->sDesc.empty() )
                                        list->PushBack(CyString("WareDesc: ") + w->sID + " " + (long)wt->iLang + " " + wt->sDesc);
                        }
                        list->PushBack("");
                }
        }

        if ( !datafile )
        {
                if ( !CBaseFile::GeneratePackagerScriptFile(wildcard, list) )
                        return false;
        }

        return true;

}

Utils::String CSpkFile::GetWareText(SWares *w, int lang)
{
        // return the text page if being used
        if ( w->iTextID > 0 && w->iTextPage > 0 )
                return Utils::String("{") + (long)w->iTextPage + "," + (long)w->iTextID + "}";

        Utils::String name;
        for ( CListNode<SWaresText> *wt = w->lText.Front(); wt; wt = wt->next() )
        {
                if ( wt->Data()->sName.empty() )
                        continue;
                if ( wt->Data()->iLang == lang )
                        name = wt->Data()->sName;
                else if ( name.empty() && wt->Data()->iLang == 44 )
                        name = wt->Data()->sName;
                else if ( name.empty() )
                        name = wt->Data()->sName;
        }

        return name;
}
Utils::String CSpkFile::GetWareDesc(SWares *w, int lang)
{
        // return the text page if being used
        if ( w->iTextID > 0 && w->iTextPage > 0 )
                return Utils::String("{") + (long)w->iTextPage + "," + ((long)w->iTextID + 1) + "}";

        Utils::String name;
        for ( CListNode<SWaresText> *wt = w->lText.Front(); wt; wt = wt->next() )
        {
                if ( wt->Data()->sDesc.empty() )
                        continue;

                if ( wt->Data()->iLang == lang )
                        name = wt->Data()->sDesc;
                else if ( name.empty() && wt->Data()->iLang == 44 )
                        name = wt->Data()->sDesc;
                else if ( name.empty() )
                        name = wt->Data()->sDesc;
        }

        return name;
}

Utils::String CSpkFile::GetCustomStartName() const
{
        if ( !this->IsCustomStart() )
                return "";

        // else find the custom start script
        C_File *file = NULL;
        for ( file = this->GetFirstFile(FILETYPE_SCRIPT); file; file = this->GetNextFile(file) )
        {
                if ( file->GetFilename().IsIn("initplayership") && file->GetFilename().GetToken(".", 1, 1).Compare("galaxy") )
                        break;
        }

        if ( !file )
                return "";

        return file->GetFilename().GetToken(".", 2, 2).ToString();
}

void CSpkFile::MergePackage(CBaseFile *base)
{
        // update version information
        m_sVersion = base->GetVersion();
        m_sCreationDate = base->GetCreationDate();
        m_iPluginType = base->GetPluginType();

        // update possible changes
        if ( base->GetType() == TYPE_SPK )
        {
                CSpkFile *spk = (CSpkFile *)base;
                m_bForceProfile = spk->IsForceProfile();

                // add any new wares
                if ( spk->AnyWares() )
                {
                        for ( CListNode<SWares> *node = spk->GetWaresList()->Front(); node; node = node->next() )
                                this->AddWare(node->Data());
                        spk->GetWaresList()->clear(); // remove wares so they aren't deleted
                }

                // add any settings
                if ( spk->AnySettings() )
                {
                        for ( CListNode<SSettingType> *node = spk->GetSettingsList()->Front(); node; node = node->next() )
                                this->AddSetting(node->Data()->sKey, node->Data()->iType);
                }

                if ( !spk->GetOtherName().empty() )
                {
                        m_sOtherName = spk->GetOtherName();
                        m_sOtherAuthor = spk->GetOtherAuthor();
                }
        }

        // copy settings from base class
        m_iGameChanging = base->GetGameChanging();
        m_iRecommended = base->GetRecommended();
        m_iEaseOfUse = base->GetEaseOfUse();

        for ( CListNode<SGameCompat> *gNode = base->GetGameCompatabilityList()->Front(); gNode; gNode = gNode->next() ) {
                if ( !gNode->Data()->sVersion.Empty() )
                        this->AddGameCompatability(gNode->Data()->iGame, gNode->Data()->sVersion);
                else
                        this->AddGameCompatability(gNode->Data()->iGame, CyString::Number(gNode->Data()->iVersion));
        }

        if ( !base->GetWebAddress().Empty() )
                m_sWebAddress = base->GetWebAddress();
        if ( !base->GetWebSite().Empty() )
                m_sWebSite = base->GetWebSite();
        if ( !base->GetEmail().Empty() )
                m_sEmail = base->GetEmail();
        if ( !base->GetForumLink().Empty() )
                m_sForumLink = base->GetForumLink();

        m_sDescription = base->GetDescription();

        // copy over needed librarys
        for ( CListNode<SNeededLibrary> *lNode = base->GetNeededLibraries()->Front(); lNode; lNode = lNode->next() )
                this->AddNeededLibrary(lNode->Data()->sName, lNode->Data()->sAuthor, lNode->Data()->sMinVersion);

        // web mirror address, add any new ones
        for ( SStringList *str = base->GetWebMirrors()->Head(); str; str = str->next )
                this->AddWebMirror(str->str);

        // copy over any install text
        for ( CListNode<SInstallText> *iNode = base->GetInstallTextList()->Front(); iNode; iNode = iNode->next() )
        {
                if ( !iNode->Data()->sAfter.Empty() )
                        this->AddInstallAfterText(iNode->Data()->iLanguage, iNode->Data()->sAfter);
                if ( !iNode->Data()->sBefore.Empty() )
                        this->AddInstallBeforeText(iNode->Data()->iLanguage, iNode->Data()->sBefore);
        }

        for ( CListNode<SInstallText> *iNode = base->GetUninstallTextList()->Front(); iNode; iNode = iNode->next() )
        {
                if ( !iNode->Data()->sAfter.Empty() )
                        this->AddUninstallAfterText(iNode->Data()->iLanguage, iNode->Data()->sAfter);
                if ( !iNode->Data()->sBefore.Empty() )
                        this->AddUninstallBeforeText(iNode->Data()->iLanguage, iNode->Data()->sBefore);
        }

        // copy over package names
        for ( CListNode<SNames> *nNode = base->GetNamesList()->Front(); nNode; nNode = nNode->next() )
                this->AddLanguageName(nNode->Data()->iLanguage, nNode->Data()->sName);

        // finally do all the files
        for ( CListNode<C_File> *node = base->GetFileList()->Front(); node; node = node->next() )
        {
                C_File *f = node->Data();
                // if it exists, remove the old
                for ( CListNode<C_File> *thisNode = m_lFiles.Front(); thisNode; thisNode = thisNode->next() )
                {
                        if ( thisNode->Data()->GetFileType() == f->GetFileType() && thisNode->Data()->GetFilename().Compare(f->GetFilename()) && thisNode->Data()->GetDir().Compare(f->GetDir()) )
                        {
                                m_lFiles.remove(thisNode);
                                break;
                        }
                }

                m_lFiles.push_back(f);
        }

        // clear the files so we dont delete them later
        base->GetFileList()->clear();
}

unsigned char *CSpkFile::_convert_uncompressFile(const Utils::String &sOldFilename, int *pLen)
{
        // firstcheck if the file exists
        FILE *id = fopen(sOldFilename.c_str(), "rb" );
        if ( !id ) {
                CLog::logf(CLog::Log_IO, "Unable to open file: %s", sOldFilename.c_str());
                return false;
        }

        // read the first 3 charaters to check if its using the original "HiP" compression
        Utils::String check((char)fgetc(id));
        check += (char)fgetc ( id );
        check += (char)fgetc ( id );

        Utils::String removeFile;

        unsigned char *uncomprData = NULL;
        unsigned char *data = NULL;
        long len = 0, newlen = 0;

        if ( check == "HiP" ) {
                fclose ( id );
                bool opened = false;
                if ( DecompressFile ( (char *)sOldFilename.c_str(), "uncompr.tmp" ) ) {
                        removeFile = "uncompr.tmp";
                        id = fopen ( "uncompr.tmp", "r" );
                        if ( id )
                                opened = true;
                }

                if ( !opened ) {
                        CLog::log(CLog::Log_IO, "Unable to uncompress file, exiting...");
                        return false;
                }

                CLog::log(CLog::Log_IO, "* Reading file into memory...");
                // get file length
                fseek ( id, 0, SEEK_END );
                len = ftell ( id );
                
                // move back to beginning
                fseek ( id, 0, SEEK_SET );

                // read the data from file into memory
                uncomprData = new unsigned char[len + 1];
                fread ( uncomprData, sizeof(unsigned char), len, id );

                newlen = len;
        }
        else
        {
                CLog::log(CLog::Log_IO, "* Reading file into memory...");
                // get file length
                fseek ( id, 0, SEEK_END );
                len = ftell ( id );
                
                // move back to beginning
                fseek ( id, 0, SEEK_SET );

                // read the data from file into memory
                data = new unsigned char[len + 1];
                fread ( data, sizeof(unsigned char), len, id );

                // uncompress the file (currently only 7zip compression)
                CLog::log(CLog::Log_IO, "* Uncompressing file...");
                newlen = len;
        #ifdef _INCLUDE7ZIP
                uncomprData = LZMADecodeData ( data, len, newlen, progress );
        #else
                uncomprData = LZMADecode_C ( (unsigned char *)data, len, (size_t*)&newlen, NULL );
        #endif
        }

        *pLen = newlen;

        if ( !removeFile.empty() ) {
                CFileIO(removeFile).Remove();
        }

        return uncomprData;
}

Utils::String CSpkFile::_convert_fileEndString(const Utils::String &sFile)
{
        if ( sFile.Compare("Text") )
                return "-- End of Script --";
        else if ( sFile.Compare("Uninstall") )
                return "-- End of Uninstall --";
        else if ( sFile.Compare("Readme") )
                return "-- End of Readme --";
        else if ( sFile.Compare("Map") )
                return "-- End of Map --";
        else if ( sFile.Compare("Mod") || sFile.Compare("Extra") || sFile.Compare("Screen") || sFile.Compare("Sound") )
                return "";
        return "-- End of Script --";
}

int CSpkFile::_convert_fileType(const Utils::String &sFile)
{
        if ( sFile.Compare("Text") )
                return FILETYPE_TEXT;
        else if ( sFile.Compare("Uninstall") )
                return FILETYPE_UNINSTALL;
        else if ( sFile.Compare("Readme") )
                return FILETYPE_README;
        else if ( sFile.Compare("Map") )
                return FILETYPE_MAP;
        else if ( sFile.Compare("Mod") )
                return FILETYPE_MOD;
        else if ( sFile.Compare("Extra") )
                return FILETYPE_EXTRA;
        else if ( sFile.Compare("Screen") )
                return FILETYPE_SCREEN;
        else if ( sFile.Compare("Sound") )
                return FILETYPE_SOUND;

        return FILETYPE_SCRIPT;
}

void CSpkFile::_convert_parse(const Utils::String &sCmd, const Utils::String &sRest)
{
        if ( sCmd == "Name:" )
        {
                this->SetName ( sRest );
                CLog::logf(CLog::Log_EditPackage, "\tScript Name: %s", sRest.c_str() );
        }
        else if ( sCmd == "Author:" )
        {
                this->SetAuthor ( sRest );
                CLog::logf(CLog::Log_EditPackage, "\tScript Author: %s", sRest.c_str() );
        }
        else if ( sCmd == "CustomStart" )
        {
                this->SetCustomStart();
                CLog::logf(CLog::Log_EditPackage, "\tPackage is a custom start!!" );
        }
        else if ( sCmd == "AnotherMod:" )
        {
                this->SetAnotherMod(sRest.token("|", 1), sRest.tokens("|", 2));
                CLog::logf(CLog::Log_EditPackage, "\tFor another Mod, Name: %s, Author: %s", this->GetOtherName().c_str(), this->GetOtherAuthor().c_str() );
        }
        else if ( sCmd == "PATCH" )
        {
                this->SetPatch();
                CLog::logf(CLog::Log_EditPackage, "\tPackage is a Patch Mod!!" );
        }
        else if ( sCmd == "Version:" )
        {
                this->SetVersion ( sRest );
                CLog::logf(CLog::Log_EditPackage, "\tScript Version: %s", sRest.c_str() );
        }
        else if ( sCmd == "Date:" )
        {
                this->SetCreationDate ( sRest );
                CLog::logf(CLog::Log_EditPackage, "\tScript Creation Date: %s", sRest.c_str() );
        }
        else if ( sCmd == "Desc:" )
        {
                this->SetDescription(sRest.findReplace("<br>", "\n") );
                CLog::logf(CLog::Log_EditPackage, "\tScript Description: %s", this->GetDescription().c_str() );
        }
        else if ( sCmd == "WebAddress:" )
        {
                this->SetWebAddress ( sRest );
                CLog::logf(CLog::Log_EditPackage, "\tWeb Address: %s", sRest.c_str() );
        }
        else if ( sCmd == "WebMirror1:" )
        {
                this->AddWebMirror(sRest);
                CLog::logf(CLog::Log_EditPackage, "\tWeb Mirror Address: %s", sRest.c_str() );
        }
        else if ( sCmd == "WebMirror2:" )
        {
                this->AddWebMirror(sRest);
                CLog::logf(CLog::Log_EditPackage, "\tWeb Mirror Address: %s", sRest.c_str() );
        }
        
        else if ( sCmd == "ScriptType:" )
                this->SetScriptType (sRest);
        else if ( sCmd == "WebSite:" )
        {
                this->SetWebSite ( sRest );
                CLog::logf(CLog::Log_EditPackage, "\tWeb Site: %s", sRest.c_str() );
        }
        else if ( sCmd == "Email:" )
        {
                this->SetEmail ( sRest );
                CLog::logf(CLog::Log_EditPackage, "\tAuthor Email Address: %s", sRest.c_str() );
        }
        else if ( sCmd == "GameVersion:" )
        {
                //TODO: fix this for new game version
                /*
                int version = sRest.ToInt();
                if ( version == 0 )
                        this->SetGameVersion ( 1 );
                else if (version == 1 )
                        this->SetGameVersion ( 0 );
                else
                        this->SetGameVersion ( version );
                CLog::logf(CLog::Log_EditPackage, "\tGame Version: %d", this->GetGameVersion () );
                */
        }
        
        else if ( sCmd == "Ware:" )
        {
                this->AddWare ( sRest );
                CLog::logf(CLog::Log_EditPackage, "\tAdding Custom Ware" );
        }
        else if ( sCmd == "WareText:" )
                this->AddWareText ( sRest );
        else if ( sCmd == "UninstallAfter:" )
                this->AddUninstallAfterText(sRest.token(" ", 1).toLong(), sRest.tokens(" ", 2));
        else if ( sCmd == "UninstallBefore:" )
                this->AddUninstallBeforeText(sRest.token(" ", 1).toLong(), sRest.tokens(" ", 2));
        else if ( sCmd == "InstallAfter:" )
                this->AddInstallAfterText(sRest.token(" ", 1).toLong(), sRest.tokens(" ", 2));
        else if ( sCmd == "InstallBefore:" )
                this->AddInstallBeforeText(sRest.token(" ", 1).toLong(), sRest.tokens(" ", 2));
        else if ( sCmd == "ScriptName:" )
        {
                Utils::String lang = sRest.token(":", 1);
                Utils::String name = sRest.tokens(":", 2);
                this->AddLanguageName(lang.toLong(), name);
                CLog::logf(CLog::Log_EditPackage, "\tScript Name Language (%s) %s", lang.c_str(), name.c_str() );
        }
}

Utils::String CSpkFile::_convert_parseFilename(const Utils::String &sRest, float fVersion, Utils::String *pDir)
{
        Utils::String sFilename;

        if ( fVersion >= 3.00f )
                sFilename = sRest.tokens(" ", 3);
        else if ( fVersion >= 2.00f )
                sFilename = sRest.tokens(" ", 2);
        else
                sFilename = sRest;

        if ( sFilename.isin("<br>") ) {
                sFilename = sFilename.findReplace("<br>", "|");
                if ( sFilename[0] == '|' ) {
                        sFilename = sFilename.token("|", 1);
                }
                else {
                        *pDir = sFilename.token("|", 1);
                        sFilename = sFilename.tokens("|", 2);
                }
        }

        return sFilename;
}

unsigned char *CSpkFile::_convert_parseFile(const Utils::String &sCmd, const Utils::String &sRest, float fVersion, unsigned char *d)
{
        bool bShared = (sCmd.left(9) == "$$$Shared") ? true : false;
        Utils::String sFile = sCmd.right(-3).left(-1);
        Utils::String sEnd = this->_convert_fileEndString(sFile);
        int iType = this->_convert_fileType(sFile);

        // convert the filename and directory
        Utils::String dir, filename = _convert_parseFilename(sRest, fVersion, &dir);

        // get the size and time
        long time = 0, size = 0;
        if ( fVersion >= 2.00f ) time = sRest.token(" ", 1).toLong();
        if ( fVersion >= 3.00f ) size = sRest.token(" ", 2).toLong();
        bool binaryRead = (CFileIO(filename).CheckFileExtension("PCK")) ? true : false;
        if ( sEnd.empty() ) binaryRead = true;
        
        C_File *file = new C_File ();

        if ( bShared )  CLog::logf(CLog::Log_File, "\tFound %s File (Shared): %s, Reading...", sFile.c_str(), filename.c_str() );
        else                    CLog::logf(CLog::Log_File, "\tFound %s File: %s, Reading...", sFile.c_str(), filename.c_str() );

        // read the data
        if ( binaryRead )
        {
                file->ReadFromData ( (char *)d, size );
                d += size;
        }
        else
        {
                Utils::String readData;
                d = LineByLineRead ( d, sEnd, &readData );
                file->ReadFromData ( (char *)readData.c_str(), (long)readData.length() );
        }

        // setup the file
        file->SetName ( filename );
        file->SetFileType ( iType );
        file->SetShared ( bShared );
        file->SetCreationTime ( time );
        if ( !dir.empty() )
                file->SetDir ( dir );
        
        this->AddFile ( file );

        CLog::logf(CLog::Log_File, "Size: %s", file->GetDataSizeString().c_str() );

        return d;
}

CSpkFile *CSpkFile::convertFromOld(const Utils::String &sOldFilename)
{
        // check if the old file is actually in an old format
        int ret = CBaseFile::CheckFile ( sOldFilename );
        if ( ret != SPKFILE_INVALID && ret != SPKFILE_OLD ) {
                return NULL;
        }

        CSpkFile *pSpkFile = new CSpkFile();
        if ( !pSpkFile->convertOld(sOldFilename) ) {
                delete pSpkFile;
                return NULL;
        }

        return pSpkFile;
}

bool CSpkFile::convertOld(const Utils::String &sOldFilename)
{
        // check if the old file is actually in an old format
        int ret = CBaseFile::CheckFile ( sOldFilename );
        if ( ret != SPKFILE_INVALID && ret != SPKFILE_OLD ) {
                return false;
        }

        //uncomress the data
        int len;
        unsigned char *uncomprData = this->_convert_uncompressFile(sOldFilename, &len);

        // uncomressed failed
        if ( !uncomprData ) {
                CLog::log(CLog::Log_IO, "Error: Unable to uncompress the file");
                return false;
        }

        // now we can read the data
        unsigned char *d = uncomprData;
        Utils::String str;

//      CMultiSpkFile *mspk = NULL;
        //SMultiSpkFile *cur_mspk = NULL;

        int numscripts = 0, curscript = 0;
        float fVersion = 1;

        CLog::log(CLog::Log_IO, "* Reading spk data...");
        while ( d )
        {
                // read the next line
                d = str.readToEndOfLine(d);
                if ( !d || d[0] == 0 ) {
                        break;
                }
                if ( str.empty() ) {
                        continue;
                }

                Utils::String sCmd = str.token(" ", 1);

                //TODO: split this into CMultiSpkFile
                /*
                if ( first == "MultiPackage:" )
                        mspk = new CMultiSpkFile;
                else if ( (first == "SelectScript:") && (mspk) )
                {
                        mspk->AddFileEntry ( rest.GetToken ( 2, -1, ' ' ) + ".spk" );
                        ++numscripts;
                }
                else if ( (str == "AllowSelection") && (mspk) )
                        mspk->SetSelection ( true );
                else if ( str == "-- Start New Script --" )
                {
                        if ( !mspk )
                        {
                                printf ( "Invalid file format, seems to be multi package file but isn't\n" );
                                CLEANUP
                                exit ( 0 );
                        }
                        cur_mspk = mspk->GetFileList()->Get ( curscript );
                        ++curscript;
                        cur_mspk->pFile = new CSpkFile;
                        spkfile = (CSpkFile *)cur_mspk->pFile;
                }
                */
                Utils::String sRest = str.tokens(" ", 2);
                if ( sCmd == "Packager:" ) {
                        fVersion = sRest;
                        CLog::logf(CLog::Log_Read, "\tPackager Version: %.2f", fVersion );
                }
                else if ( sCmd == "Icon:" )
                {
                        long size = sRest.token(" ", 1);
                        Utils::String ext = sRest.token(" ", 2);
                        
                        C_File *file = new C_File ();
                        file->ReadFromData ( (char *)d, size );

                        d += size;

                        this->SetIcon(file, ext);

                        CLog::logf(CLog::Log_File, "\tIcon (%s) Size: %s", ext.c_str(), file->GetDataSizeString ().c_str() );
                }
                else if ( sCmd.left(3) == "$$$" )
                        d = _convert_parseFile(sCmd, sRest, fVersion, d);
                else {
                        this->_convert_parse(sCmd, sRest);
                }
        }

        CLog::logf(CLog::Log_IO, "* Reading spk data..." );

        return true;
}