121 delete engine; |
121 delete engine; |
122 engine = NULL; |
122 engine = NULL; |
123 } |
123 } |
124 } |
124 } |
125 |
125 |
|
126 QString getUsage() |
|
127 { |
|
128 return QString( |
|
129 "%1: hedgewars [%2...]\n" |
|
130 "\n" |
|
131 "%3:\n" |
|
132 " --help %4\n" |
|
133 " --config-dir=PATH %5\n" |
|
134 " --data-dir=PATH %6\n" |
|
135 "\n" |
|
136 ).arg(HWApplication::tr("Usage", "command-line")) |
|
137 .arg(HWApplication::tr("OPTION", "command-line")) |
|
138 .arg(HWApplication::tr("Options", "command-line")) |
|
139 .arg(HWApplication::tr("Display this help", "command-line")) |
|
140 .arg(HWApplication::tr("Custom path for configuration data and user data", "command-line")) |
|
141 .arg(HWApplication::tr("Custom path to the game data folder", "command-line")); |
|
142 } |
|
143 |
126 int main(int argc, char *argv[]) |
144 int main(int argc, char *argv[]) |
127 { |
145 { |
128 // Since we're calling this first, closeResources() will be the last thing called after main() returns. |
146 // Since we're calling this first, closeResources() will be the last thing called after main() returns. |
129 atexit(closeResources); |
147 atexit(closeResources); |
130 |
148 |
131 #ifdef __APPLE__ |
149 #ifdef __APPLE__ |
132 cocoaInit = new CocoaInitializer(); // Creates the autoreleasepool preventing cocoa object leaks on OS X. |
150 cocoaInit = new CocoaInitializer(); // Creates the autoreleasepool preventing cocoa object leaks on OS X. |
133 #endif |
151 #endif |
134 |
152 |
135 HWApplication app(argc, argv); |
153 HWApplication app(argc, argv); |
136 |
154 app.setAttribute(Qt::AA_DontShowIconsInMenus,false); |
|
155 |
|
156 // file engine and splash. to be initialized later |
|
157 engine = NULL; |
137 QLabel *splash = NULL; |
158 QLabel *splash = NULL; |
|
159 |
|
160 // parse arguments |
|
161 |
|
162 QStringList arguments = app.arguments(); |
|
163 QMap<QString, QString> parsedArgs; |
|
164 { |
|
165 QList<QString>::iterator i = arguments.begin(); |
|
166 while(i != arguments.end()) |
|
167 { |
|
168 QString arg = *i; |
|
169 |
|
170 |
|
171 QRegExp opt("--(\\S+)=(.+)"); |
|
172 if(opt.exactMatch(arg)) |
|
173 { |
|
174 parsedArgs[opt.cap(1)] = opt.cap(2); |
|
175 i = arguments.erase(i); |
|
176 } |
|
177 else |
|
178 { |
|
179 if(arg.startsWith("--")) { |
|
180 if(arg == "--help") |
|
181 { |
|
182 printf("%s", getUsage().toUtf8().constData()); |
|
183 return 0; |
|
184 } |
|
185 // argument is something wrong |
|
186 fprintf(stderr, "%s\n\n%s", |
|
187 HWApplication::tr("Malformed option argument: %1", "command-line").arg(arg).toUtf8().constData(), |
|
188 getUsage().toUtf8().constData()); |
|
189 return 1; |
|
190 } |
|
191 |
|
192 // if not starting with --, then always skip |
|
193 // (because we can't determine if executable path/call or not - on windows) |
|
194 ++i; |
|
195 } |
|
196 } |
|
197 } |
|
198 |
|
199 if(parsedArgs.contains("data-dir")) |
|
200 { |
|
201 QFileInfo f(parsedArgs["data-dir"]); |
|
202 parsedArgs.remove("data-dir"); |
|
203 if(!f.exists()) |
|
204 { |
|
205 qWarning() << "WARNING: Cannot open DATA_PATH=" << f.absoluteFilePath(); |
|
206 } |
|
207 *cDataDir = f.absoluteFilePath(); |
|
208 custom_data = true; |
|
209 } |
|
210 |
|
211 if(parsedArgs.contains("config-dir")) |
|
212 { |
|
213 QFileInfo f(parsedArgs["config-dir"]); |
|
214 parsedArgs.remove("config-dir"); |
|
215 cfgdir->setPath(f.absoluteFilePath()); |
|
216 custom_config = true; |
|
217 } |
|
218 else |
|
219 { |
|
220 cfgdir->setPath(QDir::homePath()); |
|
221 custom_config = false; |
|
222 } |
|
223 |
|
224 if (!parsedArgs.isEmpty()) { |
|
225 foreach (const QString & key, parsedArgs.keys()) |
|
226 { |
|
227 fprintf(stderr, "%s\n", HWApplication::tr("Unknown option argument: %1", "command-line").arg(QString("--") + key).toUtf8().constData()); |
|
228 } |
|
229 fprintf(stderr, "\n%s", getUsage().toUtf8().constData()); |
|
230 return 1; |
|
231 } |
|
232 |
|
233 // end of parameter parsing |
|
234 |
138 #if defined Q_OS_WIN |
235 #if defined Q_OS_WIN |
139 QPixmap pixmap(":res/splash.png"); |
236 QPixmap pixmap(":res/splash.png"); |
140 splash = new QLabel(0, Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint); |
237 splash = new QLabel(0, Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint); |
141 splash->setAttribute(Qt::WA_TranslucentBackground); |
238 splash->setAttribute(Qt::WA_TranslucentBackground); |
142 const QRect deskSize = HWApplication::desktop()->screenGeometry(-1); |
239 const QRect deskSize = HWApplication::desktop()->screenGeometry(-1); |
144 (deskSize.height() - pixmap.height())/2 ); |
241 (deskSize.height() - pixmap.height())/2 ); |
145 splash->move(splashCenter); |
242 splash->move(splashCenter); |
146 splash->setPixmap(pixmap); |
243 splash->setPixmap(pixmap); |
147 splash->show(); |
244 splash->show(); |
148 #endif |
245 #endif |
149 |
|
150 engine = new FileEngineHandler(argv[0]); |
|
151 |
|
152 app.setAttribute(Qt::AA_DontShowIconsInMenus,false); |
|
153 |
|
154 QStringList arguments = app.arguments(); |
|
155 QMap<QString, QString> parsedArgs; |
|
156 { |
|
157 QList<QString>::iterator i = arguments.begin(); |
|
158 while(i != arguments.end()) |
|
159 { |
|
160 QString arg = *i; |
|
161 |
|
162 QRegExp opt("--(\\S+)=(.+)"); |
|
163 if(opt.exactMatch(arg)) |
|
164 { |
|
165 parsedArgs[opt.cap(1)] = opt.cap(2); |
|
166 i = arguments.erase(i); |
|
167 } |
|
168 else |
|
169 { |
|
170 ++i; |
|
171 } |
|
172 } |
|
173 } |
|
174 |
|
175 if(parsedArgs.contains("data-dir")) |
|
176 { |
|
177 QFileInfo f(parsedArgs["data-dir"]); |
|
178 if(!f.exists()) |
|
179 { |
|
180 qWarning() << "WARNING: Cannot open DATA_PATH=" << f.absoluteFilePath(); |
|
181 } |
|
182 *cDataDir = f.absoluteFilePath(); |
|
183 custom_data = true; |
|
184 } |
|
185 |
|
186 if(parsedArgs.contains("config-dir")) |
|
187 { |
|
188 QFileInfo f(parsedArgs["config-dir"]); |
|
189 cfgdir->setPath(f.absoluteFilePath()); |
|
190 custom_config = true; |
|
191 } |
|
192 else |
|
193 { |
|
194 cfgdir->setPath(QDir::homePath()); |
|
195 custom_config = false; |
|
196 } |
|
197 |
|
198 app.setStyle(new QPlastiqueStyle()); |
246 app.setStyle(new QPlastiqueStyle()); |
199 |
247 |
200 QDateTime now = QDateTime::currentDateTime(); |
248 QDateTime now = QDateTime::currentDateTime(); |
201 srand(now.toTime_t()); |
249 srand(now.toTime_t()); |
202 rand(); |
250 rand(); |