Subversion Repositories spk

Rev

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