Subversion Repositories spk

Rev

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

#include "../StdAfx.h"
#include "Form1.h"

namespace Creator {

Form1::Form1(array<System::String ^> ^args)
{
        InitializeComponent();

        m_bAutoClose = false;
        m_pWait = nullptr;
        m_bTextLoaded = false;
        this->shipToolStripMenuItem->Image = this->imageList1->Images[1];
        this->shipToolStripMenuItem1->Image = this->imageList1->Images[1];
        this->multiPackageToolStripMenuItem->Image = this->imageList1->Images[this->imageList1->Images->IndexOfKey("multi")];
        this->Closed += gcnew System::EventHandler(this, &Form1::CloseEvent);

        this->Text = "Package Creator " + GetVersionString();
        m_pLoadedList = new CyStringList;
        m_pGameDir = new CLinkList<SGameDir>;

        // default settings
        m_settings = new SSettings;
        m_settings->bGenerateUpdate = false;

        m_pComponents = NULL;
        m_pDummies = NULL;
        m_pBodies = NULL;
        m_pShields = NULL;
        m_pCockpits = new CLinkList<CyStringList>;
        m_pLasers = new CLinkList<CyStringList>;
        m_pMissiles = new CLinkList<CyStringList>;

        m_iLocX = m_iLocY = -1;

        this->toolTip1->SetToolTip(this->button1, "Closes all open windows, will ask about saving any that have been modified when they close");

        m_pPackages = new CPackages;
        m_pPackages->Startup(".", CyStringFromSystemString(IO::Path::GetTempPath()), CyStringFromSystemString(Environment::GetFolderPath(Environment::SpecialFolder::Personal )));
        m_pPackages->SetLanguage(44);

        this->LoadData();

        this->UpdateDisplay();

        // parse any switches
        this->parseCommandArguments(args);
}

void Form1::parseCommandArguments(array<System::String ^> ^args)
{
        int switchType = SWITCH_NONE;

        if ( args ) {
                ArrayList ^list = gcnew ArrayList();
                for ( int i = 0; i < args->Length; i++ ) {
                        String ^str = args[i];
                        if ( switchType != SWITCH_NONE ) {
                                switch(switchType) {
                                        case SWITCH_CREATE:
                                                this->SavePackagerScript(str);
                                                m_bAutoClose = true;
                                                break;
                                        case SWITCH_EXTRACT:
                                                this->ExtractPackage(str, false);
                                                m_bAutoClose = true;
                                                break;
                                        case SWITCH_EXTRACTHERE:
                                                this->ExtractPackage(str, true);
                                                m_bAutoClose = true;
                                                break;
                                        case SWITCH_EXPORT:
                                                this->ExportPackage(str);
                                                m_bAutoClose = true;
                                                break;
                                }
                                switchType = SWITCH_NONE;
                        }
                        else if ( !String::Compare(str, "--create", true) ) {
                                switchType = SWITCH_CREATE;                     
                        }
                        else if ( !String::Compare(str, "--extract", true) ) {
                                switchType = SWITCH_EXTRACT;
                        }
                        else if ( !String::Compare(str, "--extracthere", true) ) {
                                switchType = SWITCH_EXTRACTHERE;
                        }
                        else if ( !String::Compare(str, "--export", true) ) {
                                switchType = SWITCH_EXPORT;
                        }
                        else {
                                list->Add(args[i]);
                        }
                }

                if ( list->Count ) {
                        this->OpenFiles(list, true, true);
                        m_bAutoClose = false;
                }
        }
}


String ^Form1::getShipSelection(CVirtualFileSystem *pVfs)
{
        CyStringList list;
        if ( pVfs->loadShipData(&list) ) {
                LoadShip ^ls = gcnew LoadShip();
                for ( SStringList *str = list.Head(); str; str = str->next )
                {
                        int tId = str->data.GetToken(";", 7, 7).ToInt();
                        String ^name;
                        if ( pVfs->textExists(44, 17, tId) )
                                name = SystemStringFromCyString(pVfs->findText(44, 17, tId));
                        else
                                name = this->FindText(-1, 17, tId);
                        String ^text = this->FindText(-1, 1266, str->data.GetToken(";", 46, 46).ToInt()) + " " + name;
                        int variation = str->data.GetToken(";", 51, 51).ToInt();
                        if ( variation )
                                text = text + " " + this->FindText(-1, 17, 10000 + variation);
                        ls->AddShip(SystemStringFromCyString(str->str), text);
                }

                if ( ls->ShowDialog(this) == System::Windows::Forms::DialogResult::OK ) {
                        SStringList *str = list.FindString(CyStringFromSystemString(ls->GetData()));
                        if ( str )      {
                                return ls->GetData();
                        }
                }
                else return "";
        }

        return nullptr;
}

void Form1::ImportShip()
{
        OpenFileDialog ^ofd = gcnew OpenFileDialog();
        ofd->Filter = "X Catalog Files (*.cat)|*.cat";
        ofd->Title = "Select the mod file to import a ship from";
        ofd->FilterIndex = 1;
        ofd->RestoreDirectory = true;
        ofd->Multiselect = false;

        CVirtualFileSystem *vfs = new CVirtualFileSystem();

        if ( ofd->ShowDialog(this) == System::Windows::Forms::DialogResult::OK )
        {
                if ( !vfs->loadMod(CyStringFromSystemString(ofd->FileName).ToString()) ) {
                        MessageBox::Show(this, "There was a problem when trying to import the ship\n" + ofd->FileName + "\n\nError: Unable to open mod file", "Error Importing Ship", MessageBoxButtons::OK, MessageBoxIcon::Error);
                        return;
                }
                this->UseWaitCursor = true;

                vfs->updateModTexts(17);

                String ^shipID = getShipSelection(vfs);
                if ( !shipID ) {
                        MessageBox::Show(this, "There was a problem trying to load the mod file\n" + ofd->FileName, "Error Importing Ship", MessageBoxButtons::OK, MessageBoxIcon::Error);
                }
                else if ( shipID->Length ) {
                        String ^loadMod = ofd->FileName;
                        CXspFile *newShip = NULL;

                        while ( true ) {
                                Creator::ImportShip ^import = gcnew Creator::ImportShip(shipID, vfs);
                                import->ShowDialog(this);
                                newShip = (import->Error()) ? NULL : import->GetShip();
                                if ( !newShip ) {
                                        if ( MessageBox::Show(this, "There was a problem when trying to import the ship\n" + ofd->FileName + " (" + shipID + ")\n\nError: " + import->getErrorString() + "\n\nWould you like to read files from another mod as well?", "Error Importing Ship", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::Yes ) {
                                                // load in another file
                                                if ( ofd->ShowDialog(this) == System::Windows::Forms::DialogResult::OK ) {
                                                        loadMod = ofd->FileName;
                                                        if ( !vfs->loadMod(CyStringFromSystemString(loadMod).ToString()) ) {
                                                                MessageBox::Show(this, "There was a problem when trying to import the ship\n" + ofd->FileName + " (" + shipID + ")\n\nError: Unable to open mod file", "Error Importing Ship", MessageBoxButtons::OK, MessageBoxIcon::Error);
                                                                break;
                                                        }
                                                }
                                                else break;
                                        }
                                        else break;
                                }
                                else break;     
                        }

                        if ( newShip ) {
                                PackageForm ^childForm = this->OpenPackage(true, newShip, "", "Imported Ship");
                                childForm->Text = "Imported Ship";
                                newShip->adjustChanged(true);
                                childForm->UpdateChanged();
                        }

                }

                this->UseWaitCursor = false;
        }

        delete vfs;
}


void Form1::ImportShipFromVFS()
{
        // some way to select the VFS to use
        SelectFilesystem ^Filesystem = gcnew SelectFilesystem(m_pGameDir, this->GetHighestGame());
        if ( Filesystem->ShowDialog(this) != System::Windows::Forms::DialogResult::OK ) {
                return;
        }

        SGameDir *pDir = Filesystem->gameDir();
        CyString mod;
        if ( Filesystem->gameMod() ) {
                mod = pDir->sDir + "/mods/" + CyStringFromSystemString(Filesystem->gameMod()) + ".cat";
        }

        // load a mod on top of the VFS
        if ( !mod.Empty() ) {
                if ( !pDir->pVfs->loadMod(mod.ToString()) ) {
                        MessageBox::Show(this, "There was a problem trying to open the mod file\n" + Filesystem->gameMod(), "Error Importing Ship", MessageBoxButtons::OK, MessageBoxIcon::Error);
                        return;
                }
                pDir->pVfs->updateModTexts(17);
        }

        // get the ship id to load
        String ^shipID = getShipSelection(pDir->pVfs);
        if ( !shipID ) {
                MessageBox::Show(this, "There was a problem trying to load ship data from VFS\n", "Error Importing Ship", MessageBoxButtons::OK, MessageBoxIcon::Error);
        }
        else if ( shipID->Length ) {
                // import the ship
                Creator::ImportShip ^import = gcnew Creator::ImportShip(shipID, pDir->pVfs);
                import->ShowDialog(this);

                // create the new form with ship data
                if ( !import->Error() ) {
                        PackageForm ^childForm = this->OpenPackage(true, import->GetShip(), "", "Imported Ship");
                        childForm->Text = "Imported Ship";
                        import->GetShip()->adjustChanged(true);
                        childForm->UpdateChanged();
                }
        }

        // remove any mods that we have loaded
        pDir->pVfs->clearMods();
}

} //NAMESPACE