Subversion Repositories spk

Rev

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