Subversion Repositories spk

Rev

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

Rev Author Line No. Line
1 cycrow 1
 
2
#include "VirtualFileSystem.h"
3
#include "File_IO.h"
4
#include "CatFile.h"
35 cycrow 5
#include "XspFile.h"
1 cycrow 6
 
79 cycrow 7
#include "Packages.h"
8
 
35 cycrow 9
namespace SPK {
10
 
1 cycrow 11
CVirtualFileSystem::CVirtualFileSystem()
12
{
13
	m_bLoaded = false;
35 cycrow 14
	m_pMap = new FileList;
15
	m_pModMap = new FileList;
16
 
17
	m_pTexts = new CTextDB();
18
	m_pModTexts = new CTextDB();
19
 
1 cycrow 20
	m_sAddon = "";
35 cycrow 21
 
22
	m_iLang = 0;
1 cycrow 23
}
24
 
25
CVirtualFileSystem::~CVirtualFileSystem(void)
26
{
35 cycrow 27
	if ( m_pMap )		delete m_pMap;
28
	if ( m_pModMap )	delete m_pModMap;
29
 
30
	if ( m_pTexts )		delete m_pTexts;
31
	if ( m_pModTexts )  delete m_pModTexts;
1 cycrow 32
}
33
 
35 cycrow 34
void CVirtualFileSystem::setLanguage(int iLang)
1 cycrow 35
{
35 cycrow 36
	m_iLang = iLang;
37
}
38
 
39
bool CVirtualFileSystem::LoadFilesystem(const Utils::String &dir, int maxPatch)
40
{
41
	return this->LoadFilesystem(dir, "", maxPatch);
42
}
43
 
44
bool CVirtualFileSystem::LoadFilesystem(const Utils::String &dir, const Utils::String &mod, int maxPatch)
45
{
1 cycrow 46
	m_sDir = dir;
47
 
35 cycrow 48
	this->_clear();
1 cycrow 49
 
50
	int number = 1;
52 cycrow 51
	while ( CFileIO(CyString(dir) + "/" + CyString::Number(number).PadNumber(2) + ".cat").ExistsOld() )
1 cycrow 52
	{
58 cycrow 53
		if ( maxPatch && maxPatch < number ) break;
35 cycrow 54
		CyString file = CyString(dir) + "/" + CyString::Number(number).PadNumber(2);
52 cycrow 55
		if ( !CFileIO(file + ".dat").ExistsOld() )
1 cycrow 56
			break;
57
 
58
		CCatFile cat;
59
		if ( cat.Open(file + ".cat", m_sAddon, CATREAD_JUSTCONTENTS, false) == CATERR_NONE )
60
		{
43 cycrow 61
			for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() ) {
62
				this->_addModFile(CFileIO(c->Data()->sFile).GetFullFilename().ToString(), CyString(file + ".cat").ToString(), m_pMap);
1 cycrow 63
				m_bLoaded = true;
64
			}
65
		}
66
		++number;
67
	}
68
 
43 cycrow 69
	// add all the files from the mod
35 cycrow 70
	if ( !mod.empty() )
71
		this->addMod(mod);
72
 
43 cycrow 73
	// now add all the extracted data
74
	this->_addDir(m_sDir, "");
75
 
1 cycrow 76
	return m_bLoaded;
77
}
78
 
43 cycrow 79
 
80
void CVirtualFileSystem::_addDir(const Utils::String &sStart, const Utils::String &sDir)
81
{
82
	CDirIO Dir(sStart + "/" + sDir);
83
	CyStringList *list = Dir.DirList();
84
	if ( list ) {
85
		for ( SStringList *strNode = list->Head(); strNode; strNode = strNode->next ) {
58 cycrow 86
			if ( CDirIO(Dir.Dir(strNode->str)).IsDir() ) _addDir(sStart, sDir + "/" + strNode->str.ToString());
43 cycrow 87
			else if ( CFileIO(strNode->str).CheckFileExtension("cat") ) continue;
88
			else if ( CFileIO(strNode->str).CheckFileExtension("dat") ) continue;
58 cycrow 89
			else this->_addFile(sDir + "/" + strNode->str.ToString(), Dir.File(strNode->str).ToString(), m_pMap);
43 cycrow 90
		}
91
	}
92
}
93
 
94
void CVirtualFileSystem::_addFile(const Utils::String &sFile, const Utils::String &sDest, FileList *pList)
95
{
85 cycrow 96
	(*pList)[sFile.asFilename().removeIf(0, '/').lower()] = sDest.findReplace("//", "/");
43 cycrow 97
}
98
void CVirtualFileSystem::_addModFile(const Utils::String &sFile, const Utils::String &sMod, FileList *pList)
99
{
100
	this->_addFile(sFile, sMod + "::" + sFile, pList);
101
}
102
 
35 cycrow 103
bool CVirtualFileSystem::loadMod(const Utils::String &mod)
1 cycrow 104
{
105
	bool loaded = false;
35 cycrow 106
 
107
	if ( !m_pModMap ) m_pModMap = new FileList;
108
 
1 cycrow 109
	CCatFile cat;
110
	if ( CCatFile::Opened(cat.Open(mod, m_sAddon, CATREAD_JUSTCONTENTS, false), false) )
111
	{
43 cycrow 112
		for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() ) {
113
			this->_addModFile(CFileIO(c->Data()->sFile).GetFullFilename().ToString(), mod, m_pModMap);
35 cycrow 114
			loaded = true;
115
		}
116
	}
117
 
118
	return loaded;
119
}
120
 
121
bool CVirtualFileSystem::addMod(const Utils::String &mod)
122
{
123
	bool loaded = false;
124
 
125
	if ( !m_pMap ) m_pMap = new FileList;
126
 
127
	CCatFile cat;
128
	if ( CCatFile::Opened(cat.Open(mod, m_sAddon, CATREAD_JUSTCONTENTS, false), false) )
129
	{
130
		for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() )
131
		{
43 cycrow 132
			this->_addModFile(CFileIO(c->Data()->sFile).GetFullFilename().ToString(), mod, m_pMap);
1 cycrow 133
			loaded = true;
134
		}
135
	}
136
 
137
	return loaded;
138
}
139
 
35 cycrow 140
bool CVirtualFileSystem::textExists(int iLang, int iPage, int iID) const
1 cycrow 141
{
35 cycrow 142
	if ( m_pTexts ) {
143
		if ( m_pTexts->exists(iLang, iPage, iID) ) return m_pTexts->exists(iLang, iPage, iID);
144
	}
145
	if ( m_pModTexts ) {
146
		if ( m_pModTexts->exists(iLang, iPage, iID) ) return m_pModTexts->exists(iLang, iPage, iID);
147
	}
148
 
149
	return false;
1 cycrow 150
}
151
 
35 cycrow 152
Utils::String CVirtualFileSystem::findText(int iLang, int iPage, int iID) const
1 cycrow 153
{
35 cycrow 154
	if ( m_pTexts ) {
155
		if ( m_pTexts->exists(iLang, iPage, iID) ) return m_pTexts->get(iLang, iPage, iID);
156
	}
157
	if ( m_pModTexts ) {
158
		if ( m_pModTexts->exists(iLang, iPage, iID) ) return m_pModTexts->get(iLang, iPage, iID);
159
	}
1 cycrow 160
 
35 cycrow 161
	return Utils::String("ReadText") + (long)iPage + "-" + (long)iID;
162
}
43 cycrow 163
 
164
bool CVirtualFileSystem::isFileAvailable(const Utils::String &file)
35 cycrow 165
{
43 cycrow 166
	return !this->_findFile(file).empty();
167
}
168
 
169
Utils::String CVirtualFileSystem::_findFile(const Utils::String &file)
170
{
171
	Utils::String toFile = file.findReplace("\\", "/").lower();
172
 
35 cycrow 173
	if ( m_pModMap && !m_pModMap->empty() ) {
174
		if ( !m_sAddon.empty() ) {
43 cycrow 175
			FileListItr aitr = m_pModMap->find(CFileIO(m_sAddon + "/" + toFile).GetFullFilename().ToLower().c_str());
176
			if ( aitr == m_pModMap->end() ) aitr = m_pModMap->find(CFileIO(_convertExtension(m_sAddon + "/" + toFile)).GetFullFilename().ToLower().c_str());
35 cycrow 177
			if ( aitr != m_pModMap->end() ) return aitr->second;
178
		}
43 cycrow 179
		FileListItr itr = m_pModMap->find(CFileIO(toFile).GetFullFilename().ToLower().c_str());
180
		if ( itr == m_pModMap->end() ) itr = m_pModMap->find(CFileIO(_convertExtension(toFile)).GetFullFilename().ToLower().c_str());
35 cycrow 181
		if ( itr != m_pModMap->end() ) return itr->second;
182
	}
183
	else if ( m_pMap && !m_pMap->empty() ) {
184
		if ( !m_sAddon.empty() ) {
58 cycrow 185
			FileListItr aitr = m_pMap->find(CFileIO(m_sAddon + "/" + toFile).fullFilename().lower().c_str());
186
			if ( aitr == m_pMap->end() ) aitr = m_pMap->find(CFileIO(_convertExtension(m_sAddon + "/" + file)).fullFilename().lower().c_str());
35 cycrow 187
			if ( aitr != m_pMap->end() ) return aitr->second;
188
		}
189
		FileListItr itr = m_pMap->find(CFileIO(file).GetFullFilename().ToLower().c_str());
190
		if ( itr == m_pMap->end() ) itr = m_pMap->find(CFileIO(_convertExtension(file)).GetFullFilename().ToLower().c_str());
191
		if ( itr != m_pMap->end() ) return itr->second;
192
	}
193
	return "";
194
}
195
 
58 cycrow 196
Utils::String CVirtualFileSystem::_extractFromCat(const Utils::String &sCat, const Utils::String &sFile, const Utils::String &sTo)
35 cycrow 197
{
58 cycrow 198
	CCatFile catFile;
199
	if ( catFile.Open(sCat, m_sAddon, CATREAD_CATDECRYPT, false) == CATERR_NONE )
200
	{
201
		// check for the file
202
		if ( catFile.ExtractFile(sFile, sTo) ) return sTo;
203
		if ( catFile.Error() == CATERR_INVALIDDEST || catFile.Error() == CATERR_CANTCREATEDIR ) {
204
			if ( catFile.ExtractFile(sFile) ) return sFile;
205
		}
206
	}
207
 
208
	return "";
209
}
210
 
211
Utils::String CVirtualFileSystem::_extract(const Utils::String &sFile, const Utils::String &sTo)
212
{
213
	// check if we need to unpack the file
214
	if ( CCatFile::CheckPackedExtension(sFile) ) {
215
		CFileIO File(sFile);
216
		C_File f(CyString(File.fullFilename()));
217
		if ( !f.readFromFile(File) ) return "";
218
		if ( !f.UnPCKFile() ) return "";
219
		if ( !f.WriteToFile(sTo) ) return "";
220
		return sTo;
221
	}
222
	return sFile;
223
}
224
 
225
Utils::String CVirtualFileSystem::ExtractGameFile(const Utils::String &file, const Utils::String &to)
226
{
43 cycrow 227
	Utils::String sIn = _findFile(file);
228
	Utils::String sFile = file;
229
 
58 cycrow 230
	if ( sIn.empty() ) return "";
35 cycrow 231
 
43 cycrow 232
	if ( sIn.isin("::") ) {
233
		sFile = sIn.token("::", 2);
234
		sIn = sIn.token("::", 1);
58 cycrow 235
		return _extractFromCat(sIn, sFile, to);
43 cycrow 236
	}
58 cycrow 237
 
238
	return _extract(sIn, to);
1 cycrow 239
}
240
 
35 cycrow 241
Utils::String CVirtualFileSystem::_convertExtension(const Utils::String &sFile)
242
{
243
	CFileIO File(sFile);
244
	if ( File.CheckFileExtension("pck") ) {
245
		return File.ChangeFileExtension("xml").ToString();
246
	}
247
	else if ( File.CheckFileExtension("xml") ) {
248
		return File.ChangeFileExtension("pck").ToString();
249
	}
250
	else if ( File.CheckFileExtension("pbb") ) {
251
		return File.ChangeFileExtension("bob").ToString();
252
	}
253
	else if ( File.CheckFileExtension("bob") ) {
254
		return File.ChangeFileExtension("pbb").ToString();
255
	}
256
	else if ( File.CheckFileExtension("pbd") ) {
257
		return File.ChangeFileExtension("bod").ToString();
258
	}
259
	else if ( File.CheckFileExtension("bod") ) {
260
		return File.ChangeFileExtension("pbd").ToString();
261
	}
262
 
263
	return sFile;
264
}
265
 
266
CyStringList *CVirtualFileSystem::GetTShipsEntries()
267
{
82 cycrow 268
	Utils::String sTo = this->ExtractGameFile("types/tships.pck", CPackages::tempDirectory() + "/tships.txt");
58 cycrow 269
	if ( !sTo.empty() ) {
270
		CFileIO TShips(sTo);
52 cycrow 271
		if ( TShips.exists() )
35 cycrow 272
		{
273
			CyStringList *lines = TShips.ReadLinesStr();
274
			if ( lines )
275
			{
276
				// remove any commands, blank lines, and start
277
				// and put the id as the data
278
				int entries = -1;
279
				for ( SStringList *str = lines->Head(); str; str = str->next )
280
				{
281
					str->str.RemoveFirstSpace();
282
					str->str.RemoveChar(9);
283
					str->str.RemoveChar('\r');
284
					str->str.RemoveChar('\n');
285
					if ( str->str.Empty() )
286
					{
287
						str->remove = true;
288
						continue;
289
					}
290
 
291
					if ( str->str[0] == '/' )
292
					{
293
						str->remove = true;
294
						continue;
295
					}
296
 
297
					if ( entries == -1 )
298
					{
299
						entries = str->str.GetToken(";", 2, 2).ToInt();
300
						str->remove = true;
301
						continue;
302
					}
303
 
304
					// hopefully we now have a valid tships line
305
					int num = -1;
306
					while ( str->data.Empty() )
307
						str->data = str->str.GetToken(";", num--, (num + 2));
308
				}
309
			}
310
			// remove the tmp file
52 cycrow 311
			TShips.remove();
35 cycrow 312
 
313
			if ( lines )
314
				lines->RemoveMarked();
315
			return lines;
316
		}
317
	}
318
 
319
	return NULL;
320
}
321
 
322
C_File *CVirtualFileSystem::extractGameFileToPackage(CBaseFile *pPackage, const Utils::String &sFile, FileType iFileType)
323
{
324
	return this->extractGameFileToPackage(pPackage, sFile, iFileType, sFile);
325
}
326
 
327
C_File *CVirtualFileSystem::extractGameFileToPackage(CBaseFile *pPackage, const Utils::String &sFile, FileType iFileType, const Utils::String &sTo)
328
{
79 cycrow 329
	Utils::String to = this->ExtractGameFile(sFile, CPackages::tempDirectory() + "tmp.dat");
58 cycrow 330
	if ( !to.empty() ) {
331
		CFileIO File(to);
35 cycrow 332
 
333
		C_File *f = pPackage->AddFile(CFileIO(sTo).GetFilename(), CFileIO(sTo).GetDir(), iFileType);
334
		if ( f ) {
79 cycrow 335
			if ( f->ReadFromFile(CPackages::GetTempDirectory() + "tmp.dat") ) {
52 cycrow 336
				File.remove();
35 cycrow 337
				return f;
338
			}
339
		}
340
 
52 cycrow 341
		File.remove();
35 cycrow 342
	}
343
 
344
	return NULL;
345
}
346
 
347
Utils::String CVirtualFileSystem::getTShipsEntry(const Utils::String &sId)
348
{
349
	CyStringList *ships = this->GetTShipsEntries();
350
	if ( ships )
351
	{
352
		SStringList *node = ships->FindData(CyString(sId).upper());
353
		if ( node )
354
			return node->str.ToString();
355
	}
356
	return "";
357
}
358
 
359
void CVirtualFileSystem::extractTexts(CXspFile *pPackage, int textId)
360
{
361
	//TODO: check for better finding of files in map
362
	for ( FileListItr itr = m_pMap->begin(); itr != m_pMap->end(); ++itr ) {
363
		Utils::String str = itr->first;
364
 
365
		if ( !m_sAddon.empty() ) {
366
			if ( !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "/t/") && !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "\\t\\") )
367
				continue;
368
		}
369
		else {
370
			if ( !str.left(2).Compare("t\\") && !str.left(2).Compare("t/") && !str.left(8).Compare("addon\t\\") && !str.left(8).Compare("addon/t/") )
371
				continue;
372
		}
373
 
79 cycrow 374
		Utils::String sTo = this->ExtractGameFile(str, CPackages::tempDirectory() + "tmp.dat");
58 cycrow 375
		if ( !sTo.empty() ) {
376
			pPackage->AddTextFromFile(sTo, textId);
377
			CFileIO::Remove(sTo);
35 cycrow 378
		}
379
	}
380
}
381
 
382
void CVirtualFileSystem::updateTexts(int iFromPage, int iToPage)
383
{
384
	this->_updateTexts(iFromPage, iToPage, m_pMap, m_pTexts);
385
}
386
void CVirtualFileSystem::updateTexts(int iPage)
387
{
388
	this->_updateTexts(iPage, iPage, m_pMap, m_pTexts);
389
}
390
void CVirtualFileSystem::updateModTexts(int iFromPage, int iToPage)
391
{
392
	this->_updateTexts(iFromPage, iToPage, m_pModMap, m_pModTexts);
393
}
394
void CVirtualFileSystem::updateModTexts(int iPage)
395
{
396
	this->_updateTexts(iPage, iPage, m_pModMap, m_pModTexts);
397
}
398
 
399
void CVirtualFileSystem::_clear()
400
{
401
	m_bLoaded = false;
402
 
403
	if ( m_pMap ) delete m_pMap;
404
	m_pMap = new FileList;
405
	if ( m_pModMap ) delete m_pModMap;
406
	m_pModMap = new FileList;
407
}
408
 
409
void CVirtualFileSystem::_updateTexts(int iFromPage, int iToPage, FileList *pFileList, CTextDB *pTextList)
410
{
411
	// read all text files
412
	for ( FileListItr itr = pFileList->begin(); itr != pFileList->end(); ++itr ) {
413
		Utils::String str = itr->first;
414
 
415
		if ( !m_sAddon.empty() ) {
416
			if ( !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "/t/") && !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "\\t\\") )
417
				continue;
418
		}
419
		else {
420
			if ( !str.left(2).Compare("t\\") && !str.left(2).Compare("t/") )
421
				continue;
422
		}
423
 
79 cycrow 424
		Utils::String sTo = this->ExtractGameFile(str, CPackages::tempDirectory() + "tmp.dat");
58 cycrow 425
		if ( !sTo.empty() ) {
35 cycrow 426
			// add all texts into the map, id=(pageid, tid) data=text
57 cycrow 427
			Utils::String baseFile = CFileIO(str).baseName();
35 cycrow 428
			int lang = (baseFile.isin("-L")) ? baseFile.right(3) : baseFile.truncate(-4);
429
 
430
			if ( m_iLang && lang != m_iLang ) continue;
431
			// open the text file to add
58 cycrow 432
			pTextList->parseTextFile(iFromPage, iToPage, sTo, lang);
35 cycrow 433
		}
434
	}
435
}
436
 
78 cycrow 437
void CVirtualFileSystem::clearMods(bool bIncludeStandard)
35 cycrow 438
{
78 cycrow 439
	if ( bIncludeStandard ) {
440
		if ( m_pTexts ) delete m_pTexts;
441
		m_pTexts = new CTextDB();
442
	}
443
 
35 cycrow 444
	if ( m_pModTexts ) delete m_pModTexts;
445
	m_pModTexts = new CTextDB();
446
}
447
 
448
bool CVirtualFileSystem::loadShipData(CyStringList *list)
449
{
450
	bool ret = false;
451
 
79 cycrow 452
	Utils::String sTo = this->ExtractGameFile("types/TShips.pck", CPackages::tempDirectory() + "tmp.dat");
58 cycrow 453
	if ( !sTo.empty() ) {
454
		CFileIO File(sTo);
35 cycrow 455
		CyStringList *lines = File.ReadLinesStr();
456
		if ( lines )
457
		{
458
			bool readFirst = false;
459
			for ( SStringList *str = lines->Head(); str; str = str->next )
460
			{
461
				if ( str->str.Empty() )
462
					continue;
463
				str->str.RemoveChar('\r');
464
				str->str.RemoveChar(9);
465
				str->str.RemoveFirstSpace();
466
				if ( str->str.Empty() )
467
					continue;
468
				if ( str->str[0] == '/' || str->str[0] == '#' )
469
					continue;
470
 
471
				if ( !readFirst )
472
					readFirst = true;
473
				else
474
				{
475
					CyString t = str->str.GetToken(";", -2);
476
					while ( t.Right(1) == ";" )
477
						t.Truncate((int)t.Length() - 1);
478
					list->PushBack(t, str->str);
479
				}
480
			}
481
 
482
			delete lines;
483
			ret = true;
484
		}
485
 
52 cycrow 486
		File.remove();
35 cycrow 487
	}
488
 
489
	return ret;
490
}
491
 
492
 
493
} //NAMESPACE