Subversion Repositories spk

Rev

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