Subversion Repositories spk

Rev

Rev 95 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 cycrow 1
 
2
#include "GameDirectories.h"
3
#include "GameExe.h"
4
#include "Packages.h"
5
 
6
namespace SPK {
7
 
8
	CGameDirectories::CGameDirectories(void) : _pSelected(NULL),
9
		_pCurrentItr(NULL),
10
		_iLanguage(0)
11
	{
12
		_pDirs = new std::vector<SGameDir *>();
13
	}
14
 
15
	CGameDirectories::~CGameDirectories(void)
16
	{
17
		clear();
18
		delete _pDirs;
19
	}
20
 
21
	bool CGameDirectories::add(const Utils::String &dir, const Utils::String &name, int iGame, const Utils::String &addon, bool bLoad)
22
	{
23
		for ( std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++ ) {
24
			if ( dir.Compare((*itr)->dir) ) return false;
25
		}
26
 
27
		_add(dir, name, iGame, addon, bLoad);
28
		return true;
29
	}
30
 
31
	bool CGameDirectories::remove(const Utils::String &dir)
32
	{
33
		for ( std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++ ) {
34
			if ( dir.Compare((*itr)->dir) ) {
35
				delete (*itr);
36
				_pDirs->erase(itr);
37
				return true;
38
			}
39
		}
40
 
41
		return false;
42
	}
43
 
44
	bool CGameDirectories::findDir(const Utils::String &dir)
45
	{
46
		SGameDir *d = _findGameDir(dir);
47
		if ( d ) {
48
			_pCurrentItr = d;
49
			return true;
50
		}
51
 
52
		return false;
53
	}
54
 
55
	bool CGameDirectories::parse(const Utils::String &data, CPackages *pPackages)
56
	{
57
		int iGame = data.token(";", 2).tokens(" ", 1).toLong();
58
		SGameExe *exe = pPackages->GetGameExe()->GetGame(iGame);
59
		_add(data.token(";", 1), data.token(";", 2).tokens(" ", 2), iGame, (exe) ? exe->sAddon : Utils::String::Null(), data.token(";", 3).toBool());
60
 
61
		return true;
62
	}
63
 
64
	bool CGameDirectories::writeData(CyStringList *lines)
65
	{
66
		bool writeAny = false;
67
		for(std::vector<SGameDir *>::const_iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
68
			SGameDir *gd = *itr;
69
			lines->PushBack(CyString("GameDir:" + gd->dir + ";" + Utils::String::Number(gd->iGame) + " " + gd->name + ((gd->bLoad) ? ";1" : ";0")));
70
			writeAny = true;
71
		}
72
 
73
		return writeAny;
74
	}
75
 
76
	void CGameDirectories::updateCurrentVFS()
77
	{
78
		if ( !_pCurrentItr ) return;
79
 
80
		_pCurrentItr->pVfs->updateTexts(0);
81
	}
82
 
83
	void CGameDirectories::clear()
84
	{
85
		for ( std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++ ) {
86
			delete *itr;
87
		}
88
		_pDirs->clear();
89
	}
90
 
91
	bool CGameDirectories::setSelectedGameDirectory(int iGame, bool temp)
92
	{
93
		_pTemporary = NULL;
94
		if ( temp ) _pTemporary = _pSelected;
95
		_pSelected = _findGameDir(iGame);
96
		return hasSelected();
97
	}
98
 
99
	bool CGameDirectories::setSelectedDirectory(const Utils::String &dir, bool temp)
100
	{
101
		_pTemporary = NULL;
102
		if ( temp ) _pTemporary = _pSelected;
103
		_pSelected = _findGameDir(dir);
104
		return hasSelected();
105
	}
106
 
107
	void CGameDirectories::reselectTemporaryDirectory()
108
	{
109
		if ( _pTemporary ) _pSelected = _pTemporary;
110
		_pTemporary = NULL;
111
	}
112
 
113
	void CGameDirectories::setLanguage(int iLang)
114
	{
115
		_iLanguage = iLang;
116
	}
117
 
118
	void CGameDirectories::setLoad(bool bLoad)
119
	{
120
		if ( _pCurrentItr ) _pCurrentItr->bLoad = bLoad;
121
	}
122
 
123
	bool CGameDirectories::isDir(const Utils::String &dir) const
124
	{
125
		for ( std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++ ) {
126
			if ( dir.Compare((*itr)->dir) ) return true;
127
		}
128
 
129
		return false;
130
	}
131
 
132
	bool CGameDirectories::hasSelected() const
133
	{
134
		return (_pSelected) ? true : false;
135
	}
136
 
137
	bool CGameDirectories::isEmpty() const
138
	{
139
		return _pDirs->empty();
140
	}
141
 
142
	int CGameDirectories::highestGame() const
143
	{
144
		int highest = 1;
145
		for(std::vector<SGameDir *>::const_iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
146
			if ( (*itr)->iGame > highest )
147
				highest = (*itr)->iGame;
148
		}
149
 
150
		return highest;
151
	}
152
 
153
	Utils::String CGameDirectories::currentName() const
154
	{
155
		if ( !_pCurrentItr ) return Utils::String::Null();
156
		return _pCurrentItr->name;
157
	}
158
 
159
	int CGameDirectories::currentGame() const
160
	{
161
		if ( !_pCurrentItr ) return -1;
162
		return _pCurrentItr->iGame;
163
	}
164
 
165
	bool CGameDirectories::currentLoad() const
166
	{
167
		if ( !_pCurrentItr ) return false;
168
		return _pCurrentItr->bLoad;
169
	}
170
 
171
	CVirtualFileSystem *CGameDirectories::selectedVFS() const
172
	{
173
		if ( _pSelected ) return _pSelected->pVfs;
174
		return NULL;
175
	}
176
 
177
	Utils::String CGameDirectories::findText(int iLanguage, int iPage, int iID, Utils::String missing)
178
	{
179
		if ( iLanguage == 0 ) iLanguage = _iLanguage;
180
		if ( _pSelected ) {
181
			if ( _pSelected->pVfs->textExists(iLanguage, iPage, iID) ) {
182
				return _pSelected->pVfs->findText(iLanguage, iPage, iID);
183
			}
184
		}
185
 
186
		return missing;
187
	}
188
 
189
	Utils::String CGameDirectories::findText(int iGame, int iLanguage, int iPage, int iID)
190
	{
191
		if ( iLanguage == 0 ) iLanguage = _iLanguage;
192
		if ( iGame == -1 ) {
193
			for ( int game = (GAME_MAX - 1); game >= 0; game-- ) {
194
				SGameDir *gd = _findGameDir(game - 1);
195
				if ( !gd ) continue;
196
				if ( gd->pVfs->textExists(iLanguage, iPage, iID) ) {
197
					return gd->pVfs->findText(iLanguage, iPage, iID);
198
				}
199
			}
200
		}
201
		else {
202
			SGameDir *gd = _findGameDir(iGame);
203
			if ( gd ) {
204
				if ( gd->pVfs->textExists(iLanguage, iPage, iID) ) {
205
					return gd->pVfs->findText(iLanguage, iPage, iID);
206
				}
207
			}
208
		}
209
 
210
		return Utils::String::Null();
211
	}
212
 
213
	Utils::String CGameDirectories::first(int iGame)
214
	{
215
		if ( _pDirs->empty() ) return Utils::String::Null();
216
 
217
		_pCurrentItr = *_pDirs->begin();
218
		if ( iGame > -1 && _pCurrentItr->iGame != iGame ) return next(iGame);
219
		return (_pCurrentItr) ? _pCurrentItr->dir : Utils::String::Null();
220
	}
221
 
222
	Utils::String CGameDirectories::next(int iGame)
223
	{
224
		if ( !_pCurrentItr ) return Utils::String::Null();
225
 
226
		bool found = false;
227
		for(std::vector<SGameDir *>::iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
228
			if ( (*itr) == _pCurrentItr ) found = true;
229
			else if ( found && ((*itr)->iGame == iGame || iGame == -1) ) {
230
				_pCurrentItr = *(itr);
231
				return (_pCurrentItr) ? _pCurrentItr->dir : Utils::String::Null();
232
			}
233
		}
234
 
235
		return Utils::String::Null();
236
	}
237
 
238
	Utils::String CGameDirectories::firstShield()
239
	{
240
		if ( !_pSelected ) return Utils::String::Null();
241
		return _pSelected->pVfs->firstShield();
242
	}
243
 
244
	Utils::String CGameDirectories::nextShield()
245
	{
246
		if ( !_pSelected ) return Utils::String::Null();
247
		return _pSelected->pVfs->nextShield();
248
	}
249
 
250
	Utils::String CGameDirectories::firstComponentSection()
251
	{
252
		if ( !_pSelected ) return Utils::String::Null();
253
		return _pSelected->pVfs->firstComponentSection();
254
	}
255
 
256
	Utils::String CGameDirectories::nextComponentSection()
257
	{
258
		if ( !_pSelected ) return Utils::String::Null();
259
		return _pSelected->pVfs->nextComponentSection();
260
	}
261
 
262
	Utils::String CGameDirectories::firstDummySection()
263
	{
264
		if ( !_pSelected ) return Utils::String::Null();
265
		return _pSelected->pVfs->firstDummySection();
266
	}
267
 
268
	Utils::String CGameDirectories::nextDummySection()
269
	{
270
		if ( !_pSelected ) return Utils::String::Null();
271
		return _pSelected->pVfs->nextDummySection();
272
	}
273
 
274
	Utils::String CGameDirectories::firstBodiesSection()
275
	{
276
		if ( !_pSelected ) return Utils::String::Null();
277
		return _pSelected->pVfs->firstBodiesSection();
278
	}
279
 
280
	Utils::String CGameDirectories::nextBodiesSection()
281
	{
282
		if ( !_pSelected ) return Utils::String::Null();
283
		return _pSelected->pVfs->nextBodiesSection();
284
	}
285
 
286
	Utils::String CGameDirectories::firstCockpit()
287
	{
288
		if ( !_pSelected ) return Utils::String::Null();
289
		return _pSelected->pVfs->firstCockpit();
290
	}
291
 
292
	Utils::String CGameDirectories::nextCockpit()
293
	{
294
		if ( !_pSelected ) return Utils::String::Null();
295
		return _pSelected->pVfs->nextCockpit();
296
	}
297
 
298
	std::pair<Utils::String, Utils::String> CGameDirectories::firstLaser()
299
	{
300
		if ( !_pSelected ) return std::make_pair<Utils::String, Utils::String>(Utils::String::Null(), Utils::String::Null());
301
		return _pSelected->pVfs->firstLaser();
302
	}
303
 
304
	std::pair<Utils::String, Utils::String> CGameDirectories::nextLaser()
305
	{
306
		if ( !_pSelected ) return std::make_pair<Utils::String, Utils::String>(Utils::String::Null(), Utils::String::Null());
307
		return _pSelected->pVfs->nextLaser();
308
	}
309
 
310
	std::pair<Utils::String, Utils::String> CGameDirectories::firstMissile()
311
	{
312
		if ( !_pSelected ) return std::make_pair<Utils::String, Utils::String>(Utils::String::Null(), Utils::String::Null());
313
		return _pSelected->pVfs->firstMissile();
314
	}
315
 
316
	std::pair<Utils::String, Utils::String> CGameDirectories::nextMissile()
317
	{
318
		if ( !_pSelected ) return std::make_pair<Utils::String, Utils::String>(Utils::String::Null(), Utils::String::Null());
319
		return _pSelected->pVfs->nextMissile();
320
	}
321
 
322
	SGameDir *CGameDirectories::_findGameDir(int iGame) const
323
	{
324
		if ( iGame == -1 ) {
325
			for ( int game = (GAME_MAX - 1); game >= 0; game-- ) {
326
				SGameDir *gd = _findGameDir(game);
327
				if ( !gd ) continue;
328
				return gd;
329
			}
330
		}
331
		else {
332
			for(std::vector<SGameDir *>::const_iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
333
				if ( (*itr)->iGame == iGame ) return *itr;
334
			}
335
		}
336
		return NULL;
337
	}
338
 
339
	SGameDir *CGameDirectories::_findGameDir(const Utils::String &dir) const
340
	{
341
		for(std::vector<SGameDir *>::const_iterator itr = _pDirs->begin(); itr != _pDirs->end(); itr++) {
342
			if ( (*itr)->dir.Compare(dir) )
343
				return *itr;
344
		}
345
 
346
		return NULL;
347
	}
348
 
349
	void CGameDirectories::_add(const Utils::String &dir, const Utils::String &name, int iGame, const Utils::String &addon, bool bLoad)
350
	{
351
		SGameDir *gd = new SGameDir;
352
 
353
		gd->dir = dir;
354
		gd->name = name;
355
		gd->iGame = iGame;
356
 
357
		gd->bLoad = bLoad;
358
		gd->pVfs = new CVirtualFileSystem();
359
		gd->pVfs->setLanguage(44);
360
		gd->pVfs->LoadFilesystem(gd->dir);
361
		gd->pVfs->SetAddon(addon);
362
 
363
		_pDirs->push_back(gd);
364
	}
365
}