Subversion Repositories spk

Rev

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