Subversion Repositories spk

Rev

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

/*
 SPKTool V1.00 Created by Cycrow (Matthew Gravestock)
*/

// Main Spk File Library Include
#ifdef _WIN32
#include <spk.h>
#include <MultiSpkFile.h>
#include <spkcmdprogress.h>
#else
#include "../spk/spk.h"
#include "../spk/MultiSpkFile.h"
#include "../spk/spkcmdprogress.h"
#endif
// Multi Spk File format, required if using Multi Spk files
// Displays 7Zip compression progress to the command line
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
#include <shlobj.h>
#endif

CyString g_dir;
bool g_read;

/*
        Func:   GetInput
        Desc:   Gets an input from the user, ie, any settings required to be typed in
*/
CyString GetInput ()
{
        g_read = true;

        CyString line;
        char c = getchar();

        while ( (c != '\n') && (c != '\0') )
        {
                line += c;
                c = getchar();
        }

        return line;
}


/*
        Func:   PrintSyntax
        Args:   String cmd - The command name to be displayed
        Desc:   Displays the syntax for the program
*/
void PrintSyntax ( CyString cmd )
{
        printf ( "Syntax: %s <command>\n", cmd.c_str() );
        printf ( "Commands:\n" );
        printf ( "\t-v <spkfile>\n\t--version <spkfile>\n\t\tViews the contents of a spk file\n\n" );
        printf ( "\t-e <spkfile> [destination]\n\t--extractall <spkfile> [destination]\n\t\tExtracts all the files to the destination directory [or current if no destiantion is set]\n\n" );
        printf ( "\t-x <spkfile> <type> <file> [destination]\n\t--extract <spkfile> <type> <file> [destination]\n\t\tExtracts a single file of <type> to destination directory\n\n" );
        printf ( "\t-x <multispkfile> <file> [destination]\n\t--extractspk <multispkfile> <spkfile> [destination]\n\t\tExtracts a single spk file from a Multi-Spk Package\n\n" );
        printf ( "\t-c <spkfile>\n\t--create\n\t\tCreates a new spk file\n\n" );
        printf ( "\t-a <spkfile> <type> <filename>\n\t--append <spkfile> <type> <filename>\n\t\tAppends a file to the package of set <type>\n\n" );
        printf ( "\t-r <spkfile> <type> <filename>\n\t--remove <spkfile> <type> <filename>\n\t\tRemoves a file from the package of set <type>\n\n" );
        printf ( "\t-r <multispkfile> <filename>\n\t--removespk <multispkfile> <filename>\n\t\tRemoves a spk file from the Multi-SPK package\n\n" );
        printf ( "\t-m <spkfile> <spkfile>\n\t--mergemulti <spkfile> <spkfile>\n\t\tMerges spk files together, the second file will be merged into the first\n\n" );
        printf ( "\t-n <multispkfile>\n\t--createmulti\n\t\tCreates a multi spk file, and adds the spkfiles in\n\n" );
        printf ( "\t-s <multispkfile> [destination]\n\t--splitmulti <multispkfile> [destination]\n\t\tSplits a Multi-SPK file up, saves all the spk files to Destination\n\n" );
        printf ( "\t--set <setting> [value]\n\t\tChanges the settings of the script\n\n" );
        printf ( "\t--convertxsp <oldxsp> [newxsp]\n\t\tConverts an old XSP file into the new format\n\n");
        printf ( "\t--createscript <packagescript>\n\t\tCreates a spk file from a packager script\n\n");
        printf ( "\t--verifyscript <packagescript>\n\t\tChecks a packager script is valid without creating the resulting file\n\n");
        printf ( "\t--generatescript <package> [packagescript]\n\t\tCreates a packager script (.sps) from a spk file\n\n");
        printf ( "\t--extractship <modfile> <xspfile> [shipid]\n\t\tCreates an XSP ship file from a mod package\n\n");
        printf ( "\t--generateupdatefile <package>\n\t\tCreates an update file for the spk/xsp file\n\n");
//      printf ( "\t--convertxspwizard <oldxsp> [newxsp]\n\t\tConverts an old XSP file into the new format\n\n");
}

void Settings(CyString filename, CyString settings, int argc, char **argv, CyString cmd)
{
        if ( settings.Compare("author") )
        {
                if ( argc < 5 )
                {
                        printf ( "Syntax: %s -set <spkfile> Author <authorname>\n\tSets the authors name of the package\n", cmd.c_str() );
                        return;
                }
        }
        else
        {
                printf("Error! Unknown settings: %s\n", settings.c_str());
                return;
        }

        // first chekc if file even exists
        FILE *id = fopen ( filename.c_str(), "rb+" );
        if ( !id )
        {
                printf ( "Error: File, %s, doesn't exist\n", filename.c_str() );
                return;
        }
        fclose ( id );

        int check = CSpkFile::CheckFile ( filename );
        if ( check == SPKFILE_SINGLE )
        {
                CSpkFile spkfile;
                printf ( "* Opening SPK File, %s...\n", filename.c_str() );
                if ( !spkfile.ReadFile ( filename, SPKREAD_ALL ) )
                {
                        printf ( "Failed to open the spk files, %s\n", filename.c_str() );
                        return;
                }

                // now do the settings
                if ( settings.Compare("author") )
                {
                        spkfile.SetAuthor(argv[4]);
                        printf( "Settings Author to: %s\n", spkfile.GetAuthor().c_str());
                }

                spkfile.WriteFile ( filename );
                printf ( "\nSPK file has been written sucessfully\n" );
        }
        else
                printf ( "File, %s, is not a valid SPK file\n", filename.c_str() );
}

void SetRating ( CyString filename, int ease, int changing, int rec )
{
        if ( ease > 5 )
                ease = 5;
        if ( changing > 5 )
                changing = 5;
        if ( changing > 5 )
                changing = 5;

        FILE *id = fopen ( filename.c_str(), "rb+" );
        if ( !id )
        {
                printf ( "Error: File, %s, doesn't exist\n", filename.c_str() );
                return;
        }
        fclose ( id );

        int check = CSpkFile::CheckFile ( filename );
        if ( check == SPKFILE_SINGLE )
        {
                CSpkFile spkfile;
                printf ( "* Opening SPK File, %s...\n", filename.c_str() );
                if ( !spkfile.ReadFile ( filename, SPKREAD_ALL ) )
                {
                        printf ( "Failed to open the spk files, %s\n", filename.c_str() );
                        return;
                }

                printf ( "File Format Version: %.2f\n", spkfile.GetFileVersion() );

                printf ( "Assigning Ease Of Use:\t\t%d\nAssigning Game Changing:\t%d\nAssigning Recommended:\t\t%d\n", ease, changing, rec);
                spkfile.SetEaseOfUse(ease);
                spkfile.SetGameChanging(changing);
                spkfile.SetRecommended(rec);
                spkfile.WriteFile ( filename );
                printf ( "\nSPK file has been written sucessfully\n" );

        }
        else
                printf ( "File, %s, is not a valid SPK file\n", filename.c_str() );
}

CyString GetTerranConflictDir()
{
        while ( true )
        {
                printf ( "\nPlease enter the directory for terran conflict: ");
                CyString dir = GetInput();
                if ( dir.Empty() )
                        return NullString;

                // check if the directory exists
                if ( CFileIO(dir + "/x3tc.exe").Exists() )
                        return dir;
                printf("\nTerran Conflict installation doesn't exist: %s\n\n", dir.c_str());
        }

        return NullString;
}

void ConvertXsp(CyString filename, CyString tofile, bool wizard)
{
        // first chekc if file even exists
        FILE *id = fopen ( filename.c_str(), "rb+" );
        if ( !id )
        {
                printf ( "Error: File, %s, doesn't exist\n", filename.c_str() );
                return;
        }
        fclose ( id );

        // check its not a new format already
        int check = CSpkFile::CheckFile ( filename );
        if ( check == SPKFILE_INVALID && CFileIO(filename).CheckFileExtension("xsp") )
        {
                printf ( "* Converting XSP File, %s... ", filename.c_str() );
                CXspFile xspFile;
                if ( !xspFile.ConvertOld(filename) )
                {
                        printf ( "[ERROR]\n\nFailed to convert old xsp file, %s\n", filename.c_str() );
                        return;
                }

                printf ( "[DONE]\n");

                CyString dir;
                printf ("Do you want to create weapon masks for Terran conflict? ");
                CyString input = GetInput();
                if ( input.Compare("y") || input.Compare("yes") )
                {
                        dir = GetTerranConflictDir();
                        if ( !dir.Empty() )
                        {
                        }
                }

                printf ( "* Writing XSP File, %s... ", tofile.c_str() );
                // now we need to save it
                if ( !xspFile.WriteFile(tofile) )
                        printf ( "[ERROR]\n\nFailed to write file to, %s\n", tofile.c_str() );
                else
                        printf ( "[DONE]\n\nFile: %s, has been converted to, %s\n", filename.c_str(), tofile.c_str() );
        }
        else
                printf ( "Error: File, %s, is not an old style XSP file\n", filename.c_str() );
}

void ExtractShip(CyString catfile, CyString to, CyString shipid)
{
        if ( !CFileIO(catfile).Exists() )
        {
                printf ( "Error: Mod File, %s, does not exist\n", catfile.c_str() );
                return;
        }

        CCatFile cat;
        if ( cat.Open(catfile, "", CATREAD_CATDECRYPT, false) != CATERR_NONE )
        {
                printf("Error: Unable to read mod file, %s\n", catfile.c_str());
                return;
        }

        CyStringList *ships = cat.GetTShipsEntries();
        if ( !ships )
        {
                printf("Error: Failed to read %s::types\\TShips.pck\n", catfile.c_str());
                return;
        }

        // now get the ship line to extract
        SStringList *tshipsLine = NULL;
        if ( shipid.Empty() )
        {
                // no ship id, lets get one
                CyString ids[2];
                int i = 0;
                int id = 0;
                for ( SStringList *node = ships->Head(); node; node = node->next )
                {
                        ids[i++] = node->data;
                        if ( i >= 2 )
                        {
                                printf("[%3d][%30s]\t[%3d][%30s]\n", id - 1, ids[0].c_str(), id, ids[1].c_str());
                                for ( i = 0; i < 2; i++ )
                                        ids[i] = "";
                                i = 0;
                        }
                        ++id;
                }
                if ( i == 1 )
                        printf("[%3d][%30s]\n", id, ids[0].c_str());
                printf("Enter ID to use > ");
                shipid = GetInput();

                if ( shipid.IsNumber() )
                        tshipsLine = ships->GetAt(shipid.ToInt());
                else
                        tshipsLine = ships->FindData(shipid.upper());
        }
        else
                ships->FindData(shipid.upper());

        if ( !tshipsLine )
        {
                printf("Error, %s is not a valid ship id to use\n", shipid.c_str());
                return;
        }

        // now we have the tships to use, lets start to extract the ship
        printf("Extracting ship from %s, Ship=%s\n", catfile.c_str(), tshipsLine->data.c_str());

        CXspFile ship;
        if ( ship.ExtractShip(&cat, tshipsLine->data, 0) )
        {
                printf ( "Ship has been successfully extracted\n" );

                printf("\t- Models:\t %d\n", ship.CountFiles(FILETYPE_SHIPMODEL));
                printf("\t- Textures:\t %d\n", ship.CountFiles(FILETYPE_SHIPOTHER));
                printf("\t- Dummies:\t %d\n", ship.GetDummies()->size());
                printf("\t- Components:\t %d\n", ship.GetComponents()->size());
                printf("\t- Bodies:\t %d\n", ship.GetBodies()->Count());
                printf("\t- Cockpits:\t %d\n", ship.GetCockpits()->size());
                printf("\t- Text Entries:\t %d\n", ship.GetTexts()->size());

                printf("\nEnter the name for the ship: ");
                ship.SetName(GetInput());
                printf("Enter the author for the ship: ");
                ship.SetAuthor(GetInput());
                printf("Enter the version for the ship: V");
                ship.SetVersion(GetInput());
                printf("Enter the description for the ship: ");
                ship.SetDescription(GetInput());

                struct tm   *currDate;
                char    dateString[100];
                time_t now = time(NULL);
                currDate = localtime( &now );
                strftime(dateString, sizeof dateString, "%d/%m/%Y", currDate);
                ship.SetCreationDate ( CyString(dateString) );

                printf("\nWriting file: %s...\n", to.c_str());
                if ( ship.WriteFile(to) )
                        printf("Ship file has been created: %s\n", to.c_str());
                else
                        printf("Error: Unable to write ship file, %s\n", to.c_str());
        }
        else
                printf("Error: Unable to extract the ship\n");
}

CyString GetGameName(int game)
{
        switch(game)
        {
                case GAME_X2:
                        return "X2: The Threat";
                case GAME_X3:
                        return "X3: Reunion";
                case GAME_X3TC:
                        return "X3: Terran Conflict";
                case GAME_X3AP:
                        return "X3: Albion Prelude";
                case GAME_XREBIRTH:
                        return "X: Rebirth";
        }
        return "Unknown";
}
void DisplayVersion ( CyString filename )
{
        // first chekc if file even exists
        FILE *id = fopen ( filename.c_str(), "rb+" );
        if ( !id )
        {
                printf ( "Error: File, %s, doesn't exist\n", filename.c_str() );
                return;
        }
        fclose ( id );

        bool read = false;
        CBaseFile *pBaseFile = NULL;

        int check = CSpkFile::CheckFile ( filename );
        if ( check == SPKFILE_BASE )
        {
                pBaseFile = new CBaseFile();
                printf ( "* Opening SPK File, %s...\n", filename.c_str() );
                if ( !pBaseFile->ReadFile ( filename, SPKREAD_NODATA ) )
                {
                        printf ( "Failed to open the spk files, %s\n", filename.c_str() );
                        return;
                }
                read = true;
        }
        else if ( check == SPKFILE_SINGLE )
        {
                pBaseFile = (CBaseFile *)new CSpkFile();
                printf ( "* Opening SPK File, %s...\n", filename.c_str() );
                if ( !pBaseFile->ReadFile ( filename, SPKREAD_NODATA ) )
                {
                        printf ( "Failed to open the spk files, %s\n", filename.c_str() );
                        return;
                }
                read = true;
        }
        else if ( check == SPKFILE_SINGLESHIP )
        {
                pBaseFile = (CBaseFile *)new CXspFile();
                printf ( "* Opening XSP File, %s...\n", filename.c_str() );
                if ( !pBaseFile->ReadFile ( filename, SPKREAD_NODATA ) )
                {
                        printf ( "Failed to open the xsp files, %s\n", filename.c_str() );
                        return;
                }
                read = true;
        }
        // otherwise its an old ship file
        else if ( CFileIO(filename).CheckFileExtension("xsp") )
        {
                printf ( "* Converting XSP File, %s...\n", filename.c_str() );
                pBaseFile = new CXspFile;
                if ( !((CXspFile *)pBaseFile)->ConvertOld(filename) )
                {
                        delete pBaseFile;
                        pBaseFile = NULL;
                        printf ( "Failed to convert old xsp file, %s\n", filename.c_str() );
                        return;
                }
                check = SPKFILE_SINGLESHIP;
                read = true;
        }

        if ( pBaseFile && read)
        {
                CPackages p;
                p.Startup(".", ".", ".");

                CSpkFile *pSpkFile = NULL;
                CXspFile *pXspFile = NULL;
                if ( check == SPKFILE_SINGLE )
                        pSpkFile = (CSpkFile *)pBaseFile;
                else
                        pXspFile = (CXspFile *)pBaseFile;

                printf ( "File Format Version: %.2f\n", pBaseFile->GetFileVersion() );

                CyString sType;
                if ( check == SPKFILE_SINGLESHIP )
                        sType = "Ship";
                else
                        sType = "Package";

                printf ( "%s Name: %s\n", sType.c_str(), pBaseFile->GetName ().c_str() );
                if ( !pBaseFile->GetEmail().Empty() )
                        printf ( "%s Author: %s (%s)\n", sType.c_str(), pBaseFile->GetAuthor().c_str(), pBaseFile->GetEmail().c_str() );
                else
                        printf ( "%s Author: %s\n", sType.c_str(), pBaseFile->GetAuthor().c_str() );
                if ( !pBaseFile->GetVersion().Empty() ) printf ( "%s Version: %s\n", sType.c_str(), pBaseFile->GetVersion().c_str() );

                //for game
                printf("For Games: ");
                if ( !pBaseFile->AnyGameCompatability() )
                        printf("All");
                else
                        printf("%s", p.GetGameTypesString(pBaseFile, true).c_str());
                printf("\n");

                if ( pXspFile )
                {
                        printf ("Ship Display Name: %s\n", pXspFile->GetShipName(44).c_str());
                        if ( pXspFile->IsExistingShip() )
                                printf ( "Ship ID: %s (Replace Existing Ship)\n", pXspFile->GetShipID().c_str() );
                        else
                                printf ( "Ship ID: %s\n", pXspFile->GetShipID().c_str() );

                        if ( pXspFile->AnyShipyards() )
                        {
                                printf ( "Add To Shipyards:\n" );
                                for ( int i = 1; i <= GetMaxShipyards(); i *= 2 )
                                {
                                        if ( pXspFile->IsShipyard(i) )
                                                printf("\t%s\n", GetShipyardName(i).c_str());
                                }
                        }

                        for ( SWeaponMask *text = pXspFile->GetLaserMasks()->First(); text; text = pXspFile->GetLaserMasks()->Next() )
                                printf ( "Laser Mask, Game: %s, Mask: %d\n", GetGameName(text->iGame).c_str(), text->iMask);
                        for ( SWeaponMask *text = pXspFile->GetMissileMasks()->First(); text; text = pXspFile->GetMissileMasks()->Next() )
                                printf ( "Missile Mask, Game: %s, Mask: %d\n", GetGameName(text->iGame).c_str(), text->iMask);

                        if ( pXspFile->GetOriginalDescription() )
                                printf("Use Existing Text, ID: %d\n", pXspFile->GetOriginalDescription());

                        if ( pXspFile->AnyTexts() )
                        {
                                for ( SText *text = pXspFile->GetTexts()->First(); text; text = pXspFile->GetTexts()->Next() )
                                {
                                        printf("Ship Text, Language: %d\n", text->iId);
                                        if ( !text->sName.Empty() )
                                                printf("\tName: %s\n", text->sName.c_str());
                                        if ( !text->sDesc.Empty() )
                                                printf("\tDescription: %s\n", text->sDesc.c_str());
                                }
                        }

                        if ( pXspFile->AnyComponents() )
                        {
                                printf ( "Component Entries:\n" );
                                for ( SComponent *c = pXspFile->GetComponents()->First(); c; c = pXspFile->GetComponents()->Next() )
                                        printf ( "\t%s\n\t\t%s\n\t\t\t%s\n", c->sSection.c_str(), c->sSection2.c_str(), c->sData.c_str());
                        }
                        if ( pXspFile->AnyDummies() )
                        {
                                printf ( "Dummy Entries:\n" );
                                for ( SDummy *d = pXspFile->GetDummies()->First(); d; d = pXspFile->GetDummies()->Next() )
                                        printf ( "\t%s\n\t\t%s\n", d->sSection.c_str(), d->sData.c_str());
                        }
                        if ( pXspFile->AnyCockpits() )
                        {
                                printf ( "Cockpit Entries:\n" );
                                for ( SCockpit *c = pXspFile->GetCockpits()->First(); c; c = pXspFile->GetCockpits()->Next() )
                                        printf("\t%s\n", c->sCockpit.c_str());
                        }
                        if ( pXspFile->AnyCutData() )
                        {
                                printf ( "CutData Entries [%d]:\n", pXspFile->GetCutData()->Count() );
                                for ( SStringList *c = pXspFile->GetCutData()->Head(); c; c = c->next )
                                        printf("\t%s\n", c->str.c_str());
                        }
                }

                if ( !pBaseFile->GetCreationDate().Empty() ) printf ( "Creation Date: %s\n", pBaseFile->GetCreationDate().c_str() );
                if ( !pBaseFile->GetDescription().Empty() ) printf ( "Description: %s\n", pBaseFile->GetDescription().c_str() );
                if ( pSpkFile )
                {
                        if ( pSpkFile->IsLibrary() )
                                printf ( "Script Type: Library\n" );
                        else if ( pSpkFile->IsPatch() )
                                printf ( "Script Type: Patch Mod\n" );
                        else if ( pSpkFile->IsCustomStart() )
                                printf ( "Script Type: Custom Start\n" );
                        else if ( pSpkFile->IsPackageUpdate() )
                                printf ( "Script Type: Package Update\n" );
                        else if ( !pSpkFile->GetScriptTypeString(44).Empty() )
                                printf ( "Script Type: %s\n", pSpkFile->GetScriptTypeString(44).c_str() );

                        if ( !pSpkFile->GetWaresList()->empty() )
                        {
                                for ( CListNode<SWares> * wNode = pSpkFile->GetWaresList()->Front(); wNode; wNode = wNode->next() )
                                {
                                        SWares *w = wNode->Data();
                                        printf ( "Ware: (%c) %s\n", w->cType, CSpkFile::GetWareText(w, 44).c_str() );
                                        CyString desc = CSpkFile::GetWareDesc(w, 44);
                                        if ( !desc.Empty() )
                                                printf ( "\tDescription: %s\n", desc.c_str() );
                                }
                        }
                }
                if ( !pBaseFile->GetForumLink().Empty() ) printf ( "Forum Link: %s\n", pBaseFile->GetForumLink().c_str() );
                if ( !pBaseFile->GetWebSite().Empty() ) printf ( "Web Site Address: %s\n", pBaseFile->GetWebSite().c_str() );
                if ( !pBaseFile->GetWebAddress().Empty() ) printf ( "Update Address: %s\n", pBaseFile->GetWebAddress().c_str() );

                if ( pBaseFile->AnyWebMirrors() )
                {
                        printf("Update Mirror Addresses:\n");
                        for ( int i = 0; i < pBaseFile->GetMaxWebMirrors(); i++ )
                                printf ("\t%s\n", pBaseFile->GetWebMirror(i).c_str());
                }
                if ( pSpkFile )
                {
                        if ( (!pSpkFile->GetOtherName().Empty()) && (!pSpkFile->GetOtherAuthor().Empty()) )
                                printf ( "Script is a child to the mod: %s by %s\n", pSpkFile->GetOtherName().c_str(), pSpkFile->GetOtherAuthor().c_str() );
                }

                if ( pBaseFile->AnyDependacies() )
                {
                        printf ( "Required Dependacies:\n" );
                        for ( SNeededLibrary *needed = pBaseFile->GetNeededLibraries()->First(); needed; needed = pBaseFile->GetNeededLibraries()->Next() )
                                printf ( "\t%s by %s (Minimum Version=%s)\n", needed->sName.c_str(), needed->sAuthor.c_str(), needed->sMinVersion.c_str() );
                }
                if ( pBaseFile->GetEaseOfUse() != -1 ) printf ( "Ease Of Use Rating: %d\n", pBaseFile->GetEaseOfUse() );
                if ( pBaseFile->GetGameChanging() != -1 ) printf ( "Game Changing Rating: %d\n", pBaseFile->GetGameChanging() );
                if ( pBaseFile->GetRecommended() != -1 ) printf ( "Recommendation Rating: %d\n", pBaseFile->GetRecommended() );
                if ( pBaseFile->GetIcon() )
                {
                        C_File *icon = pBaseFile->GetIcon();
                        printf ( "Icon File Found, Type: %s, Size: %s\n", pBaseFile->GetIconExt().c_str(), icon->GetDataSizeString().c_str() );
                }

                if ( pBaseFile->GetFileList()->size() )
                {
                        printf ( "\nListing files in package:\n" );
                        if ( pBaseFile->GetIcon() )
                                printf("\tIcon: %s (Size: %s)\n", pBaseFile->GetIconExt().c_str(), pBaseFile->GetIcon()->GetDataSizeString().c_str());
                        CLinkList<C_File> *list = pBaseFile->GetFileList();
                        for ( C_File *file = list->First(); file; file = list->Next() )
                                printf ( "\t%s (%s) Size: %s\n", file->GetNameDirectory(pBaseFile).c_str(), file->GetFileTypeString().c_str(), file->GetDataSizeString().c_str() );
                }
                else
                        printf ( "\nThere are currently no files in the package\n" );
        }
        else if ( check == SPKFILE_MULTI )
        {
                CMultiSpkFile spkfile;
                printf ( "* Opening Multi-SPK file, %s...\n", filename.c_str() );
                if ( !pBaseFile->ReadFile ( filename, false ) )
                {
                        printf ( "Error: Failed to open the Multi-SPK file, %s\n", filename.c_str() );
                        return;
                }

                printf ( "Multi Package Name: %s\n", spkfile.GetName().c_str() );
                printf ( "Selection Mode: " );
                if ( spkfile.IsSelection () )
                        printf ( "On\n" );
                else
                        printf ( "Off\n" );

                CLinkList<SMultiSpkFile> *list = spkfile.GetFileList();
                for ( SMultiSpkFile *ms = list->First(); ms; ms = list->Next() )
                {
                        printf ( "File, %s:\n", ms->sName.c_str() );
                        printf ( "\tSize: %s\n", SPK::GetSizeString (ms->lSize).c_str() );
                        if ( (!ms->sScriptName.Empty()) && (!ms->sScriptAuthor.Empty()) )
                                printf ( "\tScript: %s %s by %s\n", ms->sScriptName.c_str(), ms->sScriptVersion.c_str(), ms->sScriptAuthor.c_str() );
                        printf ( "\tDefault Install: " );
                        if ( ms->bOn )
                                printf ( "Yes\n" );
                        else
                                printf ( "No\n" );
                }
        }
        else
                printf ( "File, %s, is not a valid SPK file\n", filename.c_str() );

        if ( pBaseFile )
                delete pBaseFile;
}

void AppendFile ( CyString sfile, CyString type, CyString addfile )
{
        int t = GetFileTypeFromString(type);
        if ( t == -1 )
        {
                printf ( "The file type \"%s\" is invalid\n", type.c_str() );
                return;
        }

        int check = CSpkFile::CheckFile ( sfile );
        CBaseFile *pBaseFile = 0;
        if ( check == SPKFILE_SINGLE )
                pBaseFile = new CSpkFile();
        else if ( check == SPKFILE_SINGLESHIP )
                pBaseFile = new CXspFile();
        else if ( check == SPKFILE_BASE )
                pBaseFile = new CBaseFile();

        if ( !pBaseFile )
        {
                printf ( "Error: Invalid file format, unable to open\n" );
                return;
        }

        printf ( "Opening File, %s... ", sfile.c_str() );
        if ( !pBaseFile->ReadFile ( sfile ) )
        {
                printf ( "(Error)\nUnable to open the file, %s\n", sfile.c_str() );
                return;
        }
        printf ( "(Done)\n" );

        MyProgress progress(0);

        printf ( "Adding file %s to package\n\t>", addfile.c_str() );

        if ( pBaseFile->AppendFile ( addfile, t, 0, NullString, &progress ) )
        {
                progress.PrintDone();
                printf ( "< (Done)\n" );

                pBaseFile->WriteFile ( sfile );
                printf ( "\nFile has been written sucessfully\n" );
        }
        else
                printf ( "< (Error)\n" );
}

void RemoveFile ( CyString sfile, CyString type, CyString addfile )
{
        FILE *id = fopen ( sfile.c_str(), "rb+" );
        if ( !id )
        {
                printf ( "Error: File, %s, doesn't exist\n", sfile.c_str() );
                return;
        }

        if ( addfile.Empty() )
        {
                printf ( "Error: Remove filename is invalid\n" );
                return;
        }
        int t = GetFileTypeFromString(type);
        if ( t == -1 )
        {
                printf ( "The file type \"%s\" is invalid\n", type.c_str() );
                return;
        }

        int check = CSpkFile::CheckFile ( sfile );
        CBaseFile *pBaseFile = 0;
        if ( check == SPKFILE_SINGLE )
                pBaseFile = new CSpkFile();
        else if ( check == SPKFILE_SINGLESHIP )
                pBaseFile = new CXspFile();
        else if ( check == SPKFILE_BASE )
                pBaseFile = new CBaseFile();

        if ( pBaseFile )
        {
                printf ( "Opening File, %s... ", sfile.c_str() );
                if ( !pBaseFile->ReadFile ( sfile ) )
                {
                        printf ( "(Error)\nUnable to open the file, %s\n", sfile.c_str() );
                        return;
                }
                printf ( "(Done)\n" );

                printf ( "Removing file, %s, from Package\n", addfile.c_str() );

                if ( pBaseFile->RemoveFile ( addfile, t ) )
                {
                        printf ( "File, %s, has been remove from package\n", addfile.c_str() );
                        pBaseFile->WriteFile ( sfile );
                        printf ( "File has been written to disk successfully\n" );
                }
                else
                        printf ( "Unable to remove the file, %s, from the package\n", addfile.c_str() );
        }
        else if ( check == SPKFILE_MULTI )
        {
                CMultiSpkFile spkfile;
                printf ( "Opening Multi-SPK file, %s...", sfile.c_str() );
                if ( !spkfile.ReadFile ( sfile ) )
                {
                        printf ( "(Error)\nUnable to open the Multi-SPK file, %s\n", sfile.c_str() );
                        return;
                }
                printf ( "(Done)\n" );

                SMultiSpkFile *ms = spkfile.FindFile ( type );
                if ( !ms )
                {
                        printf ( "Unable to find the file \"%s\" in the package\n", type.c_str() );
                        return;
                }

                printf ( "Removing file, %s, from Package\n", addfile.c_str() );
                if ( !spkfile.RemoveFile ( ms ) )
                {
                        printf ( "Error: Unable to remove file, %s, from package\n", type.c_str() );
                        return;
                }

                printf ( "Writing SPK File, %s... ", sfile.c_str() );
                if ( spkfile.WriteFile ( sfile ) )
                        printf ( "(Done)\n" );
                else
                        printf ( "(Error)\n" );
        }
        else
                printf ( "Error: Invalid file format, unable to open\n" );
}


void CreateMultiFile ( CyString filename )
{
        printf ( "* Creating new Multi-SPK File, %s\n\n", filename.c_str() );

        FILE *id = fopen ( filename.c_str(), "rb+" );
        if ( id )
        {
                fclose ( id );
                printf ( "* File already exists, unable to create\n" );
                return;
        }

        id = fopen ( filename.c_str(), "wb" );
        if ( !id )
        {
                printf ( "* Unable to open file for writing\n" );
                return;
        }
        fclose ( id );
        remove ( filename.c_str() );

        CMultiSpkFile spkfile;

        CyString sInput;
        printf ( "Enter Multi-Spk Package Name: " );
        spkfile.SetName ( GetInput() );

        while ( true )
        {
                printf ( "\nDo you want users to select scripts to install? (Y/N): " );
                CyString i = GetInput();
                i = i.ToUpper();
                if ( i == "Y" )
                        spkfile.SetSelection ( true );
                if ( (i == "Y") || (i == "N") )
                        break;
        }

        while ( true )
        {
                printf ( "\nEnter Spk File to add (Enter \"0\" to finish): " );
                sInput = GetInput();
                if ( sInput == "0" )
                        break;

                // check if file can be opened
                FILE *id2 = fopen ( sInput.c_str(), "rb+" );
                if ( !id2 )
                        printf ( "Error: Unable to open SPK file %s\n", sInput.c_str() );
                else
                {
                        fclose ( id2 );
                        if ( !spkfile.AddFile ( sInput ) )
                                printf ( "Error: Unable to add SPK file to package\n" );
                        else
                                printf ( "File Added to package (%s)\n", sInput.c_str() );
                }
        }

        if ( spkfile.GetNumFiles() < 1 )
                printf ( "\nError: You have added no files, you must add at least one file to create a package\n" );
        else
        {
                printf ( "Writing MultiSpk file... " );
                if ( spkfile.WriteFile ( filename ) )
                        printf ( "(Done)\n" );
                else
                        printf ( "(Error)\n" );
        }

}

void CreateFile ( CyString filename )
{
        printf ( "* Creating new SPK File, %s\n\n", filename.c_str() );

        FILE *id = fopen ( filename.c_str(), "rb+" );
        if ( id )
        {
                fclose ( id );
                printf ( "* File already exists, unable to create\n" );
                return;
        }

        id = fopen ( filename.c_str(), "wb" );
        if ( !id )
        {
                printf ( "* Unable to open file for writing\n" );
                return;
        }
        fclose ( id );
        remove ( filename.c_str() );

        CSpkFile spkfile;

        printf ( "Enter Script Name: " );
        spkfile.SetName ( GetInput() );

        printf ( "Enter Script Author: " );
        spkfile.SetAuthor ( GetInput() );

        printf ( "Enter Script Version: " );
        spkfile.SetVersion ( GetInput() );

        printf ( "Enter Script Description: " );
        spkfile.SetDescription ( GetInput() );

        struct tm   *currDate;
        char    dateString[100];
        time_t now = time(NULL);

        currDate = localtime( &now );
        strftime(dateString, sizeof dateString, "%d %m %Y", currDate);
        spkfile.SetCreationDate ( CyString(dateString) );

        spkfile.WriteFile ( filename, NULL );
        printf ( "SPK file has been written to disk: %s\n", filename.c_str() );
}

void ExtractFiles ( CyString sfile, CyString dir, int game )
{
        // First checks if the file exists by opening it
        FILE *id = fopen ( sfile.c_str(), "rb+" );
        if ( !id )
        {
                printf ( "Error: File, %s, doesn't exist\n", sfile.c_str() );
                return;
        }
        fclose ( id );

        int check = CSpkFile::CheckFile ( sfile );

        // extracts a file from single packages file
        if ( check == SPKFILE_SINGLE || check == SPKFILE_BASE || check == SPKFILE_SINGLESHIP )
        {
                // creates the spkfile object
                CBaseFile *pBaseFile = 0;
                if ( check == SPKFILE_SINGLE )
                        pBaseFile = (CBaseFile *)new CSpkFile();
                else if ( check == SPKFILE_SINGLESHIP )
                        pBaseFile = (CBaseFile *)new CXspFile();
                else
                        pBaseFile = new CBaseFile();

                printf ( "Opening File, %s... ", sfile.c_str() );
                // reads the file into memory
                // the SPKREAD_NODATA flag causes it to just read the settings, and skips the file data
                if ( !pBaseFile->ReadFile ( sfile, SPKREAD_NODATA ) )
                {
                        printf ( "(Error)\nUnable to open the file, %s\n", sfile.c_str() );
                        return;
                }
                printf ( "(Done)\n" );

                printf ( "Extracting all files from archive..." );
                if ( pBaseFile->ExtractAll ( dir, game ) )
                        printf ( "(Done)\nFiles have been extracted successfully\n" );
                else
                        printf ( "(Error)\nThere was a problem extracting the files\n" );
        }
        else
                printf("Invalid package file, %s\n", sfile.c_str());
}


/*
        Func:   AppendMultiFile
        Args:   String toFile   - The spk file to append onto
                        String addfile  - The spk file to append
        Desc:   Appends a spk file into a Multi-Spk Archive
                        If toFile is a single spk file, it will be converted to a Multi-Spk File
                        if addfile is a Multi-Spk file, all files from it will be appended
*/
void AppendMultiFile ( CyString toFile, CyString addfile )
{
        // create destination object
        CMultiSpkFile spkfile;

        // checks the destination
        int checkto = CSpkFile::CheckFile ( toFile );

        // if the destination is a single file, then convert it to a Multi-Spk package
        if ( checkto == SPKFILE_SINGLE )
        {
                // adds the single file to the Multi-Spk object
                // Add file also reads it into memory
                if ( !spkfile.AddFile ( toFile ) )
                {
                        printf ( "Error: Unable to create Multi-Spk file\n" );
                        return;
                }
        }
        else
        {
                // if its already a multispk file, then simply open it and read it to memory
                if ( !spkfile.ReadFile ( toFile ) )
                {
                        printf ( "Error: Unable to open Multi-Spk file, %s\n", toFile.c_str() );
                        return;
                }
        }

        // now add the file into the Multi-Spk Object
        // the AddFile function will handle both single and Multi-Spk files
        // So you dont need to test what the appending file is
        if ( spkfile.AddFile ( addfile ) )
        {

                // if it added correctly, then simply write the new Multi-Spk Object to disk
                printf ( "File, %s, has been added to Multi-Spk Package\n", addfile.c_str() );
                printf ( "Saving Multi-Spk File: %s... ", toFile.c_str() );
                if ( spkfile.WriteFile ( toFile ) )
                        printf ( "(Done)\n" );
                else
                        printf ( "(Error)\n" );
        }
        else
                printf ( "Error: Unable to add files, %s, to Multi-Spk Package\n", addfile.c_str() );
}

/*
        Func:   ExtractFile
        Args:   String sfile    - the spk file to read from
                        String type             - the type of the file to find
                        String addfile  - The filename to extract
                        String dir              - The directory to extract to
        Desc:   Finds and extracts a file from a Spk Package
*/
void ExtractFile ( CyString sfile, CyString type, CyString addfile, CyString dir )
{
        // First checks if the file exists by opening it
        FILE *id = fopen ( sfile.c_str(), "rb+" );
        if ( !id )
        {
                printf ( "Error: File, %s, doesn't exist\n", sfile.c_str() );
                return;
        }
        fclose ( id );

        // now check the type of file it is, using the static function CheckFile(filename).
        // This will return the type of file
        //              SPK_INVALID             - Invalid file format, wont be able to open
        //              SPK_SINGLE              - A Single spk package file
        //              SPK_MULTI               - A Multi-Spk Archive
        //              SPK_BASE                - A Base File
        //              SPK_SINGLESHIP  - A Ship file
        int check = CSpkFile::CheckFile ( sfile );

        // extracts a file from single packages file
        if ( check == SPKFILE_SINGLE || check == SPKFILE_BASE || check == SPKFILE_SINGLESHIP )
        {
                // first get the file type is valid
                // converts the string type into its filetype flag
                int t = GetFileTypeFromString(type);
                // incorrect text type, display the error
                if ( t == -1 )
                {
                        printf ( "The file type \"%s\" is invalid\n", type.c_str() );
                        return;
                }

                // creates the spkfile object
                CBaseFile *pBaseFile = 0;
                if ( check == SPKFILE_SINGLE )
                        pBaseFile = (CBaseFile *)new CSpkFile();
                else if ( check == SPKFILE_SINGLESHIP )
                        pBaseFile = (CBaseFile *)new CXspFile();
                else
                        pBaseFile = new CBaseFile();

                printf ( "Opening File, %s... ", sfile.c_str() );
                // reads the file into memory
                // the SPKREAD_NODATA flag causes it to just read the settings, and skips the file data
                if ( !pBaseFile->ReadFile ( sfile, SPKREAD_NODATA ) )
                {
                        printf ( "(Error)\nUnable to open the file, %s\n", sfile.c_str() );
                        return;
                }

                // No read all the file data into memory
                // This can be done all together in the ReadFile function
                // Doing it seperatly allows you to save time if an error occurs, so you dont have to wait for it to read the whole thing
                pBaseFile->ReadAllFilesToMemory ();

                printf ( "(Done)\n" );

                // uses the FindFile function to find the selected file in the archive
                // requires the filename, type and custom directory (only for Extra File Type)
                C_File *f = pBaseFile->FindFile ( addfile, t );
                if ( !f )
                {
                        printf ( "Unable to find the file \"%s\" in the package\n", addfile.c_str() );
                        return;
                }

                // creates the directory so it can be extracted
                CDirIO Dir(dir);
                if ( !Dir.Create(f->GetDirectory(pBaseFile)) )
                {
                        printf ( "Unable to create the directory \"%s\" to extract into\n", dir.c_str() );
                        return;
                }

                // sets up the progress pointer
                // if it uses 7zip, the progress will be displayed in the command prompt
                MyProgress progress(0);
                printf ( "Extracting the file from package\n\t>" );

                // Extracts the file to the specified directory
                if ( !pBaseFile->ExtractFile ( f, dir, true, &progress ) )
                        printf ( "< (Error)\nUnable to extract the file\n" );
                else
                {
                        progress.PrintDone ();
                        printf ( "< (Done)\nFile has been extracted successfully\n" );
                }
        }

        // the file is a Multi-Spk File, extracts a single spk file from the archive
        else if ( check == SPKFILE_MULTI )
        {
                // creates MultiSpkFile object
                CMultiSpkFile spkfile;
                printf ( "Opening Multi-SPK file, %s...", sfile.c_str() );

                // reads the MultiSpkFile into memory
                if ( !spkfile.ReadFile ( sfile ) )
                {
                        printf ( "(Error)\nUnable to open the Multi-SPK file, %s\n", sfile.c_str() );
                        return;
                }
                printf ( "(Done)\n" );

                // searchs the archive for a matching file
                SMultiSpkFile *ms = spkfile.FindFile ( type );
                if ( !ms )
                {
                        printf ( "Unable to find the file \"%s\" in the package\n", type.c_str() );
                        return;
                }

                // extracts the file from the archive, to the given directory
                printf ( "Extracting SPK file, %s, from package... ", ms->sName.c_str() );
                if ( spkfile.ExtractFile ( ms, addfile ) )
                        printf ( "(Done)\n" );
                else
                        printf ( "(Error)\n" );
        }
        else
                printf ( "Error: Invalid file format, unable to open\n" );
}

/*
        Func:   SplitMulti
        Args:   String filename - the filename of the multispk file to open
                        String dest             - The destination directory to extract the files to
        Desc:   Splits a multi-spk file into its seperate spk files
*/
void SplitMulti ( CyString filename, CyString dest )
{
        // Check the file type, Must return SPKFILE_MULTI, otherwise its not a Multi-Spk Packages
        if ( CSpkFile::CheckFile ( filename ) != SPKFILE_MULTI )
        {
                printf ( "Error: The file is not a Multi-Spk packages\n" );
                return;
        }

        printf ( "Spliting Multi-SPK File to, %s... ", dest.c_str() );

        // create the MultiSpkFile object
        CMultiSpkFile spkfile;

        // Splits the files to the destination
        if ( !spkfile.SplitMulti  ( filename, dest ) )
        {
                printf ( "(Error)\nUnable to split Multi-SPK package, %s\n", filename.c_str() );
                return;
        }

        printf ( "(Done)\n" );
}

CyString GetAsk(CyString command)
{
        printf("(ASK) Enter the value for, %s: ", command.c_str());
        return GetInput();
}

void GenerateUpdateFile(CyString spkfile)
{
        if ( !CFileIO(spkfile).Exists() )
        {
                printf("Error: The package file, %s, does not exist", spkfile.c_str());
                return;
        }

        int check = CSpkFile::CheckFile(spkfile);
        if ( check == SPKFILE_MULTI )
        {
                printf("Error: Multi-Package files currently not supported\n");
                return;
        }
        else if ( check == SPKFILE_OLD )
        {
                printf("Error: unable to read old format spk file, try spkconvert first\n");
                return;
        }
        else if ( check == SPKFILE_INVALID )
        {
                printf("Error: %s doesn't appear to be a valid package file\n", spkfile.c_str());
                return;
        }

        CPackages p;
        int error;
        CBaseFile *package = p.OpenPackage(spkfile, &error, 0, SPKREAD_NODATA);
        if ( !package )
        {
                printf("Error: unable to open package file, %s, Error=%d\n", spkfile.c_str(), error);
                return;
        }

        CyString file = package->CreateUpdateFile(CFileIO(spkfile).GetDir());
        if ( file.Empty() )
                printf("Error: unable to create update file for: %s, Directory=%s\n", spkfile.c_str(), CFileIO(spkfile).GetDir().c_str());
        else
                printf("Update file: %s has been created for package, %s\n", file.c_str(), spkfile.c_str());

        delete package;

}

void GeneratePackagerScript(CyString spkfile, CyString toFile)
{
        if ( !CFileIO(spkfile).Exists() )
        {
                printf("Error: The package file, %s, does not exist", spkfile.c_str());
                return;
        }

        int check = CSpkFile::CheckFile(spkfile);
        if ( check == SPKFILE_MULTI )
        {
                printf("Error: Cant generate a script from a multi-spk file\n");
                return;
        }
        else if ( check == SPKFILE_OLD )
        {
                printf("Error: unable to read old format spk file, try spkconvert first\n");
                return;
        }
        else if ( check == SPKFILE_INVALID )
        {
                printf("Error: %s doesn't appear to be a spk file\n", spkfile.c_str());
                return;
        }

        CPackages p;
        int error;
        CBaseFile *package = p.OpenPackage(spkfile, &error, 0, SPKREAD_NODATA);
        if ( !package )
        {
                printf("Error: unable to open package files, %s, Error=%d\n", spkfile.c_str(), error);
                return;
        }

        CyStringList list;
        if ( !package->GeneratePackagerScript(true, &list) )
        {
                printf("Error: Unable to generate packager script\n");
                return;
        }

        if ( toFile.Empty() )
                toFile = CFileIO(spkfile).GetDirIO().File(package->GetName() + "_" + package->GetAuthor() + ".sps");

        // save package file
        if ( CFileIO(toFile).WriteFile(&list) )
                printf("Packager script, %s, has been geenrated\n", toFile.c_str());
        else
                printf("Error: unable to write packager script, %s\n", toFile.c_str());

        delete package;
}

/*
        Func:   LoadPackagerScript
        Args:   String filename -       The filename of the packager script to load
        Desc:   Loads the packager scripts and creates a spk/xsp file from it
*/
void LoadPackagerScript(CyString filename, bool verify)
{
        if ( !CFileIO(filename).Exists() )
        {
                printf("Error: The packager script, %s, does not exist", filename.c_str());
                return;
        }

        CyString myDoc;
#ifdef _WIN32
        TCHAR pszPath[MAX_PATH];
        if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, pszPath)))
        {
                myDoc = (char *)pszPath;
        }
#endif

        if ( verify )
                printf("Verifying Packager Script: %s\n", filename.c_str());

        CPackages p;
        p.Startup(".", ".", myDoc);
        CyStringList malformed, unknown;

        CyString curDir = CFileIO(filename).GetDir();
        if ( curDir.Empty() ) curDir = "./";

        CyStringList variables;
        variables.PushBack("$PATH", curDir);

        CBaseFile *package = p.LoadPackagerScript(filename, -1, (verify) ? NULL : &GetAsk, &malformed, &unknown, &variables);

        if ( verify )
        {
                if ( !malformed.Empty() )
                {
                        printf("Malformed Lines (%d):\n", malformed.Count());
                        for ( SStringList *strNode = malformed.Head(); strNode; strNode = strNode->next )
                                printf("\t(Line %3d) %s\n", strNode->data.ToInt(), strNode->str.c_str());
                }
                if ( !unknown.Empty() )
                {
                        printf("Unknown Commands (%d):\n", unknown.Count());
                        for ( SStringList *strNode = unknown.Head(); strNode; strNode = strNode->next )
                                printf("\t* Command: %s = %s\n", strNode->str.c_str(), strNode->data.c_str());
                }
        }

        if ( !package )
        {
                if ( verify )
                        printf("Error: There are errors in the packager script which prevents it from being created\n");
                else
                        printf("Error: Unable to create package, from script: %s\n", filename.c_str());
        }
        else
        {
                CyString saveto = package->GetFilename();
                saveto = saveto.FindReplace("$DEFAULTDIR", curDir + "/");
                saveto = saveto.FindReplace("$PATH", curDir);
                saveto = saveto.FindReplace("\\", "/");
                saveto = saveto.FindReplace("//", "/");
                if ( !saveto.Right(4).Compare(".spk") && package->GetType() != TYPE_XSP )
                        saveto += ".spk";
                else if ( !saveto.Right(4).Compare(".xsp") && package->GetType() == TYPE_XSP )
                        saveto += ".xsp";
                printf("Saving file to: %s\n", CFileIO(saveto).GetFullFilename().c_str());
                if ( verify )
                        printf("Package can be created from this script\n");
                else
                {
                        // write script
                        if ( package->WriteFile(saveto) )
                        {
                                if ( package->AutoGenerateUpdateFile() )
                                        package->CreateUpdateFile(CFileIO(saveto).GetDir());
                                printf("Package: %s was created\n", saveto.c_str());
                        }
                        else
                                printf("Error! There was a problem writing the package\n");
                }

                saveto = package->GetExportFilename();
                if ( !saveto.Empty() ) {
                        saveto = saveto.FindReplace("$DEFAULTDIR", curDir + "/");
                        saveto = saveto.FindReplace("$PATH", curDir);
                        saveto = saveto.FindReplace("\\", "/");
                        saveto = saveto.FindReplace("//", "/");
                        printf("Exporting file to: %s\n", CFileIO(saveto).GetFullFilename().c_str());
                        if ( verify )
                                printf("Package can be exported from this script\n");
                        else
                        {
                                // export
                                if ( package->SaveToArchive(saveto, 0) ) {
                                        if ( package->IsAnyGameInPackage() ) {
                                                for ( int i = 0; i < p.GetGameExe()->GetNumGames(); i++ ) {
                                                        if ( package->IsGameInPackage(i + 1) ) {                                                                
                                                                CyString exportFile = CFileIO(saveto).GetDir() + "/" + CFileIO(saveto).GetBaseName() + "_" + CBaseFile::ConvertGameToString(i + 1) + "." + CFileIO(saveto).GetFileExtension();
                                                                package->SaveToArchive(exportFile, i + 1);
                                                        }
                                                }
                                        }
                                        printf("Package: %s was exported\n", saveto.c_str());
                                }
                                else
                                        printf("Error! There was a problem exporting the package\n");
                        }
                }
        }
}

/*
        Main entry point to program
*/
int main ( int argc, char **argv )
{
        // display program header to command prompt
        printf ( "\nSPKTool V1.42 (SPK File Version %.2f) 03/01/2012 Created by Cycrow\n\n", (float)FILEVERSION );

        //TEST: test new string library
        CyString a = "a";
        printf("Test: %s\n", a.c_str());
        char pause2;
        scanf ( "%s", &pause2 );


        // parse the cmd name
        CyString cmd (argv[0]);
        cmd = cmd.FindReplace ( "\\", "/" );
        g_dir = cmd.GetToken ( 0, cmd.NumToken('/') - 1, '/' );
        cmd = cmd.GetToken ( cmd.NumToken('/'), '/' );

        g_read = false;

        // not enough arguments, display the syntax and exit
        if ( argc < 2 )
        {
                PrintSyntax ( cmd );
                exit ( 1 );
        }

        CyString fileTypes;
        for ( int i = 0; i < FILETYPE_MAX; i++ )
        {
                CyString sT = GetFileTypeString(i);
                if ( !sT.Empty() )
                {
                        if ( fileTypes.Empty() )
                                fileTypes = sT;
                        else
                        {
                                fileTypes += ", ";
                                fileTypes += sT;
                        }
                }
        }

        // get the command flag
        CyString command(argv[1]);

        // display the contents of the spk file
        if ( command == "-v" || command == "--version" )
        {
                if ( argc < 3 )
                        printf ( "Syntax: %s -v <spkfile>\n\tWill open and display the contents of the spkfile\n", cmd.c_str() );
                else
                        DisplayVersion ( argv[2] );
        }

        // creates a new spk file
        else if ( command == "-c" || command == "--create" )
        {
                if ( argc < 3 )
                        printf ( "Syntax:\n\t%s -c <spkfile>\n\t%s --create <spkfile>\n\t\tThis will create a new SPK file and allow you to set some basic settings for the file\n", cmd.c_str(), cmd.c_str() );
                else
                        CreateFile ( argv[2] );
        }

        // appends a file onto the spk archive
        else if ( command == "-a" || command == "--append" )
        {
                if ( argc < 4 )
                        printf ( "Syntax:\n\t%s -a <spkfile> <type> <filename>\n\t%s --append <spkfile> <type> <filename>\n\t\tThis will append the file into the archive and compress it according to the files default compression\n\t<type> = %s\n", cmd.c_str(), cmd.c_str(), fileTypes.c_str() );
                else
                {
                        CyString arg4;
                        if ( argc > 4 )
                                arg4 = argv[4];
                        AppendFile ( argv[2], argv[3], arg4 );
                }
        }

        // removes a file from the spk archive
        else if ( command == "-r" || command == "--remove" || command == "--removespk" )
        {
                if ( argc < 4 )
                {
                        printf ( "Syntax:\n\t%s -r <spkfile> <type> <filename>\n\t%s --remove <spkfile> <type> <filename\n\t\tThis will remove a file from the archive\n\t<type> = %s\n", cmd.c_str(), cmd.c_str(), fileTypes.c_str() );
                        printf ( "\t%s -r <multispkfile> <filename>\n\t%s --removespk <multispkfile> <filename>\n\t\tThis will remove a spk file from the Multi-SPK package\n", cmd.c_str(), cmd.c_str() );
                }
                else
                {
                        CyString arg4;
                        if ( argc > 4 )
                                arg4 = argv[4];
                        RemoveFile ( argv[2], argv[3], arg4 );
                }
        }

        // extracts a file from a spk file
        else if ( command == "--extractspk" )
        {
                if ( argc < 4 )
                        printf ( "Syntax:\n\t%s --extractspk <multispkfile> <filename> [destination]\n\tThis will extract a spk file from the Multi-Spk package and save it to the destination path\n", cmd.c_str() );
                else
                {
                        CyString arg4;
                        if ( argc > 4 )
                                arg4 = argv[3];
                        ExtractFile ( argv[2], argv[3], arg4, NullString );
                }
        }
        else if ( command == "-x" || command == "--extract")
        {
                if ( argc < 4 )
                {
                        printf ( "Syntax:\n\t%s -x <spkfile> <type> <filename> [destination]\n\tThis will extract a file from the package and save it to the corect path in <directory>\n\t<type> = %s\n", cmd.c_str(), fileTypes.c_str() );
                        printf ( "\t%s -x <multispkfile> <filename> [destination]\n\tThis will extract a spk file from the Multi-Spk package and save it to the destination path\n", cmd.c_str() );
                }
                else
                {
                        CyString arg5, arg4;
                        if ( argc > 5 )
                                arg5 = argv[5];
                        if ( argc > 4 )
                                arg4 = argv[4];
                        ExtractFile ( argv[2], argv[3], arg4, arg5 );
                }
        }

        // extracts all the files from an archive
        else if ( command == "-e" || command == "--extractall" )
        {
                if ( argc < 3 )
                        printf ( "Syntax:\n\t%s -e <spkfile> [destination]\n\t--extractall <spkfile> <game> [destination]\n\t\tThis will extract all files of a set game into the destination directory\n\n\t\tGame = 0 will extract all files", cmd.c_str() );
                else
                {
                        CyString arg4;
                        if ( argc > 4 )
                                arg4 = argv[4];
                        ExtractFiles ( argv[2], arg4, atoi(argv[3]) );
                }
        }

        // creates a multispk archive
        else if ( command == "-n" || command == "--createmulti" )
        {
                if ( argc < 3 )
                        printf ( "Syntax:\n\t%s -n <multispkfile>\n\t%s --createmulti <multispkfile>\n\t\tThis will create a multispk file and allow you to add spk files to it\n", cmd.c_str(), cmd.c_str() );
                else
                        CreateMultiFile ( argv[2] );
        }

        // merges 2 multi-spk archives together, or appends a single spk file into a multi-spk file
        else if ( command == "-m" || command == "--mergemulti" )
        {
                if ( argc < 4 )
                        printf ( "Syntax:\n\t%s -m <spkfile1> <spkfile2>\n\t%s --mergemulti <spkfile1> <spkfile2>\n\t\tThis will add spkfile2 into spkfile1, if spkfile1 is a normal SPK file, it will be saved into a Multi-Spk file\nspkfile2 can also be a Multi-Spk file, all files within it will be added\n", cmd.c_str(), cmd.c_str() );
                else
                        AppendMultiFile ( argv[2], argv[3] );
        }

        // splits the multi-spk file, exracts all spk files
        else if ( command == "-s" || command == "--splitmulti" )
        {
                if ( argc < 3 )
                        printf ( "Syntax: %s -s <multispkfile> [destination]\n\tSplits the Multi-SPK file and saves each spk file to the destiantion directory\n", cmd.c_str() );
                else
                {
                        CyString arg3;
                        if ( argc > 3 )
                                arg3 = argv[3];
                        SplitMulti ( argv[2], arg3 );
                }
        }

        else if ( command == "--set" )
        {
                if ( argc < 4 )
                        printf ( "Syntax: %s --set <spkfile> <setting> [values]\n\tSets various settings in the package\n", cmd.c_str() );
                else
                        Settings(argv[2], argv[3], argc, argv, cmd);
        }
        else if ( command == "--setrating" )
        {
                if ( argc < 6 )
                        printf ( "Syntax: %s --setrating <spkfile> <easeofuse> <gamechanging> <recommended>\n\tSets the rating of the spk package\n", cmd.c_str() );
                else
                        SetRating(argv[2], CyString(argv[3]).ToInt(), CyString(argv[4]).ToInt(), CyString(argv[5]).ToInt());
        }
        else if ( command == "--convertxsp" || command == "--convertxspwizard" )
        {
                if ( argc < 3 )
                        printf ( "Syntax: %s %s <xspfile> [newxspfile]\n\tConverts an old format xsp file to work with the new format\n", cmd.c_str(), command.c_str() );
                else
                {
                        CyString arg3;
                        if ( argc > 3 )
                                arg3 = argv[3];
                        ConvertXsp(argv[2], arg3, (command == "--convertxspwizard") ? true : false);
                }
        }
        else if ( command == "--createscript" )
        {
                if ( argc < 3 )
                        printf ( "Syntax:\n\t%s --createscript <packagerscript>\n\t\tThis will create a spk/xsp file from a packager script\n", cmd.c_str() );
                else
                        LoadPackagerScript ( argv[2], false );
        }
        else if ( command == "--generatescript" )
        {
                if ( argc < 3 )
                        printf ( "Syntax:\n\t%s --generatescript <package> [packagerscript]\n\t\tThis will create a spk/xsp file from a packager script\n", cmd.c_str() );
                else
                {
                        if ( argc < 4 )
                                GeneratePackagerScript ( argv[2], "" );
                        else
                                GeneratePackagerScript ( argv[2], argv[3] );
                }
        }
        else if ( command == "--verifyscript" )
        {
                if ( argc < 3 )
                        printf ( "Syntax:\n\t%s --verifyscript <packagerscript>\n\t\tThis will read a packager script and check its correct without creating the resulting spk/xsp file\n", cmd.c_str() );
                else
                        LoadPackagerScript ( argv[2], true );
        }
        else if ( command == "--extractship" )
        {
                if ( argc < 4 )
                        printf ( "Syntax:\n\t%s --extractship <modfile> <xspfile> [shipid]\n\t\tThis will create an xsp ship file by extracting from a mod file\n", cmd.c_str() );
                else
                {
                        if ( argc < 5 )
                                ExtractShip ( argv[2], argv[3], "" );
                        else
                                ExtractShip ( argv[2], argv[3], argv[4] );
                }
        }
        else if ( command == "--generateupdatefile" )
        {
                if ( argc < 3 )
                        printf ( "Syntax:\n\t%s --generateupdatefile <package>\n\t\tThis will generate the update file to allow auto updates for a package\n", cmd.c_str() );
                else
                        GenerateUpdateFile(argv[2]);
        }

        // not a valid switch, display syntax
        else
                PrintSyntax ( cmd );

#ifdef _DEBUG
        char pause;
        scanf ( "%s", &pause );
#endif

        return 0;
}