Subversion Repositories spk

Rev

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

Rev Author Line No. Line
1 cycrow 1
#include <spk.h>
305 cycrow 2
#include "Utils/CommandLine.h"
3
 
1 cycrow 4
void PrintError ( int err )
5
{
6
	switch ( err )
7
	{
8
		case CATERR_NODATFILE:
9
			printf ( "No dat file found\n" );
10
			break;
11
		case CATERR_NOCATFILE:
12
			printf ( "Unable to open cat file\n" );
13
			break;
14
		case CATERR_FILEEMPTY:
15
			printf ( "Cat file is empty\n" );
16
			break;
17
		case CATERR_READCAT:
18
			printf ( "Unable to read cat file\n" );
19
			break;
20
		case CATERR_DECRYPT:
21
			printf ( "Unable to decrypt cat file\n" );
22
			break;
23
		case CATERR_MISMATCH:
317 cycrow 24
			printf("Dat file size mismatch\n");
1 cycrow 25
			break;
317 cycrow 26
		case CATERR_CREATED:
27
			printf("Cat file does not exist\n");
28
			break;
1 cycrow 29
	}
30
}
31
 
316 cycrow 32
void ListFiles(const Utils::CommandLine &cmd)
1 cycrow 33
{
316 cycrow 34
	const Utils::WString& filename = cmd.arg(1);
35
	const Utils::WString& searchmask = cmd.arg(2);
36
 
320 cycrow 37
	CFileIO File(!filename.contains(L":") ? CDirIO(cmd.cmdDir()).file(filename) : filename);
316 cycrow 38
	if(!File.exists())
39
		File.open(filename);
40
	if (!File.exists())
41
	{
42
		wprintf(L"Error: Cat file: %s, does not exist", filename.c_str());
43
		return;
44
	}
45
 
46
	wprintf(L"Listing files in %s...", filename.c_str());
197 cycrow 47
	if (!searchmask.empty())
48
		wprintf(L"(%s)", searchmask.c_str() );
49
	printf("\n");
1 cycrow 50
 
51
	CCatFile catfile;
316 cycrow 52
	int err = catfile.open(File.fullFilename(), L"", CATREAD_CATDECRYPT);
1 cycrow 53
 
316 cycrow 54
	int listed = 0;
1 cycrow 55
	if ( err == CATERR_NONE )
56
	{
57
		printf ( "Opened file\n" );
124 cycrow 58
		for (unsigned int i = 0; i < catfile.GetNumFiles(); i++)
1 cycrow 59
		{
60
			SInCatFile *file = catfile.GetFile ( i );
61
 
197 cycrow 62
			if (!searchmask.empty())
1 cycrow 63
			{
316 cycrow 64
				if (!file->sFile.match(searchmask))
1 cycrow 65
					continue;
66
			}
67
 
305 cycrow 68
			wprintf(L"[%9s] %s\n", SPK::GetSizeString(static_cast<unsigned long>(file->lSize)).c_str(), file->sFile.c_str() );
316 cycrow 69
			++listed;
1 cycrow 70
		}
316 cycrow 71
 
72
		if (!searchmask.empty())
73
			wprintf(L"\nListed Files: [%d/%d]", listed, static_cast<int>(catfile.GetNumFiles()));
1 cycrow 74
	}
75
	else
76
		PrintError ( err );
77
}
78
 
314 cycrow 79
void findAllFiles(const CDirIO &dirIO, const Utils::WString& dir, const Utils::WString& filepattern, Utils::WStringList& list)
80
{
81
	Utils::WStringList dirList;
82
	if (dirIO.dirList(dirList, dir, filepattern, true))
83
	{
84
		for (auto itr = dirList.begin(); itr != dirList.end(); itr++)
85
		{
86
			if (dirIO.isDir((*itr)->str))
87
				findAllFiles(dirIO, (*itr)->str, filepattern, list);
88
			else
89
				list.pushBack((*itr)->str);
90
		}
91
	}
92
}
93
 
320 cycrow 94
bool findFiles(Utils::WStringList &files, const Utils::WString &filepattern, const Utils::CommandLine &cmd)
127 cycrow 95
{
320 cycrow 96
	CFileIO File((filepattern.contains(L":")) ? filepattern : cmd.cmdDir() + L"/" + filepattern);
305 cycrow 97
	return File.GetDirIO().dirList(files, Utils::WString::Null(), File.filename(), true);
127 cycrow 98
}
1 cycrow 99
 
127 cycrow 100
 
317 cycrow 101
void ExtractFile (const Utils::CommandLine& cmd, const Utils::WString& fileMask, bool preserve)
1 cycrow 102
{
317 cycrow 103
	Utils::WString filename = cmd.arg(1) + fileMask;
104
	Utils::WString to = cmd.argCount() < 2 ? cmd.cmdDir() : cmd.arg(2);
105
	if (!to.contains(L":")) to = cmd.file()->dirIO().file(to);
106
	CDirIO destination(to);
107
 
108
	if (cmd.hasSwitch("preserve"))
109
		preserve = true;
110
 
222 cycrow 111
	if ( !filename.contains(L"::") ) return;
41 cycrow 112
 
222 cycrow 113
	Utils::WString catfile = filename.token(L"::", 1);
320 cycrow 114
	CFileIO File((catfile.contains(L":")) ? catfile : cmd.cmdDir() + L"/" + catfile);
222 cycrow 115
	Utils::WString filemask = filename.token(L"::", 2);
1 cycrow 116
 
117
	CCatFile cat;
317 cycrow 118
	int err = cat.open(File.fullFilename(), L"", CATREAD_DAT);
1 cycrow 119
	if ( err )
120
	{
121
		PrintError ( err );
122
		return;
123
	}
124
 
317 cycrow 125
	Utils::WStringList fileList;
126
	if (filemask.containsAny(L"*?"))
1 cycrow 127
	{
124 cycrow 128
		for (unsigned int i = 0; i < cat.GetNumFiles(); i++)
1 cycrow 129
		{
317 cycrow 130
			SInCatFile* f = cat.GetFile(i);
131
			if(f->sFile.match(filemask))
132
				fileList.pushBack(f->sFile);
1 cycrow 133
		}
134
	}
317 cycrow 135
	else
136
		fileList.pushBack(filemask);
1 cycrow 137
 
222 cycrow 138
	if (fileList.empty())
1 cycrow 139
	{
222 cycrow 140
		wprintf(L"Error: unable to find any files matching: %s\n", filemask.c_str() );
1 cycrow 141
		return;
142
	}
143
 
222 cycrow 144
	for (auto itr = fileList.begin(); itr != fileList.end(); itr++)
1 cycrow 145
	{
222 cycrow 146
		Utils::WString file = (*itr)->str;
147
		if (!cat.extractFile(file, to, preserve))
197 cycrow 148
			wprintf(L"Error: %s\n", cat.getErrorString().c_str() );
1 cycrow 149
		else
317 cycrow 150
			wprintf(L"File has been written: %s\n", preserve ? file.c_str() : CFileIO(file).filename().c_str());
1 cycrow 151
	}
152
}
153
 
314 cycrow 154
void AppendFile (const Utils::CommandLine& cmd)
1 cycrow 155
{
314 cycrow 156
	Utils::WString sFile = cmd.arg(2);
157
	Utils::WString filepattern = cmd.arg(1);
197 cycrow 158
	Utils::WString catfile;
159
	Utils::WString file;
1 cycrow 160
 
314 cycrow 161
	bool recursive = cmd.hasSwitch(L"recursive");
162
 
276 cycrow 163
	Utils::WString C_File = sFile.findReplace(L"\\", L"/");
1 cycrow 164
	bool doFile = false;
276 cycrow 165
	if ( !C_File.contains(L"::"))
1 cycrow 166
	{
276 cycrow 167
		catfile = C_File;
1 cycrow 168
		doFile = true;
169
	}
170
	else
171
	{
276 cycrow 172
		catfile = C_File.token(L"::", 1);
305 cycrow 173
		file = C_File.token(L"::", 2);
1 cycrow 174
	}
175
 
305 cycrow 176
	if (!catfile.contains(":"))
320 cycrow 177
		catfile = cmd.cmdDir() + L"/" + catfile;
305 cycrow 178
 
197 cycrow 179
	Utils::WStringList list;
314 cycrow 180
 
320 cycrow 181
	CFileIO File((filepattern.contains(L":")) ? filepattern : cmd.cmdDir() + L"/" + filepattern);
314 cycrow 182
	if (recursive)
183
		findAllFiles(File.dirIO(), L"", File.filename(), list);
184
	else
320 cycrow 185
		findFiles(list, filepattern, cmd);
158 cycrow 186
	if (!list.size())
1 cycrow 187
	{
197 cycrow 188
		wprintf(L"Error: no files found to add: %s\n", filepattern.c_str());
1 cycrow 189
		return;
190
	}
191
 
192
	CCatFile cat;
197 cycrow 193
	int err = cat.open(catfile, L"", CATREAD_CATDECRYPT);
305 cycrow 194
	if ( err && err != CATERR_CREATED)
1 cycrow 195
	{
196
		PrintError ( err );
197
		return;
198
	}
199
 
314 cycrow 200
 
201
	int added = 0;
158 cycrow 202
	for (auto itr = list.begin(); itr != list.end(); itr++)
1 cycrow 203
	{
314 cycrow 204
		if (File.dirIO().isDir((*itr)->str))
205
			continue;
206
 
197 cycrow 207
		Utils::WString filename = (*itr)->str;
305 cycrow 208
		Utils::WString toFile = file;
314 cycrow 209
		if (doFile)			
210
			toFile = File.dirIO().relativePath(filename);;
1 cycrow 211
 
305 cycrow 212
		if (!toFile.contains(L"."))
1 cycrow 213
		{
316 cycrow 214
			if (toFile[static_cast<unsigned long>(toFile.length() - 1)] != L'/')
305 cycrow 215
				toFile += L"/";
216
			toFile += CFileIO(filename).filename();
1 cycrow 217
		}
218
 
314 cycrow 219
		++added;
305 cycrow 220
		if (cat.appendFile(filename, toFile))
221
			wprintf(L"File %s has beed added to: %s::%s\n", filename.c_str(), catfile.c_str(), toFile.c_str());
1 cycrow 222
		else
197 cycrow 223
			wprintf(L"Error: Unable to add file: %s\n", filename.c_str());
1 cycrow 224
	}
314 cycrow 225
 
226
	if(!added)
227
		wprintf(L"Warning: Nothing to add: %s\n", filepattern.c_str());
1 cycrow 228
}
229
 
320 cycrow 230
void RemoveFile (const Utils::CommandLine &cmd)
1 cycrow 231
{
320 cycrow 232
	Utils::WString C_File = cmd.arg(1);
233
	Utils::WString remfile = cmd.arg(2);
234
 
235
	CFileIO File((C_File.contains(L":")) ? C_File : cmd.cmdDir() + L"/" + C_File);
1 cycrow 236
	// first open the cat file
237
	CCatFile cat;
320 cycrow 238
	int err = cat.open(File.fullFilename(), L"", CATREAD_CATDECRYPT);
1 cycrow 239
	if ( err )
240
	{
241
		PrintError ( err );
242
		return;
243
	}
244
 
320 cycrow 245
	std::vector<Utils::WString> files;
246
	if (remfile.containsAny(L"*?"))
1 cycrow 247
	{
320 cycrow 248
		for (unsigned int i = 0; i < cat.GetNumFiles(); i++)
249
		{
250
			SInCatFile* f = cat.GetFile(i);
251
			if (f->sFile.match(remfile))
252
				files.push_back(f->sFile);
253
		}
1 cycrow 254
	}
320 cycrow 255
	else
256
		files.push_back(remfile);
1 cycrow 257
 
320 cycrow 258
	for (auto itr = files.begin(); itr != files.end(); itr++)
259
	{
260
		if (!cat.removeFile(*itr))
261
			wprintf(L"Unable to find %s in cat file\n", itr->c_str());
262
		else
263
			wprintf(L"%s: has been removed from archive\n", itr->c_str());
264
	}
1 cycrow 265
}
266
 
320 cycrow 267
void UnpackFile(const Utils::CommandLine &cmd)
1 cycrow 268
{
320 cycrow 269
	const Utils::WString& file = cmd.arg(1);
270
	const Utils::WString & tofile = cmd.arg(2);
271
	const Utils::WString& ext = cmd.switchData(L"ext");
272
 
197 cycrow 273
	Utils::WStringList list;
320 cycrow 274
	if(!findFiles(list, file, cmd) || !list.size())
1 cycrow 275
	{
226 cycrow 276
		wprintf(L"Error: no files found to unpack: %s\n", file.c_str());
1 cycrow 277
		return;
278
	}
279
 
276 cycrow 280
	for(auto itr = list.first(); itr; itr = list.next())
1 cycrow 281
	{
312 cycrow 282
		Utils::WString filename = itr->str;
1 cycrow 283
 
284
		C_File f(filename);
276 cycrow 285
		if (!f.CheckValidFilePointer())
286
		{
320 cycrow 287
			CFileIO File((file.contains(L":")) ? file : cmd.cmdDir() + L"/" + file);
276 cycrow 288
			filename = File.GetDirIO().file(itr->str);
289
			f.setFilename(filename);
290
		}
291
 
292
		Utils::WString oldFilename = f.filename();
293
 
1 cycrow 294
		if ( !f.CheckValidFilePointer() )
197 cycrow 295
			wprintf(L"Error: %s doesn't exists\n", filename.c_str() );
1 cycrow 296
		else if ( !f.ReadFromFile() )
197 cycrow 297
			wprintf(L"Error: unable to open file: %s\n", filename.c_str() );
1 cycrow 298
		else
299
		{
312 cycrow 300
			if(!f.UnPCKFile())
301
				wprintf(L"Error: unable to unpack file: %s\n", filename.c_str());
1 cycrow 302
			else
312 cycrow 303
			{
304
				if (tofile.empty())
305
					f.changeFileExt(ext.empty() ? L"xml" : ext);
306
				else if (tofile.left(2) == L"*.")
307
					f.changeFileExt(CFileIO(tofile).extension().toString());
308
				else
309
					f.setFilename(tofile);
1 cycrow 310
 
312 cycrow 311
				if (!f.writeFilePointer())
312
					wprintf(L"Error: unable to write file: %s\n", tofile.c_str());
313
				else
314
					wprintf(L"%s has been unpacked to %s\n", oldFilename.c_str(), f.filename().c_str());
315
			}
1 cycrow 316
		}
317
	}
318
}
319
 
320 cycrow 320
void PackFile(const Utils::CommandLine &cmd)
1 cycrow 321
{
320 cycrow 322
	const Utils::WString& file = cmd.arg(1);
323
	const Utils::WString& tofile = cmd.arg(2);
324
 
197 cycrow 325
	Utils::WStringList list;
320 cycrow 326
	if(!findFiles(list, file, cmd) || !list.size())
1 cycrow 327
	{
276 cycrow 328
		wprintf(L"Error: no files found to pack: %s\n", file.c_str());
1 cycrow 329
		return;
330
	}
331
 
127 cycrow 332
	for(auto itr = list.first(); itr; itr = list.next())
1 cycrow 333
	{
312 cycrow 334
		Utils::WString filename = itr->str;
1 cycrow 335
 
336
		C_File f(filename);
337
		if ( !f.CheckValidFilePointer() )
197 cycrow 338
			wprintf(L"Error: %s doesn't exists\n", filename.c_str() );
1 cycrow 339
		else if ( !f.ReadFromFile() )
197 cycrow 340
			wprintf(L"Error: unable to open file: %s\n", filename.c_str() );
1 cycrow 341
		else if ( !f.PCKFile() )
197 cycrow 342
			wprintf(L"Error: unable to pack file: %s\n", filename.c_str() );
1 cycrow 343
		else
344
		{
185 cycrow 345
			if ( tofile.empty() )
1 cycrow 346
			{
197 cycrow 347
				if ( f.checkFileExt(L"bob") )
348
					f.changeFileExt(L"pbb");
349
				else if ( f.checkFileExt(L"bod") )
350
					f.changeFileExt(L"pbd");
1 cycrow 351
				else
197 cycrow 352
					f.changeFileExt(L"pck");
1 cycrow 353
			}
197 cycrow 354
			else if ( tofile.left(2) == L"*." )
185 cycrow 355
				f.changeFileExt(tofile.right(3));
1 cycrow 356
			else
185 cycrow 357
				f.setFilename(tofile);
1 cycrow 358
 
129 cycrow 359
			if ( !f.writeFilePointer() )
276 cycrow 360
				wprintf(L"Error: unable to write file: %s\n", tofile.c_str() );
1 cycrow 361
			else
320 cycrow 362
				wprintf(L"%s has been packed to %s\n", CFileIO(filename).filename().c_str(), f.filename().c_str());
1 cycrow 363
		}
364
	}
365
}
366
 
298 cycrow 367
void PrintSyntax(const Utils::WString &cmd)
1 cycrow 368
{
298 cycrow 369
	wprintf(L"Syntax: %s <flags> [arguments]\n", cmd.c_str() );
370
	wprintf(L"Flags:\n");
371
	wprintf(L"\t-l <filename> [filemask]\n");
372
	wprintf(L"\t\tLists the contents of a cat file, with optional search mask\n");
317 cycrow 373
	wprintf(L"\t-x [--preserve] <catfile::filemask> [dir]\n");
374
	wprintf(L"\t\textracts a file from a cat file to the optional directory\n\t--preverve: Preserves directory structure");
298 cycrow 375
	wprintf(L"\t-xa <catfile> [dir]\n");
376
	wprintf(L"\t\textracts all files to optional directory\n");
377
	wprintf(L"\t-a <filename> <catfile::tofile>\n");
378
	wprintf(L"\t\tAdds the file into a cat archive, saves it as <tofile>\n");
379
	wprintf(L"\t-r <catfile> <file>\n");
380
	wprintf(L"\t\tRemoves a file from an archive\n");
381
	wprintf(L"\t[--ext:EXTENSION] -u <filename> [tofile]\n");
382
	wprintf(L"\t\tUnpacks a file, ie from pck to xml/txt\n");
383
	wprintf(L"\t-p <filename> <tofile>\n");
384
	wprintf(L"\t\tPacks a file, ie from xml/txt to pck\n");
1 cycrow 385
}
386
 
387
int main ( int argc, char **argv )
388
{
316 cycrow 389
	printf ( "\nCATPCK Tool V1.22 17/05/2025 (SPK: %.2f) Created by Cycrow\n\n", GetLibraryVersion() );
1 cycrow 390
 
391
	// parse the cmd name
305 cycrow 392
	Utils::CommandLine cmd(argc, argv);
276 cycrow 393
 
1 cycrow 394
	if ( argc < 2 )
305 cycrow 395
		PrintSyntax(cmd.cmdName());
1 cycrow 396
	else
397
	{
305 cycrow 398
		Utils::WString command(cmd.arg(0).lower());
1 cycrow 399
 
305 cycrow 400
		auto& args = cmd.args();
276 cycrow 401
 
402
		if ( (command == L"-l") || (command == L"-list") )
403
		{
316 cycrow 404
			if (cmd.argCount() < 2)
305 cycrow 405
				wprintf(L"Syntax: %s -l <filename> [filemask]\n\tLists the contents of the cat file\n", cmd.cmdName().c_str());
1 cycrow 406
			else
316 cycrow 407
				ListFiles(cmd);
1 cycrow 408
		}
317 cycrow 409
		else if ((command == L"-x") || (command == L"-extract"))
1 cycrow 410
		{
317 cycrow 411
			if (cmd.argCount() < 2 )
412
				wprintf(L"Syntax: %s %s [--preserve] <catfile::filemask> [dir]\n\tExtracts a file from the cat archive\n", cmd.cmdName().c_str(), command.c_str() );
1 cycrow 413
			else
317 cycrow 414
				ExtractFile (cmd, L"", false);
1 cycrow 415
		}
276 cycrow 416
		else if ( (command == L"-xa") || (command == L"-extractall") )
1 cycrow 417
		{
317 cycrow 418
			if (cmd.argCount() < 2)
305 cycrow 419
				wprintf(L"Syntax: %s %s <catfile> [dir]\n\tExtracts all files from the cat archive\n", cmd.cmdName().c_str(), command.c_str() );
1 cycrow 420
			else
317 cycrow 421
				ExtractFile(cmd, L"::*", true);
1 cycrow 422
		}
276 cycrow 423
		else if ( (command == L"-a") || (command == L"-append") )
1 cycrow 424
		{
320 cycrow 425
			if (cmd.argCount() < 3 )
305 cycrow 426
				wprintf(L"Syntax: %s -a <filename> <catfile::tofile>\n\tAppends a file into the archive\n", cmd.cmdName().c_str() );
1 cycrow 427
			else
314 cycrow 428
				AppendFile(cmd);
1 cycrow 429
		}
276 cycrow 430
		else if ( (command == L"-r") || (command == L"-remove") )
1 cycrow 431
		{
320 cycrow 432
			if (cmd.argCount() < 3 )
305 cycrow 433
				wprintf(L"Syntax: %s -r <catfile> <filename>\n\tRemoves a file from the archive\n", cmd.cmdName().c_str() );
1 cycrow 434
			else
320 cycrow 435
				RemoveFile(cmd);
1 cycrow 436
		}
276 cycrow 437
		else if ( (command == L"-u") || (command == L"-unpack") )
1 cycrow 438
		{
320 cycrow 439
			if (cmd.argCount() < 2 )
305 cycrow 440
				wprintf(L"Syntax: %s [--ext:EXTENSION] -u <filename> [to]\n\tUnpacks a file", cmd.cmdName().c_str() );
1 cycrow 441
			else
320 cycrow 442
				UnpackFile(cmd);
1 cycrow 443
		}
276 cycrow 444
		else if ( (command == L"-p") || (command == L"-pack") )
1 cycrow 445
		{
320 cycrow 446
			if (cmd.argCount() < 2 )
305 cycrow 447
				wprintf(L"Syntax: %s -p <filename> [to]\n\tPacks a file to .pck", cmd.cmdName().c_str() );
1 cycrow 448
			else
320 cycrow 449
				PackFile(cmd);
1 cycrow 450
		}
451
		else
452
		{
276 cycrow 453
			wprintf(L"Invalaid flag: %s\n\n", command.c_str());
305 cycrow 454
			PrintSyntax(cmd.cmdName());
1 cycrow 455
		}
456
	}
457
 
458
#ifdef _DEBUG
459
	char pause;
460
	printf ( "\n\nPress a key to end\n" );
461
	scanf ( "%c", &pause );
462
#endif
463
 
464
	return 0;
465
}
466