File: attalCommon.cpp

package info (click to toggle)
attal 1.0~rc2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 4,348 kB
  • ctags: 7,428
  • sloc: cpp: 55,101; sh: 267; ansic: 100; makefile: 54
file content (283 lines) | stat: -rw-r--r-- 7,805 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/****************************************************************
**
** Attal : Lords of Doom
**
** attalCommon.cpp
** Manage common data for everyone
**
** Version : $Id: attalCommon.cpp,v 1.25 2008/05/22 21:06:06 lusum Exp $
**
** Author(s) : Pascal Audoux
**
** Date : 26/07/2005
**
** Licence :
**  This program is free software; you can redistribute it and/or modify
**      it under the terms of the GNU General Public License as published by
**      the Free Software Foundation; either version 2, or (at your option)
**      any later version.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
****************************************************************/

#include "attalCommon.h"

// generic include files
#include <stdio.h>
#include <iostream>
#include <cstdlib>
// include files for QT
#include <QDir>
#include <QLocale>
#include <QString>
// application specific include files
#include "conf.h"
#include "libCommon/log.h"

using namespace std;

QString IMAGE_PATH( IMAGE_DEFAULT_PATH );

QString MUSIC_PATH( MUSIC_DEFAULT_PATH );

QString SOUND_PATH( SOUND_DEFAULT_PATH );

QString DATA_PATH( DATA_DEFAULT_PATH );

QString CAMPAIGN_PATH( CAMPAIGN_DEFAULT_PATH );

QString SCENARIO_PATH( SCENARIO_DEFAULT_PATH );

QString CONFIG_PATH( CONFIG_DEFAULT_PATH );

QString THEME( THEME_DEFAULT );

QString SAVE_PATH;

QString THEME_CONFIG_PATH;

QString VERSION(ATTAL_VERSION);

QString THEME_PATH;

QString PORT = "1717";
bool FAST = false;
bool HIDE = false;

#ifdef WITH_SOUND
bool ATT_SOUND = true;
#endif

using namespace AttalCommon;

int parseArgs(int argc, char** argv, AttalProgramType prog )
{

#ifdef WITH_SOUND
	if( prog !=  ATTALCLIENT /* && prog != ATTALTHEMEEDITOR */ ) {
		ATT_SOUND = false;
	}
#endif

	for( int i = 1; i < argc; i++ ) {
		if( ( strcmp( argv[i], "-theme" ) == 0 ) || ( strcmp( argv[i], "--theme" ) == 0 ) ) {
			i++;
			if( i < argc )
				THEME = QString( argv[i] );
		} else if(  (strcmp( argv[i], "-port" ) == 0 ) || ( strcmp( argv[i], "--port" ) == 0) )  {
			i++;
			if( i < argc ){
				PORT = QString( argv[i] );
			}
		} else if( ( strcmp( argv[i], "-d" ) == 0 )  ) {	
			int level;
			i++;
			if( i < argc ){
				level = atoi(argv[i]) ;
				setLogLevel((LogLevel) level );
			}
		} else if(  strcmp( argv[i], "--hide" ) == 0  ) {
			HIDE = true;
		} else if( ( strcmp( argv[i], "--themepath" ) == 0 )  ) {	
			i++;
			if( i < argc )
				THEME_PATH = QString( argv[i] );
		} else if( ( strcmp( argv[i], "-fast" ) == 0 ) || ( strcmp( argv[i], "--fast" ) == 0 ) ) {		
			FAST = true;
		} else if(  strcmp( argv[i], "--nosound")  == 0  ) {
#ifdef WITH_SOUND
			ATT_SOUND = false;
#endif
		} else if( ( strcmp( argv[i], "-help" ) == 0 ) || ( strcmp( argv[i], "--help" ) == 0 ) ) {
			switch( prog ) {
				case ATTALSERVER:
					cout << "Usage: attal-server [OPTION]"  << endl;
					cout << "Start the server "  << endl;
					cout << endl;
					break;
				case ATTALCLIENT:
					cout << "Usage: attal-client [OPTION]"  << endl;
					cout << "Start client "  << endl;
					cout << endl;
					break;
				case ATTALCAMPAIGNEDITOR:
					cout << "Usage: campaignEditor [OPTION]"  << endl;
					cout << "Start campaignEditor "  << endl;
					cout << endl;
					break;
				case ATTALSCENARIOEDITOR:
					cout << "Usage: scenarioEditor [OPTION]"  << endl;
					cout << "Start scenarioEditor "  << endl;
					cout << endl;
					break;
				case ATTALTHEMEEDITOR:
					cout << "Usage: themeEditor [OPTION]"  << endl;
					cout << "Start themeEditor "  << endl;
					cout << endl;
					break;
				case ATTALAI:
					cout << "Usage: attal-ai [OPTION]"  << endl;
					cout << "Start ai robot "  << endl;
					cout << endl;
					cout << "  --hide                      Hide ai (remember to kill)"  << endl;
					break;
			}
			switch( prog ) {
				case ATTALSERVER:
				case ATTALAI:
					cout << "  -port   --port [PORT]       Used to use a particular port"  << endl;
					cout << "  -fast   --fast              Fast start use: localhost:1717"  << endl;
					break;
				case ATTALCLIENT:
				case ATTALTHEMEEDITOR:
#ifdef WITH_SOUND
					cout << "  --nosound                   Disable totally sound (ex: for hardware problems)"  << endl;
#endif
					break;
				default:
					break;
			};

			cout << "  -theme  --theme [NAME]      Load a particular theme (Medieval by default)"  << endl;
			cout << "  --themepath [NAME]          Get the path of theme (local directory or hardcoded path will be the default)"  << endl;
			cout << "  -d [LEVEL]                  Level of debug messages (0-5) (default 4)"  << endl;
			cout << "  --version -v                Prints attal's version and exits."  << endl;
			cout << "  -help   --help              This screen"  << endl;
			return 1; 
		} else if( ( strcmp( argv[i], "--version" ) == 0 ) || ( strcmp( argv[i], "-v" ) == 0)  ) {	
			cout << "Attal Lords of Doom " << VERSION.toLatin1().constData() << endl;
#ifdef __GNUC__
			cout << "Compiler:" << "\tGCC " <<  __GNUC__ << "." << __GNUC_MINOR__ << "." <<  __GNUC_PATCHLEVEL__  << "\n";
#endif
			return 1;
		}
	}
	return 0;

}

void useTheme()
{
	QDir folder;
	
	folder = setThemePath( THEME_PATH );

	if( ! folder.exists() ) {
		QString envpath = getenv("ATTAL_THEME_PATH");
		TRACE("EnvPath %s", envpath.toLatin1().constData());
		folder = setThemePath( envpath );
	}

	if( ! folder.exists() ) {
		folder = setThemePath( "./themes" );
	}

#ifndef WIN32
	QString qConfigPath;
	qConfigPath = ATT_THEMES_DIR;
	if( ! folder.exists() ) {
		folder = setThemePath( qConfigPath );
	}
#endif

	if( ! folder.exists() ) {
		logEE( "Theme %s not found", THEME.toLatin1().constData() );
		THEME = THEME_DEFAULT;
	}
	THEME_PATH = folder.absolutePath() + "/";

	TRACE("ThemePath %s", THEME_PATH.toLatin1().constData());

	IMAGE_PATH = THEME_PATH + IMAGE_DEFAULT_PATH;
	SOUND_PATH = THEME_PATH + SOUND_DEFAULT_PATH;
	MUSIC_PATH = THEME_PATH + MUSIC_DEFAULT_PATH;
	DATA_PATH = THEME_PATH + DATA_DEFAULT_PATH;
	CAMPAIGN_PATH = THEME_PATH + CAMPAIGN_DEFAULT_PATH;
	
	TRACE("Campaign Path %s", CAMPAIGN_PATH.toLatin1().constData());
	SCENARIO_PATH = THEME_PATH + SCENARIO_DEFAULT_PATH;

	QDir tmpdir = QDir::home();
	if( tmpdir.isRoot() ) {
		CONFIG_PATH = QDir::homePath() + CONFIG_DEFAULT_PATH; 
	} else {
		CONFIG_PATH = QDir::homePath() + QDir::separator() + CONFIG_DEFAULT_PATH; 
	}
	folder.setPath(CONFIG_PATH);
	if( ! folder.exists() ) {
		if(!folder.mkdir(CONFIG_PATH)) {
			logEE( "Cannot create config dir %s", CONFIG_PATH.toLatin1().constData() );
		}
	}
	THEME_CONFIG_PATH = CONFIG_PATH + THEME;
	folder.setPath(THEME_CONFIG_PATH);
	if( ! folder.exists() ) {
		if(!folder.mkdir(THEME_CONFIG_PATH)) {
			logEE( "Cannot create theme config dir %s", THEME_CONFIG_PATH.toLatin1().constData() );
		}
	}
	SAVE_PATH =  THEME_CONFIG_PATH +  "/saves/";

	TRACE("ThemeConfigPath %s, savepath %s", THEME_CONFIG_PATH.toLatin1().constData(), SAVE_PATH.toLatin1().constData());

	folder.setPath(SAVE_PATH);
	if( ! folder.exists() ) {
		if(!folder.mkdir(SAVE_PATH)) {
			logEE( "Cannot create save dir %s", SAVE_PATH.toLatin1().constData() );
		}
	}
	/// XXX: need more tests of existence
}

QDir translationsDir()
{

    QDir trfolder;
    trfolder.setPath( "./i18n/" +  QLocale::system().name() .section('_', 0,0 ) );
#ifndef WIN32
		QString qConfigPath;
		qConfigPath = ATT_TRANSL_DIR;
		//logDD("attalCommon %s", qConfigPath.toLatin1().constData());
    if( ! trfolder.exists() ) {
			trfolder.setPath( qConfigPath  );
    }
#endif
    return trfolder;

}


QDir setThemePath( QString path )
{
	QDir tmpfolder;

	tmpfolder.setPath( path + QDir::separator() + THEME );

	return tmpfolder;

}