Subversion Repositories spk

Rev

Rev 226 | 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();

        System::String ^mydoc = Environment::GetFolderPath(Environment::SpecialFolder::Personal );

        m_bAutoClose = false;
        m_pWait = nullptr;
        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 Utils::WStringList;
        _pGameDir = new CGameDirectories(_WS(mydoc));
        _pGameDir->setLanguage(44);

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

        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(L".", _WS(IO::Path::GetTempPath()), _WS(Environment::GetFolderPath(Environment::SpecialFolder::Personal)));
        m_pPackages->SetLanguage(44);

        this->LoadData();

        this->UpdateDisplay();

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

        if ( _pGameDir->isEmpty() )
                _pGameDir->syncWithControlled(m_pPackages->GetGameExe());
}

Form1::!Form1()
{
}

bool Form1::CheckCommandArguments(array<System::String ^> ^args)
{
        for ( int i = 0; i < args->Length; i++ ) {
                String ^str = args[i];
                if ( !String::Compare(str, "--create", true) )
                        return true;
                else if ( !String::Compare(str, "--extract", true) )
                        return true;
                else if ( !String::Compare(str, "--extracthere", true) )
                        return true;
                else if ( !String::Compare(str, "--export", true) )
                        return true;
        }

        return false;
}

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, System::String ^*shipName)
{
        Utils::WStringList *ships = pVfs->getTShipsEntries();
        if ( ships ) {
                this->LoadText(pVfs);

                LoadShip ^ls = gcnew LoadShip();
                for(Utils::WStringNode *str = ships->first(); str; str = ships->next()) {
                        int tId = str->str.token(L";", 7).toLong();
                        String ^name = _US(pVfs->findText(0, 17, tId));
                        String ^race = (str->str.token(L";", 46).toLong() == 0) ? "" : _US(pVfs->findText(0, 1266, str->str.token(L";", 46).toLong()));
                        String ^text = race + " " + name;
                        int variation = str->str.token(L";", 51).toLong();
                        if ( variation && variation != 20 )
                                text = text + " " + _US(pVfs->findText(0, 17, 10000 + variation));
                        ls->AddShip(_US(str->data), text);
                }

                if ( ls->ShowDialog(this) == System::Windows::Forms::DialogResult::OK ) {
                        Utils::WString line = pVfs->getTShipsEntry(_WS(ls->GetData()));
        
                        if ( !line.empty() ) {
                                if ( shipName ) {
                                        (*shipName) = ls->GetShipName();
                                }
                                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(_WS(ofd->FileName)) ) {
                        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 ^shipName = "";
                String ^shipID = getShipSelection(vfs, &shipName);
                if ( !shipID ) {
                        MessageBox::Show(this, "There was a problem trying to load the mod file\n" + ofd->FileName + "\n\nNo TShips file found", "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, shipName, 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(_WS(loadMod)) ) {
                                                                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();
                                childForm->WindowState = Windows::Forms::FormWindowState::Maximized;
                        }

                }

                this->UseWaitCursor = false;
        }

        delete vfs;
}


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

        Utils::WString dir = Filesystem->gameDir();
        Utils::WString mod;
        if ( Filesystem->gameMod() ) {
                mod = dir + L"/mods/" + _WS(Filesystem->gameMod()) + L".cat";
        }

        CVirtualFileSystem *vfs = _pGameDir->selectedVFS();

        if ( !vfs ) {
                MessageBox::Show(this, "Error: Couldn't open Virtual File System", "Error Importing Ship", MessageBoxButtons::OK, MessageBoxIcon::Error);
                return;
        }

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

        // get the ship id to load
        String ^shipName = "";
        String ^shipID = getShipSelection(vfs, &shipName);
        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, shipName, vfs);
                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();
                        childForm->WindowState = Windows::Forms::FormWindowState::Maximized;
                }
        }

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

} //NAMESPACE