Subversion Repositories spk

Rev

Rev 226 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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();
197 cycrow 20
	m_pLoadedList = new Utils::WStringList;
224 cycrow 21
	_pGameDir = new CGameDirectories(_WS(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;
264 cycrow 33
	m_pPackages->startup(L".", _WS(IO::Path::GetTempPath()), _WS(Environment::GetFolderPath(Environment::SpecialFolder::Personal)));
36 cycrow 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
 
110 cycrow 47
Form1::!Form1()
48
{
49
}
50
 
109 cycrow 51
bool Form1::CheckCommandArguments(array<System::String ^> ^args)
52
{
53
	for ( int i = 0; i < args->Length; i++ ) {
54
		String ^str = args[i];
55
		if ( !String::Compare(str, "--create", true) )
56
			return true;
57
		else if ( !String::Compare(str, "--extract", true) )
58
			return true;
59
		else if ( !String::Compare(str, "--extracthere", true) )
60
			return true;
61
		else if ( !String::Compare(str, "--export", true) )
62
			return true;
63
	}
64
 
65
	return false;
66
}
67
 
36 cycrow 68
void Form1::parseCommandArguments(array<System::String ^> ^args)
69
{
70
	int switchType = SWITCH_NONE;
71
 
72
	if ( args ) {
73
		ArrayList ^list = gcnew ArrayList();
74
		for ( int i = 0; i < args->Length; i++ ) {
75
			String ^str = args[i];
76
			if ( switchType != SWITCH_NONE ) {
77
				switch(switchType) {
78
					case SWITCH_CREATE:
79
						this->SavePackagerScript(str);
80
						m_bAutoClose = true;
81
						break;
82
					case SWITCH_EXTRACT:
83
						this->ExtractPackage(str, false);
84
						m_bAutoClose = true;
85
						break;
86
					case SWITCH_EXTRACTHERE:
87
						this->ExtractPackage(str, true);
88
						m_bAutoClose = true;
89
						break;
90
					case SWITCH_EXPORT:
91
						this->ExportPackage(str);
92
						m_bAutoClose = true;
93
						break;
94
				}
95
				switchType = SWITCH_NONE;
96
			}
97
			else if ( !String::Compare(str, "--create", true) ) {
98
				switchType = SWITCH_CREATE;			
99
			}
100
			else if ( !String::Compare(str, "--extract", true) ) {
101
				switchType = SWITCH_EXTRACT;
102
			}
103
			else if ( !String::Compare(str, "--extracthere", true) ) {
104
				switchType = SWITCH_EXTRACTHERE;
105
			}
106
			else if ( !String::Compare(str, "--export", true) ) {
107
				switchType = SWITCH_EXPORT;
108
			}
109
			else {
110
				list->Add(args[i]);
111
			}
112
		}
113
 
114
		if ( list->Count ) {
115
			this->OpenFiles(list, true, true);
116
			m_bAutoClose = false;
117
		}
118
	}
119
}
120
 
121
 
116 cycrow 122
String ^Form1::getShipSelection(CVirtualFileSystem *pVfs, System::String ^*shipName)
36 cycrow 123
{
197 cycrow 124
	Utils::WStringList *ships = pVfs->getTShipsEntries();
101 cycrow 125
	if ( ships ) {
126
		this->LoadText(pVfs);
94 cycrow 127
 
36 cycrow 128
		LoadShip ^ls = gcnew LoadShip();
197 cycrow 129
		for(Utils::WStringNode *str = ships->first(); str; str = ships->next()) {
130
			int tId = str->str.token(L";", 7).toLong();
94 cycrow 131
			String ^name = _US(pVfs->findText(0, 17, tId));
197 cycrow 132
			String ^race = (str->str.token(L";", 46).toLong() == 0) ? "" : _US(pVfs->findText(0, 1266, str->str.token(L";", 46).toLong()));
94 cycrow 133
			String ^text = race + " " + name;
197 cycrow 134
			int variation = str->str.token(L";", 51).toLong();
115 cycrow 135
			if ( variation && variation != 20 )
94 cycrow 136
				text = text + " " + _US(pVfs->findText(0, 17, 10000 + variation));
101 cycrow 137
			ls->AddShip(_US(str->data), text);
36 cycrow 138
		}
139
 
140
		if ( ls->ShowDialog(this) == System::Windows::Forms::DialogResult::OK )	{
224 cycrow 141
			Utils::WString line = pVfs->getTShipsEntry(_WS(ls->GetData()));
101 cycrow 142
 
143
			if ( !line.empty() ) {
116 cycrow 144
				if ( shipName ) {
145
					(*shipName) = ls->GetShipName();
146
				}
36 cycrow 147
				return ls->GetData();
148
			}
149
		}
150
		else return "";
151
	}
152
 
153
	return nullptr;
154
}
155
 
156
void Form1::ImportShip()
157
{
158
	OpenFileDialog ^ofd = gcnew OpenFileDialog();
159
	ofd->Filter = "X Catalog Files (*.cat)|*.cat";
160
	ofd->Title = "Select the mod file to import a ship from";
161
	ofd->FilterIndex = 1;
162
	ofd->RestoreDirectory = true;
163
	ofd->Multiselect = false;
164
 
165
	CVirtualFileSystem *vfs = new CVirtualFileSystem();
166
 
167
	if ( ofd->ShowDialog(this) == System::Windows::Forms::DialogResult::OK )
168
	{
225 cycrow 169
		if ( !vfs->loadMod(_WS(ofd->FileName)) ) {
36 cycrow 170
			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);
171
			return;
172
		}
173
		this->UseWaitCursor = true;
174
 
175
		vfs->updateModTexts(17);
176
 
116 cycrow 177
		String ^shipName = "";
178
		String ^shipID = getShipSelection(vfs, &shipName);
39 cycrow 179
		if ( !shipID ) {
77 cycrow 180
			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 181
		}
182
		else if ( shipID->Length ) {
36 cycrow 183
			String ^loadMod = ofd->FileName;
184
			CXspFile *newShip = NULL;
185
 
186
			while ( true ) {
116 cycrow 187
				Creator::ImportShip ^import = gcnew Creator::ImportShip(shipID, shipName, vfs);
36 cycrow 188
				import->ShowDialog(this);
189
				newShip = (import->Error()) ? NULL : import->GetShip();
190
				if ( !newShip ) {
191
					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 ) {
192
						// load in another file
193
						if ( ofd->ShowDialog(this) == System::Windows::Forms::DialogResult::OK ) {
194
							loadMod = ofd->FileName;
225 cycrow 195
							if ( !vfs->loadMod(_WS(loadMod)) ) {
36 cycrow 196
								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);
197
								break;
198
							}
199
						}
200
						else break;
201
					}
202
					else break;
203
				}
204
				else break;	
205
			}
206
 
207
			if ( newShip ) {
208
				PackageForm ^childForm = this->OpenPackage(true, newShip, "", "Imported Ship");
209
				childForm->Text = "Imported Ship";
50 cycrow 210
				newShip->adjustChanged(true);
36 cycrow 211
				childForm->UpdateChanged();
107 cycrow 212
				childForm->WindowState = Windows::Forms::FormWindowState::Maximized;
36 cycrow 213
			}
214
 
215
		}
216
 
217
		this->UseWaitCursor = false;
218
	}
219
 
220
	delete vfs;
221
}
222
 
223
 
224
void Form1::ImportShipFromVFS()
225
{
226
	// some way to select the VFS to use
94 cycrow 227
	SelectFilesystem ^Filesystem = gcnew SelectFilesystem(_pGameDir);
36 cycrow 228
	if ( Filesystem->ShowDialog(this) != System::Windows::Forms::DialogResult::OK ) {
229
		return;
230
	}
231
 
197 cycrow 232
	Utils::WString dir = Filesystem->gameDir();
233
	Utils::WString mod;
36 cycrow 234
	if ( Filesystem->gameMod() ) {
197 cycrow 235
		mod = dir + L"/mods/" + _WS(Filesystem->gameMod()) + L".cat";
36 cycrow 236
	}
237
 
94 cycrow 238
	CVirtualFileSystem *vfs = _pGameDir->selectedVFS();
239
 
240
	if ( !vfs ) {
241
		MessageBox::Show(this, "Error: Couldn't open Virtual File System", "Error Importing Ship", MessageBoxButtons::OK, MessageBoxIcon::Error);
242
		return;
243
	}
244
 
36 cycrow 245
	// load a mod on top of the VFS
197 cycrow 246
	if ( !mod.empty() ) {
247
		if ( !vfs->loadMod(mod) ) {
36 cycrow 248
			MessageBox::Show(this, "There was a problem trying to open the mod file\n" + Filesystem->gameMod(), "Error Importing Ship", MessageBoxButtons::OK, MessageBoxIcon::Error);
249
			return;
250
		}
94 cycrow 251
		vfs->updateModTexts(17);
36 cycrow 252
	}
253
 
254
	// get the ship id to load
116 cycrow 255
	String ^shipName = "";
256
	String ^shipID = getShipSelection(vfs, &shipName);
36 cycrow 257
	if ( !shipID ) {
258
		MessageBox::Show(this, "There was a problem trying to load ship data from VFS\n", "Error Importing Ship", MessageBoxButtons::OK, MessageBoxIcon::Error);
259
	}
260
	else if ( shipID->Length ) {
261
		// import the ship
116 cycrow 262
		Creator::ImportShip ^import = gcnew Creator::ImportShip(shipID, shipName, vfs);
36 cycrow 263
		import->ShowDialog(this);
264
 
265
		// create the new form with ship data
266
		if ( !import->Error() ) {
267
			PackageForm ^childForm = this->OpenPackage(true, import->GetShip(), "", "Imported Ship");
268
			childForm->Text = "Imported Ship";
50 cycrow 269
			import->GetShip()->adjustChanged(true);
36 cycrow 270
			childForm->UpdateChanged();
107 cycrow 271
			childForm->WindowState = Windows::Forms::FormWindowState::Maximized;
36 cycrow 272
		}
273
	}
274
 
275
	// remove any mods that we have loaded
94 cycrow 276
	vfs->clearMods();
36 cycrow 277
}
278
 
279
} //NAMESPACE