Subversion Repositories spk

Rev

Rev 112 | Rev 116 | 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());
43 cycrow 212
			else if ( CFileIO(strNode->str).CheckFileExtension("cat") ) continue;
213
			else if ( CFileIO(strNode->str).CheckFileExtension("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);
241
	if ( F.CheckFileExtension("bob") || F.CheckFileExtension("pbd") || F.CheckFileExtension("bod") ) {
242
		_removeSameFile(file, sDest, "pbb", pList);
243
		_removeSameFile(file, sDest, "bob", pList);
244
		_removeSameFile(file, sDest, "pbd", pList);
245
	}
246
 
247
	(*pList)[file.lower()] = sDest.findReplace("//", "/");
43 cycrow 248
}
249
void CVirtualFileSystem::_addModFile(const Utils::String &sFile, const Utils::String &sMod, FileList *pList)
250
{
251
	this->_addFile(sFile, sMod + "::" + sFile, pList);
252
}
253
 
35 cycrow 254
bool CVirtualFileSystem::loadMod(const Utils::String &mod)
1 cycrow 255
{
256
	bool loaded = false;
35 cycrow 257
 
258
	if ( !m_pModMap ) m_pModMap = new FileList;
259
 
1 cycrow 260
	CCatFile cat;
261
	if ( CCatFile::Opened(cat.Open(mod, m_sAddon, CATREAD_JUSTCONTENTS, false), false) )
262
	{
43 cycrow 263
		for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() ) {
102 cycrow 264
			this->_addModFile(CFileIO(c->Data()->sFile).fullFilename(), mod, m_pModMap);
35 cycrow 265
			loaded = true;
266
		}
267
	}
268
 
269
	return loaded;
270
}
271
 
272
bool CVirtualFileSystem::addMod(const Utils::String &mod)
273
{
274
	bool loaded = false;
275
 
276
	if ( !m_pMap ) m_pMap = new FileList;
277
 
278
	CCatFile cat;
279
	if ( CCatFile::Opened(cat.Open(mod, m_sAddon, CATREAD_JUSTCONTENTS, false), false) )
280
	{
281
		for ( CListNode<SInCatFile> *c = cat.GetFiles()->Front(); c; c = c->next() )
282
		{
102 cycrow 283
			this->_addModFile(CFileIO(c->Data()->sFile).fullFilename(), mod, m_pMap);
1 cycrow 284
			loaded = true;
285
		}
286
	}
287
 
288
	return loaded;
289
}
290
 
35 cycrow 291
bool CVirtualFileSystem::textExists(int iLang, int iPage, int iID) const
1 cycrow 292
{
94 cycrow 293
	if ( iLang <= 0 ) iLang = m_iLang;
294
 
35 cycrow 295
	if ( m_pTexts ) {
296
		if ( m_pTexts->exists(iLang, iPage, iID) ) return m_pTexts->exists(iLang, iPage, iID);
297
	}
298
	if ( m_pModTexts ) {
299
		if ( m_pModTexts->exists(iLang, iPage, iID) ) return m_pModTexts->exists(iLang, iPage, iID);
300
	}
301
 
302
	return false;
1 cycrow 303
}
304
 
35 cycrow 305
Utils::String CVirtualFileSystem::findText(int iLang, int iPage, int iID) const
1 cycrow 306
{
94 cycrow 307
	if ( iLang <= 0 ) iLang = m_iLang;
308
 
35 cycrow 309
	if ( m_pTexts ) {
310
		if ( m_pTexts->exists(iLang, iPage, iID) ) return m_pTexts->get(iLang, iPage, iID);
311
	}
312
	if ( m_pModTexts ) {
313
		if ( m_pModTexts->exists(iLang, iPage, iID) ) return m_pModTexts->get(iLang, iPage, iID);
314
	}
1 cycrow 315
 
35 cycrow 316
	return Utils::String("ReadText") + (long)iPage + "-" + (long)iID;
317
}
43 cycrow 318
 
101 cycrow 319
const Utils::String &CVirtualFileSystem::directory() const
320
{
321
	return m_sDir;
322
}
323
 
43 cycrow 324
bool CVirtualFileSystem::isFileAvailable(const Utils::String &file)
35 cycrow 325
{
43 cycrow 326
	return !this->_findFile(file).empty();
327
}
328
 
101 cycrow 329
bool CVirtualFileSystem::isTextUpdated() const
330
{
331
	if ( m_pTexts ) return m_pTexts->anyTextLoaded();
332
	return false;
333
}
334
 
43 cycrow 335
Utils::String CVirtualFileSystem::_findFile(const Utils::String &file)
336
{
337
	Utils::String toFile = file.findReplace("\\", "/").lower();
338
 
35 cycrow 339
	if ( m_pModMap && !m_pModMap->empty() ) {
340
		if ( !m_sAddon.empty() ) {
102 cycrow 341
			FileListItr aitr = m_pModMap->find(CFileIO(m_sAddon + "/" + toFile).fullFilename().lower().c_str());
342
			if ( aitr == m_pModMap->end() ) aitr = m_pModMap->find(CFileIO(_convertExtension(m_sAddon + "/" + toFile)).fullFilename().lower().c_str());
35 cycrow 343
			if ( aitr != m_pModMap->end() ) return aitr->second;
344
		}
102 cycrow 345
		FileListItr itr = m_pModMap->find(CFileIO(toFile).fullFilename().lower().c_str());
346
		if ( itr == m_pModMap->end() ) itr = m_pModMap->find(CFileIO(_convertExtension(toFile)).fullFilename().lower().c_str());
35 cycrow 347
		if ( itr != m_pModMap->end() ) return itr->second;
348
	}
114 cycrow 349
 
350
	if ( m_pMap && !m_pMap->empty() ) {
35 cycrow 351
		if ( !m_sAddon.empty() ) {
58 cycrow 352
			FileListItr aitr = m_pMap->find(CFileIO(m_sAddon + "/" + toFile).fullFilename().lower().c_str());
353
			if ( aitr == m_pMap->end() ) aitr = m_pMap->find(CFileIO(_convertExtension(m_sAddon + "/" + file)).fullFilename().lower().c_str());
35 cycrow 354
			if ( aitr != m_pMap->end() ) return aitr->second;
355
		}
102 cycrow 356
		FileListItr itr = m_pMap->find(CFileIO(file).fullFilename().lower().c_str());
357
		if ( itr == m_pMap->end() ) itr = m_pMap->find(CFileIO(_convertExtension(file)).fullFilename().lower().c_str());
35 cycrow 358
		if ( itr != m_pMap->end() ) return itr->second;
359
	}
360
	return "";
361
}
362
 
58 cycrow 363
Utils::String CVirtualFileSystem::_extractFromCat(const Utils::String &sCat, const Utils::String &sFile, const Utils::String &sTo)
35 cycrow 364
{
58 cycrow 365
	CCatFile catFile;
366
	if ( catFile.Open(sCat, m_sAddon, CATREAD_CATDECRYPT, false) == CATERR_NONE )
367
	{
368
		// check for the file
369
		if ( catFile.ExtractFile(sFile, sTo) ) return sTo;
370
		if ( catFile.Error() == CATERR_INVALIDDEST || catFile.Error() == CATERR_CANTCREATEDIR ) {
371
			if ( catFile.ExtractFile(sFile) ) return sFile;
372
		}
373
	}
374
 
375
	return "";
376
}
377
 
378
Utils::String CVirtualFileSystem::_extract(const Utils::String &sFile, const Utils::String &sTo)
379
{
380
	// check if we need to unpack the file
381
	if ( CCatFile::CheckPackedExtension(sFile) ) {
382
		CFileIO File(sFile);
383
		C_File f(CyString(File.fullFilename()));
384
		if ( !f.readFromFile(File) ) return "";
385
		if ( !f.UnPCKFile() ) return "";
386
		if ( !f.WriteToFile(sTo) ) return "";
387
		return sTo;
388
	}
389
	return sFile;
390
}
391
 
392
Utils::String CVirtualFileSystem::ExtractGameFile(const Utils::String &file, const Utils::String &to)
393
{
43 cycrow 394
	Utils::String sIn = _findFile(file);
395
	Utils::String sFile = file;
396
 
58 cycrow 397
	if ( sIn.empty() ) return "";
35 cycrow 398
 
43 cycrow 399
	if ( sIn.isin("::") ) {
400
		sFile = sIn.token("::", 2);
401
		sIn = sIn.token("::", 1);
58 cycrow 402
		return _extractFromCat(sIn, sFile, to);
43 cycrow 403
	}
58 cycrow 404
 
405
	return _extract(sIn, to);
1 cycrow 406
}
407
 
35 cycrow 408
Utils::String CVirtualFileSystem::_convertExtension(const Utils::String &sFile)
409
{
410
	CFileIO File(sFile);
411
	if ( File.CheckFileExtension("pck") ) {
412
		return File.ChangeFileExtension("xml").ToString();
413
	}
414
	else if ( File.CheckFileExtension("xml") ) {
415
		return File.ChangeFileExtension("pck").ToString();
416
	}
417
	else if ( File.CheckFileExtension("pbb") ) {
418
		return File.ChangeFileExtension("bob").ToString();
419
	}
420
	else if ( File.CheckFileExtension("bob") ) {
421
		return File.ChangeFileExtension("pbb").ToString();
422
	}
423
	else if ( File.CheckFileExtension("pbd") ) {
424
		return File.ChangeFileExtension("bod").ToString();
425
	}
426
	else if ( File.CheckFileExtension("bod") ) {
427
		return File.ChangeFileExtension("pbd").ToString();
428
	}
429
 
430
	return sFile;
431
}
432
 
101 cycrow 433
Utils::CStringList *CVirtualFileSystem::getTShipsEntries()
35 cycrow 434
{
101 cycrow 435
	if ( !_lShips || _lShips->empty() ) _updateShips();
436
	return _lShips;
35 cycrow 437
}
438
 
439
C_File *CVirtualFileSystem::extractGameFileToPackage(CBaseFile *pPackage, const Utils::String &sFile, FileType iFileType)
440
{
441
	return this->extractGameFileToPackage(pPackage, sFile, iFileType, sFile);
442
}
443
 
444
C_File *CVirtualFileSystem::extractGameFileToPackage(CBaseFile *pPackage, const Utils::String &sFile, FileType iFileType, const Utils::String &sTo)
445
{
79 cycrow 446
	Utils::String to = this->ExtractGameFile(sFile, CPackages::tempDirectory() + "tmp.dat");
58 cycrow 447
	if ( !to.empty() ) {
448
		CFileIO File(to);
35 cycrow 449
 
102 cycrow 450
		C_File *f = pPackage->AddFile(CFileIO(sTo).filename(), CFileIO(sTo).dir(), iFileType);
35 cycrow 451
		if ( f ) {
79 cycrow 452
			if ( f->ReadFromFile(CPackages::GetTempDirectory() + "tmp.dat") ) {
52 cycrow 453
				File.remove();
35 cycrow 454
				return f;
455
			}
456
		}
457
 
52 cycrow 458
		File.remove();
35 cycrow 459
	}
460
 
461
	return NULL;
462
}
463
 
464
Utils::String CVirtualFileSystem::getTShipsEntry(const Utils::String &sId)
465
{
101 cycrow 466
	if ( !_lShips || _lShips->empty() ) _updateShips();
467
	return _lShips->findData(sId, true);
35 cycrow 468
}
469
 
470
void CVirtualFileSystem::extractTexts(CXspFile *pPackage, int textId)
471
{
472
	//TODO: check for better finding of files in map
473
	for ( FileListItr itr = m_pMap->begin(); itr != m_pMap->end(); ++itr ) {
474
		Utils::String str = itr->first;
475
 
476
		if ( !m_sAddon.empty() ) {
477
			if ( !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "/t/") && !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "\\t\\") )
478
				continue;
479
		}
480
		else {
481
			if ( !str.left(2).Compare("t\\") && !str.left(2).Compare("t/") && !str.left(8).Compare("addon\t\\") && !str.left(8).Compare("addon/t/") )
482
				continue;
483
		}
484
 
79 cycrow 485
		Utils::String sTo = this->ExtractGameFile(str, CPackages::tempDirectory() + "tmp.dat");
58 cycrow 486
		if ( !sTo.empty() ) {
487
			pPackage->AddTextFromFile(sTo, textId);
488
			CFileIO::Remove(sTo);
35 cycrow 489
		}
490
	}
491
}
492
 
493
void CVirtualFileSystem::updateTexts(int iFromPage, int iToPage)
494
{
495
	this->_updateTexts(iFromPage, iToPage, m_pMap, m_pTexts);
496
}
497
void CVirtualFileSystem::updateTexts(int iPage)
498
{
499
	this->_updateTexts(iPage, iPage, m_pMap, m_pTexts);
500
}
501
void CVirtualFileSystem::updateModTexts(int iFromPage, int iToPage)
502
{
503
	this->_updateTexts(iFromPage, iToPage, m_pModMap, m_pModTexts);
504
}
505
void CVirtualFileSystem::updateModTexts(int iPage)
506
{
507
	this->_updateTexts(iPage, iPage, m_pModMap, m_pModTexts);
508
}
509
 
510
void CVirtualFileSystem::_clear()
511
{
512
	m_bLoaded = false;
513
 
514
	if ( m_pMap ) delete m_pMap;
515
	m_pMap = new FileList;
516
	if ( m_pModMap ) delete m_pModMap;
517
	m_pModMap = new FileList;
101 cycrow 518
 
519
	DELETELIST(_lShields);
520
	DELETELIST(_lLasers);
521
	DELETELIST(_lMissiles);
522
	DELETELIST(_lCockpits);
523
	DELETELIST(_lComponentSections);
524
	DELETELIST(_lDummySections);
525
	DELETELIST(_lBodiesSections);
526
	DELETELIST(_lShips);
35 cycrow 527
}
528
 
529
void CVirtualFileSystem::_updateTexts(int iFromPage, int iToPage, FileList *pFileList, CTextDB *pTextList)
530
{
531
	// read all text files
532
	for ( FileListItr itr = pFileList->begin(); itr != pFileList->end(); ++itr ) {
533
		Utils::String str = itr->first;
534
 
535
		if ( !m_sAddon.empty() ) {
536
			if ( !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "/t/") && !str.left(m_sAddon.length() + 3).Compare(m_sAddon + "\\t\\") )
537
				continue;
538
		}
539
		else {
540
			if ( !str.left(2).Compare("t\\") && !str.left(2).Compare("t/") )
541
				continue;
542
		}
543
 
79 cycrow 544
		Utils::String sTo = this->ExtractGameFile(str, CPackages::tempDirectory() + "tmp.dat");
58 cycrow 545
		if ( !sTo.empty() ) {
35 cycrow 546
			// add all texts into the map, id=(pageid, tid) data=text
57 cycrow 547
			Utils::String baseFile = CFileIO(str).baseName();
35 cycrow 548
			int lang = (baseFile.isin("-L")) ? baseFile.right(3) : baseFile.truncate(-4);
549
 
550
			if ( m_iLang && lang != m_iLang ) continue;
551
			// open the text file to add
58 cycrow 552
			pTextList->parseTextFile(iFromPage, iToPage, sTo, lang);
35 cycrow 553
		}
554
	}
555
}
556
 
78 cycrow 557
void CVirtualFileSystem::clearMods(bool bIncludeStandard)
35 cycrow 558
{
78 cycrow 559
	if ( bIncludeStandard ) {
560
		if ( m_pTexts ) delete m_pTexts;
561
		m_pTexts = new CTextDB();
562
	}
563
 
35 cycrow 564
	if ( m_pModTexts ) delete m_pModTexts;
565
	m_pModTexts = new CTextDB();
566
}
567
 
94 cycrow 568
Utils::CStringList *CVirtualFileSystem::_updateList(const Utils::String &typesFile, int iTextPos)
569
{
570
	Utils::CStringList *list = new Utils::CStringList();
35 cycrow 571
 
94 cycrow 572
	Utils::String file = this->ExtractGameFile("types\\" + typesFile + ".pck", CPackages::tempDirectory() + "/" + typesFile + ".txt");
573
 
574
	CFileIO f(file);
575
 
576
	if ( f.exists() && f.startRead() ) {
577
		int itemsLeft = -1;
578
 
579
		while(!f.atEnd()) {
580
			// read the next line
581
			Utils::String line = f.readEndOfLine();
582
 
583
			// remove the unneeded characters
584
			line.removeChar('\r');
585
			line.removeChar(9);
586
			line.removeFirstSpace();
587
 
588
			// blank line ? skip it
589
			if ( line.empty() ) continue;
590
			// skip anything starting with / as its a comment
591
			if ( line[0] == '/' ) continue;
592
 
593
			// first line is the count
594
			if ( itemsLeft == -1 )
595
				itemsLeft = line.token(";", 2);
596
			else {
597
				if ( iTextPos == 0 )
598
					list->pushBack(line, line);
599
				else if ( iTextPos == -1 ) 
600
					list->pushBack(line, line.token(";", -2));
601
				else
602
					list->pushBack(line, this->findText(m_iLang, TEXTPAGE_OBJECTS, line.token(";", iTextPos).toLong()));
603
				--itemsLeft;
604
			}
605
		}
606
 
607
		f.close();
608
		f.remove();
609
	}
610
 
611
	return list;
612
}
613
 
614
void CVirtualFileSystem::_updateShields()
615
{
616
	DELETELIST(_lShields);
617
	_lShields = _updateList("TShields", 7);
618
}
619
 
620
void CVirtualFileSystem::_updateLasers()
621
{
622
	DELETELIST(_lLasers);
623
	_lLasers = _updateList("TLaser", 7);
624
}
625
 
626
void CVirtualFileSystem::_updateMissiles()
627
{
628
	DELETELIST(_lMissiles);
629
	_lMissiles = _updateList("TMissiles", 7);
630
}
631
 
632
void CVirtualFileSystem::_updateCockpits()
633
{
634
	DELETELIST(_lCockpits);
635
	_lCockpits = _updateList("TCockpits", -1);
636
}
637
 
101 cycrow 638
void CVirtualFileSystem::_updateShips()
639
{
640
	DELETELIST(_lShips);
641
	_lShips = _updateList("TShips", -1);
642
}
643
 
94 cycrow 644
void CVirtualFileSystem::_updateComponentSections()
645
{
646
	DELETELIST(_lComponentSections);
647
 
648
	_lComponentSections = new Utils::CStringList();
649
 
650
	Utils::String file = this->ExtractGameFile("types\\Components.pck", CPackages::tempDirectory() + "/Components.txt");
651
 
652
	CFileIO f(file);
653
 
654
	if ( f.exists() && f.startRead() ) {
655
		int sectionRemaining = 0;
656
		int itemsRemaining = 0;
657
 
658
		while(!f.atEnd()) {
659
			// read the next line
660
			Utils::String line = f.readEndOfLine();
661
 
662
			// remove the unneeded characters
663
			line.removeChar('\r');
664
			line.removeChar(9);
665
			line.removeFirstSpace();
666
 
667
			// blank line ? skip it
668
			if ( line.empty() ) continue;
669
			// skip anything starting with / as its a comment
670
			if ( line[0] == '/' ) continue;
671
 
672
			if ( itemsRemaining )
673
				--itemsRemaining;
674
			else if ( !sectionRemaining )
675
			{
676
				sectionRemaining = line.token(";", 2).toLong();
677
				Utils::String section = line.token(";", 1);
678
				if ( !section.empty() )
679
					_lComponentSections->pushBack(section, section);
680
			}
681
			else if ( !itemsRemaining )
682
			{
683
				itemsRemaining = line.token(";", 2).toLong();
684
				--sectionRemaining;
685
			}
686
		}
687
 
688
		f.close();
689
		f.remove();
690
	}
691
 
692
	if ( _lComponentSections->empty() )
693
	{
694
		_lComponentSections->pushBack("SCTYPE_LASER", "SCTYPE_LASER");
695
		_lComponentSections->pushBack("SCTYPE_COCKPIT", "SCTYPE_COCKPIT");
696
		_lComponentSections->pushBack("SCTYPE_DOCKING", "SCTYPE_DOCKING");
697
	}
698
}
699
 
700
Utils::CStringList *CVirtualFileSystem::_updateSectionList(const Utils::String &sFile, bool singleEntry)
701
{
702
	Utils::CStringList *list = new Utils::CStringList();
703
 
704
	Utils::String file = this->ExtractGameFile("types\\" + sFile + ".pck", CPackages::tempDirectory() + "/" + sFile + ".txt");
705
 
706
	CFileIO f(file);
707
 
708
	if ( f.exists() && f.startRead() ) {
709
		int sectionRemaining = 0;
710
 
711
		while(!f.atEnd()) {
712
			// read the next line
713
			Utils::String line = f.readEndOfLine();
714
 
715
			// remove the unneeded characters
716
			line.removeChar('\r');
717
			line.removeChar(9);
718
			line.removeFirstSpace();
719
 
720
			// blank line ? skip it
721
			if ( line.empty() ) continue;
722
			// skip anything starting with / as its a comment
723
			if ( line[0] == '/' ) continue;
724
 
725
			if ( !sectionRemaining )
726
			{
727
				sectionRemaining = line.token(";", 2).toLong();
728
				Utils::String section = line.token(";", 1);
729
				if ( !section.empty() )
730
					list->pushBack(section, section);
731
			}
732
			else {
733
				if ( singleEntry )
734
					sectionRemaining -= (line.countToken(";") - 1);
735
				else 
736
					--sectionRemaining;
737
			}
738
		}
739
 
740
		f.close();
741
		f.remove();
742
	}
743
 
744
	return list;
745
}
746
 
747
void CVirtualFileSystem::_updateDummySections()
748
{
749
	DELETELIST(_lDummySections);
750
 
751
	_lDummySections = _updateSectionList("Dummies", false);
752
 
753
	if ( _lDummySections->empty() )
754
	{
755
		_lDummySections->pushBack("SDTYPE_ANIMATED", "SDTYPE_ANIMATED");
756
		_lDummySections->pushBack("SDTYPE_DOCK", "SDTYPE_DOCK");
757
		_lDummySections->pushBack("SDTYPE_DOORWAY", "SDTYPE_DOORWAY");
758
		_lDummySections->pushBack("SDTYPE_GUN", "SDTYPE_GUN");
759
		_lDummySections->pushBack("SDTYPE_CONNECTION", "SDTYPE_CONNECTION");
760
	}
761
}
762
 
763
void CVirtualFileSystem::_updateBodiesSections()
764
{
765
	DELETELIST(_lBodiesSections);
766
 
767
	_lBodiesSections = _updateSectionList("Bodies", true);
768
 
769
	if ( _lBodiesSections->empty() )
770
	{
771
		_lBodiesSections->pushBack("SBTYPE_2D", "SBTYPE_2D");
772
		_lBodiesSections->pushBack("SBTYPE_FACECAMERA", "SBTYPE_FACECAMERA");
773
		_lBodiesSections->pushBack("SBTYPE_2D2", "SBTYPE_2D2");
774
		_lBodiesSections->pushBack("SBTYPE_2DY", "SBTYPE_2DY");
775
		_lBodiesSections->pushBack("SBTYPE_LOGO", "SBTYPE_LOGO");
776
		_lBodiesSections->pushBack("SBTYPE_FC", "SBTYPE_FC");
777
		_lBodiesSections->pushBack("SBTYPE_TURRET", "SBTYPE_TURRET");
778
		_lBodiesSections->pushBack("SBTYPE_JET", "SBTYPE_JET");
779
		_lBodiesSections->pushBack("SBTYPE_RADAR", "SBTYPE_RADAR");
780
		_lBodiesSections->pushBack("SBTYPE_CONTAINER", "SBTYPE_CONTAINER");
781
		_lBodiesSections->pushBack("SBTYPE_DISPLAY", "SBTYPE_DISPLAY");
782
		_lBodiesSections->pushBack("SBTYPE_DOCKPOINT", "SBTYPE_DOCKPOINT");
783
		_lBodiesSections->pushBack("SBTYPE_SMALLJET", "SBTYPE_SMALLJET");
784
	}
785
}
786
 
114 cycrow 787
 
788
/////////////////////////////////////////////////////////////////////////////////////////
789
//	DEBUG Functions
790
 
791
 
792
void CVirtualFileSystem::DEBUG_LogContents(const Utils::String &sFile)
793
{
794
	CFileIO File(sFile);
795
 
796
	if ( File.startWrite() ) {
797
		if ( m_pMap ) {
798
			for(FileListItr itr = m_pMap->begin(); itr != m_pMap->end(); itr++) {
799
				Utils::String line = itr->first + " => " + itr->second + "\r\n";
800
				File.write(line.c_str(), line.length());
801
			}
802
		}
803
 
804
		File.close();
805
	}
806
}
807
 
35 cycrow 808
} //NAMESPACE