Subversion Repositories spk

Rev

Rev 116 | Rev 121 | 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"
94 cycrow 6
#include "TextDB.h"
1 cycrow 7
 
79 cycrow 8
#include "Packages.h"
94 cycrow 9
#include "Utils/StringList.h"
101 cycrow 10
#include "CyString.h"
79 cycrow 11
 
112 cycrow 12
#define DELETELIST(list) if ( list ) { list->clear(); delete list; }
94 cycrow 13
 
35 cycrow 14
namespace SPK {
15
 
94 cycrow 16
CVirtualFileSystem::CVirtualFileSystem() : _lShields(NULL),
17
	_lLasers(NULL),
18
	_lMissiles(NULL),
19
	_lCockpits(NULL),
20
	_lComponentSections(NULL),
21
	_lDummySections(NULL),
101 cycrow 22
	_lBodiesSections(NULL),
23
	_lShips(NULL)
1 cycrow 24
{
25
	m_bLoaded = false;
35 cycrow 26
	m_pMap = new FileList;
27
	m_pModMap = new FileList;
28
 
29
	m_pTexts = new CTextDB();
30
	m_pModTexts = new CTextDB();
31
 
1 cycrow 32
	m_sAddon = "";
35 cycrow 33
 
34
	m_iLang = 0;
1 cycrow 35
}
36
 
37
CVirtualFileSystem::~CVirtualFileSystem(void)
38
{
101 cycrow 39
	_clear();
35 cycrow 40
 
41
	if ( m_pTexts )		delete m_pTexts;
42
	if ( m_pModTexts )  delete m_pModTexts;
101 cycrow 43
}
94 cycrow 44
 
101 cycrow 45
void CVirtualFileSystem::setAddon(const Utils::String &addon)
46
{ 
47
	m_sAddon = addon; 
1 cycrow 48
}
35 cycrow 49
void CVirtualFileSystem::setLanguage(int iLang)
1 cycrow 50
{
35 cycrow 51
	m_iLang = iLang;
52
}
53
 
94 cycrow 54
Utils::String CVirtualFileSystem::firstShield()
55
{
56
	if ( !_lShields || _lShields->empty() ) _updateShields();
112 cycrow 57
	return _returnText(_lShields->first());
94 cycrow 58
}
59
 
60
Utils::String CVirtualFileSystem::nextShield()
61
{
112 cycrow 62
	return _returnText(_lShields->next());
94 cycrow 63
}
64
 
65
Utils::String CVirtualFileSystem::firstComponentSection()
66
{
67
	if ( !_lComponentSections || _lComponentSections->empty() ) _updateComponentSections();
112 cycrow 68
	return _returnLine(_lComponentSections->first());
94 cycrow 69
}
70
 
71
Utils::String CVirtualFileSystem::nextComponentSection()
72
{
112 cycrow 73
	return _returnLine(_lComponentSections->next());
94 cycrow 74
}
75
 
76
Utils::String CVirtualFileSystem::firstDummySection()
77
{
78
	if ( !_lDummySections || _lDummySections->empty() ) _updateDummySections();
112 cycrow 79
	return _returnLine(_lDummySections->first());
94 cycrow 80
}
81
 
82
Utils::String CVirtualFileSystem::nextDummySection()
83
{
112 cycrow 84
	return _returnLine(_lDummySections->next());
94 cycrow 85
}
86
 
87
Utils::String CVirtualFileSystem::firstBodiesSection()
88
{
89
	if ( !_lBodiesSections || _lBodiesSections->empty() ) _updateBodiesSections();
112 cycrow 90
	return _returnLine(_lBodiesSections->first());
94 cycrow 91
}
92
 
93
Utils::String CVirtualFileSystem::nextBodiesSection()
94
{
112 cycrow 95
	return _returnLine(_lBodiesSections->next());
94 cycrow 96
}
97
 
98
std::pair<Utils::String, Utils::String> CVirtualFileSystem::nextLaser()
99
{
112 cycrow 100
	return _returnPair(_lLasers->next());
94 cycrow 101
}
102
 
103
std::pair<Utils::String, Utils::String>  CVirtualFileSystem::firstLaser()
104
{
105
	if ( !_lLasers || _lLasers->empty() ) _updateLasers();
112 cycrow 106
	return _returnPair(_lLasers->first());
94 cycrow 107
}
108
 
109
std::pair<Utils::String, Utils::String>  CVirtualFileSystem::firstMissile()
110
{
111
	if ( !_lMissiles || _lMissiles->empty() ) _updateMissiles();
112 cycrow 112
	return _returnPair(_lMissiles->first());
94 cycrow 113
}
114
 
115
std::pair<Utils::String, Utils::String> CVirtualFileSystem::nextMissile()
116
{
112 cycrow 117
	return _returnPair(_lMissiles->next());
94 cycrow 118
}
119
 
120
Utils::String CVirtualFileSystem::firstCockpit()
121
{
122
	if ( !_lCockpits || _lCockpits->empty() ) _updateCockpits();
112 cycrow 123
	return _returnID(_lCockpits->first());
94 cycrow 124
}
125
 
126
Utils::String CVirtualFileSystem::nextCockpit()
127
{
112 cycrow 128
	return _returnID(_lCockpits->next());
94 cycrow 129
}
130
 
131
std::pair<Utils::String, Utils::String> CVirtualFileSystem::_returnPair(Utils::SStringList *s)
132
{
133
	if ( s && !s->data.empty() && !s->data.left(8).Compare("ReadText") ) return std::make_pair<Utils::String, Utils::String>(s->str, s->data);
134
	if ( s ) return std::make_pair<Utils::String, Utils::String>(s->str, s->str.token(";", -2));
135
	return std::make_pair<Utils::String, Utils::String>(Utils::String::Null(), Utils::String::Null());
136
}
137
 
138
Utils::String CVirtualFileSystem::_returnText(Utils::SStringList *s)
139
{
140
	if ( s && !s->data.empty() && !s->data.left(8).Compare("ReadText") ) return s->data;
141
	if ( s ) return s->str.token(";", -2);
142
	return Utils::String::Null();
143
}
144
 
145
Utils::String CVirtualFileSystem::_returnID(Utils::SStringList *s)
146
{
147
	if ( s ) return s->str.token(";", -2);
148
	return Utils::String::Null();
149
}
150
 
151
Utils::String CVirtualFileSystem::_returnLine(Utils::SStringList *s)
152
{
153
	if ( s ) return s->str;
154
	return Utils::String::Null();
155
}
156
 
157
 
35 cycrow 158
bool CVirtualFileSystem::LoadFilesystem(const Utils::String &dir, int maxPatch)
159
{
160
	return this->LoadFilesystem(dir, "", maxPatch);
161
}
162
 
163
bool CVirtualFileSystem::LoadFilesystem(const Utils::String &dir, const Utils::String &mod, int maxPatch)
164
{
114 cycrow 165
	m_sDir = dir.findReplace("\\", "/");
1 cycrow 166
 
35 cycrow 167
	this->_clear();
1 cycrow 168
 
114 cycrow 169
	// if we are in the addon directory, then also read the previous directory
170
	Utils::String s = dir.token("/", -1);
171
	if ( !this->m_sAddon.empty() && dir.token("/", -1).Compare(this->m_sAddon) ) {
172
		//LoadFilesystem(dir.tokens("/", 1, -2), "", 0);
173
	}
174
 
1 cycrow 175
	int number = 1;
101 cycrow 176
	while ( CFileIO::Exists(dir + "/" + Utils::String::PadNumber(number, 2) + ".cat") )
1 cycrow 177
	{
58 cycrow 178
		if ( maxPatch && maxPatch < number ) break;
101 cycrow 179
		Utils::String file = dir + "/" + Utils::String::PadNumber(number, 2);
180
		if ( !CFileIO::Exists(file + ".dat") )
1 cycrow 181
			break;
182
 
183
		CCatFile cat;
184
		if ( cat.Open(file + ".cat", m_sAddon, CATREAD_JUSTCONTENTS, false) == CATERR_NONE )
185
		{
43 cycrow 186
			for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() ) {
101 cycrow 187
				this->_addModFile(CFileIO(c->Data()->sFile).fullFilename(), file + ".cat", m_pMap);
1 cycrow 188
				m_bLoaded = true;
189
			}
190
		}
191
		++number;
192
	}
193
 
43 cycrow 194
	// add all the files from the mod
35 cycrow 195
	if ( !mod.empty() )
196
		this->addMod(mod);
197
 
43 cycrow 198
	// now add all the extracted data
199
	this->_addDir(m_sDir, "");
200
 
1 cycrow 201
	return m_bLoaded;
202
}
203
 
43 cycrow 204
 
205
void CVirtualFileSystem::_addDir(const Utils::String &sStart, const Utils::String &sDir)
206
{
207
	CDirIO Dir(sStart + "/" + sDir);
208
	CyStringList *list = Dir.DirList();
209
	if ( list ) {
210
		for ( SStringList *strNode = list->Head(); strNode; strNode = strNode->next ) {
58 cycrow 211
			if ( CDirIO(Dir.Dir(strNode->str)).IsDir() ) _addDir(sStart, sDir + "/" + strNode->str.ToString());
119 cycrow 212
			else if ( CFileIO(strNode->str).isFileExtension("cat") ) continue;
213
			else if ( CFileIO(strNode->str).isFileExtension("dat") ) continue;
58 cycrow 214
			else this->_addFile(sDir + "/" + strNode->str.ToString(), Dir.File(strNode->str).ToString(), m_pMap);
43 cycrow 215
		}
216
	}
217
}
218
 
114 cycrow 219
 
220
void CVirtualFileSystem::_removeSameFile(const Utils::String &sFile, const Utils::String &sDest, const Utils::String &ext, FileList *pList)
221
{
222
	CFileIO F(sFile);
223
 
224
	Utils::String newFile = F.ChangeFileExtension(ext).ToString();
225
	newFile = newFile.lower();
226
 
227
	FileListItr itr = pList->find(newFile);
228
	if ( itr != pList->end() ) {
229
		if ( !CFileIO((*pList)[newFile]).dir().Compare(CFileIO(sDest).dir()) ) {
230
			pList->erase(newFile);
231
		}
232
	}
233
}
234
 
43 cycrow 235
void CVirtualFileSystem::_addFile(const Utils::String &sFile, const Utils::String &sDest, FileList *pList)
236
{
114 cycrow 237
	Utils::String file = sFile.asFilename().removeIf(0, '/');
238
 
239
	// check file extension, remove any patching files in previous files
240
	CFileIO F(file);
119 cycrow 241
	if ( F.isFileExtension("bob") || F.isFileExtension("pbd") || F.isFileExtension("bod") ) {
114 cycrow 242
		_removeSameFile(file, sDest, "pbb", pList);
243
		_removeSameFile(file, sDest, "bob", pList);
244
		_removeSameFile(file, sDest, "pbd", pList);
245
	}
119 cycrow 246
	else if ( F.isFileExtension("xml") || F.isFileExtension("txt") )
116 cycrow 247
		_removeSameFile(file, sDest, "pck", pList);
114 cycrow 248
 
249
	(*pList)[file.lower()] = sDest.findReplace("//", "/");
43 cycrow 250
}
251
void CVirtualFileSystem::_addModFile(const Utils::String &sFile, const Utils::String &sMod, FileList *pList)
252
{
253
	this->_addFile(sFile, sMod + "::" + sFile, pList);
254
}
255
 
35 cycrow 256
bool CVirtualFileSystem::loadMod(const Utils::String &mod)
1 cycrow 257
{
258
	bool loaded = false;
35 cycrow 259
 
260
	if ( !m_pModMap ) m_pModMap = new FileList;
261
 
1 cycrow 262
	CCatFile cat;
263
	if ( CCatFile::Opened(cat.Open(mod, m_sAddon, CATREAD_JUSTCONTENTS, false), false) )
264
	{
43 cycrow 265
		for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() ) {
102 cycrow 266
			this->_addModFile(CFileIO(c->Data()->sFile).fullFilename(), mod, m_pModMap);
35 cycrow 267
			loaded = true;
268
		}
269
	}
270
 
271
	return loaded;
272
}
273
 
274
bool CVirtualFileSystem::addMod(const Utils::String &mod)
275
{
276
	bool loaded = false;
277
 
278
	if ( !m_pMap ) m_pMap = new FileList;
279
 
280
	CCatFile cat;
281
	if ( CCatFile::Opened(cat.Open(mod, m_sAddon, CATREAD_JUSTCONTENTS, false), false) )
282
	{
283
		for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() )
284
		{
102 cycrow 285
			this->_addModFile(CFileIO(c->Data()->sFile).fullFilename(), mod, m_pMap);
1 cycrow 286
			loaded = true;
287
		}
288
	}
289
 
290
	return loaded;
291
}
292
 
35 cycrow 293
bool CVirtualFileSystem::textExists(int iLang, int iPage, int iID) const
1 cycrow 294
{
94 cycrow 295
	if ( iLang <= 0 ) iLang = m_iLang;
296
 
35 cycrow 297
	if ( m_pTexts ) {
298
		if ( m_pTexts->exists(iLang, iPage, iID) ) return m_pTexts->exists(iLang, iPage, iID);
299
	}
300
	if ( m_pModTexts ) {
301
		if ( m_pModTexts->exists(iLang, iPage, iID) ) return m_pModTexts->exists(iLang, iPage, iID);
302
	}
303
 
304
	return false;
1 cycrow 305
}
306
 
35 cycrow 307
Utils::String CVirtualFileSystem::findText(int iLang, int iPage, int iID) const
1 cycrow 308
{
94 cycrow 309
	if ( iLang <= 0 ) iLang = m_iLang;
310
 
35 cycrow 311
	if ( m_pTexts ) {
312
		if ( m_pTexts->exists(iLang, iPage, iID) ) return m_pTexts->get(iLang, iPage, iID);
313
	}
314
	if ( m_pModTexts ) {
315
		if ( m_pModTexts->exists(iLang, iPage, iID) ) return m_pModTexts->get(iLang, iPage, iID);
316
	}
1 cycrow 317
 
35 cycrow 318
	return Utils::String("ReadText") + (long)iPage + "-" + (long)iID;
319
}
43 cycrow 320
 
101 cycrow 321
const Utils::String &CVirtualFileSystem::directory() const
322
{
323
	return m_sDir;
324
}
325
 
43 cycrow 326
bool CVirtualFileSystem::isFileAvailable(const Utils::String &file)
35 cycrow 327
{
43 cycrow 328
	return !this->_findFile(file).empty();
329
}
330
 
101 cycrow 331
bool CVirtualFileSystem::isTextUpdated() const
332
{
333
	if ( m_pTexts ) return m_pTexts->anyTextLoaded();
334
	return false;
335
}
336
 
43 cycrow 337
Utils::String CVirtualFileSystem::_findFile(const Utils::String &file)
338
{
339
	Utils::String toFile = file.findReplace("\\", "/").lower();
340
 
35 cycrow 341
	if ( m_pModMap && !m_pModMap->empty() ) {
342
		if ( !m_sAddon.empty() ) {
102 cycrow 343
			FileListItr aitr = m_pModMap->find(CFileIO(m_sAddon + "/" + toFile).fullFilename().lower().c_str());
344
			if ( aitr == m_pModMap->end() ) aitr = m_pModMap->find(CFileIO(_convertExtension(m_sAddon + "/" + toFile)).fullFilename().lower().c_str());
35 cycrow 345
			if ( aitr != m_pModMap->end() ) return aitr->second;
346
		}
102 cycrow 347
		FileListItr itr = m_pModMap->find(CFileIO(toFile).fullFilename().lower().c_str());
348
		if ( itr == m_pModMap->end() ) itr = m_pModMap->find(CFileIO(_convertExtension(toFile)).fullFilename().lower().c_str());
35 cycrow 349
		if ( itr != m_pModMap->end() ) return itr->second;
350
	}
114 cycrow 351
 
352
	if ( m_pMap && !m_pMap->empty() ) {
35 cycrow 353
		if ( !m_sAddon.empty() ) {
58 cycrow 354
			FileListItr aitr = m_pMap->find(CFileIO(m_sAddon + "/" + toFile).fullFilename().lower().c_str());
355
			if ( aitr == m_pMap->end() ) aitr = m_pMap->find(CFileIO(_convertExtension(m_sAddon + "/" + file)).fullFilename().lower().c_str());
35 cycrow 356
			if ( aitr != m_pMap->end() ) return aitr->second;
357
		}
102 cycrow 358
		FileListItr itr = m_pMap->find(CFileIO(file).fullFilename().lower().c_str());
359
		if ( itr == m_pMap->end() ) itr = m_pMap->find(CFileIO(_convertExtension(file)).fullFilename().lower().c_str());
35 cycrow 360
		if ( itr != m_pMap->end() ) return itr->second;
361
	}
362
	return "";
363
}
364
 
58 cycrow 365
Utils::String CVirtualFileSystem::_extractFromCat(const Utils::String &sCat, const Utils::String &sFile, const Utils::String &sTo)
35 cycrow 366
{
58 cycrow 367
	CCatFile catFile;
368
	if ( catFile.Open(sCat, m_sAddon, CATREAD_CATDECRYPT, false) == CATERR_NONE )
369
	{
370
		// check for the file
371
		if ( catFile.ExtractFile(sFile, sTo) ) return sTo;
372
		if ( catFile.Error() == CATERR_INVALIDDEST || catFile.Error() == CATERR_CANTCREATEDIR ) {
373
			if ( catFile.ExtractFile(sFile) ) return sFile;
374
		}
375
	}
376
 
377
	return "";
378
}
379
 
380
Utils::String CVirtualFileSystem::_extract(const Utils::String &sFile, const Utils::String &sTo)
381
{
382
	// check if we need to unpack the file
383
	if ( CCatFile::CheckPackedExtension(sFile) ) {
384
		CFileIO File(sFile);
385
		C_File f(CyString(File.fullFilename()));
386
		if ( !f.readFromFile(File) ) return "";
387
		if ( !f.UnPCKFile() ) return "";
388
		if ( !f.WriteToFile(sTo) ) return "";
389
		return sTo;
390
	}
391
	return sFile;
392
}
393
 
394
Utils::String CVirtualFileSystem::ExtractGameFile(const Utils::String &file, const Utils::String &to)
395
{
43 cycrow 396
	Utils::String sIn = _findFile(file);
397
	Utils::String sFile = file;
398
 
58 cycrow 399
	if ( sIn.empty() ) return "";
35 cycrow 400
 
43 cycrow 401
	if ( sIn.isin("::") ) {
402
		sFile = sIn.token("::", 2);
403
		sIn = sIn.token("::", 1);
58 cycrow 404
		return _extractFromCat(sIn, sFile, to);
43 cycrow 405
	}
58 cycrow 406
 
407
	return _extract(sIn, to);
1 cycrow 408
}
409
 
35 cycrow 410
Utils::String CVirtualFileSystem::_convertExtension(const Utils::String &sFile)
411
{
412
	CFileIO File(sFile);
119 cycrow 413
	if ( File.isFileExtension("pck") ) {
35 cycrow 414
		return File.ChangeFileExtension("xml").ToString();
415
	}
119 cycrow 416
	else if ( File.isFileExtension("xml") ) {
35 cycrow 417
		return File.ChangeFileExtension("pck").ToString();
418
	}
119 cycrow 419
	else if ( File.isFileExtension("pbb") ) {
35 cycrow 420
		return File.ChangeFileExtension("bob").ToString();
421
	}
119 cycrow 422
	else if ( File.isFileExtension("bob") ) {
35 cycrow 423
		return File.ChangeFileExtension("pbb").ToString();
424
	}
119 cycrow 425
	else if ( File.isFileExtension("pbd") ) {
35 cycrow 426
		return File.ChangeFileExtension("bod").ToString();
427
	}
119 cycrow 428
	else if ( File.isFileExtension("bod") ) {
35 cycrow 429
		return File.ChangeFileExtension("pbd").ToString();
430
	}
431
 
432
	return sFile;
433
}
434
 
101 cycrow 435
Utils::CStringList *CVirtualFileSystem::getTShipsEntries()
35 cycrow 436
{
101 cycrow 437
	if ( !_lShips || _lShips->empty() ) _updateShips();
438
	return _lShips;
35 cycrow 439
}
440
 
441
C_File *CVirtualFileSystem::extractGameFileToPackage(CBaseFile *pPackage, const Utils::String &sFile, FileType iFileType)
442
{
443
	return this->extractGameFileToPackage(pPackage, sFile, iFileType, sFile);
444
}
445
 
446
C_File *CVirtualFileSystem::extractGameFileToPackage(CBaseFile *pPackage, const Utils::String &sFile, FileType iFileType, const Utils::String &sTo)
447
{
79 cycrow 448
	Utils::String to = this->ExtractGameFile(sFile, CPackages::tempDirectory() + "tmp.dat");
58 cycrow 449
	if ( !to.empty() ) {
450
		CFileIO File(to);
35 cycrow 451
 
102 cycrow 452
		C_File *f = pPackage->AddFile(CFileIO(sTo).filename(), CFileIO(sTo).dir(), iFileType);
35 cycrow 453
		if ( f ) {
79 cycrow 454
			if ( f->ReadFromFile(CPackages::GetTempDirectory() + "tmp.dat") ) {
52 cycrow 455
				File.remove();
35 cycrow 456
				return f;
457
			}
458
		}
459
 
52 cycrow 460
		File.remove();
35 cycrow 461
	}
462
 
463
	return NULL;
464
}
465
 
466
Utils::String CVirtualFileSystem::getTShipsEntry(const Utils::String &sId)
467
{
101 cycrow 468
	if ( !_lShips || _lShips->empty() ) _updateShips();
469
	return _lShips->findData(sId, true);
35 cycrow 470
}
471
 
472
void CVirtualFileSystem::extractTexts(CXspFile *pPackage, int textId)
473
{
474
	//TODO: check for better finding of files in map
475
	for ( FileListItr itr = m_pMap->begin(); itr != m_pMap->end(); ++itr ) {
476
		Utils::String str = itr->first;
477
 
478
		if ( !m_sAddon.empty() ) {
479
			if ( !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "/t/") && !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "\\t\\") )
480
				continue;
481
		}
482
		else {
483
			if ( !str.left(2).Compare("t\\") && !str.left(2).Compare("t/") && !str.left(8).Compare("addon\t\\") && !str.left(8).Compare("addon/t/") )
484
				continue;
485
		}
486
 
79 cycrow 487
		Utils::String sTo = this->ExtractGameFile(str, CPackages::tempDirectory() + "tmp.dat");
58 cycrow 488
		if ( !sTo.empty() ) {
489
			pPackage->AddTextFromFile(sTo, textId);
490
			CFileIO::Remove(sTo);
35 cycrow 491
		}
492
	}
493
}
494
 
495
void CVirtualFileSystem::updateTexts(int iFromPage, int iToPage)
496
{
497
	this->_updateTexts(iFromPage, iToPage, m_pMap, m_pTexts);
498
}
499
void CVirtualFileSystem::updateTexts(int iPage)
500
{
501
	this->_updateTexts(iPage, iPage, m_pMap, m_pTexts);
502
}
503
void CVirtualFileSystem::updateModTexts(int iFromPage, int iToPage)
504
{
505
	this->_updateTexts(iFromPage, iToPage, m_pModMap, m_pModTexts);
506
}
507
void CVirtualFileSystem::updateModTexts(int iPage)
508
{
509
	this->_updateTexts(iPage, iPage, m_pModMap, m_pModTexts);
510
}
511
 
512
void CVirtualFileSystem::_clear()
513
{
514
	m_bLoaded = false;
515
 
516
	if ( m_pMap ) delete m_pMap;
517
	m_pMap = new FileList;
518
	if ( m_pModMap ) delete m_pModMap;
519
	m_pModMap = new FileList;
101 cycrow 520
 
521
	DELETELIST(_lShields);
522
	DELETELIST(_lLasers);
523
	DELETELIST(_lMissiles);
524
	DELETELIST(_lCockpits);
525
	DELETELIST(_lComponentSections);
526
	DELETELIST(_lDummySections);
527
	DELETELIST(_lBodiesSections);
528
	DELETELIST(_lShips);
35 cycrow 529
}
530
 
531
void CVirtualFileSystem::_updateTexts(int iFromPage, int iToPage, FileList *pFileList, CTextDB *pTextList)
532
{
533
	// read all text files
534
	for ( FileListItr itr = pFileList->begin(); itr != pFileList->end(); ++itr ) {
535
		Utils::String str = itr->first;
536
 
537
		if ( !m_sAddon.empty() ) {
538
			if ( !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "/t/") && !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "\\t\\") )
539
				continue;
540
		}
541
		else {
542
			if ( !str.left(2).Compare("t\\") && !str.left(2).Compare("t/") )
543
				continue;
544
		}
545
 
79 cycrow 546
		Utils::String sTo = this->ExtractGameFile(str, CPackages::tempDirectory() + "tmp.dat");
58 cycrow 547
		if ( !sTo.empty() ) {
35 cycrow 548
			// add all texts into the map, id=(pageid, tid) data=text
57 cycrow 549
			Utils::String baseFile = CFileIO(str).baseName();
35 cycrow 550
			int lang = (baseFile.isin("-L")) ? baseFile.right(3) : baseFile.truncate(-4);
551
 
552
			if ( m_iLang && lang != m_iLang ) continue;
553
			// open the text file to add
58 cycrow 554
			pTextList->parseTextFile(iFromPage, iToPage, sTo, lang);
35 cycrow 555
		}
556
	}
557
}
558
 
78 cycrow 559
void CVirtualFileSystem::clearMods(bool bIncludeStandard)
35 cycrow 560
{
78 cycrow 561
	if ( bIncludeStandard ) {
562
		if ( m_pTexts ) delete m_pTexts;
563
		m_pTexts = new CTextDB();
564
	}
565
 
35 cycrow 566
	if ( m_pModTexts ) delete m_pModTexts;
567
	m_pModTexts = new CTextDB();
568
}
569
 
94 cycrow 570
Utils::CStringList *CVirtualFileSystem::_updateList(const Utils::String &typesFile, int iTextPos)
571
{
572
	Utils::CStringList *list = new Utils::CStringList();
35 cycrow 573
 
94 cycrow 574
	Utils::String file = this->ExtractGameFile("types\\" + typesFile + ".pck", CPackages::tempDirectory() + "/" + typesFile + ".txt");
575
 
576
	CFileIO f(file);
577
 
578
	if ( f.exists() && f.startRead() ) {
579
		int itemsLeft = -1;
580
 
581
		while(!f.atEnd()) {
582
			// read the next line
583
			Utils::String line = f.readEndOfLine();
584
 
585
			// remove the unneeded characters
586
			line.removeChar('\r');
587
			line.removeChar(9);
588
			line.removeFirstSpace();
589
 
590
			// blank line ? skip it
591
			if ( line.empty() ) continue;
592
			// skip anything starting with / as its a comment
593
			if ( line[0] == '/' ) continue;
594
 
595
			// first line is the count
596
			if ( itemsLeft == -1 )
597
				itemsLeft = line.token(";", 2);
598
			else {
599
				if ( iTextPos == 0 )
600
					list->pushBack(line, line);
601
				else if ( iTextPos == -1 ) 
602
					list->pushBack(line, line.token(";", -2));
603
				else
604
					list->pushBack(line, this->findText(m_iLang, TEXTPAGE_OBJECTS, line.token(";", iTextPos).toLong()));
605
				--itemsLeft;
606
			}
607
		}
608
 
609
		f.close();
610
		f.remove();
611
	}
612
 
613
	return list;
614
}
615
 
616
void CVirtualFileSystem::_updateShields()
617
{
618
	DELETELIST(_lShields);
619
	_lShields = _updateList("TShields", 7);
620
}
621
 
622
void CVirtualFileSystem::_updateLasers()
623
{
624
	DELETELIST(_lLasers);
625
	_lLasers = _updateList("TLaser", 7);
626
}
627
 
628
void CVirtualFileSystem::_updateMissiles()
629
{
630
	DELETELIST(_lMissiles);
631
	_lMissiles = _updateList("TMissiles", 7);
632
}
633
 
634
void CVirtualFileSystem::_updateCockpits()
635
{
636
	DELETELIST(_lCockpits);
637
	_lCockpits = _updateList("TCockpits", -1);
638
}
639
 
101 cycrow 640
void CVirtualFileSystem::_updateShips()
641
{
642
	DELETELIST(_lShips);
643
	_lShips = _updateList("TShips", -1);
644
}
645
 
94 cycrow 646
void CVirtualFileSystem::_updateComponentSections()
647
{
648
	DELETELIST(_lComponentSections);
649
 
650
	_lComponentSections = new Utils::CStringList();
651
 
652
	Utils::String file = this->ExtractGameFile("types\\Components.pck", CPackages::tempDirectory() + "/Components.txt");
653
 
654
	CFileIO f(file);
655
 
656
	if ( f.exists() && f.startRead() ) {
657
		int sectionRemaining = 0;
658
		int itemsRemaining = 0;
659
 
660
		while(!f.atEnd()) {
661
			// read the next line
662
			Utils::String line = f.readEndOfLine();
663
 
664
			// remove the unneeded characters
665
			line.removeChar('\r');
666
			line.removeChar(9);
667
			line.removeFirstSpace();
668
 
669
			// blank line ? skip it
670
			if ( line.empty() ) continue;
671
			// skip anything starting with / as its a comment
672
			if ( line[0] == '/' ) continue;
673
 
674
			if ( itemsRemaining )
675
				--itemsRemaining;
676
			else if ( !sectionRemaining )
677
			{
678
				sectionRemaining = line.token(";", 2).toLong();
679
				Utils::String section = line.token(";", 1);
680
				if ( !section.empty() )
681
					_lComponentSections->pushBack(section, section);
682
			}
683
			else if ( !itemsRemaining )
684
			{
685
				itemsRemaining = line.token(";", 2).toLong();
686
				--sectionRemaining;
687
			}
688
		}
689
 
690
		f.close();
691
		f.remove();
692
	}
693
 
694
	if ( _lComponentSections->empty() )
695
	{
696
		_lComponentSections->pushBack("SCTYPE_LASER", "SCTYPE_LASER");
697
		_lComponentSections->pushBack("SCTYPE_COCKPIT", "SCTYPE_COCKPIT");
698
		_lComponentSections->pushBack("SCTYPE_DOCKING", "SCTYPE_DOCKING");
699
	}
700
}
701
 
702
Utils::CStringList *CVirtualFileSystem::_updateSectionList(const Utils::String &sFile, bool singleEntry)
703
{
704
	Utils::CStringList *list = new Utils::CStringList();
705
 
706
	Utils::String file = this->ExtractGameFile("types\\" + sFile + ".pck", CPackages::tempDirectory() + "/" + sFile + ".txt");
707
 
708
	CFileIO f(file);
709
 
710
	if ( f.exists() && f.startRead() ) {
711
		int sectionRemaining = 0;
712
 
713
		while(!f.atEnd()) {
714
			// read the next line
715
			Utils::String line = f.readEndOfLine();
716
 
717
			// remove the unneeded characters
718
			line.removeChar('\r');
719
			line.removeChar(9);
720
			line.removeFirstSpace();
721
 
722
			// blank line ? skip it
723
			if ( line.empty() ) continue;
724
			// skip anything starting with / as its a comment
725
			if ( line[0] == '/' ) continue;
726
 
727
			if ( !sectionRemaining )
728
			{
729
				sectionRemaining = line.token(";", 2).toLong();
730
				Utils::String section = line.token(";", 1);
731
				if ( !section.empty() )
732
					list->pushBack(section, section);
733
			}
734
			else {
735
				if ( singleEntry )
736
					sectionRemaining -= (line.countToken(";") - 1);
737
				else 
738
					--sectionRemaining;
739
			}
740
		}
741
 
742
		f.close();
743
		f.remove();
744
	}
745
 
746
	return list;
747
}
748
 
749
void CVirtualFileSystem::_updateDummySections()
750
{
751
	DELETELIST(_lDummySections);
752
 
753
	_lDummySections = _updateSectionList("Dummies", false);
754
 
755
	if ( _lDummySections->empty() )
756
	{
757
		_lDummySections->pushBack("SDTYPE_ANIMATED", "SDTYPE_ANIMATED");
758
		_lDummySections->pushBack("SDTYPE_DOCK", "SDTYPE_DOCK");
759
		_lDummySections->pushBack("SDTYPE_DOORWAY", "SDTYPE_DOORWAY");
760
		_lDummySections->pushBack("SDTYPE_GUN", "SDTYPE_GUN");
761
		_lDummySections->pushBack("SDTYPE_CONNECTION", "SDTYPE_CONNECTION");
762
	}
763
}
764
 
765
void CVirtualFileSystem::_updateBodiesSections()
766
{
767
	DELETELIST(_lBodiesSections);
768
 
769
	_lBodiesSections = _updateSectionList("Bodies", true);
770
 
771
	if ( _lBodiesSections->empty() )
772
	{
773
		_lBodiesSections->pushBack("SBTYPE_2D", "SBTYPE_2D");
774
		_lBodiesSections->pushBack("SBTYPE_FACECAMERA", "SBTYPE_FACECAMERA");
775
		_lBodiesSections->pushBack("SBTYPE_2D2", "SBTYPE_2D2");
776
		_lBodiesSections->pushBack("SBTYPE_2DY", "SBTYPE_2DY");
777
		_lBodiesSections->pushBack("SBTYPE_LOGO", "SBTYPE_LOGO");
778
		_lBodiesSections->pushBack("SBTYPE_FC", "SBTYPE_FC");
779
		_lBodiesSections->pushBack("SBTYPE_TURRET", "SBTYPE_TURRET");
780
		_lBodiesSections->pushBack("SBTYPE_JET", "SBTYPE_JET");
781
		_lBodiesSections->pushBack("SBTYPE_RADAR", "SBTYPE_RADAR");
782
		_lBodiesSections->pushBack("SBTYPE_CONTAINER", "SBTYPE_CONTAINER");
783
		_lBodiesSections->pushBack("SBTYPE_DISPLAY", "SBTYPE_DISPLAY");
784
		_lBodiesSections->pushBack("SBTYPE_DOCKPOINT", "SBTYPE_DOCKPOINT");
785
		_lBodiesSections->pushBack("SBTYPE_SMALLJET", "SBTYPE_SMALLJET");
786
	}
787
}
788
 
114 cycrow 789
 
790
/////////////////////////////////////////////////////////////////////////////////////////
791
//	DEBUG Functions
792
 
793
 
794
void CVirtualFileSystem::DEBUG_LogContents(const Utils::String &sFile)
795
{
796
	CFileIO File(sFile);
797
 
798
	if ( File.startWrite() ) {
799
		if ( m_pMap ) {
800
			for(FileListItr itr = m_pMap->begin(); itr != m_pMap->end(); itr++) {
801
				Utils::String line = itr->first + " => " + itr->second + "\r\n";
802
				File.write(line.c_str(), line.length());
803
			}
804
		}
805
 
806
		File.close();
807
	}
808
}
809
 
35 cycrow 810
} //NAMESPACE