Subversion Repositories spk

Rev

Rev 307 | Rev 315 | 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
 
298 cycrow 964
void ExtractFiles (const Utils::WString &sfile, const Utils::WString &dir, int game )
1 cycrow 965
{
966
	// First checks if the file exists by opening it
298 cycrow 967
	FILE *id = _wfopen(sfile.c_str(), L"rb+");
1 cycrow 968
	if ( !id )
969
	{
298 cycrow 970
		wprintf(L"Error: File, %s, doesn't exist\n", sfile.c_str() );
1 cycrow 971
		return;
972
	}
973
	fclose ( id );
974
 
129 cycrow 975
	CPackages packages;
226 cycrow 976
	packages.startup(L".", L".", L".");
129 cycrow 977
 
1 cycrow 978
	int check = CSpkFile::CheckFile ( sfile );
979
 
980
	// extracts a file from single packages file
981
	if ( check == SPKFILE_SINGLE || check == SPKFILE_BASE || check == SPKFILE_SINGLESHIP )
982
	{
983
		// creates the spkfile object
984
		CBaseFile *pBaseFile = 0;
985
		if ( check == SPKFILE_SINGLE )
986
			pBaseFile = (CBaseFile *)new CSpkFile();
987
		else if ( check == SPKFILE_SINGLESHIP )
988
			pBaseFile = (CBaseFile *)new CXspFile();
989
		else
990
			pBaseFile = new CBaseFile();
991
 
298 cycrow 992
		wprintf(L"Opening File, %s... ", sfile.c_str());
1 cycrow 993
		// reads the file into memory
994
		// the SPKREAD_NODATA flag causes it to just read the settings, and skips the file data
175 cycrow 995
		if ( !pBaseFile->readFile(sfile, SPKREAD_NODATA))
1 cycrow 996
		{
298 cycrow 997
			wprintf(L"(Error)\nUnable to open the file, %s\n", sfile.c_str() );
1 cycrow 998
			return;
999
		}
298 cycrow 1000
		wprintf(L"(Done)\n");
1 cycrow 1001
 
134 cycrow 1002
		if(game == 0)
298 cycrow 1003
			wprintf(L"Extracting all files from archive...\n\n");
1 cycrow 1004
		else
197 cycrow 1005
			wprintf(L"Extracting %s files from archive...\n\n", packages.GetGameExe()->gameNameFromType(game - 1).c_str());
134 cycrow 1006
 
1007
		CFileProgress info(&packages, pBaseFile, "Extracting");
1008
		if (packages.extractAll(pBaseFile, dir, game, true, &info) )
298 cycrow 1009
			wprintf(L"\n(Done)\nFiles have been extracted successfully\n");
134 cycrow 1010
		else
298 cycrow 1011
			wprintf(L"\n(Error)\nThere was a problem extracting the files\n");
1 cycrow 1012
	}
1013
	else
298 cycrow 1014
		wprintf(L"Invalid package file, %s\n", sfile.c_str());
1 cycrow 1015
}
1016
 
1017
 
1018
/*
1019
	Func:	AppendMultiFile
1020
	Args:	String toFile	- The spk file to append onto
1021
			String addfile	- The spk file to append
1022
	Desc:	Appends a spk file into a Multi-Spk Archive
1023
			If toFile is a single spk file, it will be converted to a Multi-Spk File
1024
			if addfile is a Multi-Spk file, all files from it will be appended
1025
*/
298 cycrow 1026
void AppendMultiFile(const Utils::WString &toFile, const Utils::WString &addfile)
1 cycrow 1027
{
1028
	// create destination object
1029
	CMultiSpkFile spkfile;
1030
 
1031
	// checks the destination
298 cycrow 1032
	int checkto = CSpkFile::CheckFile(toFile);
1 cycrow 1033
 
1034
	// if the destination is a single file, then convert it to a Multi-Spk package
1035
	if ( checkto == SPKFILE_SINGLE )
1036
	{
1037
		// adds the single file to the Multi-Spk object
1038
		// Add file also reads it into memory
298 cycrow 1039
		if ( !spkfile.addFile(toFile))
1 cycrow 1040
		{
298 cycrow 1041
			wprintf(L"Error: Unable to create Multi-Spk file\n");
1 cycrow 1042
			return;
1043
		}
1044
	}
1045
	else
1046
	{
1047
		// if its already a multispk file, then simply open it and read it to memory
298 cycrow 1048
		if ( !spkfile.readFile(toFile))
1 cycrow 1049
		{
298 cycrow 1050
			wprintf(L"Error: Unable to open Multi-Spk file, %s\n", toFile.c_str());
1 cycrow 1051
			return;
1052
		}
1053
	}
1054
 
1055
	// now add the file into the Multi-Spk Object
1056
	// the AddFile function will handle both single and Multi-Spk files
1057
	// So you dont need to test what the appending file is
298 cycrow 1058
	if ( spkfile.addFile(addfile))
1 cycrow 1059
	{
1060
 
1061
		// if it added correctly, then simply write the new Multi-Spk Object to disk
298 cycrow 1062
		wprintf(L"File, %s, has been added to Multi-Spk Package\n", addfile.c_str());
1063
		wprintf(L"Saving Multi-Spk File: %s... ", toFile.c_str());
1064
		if ( spkfile.writeFile(toFile))
1065
			wprintf(L"(Done)\n");
1 cycrow 1066
		else
298 cycrow 1067
			wprintf(L"(Error)\n");
1 cycrow 1068
	}
1069
	else
298 cycrow 1070
		wprintf(L"Error: Unable to add files, %s, to Multi-Spk Package\n", addfile.c_str());
1 cycrow 1071
}
1072
 
1073
/*
1074
	Func:	ExtractFile
1075
	Args:	String sfile	- the spk file to read from
1076
			String type		- the type of the file to find
1077
			String addfile	- The filename to extract
1078
			String dir		- The directory to extract to
1079
	Desc:	Finds and extracts a file from a Spk Package
1080
*/
298 cycrow 1081
void ExtractFile (const Utils::WString &sfile, const Utils::WString &type, const Utils::WString &addfile, const Utils::WString &dir)
1 cycrow 1082
{
1083
	// First checks if the file exists by opening it
298 cycrow 1084
	FILE *id = _wfopen(sfile.c_str(), L"rb+");
1 cycrow 1085
	if ( !id )
1086
	{
298 cycrow 1087
		wprintf(L"Error: File, %s, doesn't exist\n", sfile.c_str());
1 cycrow 1088
		return;
1089
	}
1090
	fclose ( id );
1091
 
1092
	// now check the type of file it is, using the static function CheckFile(filename).
1093
	// This will return the type of file
1094
	//		SPK_INVALID		- Invalid file format, wont be able to open
1095
	//		SPK_SINGLE		- A Single spk package file
1096
	//		SPK_MULTI		- A Multi-Spk Archive
1097
	//		SPK_BASE		- A Base File
1098
	//		SPK_SINGLESHIP	- A Ship file
1099
	int check = CSpkFile::CheckFile ( sfile );
1100
 
1101
	// extracts a file from single packages file
1102
	if ( check == SPKFILE_SINGLE || check == SPKFILE_BASE || check == SPKFILE_SINGLESHIP )
1103
	{
1104
		// first get the file type is valid
1105
		// converts the string type into its filetype flag
170 cycrow 1106
		FileType t = GetFileTypeFromString(type);
1 cycrow 1107
		// incorrect text type, display the error
170 cycrow 1108
		if (t == FileType::FILETYPE_UNKNOWN)
1 cycrow 1109
		{
298 cycrow 1110
			wprintf(L"The file type \"%s\" is invalid\n", type.c_str());
1 cycrow 1111
			return;
1112
		}
1113
 
1114
		// creates the spkfile object
1115
		CBaseFile *pBaseFile = 0;
1116
		if ( check == SPKFILE_SINGLE )
1117
			pBaseFile = (CBaseFile *)new CSpkFile();
1118
		else if ( check == SPKFILE_SINGLESHIP )
1119
			pBaseFile = (CBaseFile *)new CXspFile();
1120
		else
1121
			pBaseFile = new CBaseFile();
1122
 
298 cycrow 1123
		wprintf(L"Opening File, %s... ", sfile.c_str());
1 cycrow 1124
		// reads the file into memory
1125
		// the SPKREAD_NODATA flag causes it to just read the settings, and skips the file data
175 cycrow 1126
		if ( !pBaseFile->readFile(sfile, SPKREAD_NODATA))
1 cycrow 1127
		{
298 cycrow 1128
			wprintf(L"(Error)\nUnable to open the file, %s\n", sfile.c_str());
1 cycrow 1129
			return;
1130
		}
1131
 
1132
		// No read all the file data into memory
1133
		// This can be done all together in the ReadFile function
1134
		// 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
1135
		pBaseFile->ReadAllFilesToMemory ();
1136
 
298 cycrow 1137
		wprintf(L"(Done)\n");
1 cycrow 1138
 
1139
		// uses the FindFile function to find the selected file in the archive
1140
		// requires the filename, type and custom directory (only for Extra File Type)
170 cycrow 1141
		C_File *f = pBaseFile->findFile(addfile, t);
1 cycrow 1142
		if ( !f )
1143
		{
298 cycrow 1144
			wprintf(L"Unable to find the file \"%s\" in the package\n", addfile.c_str());
1 cycrow 1145
			return;
1146
		}
1147
 
1148
		// creates the directory so it can be extracted
1149
		CDirIO Dir(dir);
160 cycrow 1150
		if ( !Dir.create(f->getDirectory(pBaseFile)) )
1 cycrow 1151
		{
298 cycrow 1152
			wprintf(L"Unable to create the directory \"%s\" to extract into\n", dir.c_str());
1 cycrow 1153
			return;
1154
		}
1155
 
1156
		// sets up the progress pointer
1157
		// if it uses 7zip, the progress will be displayed in the command prompt
1158
		MyProgress progress(0);
298 cycrow 1159
		wprintf(L"Extracting the file from package\n\t>");
1 cycrow 1160
 
1161
		// Extracts the file to the specified directory
175 cycrow 1162
		if ( !pBaseFile->extractFile(f, dir, true, &progress))
298 cycrow 1163
			wprintf(L"< (Error)\nUnable to extract the file\n");
1 cycrow 1164
		else
1165
		{
1166
			progress.PrintDone ();
298 cycrow 1167
			wprintf(L"< (Done)\nFile has been extracted successfully\n");
1 cycrow 1168
		}
1169
	}
1170
 
1171
	// the file is a Multi-Spk File, extracts a single spk file from the archive
1172
	else if ( check == SPKFILE_MULTI )
1173
	{
1174
		// creates MultiSpkFile object
1175
		CMultiSpkFile spkfile;
298 cycrow 1176
		wprintf(L"Opening Multi-SPK file, %s...", sfile.c_str());
1 cycrow 1177
 
1178
		// reads the MultiSpkFile into memory
177 cycrow 1179
		if ( !spkfile.readFile(sfile))
1 cycrow 1180
		{
298 cycrow 1181
			wprintf(L"(Error)\nUnable to open the Multi-SPK file, %s\n", sfile.c_str());
1 cycrow 1182
			return;
1183
		}
298 cycrow 1184
		wprintf(L"(Done)\n");
1 cycrow 1185
 
1186
		// searchs the archive for a matching file
177 cycrow 1187
		const SMultiSpkFile *ms = spkfile.findFile(type);
1 cycrow 1188
		if ( !ms )
1189
		{
298 cycrow 1190
			wprintf(L"Unable to find the file \"%s\" in the package\n", type.c_str());
1 cycrow 1191
			return;
1192
		}
1193
 
1194
		// extracts the file from the archive, to the given directory
298 cycrow 1195
		wprintf(L"Extracting SPK file, %s, from package... ", ms->sName.c_str());
177 cycrow 1196
		if (spkfile.extractFile(ms, addfile))
298 cycrow 1197
			wprintf(L"(Done)\n");
1 cycrow 1198
		else
298 cycrow 1199
			wprintf(L"(Error)\n");
1 cycrow 1200
	}
1201
	else
298 cycrow 1202
		wprintf(L"Error: Invalid file format, unable to open\n");
1 cycrow 1203
}
1204
 
1205
/*
1206
	Func:	SplitMulti
1207
	Args:	String filename - the filename of the multispk file to open
1208
			String dest		- The destination directory to extract the files to
1209
	Desc:	Splits a multi-spk file into its seperate spk files
1210
*/
298 cycrow 1211
void SplitMulti(const Utils::WString &filename, const Utils::WString &dest)
1 cycrow 1212
{
1213
	// Check the file type, Must return SPKFILE_MULTI, otherwise its not a Multi-Spk Packages
298 cycrow 1214
	if ( CSpkFile::CheckFile(filename) != SPKFILE_MULTI )
1 cycrow 1215
	{
298 cycrow 1216
		wprintf(L"Error: The file is not a Multi-Spk packages\n");
1 cycrow 1217
		return;
1218
	}
1219
 
298 cycrow 1220
	wprintf(L"Spliting Multi-SPK File to, %s... ", dest.c_str());
1 cycrow 1221
 
1222
	// create the MultiSpkFile object
1223
	CMultiSpkFile spkfile;
1224
 
1225
	// Splits the files to the destination
298 cycrow 1226
	if ( !spkfile.splitMulti(filename, dest))
1 cycrow 1227
	{
298 cycrow 1228
		wprintf(L"(Error)\nUnable to split Multi-SPK package, %s\n", filename.c_str());
1 cycrow 1229
		return;
1230
	}
1231
 
298 cycrow 1232
	wprintf(L"(Done)\n");
1 cycrow 1233
}
1234
 
197 cycrow 1235
Utils::WString GetAsk(const Utils::WString &command)
1 cycrow 1236
{
197 cycrow 1237
	wprintf(L"(ASK) Enter the value for, %s: ", command.c_str());
1 cycrow 1238
	return GetInput();
1239
}
1240
 
126 cycrow 1241
void GenerateUpdateList(int argc, char **argv)
1242
{
226 cycrow 1243
	Utils::WString currentDir = CFileIO(Utils::WString::FromString(argv[0])).dir();
126 cycrow 1244
	CDirIO Dir(currentDir);
1245
 
196 cycrow 1246
	Utils::WStringList list;
126 cycrow 1247
	for (int i = 2; i < argc; ++i)
1248
	{
298 cycrow 1249
		Utils::WString file(argv[i]);
126 cycrow 1250
		if (CFileIO::Exists(file))
196 cycrow 1251
			list.pushBack(file, L"file");
126 cycrow 1252
		else if (CFileIO::Exists(Dir.file(file)))
196 cycrow 1253
			list.pushBack(Dir.file(file), L"file");
126 cycrow 1254
		else if (CDirIO::Exists(file))
196 cycrow 1255
			list.pushBack(file, L"dir");
126 cycrow 1256
		else if (CDirIO::Exists(Dir.dir(file)))
196 cycrow 1257
			list.pushBack(Dir.dir(file), L"dir");
126 cycrow 1258
		else
196 cycrow 1259
			list.pushBack(file, L"pattern");
126 cycrow 1260
	}
1261
 
1262
	if (list.empty())
298 cycrow 1263
		wprintf(L"unable to find any packages");
126 cycrow 1264
	else
1265
	{
196 cycrow 1266
		Utils::WStringList fileList;
126 cycrow 1267
		for (auto itr = list.begin(); itr != list.end(); itr++)
1268
		{
196 cycrow 1269
			Utils::WString file = (*itr)->str;
1270
			Utils::WString data = (*itr)->data;
1271
			if (data == L"file")
126 cycrow 1272
				fileList.pushBack(file);
196 cycrow 1273
			else if (data == L"dir")
126 cycrow 1274
			{
1275
				CDirIO dir(file);
196 cycrow 1276
				Utils::WStringList d;
126 cycrow 1277
				if (dir.dirList(d))
1278
				{
1279
					for (auto itr2 = d.begin(); itr2 != d.end(); itr2++)
1280
					{
196 cycrow 1281
						Utils::WString ext = CFileIO((*itr2)->str).extension().lower();
1282
						if(ext == L"xsp" || ext == L"spk")
126 cycrow 1283
							fileList.pushBack(dir.file((*itr2)->str));
1284
					}
1285
				}
1286
			}
226 cycrow 1287
			else if (data == L"pattern")
126 cycrow 1288
			{				
1289
				CFileIO f(file);
1290
				CDirIO dir(f.dir());
1291
 
1292
				if (!dir.exists())
1293
					dir = CDirIO(Dir.dir(dir.dir()));
1294
 
196 cycrow 1295
				Utils::WStringList d;
1296
				if (dir.dirList(d, Utils::WString::Null(), f.filename()))
126 cycrow 1297
				{
1298
					for (auto itr2 = d.begin(); itr2 != d.end(); itr2++)
1299
					{
196 cycrow 1300
						Utils::WString ext = CFileIO((*itr2)->str).extension().lower();
1301
						if (ext == L"xsp" || ext == L"spk")
126 cycrow 1302
							fileList.pushBack(dir.file((*itr2)->str));
1303
					}
1304
				}
1305
			}
1306
		}
1307
 
1308
		if (fileList.empty())
298 cycrow 1309
			wprintf(L"unable to find any packages");
126 cycrow 1310
		else
1311
		{
1312
			CPackages packages;
1313
 
197 cycrow 1314
			Utils::WStringList filedata;
126 cycrow 1315
			for (auto itr = fileList.begin(); itr != fileList.end(); itr++)
1316
			{
196 cycrow 1317
				wprintf(L"Reading file: %s\n", (*itr)->str.c_str());
126 cycrow 1318
 
1319
				int error = 0;
196 cycrow 1320
				CBaseFile *p = packages.openPackage((*itr)->str.toString(), &error, 0, SPKREAD_NODATA);
126 cycrow 1321
				if (!p)
1322
				{
1323
					printf("\tERROR!\n");
1324
					continue;
1325
				}
204 cycrow 1326
				wprintf(L"\tData extracted: %s %s by %s\n", p->name().c_str(), p->version().c_str(), p->author().c_str());
126 cycrow 1327
				if (!p->creationDate().empty())
204 cycrow 1328
					wprintf(L"\t\tCreated: %s\n", p->creationDate().c_str());
126 cycrow 1329
				if(!p->description().empty())
206 cycrow 1330
					wprintf(L"\t\t%s\n", p->description().c_str());
126 cycrow 1331
 
1332
				filedata.pushBack(CPackages::FormatAvailablePackageData(p));
1333
				delete p;
1334
			}
1335
 
1336
			if (filedata.empty())
298 cycrow 1337
				wprintf(L"unable to find any packages");
126 cycrow 1338
			else				
1339
			{
226 cycrow 1340
				Utils::WString dest = Dir.file(L"xpackagedata.dat");
126 cycrow 1341
				if (CFileIO(dest).writeFile(&filedata))
298 cycrow 1342
					wprintf(L"web update file, xpackagedata.dat, generated");
126 cycrow 1343
				else
298 cycrow 1344
					wprintf(L"unable to write update file");
126 cycrow 1345
			}
1346
		}
1347
	}
1348
}
1349
 
196 cycrow 1350
void GenerateUpdateFile(const Utils::WString &spkfile)
1 cycrow 1351
{
182 cycrow 1352
	if (!CFileIO::Exists(spkfile))
1 cycrow 1353
	{
196 cycrow 1354
		wprintf(L"Error: The package file, %s, does not exist", spkfile.c_str());
1 cycrow 1355
		return;
1356
	}
1357
 
197 cycrow 1358
	int check = CSpkFile::CheckFile(spkfile);
1 cycrow 1359
	if ( check == SPKFILE_MULTI )
1360
	{
298 cycrow 1361
		wprintf(L"Error: Multi-Package files currently not supported\n");
1 cycrow 1362
		return;
1363
	}
1364
	else if ( check == SPKFILE_OLD )
1365
	{
298 cycrow 1366
		wprintf(L"Error: unable to read old format spk file, try spkconvert first\n");
1 cycrow 1367
		return;
1368
	}
1369
	else if ( check == SPKFILE_INVALID )
1370
	{
196 cycrow 1371
		wprintf(L"Error: %s doesn't appear to be a valid package file\n", spkfile.c_str());
1 cycrow 1372
		return;
1373
	}
1374
 
1375
	CPackages p;
1376
	int error;
298 cycrow 1377
	CBaseFile *package = p.openPackage(spkfile, &error, 0, SPKREAD_NODATA);
1 cycrow 1378
	if ( !package )
1379
	{
196 cycrow 1380
		wprintf(L"Error: unable to open package file, %s, Error=%d\n", spkfile.c_str(), error);
1 cycrow 1381
		return;
1382
	}
1383
 
196 cycrow 1384
	Utils::WString file = package->createUpdateFile(CFileIO(spkfile).dir());
102 cycrow 1385
	if ( file.empty() )
196 cycrow 1386
		wprintf(L"Error: unable to create update file for: %s, Directory=%s\n", spkfile.c_str(), CFileIO(spkfile).dir().c_str());
1 cycrow 1387
	else
196 cycrow 1388
		wprintf(L"Update file: %s has been created for package, %s\n", file.c_str(), spkfile.c_str());
1 cycrow 1389
 
1390
	delete package;
1391
 
1392
}
1393
 
298 cycrow 1394
void GeneratePackagerScript(const Utils::WString &spkfile, Utils::WString toFile, int game)
1 cycrow 1395
{
126 cycrow 1396
	if (!CFileIO::Exists(spkfile))
1 cycrow 1397
	{
298 cycrow 1398
		wprintf(L"Error: The package file, %s, does not exist", spkfile.c_str());
1 cycrow 1399
		return;
1400
	}
1401
 
1402
	int check = CSpkFile::CheckFile(spkfile);
1403
	if ( check == SPKFILE_MULTI )
1404
	{
298 cycrow 1405
		wprintf(L"Error: Cant generate a script from a multi-spk file\n");
1 cycrow 1406
		return;
1407
	}
1408
	else if ( check == SPKFILE_OLD )
1409
	{
298 cycrow 1410
		wprintf(L"Error: unable to read old format spk file, try spkconvert first\n");
1 cycrow 1411
		return;
1412
	}
1413
	else if ( check == SPKFILE_INVALID )
1414
	{
298 cycrow 1415
		wprintf(L"Error: %s doesn't appear to be a spk file\n", spkfile.c_str());
1 cycrow 1416
		return;
1417
	}
1418
 
1419
	CPackages p;
226 cycrow 1420
	p.startup(L".", L".", L".");
1 cycrow 1421
	int error;
182 cycrow 1422
	CBaseFile *package = p.openPackage(spkfile, &error, 0, SPKREAD_NODATA);
1 cycrow 1423
	if ( !package )
1424
	{
298 cycrow 1425
		wprintf(L"Error: unable to open package files, %s, Error=%d\n", spkfile.c_str(), error);
1 cycrow 1426
		return;
1427
	}
1428
 
210 cycrow 1429
	Utils::WStringList list;
127 cycrow 1430
	if ( !p.generatePackagerScript(package, true, &list, game) )
1 cycrow 1431
	{
298 cycrow 1432
		wprintf(L"Error: Unable to generate packager script\n");
1 cycrow 1433
		return;
1434
	}
1435
 
298 cycrow 1436
	if ( toFile.empty() )
1437
		toFile = CFileIO(spkfile).GetDirIO().file(package->name() + L"_" + package->author() + L".sps");
1 cycrow 1438
 
1439
	// save package file
298 cycrow 1440
	if ( CFileIO(toFile).writeFile(&list) )
1441
		wprintf(L"Packager script, %s, has been geenrated\n", toFile.c_str());
1 cycrow 1442
	else
298 cycrow 1443
		wprintf(L"Error: unable to write packager script, %s\n", toFile.c_str());
1 cycrow 1444
 
1445
	delete package;
1446
}
1447
 
1448
/*
1449
	Func:	LoadPackagerScript
1450
	Args:	String filename	-	The filename of the packager script to load
1451
	Desc:	Loads the packager scripts and creates a spk/xsp file from it
1452
*/
311 cycrow 1453
void LoadPackagerScript(const Utils::CommandLine &cmd, bool verify)
1 cycrow 1454
{
311 cycrow 1455
	Utils::WString filename = cmd.arg(1);
1456
	// check if the file exists in the current directory
1457
	if (!filename.contains(L":"))
1458
		filename = CDirIO(g_dir).file(filename);
1459
 
1460
	if (!CFileIO::Exists(filename))
1 cycrow 1461
	{
197 cycrow 1462
		wprintf(L"Error: The packager script, %s, does not exist", filename.c_str());
1 cycrow 1463
		return;
1464
	}
1465
 
197 cycrow 1466
	Utils::WString myDoc;
1 cycrow 1467
#ifdef _WIN32
1468
	TCHAR pszPath[MAX_PATH];
1469
	if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, pszPath)))
1470
	{
311 cycrow 1471
		myDoc = pszPath;
1 cycrow 1472
	}
1473
#endif
1474
 
1475
	if ( verify )
197 cycrow 1476
		wprintf(L"Verifying Packager Script: %s\n", filename.c_str());
1 cycrow 1477
 
1478
	CPackages p;
197 cycrow 1479
	p.startup(L".", L".", myDoc);
1480
	Utils::WStringList malformed, unknown;
1 cycrow 1481
 
197 cycrow 1482
	Utils::WString curDir = CFileIO(filename).dir();
196 cycrow 1483
	if ( curDir.empty() ) curDir = L"./";
1 cycrow 1484
 
197 cycrow 1485
	Utils::WStringList variables;
1486
	variables.pushBack(L"$PATH", curDir);
1 cycrow 1487
 
311 cycrow 1488
	CFileProgress info(&p, NULL, "Added File");
197 cycrow 1489
	CBaseFile *package = p.loadPackagerScript(filename, -1, (verify) ? NULL : &GetAsk, &malformed, &unknown, &variables, &info);
1 cycrow 1490
 
134 cycrow 1491
	printf("\n");
1492
 
1 cycrow 1493
	if ( verify )
1494
	{
131 cycrow 1495
		if ( !malformed.empty() )
1 cycrow 1496
		{
306 cycrow 1497
			printf("Malformed Lines (%lu):\n", static_cast<unsigned long>(malformed.size()));
131 cycrow 1498
			for(auto itr = malformed.begin(); itr != malformed.end(); itr++)				
197 cycrow 1499
				wprintf(L"\t(Line %3d) %s\n", (*itr)->data.toInt(), (*itr)->str.c_str());
1 cycrow 1500
		}
131 cycrow 1501
		if ( !unknown.empty() )
1 cycrow 1502
		{
306 cycrow 1503
			printf("Unknown Commands (%lu):\n", static_cast<unsigned long>(unknown.size()));
131 cycrow 1504
			for (auto itr = unknown.begin(); itr != unknown.end(); itr++)
197 cycrow 1505
				wprintf(L"\t* Command: %s = %s\n", (*itr)->str.c_str(), (*itr)->data.c_str());
1 cycrow 1506
		}
1507
	}
1508
 
1509
	if ( !package )
1510
	{
1511
		if ( verify )
1512
			printf("Error: There are errors in the packager script which prevents it from being created\n");
1513
		else
197 cycrow 1514
			wprintf(L"Error: Unable to create package, from script: %s\n", filename.c_str());
1 cycrow 1515
	}
1516
	else
1517
	{
197 cycrow 1518
		Utils::WString saveto = package->filename();
1519
		saveto = saveto.findReplace(L"$DEFAULTDIR", curDir + L"/");
1520
		saveto = saveto.findReplace(L"$PATH", curDir);
1521
		saveto = saveto.findReplace(L"\\", L"/");
1522
		saveto = saveto.findReplace(L"//", L"/");
1523
		if ( !saveto.right(4).Compare(L".spk") && package->GetType() != TYPE_XSP )
1524
			saveto += L".spk";
1525
		else if ( !saveto.right(4).Compare(L".xsp") && package->GetType() == TYPE_XSP )
1526
			saveto += L".xsp";
196 cycrow 1527
		wprintf(L"Saving file to: %s\n", CFileIO(saveto).fullFilename().c_str());
1 cycrow 1528
		if ( verify )
1529
			printf("Package can be created from this script\n");
1530
		else
1531
		{
1532
			// write script
197 cycrow 1533
			if ( package->writeFile(saveto.toString()) )
1 cycrow 1534
			{
1535
				if ( package->AutoGenerateUpdateFile() )
134 cycrow 1536
					package->createUpdateFile(CFileIO(saveto).dir());
197 cycrow 1537
				wprintf(L"Package: %s was created\n", saveto.c_str());
1 cycrow 1538
			}
1539
			else
1540
				printf("Error! There was a problem writing the package\n");
1541
		}
1542
 
204 cycrow 1543
		saveto = package->exportFilename();
131 cycrow 1544
		if ( !saveto.empty() ) {
197 cycrow 1545
			saveto = saveto.findReplace(L"$DEFAULTDIR", curDir + L"/");
1546
			saveto = saveto.findReplace(L"$PATH", curDir);
1547
			saveto = saveto.findReplace(L"\\", L"/");
1548
			saveto = saveto.findReplace(L"//", L"/");
196 cycrow 1549
			wprintf(L"Exporting file to: %s\n", CFileIO(saveto).fullFilename().c_str());
1 cycrow 1550
			if ( verify )
1551
				printf("Package can be exported from this script\n");
1552
			else
1553
			{
1554
				// export
197 cycrow 1555
				if ( package->saveToArchive(saveto.toString(), 0, p.GetGameExe())) {
1 cycrow 1556
					if ( package->IsAnyGameInPackage() ) {
197 cycrow 1557
						for ( int i = 0; i < p.GetGameExe()->numGames(); i++ ) {
1 cycrow 1558
							if ( package->IsGameInPackage(i + 1) ) {								
197 cycrow 1559
								Utils::WString exportFile = CFileIO(saveto).dir() + L"/" + CFileIO(saveto).baseName() + L"_" + CBaseFile::ConvertGameToString(i + 1) + L"." + CFileIO(saveto).extension();
196 cycrow 1560
								package->saveToArchive(exportFile.toString(), i + 1, p.GetGameExe());
1 cycrow 1561
							}
1562
						}
1563
					}
197 cycrow 1564
					wprintf(L"Package: %s was exported\n", saveto.c_str());
1 cycrow 1565
				}
1566
				else
1567
					printf("Error! There was a problem exporting the package\n");
1568
			}
1569
		}
1570
	}
1571
}
1572
 
311 cycrow 1573
Utils::WString GetFileTypes()
1574
{
1575
	Utils::WString fileTypes;
1576
	for (int i = 0; i < FILETYPE_MAX; i++)
1577
	{
1578
		Utils::WString sT = GetFileTypeString(i);
1579
		if (!sT.empty())
1580
		{
1581
			if (fileTypes.empty())
1582
				fileTypes = sT;
1583
			else
1584
			{
1585
				fileTypes += L", ";
1586
				fileTypes += sT;
1587
			}
1588
		}
1589
	}
1590
	return fileTypes;
1591
}
1592
 
1 cycrow 1593
/*
1594
	Main entry point to program
1595
*/
1596
int main ( int argc, char **argv )
1597
{
1598
	// display program header to command prompt
311 cycrow 1599
	printf ( "\nSPKTool V1.51 (SPK File Version %.2f) 17/05/2025 Created by Cycrow\n\n", (float)FILEVERSION );
1 cycrow 1600
 
307 cycrow 1601
	Utils::CommandLine cmd(argc, argv);
1602
	g_dir = cmd.cmdDir();
1 cycrow 1603
	g_read = false;
1604
 
1605
	// not enough arguments, display the syntax and exit
311 cycrow 1606
	if (cmd.argCount() < 1)
1 cycrow 1607
	{
307 cycrow 1608
		PrintSyntax(cmd.cmdName());
1 cycrow 1609
		exit ( 1 );
1610
	}
1611
 
1612
	// get the command flag
311 cycrow 1613
	Utils::WString command(cmd.arg(0));
1 cycrow 1614
 
1615
	// display the contents of the spk file
307 cycrow 1616
	if ( command == L"-v" || command == L"-view" || command == L"-version")
1 cycrow 1617
	{
1618
		if ( argc < 3 )
307 cycrow 1619
			wprintf(L"Syntax: %s -v <spkfile>\n\tWill open and display the contents of the spkfile\n", cmd.cmdName().c_str());
1 cycrow 1620
		else
134 cycrow 1621
			DisplayVersion(argv[2]);
1 cycrow 1622
	}
1623
 
1624
	// creates a new spk file
307 cycrow 1625
	else if ( command == L"-c" || command == L"-create" )
1 cycrow 1626
	{
1627
		if ( argc < 3 )
307 cycrow 1628
			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 1629
		else
1630
			CreateFile ( argv[2] );
1631
	}
1632
 
1633
	// appends a file onto the spk archive
307 cycrow 1634
	else if ( command == L"-a" || command == L"-append" )
1 cycrow 1635
	{
1636
		if ( argc < 4 )
311 cycrow 1637
			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 1638
		else
1639
		{
298 cycrow 1640
			Utils::WString arg4;
1 cycrow 1641
			if ( argc > 4 )
1642
				arg4 = argv[4];
1643
			AppendFile ( argv[2], argv[3], arg4 );
1644
		}
1645
	}
1646
 
1647
	// removes a file from the spk archive
307 cycrow 1648
	else if ( command == L"-r" || command == L"-remove" || command == L"-removespk" )
1 cycrow 1649
	{
1650
		if ( argc < 4 )
1651
		{
311 cycrow 1652
			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 1653
			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 1654
		}
1655
		else
1656
		{
298 cycrow 1657
			Utils::WString arg4;
1 cycrow 1658
			if ( argc > 4 )
1659
				arg4 = argv[4];
1660
			RemoveFile ( argv[2], argv[3], arg4 );
1661
		}
1662
	}
1663
 
1664
	// extracts a file from a spk file
307 cycrow 1665
	else if ( command == L"-extractspk" )
1 cycrow 1666
	{
1667
		if ( argc < 4 )
307 cycrow 1668
			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 1669
		else
1670
		{
298 cycrow 1671
			Utils::WString arg4;
1 cycrow 1672
			if ( argc > 4 )
1673
				arg4 = argv[3];
298 cycrow 1674
			ExtractFile(argv[2], argv[3], arg4, Utils::WString::Null());
1 cycrow 1675
		}
1676
	}
307 cycrow 1677
	else if ( command == L"-x" || command == L"-extract")
1 cycrow 1678
	{
1679
		if ( argc < 4 )
1680
		{
311 cycrow 1681
			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 1682
			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 1683
		}
1684
		else
1685
		{
298 cycrow 1686
			Utils::WString arg5, arg4;
1 cycrow 1687
			if ( argc > 5 )
1688
				arg5 = argv[5];
1689
			if ( argc > 4 )
1690
				arg4 = argv[4];
134 cycrow 1691
			ExtractFile(argv[2], argv[3], arg4, arg5);
1 cycrow 1692
		}
1693
	}
1694
 
1695
	// extracts all the files from an archive
307 cycrow 1696
	else if ( command == L"-e" || command == L"-extractall" )
1 cycrow 1697
	{
1698
		if ( argc < 3 )
307 cycrow 1699
			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 1700
		else
1701
		{
298 cycrow 1702
			Utils::WString arg4;
1 cycrow 1703
			if ( argc > 4 )
1704
				arg4 = argv[4];
134 cycrow 1705
			ExtractFiles(argv[2], arg4, CBaseFile::GetGameFromString(argv[3]));
1 cycrow 1706
		}
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
}