| 36 | cycrow | 1 | #include "../StdAfx.h"
 | 
        
           |  |  | 2 | #include "Form1.h"
 | 
        
           |  |  | 3 |   | 
        
           |  |  | 4 | namespace Creator {
 | 
        
           |  |  | 5 |   | 
        
           |  |  | 6 | Form1::Form1(array<System::String ^> ^args)
 | 
        
           |  |  | 7 | {
 | 
        
           |  |  | 8 | 	InitializeComponent();
 | 
        
           |  |  | 9 |   | 
        
           | 95 | cycrow | 10 | 	System::String ^mydoc = Environment::GetFolderPath(Environment::SpecialFolder::Personal );
 | 
        
           |  |  | 11 |   | 
        
           | 36 | cycrow | 12 | 	m_bAutoClose = false;
 | 
        
           |  |  | 13 | 	m_pWait = nullptr;
 | 
        
           |  |  | 14 | 	this->shipToolStripMenuItem->Image = this->imageList1->Images[1];
 | 
        
           |  |  | 15 | 	this->shipToolStripMenuItem1->Image = this->imageList1->Images[1];
 | 
        
           |  |  | 16 | 	this->multiPackageToolStripMenuItem->Image = this->imageList1->Images[this->imageList1->Images->IndexOfKey("multi")];
 | 
        
           |  |  | 17 | 	this->Closed += gcnew System::EventHandler(this, &Form1::CloseEvent);
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | 	this->Text = "Package Creator " + GetVersionString();
 | 
        
           |  |  | 20 | 	m_pLoadedList = new CyStringList;
 | 
        
           | 95 | cycrow | 21 | 	_pGameDir = new CGameDirectories(_S(mydoc));
 | 
        
           | 94 | cycrow | 22 | 	_pGameDir->setLanguage(44);
 | 
        
           | 36 | cycrow | 23 |   | 
        
           |  |  | 24 | 	// default settings
 | 
        
           |  |  | 25 | 	m_settings = new SSettings;
 | 
        
           |  |  | 26 | 	m_settings->bGenerateUpdate = false;
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 | 	m_iLocX = m_iLocY = -1;
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 | 	this->toolTip1->SetToolTip(this->button1, "Closes all open windows, will ask about saving any that have been modified when they close");
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 | 	m_pPackages = new CPackages;
 | 
        
           |  |  | 33 | 	m_pPackages->Startup(".", CyStringFromSystemString(IO::Path::GetTempPath()), CyStringFromSystemString(Environment::GetFolderPath(Environment::SpecialFolder::Personal )));
 | 
        
           |  |  | 34 | 	m_pPackages->SetLanguage(44);
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 | 	this->LoadData();
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 | 	this->UpdateDisplay();
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 | 	// parse any switches
 | 
        
           |  |  | 41 | 	this->parseCommandArguments(args);
 | 
        
           | 95 | cycrow | 42 |   | 
        
           |  |  | 43 | 	if ( _pGameDir->isEmpty() )
 | 
        
           |  |  | 44 | 		_pGameDir->syncWithControlled(m_pPackages->GetGameExe());
 | 
        
           | 36 | cycrow | 45 | }
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 | void Form1::parseCommandArguments(array<System::String ^> ^args)
 | 
        
           |  |  | 48 | {
 | 
        
           |  |  | 49 | 	int switchType = SWITCH_NONE;
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 | 	if ( args ) {
 | 
        
           |  |  | 52 | 		ArrayList ^list = gcnew ArrayList();
 | 
        
           |  |  | 53 | 		for ( int i = 0; i < args->Length; i++ ) {
 | 
        
           |  |  | 54 | 			String ^str = args[i];
 | 
        
           |  |  | 55 | 			if ( switchType != SWITCH_NONE ) {
 | 
        
           |  |  | 56 | 				switch(switchType) {
 | 
        
           |  |  | 57 | 					case SWITCH_CREATE:
 | 
        
           |  |  | 58 | 						this->SavePackagerScript(str);
 | 
        
           |  |  | 59 | 						m_bAutoClose = true;
 | 
        
           |  |  | 60 | 						break;
 | 
        
           |  |  | 61 | 					case SWITCH_EXTRACT:
 | 
        
           |  |  | 62 | 						this->ExtractPackage(str, false);
 | 
        
           |  |  | 63 | 						m_bAutoClose = true;
 | 
        
           |  |  | 64 | 						break;
 | 
        
           |  |  | 65 | 					case SWITCH_EXTRACTHERE:
 | 
        
           |  |  | 66 | 						this->ExtractPackage(str, true);
 | 
        
           |  |  | 67 | 						m_bAutoClose = true;
 | 
        
           |  |  | 68 | 						break;
 | 
        
           |  |  | 69 | 					case SWITCH_EXPORT:
 | 
        
           |  |  | 70 | 						this->ExportPackage(str);
 | 
        
           |  |  | 71 | 						m_bAutoClose = true;
 | 
        
           |  |  | 72 | 						break;
 | 
        
           |  |  | 73 | 				}
 | 
        
           |  |  | 74 | 				switchType = SWITCH_NONE;
 | 
        
           |  |  | 75 | 			}
 | 
        
           |  |  | 76 | 			else if ( !String::Compare(str, "--create", true) ) {
 | 
        
           |  |  | 77 | 				switchType = SWITCH_CREATE;			
 | 
        
           |  |  | 78 | 			}
 | 
        
           |  |  | 79 | 			else if ( !String::Compare(str, "--extract", true) ) {
 | 
        
           |  |  | 80 | 				switchType = SWITCH_EXTRACT;
 | 
        
           |  |  | 81 | 			}
 | 
        
           |  |  | 82 | 			else if ( !String::Compare(str, "--extracthere", true) ) {
 | 
        
           |  |  | 83 | 				switchType = SWITCH_EXTRACTHERE;
 | 
        
           |  |  | 84 | 			}
 | 
        
           |  |  | 85 | 			else if ( !String::Compare(str, "--export", true) ) {
 | 
        
           |  |  | 86 | 				switchType = SWITCH_EXPORT;
 | 
        
           |  |  | 87 | 			}
 | 
        
           |  |  | 88 | 			else {
 | 
        
           |  |  | 89 | 				list->Add(args[i]);
 | 
        
           |  |  | 90 | 			}
 | 
        
           |  |  | 91 | 		}
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 | 		if ( list->Count ) {
 | 
        
           |  |  | 94 | 			this->OpenFiles(list, true, true);
 | 
        
           |  |  | 95 | 			m_bAutoClose = false;
 | 
        
           |  |  | 96 | 		}
 | 
        
           |  |  | 97 | 	}
 | 
        
           |  |  | 98 | }
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 |   | 
        
           |  |  | 101 | String ^Form1::getShipSelection(CVirtualFileSystem *pVfs)
 | 
        
           |  |  | 102 | {
 | 
        
           | 101 | cycrow | 103 | 	Utils::CStringList *ships = pVfs->getTShipsEntries();
 | 
        
           |  |  | 104 | 	if ( ships ) {
 | 
        
           |  |  | 105 | 		this->LoadText(pVfs);
 | 
        
           | 94 | cycrow | 106 |   | 
        
           | 36 | cycrow | 107 | 		LoadShip ^ls = gcnew LoadShip();
 | 
        
           | 101 | cycrow | 108 | 		for(Utils::SStringList *str = ships->First(); str; str = ships->Next()) {
 | 
        
           |  |  | 109 | 			int tId = str->str.token(";", 7).toLong();
 | 
        
           | 94 | cycrow | 110 | 			String ^name = _US(pVfs->findText(0, 17, tId));
 | 
        
           | 101 | cycrow | 111 | 			String ^race = _US(pVfs->findText(0, 1266, str->str.token(";", 46).toLong()));
 | 
        
           | 94 | cycrow | 112 | 			String ^text = race + " " + name;
 | 
        
           | 101 | cycrow | 113 | 			int variation = str->str.token(";", 51).toLong();
 | 
        
           | 36 | cycrow | 114 | 			if ( variation )
 | 
        
           | 94 | cycrow | 115 | 				text = text + " " + _US(pVfs->findText(0, 17, 10000 + variation));
 | 
        
           | 101 | cycrow | 116 | 			ls->AddShip(_US(str->data), text);
 | 
        
           | 36 | cycrow | 117 | 		}
 | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 | 		if ( ls->ShowDialog(this) == System::Windows::Forms::DialogResult::OK )	{
 | 
        
           | 101 | cycrow | 120 | 			Utils::String line = pVfs->getTShipsEntry(_S(ls->GetData()));
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 | 			if ( !line.empty() ) {
 | 
        
           | 36 | cycrow | 123 | 				return ls->GetData();
 | 
        
           |  |  | 124 | 			}
 | 
        
           |  |  | 125 | 		}
 | 
        
           |  |  | 126 | 		else return "";
 | 
        
           |  |  | 127 | 	}
 | 
        
           |  |  | 128 |   | 
        
           |  |  | 129 | 	return nullptr;
 | 
        
           |  |  | 130 | }
 | 
        
           |  |  | 131 |   | 
        
           |  |  | 132 | void Form1::ImportShip()
 | 
        
           |  |  | 133 | {
 | 
        
           |  |  | 134 | 	OpenFileDialog ^ofd = gcnew OpenFileDialog();
 | 
        
           |  |  | 135 | 	ofd->Filter = "X Catalog Files (*.cat)|*.cat";
 | 
        
           |  |  | 136 | 	ofd->Title = "Select the mod file to import a ship from";
 | 
        
           |  |  | 137 | 	ofd->FilterIndex = 1;
 | 
        
           |  |  | 138 | 	ofd->RestoreDirectory = true;
 | 
        
           |  |  | 139 | 	ofd->Multiselect = false;
 | 
        
           |  |  | 140 |   | 
        
           |  |  | 141 | 	CVirtualFileSystem *vfs = new CVirtualFileSystem();
 | 
        
           |  |  | 142 |   | 
        
           |  |  | 143 | 	if ( ofd->ShowDialog(this) == System::Windows::Forms::DialogResult::OK )
 | 
        
           |  |  | 144 | 	{
 | 
        
           |  |  | 145 | 		if ( !vfs->loadMod(CyStringFromSystemString(ofd->FileName).ToString()) ) {
 | 
        
           |  |  | 146 | 			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);
 | 
        
           |  |  | 147 | 			return;
 | 
        
           |  |  | 148 | 		}
 | 
        
           |  |  | 149 | 		this->UseWaitCursor = true;
 | 
        
           |  |  | 150 |   | 
        
           |  |  | 151 | 		vfs->updateModTexts(17);
 | 
        
           |  |  | 152 |   | 
        
           |  |  | 153 | 		String ^shipID = getShipSelection(vfs);
 | 
        
           | 39 | cycrow | 154 | 		if ( !shipID ) {
 | 
        
           | 77 | cycrow | 155 | 			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);
 | 
        
           | 39 | cycrow | 156 | 		}
 | 
        
           |  |  | 157 | 		else if ( shipID->Length ) {
 | 
        
           | 36 | cycrow | 158 | 			String ^loadMod = ofd->FileName;
 | 
        
           |  |  | 159 | 			CXspFile *newShip = NULL;
 | 
        
           |  |  | 160 |   | 
        
           |  |  | 161 | 			while ( true ) {
 | 
        
           |  |  | 162 | 				Creator::ImportShip ^import = gcnew Creator::ImportShip(shipID, vfs);
 | 
        
           |  |  | 163 | 				import->ShowDialog(this);
 | 
        
           |  |  | 164 | 				newShip = (import->Error()) ? NULL : import->GetShip();
 | 
        
           |  |  | 165 | 				if ( !newShip ) {
 | 
        
           |  |  | 166 | 					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 ) {
 | 
        
           |  |  | 167 | 						// load in another file
 | 
        
           |  |  | 168 | 						if ( ofd->ShowDialog(this) == System::Windows::Forms::DialogResult::OK ) {
 | 
        
           |  |  | 169 | 							loadMod = ofd->FileName;
 | 
        
           |  |  | 170 | 							if ( !vfs->loadMod(CyStringFromSystemString(loadMod).ToString()) ) {
 | 
        
           |  |  | 171 | 								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);
 | 
        
           |  |  | 172 | 								break;
 | 
        
           |  |  | 173 | 							}
 | 
        
           |  |  | 174 | 						}
 | 
        
           |  |  | 175 | 						else break;
 | 
        
           |  |  | 176 | 					}
 | 
        
           |  |  | 177 | 					else break;
 | 
        
           |  |  | 178 | 				}
 | 
        
           |  |  | 179 | 				else break;	
 | 
        
           |  |  | 180 | 			}
 | 
        
           |  |  | 181 |   | 
        
           |  |  | 182 | 			if ( newShip ) {
 | 
        
           |  |  | 183 | 				PackageForm ^childForm = this->OpenPackage(true, newShip, "", "Imported Ship");
 | 
        
           |  |  | 184 | 				childForm->Text = "Imported Ship";
 | 
        
           | 50 | cycrow | 185 | 				newShip->adjustChanged(true);
 | 
        
           | 36 | cycrow | 186 | 				childForm->UpdateChanged();
 | 
        
           |  |  | 187 | 			}
 | 
        
           |  |  | 188 |   | 
        
           |  |  | 189 | 		}
 | 
        
           |  |  | 190 |   | 
        
           |  |  | 191 | 		this->UseWaitCursor = false;
 | 
        
           |  |  | 192 | 	}
 | 
        
           |  |  | 193 |   | 
        
           |  |  | 194 | 	delete vfs;
 | 
        
           |  |  | 195 | }
 | 
        
           |  |  | 196 |   | 
        
           |  |  | 197 |   | 
        
           |  |  | 198 | void Form1::ImportShipFromVFS()
 | 
        
           |  |  | 199 | {
 | 
        
           |  |  | 200 | 	// some way to select the VFS to use
 | 
        
           | 94 | cycrow | 201 | 	SelectFilesystem ^Filesystem = gcnew SelectFilesystem(_pGameDir);
 | 
        
           | 36 | cycrow | 202 | 	if ( Filesystem->ShowDialog(this) != System::Windows::Forms::DialogResult::OK ) {
 | 
        
           |  |  | 203 | 		return;
 | 
        
           |  |  | 204 | 	}
 | 
        
           |  |  | 205 |   | 
        
           | 94 | cycrow | 206 | 	Utils::String dir = Filesystem->gameDir();
 | 
        
           | 36 | cycrow | 207 | 	CyString mod;
 | 
        
           |  |  | 208 | 	if ( Filesystem->gameMod() ) {
 | 
        
           | 94 | cycrow | 209 | 		mod = CyString(dir) + "/mods/" + CyStringFromSystemString(Filesystem->gameMod()) + ".cat";
 | 
        
           | 36 | cycrow | 210 | 	}
 | 
        
           |  |  | 211 |   | 
        
           | 94 | cycrow | 212 | 	CVirtualFileSystem *vfs = _pGameDir->selectedVFS();
 | 
        
           |  |  | 213 |   | 
        
           |  |  | 214 | 	if ( !vfs ) {
 | 
        
           |  |  | 215 | 		MessageBox::Show(this, "Error: Couldn't open Virtual File System", "Error Importing Ship", MessageBoxButtons::OK, MessageBoxIcon::Error);
 | 
        
           |  |  | 216 | 		return;
 | 
        
           |  |  | 217 | 	}
 | 
        
           |  |  | 218 |   | 
        
           | 36 | cycrow | 219 | 	// load a mod on top of the VFS
 | 
        
           |  |  | 220 | 	if ( !mod.Empty() ) {
 | 
        
           | 94 | cycrow | 221 | 		if ( !vfs->loadMod(mod.ToString()) ) {
 | 
        
           | 36 | cycrow | 222 | 			MessageBox::Show(this, "There was a problem trying to open the mod file\n" + Filesystem->gameMod(), "Error Importing Ship", MessageBoxButtons::OK, MessageBoxIcon::Error);
 | 
        
           |  |  | 223 | 			return;
 | 
        
           |  |  | 224 | 		}
 | 
        
           | 94 | cycrow | 225 | 		vfs->updateModTexts(17);
 | 
        
           | 36 | cycrow | 226 | 	}
 | 
        
           |  |  | 227 |   | 
        
           |  |  | 228 | 	// get the ship id to load
 | 
        
           | 94 | cycrow | 229 | 	String ^shipID = getShipSelection(vfs);
 | 
        
           | 36 | cycrow | 230 | 	if ( !shipID ) {
 | 
        
           |  |  | 231 | 		MessageBox::Show(this, "There was a problem trying to load ship data from VFS\n", "Error Importing Ship", MessageBoxButtons::OK, MessageBoxIcon::Error);
 | 
        
           |  |  | 232 | 	}
 | 
        
           |  |  | 233 | 	else if ( shipID->Length ) {
 | 
        
           |  |  | 234 | 		// import the ship
 | 
        
           | 94 | cycrow | 235 | 		Creator::ImportShip ^import = gcnew Creator::ImportShip(shipID, vfs);
 | 
        
           | 36 | cycrow | 236 | 		import->ShowDialog(this);
 | 
        
           |  |  | 237 |   | 
        
           |  |  | 238 | 		// create the new form with ship data
 | 
        
           |  |  | 239 | 		if ( !import->Error() ) {
 | 
        
           |  |  | 240 | 			PackageForm ^childForm = this->OpenPackage(true, import->GetShip(), "", "Imported Ship");
 | 
        
           |  |  | 241 | 			childForm->Text = "Imported Ship";
 | 
        
           | 50 | cycrow | 242 | 			import->GetShip()->adjustChanged(true);
 | 
        
           | 36 | cycrow | 243 | 			childForm->UpdateChanged();
 | 
        
           |  |  | 244 | 		}
 | 
        
           |  |  | 245 | 	}
 | 
        
           |  |  | 246 |   | 
        
           |  |  | 247 | 	// remove any mods that we have loaded
 | 
        
           | 94 | cycrow | 248 | 	vfs->clearMods();
 | 
        
           | 36 | cycrow | 249 | }
 | 
        
           |  |  | 250 |   | 
        
           |  |  | 251 | } //NAMESPACE
 |