Subversion Repositories spk

Rev

Rev 112 | Rev 116 | Go to most recent revision | 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();
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
 
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
 
122
String ^Form1::getShipSelection(CVirtualFileSystem *pVfs)
123
{
101 cycrow 124
	Utils::CStringList *ships = pVfs->getTShipsEntries();
125
	if ( ships ) {
126
		this->LoadText(pVfs);
94 cycrow 127
 
36 cycrow 128
		LoadShip ^ls = gcnew LoadShip();
112 cycrow 129
		for(Utils::SStringList *str = ships->first(); str; str = ships->next()) {
101 cycrow 130
			int tId = str->str.token(";", 7).toLong();
94 cycrow 131
			String ^name = _US(pVfs->findText(0, 17, tId));
115 cycrow 132
			String ^race = (str->str.token(";", 46).toLong() == 0) ? "" : _US(pVfs->findText(0, 1266, str->str.token(";", 46).toLong()));
94 cycrow 133
			String ^text = race + " " + name;
101 cycrow 134
			int variation = str->str.token(";", 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 )	{
101 cycrow 141
			Utils::String line = pVfs->getTShipsEntry(_S(ls->GetData()));
142
 
143
			if ( !line.empty() ) {
36 cycrow 144
				return ls->GetData();
145
			}
146
		}
147
		else return "";
148
	}
149
 
150
	return nullptr;
151
}
152
 
153
void Form1::ImportShip()
154
{
155
	OpenFileDialog ^ofd = gcnew OpenFileDialog();
156
	ofd->Filter = "X Catalog Files (*.cat)|*.cat";
157
	ofd->Title = "Select the mod file to import a ship from";
158
	ofd->FilterIndex = 1;
159
	ofd->RestoreDirectory = true;
160
	ofd->Multiselect = false;
161
 
162
	CVirtualFileSystem *vfs = new CVirtualFileSystem();
163
 
164
	if ( ofd->ShowDialog(this) == System::Windows::Forms::DialogResult::OK )
165
	{
166
		if ( !vfs->loadMod(CyStringFromSystemString(ofd->FileName).ToString()) ) {
167
			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);
168
			return;
169
		}
170
		this->UseWaitCursor = true;
171
 
172
		vfs->updateModTexts(17);
173
 
174
		String ^shipID = getShipSelection(vfs);
39 cycrow 175
		if ( !shipID ) {
77 cycrow 176
			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 177
		}
178
		else if ( shipID->Length ) {
36 cycrow 179
			String ^loadMod = ofd->FileName;
180
			CXspFile *newShip = NULL;
181
 
182
			while ( true ) {
183
				Creator::ImportShip ^import = gcnew Creator::ImportShip(shipID, vfs);
184
				import->ShowDialog(this);
185
				newShip = (import->Error()) ? NULL : import->GetShip();
186
				if ( !newShip ) {
187
					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 ) {
188
						// load in another file
189
						if ( ofd->ShowDialog(this) == System::Windows::Forms::DialogResult::OK ) {
190
							loadMod = ofd->FileName;
191
							if ( !vfs->loadMod(CyStringFromSystemString(loadMod).ToString()) ) {
192
								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);
193
								break;
194
							}
195
						}
196
						else break;
197
					}
198
					else break;
199
				}
200
				else break;	
201
			}
202
 
203
			if ( newShip ) {
204
				PackageForm ^childForm = this->OpenPackage(true, newShip, "", "Imported Ship");
205
				childForm->Text = "Imported Ship";
50 cycrow 206
				newShip->adjustChanged(true);
36 cycrow 207
				childForm->UpdateChanged();
107 cycrow 208
				childForm->WindowState = Windows::Forms::FormWindowState::Maximized;
36 cycrow 209
			}
210
 
211
		}
212
 
213
		this->UseWaitCursor = false;
214
	}
215
 
216
	delete vfs;
217
}
218
 
219
 
220
void Form1::ImportShipFromVFS()
221
{
222
	// some way to select the VFS to use
94 cycrow 223
	SelectFilesystem ^Filesystem = gcnew SelectFilesystem(_pGameDir);
36 cycrow 224
	if ( Filesystem->ShowDialog(this) != System::Windows::Forms::DialogResult::OK ) {
225
		return;
226
	}
227
 
94 cycrow 228
	Utils::String dir = Filesystem->gameDir();
36 cycrow 229
	CyString mod;
230
	if ( Filesystem->gameMod() ) {
94 cycrow 231
		mod = CyString(dir) + "/mods/" + CyStringFromSystemString(Filesystem->gameMod()) + ".cat";
36 cycrow 232
	}
233
 
94 cycrow 234
	CVirtualFileSystem *vfs = _pGameDir->selectedVFS();
235
 
236
	if ( !vfs ) {
237
		MessageBox::Show(this, "Error: Couldn't open Virtual File System", "Error Importing Ship", MessageBoxButtons::OK, MessageBoxIcon::Error);
238
		return;
239
	}
240
 
36 cycrow 241
	// load a mod on top of the VFS
242
	if ( !mod.Empty() ) {
94 cycrow 243
		if ( !vfs->loadMod(mod.ToString()) ) {
36 cycrow 244
			MessageBox::Show(this, "There was a problem trying to open the mod file\n" + Filesystem->gameMod(), "Error Importing Ship", MessageBoxButtons::OK, MessageBoxIcon::Error);
245
			return;
246
		}
94 cycrow 247
		vfs->updateModTexts(17);
36 cycrow 248
	}
249
 
250
	// get the ship id to load
94 cycrow 251
	String ^shipID = getShipSelection(vfs);
36 cycrow 252
	if ( !shipID ) {
253
		MessageBox::Show(this, "There was a problem trying to load ship data from VFS\n", "Error Importing Ship", MessageBoxButtons::OK, MessageBoxIcon::Error);
254
	}
255
	else if ( shipID->Length ) {
256
		// import the ship
94 cycrow 257
		Creator::ImportShip ^import = gcnew Creator::ImportShip(shipID, vfs);
36 cycrow 258
		import->ShowDialog(this);
259
 
260
		// create the new form with ship data
261
		if ( !import->Error() ) {
262
			PackageForm ^childForm = this->OpenPackage(true, import->GetShip(), "", "Imported Ship");
263
			childForm->Text = "Imported Ship";
50 cycrow 264
			import->GetShip()->adjustChanged(true);
36 cycrow 265
			childForm->UpdateChanged();
107 cycrow 266
			childForm->WindowState = Windows::Forms::FormWindowState::Maximized;
36 cycrow 267
		}
268
	}
269
 
270
	// remove any mods that we have loaded
94 cycrow 271
	vfs->clearMods();
36 cycrow 272
}
273
 
274
} //NAMESPACE