Subversion Repositories spk

Rev

Rev 294 | Rev 306 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
/*
2
 SPKTool V1.00 Created by Cycrow (Matthew Gravestock)
3
*/
4
 
5
// Main Spk File Library Include
6
#ifdef _WIN32
7
#include <spk.h>
8
#include <MultiSpkFile.h>
9
#include <spkcmdprogress.h>
10
#else
11
#include "../spk/spk.h"
12
#include "../spk/MultiSpkFile.h"
13
#include "../spk/spkcmdprogress.h"
14
#endif
15
// Multi Spk File format, required if using Multi Spk files
16
// Displays 7Zip compression progress to the command line
17
#include <time.h>
18
#ifdef _WIN32
19
#include <windows.h>
20
#include <direct.h>
21
#include <shlobj.h>
22
#endif
23
 
197 cycrow 24
Utils::WString g_dir;
1 cycrow 25
bool g_read;
26
 
134 cycrow 27
class CFileProgress : public CProgressInfo
28
{
29
private:
298 cycrow 30
	Utils::WString _display;
134 cycrow 31
	CBaseFile *_package;
32
	CPackages *_packages;
33
 
34
public:
298 cycrow 35
	CFileProgress(CPackages *packages, CBaseFile *package, const Utils::WString &display) : 
134 cycrow 36
		_display(display),
37
		_packages(packages),
38
		_package(package)
39
	{
40
 
41
	}
42
 
43
protected:
44
	void ProgressUpdated(const long cur, const long max)
45
	{
46
	}
47
	void DoingFile(C_File *file)
48
	{
197 cycrow 49
		Utils::WString game;
134 cycrow 50
 
51
		if (!file->game() || file->game() == GAME_ALLNEW)
197 cycrow 52
			game = L"All Games";
134 cycrow 53
		else if (_packages)
54
		{
55
			for (unsigned int i = 0; i < 31; ++i)
56
			{
57
				if (file->game() & (1 << i))
58
				{
197 cycrow 59
					Utils::WString g = _packages->GetGameExe()->gameNameFromType(i - 1);
134 cycrow 60
					if (!g.empty())
61
					{
62
						if (game.empty())
63
							game = g;
64
						else
197 cycrow 65
							game = game + L", " + g;
134 cycrow 66
					}
67
				}
68
			}
69
		}
70
 
71
		if(game.empty())
298 cycrow 72
			wprintf(L"%s: %s\n", _display.c_str(), file->getNameDirectory(_package).c_str());
134 cycrow 73
		else
298 cycrow 74
			wprintf(L"%s: %s [%s]\n", _display.c_str(), file->getNameDirectory(_package).c_str(), game.c_str());
134 cycrow 75
	}
76
 
77
};
78
 
1 cycrow 79
/*
80
	Func:	GetInput
81
	Desc:	Gets an input from the user, ie, any settings required to be typed in
82
*/
197 cycrow 83
Utils::WString GetInput ()
1 cycrow 84
{
85
	g_read = true;
86
 
298 cycrow 87
	Utils::WString line;
88
	wchar_t c = getchar();
1 cycrow 89
 
298 cycrow 90
	while ( (c != L'\n') && (c != L'\0') )
1 cycrow 91
	{
92
		line += c;
93
		c = getchar();
94
	}
95
 
298 cycrow 96
	return line;
1 cycrow 97
}
98
 
99
 
100
/*
101
	Func:	PrintSyntax
102
	Args:	String cmd - The command name to be displayed
103
	Desc:	Displays the syntax for the program
104
*/
298 cycrow 105
void PrintSyntax(const Utils::WString &cmd)
1 cycrow 106
{
298 cycrow 107
	wprintf(L"Syntax: %s <command>\n", cmd.c_str() );
108
	wprintf(L"Commands:\n" );
109
	wprintf(L"\t-v <spkfile>\n\t--view <spkfile>\n\t\tViews the contents of a spk file\n\n" );
110
	wprintf(L"\t-e <spkfile> [destination]\n\t--extractall <spkfile> [destination]\n\t\tExtracts all the files to the destination directory [or current if no destiantion is set]\n\n" );
111
	wprintf(L"\t-x <spkfile> <type> <file> [destination]\n\t--extract <spkfile> <type> <file> [destination]\n\t\tExtracts a single file of <type> to destination directory\n\n" );
112
	wprintf(L"\t-x <multispkfile> <file> [destination]\n\t--extractspk <multispkfile> <spkfile> [destination]\n\t\tExtracts a single spk file from a Multi-Spk Package\n\n" );
113
	wprintf(L"\t-c <spkfile>\n\t--create\n\t\tCreates a new spk file\n\n" );
114
	wprintf(L"\t-a <spkfile> <type> <filename>\n\t--append <spkfile> <type> <filename>\n\t\tAppends a file to the package of set <type>\n\n" );
115
	wprintf(L"\t-r <spkfile> <type> <filename>\n\t--remove <spkfile> <type> <filename>\n\t\tRemoves a file from the package of set <type>\n\n" );
116
	wprintf(L"\t-r <multispkfile> <filename>\n\t--removespk <multispkfile> <filename>\n\t\tRemoves a spk file from the Multi-SPK package\n\n" );
117
	wprintf(L"\t-m <spkfile> <spkfile>\n\t--mergemulti <spkfile> <spkfile>\n\t\tMerges spk files together, the second file will be merged into the first\n\n" );
118
	wprintf(L"\t-n <multispkfile>\n\t--createmulti\n\t\tCreates a multi spk file, and adds the spkfiles in\n\n" );
119
	wprintf(L"\t-s <multispkfile> [destination]\n\t--splitmulti <multispkfile> [destination]\n\t\tSplits a Multi-SPK file up, saves all the spk files to Destination\n\n" );
120
	wprintf(L"\t--set <setting> [value]\n\t\tChanges the settings of the script\n\n" );
121
	wprintf(L"\t--convertxsp <oldxsp> [newxsp]\n\t\tConverts an old XSP file into the new format\n\n");
122
	wprintf(L"\t--createscript <packagescript>\n\t\tCreates a spk file from a packager script\n\n");
123
	wprintf(L"\t--verifyscript <packagescript>\n\t\tChecks a packager script is valid without creating the resulting file\n\n");
124
	wprintf(L"\t--generatescript <package> [packagescript]\n\t\tCreates a packager script (.sps) from a spk file\n\n");
125
	wprintf(L"\t--extractship <modfile> <xspfile> [shipid]\n\t\tCreates an XSP ship file from a mod package\n\n");
126
	wprintf(L"\t--generateupdatefile <package>\n\t\tCreates an update file for the spk/xsp file\n\n");
127
	wprintf(L"\t--packagelist <filenames>\n\t\tThis will generate the update file to allow downloading packages from a server.\n\n");
126 cycrow 128
	//	printf ( "\t--convertxspwizard <oldxsp> [newxsp]\n\t\tConverts an old XSP file into the new format\n\n");
1 cycrow 129
}
130
 
226 cycrow 131
void Settings(const Utils::WString &filename, const Utils::WString &settings, int argc, char **argv, const Utils::WString &cmd)
1 cycrow 132
{
226 cycrow 133
	if ( settings.Compare(L"author") )
1 cycrow 134
	{
135
		if ( argc < 5 )
136
		{
226 cycrow 137
			wprintf(L"Syntax: %s -set <spkfile> Author <authorname>\n\tSets the authors name of the package\n", cmd.c_str() );
1 cycrow 138
			return;
139
		}
140
	}
141
	else
142
	{
226 cycrow 143
		wprintf(L"Error! Unknown settings: %s\n", settings.c_str());
1 cycrow 144
		return;
145
	}
146
 
147
	// first chekc if file even exists
226 cycrow 148
	FILE *id = _wfopen(filename.c_str(), L"rb+");
1 cycrow 149
	if ( !id )
150
	{
226 cycrow 151
		wprintf(L"Error: File, %s, doesn't exist\n", filename.c_str() );
1 cycrow 152
		return;
153
	}
154
	fclose ( id );
155
 
226 cycrow 156
	int check = CSpkFile::CheckFile(filename);
1 cycrow 157
	if ( check == SPKFILE_SINGLE )
158
	{
159
		CSpkFile spkfile;
226 cycrow 160
		wprintf(L"* Opening SPK File, %s...\n", filename.c_str());
161
		if ( !spkfile.readFile(filename, SPKREAD_ALL))
1 cycrow 162
		{
226 cycrow 163
			wprintf(L"Failed to open the spk files, %s\n", filename.c_str() );
1 cycrow 164
			return;
165
		}
166
 
167
		// now do the settings
226 cycrow 168
		if ( settings.Compare(L"author") )
1 cycrow 169
		{
226 cycrow 170
			spkfile.setAuthor(Utils::WString::FromString(argv[4]));
203 cycrow 171
			wprintf(L"Settings Author to: %s\n", spkfile.author().c_str());
1 cycrow 172
		}
173
 
226 cycrow 174
		spkfile.writeFile(filename);
1 cycrow 175
		printf ( "\nSPK file has been written sucessfully\n" );
176
	}
177
	else
226 cycrow 178
		wprintf(L"File, %s, is not a valid SPK file\n", filename.c_str() );
1 cycrow 179
}
180
 
298 cycrow 181
void SetRating (const Utils::WString &filename, int ease, int changing, int rec )
1 cycrow 182
{
183
	if ( ease > 5 )
184
		ease = 5;
185
	if ( changing > 5 )
186
		changing = 5;
187
	if ( changing > 5 )
188
		changing = 5;
189
 
298 cycrow 190
	FILE *id = _wfopen(filename.c_str(), L"rb+");
1 cycrow 191
	if ( !id )
192
	{
298 cycrow 193
		wprintf(L"Error: File, %s, doesn't exist\n", filename.c_str() );
1 cycrow 194
		return;
195
	}
196
	fclose ( id );
197
 
298 cycrow 198
	int check = CSpkFile::CheckFile(filename);
1 cycrow 199
	if ( check == SPKFILE_SINGLE )
200
	{
201
		CSpkFile spkfile;
298 cycrow 202
		wprintf(L"* Opening SPK File, %s...\n", filename.c_str() );
203
		if ( !spkfile.readFile(filename, SPKREAD_ALL))
1 cycrow 204
		{
298 cycrow 205
			wprintf(L"Failed to open the spk files, %s\n", filename.c_str() );
1 cycrow 206
			return;
207
		}
208
 
298 cycrow 209
		wprintf(L"File Format Version: %.2f\n", spkfile.fileVersion());
1 cycrow 210
 
298 cycrow 211
		wprintf(L"Assigning Ease Of Use:\t\t%d\nAssigning Game Changing:\t%d\nAssigning Recommended:\t\t%d\n", ease, changing, rec);
46 cycrow 212
		spkfile.setEaseOfUse(ease);
213
		spkfile.setGameChanging(changing);
214
		spkfile.setRecommended(rec);
298 cycrow 215
		spkfile.writeFile(filename);
216
		wprintf(L"\nSPK file has been written sucessfully\n" );
1 cycrow 217
	}
218
	else
298 cycrow 219
		wprintf(L"File, %s, is not a valid SPK file\n", filename.c_str() );
1 cycrow 220
}
221
 
197 cycrow 222
Utils::WString GetTerranConflictDir()
1 cycrow 223
{
224
	while ( true )
225
	{
226
		printf ( "\nPlease enter the directory for terran conflict: ");
197 cycrow 227
		Utils::WString dir = GetInput();
185 cycrow 228
		if ( dir.empty() )
197 cycrow 229
			return Utils::WString::Null();
1 cycrow 230
 
231
		// check if the directory exists
197 cycrow 232
		if ( CFileIO::Exists(dir + L"/x3tc.exe") )
1 cycrow 233
			return dir;
197 cycrow 234
		wprintf(L"\nTerran Conflict installation doesn't exist: %s\n\n", dir.c_str());
1 cycrow 235
	}
236
 
197 cycrow 237
	return Utils::WString::Null();
1 cycrow 238
}
239
 
298 cycrow 240
void ConvertXsp(const Utils::WString &filename, const Utils::WString &tofile, bool wizard)
1 cycrow 241
{
242
	// first chekc if file even exists
218 cycrow 243
	FILE *id = _wfopen (filename.c_str(), L"rb+" );
1 cycrow 244
	if ( !id )
245
	{
218 cycrow 246
		wprintf(L"Error: File, %s, doesn't exist\n", filename.c_str() );
1 cycrow 247
		return;
248
	}
249
	fclose ( id );
250
 
251
	// check its not a new format already
185 cycrow 252
	int check = CSpkFile::CheckFile(filename);
196 cycrow 253
	if ( check == SPKFILE_INVALID && CFileIO(filename).isFileExtension(L"xsp") )
1 cycrow 254
	{
218 cycrow 255
		wprintf(L"* Converting XSP File, %s... ", filename.c_str() );
1 cycrow 256
		CXspFile xspFile;
218 cycrow 257
		if ( !xspFile.convertOld(filename) )
1 cycrow 258
		{
218 cycrow 259
			wprintf(L"[ERROR]\n\nFailed to convert old xsp file, %s\n", filename.c_str() );
1 cycrow 260
			return;
261
		}
262
 
263
		printf ( "[DONE]\n");
264
 
197 cycrow 265
		Utils::WString dir;
1 cycrow 266
		printf ("Do you want to create weapon masks for Terran conflict? ");
197 cycrow 267
		Utils::WString input = GetInput();
268
		if ( input.Compare(L"y") || input.Compare(L"yes") )
1 cycrow 269
		{
270
			dir = GetTerranConflictDir();
197 cycrow 271
			if (!dir.empty())
1 cycrow 272
			{
273
			}
274
		}
275
 
298 cycrow 276
		wprintf(L"* Writing XSP File, %s... ", tofile.c_str());
1 cycrow 277
		// now we need to save it
185 cycrow 278
		if ( !xspFile.writeFile(tofile) )
298 cycrow 279
			wprintf(L"[ERROR]\n\nFailed to write file to, %s\n", tofile.c_str() );
1 cycrow 280
		else
298 cycrow 281
			wprintf(L"[DONE]\n\nFile: %s, has been converted to, %s\n", filename.c_str(), tofile.c_str() );
1 cycrow 282
	}
283
	else
218 cycrow 284
		wprintf(L"Error: File, %s, is not an old style XSP file\n", filename.c_str() );
1 cycrow 285
}
286
 
298 cycrow 287
void ExtractShip(const Utils::WString &catfile, const Utils::WString &to, Utils::WString shipid)
1 cycrow 288
{
185 cycrow 289
	if ( !CFileIO::Exists(catfile) )
1 cycrow 290
	{
298 cycrow 291
		wprintf(L"Error: Mod File, %s, does not exist\n", catfile.c_str());
1 cycrow 292
		return;
293
	}
294
 
36 cycrow 295
	CVirtualFileSystem *pVfs = new CVirtualFileSystem();
185 cycrow 296
	if ( !pVfs->loadMod(catfile) ) {
298 cycrow 297
		wprintf(L"Error: Unable to read mod file, %s\n", catfile.c_str());
36 cycrow 298
		delete pVfs;
1 cycrow 299
		return;
300
	}
301
 
197 cycrow 302
	Utils::WStringList *ships = pVfs->getTShipsEntries();
1 cycrow 303
	if ( !ships )
304
	{
298 cycrow 305
		wprintf(L"Error: Failed to read %s::types\\TShips.pck\n", catfile.c_str());
36 cycrow 306
		delete pVfs;
1 cycrow 307
		return;
308
	}
309
 
310
	// now get the ship line to extract
197 cycrow 311
	Utils::WString tShipsLine;
185 cycrow 312
	if (shipid.empty())
1 cycrow 313
	{
314
		// no ship id, lets get one
197 cycrow 315
		Utils::WString ids[2];
1 cycrow 316
		int i = 0;
317
		int id = 0;
197 cycrow 318
		for ( Utils::WStringNode *str = ships->first(); str; str = ships->next() )
1 cycrow 319
		{
116 cycrow 320
			ids[i++] = str->data;
1 cycrow 321
			if ( i >= 2 )
322
			{
197 cycrow 323
				wprintf(L"[%3d][%30s]\t[%3d][%30s]\n", id - 1, ids[0].c_str(), id, ids[1].c_str());
1 cycrow 324
				for ( i = 0; i < 2; i++ )
197 cycrow 325
					ids[i] = L"";
1 cycrow 326
				i = 0;
327
			}
328
			++id;
329
		}
330
		if ( i == 1 )
197 cycrow 331
			wprintf(L"[%3d][%30s]\n", id, ids[0].c_str());
1 cycrow 332
		printf("Enter ID to use > ");
333
		shipid = GetInput();
334
 
185 cycrow 335
		if ( shipid.isNumber() )
336
			tShipsLine = ships->get(shipid.toInt())->str;
1 cycrow 337
		else
185 cycrow 338
			tShipsLine = ships->findData(shipid, true);
1 cycrow 339
	}
340
	else
185 cycrow 341
		ships->findData(shipid, true);
1 cycrow 342
 
101 cycrow 343
	if ( tShipsLine.empty() )
1 cycrow 344
	{
197 cycrow 345
		wprintf(L"Error, %s is not a valid ship id to use\n", shipid.c_str());
36 cycrow 346
		delete pVfs;
1 cycrow 347
		return;
348
	}
349
 
350
	// now we have the tships to use, lets start to extract the ship
298 cycrow 351
	wprintf(L"Extracting ship from %s, Ship=%s\n", catfile.c_str(), tShipsLine.token(L";", -2).c_str());
1 cycrow 352
 
353
	CXspFile ship;
197 cycrow 354
	if ( ship.extractShip(pVfs, tShipsLine.token(L";", -2).toString(), 0))
1 cycrow 355
	{
356
		printf ( "Ship has been successfully extracted\n" );
357
 
170 cycrow 358
		printf("\t- Models:\t %d\n", ship.countFiles(FILETYPE_SHIPMODEL));
359
		printf("\t- Textures:\t %d\n", ship.countFiles(FILETYPE_SHIPOTHER));
1 cycrow 360
		printf("\t- Dummies:\t %d\n", ship.GetDummies()->size());
361
		printf("\t- Components:\t %d\n", ship.GetComponents()->size());
170 cycrow 362
		printf("\t- Bodies:\t %d\n", ship.getBodies().size());
1 cycrow 363
		printf("\t- Cockpits:\t %d\n", ship.GetCockpits()->size());
364
		printf("\t- Text Entries:\t %d\n", ship.GetTexts()->size());
365
 
366
		printf("\nEnter the name for the ship: ");
197 cycrow 367
		ship.setName(GetInput().toString());
1 cycrow 368
		printf("Enter the author for the ship: ");
197 cycrow 369
		ship.setAuthor(GetInput().toString());
1 cycrow 370
		printf("Enter the version for the ship: V");
197 cycrow 371
		ship.setVersion(GetInput().toString());
1 cycrow 372
		printf("Enter the description for the ship: ");
197 cycrow 373
		ship.setDescription(GetInput().toString());
1 cycrow 374
 
375
		struct tm   *currDate;
376
		char    dateString[100];
377
		time_t now = time(NULL);
378
		currDate = localtime( &now );
379
		strftime(dateString, sizeof dateString, "%d/%m/%Y", currDate);
226 cycrow 380
		ship.setCreationDate(Utils::WString::FromString(dateString));
1 cycrow 381
 
298 cycrow 382
		wprintf(L"\nWriting file: %s...\n", to.c_str());
185 cycrow 383
		if ( ship.writeFile(to) )
298 cycrow 384
			wprintf(L"Ship file has been created: %s\n", to.c_str());
1 cycrow 385
		else
298 cycrow 386
			wprintf(L"Error: Unable to write ship file, %s\n", to.c_str());
1 cycrow 387
	}
388
	else
298 cycrow 389
		wprintf(L"Error: Unable to extract the ship\n");
36 cycrow 390
 
391
	delete pVfs;
1 cycrow 392
}
393
 
298 cycrow 394
Utils::WString GetGameName(int game)
1 cycrow 395
{
396
	switch(game)
397
	{
398
		case GAME_X2:
298 cycrow 399
			return L"X2: The Threat";
1 cycrow 400
		case GAME_X3:
298 cycrow 401
			return L"X3: Reunion";
1 cycrow 402
		case GAME_X3TC:
298 cycrow 403
			return L"X3: Terran Conflict";
1 cycrow 404
		case GAME_X3AP:
298 cycrow 405
			return L"X3: Albion Prelude";
126 cycrow 406
		case GAME_X3FL:
298 cycrow 407
			return L"X3: Farnham's Legacy";
1 cycrow 408
	}
298 cycrow 409
	return L"Unknown";
1 cycrow 410
}
298 cycrow 411
void DisplayVersion(const Utils::WString &filename)
1 cycrow 412
{
413
	// first chekc if file even exists
298 cycrow 414
	FILE *id = _wfopen(filename.c_str(), L"rb+");
1 cycrow 415
	if ( !id )
416
	{
298 cycrow 417
		wprintf(L"Error: File, %s, doesn't exist\n", filename.c_str());
1 cycrow 418
		return;
419
	}
420
	fclose ( id );
421
 
422
	bool read = false;
423
	CBaseFile *pBaseFile = NULL;
424
 
425
	int check = CSpkFile::CheckFile ( filename );
426
	if ( check == SPKFILE_BASE )
427
	{
428
		pBaseFile = new CBaseFile();
298 cycrow 429
		wprintf(L"* Opening SPK File, %s...\n", filename.c_str() );
175 cycrow 430
		if ( !pBaseFile->readFile(filename, SPKREAD_NODATA))
1 cycrow 431
		{
298 cycrow 432
			wprintf(L"Failed to open the spk files, %s\n", filename.c_str() );
1 cycrow 433
			return;
434
		}
435
		read = true;
436
	}
437
	else if ( check == SPKFILE_SINGLE )
438
	{
439
		pBaseFile = (CBaseFile *)new CSpkFile();
298 cycrow 440
		wprintf(L"* Opening SPK File, %s...\n", filename.c_str() );
175 cycrow 441
		if ( !pBaseFile->readFile(filename, SPKREAD_NODATA))
1 cycrow 442
		{
298 cycrow 443
			wprintf(L"Failed to open the spk files, %s\n", filename.c_str() );
1 cycrow 444
			return;
445
		}
446
		read = true;
447
	}
448
	else if ( check == SPKFILE_SINGLESHIP )
449
	{
450
		pBaseFile = (CBaseFile *)new CXspFile();
298 cycrow 451
		wprintf(L"* Opening XSP File, %s...\n", filename.c_str() );
175 cycrow 452
		if ( !pBaseFile->readFile(filename, SPKREAD_NODATA))
1 cycrow 453
		{
298 cycrow 454
			wprintf(L"Failed to open the xsp files, %s\n", filename.c_str() );
1 cycrow 455
			return;
456
		}
457
		read = true;
458
	}
459
	// otherwise its an old ship file
196 cycrow 460
	else if ( CFileIO(filename).isFileExtension(L"xsp") )
1 cycrow 461
	{
298 cycrow 462
		wprintf(L"* Converting XSP File, %s...\n", filename.c_str() );
1 cycrow 463
		pBaseFile = new CXspFile;
218 cycrow 464
		if ( !((CXspFile *)pBaseFile)->convertOld(filename) )
1 cycrow 465
		{
466
			delete pBaseFile;
467
			pBaseFile = NULL;
298 cycrow 468
			wprintf(L"Failed to convert old xsp file, %s\n", filename.c_str() );
1 cycrow 469
			return;
470
		}
471
		check = SPKFILE_SINGLESHIP;
472
		read = true;
473
	}
474
 
475
	if ( pBaseFile && read)
476
	{
477
		CPackages p;
226 cycrow 478
		p.startup(L".", L".", L".");
1 cycrow 479
 
480
		CSpkFile *pSpkFile = NULL;
481
		CXspFile *pXspFile = NULL;
482
		if ( check == SPKFILE_SINGLE )
483
			pSpkFile = (CSpkFile *)pBaseFile;
484
		else
485
			pXspFile = (CXspFile *)pBaseFile;
486
 
298 cycrow 487
		wprintf(L"File Format Version: %.2f\n", pBaseFile->fileVersion());
1 cycrow 488
 
226 cycrow 489
		Utils::WString sType;
1 cycrow 490
		if ( check == SPKFILE_SINGLESHIP )
226 cycrow 491
			sType = L"Ship";
1 cycrow 492
		else
226 cycrow 493
			sType = L"Package";
1 cycrow 494
 
226 cycrow 495
		wprintf(L"%s Name: %s\n", sType.c_str(), pBaseFile->name().c_str() );
49 cycrow 496
		if ( !pBaseFile->email().empty() )
226 cycrow 497
			wprintf(L"%s Author: %s (%s)\n", sType.c_str(), pBaseFile->author().c_str(), pBaseFile->email().c_str() );
1 cycrow 498
		else
226 cycrow 499
			wprintf(L"%s Author: %s\n", sType.c_str(), pBaseFile->author().c_str() );
500
		if ( !pBaseFile->version().empty() ) wprintf(L"%s Version: %s\n", sType.c_str(), pBaseFile->version().c_str() );
1 cycrow 501
 
502
		//for game
503
		printf("For Games: ");
504
		if ( !pBaseFile->AnyGameCompatability() )
505
			printf("All");
506
		else
197 cycrow 507
			wprintf(L"%s", p.getGameTypesString(pBaseFile, true).c_str());
1 cycrow 508
		printf("\n");
509
 
510
		if ( pXspFile )
511
		{
203 cycrow 512
			wprintf(L"Ship Display Name: %s\n", pXspFile->shipName(44).c_str());
1 cycrow 513
			if ( pXspFile->IsExistingShip() )
217 cycrow 514
				wprintf(L"Ship ID: %s (Replace Existing Ship)\n", pXspFile->shipID().c_str() );
1 cycrow 515
			else
217 cycrow 516
				wprintf(L"Ship ID: %s\n", pXspFile->shipID().c_str() );
1 cycrow 517
 
244 cycrow 518
			if ( pXspFile->anyShipyards() )
1 cycrow 519
			{
520
				printf ( "Add To Shipyards:\n" );
244 cycrow 521
				for (unsigned long i = 1; i <= GetMaxShipyards(); i *= 2 )
1 cycrow 522
				{
244 cycrow 523
					if ( pXspFile->isShipyard(static_cast<ShipyardRace>(i)) )
524
						wprintf(L"\t%s\n", GetShipyardName(static_cast<ShipyardRace>(i)).c_str());
1 cycrow 525
				}
526
			}
527
 
528
			for ( SWeaponMask *text = pXspFile->GetLaserMasks()->First(); text; text = pXspFile->GetLaserMasks()->Next() )
298 cycrow 529
				wprintf(L"Laser Mask, Game: %s, Mask: %d\n", GetGameName(text->iGame).c_str(), text->iMask);
1 cycrow 530
			for ( SWeaponMask *text = pXspFile->GetMissileMasks()->First(); text; text = pXspFile->GetMissileMasks()->Next() )
298 cycrow 531
				wprintf(L"Missile Mask, Game: %s, Mask: %d\n", GetGameName(text->iGame).c_str(), text->iMask);
1 cycrow 532
 
533
			if ( pXspFile->GetOriginalDescription() )
298 cycrow 534
				wprintf(L"Use Existing Text, ID: %d\n", pXspFile->GetOriginalDescription());
1 cycrow 535
 
536
			if ( pXspFile->AnyTexts() )
537
			{
538
				for ( SText *text = pXspFile->GetTexts()->First(); text; text = pXspFile->GetTexts()->Next() )
539
				{
298 cycrow 540
					wprintf(L"Ship Text, Language: %d\n", text->iId);
39 cycrow 541
					if ( !text->sName.empty() )
216 cycrow 542
						wprintf(L"\tName: %s\n", text->sName.c_str());
39 cycrow 543
					if ( !text->sDesc.empty() )
216 cycrow 544
						wprintf(L"\tDescription: %s\n", text->sDesc.c_str());
1 cycrow 545
				}
546
			}
547
 
548
			if ( pXspFile->AnyComponents() )
549
			{
298 cycrow 550
				wprintf(L"Component Entries:\n" );
1 cycrow 551
				for ( SComponent *c = pXspFile->GetComponents()->First(); c; c = pXspFile->GetComponents()->Next() )
216 cycrow 552
					wprintf(L"\t%s\n\t\t%s\n\t\t\t%s\n", c->sSection.c_str(), c->sSection2.c_str(), c->sData.c_str());
1 cycrow 553
			}
554
			if ( pXspFile->AnyDummies() )
555
			{
298 cycrow 556
				wprintf(L"Dummy Entries:\n" );
1 cycrow 557
				for ( SDummy *d = pXspFile->GetDummies()->First(); d; d = pXspFile->GetDummies()->Next() )
216 cycrow 558
					wprintf(L"\t%s\n\t\t%s\n", d->sSection.c_str(), d->sData.c_str());
1 cycrow 559
			}
560
			if ( pXspFile->AnyCockpits() )
561
			{
298 cycrow 562
				wprintf(L"Cockpit Entries:\n" );
1 cycrow 563
				for ( SCockpit *c = pXspFile->GetCockpits()->First(); c; c = pXspFile->GetCockpits()->Next() )
216 cycrow 564
					wprintf(L"\t%s\n", c->sCockpit.c_str());
1 cycrow 565
			}
165 cycrow 566
			if ( pXspFile->anyCutData() )
1 cycrow 567
			{
298 cycrow 568
				wprintf(L"CutData Entries [%d]:\n", pXspFile->getCutData().size() );
165 cycrow 569
				for(auto itr = pXspFile->getCutData().begin(); itr != pXspFile->getCutData().end(); itr++)
216 cycrow 570
					wprintf(L"\t%s\n", (*itr)->str.c_str());
1 cycrow 571
			}
572
		}
573
 
204 cycrow 574
		if ( !pBaseFile->creationDate().empty() ) wprintf(L"Creation Date: %s\n", pBaseFile->creationDate().c_str() );
206 cycrow 575
		if ( !pBaseFile->description().empty() ) wprintf(L"Description: %s\n", pBaseFile->description().c_str() );
1 cycrow 576
		if ( pSpkFile )
577
		{
578
			if ( pSpkFile->IsLibrary() )
579
				printf ( "Script Type: Library\n" );
580
			else if ( pSpkFile->IsPatch() )
581
				printf ( "Script Type: Patch Mod\n" );
582
			else if ( pSpkFile->IsCustomStart() )
583
				printf ( "Script Type: Custom Start\n" );
584
			else if ( pSpkFile->IsPackageUpdate() )
585
				printf ( "Script Type: Package Update\n" );
214 cycrow 586
			else if ( !pSpkFile->scriptTypeString(44).empty() )
587
				wprintf(L"Script Type: %s\n", pSpkFile->scriptTypeString(44).c_str() );
1 cycrow 588
 
589
			if ( !pSpkFile->GetWaresList()->empty() )
590
			{
591
				for ( CListNode<SWares> * wNode = pSpkFile->GetWaresList()->Front(); wNode; wNode = wNode->next() )
592
				{
593
					SWares *w = wNode->Data();
197 cycrow 594
					wprintf (L"Ware: (%c) %s\n", w->cType, CSpkFile::GetWareText(w, 44).c_str() );
595
					Utils::WString desc = CSpkFile::GetWareDesc(w, 44);
596
					if (!desc.empty())
597
						wprintf(L"\tDescription: %s\n", desc.c_str() );
1 cycrow 598
				}
599
			}
600
		}
206 cycrow 601
		if ( !pBaseFile->forumLink().empty() )	wprintf(L"Forum Link: %s\n", pBaseFile->forumLink().c_str() );
602
		if ( !pBaseFile->webSite().empty() )	wprintf(L"Web Site Address: %s\n", pBaseFile->webSite().c_str() );
603
		if ( !pBaseFile->webAddress().empty() ) wprintf(L"Update Address: %s\n", pBaseFile->webAddress().c_str() );
1 cycrow 604
 
162 cycrow 605
		if ( pBaseFile->anyWebMirrors() )
1 cycrow 606
		{
607
			printf("Update Mirror Addresses:\n");
294 cycrow 608
			for (size_t i = 0; i < pBaseFile->getMaxWebMirrors(); i++ )
208 cycrow 609
				wprintf(L"\t%s\n", pBaseFile->getWebMirror(i).c_str());
1 cycrow 610
		}
611
		if ( pSpkFile )
612
		{
214 cycrow 613
			if ( (!pSpkFile->otherName().empty()) && (!pSpkFile->otherAuthor().empty()) )
614
				wprintf(L"Script is a child to the mod: %s by %s\n", pSpkFile->otherName().c_str(), pSpkFile->otherAuthor().c_str() );
1 cycrow 615
		}
616
 
617
		if ( pBaseFile->AnyDependacies() )
618
		{
619
			printf ( "Required Dependacies:\n" );
620
			for ( SNeededLibrary *needed = pBaseFile->GetNeededLibraries()->First(); needed; needed = pBaseFile->GetNeededLibraries()->Next() )
204 cycrow 621
				wprintf(L"\t%s by %s (Minimum Version=%s)\n", needed->sName.c_str(), needed->sAuthor.c_str(), needed->sMinVersion.c_str() );
1 cycrow 622
		}
46 cycrow 623
		if ( pBaseFile->easeOfUse() != -1 ) printf ( "Ease Of Use Rating: %d\n", pBaseFile->easeOfUse() );
624
		if ( pBaseFile->gameChanging() != -1 ) printf ( "Game Changing Rating: %d\n", pBaseFile->gameChanging() );
625
		if ( pBaseFile->recommended() != -1 ) printf ( "Recommendation Rating: %d\n", pBaseFile->recommended() );
170 cycrow 626
		if (pBaseFile->icon())
197 cycrow 627
			wprintf(L"Icon File Found, Type: %s, Size: %s\n", pBaseFile->iconExt().c_str(), pBaseFile->icon()->dataSizeString().c_str() );
1 cycrow 628
 
629
		if ( pBaseFile->GetFileList()->size() )
630
		{
134 cycrow 631
			printf("\nListing files in package:\n");
1 cycrow 632
			CLinkList<C_File> *list = pBaseFile->GetFileList();
134 cycrow 633
 
634
			for (unsigned int game = 0; game < 31; ++game)
635
			{
636
				bool heading = true;
637
				for (C_File *file = list->First(); file; file = list->Next())
638
				{
639
					bool display = false;
640
					if (game == 0 && (!file->game() || file->game() == GAME_ALLNEW))
641
						display = true;
642
					if (game > 0 && file->game() & (1 << game))
643
						display = true;
644
 
645
					if (display)
646
					{
647
						if (heading)
648
						{
197 cycrow 649
							Utils::WString sGame = p.GetGameExe()->gameNameFromType(game - 1);
650
							wprintf(L"\tGame: %s\n", sGame.c_str());
134 cycrow 651
							heading = false;
652
						}
197 cycrow 653
						wprintf(L"\t\t%s (%s) Size: %s\n", file->getNameDirectory(pBaseFile).c_str(), file->fileTypeString().c_str(), file->dataSizeString().c_str());
134 cycrow 654
 
655
					}
656
				}
657
			}
658
 
1 cycrow 659
		}
660
		else
661
			printf ( "\nThere are currently no files in the package\n" );
662
	}
663
	else if ( check == SPKFILE_MULTI )
664
	{
665
		CMultiSpkFile spkfile;
298 cycrow 666
		wprintf(L"* Opening Multi-SPK file, %s...\n", filename.c_str() );
175 cycrow 667
		if ( !pBaseFile->readFile(filename, false))
1 cycrow 668
		{
298 cycrow 669
			wprintf(L"Error: Failed to open the Multi-SPK file, %s\n", filename.c_str() );
1 cycrow 670
			return;
671
		}
672
 
220 cycrow 673
		wprintf(L"Multi Package Name: %s\n", spkfile.name().c_str() );
298 cycrow 674
		wprintf(L"Selection Mode: " );
177 cycrow 675
		if (spkfile.isSelection ())
298 cycrow 676
			wprintf(L"On\n" );
1 cycrow 677
		else
298 cycrow 678
			wprintf(L"Off\n" );
1 cycrow 679
 
680
		CLinkList<SMultiSpkFile> *list = spkfile.GetFileList();
681
		for ( SMultiSpkFile *ms = list->First(); ms; ms = list->Next() )
682
		{
220 cycrow 683
			wprintf(L"File, %s:\n", ms->sName.c_str() );
197 cycrow 684
			wprintf(L"\tSize: %s\n", SPK::GetSizeString (ms->lSize).c_str() );
176 cycrow 685
			if ( (!ms->sScriptName.empty()) && (!ms->sScriptAuthor.empty()) )
220 cycrow 686
				wprintf(L"\tScript: %s %s by %s\n", ms->sScriptName.c_str(), ms->sScriptVersion.c_str(), ms->sScriptAuthor.c_str() );
298 cycrow 687
			wprintf(L"\tDefault Install: " );
1 cycrow 688
			if ( ms->bOn )
298 cycrow 689
				wprintf(L"Yes\n" );
1 cycrow 690
			else
298 cycrow 691
				wprintf(L"No\n" );
1 cycrow 692
		}
693
	}
694
	else
298 cycrow 695
		wprintf(L"File, %s, is not a valid SPK file\n", filename.c_str() );
1 cycrow 696
 
697
	if ( pBaseFile )
698
		delete pBaseFile;
699
}
700
 
298 cycrow 701
void AppendFile (const Utils::WString &sfile, const Utils::WString &type, const Utils::WString &addfile )
1 cycrow 702
{
298 cycrow 703
	int t = GetFileTypeFromString(type);
1 cycrow 704
	if ( t == -1 )
705
	{
298 cycrow 706
		wprintf(L"The file type \"%s\" is invalid\n", type.c_str());
1 cycrow 707
		return;
708
	}
709
 
298 cycrow 710
	int check = CSpkFile::CheckFile(sfile);
1 cycrow 711
	CBaseFile *pBaseFile = 0;
712
	if ( check == SPKFILE_SINGLE )
713
		pBaseFile = new CSpkFile();
714
	else if ( check == SPKFILE_SINGLESHIP )
715
		pBaseFile = new CXspFile();
716
	else if ( check == SPKFILE_BASE )
717
		pBaseFile = new CBaseFile();
718
 
719
	if ( !pBaseFile )
720
	{
298 cycrow 721
		wprintf(L"Error: Invalid file format, unable to open\n");
1 cycrow 722
		return;
723
	}
724
 
298 cycrow 725
	wprintf(L"Opening File, %s... ", sfile.c_str());
726
	if ( !pBaseFile->readFile(sfile))
1 cycrow 727
	{
298 cycrow 728
		wprintf(L"(Error)\nUnable to open the file, %s\n", sfile.c_str());
1 cycrow 729
		return;
730
	}
298 cycrow 731
	wprintf(L"(Done)\n" );
1 cycrow 732
 
733
	MyProgress progress(0);
734
 
298 cycrow 735
	wprintf(L"Adding file %s to package\n\t>", addfile.c_str() );
1 cycrow 736
 
298 cycrow 737
	if ( pBaseFile->appendFile(addfile, t, 0, false, Utils::WString::Null(), &progress))
1 cycrow 738
	{
739
		progress.PrintDone();
298 cycrow 740
		wprintf(L"< (Done)\n");
1 cycrow 741
 
298 cycrow 742
		pBaseFile->writeFile(sfile);
743
		wprintf(L"\nFile has been written sucessfully\n");
1 cycrow 744
	}
745
	else
298 cycrow 746
		wprintf(L"< (Error)\n");
1 cycrow 747
}
748
 
298 cycrow 749
void RemoveFile(const Utils::WString &sfile, const Utils::WString &type, const Utils::WString &addfile)
1 cycrow 750
{
298 cycrow 751
	FILE *id = _wfopen(sfile.c_str(), L"rb+");
1 cycrow 752
	if ( !id )
753
	{
298 cycrow 754
		wprintf(L"Error: File, %s, doesn't exist\n", sfile.c_str() );
1 cycrow 755
		return;
756
	}
757
 
298 cycrow 758
	if ( addfile.empty() )
1 cycrow 759
	{
298 cycrow 760
		wprintf(L"Error: Remove filename is invalid\n" );
1 cycrow 761
		return;
762
	}
298 cycrow 763
	FileType t = GetFileTypeFromString(type);
172 cycrow 764
	if (t == FileType::FILETYPE_UNKNOWN)
1 cycrow 765
	{
298 cycrow 766
		wprintf(L"The file type \"%s\" is invalid\n", type.c_str() );
1 cycrow 767
		return;
768
	}
769
 
298 cycrow 770
	int check = CSpkFile::CheckFile(sfile);
1 cycrow 771
	CBaseFile *pBaseFile = 0;
772
	if ( check == SPKFILE_SINGLE )
773
		pBaseFile = new CSpkFile();
774
	else if ( check == SPKFILE_SINGLESHIP )
775
		pBaseFile = new CXspFile();
776
	else if ( check == SPKFILE_BASE )
777
		pBaseFile = new CBaseFile();
778
 
779
	if ( pBaseFile )
780
	{
298 cycrow 781
		wprintf(L"Opening File, %s... ", sfile.c_str());
782
		if ( !pBaseFile->readFile(sfile))
1 cycrow 783
		{
298 cycrow 784
			wprintf(L"(Error)\nUnable to open the file, %s\n", sfile.c_str());
1 cycrow 785
			return;
786
		}
298 cycrow 787
		wprintf(L"(Done)\n");
1 cycrow 788
 
298 cycrow 789
		wprintf(L"Removing file, %s, from Package\n", addfile.c_str());
1 cycrow 790
 
298 cycrow 791
		if(pBaseFile->removeFile(addfile, t))
1 cycrow 792
		{
298 cycrow 793
			wprintf(L"File, %s, has been remove from package\n", addfile.c_str() );
794
			pBaseFile->writeFile(sfile);
795
			wprintf(L"File has been written to disk successfully\n" );
1 cycrow 796
		}
797
		else
298 cycrow 798
			wprintf(L"Unable to remove the file, %s, from the package\n", addfile.c_str());
1 cycrow 799
	}
800
	else if ( check == SPKFILE_MULTI )
801
	{
802
		CMultiSpkFile spkfile;
298 cycrow 803
		wprintf(L"Opening Multi-SPK file, %s...", sfile.c_str());
804
		if ( !spkfile.readFile(sfile))
1 cycrow 805
		{
298 cycrow 806
			wprintf(L"(Error)\nUnable to open the Multi-SPK file, %s\n", sfile.c_str());
1 cycrow 807
			return;
808
		}
298 cycrow 809
		wprintf(L"(Done)\n");
1 cycrow 810
 
298 cycrow 811
		const SMultiSpkFile *ms = spkfile.findFile(type);
1 cycrow 812
		if ( !ms )
813
		{
298 cycrow 814
			wprintf(L"Unable to find the file \"%s\" in the package\n", type.c_str());
1 cycrow 815
			return;
816
		}
817
 
298 cycrow 818
		wprintf(L"Removing file, %s, from Package\n", addfile.c_str());
177 cycrow 819
		if (!spkfile.removeFile(ms))
1 cycrow 820
		{
298 cycrow 821
			wprintf(L"Error: Unable to remove file, %s, from package\n", type.c_str());
1 cycrow 822
			return;
823
		}
824
 
298 cycrow 825
		wprintf(L"Writing SPK File, %s... ", sfile.c_str());
826
		if (spkfile.writeFile(sfile))
827
			wprintf(L"(Done)\n");
1 cycrow 828
		else
298 cycrow 829
			wprintf(L"(Error)\n");
1 cycrow 830
	}
831
	else
298 cycrow 832
		wprintf(L"Error: Invalid file format, unable to open\n");
1 cycrow 833
}
834
 
835
 
298 cycrow 836
void CreateMultiFile (const Utils::WString &filename)
1 cycrow 837
{
298 cycrow 838
	wprintf(L"* Creating new Multi-SPK File, %s\n\n", filename.c_str());
1 cycrow 839
 
298 cycrow 840
	FILE *id = _wfopen(filename.c_str(), L"rb+");
1 cycrow 841
	if ( id )
842
	{
843
		fclose ( id );
298 cycrow 844
		wprintf(L"* File already exists, unable to create\n");
1 cycrow 845
		return;
846
	}
847
 
298 cycrow 848
	id = _wfopen(filename.c_str(), L"wb");
1 cycrow 849
	if ( !id )
850
	{
298 cycrow 851
		wprintf(L"* Unable to open file for writing\n");
1 cycrow 852
		return;
853
	}
298 cycrow 854
	fclose(id);
855
	_wremove(filename.c_str());
1 cycrow 856
 
857
	CMultiSpkFile spkfile;
858
 
197 cycrow 859
	Utils::WString sInput;
298 cycrow 860
	wprintf(L"Enter Multi-Spk Package Name: ");
861
	spkfile.setName(GetInput());
1 cycrow 862
 
863
	while ( true )
864
	{
298 cycrow 865
		wprintf(L"\nDo you want users to select scripts to install? (Y/N): " );
197 cycrow 866
		Utils::WString i = GetInput();
867
		i = i.upper();
868
		if ( i == L"Y" )
177 cycrow 869
			spkfile.setSelection(true);
197 cycrow 870
		if ( (i == L"Y") || (i == L"N") )
1 cycrow 871
			break;
872
	}
873
 
874
	while ( true )
875
	{
298 cycrow 876
		wprintf(L"\nEnter Spk File to add (Enter \"0\" to finish): " );
1 cycrow 877
		sInput = GetInput();
226 cycrow 878
		if ( sInput == L"0" )
1 cycrow 879
			break;
880
 
881
		// check if file can be opened
298 cycrow 882
		FILE *id2 = _wfopen(sInput.c_str(), L"rb+");
1 cycrow 883
		if ( !id2 )
197 cycrow 884
			wprintf(L"Error: Unable to open SPK file %s\n", sInput.c_str() );
1 cycrow 885
		else
886
		{
298 cycrow 887
			fclose(id2);
888
			if ( !spkfile.addFile(sInput))
889
				wprintf(L"Error: Unable to add SPK file to package\n");
1 cycrow 890
			else
298 cycrow 891
				wprintf(L"File Added to package (%s)\n", sInput.c_str());
1 cycrow 892
		}
893
	}
894
 
177 cycrow 895
	if ( spkfile.numFiles() < 1 )
298 cycrow 896
		wprintf(L"\nError: You have added no files, you must add at least one file to create a package\n");
1 cycrow 897
	else
898
	{
298 cycrow 899
		wprintf(L"Writing MultiSpk file... ");
900
		if ( spkfile.writeFile(filename))
901
			wprintf(L"(Done)\n" );
1 cycrow 902
		else
298 cycrow 903
			wprintf(L"(Error)\n" );
1 cycrow 904
	}
905
 
906
}
907
 
298 cycrow 908
void CreateFile(const Utils::WString &filename)
1 cycrow 909
{
298 cycrow 910
	wprintf(L"* Creating new SPK File, %s\n\n", filename.c_str());
1 cycrow 911
 
298 cycrow 912
	FILE *id = _wfopen(filename.c_str(), L"rb+");
1 cycrow 913
	if ( id )
914
	{
298 cycrow 915
		fclose(id);
916
		wprintf(L"* File already exists, unable to create\n");
1 cycrow 917
		return;
918
	}
919
 
298 cycrow 920
	id = _wfopen(filename.c_str(), L"wb");
1 cycrow 921
	if ( !id )
922
	{
298 cycrow 923
		wprintf(L"* Unable to open file for writing\n");
1 cycrow 924
		return;
925
	}
298 cycrow 926
	fclose(id);
927
	_wremove(filename.c_str());
1 cycrow 928
 
929
	CSpkFile spkfile;
930
 
298 cycrow 931
	wprintf(L"Enter Script Name: ");
932
	spkfile.setName(GetInput());
1 cycrow 933
 
298 cycrow 934
	wprintf(L"Enter Script Author: ");
935
	spkfile.setAuthor(GetInput());
1 cycrow 936
 
298 cycrow 937
	wprintf(L"Enter Script Version: ");
938
	spkfile.setVersion(GetInput());
1 cycrow 939
 
298 cycrow 940
	wprintf(L"Enter Script Description: ");
941
	spkfile.setDescription(GetInput());
1 cycrow 942
 
943
	struct tm   *currDate;
944
	char    dateString[100];
945
	time_t now = time(NULL);
946
 
947
	currDate = localtime( &now );
948
	strftime(dateString, sizeof dateString, "%d %m %Y", currDate);
226 cycrow 949
	spkfile.setCreationDate(Utils::WString::FromString(dateString));
1 cycrow 950
 
298 cycrow 951
	spkfile.writeFile(filename, NULL);
952
	wprintf(L"SPK file has been written to disk: %s\n", filename.c_str() );
1 cycrow 953
}
954
 
298 cycrow 955
void ExtractFiles (const Utils::WString &sfile, const Utils::WString &dir, int game )
1 cycrow 956
{
957
	// First checks if the file exists by opening it
298 cycrow 958
	FILE *id = _wfopen(sfile.c_str(), L"rb+");
1 cycrow 959
	if ( !id )
960
	{
298 cycrow 961
		wprintf(L"Error: File, %s, doesn't exist\n", sfile.c_str() );
1 cycrow 962
		return;
963
	}
964
	fclose ( id );
965
 
129 cycrow 966
	CPackages packages;
226 cycrow 967
	packages.startup(L".", L".", L".");
129 cycrow 968
 
1 cycrow 969
	int check = CSpkFile::CheckFile ( sfile );
970
 
971
	// extracts a file from single packages file
972
	if ( check == SPKFILE_SINGLE || check == SPKFILE_BASE || check == SPKFILE_SINGLESHIP )
973
	{
974
		// creates the spkfile object
975
		CBaseFile *pBaseFile = 0;
976
		if ( check == SPKFILE_SINGLE )
977
			pBaseFile = (CBaseFile *)new CSpkFile();
978
		else if ( check == SPKFILE_SINGLESHIP )
979
			pBaseFile = (CBaseFile *)new CXspFile();
980
		else
981
			pBaseFile = new CBaseFile();
982
 
298 cycrow 983
		wprintf(L"Opening File, %s... ", sfile.c_str());
1 cycrow 984
		// reads the file into memory
985
		// the SPKREAD_NODATA flag causes it to just read the settings, and skips the file data
175 cycrow 986
		if ( !pBaseFile->readFile(sfile, SPKREAD_NODATA))
1 cycrow 987
		{
298 cycrow 988
			wprintf(L"(Error)\nUnable to open the file, %s\n", sfile.c_str() );
1 cycrow 989
			return;
990
		}
298 cycrow 991
		wprintf(L"(Done)\n");
1 cycrow 992
 
134 cycrow 993
		if(game == 0)
298 cycrow 994
			wprintf(L"Extracting all files from archive...\n\n");
1 cycrow 995
		else
197 cycrow 996
			wprintf(L"Extracting %s files from archive...\n\n", packages.GetGameExe()->gameNameFromType(game - 1).c_str());
134 cycrow 997
 
998
		CFileProgress info(&packages, pBaseFile, "Extracting");
999
		if (packages.extractAll(pBaseFile, dir, game, true, &info) )
298 cycrow 1000
			wprintf(L"\n(Done)\nFiles have been extracted successfully\n");
134 cycrow 1001
		else
298 cycrow 1002
			wprintf(L"\n(Error)\nThere was a problem extracting the files\n");
1 cycrow 1003
	}
1004
	else
298 cycrow 1005
		wprintf(L"Invalid package file, %s\n", sfile.c_str());
1 cycrow 1006
}
1007
 
1008
 
1009
/*
1010
	Func:	AppendMultiFile
1011
	Args:	String toFile	- The spk file to append onto
1012
			String addfile	- The spk file to append
1013
	Desc:	Appends a spk file into a Multi-Spk Archive
1014
			If toFile is a single spk file, it will be converted to a Multi-Spk File
1015
			if addfile is a Multi-Spk file, all files from it will be appended
1016
*/
298 cycrow 1017
void AppendMultiFile(const Utils::WString &toFile, const Utils::WString &addfile)
1 cycrow 1018
{
1019
	// create destination object
1020
	CMultiSpkFile spkfile;
1021
 
1022
	// checks the destination
298 cycrow 1023
	int checkto = CSpkFile::CheckFile(toFile);
1 cycrow 1024
 
1025
	// if the destination is a single file, then convert it to a Multi-Spk package
1026
	if ( checkto == SPKFILE_SINGLE )
1027
	{
1028
		// adds the single file to the Multi-Spk object
1029
		// Add file also reads it into memory
298 cycrow 1030
		if ( !spkfile.addFile(toFile))
1 cycrow 1031
		{
298 cycrow 1032
			wprintf(L"Error: Unable to create Multi-Spk file\n");
1 cycrow 1033
			return;
1034
		}
1035
	}
1036
	else
1037
	{
1038
		// if its already a multispk file, then simply open it and read it to memory
298 cycrow 1039
		if ( !spkfile.readFile(toFile))
1 cycrow 1040
		{
298 cycrow 1041
			wprintf(L"Error: Unable to open Multi-Spk file, %s\n", toFile.c_str());
1 cycrow 1042
			return;
1043
		}
1044
	}
1045
 
1046
	// now add the file into the Multi-Spk Object
1047
	// the AddFile function will handle both single and Multi-Spk files
1048
	// So you dont need to test what the appending file is
298 cycrow 1049
	if ( spkfile.addFile(addfile))
1 cycrow 1050
	{
1051
 
1052
		// if it added correctly, then simply write the new Multi-Spk Object to disk
298 cycrow 1053
		wprintf(L"File, %s, has been added to Multi-Spk Package\n", addfile.c_str());
1054
		wprintf(L"Saving Multi-Spk File: %s... ", toFile.c_str());
1055
		if ( spkfile.writeFile(toFile))
1056
			wprintf(L"(Done)\n");
1 cycrow 1057
		else
298 cycrow 1058
			wprintf(L"(Error)\n");
1 cycrow 1059
	}
1060
	else
298 cycrow 1061
		wprintf(L"Error: Unable to add files, %s, to Multi-Spk Package\n", addfile.c_str());
1 cycrow 1062
}
1063
 
1064
/*
1065
	Func:	ExtractFile
1066
	Args:	String sfile	- the spk file to read from
1067
			String type		- the type of the file to find
1068
			String addfile	- The filename to extract
1069
			String dir		- The directory to extract to
1070
	Desc:	Finds and extracts a file from a Spk Package
1071
*/
298 cycrow 1072
void ExtractFile (const Utils::WString &sfile, const Utils::WString &type, const Utils::WString &addfile, const Utils::WString &dir)
1 cycrow 1073
{
1074
	// First checks if the file exists by opening it
298 cycrow 1075
	FILE *id = _wfopen(sfile.c_str(), L"rb+");
1 cycrow 1076
	if ( !id )
1077
	{
298 cycrow 1078
		wprintf(L"Error: File, %s, doesn't exist\n", sfile.c_str());
1 cycrow 1079
		return;
1080
	}
1081
	fclose ( id );
1082
 
1083
	// now check the type of file it is, using the static function CheckFile(filename).
1084
	// This will return the type of file
1085
	//		SPK_INVALID		- Invalid file format, wont be able to open
1086
	//		SPK_SINGLE		- A Single spk package file
1087
	//		SPK_MULTI		- A Multi-Spk Archive
1088
	//		SPK_BASE		- A Base File
1089
	//		SPK_SINGLESHIP	- A Ship file
1090
	int check = CSpkFile::CheckFile ( sfile );
1091
 
1092
	// extracts a file from single packages file
1093
	if ( check == SPKFILE_SINGLE || check == SPKFILE_BASE || check == SPKFILE_SINGLESHIP )
1094
	{
1095
		// first get the file type is valid
1096
		// converts the string type into its filetype flag
170 cycrow 1097
		FileType t = GetFileTypeFromString(type);
1 cycrow 1098
		// incorrect text type, display the error
170 cycrow 1099
		if (t == FileType::FILETYPE_UNKNOWN)
1 cycrow 1100
		{
298 cycrow 1101
			wprintf(L"The file type \"%s\" is invalid\n", type.c_str());
1 cycrow 1102
			return;
1103
		}
1104
 
1105
		// creates the spkfile object
1106
		CBaseFile *pBaseFile = 0;
1107
		if ( check == SPKFILE_SINGLE )
1108
			pBaseFile = (CBaseFile *)new CSpkFile();
1109
		else if ( check == SPKFILE_SINGLESHIP )
1110
			pBaseFile = (CBaseFile *)new CXspFile();
1111
		else
1112
			pBaseFile = new CBaseFile();
1113
 
298 cycrow 1114
		wprintf(L"Opening File, %s... ", sfile.c_str());
1 cycrow 1115
		// reads the file into memory
1116
		// the SPKREAD_NODATA flag causes it to just read the settings, and skips the file data
175 cycrow 1117
		if ( !pBaseFile->readFile(sfile, SPKREAD_NODATA))
1 cycrow 1118
		{
298 cycrow 1119
			wprintf(L"(Error)\nUnable to open the file, %s\n", sfile.c_str());
1 cycrow 1120
			return;
1121
		}
1122
 
1123
		// No read all the file data into memory
1124
		// This can be done all together in the ReadFile function
1125
		// Doing it seperatly allows you to save time if an error occurs, so you dont have to wait for it to read the whole thing
1126
		pBaseFile->ReadAllFilesToMemory ();
1127
 
298 cycrow 1128
		wprintf(L"(Done)\n");
1 cycrow 1129
 
1130
		// uses the FindFile function to find the selected file in the archive
1131
		// requires the filename, type and custom directory (only for Extra File Type)
170 cycrow 1132
		C_File *f = pBaseFile->findFile(addfile, t);
1 cycrow 1133
		if ( !f )
1134
		{
298 cycrow 1135
			wprintf(L"Unable to find the file \"%s\" in the package\n", addfile.c_str());
1 cycrow 1136
			return;
1137
		}
1138
 
1139
		// creates the directory so it can be extracted
1140
		CDirIO Dir(dir);
160 cycrow 1141
		if ( !Dir.create(f->getDirectory(pBaseFile)) )
1 cycrow 1142
		{
298 cycrow 1143
			wprintf(L"Unable to create the directory \"%s\" to extract into\n", dir.c_str());
1 cycrow 1144
			return;
1145
		}
1146
 
1147
		// sets up the progress pointer
1148
		// if it uses 7zip, the progress will be displayed in the command prompt
1149
		MyProgress progress(0);
298 cycrow 1150
		wprintf(L"Extracting the file from package\n\t>");
1 cycrow 1151
 
1152
		// Extracts the file to the specified directory
175 cycrow 1153
		if ( !pBaseFile->extractFile(f, dir, true, &progress))
298 cycrow 1154
			wprintf(L"< (Error)\nUnable to extract the file\n");
1 cycrow 1155
		else
1156
		{
1157
			progress.PrintDone ();
298 cycrow 1158
			wprintf(L"< (Done)\nFile has been extracted successfully\n");
1 cycrow 1159
		}
1160
	}
1161
 
1162
	// the file is a Multi-Spk File, extracts a single spk file from the archive
1163
	else if ( check == SPKFILE_MULTI )
1164
	{
1165
		// creates MultiSpkFile object
1166
		CMultiSpkFile spkfile;
298 cycrow 1167
		wprintf(L"Opening Multi-SPK file, %s...", sfile.c_str());
1 cycrow 1168
 
1169
		// reads the MultiSpkFile into memory
177 cycrow 1170
		if ( !spkfile.readFile(sfile))
1 cycrow 1171
		{
298 cycrow 1172
			wprintf(L"(Error)\nUnable to open the Multi-SPK file, %s\n", sfile.c_str());
1 cycrow 1173
			return;
1174
		}
298 cycrow 1175
		wprintf(L"(Done)\n");
1 cycrow 1176
 
1177
		// searchs the archive for a matching file
177 cycrow 1178
		const SMultiSpkFile *ms = spkfile.findFile(type);
1 cycrow 1179
		if ( !ms )
1180
		{
298 cycrow 1181
			wprintf(L"Unable to find the file \"%s\" in the package\n", type.c_str());
1 cycrow 1182
			return;
1183
		}
1184
 
1185
		// extracts the file from the archive, to the given directory
298 cycrow 1186
		wprintf(L"Extracting SPK file, %s, from package... ", ms->sName.c_str());
177 cycrow 1187
		if (spkfile.extractFile(ms, addfile))
298 cycrow 1188
			wprintf(L"(Done)\n");
1 cycrow 1189
		else
298 cycrow 1190
			wprintf(L"(Error)\n");
1 cycrow 1191
	}
1192
	else
298 cycrow 1193
		wprintf(L"Error: Invalid file format, unable to open\n");
1 cycrow 1194
}
1195
 
1196
/*
1197
	Func:	SplitMulti
1198
	Args:	String filename - the filename of the multispk file to open
1199
			String dest		- The destination directory to extract the files to
1200
	Desc:	Splits a multi-spk file into its seperate spk files
1201
*/
298 cycrow 1202
void SplitMulti(const Utils::WString &filename, const Utils::WString &dest)
1 cycrow 1203
{
1204
	// Check the file type, Must return SPKFILE_MULTI, otherwise its not a Multi-Spk Packages
298 cycrow 1205
	if ( CSpkFile::CheckFile(filename) != SPKFILE_MULTI )
1 cycrow 1206
	{
298 cycrow 1207
		wprintf(L"Error: The file is not a Multi-Spk packages\n");
1 cycrow 1208
		return;
1209
	}
1210
 
298 cycrow 1211
	wprintf(L"Spliting Multi-SPK File to, %s... ", dest.c_str());
1 cycrow 1212
 
1213
	// create the MultiSpkFile object
1214
	CMultiSpkFile spkfile;
1215
 
1216
	// Splits the files to the destination
298 cycrow 1217
	if ( !spkfile.splitMulti(filename, dest))
1 cycrow 1218
	{
298 cycrow 1219
		wprintf(L"(Error)\nUnable to split Multi-SPK package, %s\n", filename.c_str());
1 cycrow 1220
		return;
1221
	}
1222
 
298 cycrow 1223
	wprintf(L"(Done)\n");
1 cycrow 1224
}
1225
 
197 cycrow 1226
Utils::WString GetAsk(const Utils::WString &command)
1 cycrow 1227
{
197 cycrow 1228
	wprintf(L"(ASK) Enter the value for, %s: ", command.c_str());
1 cycrow 1229
	return GetInput();
1230
}
1231
 
126 cycrow 1232
void GenerateUpdateList(int argc, char **argv)
1233
{
226 cycrow 1234
	Utils::WString currentDir = CFileIO(Utils::WString::FromString(argv[0])).dir();
126 cycrow 1235
	CDirIO Dir(currentDir);
1236
 
196 cycrow 1237
	Utils::WStringList list;
126 cycrow 1238
	for (int i = 2; i < argc; ++i)
1239
	{
298 cycrow 1240
		Utils::WString file(argv[i]);
126 cycrow 1241
		if (CFileIO::Exists(file))
196 cycrow 1242
			list.pushBack(file, L"file");
126 cycrow 1243
		else if (CFileIO::Exists(Dir.file(file)))
196 cycrow 1244
			list.pushBack(Dir.file(file), L"file");
126 cycrow 1245
		else if (CDirIO::Exists(file))
196 cycrow 1246
			list.pushBack(file, L"dir");
126 cycrow 1247
		else if (CDirIO::Exists(Dir.dir(file)))
196 cycrow 1248
			list.pushBack(Dir.dir(file), L"dir");
126 cycrow 1249
		else
196 cycrow 1250
			list.pushBack(file, L"pattern");
126 cycrow 1251
	}
1252
 
1253
	if (list.empty())
298 cycrow 1254
		wprintf(L"unable to find any packages");
126 cycrow 1255
	else
1256
	{
196 cycrow 1257
		Utils::WStringList fileList;
126 cycrow 1258
		for (auto itr = list.begin(); itr != list.end(); itr++)
1259
		{
196 cycrow 1260
			Utils::WString file = (*itr)->str;
1261
			Utils::WString data = (*itr)->data;
1262
			if (data == L"file")
126 cycrow 1263
				fileList.pushBack(file);
196 cycrow 1264
			else if (data == L"dir")
126 cycrow 1265
			{
1266
				CDirIO dir(file);
196 cycrow 1267
				Utils::WStringList d;
126 cycrow 1268
				if (dir.dirList(d))
1269
				{
1270
					for (auto itr2 = d.begin(); itr2 != d.end(); itr2++)
1271
					{
196 cycrow 1272
						Utils::WString ext = CFileIO((*itr2)->str).extension().lower();
1273
						if(ext == L"xsp" || ext == L"spk")
126 cycrow 1274
							fileList.pushBack(dir.file((*itr2)->str));
1275
					}
1276
				}
1277
			}
226 cycrow 1278
			else if (data == L"pattern")
126 cycrow 1279
			{				
1280
				CFileIO f(file);
1281
				CDirIO dir(f.dir());
1282
 
1283
				if (!dir.exists())
1284
					dir = CDirIO(Dir.dir(dir.dir()));
1285
 
196 cycrow 1286
				Utils::WStringList d;
1287
				if (dir.dirList(d, Utils::WString::Null(), f.filename()))
126 cycrow 1288
				{
1289
					for (auto itr2 = d.begin(); itr2 != d.end(); itr2++)
1290
					{
196 cycrow 1291
						Utils::WString ext = CFileIO((*itr2)->str).extension().lower();
1292
						if (ext == L"xsp" || ext == L"spk")
126 cycrow 1293
							fileList.pushBack(dir.file((*itr2)->str));
1294
					}
1295
				}
1296
			}
1297
		}
1298
 
1299
		if (fileList.empty())
298 cycrow 1300
			wprintf(L"unable to find any packages");
126 cycrow 1301
		else
1302
		{
1303
			CPackages packages;
1304
 
197 cycrow 1305
			Utils::WStringList filedata;
126 cycrow 1306
			for (auto itr = fileList.begin(); itr != fileList.end(); itr++)
1307
			{
196 cycrow 1308
				wprintf(L"Reading file: %s\n", (*itr)->str.c_str());
126 cycrow 1309
 
1310
				int error = 0;
196 cycrow 1311
				CBaseFile *p = packages.openPackage((*itr)->str.toString(), &error, 0, SPKREAD_NODATA);
126 cycrow 1312
				if (!p)
1313
				{
1314
					printf("\tERROR!\n");
1315
					continue;
1316
				}
204 cycrow 1317
				wprintf(L"\tData extracted: %s %s by %s\n", p->name().c_str(), p->version().c_str(), p->author().c_str());
126 cycrow 1318
				if (!p->creationDate().empty())
204 cycrow 1319
					wprintf(L"\t\tCreated: %s\n", p->creationDate().c_str());
126 cycrow 1320
				if(!p->description().empty())
206 cycrow 1321
					wprintf(L"\t\t%s\n", p->description().c_str());
126 cycrow 1322
 
1323
				filedata.pushBack(CPackages::FormatAvailablePackageData(p));
1324
				delete p;
1325
			}
1326
 
1327
			if (filedata.empty())
298 cycrow 1328
				wprintf(L"unable to find any packages");
126 cycrow 1329
			else				
1330
			{
226 cycrow 1331
				Utils::WString dest = Dir.file(L"xpackagedata.dat");
126 cycrow 1332
				if (CFileIO(dest).writeFile(&filedata))
298 cycrow 1333
					wprintf(L"web update file, xpackagedata.dat, generated");
126 cycrow 1334
				else
298 cycrow 1335
					wprintf(L"unable to write update file");
126 cycrow 1336
			}
1337
		}
1338
	}
1339
}
1340
 
196 cycrow 1341
void GenerateUpdateFile(const Utils::WString &spkfile)
1 cycrow 1342
{
182 cycrow 1343
	if (!CFileIO::Exists(spkfile))
1 cycrow 1344
	{
196 cycrow 1345
		wprintf(L"Error: The package file, %s, does not exist", spkfile.c_str());
1 cycrow 1346
		return;
1347
	}
1348
 
197 cycrow 1349
	int check = CSpkFile::CheckFile(spkfile);
1 cycrow 1350
	if ( check == SPKFILE_MULTI )
1351
	{
298 cycrow 1352
		wprintf(L"Error: Multi-Package files currently not supported\n");
1 cycrow 1353
		return;
1354
	}
1355
	else if ( check == SPKFILE_OLD )
1356
	{
298 cycrow 1357
		wprintf(L"Error: unable to read old format spk file, try spkconvert first\n");
1 cycrow 1358
		return;
1359
	}
1360
	else if ( check == SPKFILE_INVALID )
1361
	{
196 cycrow 1362
		wprintf(L"Error: %s doesn't appear to be a valid package file\n", spkfile.c_str());
1 cycrow 1363
		return;
1364
	}
1365
 
1366
	CPackages p;
1367
	int error;
298 cycrow 1368
	CBaseFile *package = p.openPackage(spkfile, &error, 0, SPKREAD_NODATA);
1 cycrow 1369
	if ( !package )
1370
	{
196 cycrow 1371
		wprintf(L"Error: unable to open package file, %s, Error=%d\n", spkfile.c_str(), error);
1 cycrow 1372
		return;
1373
	}
1374
 
196 cycrow 1375
	Utils::WString file = package->createUpdateFile(CFileIO(spkfile).dir());
102 cycrow 1376
	if ( file.empty() )
196 cycrow 1377
		wprintf(L"Error: unable to create update file for: %s, Directory=%s\n", spkfile.c_str(), CFileIO(spkfile).dir().c_str());
1 cycrow 1378
	else
196 cycrow 1379
		wprintf(L"Update file: %s has been created for package, %s\n", file.c_str(), spkfile.c_str());
1 cycrow 1380
 
1381
	delete package;
1382
 
1383
}
1384
 
298 cycrow 1385
void GeneratePackagerScript(const Utils::WString &spkfile, Utils::WString toFile, int game)
1 cycrow 1386
{
126 cycrow 1387
	if (!CFileIO::Exists(spkfile))
1 cycrow 1388
	{
298 cycrow 1389
		wprintf(L"Error: The package file, %s, does not exist", spkfile.c_str());
1 cycrow 1390
		return;
1391
	}
1392
 
1393
	int check = CSpkFile::CheckFile(spkfile);
1394
	if ( check == SPKFILE_MULTI )
1395
	{
298 cycrow 1396
		wprintf(L"Error: Cant generate a script from a multi-spk file\n");
1 cycrow 1397
		return;
1398
	}
1399
	else if ( check == SPKFILE_OLD )
1400
	{
298 cycrow 1401
		wprintf(L"Error: unable to read old format spk file, try spkconvert first\n");
1 cycrow 1402
		return;
1403
	}
1404
	else if ( check == SPKFILE_INVALID )
1405
	{
298 cycrow 1406
		wprintf(L"Error: %s doesn't appear to be a spk file\n", spkfile.c_str());
1 cycrow 1407
		return;
1408
	}
1409
 
1410
	CPackages p;
226 cycrow 1411
	p.startup(L".", L".", L".");
1 cycrow 1412
	int error;
182 cycrow 1413
	CBaseFile *package = p.openPackage(spkfile, &error, 0, SPKREAD_NODATA);
1 cycrow 1414
	if ( !package )
1415
	{
298 cycrow 1416
		wprintf(L"Error: unable to open package files, %s, Error=%d\n", spkfile.c_str(), error);
1 cycrow 1417
		return;
1418
	}
1419
 
210 cycrow 1420
	Utils::WStringList list;
127 cycrow 1421
	if ( !p.generatePackagerScript(package, true, &list, game) )
1 cycrow 1422
	{
298 cycrow 1423
		wprintf(L"Error: Unable to generate packager script\n");
1 cycrow 1424
		return;
1425
	}
1426
 
298 cycrow 1427
	if ( toFile.empty() )
1428
		toFile = CFileIO(spkfile).GetDirIO().file(package->name() + L"_" + package->author() + L".sps");
1 cycrow 1429
 
1430
	// save package file
298 cycrow 1431
	if ( CFileIO(toFile).writeFile(&list) )
1432
		wprintf(L"Packager script, %s, has been geenrated\n", toFile.c_str());
1 cycrow 1433
	else
298 cycrow 1434
		wprintf(L"Error: unable to write packager script, %s\n", toFile.c_str());
1 cycrow 1435
 
1436
	delete package;
1437
}
1438
 
1439
/*
1440
	Func:	LoadPackagerScript
1441
	Args:	String filename	-	The filename of the packager script to load
1442
	Desc:	Loads the packager scripts and creates a spk/xsp file from it
1443
*/
197 cycrow 1444
void LoadPackagerScript(const Utils::WString &filename, bool verify)
1 cycrow 1445
{
131 cycrow 1446
	if ( !CFileIO::Exists(filename) )
1 cycrow 1447
	{
197 cycrow 1448
		wprintf(L"Error: The packager script, %s, does not exist", filename.c_str());
1 cycrow 1449
		return;
1450
	}
1451
 
197 cycrow 1452
	Utils::WString myDoc;
1 cycrow 1453
#ifdef _WIN32
1454
	TCHAR pszPath[MAX_PATH];
1455
	if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, pszPath)))
1456
	{
1457
		myDoc = (char *)pszPath;
1458
	}
1459
#endif
1460
 
1461
	if ( verify )
197 cycrow 1462
		wprintf(L"Verifying Packager Script: %s\n", filename.c_str());
1 cycrow 1463
 
1464
	CPackages p;
197 cycrow 1465
	p.startup(L".", L".", myDoc);
1466
	Utils::WStringList malformed, unknown;
1 cycrow 1467
 
197 cycrow 1468
	Utils::WString curDir = CFileIO(filename).dir();
196 cycrow 1469
	if ( curDir.empty() ) curDir = L"./";
1 cycrow 1470
 
197 cycrow 1471
	Utils::WStringList variables;
1472
	variables.pushBack(L"$PATH", curDir);
1 cycrow 1473
 
134 cycrow 1474
	CFileProgress info(&p, NULL, "Adding File");
197 cycrow 1475
	CBaseFile *package = p.loadPackagerScript(filename, -1, (verify) ? NULL : &GetAsk, &malformed, &unknown, &variables, &info);
1 cycrow 1476
 
134 cycrow 1477
	printf("\n");
1478
 
1 cycrow 1479
	if ( verify )
1480
	{
131 cycrow 1481
		if ( !malformed.empty() )
1 cycrow 1482
		{
131 cycrow 1483
			printf("Malformed Lines (%d):\n", malformed.size());
1484
			for(auto itr = malformed.begin(); itr != malformed.end(); itr++)				
197 cycrow 1485
				wprintf(L"\t(Line %3d) %s\n", (*itr)->data.toInt(), (*itr)->str.c_str());
1 cycrow 1486
		}
131 cycrow 1487
		if ( !unknown.empty() )
1 cycrow 1488
		{
131 cycrow 1489
			printf("Unknown Commands (%d):\n", unknown.size());
1490
			for (auto itr = unknown.begin(); itr != unknown.end(); itr++)
197 cycrow 1491
				wprintf(L"\t* Command: %s = %s\n", (*itr)->str.c_str(), (*itr)->data.c_str());
1 cycrow 1492
		}
1493
	}
1494
 
1495
	if ( !package )
1496
	{
1497
		if ( verify )
1498
			printf("Error: There are errors in the packager script which prevents it from being created\n");
1499
		else
197 cycrow 1500
			wprintf(L"Error: Unable to create package, from script: %s\n", filename.c_str());
1 cycrow 1501
	}
1502
	else
1503
	{
197 cycrow 1504
		Utils::WString saveto = package->filename();
1505
		saveto = saveto.findReplace(L"$DEFAULTDIR", curDir + L"/");
1506
		saveto = saveto.findReplace(L"$PATH", curDir);
1507
		saveto = saveto.findReplace(L"\\", L"/");
1508
		saveto = saveto.findReplace(L"//", L"/");
1509
		if ( !saveto.right(4).Compare(L".spk") && package->GetType() != TYPE_XSP )
1510
			saveto += L".spk";
1511
		else if ( !saveto.right(4).Compare(L".xsp") && package->GetType() == TYPE_XSP )
1512
			saveto += L".xsp";
196 cycrow 1513
		wprintf(L"Saving file to: %s\n", CFileIO(saveto).fullFilename().c_str());
1 cycrow 1514
		if ( verify )
1515
			printf("Package can be created from this script\n");
1516
		else
1517
		{
1518
			// write script
197 cycrow 1519
			if ( package->writeFile(saveto.toString()) )
1 cycrow 1520
			{
1521
				if ( package->AutoGenerateUpdateFile() )
134 cycrow 1522
					package->createUpdateFile(CFileIO(saveto).dir());
197 cycrow 1523
				wprintf(L"Package: %s was created\n", saveto.c_str());
1 cycrow 1524
			}
1525
			else
1526
				printf("Error! There was a problem writing the package\n");
1527
		}
1528
 
204 cycrow 1529
		saveto = package->exportFilename();
131 cycrow 1530
		if ( !saveto.empty() ) {
197 cycrow 1531
			saveto = saveto.findReplace(L"$DEFAULTDIR", curDir + L"/");
1532
			saveto = saveto.findReplace(L"$PATH", curDir);
1533
			saveto = saveto.findReplace(L"\\", L"/");
1534
			saveto = saveto.findReplace(L"//", L"/");
196 cycrow 1535
			wprintf(L"Exporting file to: %s\n", CFileIO(saveto).fullFilename().c_str());
1 cycrow 1536
			if ( verify )
1537
				printf("Package can be exported from this script\n");
1538
			else
1539
			{
1540
				// export
197 cycrow 1541
				if ( package->saveToArchive(saveto.toString(), 0, p.GetGameExe())) {
1 cycrow 1542
					if ( package->IsAnyGameInPackage() ) {
197 cycrow 1543
						for ( int i = 0; i < p.GetGameExe()->numGames(); i++ ) {
1 cycrow 1544
							if ( package->IsGameInPackage(i + 1) ) {								
197 cycrow 1545
								Utils::WString exportFile = CFileIO(saveto).dir() + L"/" + CFileIO(saveto).baseName() + L"_" + CBaseFile::ConvertGameToString(i + 1) + L"." + CFileIO(saveto).extension();
196 cycrow 1546
								package->saveToArchive(exportFile.toString(), i + 1, p.GetGameExe());
1 cycrow 1547
							}
1548
						}
1549
					}
197 cycrow 1550
					wprintf(L"Package: %s was exported\n", saveto.c_str());
1 cycrow 1551
				}
1552
				else
1553
					printf("Error! There was a problem exporting the package\n");
1554
			}
1555
		}
1556
	}
1557
}
1558
 
1559
/*
1560
	Main entry point to program
1561
*/
1562
int main ( int argc, char **argv )
1563
{
1564
	// display program header to command prompt
126 cycrow 1565
	printf ( "\nSPKTool V1.50 (SPK File Version %.2f) 14/02/2021 Created by Cycrow\n\n", (float)FILEVERSION );
1 cycrow 1566
 
1567
	// parse the cmd name
197 cycrow 1568
	Utils::WString cmd (argv[0]);
1569
	cmd = cmd.findReplace(L"\\", L"/");
1570
	g_dir = cmd.tokens(L"/", 1, cmd.countToken(L"/") - 1);
1571
	cmd = cmd.token(L"/", cmd.countToken(L"/"));
1 cycrow 1572
 
1573
	g_read = false;
1574
 
1575
	// not enough arguments, display the syntax and exit
1576
	if ( argc < 2 )
1577
	{
197 cycrow 1578
		PrintSyntax ( cmd.toString() );
1 cycrow 1579
		exit ( 1 );
1580
	}
1581
 
197 cycrow 1582
	Utils::WString fileTypes;
1 cycrow 1583
	for ( int i = 0; i < FILETYPE_MAX; i++ )
1584
	{
197 cycrow 1585
		Utils::WString sT = GetFileTypeString(i);
1586
		if ( !sT.empty() )
1 cycrow 1587
		{
197 cycrow 1588
			if ( fileTypes.empty() )
1 cycrow 1589
				fileTypes = sT;
1590
			else
1591
			{
197 cycrow 1592
				fileTypes += L", ";
1 cycrow 1593
				fileTypes += sT;
1594
			}
1595
		}
1596
	}
1597
 
1598
	// get the command flag
197 cycrow 1599
	Utils::WString command(argv[1]);
1 cycrow 1600
 
1601
	// display the contents of the spk file
197 cycrow 1602
	if ( command == L"-v" || command == L"--view" || command == L"--version")
1 cycrow 1603
	{
1604
		if ( argc < 3 )
197 cycrow 1605
			wprintf(L"Syntax: %s -v <spkfile>\n\tWill open and display the contents of the spkfile\n", cmd.c_str() );
1 cycrow 1606
		else
134 cycrow 1607
			DisplayVersion(argv[2]);
1 cycrow 1608
	}
1609
 
1610
	// creates a new spk file
197 cycrow 1611
	else if ( command == L"-c" || command == L"--create" )
1 cycrow 1612
	{
1613
		if ( argc < 3 )
197 cycrow 1614
			wprintf(L"Syntax:\n\t%s -c <spkfile>\n\t%s --create <spkfile>\n\t\tThis will create a new SPK file and allow you to set some basic settings for the file\n", cmd.c_str(), cmd.c_str() );
1 cycrow 1615
		else
1616
			CreateFile ( argv[2] );
1617
	}
1618
 
1619
	// appends a file onto the spk archive
197 cycrow 1620
	else if ( command == L"-a" || command == L"--append" )
1 cycrow 1621
	{
1622
		if ( argc < 4 )
197 cycrow 1623
			wprintf(L"Syntax:\n\t%s -a <spkfile> <type> <filename>\n\t%s --append <spkfile> <type> <filename>\n\t\tThis will append the file into the archive and compress it according to the files default compression\n\t<type> = %s\n", cmd.c_str(), cmd.c_str(), fileTypes.c_str() );
1 cycrow 1624
		else
1625
		{
298 cycrow 1626
			Utils::WString arg4;
1 cycrow 1627
			if ( argc > 4 )
1628
				arg4 = argv[4];
1629
			AppendFile ( argv[2], argv[3], arg4 );
1630
		}
1631
	}
1632
 
1633
	// removes a file from the spk archive
197 cycrow 1634
	else if ( command == L"-r" || command == L"--remove" || command == L"--removespk" )
1 cycrow 1635
	{
1636
		if ( argc < 4 )
1637
		{
197 cycrow 1638
			wprintf(L"Syntax:\n\t%s -r <spkfile> <type> <filename>\n\t%s --remove <spkfile> <type> <filename\n\t\tThis will remove a file from the archive\n\t<type> = %s\n", cmd.c_str(), cmd.c_str(), fileTypes.c_str() );
1639
			wprintf(L"\t%s -r <multispkfile> <filename>\n\t%s --removespk <multispkfile> <filename>\n\t\tThis will remove a spk file from the Multi-SPK package\n", cmd.c_str(), cmd.c_str() );
1 cycrow 1640
		}
1641
		else
1642
		{
298 cycrow 1643
			Utils::WString arg4;
1 cycrow 1644
			if ( argc > 4 )
1645
				arg4 = argv[4];
1646
			RemoveFile ( argv[2], argv[3], arg4 );
1647
		}
1648
	}
1649
 
1650
	// extracts a file from a spk file
197 cycrow 1651
	else if ( command == L"--extractspk" )
1 cycrow 1652
	{
1653
		if ( argc < 4 )
197 cycrow 1654
			wprintf(L"Syntax:\n\t%s --extractspk <multispkfile> <filename> [destination]\n\tThis will extract a spk file from the Multi-Spk package and save it to the destination path\n", cmd.c_str() );
1 cycrow 1655
		else
1656
		{
298 cycrow 1657
			Utils::WString arg4;
1 cycrow 1658
			if ( argc > 4 )
1659
				arg4 = argv[3];
298 cycrow 1660
			ExtractFile(argv[2], argv[3], arg4, Utils::WString::Null());
1 cycrow 1661
		}
1662
	}
197 cycrow 1663
	else if ( command == L"-x" || command == L"--extract")
1 cycrow 1664
	{
1665
		if ( argc < 4 )
1666
		{
197 cycrow 1667
			wprintf(L"Syntax:\n\t%s -x <spkfile> <type> <filename> [destination]\n\tThis will extract a file from the package and save it to the corect path in <directory>\n\t<type> = %s\n", cmd.c_str(), fileTypes.c_str() );
1668
			wprintf(L"\t%s -x <multispkfile> <filename> [destination]\n\tThis will extract a spk file from the Multi-Spk package and save it to the destination path\n", cmd.c_str() );
1 cycrow 1669
		}
1670
		else
1671
		{
298 cycrow 1672
			Utils::WString arg5, arg4;
1 cycrow 1673
			if ( argc > 5 )
1674
				arg5 = argv[5];
1675
			if ( argc > 4 )
1676
				arg4 = argv[4];
134 cycrow 1677
			ExtractFile(argv[2], argv[3], arg4, arg5);
1 cycrow 1678
		}
1679
	}
1680
 
1681
	// extracts all the files from an archive
197 cycrow 1682
	else if ( command == L"-e" || command == L"--extractall" )
1 cycrow 1683
	{
1684
		if ( argc < 3 )
197 cycrow 1685
			wprintf(L"Syntax:\n\t%s -e <spkfile> [destination]\n\t--extractall <spkfile> <game> [destination]\n\t\tThis will extract all files of a set game into the destination directory\n\n\t\tGame = 0 will extract all files", cmd.c_str() );
1 cycrow 1686
		else
1687
		{
298 cycrow 1688
			Utils::WString arg4;
1 cycrow 1689
			if ( argc > 4 )
1690
				arg4 = argv[4];
134 cycrow 1691
			ExtractFiles(argv[2], arg4, CBaseFile::GetGameFromString(argv[3]));
1 cycrow 1692
		}
1693
	}
1694
 
1695
	// creates a multispk archive
197 cycrow 1696
	else if ( command == L"-n" || command == L"--createmulti" )
1 cycrow 1697
	{
1698
		if ( argc < 3 )
197 cycrow 1699
			wprintf(L"Syntax:\n\t%s -n <multispkfile>\n\t%s --createmulti <multispkfile>\n\t\tThis will create a multispk file and allow you to add spk files to it\n", cmd.c_str(), cmd.c_str() );
1 cycrow 1700
		else
1701
			CreateMultiFile ( argv[2] );
1702
	}
1703
 
1704
	// merges 2 multi-spk archives together, or appends a single spk file into a multi-spk file
197 cycrow 1705
	else if ( command == L"-m" || command == L"--mergemulti" )
1 cycrow 1706
	{
1707
		if ( argc < 4 )
197 cycrow 1708
			wprintf(L"Syntax:\n\t%s -m <spkfile1> <spkfile2>\n\t%s --mergemulti <spkfile1> <spkfile2>\n\t\tThis will add spkfile2 into spkfile1, if spkfile1 is a normal SPK file, it will be saved into a Multi-Spk file\nspkfile2 can also be a Multi-Spk file, all files within it will be added\n", cmd.c_str(), cmd.c_str() );
1 cycrow 1709
		else
1710
			AppendMultiFile ( argv[2], argv[3] );
1711
	}
1712
 
1713
	// splits the multi-spk file, exracts all spk files
197 cycrow 1714
	else if ( command == L"-s" || command == L"--splitmulti" )
1 cycrow 1715
	{
1716
		if ( argc < 3 )
197 cycrow 1717
			wprintf(L"Syntax: %s -s <multispkfile> [destination]\n\tSplits the Multi-SPK file and saves each spk file to the destiantion directory\n", cmd.c_str() );
1 cycrow 1718
		else
1719
		{
298 cycrow 1720
			Utils::WString arg3;
1 cycrow 1721
			if ( argc > 3 )
1722
				arg3 = argv[3];
1723
			SplitMulti ( argv[2], arg3 );
1724
		}
1725
	}
1726
 
197 cycrow 1727
	else if ( command == L"--set" )
1 cycrow 1728
	{
1729
		if ( argc < 4 )
197 cycrow 1730
			wprintf(L"Syntax: %s --set <spkfile> <setting> [values]\n\tSets various settings in the package\n", cmd.c_str() );
1 cycrow 1731
		else
226 cycrow 1732
			Settings(argv[2], argv[3], argc, argv, cmd);
1 cycrow 1733
	}
197 cycrow 1734
	else if ( command == L"--setrating" )
1 cycrow 1735
	{
1736
		if ( argc < 6 )
197 cycrow 1737
			wprintf(L"Syntax: %s --setrating <spkfile> <easeofuse> <gamechanging> <recommended>\n\tSets the rating of the spk package\n", cmd.c_str() );
1 cycrow 1738
		else
298 cycrow 1739
			SetRating(argv[2], Utils::WString(argv[3]).toInt(), Utils::WString(argv[4]).toInt(), Utils::WString(argv[5]).toInt());
1 cycrow 1740
	}
197 cycrow 1741
	else if ( command == L"--convertxsp" || command == L"--convertxspwizard" )
1 cycrow 1742
	{
1743
		if ( argc < 3 )
197 cycrow 1744
			wprintf(L"Syntax: %s %s <xspfile> [newxspfile]\n\tConverts an old format xsp file to work with the new format\n", cmd.c_str(), command.c_str() );
1 cycrow 1745
		else
1746
		{
298 cycrow 1747
			Utils::WString arg3;
1 cycrow 1748
			if ( argc > 3 )
1749
				arg3 = argv[3];
226 cycrow 1750
			ConvertXsp(Utils::WString::FromString(argv[2]), arg3, (command == L"--convertxspwizard") ? true : false);
1 cycrow 1751
		}
1752
	}
197 cycrow 1753
	else if ( command == L"--createscript" )
1 cycrow 1754
	{
1755
		if ( argc < 3 )
197 cycrow 1756
			wprintf(L"Syntax:\n\t%s --createscript <packagerscript>\n\t\tThis will create a spk/xsp file from a packager script\n", cmd.c_str() );
1 cycrow 1757
		else
1758
			LoadPackagerScript ( argv[2], false );
1759
	}
197 cycrow 1760
	else if ( command == L"--generatescript" )
1 cycrow 1761
	{
1762
		if ( argc < 3 )
197 cycrow 1763
			wprintf(L"Syntax:\n\t%s --generatescript <package> [packagerscript]\n\t\tThis will generate a packager script file from a spk/xsp file.\n", cmd.c_str() );
1 cycrow 1764
		else
1765
		{
1766
			if ( argc < 4 )
126 cycrow 1767
				GeneratePackagerScript ( argv[2], "", 0);
1 cycrow 1768
			else
126 cycrow 1769
				GeneratePackagerScript ( argv[2], argv[3], 0);
1 cycrow 1770
		}
1771
	}
197 cycrow 1772
	else if ( command == L"--verifyscript" )
1 cycrow 1773
	{
1774
		if ( argc < 3 )
197 cycrow 1775
			wprintf(L"Syntax:\n\t%s --verifyscript <packagerscript>\n\t\tThis will read a packager script and check its correct without creating the resulting spk/xsp file\n", cmd.c_str() );
1 cycrow 1776
		else
1777
			LoadPackagerScript ( argv[2], true );
1778
	}
197 cycrow 1779
	else if ( command == L"--extractship" )
1 cycrow 1780
	{
1781
		if ( argc < 4 )
197 cycrow 1782
			wprintf(L"Syntax:\n\t%s --extractship <modfile> <xspfile> [shipid]\n\t\tThis will create an xsp ship file by extracting from a mod file\n", cmd.c_str() );
1 cycrow 1783
		else
1784
		{
1785
			if ( argc < 5 )
197 cycrow 1786
				ExtractShip ( argv[2], argv[3], L"" );
1 cycrow 1787
			else
1788
				ExtractShip ( argv[2], argv[3], argv[4] );
1789
		}
1790
	}
197 cycrow 1791
	else if (command == L"--generateupdatefile")
1 cycrow 1792
	{
197 cycrow 1793
		if (argc < 3)
1794
			wprintf(L"Syntax:\n\t%s --generateupdatefile <package>\n\t\tThis will generate the update file to allow auto updates for a package\n", cmd.c_str());
1795
		else
1796
			GenerateUpdateFile(argv[2]);
126 cycrow 1797
	}
197 cycrow 1798
	else if (command == L"--packagelist")
126 cycrow 1799
	{
1800
		if (argc < 3)
197 cycrow 1801
			wprintf(L"Syntax:\n\t%s --packagelist <filenames>\n\t\tThis will generate the update file to allow downloading packages from a server.\n", cmd.c_str());
1 cycrow 1802
		else
126 cycrow 1803
			GenerateUpdateList(argc, argv);
1 cycrow 1804
	}
1805
 
1806
	// not a valid switch, display syntax
1807
	else
298 cycrow 1808
		PrintSyntax(cmd);
1 cycrow 1809
 
1810
#ifdef _DEBUG
1811
	char pause;
1812
	scanf ( "%s", &pause );
1813
#endif
1814
 
1815
	return 0;
1816
}