gamehost.cpp
1 //------------------------------------------------------------------------------
2 // gamehost.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2013 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "gamehost.h"
24 
25 #include "apprunner.h"
26 #include "commandlinetokenizer.h"
27 #include "configuration/doomseekerconfig.h"
28 #include "gamedemo.h"
29 #include "ini/inisection.h"
30 #include "ini/inivariable.h"
31 #include "plugins/engineplugin.h"
32 #include "serverapi/gamecreateparams.h"
33 #include "serverapi/message.h"
35 #include <cassert>
36 #include <QFileInfo>
37 #include <QStringList>
38 
39 #define BAIL_ON_ERROR(method) \
40  { \
41  method; \
42  if (d->message.isError()) \
43  { \
44  return; \
45  } \
46  }
47 
48 DClass<GameHost>
49 {
50 public:
51  QString argBexLoading;
52  QString argDehLoading;
53  QString argIwadLoading;
54  QString argOptionalWadLoading;
55  QString argPort;
56  QString argPwadLoading;
57  QString argDemoPlayback;
58  QString argDemoRecord;
59  QString argServerLaunch;
60 
61  CommandLineInfo *currentCmdLine;
62  Message message;
63  GameCreateParams params;
64  EnginePlugin *plugin;
65 
66  void (GameHost::*addIwad)();
67  void (GameHost::*addPwads)();
68  void (GameHost::*addDMFlags)();
69  void (GameHost::*addGlobalGameCustomParameters)();
70 };
71 
72 DPointered(GameHost)
73 
75 {
76  d->argBexLoading = "-deh";
77  d->argDehLoading = "-deh";
78  d->argIwadLoading = "-iwad";
79  d->argOptionalWadLoading = "-file";
80  d->argPort = "-port";
81  d->argPwadLoading = "-file";
82  d->argDemoPlayback = "-playdemo";
83  d->argDemoRecord = "-record";
84  d->currentCmdLine = nullptr;
85  d->plugin = plugin;
86 
87  set_addIwad(&GameHost::addIwad_default);
88  set_addPwads(&GameHost::addPwads_default);
89  set_addDMFlags(&GameHost::addDMFlags_default);
90  set_addGlobalGameCustomParameters(&GameHost::addGlobalGameCustomParameters_default);
91 }
92 
93 GameHost::~GameHost()
94 {
95 }
96 
97 POLYMORPHIC_DEFINE(void, GameHost, addIwad, (), ())
98 POLYMORPHIC_DEFINE(void, GameHost, addPwads, (), ())
99 POLYMORPHIC_DEFINE(void, GameHost, addDMFlags, (), ())
100 POLYMORPHIC_DEFINE(void, GameHost, addGlobalGameCustomParameters, (), ())
101 
102 void GameHost::addCustomParameters()
103 {
104  args().append(params().customParameters());
105 }
106 
107 void GameHost::addDemoPlaybackIfApplicable()
108 {
109  if (params().hostMode() == GameCreateParams::Demo)
110  {
111  args() << argForDemoPlayback();
112  args() << params().demoPath();
113  }
114 }
115 
116 void GameHost::addDemoRecordIfApplicable()
117 {
118  if (params().hostMode() == GameCreateParams::Offline
119  && params().demoRecord() != GameDemo::NoDemo)
120  {
121  args() << argForDemoRecord();
122  args() << params().demoPath();
123  }
124 }
125 
126 void GameHost::addDMFlags_default()
127 {
128 }
129 
131 {
132 }
133 
134 void GameHost::addGlobalGameCustomParameters_default()
135 {
136  IniSection config = gConfig.iniSectionForPlugin(plugin());
137  QString customParameters = config["CustomParameters"];
138  CommandLineTokenizer tokenizer;
139  args() << tokenizer.tokenize(customParameters);
140 }
141 
142 void GameHost::addIwad_default()
143 {
144  const QString &iwadPath = params().iwadPath();
145 
146  if (iwadPath.isEmpty())
147  {
148  setMessage(Message::customError(tr("IWAD is not set")));
149  return;
150  }
151 
152  QFileInfo fi(iwadPath);
153 
154  if (!fi.isFile())
155  {
156  QString error = tr("IWAD Path error:\n\"%1\" doesn't exist or is a directory!").arg(iwadPath);
158  }
159 
160  args() << argForIwadLoading() << iwadPath;
161 }
162 
163 void GameHost::addPwads_default()
164 {
165  verifyPwadPaths();
166  for (int i = 0; i < params().pwadsPaths().size(); ++i)
167  {
168  const QString &pwad = params().pwadsPaths()[i];
169  args() << fileLoadingPrefix(i) << pwad;
170  }
171 }
172 
174 {
175  verifyPwadPaths();
176  QMap<QString, QStringList> groups;
177  for (int i = 0; i < params().pwadsPaths().size(); ++i)
178  {
179  const QString &pwad = params().pwadsPaths()[i];
180  QString prefix = fileLoadingPrefix(i);
181  groups[prefix] << pwad;
182  }
183  for (const QString &prefix : groups.keys())
184  {
185  args() << prefix;
186  for (const QString &file : groups[prefix])
187  {
188  args() << file;
189  }
190  }
191 }
192 
193 QString GameHost::fileLoadingPrefix(int index) const
194 {
195  const QString &pwad = params().pwadsPaths()[index];
196  bool optional = false;
197  if (params().pwadsOptional().size() > index)
198  {
199  optional = params().pwadsOptional()[index];
200  }
201 
202  if (pwad.toLower().endsWith(".deh"))
203  {
204  return argForDehLoading();
205  }
206  else if (pwad.toLower().endsWith(".bex"))
207  {
208  return argForBexLoading();
209  }
210 
211  if (optional)
212  return argForOptionalWadLoading();
213  return argForPwadLoading();
214 }
215 
216 const QString &GameHost::argForBexLoading() const
217 {
218  return d->argBexLoading;
219 }
220 
221 const QString &GameHost::argForDehLoading() const
222 {
223  return d->argDehLoading;
224 }
225 
226 const QString &GameHost::argForIwadLoading() const
227 {
228  return d->argIwadLoading;
229 }
230 
232 {
233  return d->argOptionalWadLoading;
234 }
235 
236 const QString &GameHost::argForPort() const
237 {
238  return d->argPort;
239 }
240 
241 const QString &GameHost::argForPwadLoading() const
242 {
243  return d->argPwadLoading;
244 }
245 
246 const QString &GameHost::argForDemoPlayback() const
247 {
248  return d->argDemoPlayback;
249 }
250 
251 const QString &GameHost::argForDemoRecord() const
252 {
253  return d->argDemoRecord;
254 }
255 
256 const QString &GameHost::argForServerLaunch() const
257 {
258  return d->argServerLaunch;
259 }
260 
261 QStringList &GameHost::args()
262 {
263  return d->currentCmdLine->args;
264 }
265 
267 {
268  BAIL_ON_ERROR(addGlobalGameCustomParameters());
269  BAIL_ON_ERROR(addIwad());
270  BAIL_ON_ERROR(addPwads());
271 
272  // Port
273  if (params().hostMode() == GameCreateParams::Host && params().port() > 0)
274  {
275  args() << argForPort() << QString::number(params().port());
276  }
277 
278  // CVars
279  const QList<GameCVar> &cvars = params().cvars();
280  for (const GameCVar &c : cvars)
281  {
282  if (c.value().type() == QVariant::Bool)
283  {
284  // Games like their bools passed in as 0/1 numbers.
285  args() << c.command() << QString::number(c.valueInt());
286  }
287  else
288  {
289  args() << c.command() << c.valueString();
290  }
291  }
292 
293  if (params().hostMode() == GameCreateParams::Host)
294  {
295  // Some games may not offer such argument.
296  if (!argForServerLaunch().isEmpty())
297  {
298  args() << argForServerLaunch();
299  }
300  }
301 
302  BAIL_ON_ERROR(addDMFlags());
303  BAIL_ON_ERROR(addExtra());
304  BAIL_ON_ERROR(addCustomParameters());
305 
306  addDemoPlaybackIfApplicable();
307  addDemoRecordIfApplicable();
308  saveDemoMetaData();
309 }
310 
312 {
313  d->message = Message();
314  d->currentCmdLine = &cmdLine;
315  d->params = params;
316 
317  args().clear();
318 
319  setupGamePaths();
320  if (d->message.isError())
321  {
322  return d->message;
323  }
324 
326  return d->message;
327 }
328 
330 {
331  CommandLineInfo cmdLine;
332 
333  Message message = createHostCommandLine(params, cmdLine);
334  if (!message.isIgnore())
335  {
336  return message;
337  }
338 
339  #ifdef Q_OS_WIN32
340  const bool WRAP_IN_SSS_CONSOLE = false;
341  #else
342  const bool WRAP_IN_SSS_CONSOLE = params.hostMode() == GameCreateParams::Host;
343  #endif
344 
345  if (WRAP_IN_SSS_CONSOLE)
346  {
347  QIcon icon;
348  if (plugin() != nullptr)
349  {
350  icon = plugin()->icon();
351  }
353  return Message();
354  }
355  else
356  {
357  return AppRunner::runExecutable(cmdLine);
358  }
359 }
360 
362 {
363  return d->params;
364 }
365 
367 {
368  return d->plugin;
369 }
370 
371 void GameHost::saveDemoMetaData()
372 {
373  if (params().demoRecord() == GameDemo::Managed)
374  {
375  GameDemo::saveDemoMetaData(params().demoPath(), *plugin(),
376  params().iwadName(), params().pwads());
377  }
378 }
379 
380 void GameHost::setArgForBexLoading(const QString &arg)
381 {
382  d->argBexLoading = arg;
383 }
384 
385 void GameHost::setArgForDehLoading(const QString &arg)
386 {
387  d->argDehLoading = arg;
388 }
389 
390 void GameHost::setArgForIwadLoading(const QString &arg)
391 {
392  d->argIwadLoading = arg;
393 }
394 
395 void GameHost::setArgForOptionalWadLoading(const QString &arg)
396 {
397  d->argOptionalWadLoading = arg;
398 }
399 
400 void GameHost::setArgForPort(const QString &arg)
401 {
402  d->argPort = arg;
403 }
404 
405 void GameHost::setArgForPwadLoading(const QString &arg)
406 {
407  d->argPwadLoading = arg;
408 }
409 
410 void GameHost::setArgForDemoPlayback(const QString &arg)
411 {
412  d->argDemoPlayback = arg;
413 }
414 
415 void GameHost::setArgForDemoRecord(const QString &arg)
416 {
417  d->argDemoRecord = arg;
418 }
419 
420 void GameHost::setArgForServerLaunch(const QString &arg)
421 {
422  d->argServerLaunch = arg;
423 }
424 
425 void GameHost::setMessage(const Message &message)
426 {
427  d->message = message;
428 }
429 
430 void GameHost::setupGamePaths()
431 {
432  QFileInfo fileInfo(params().executablePath());
433  if (!fileInfo.isFile() && !fileInfo.isBundle())
434  {
435  QString error = tr("%1\ndoesn't exist or is not a file.").arg(fileInfo.filePath());
437  return;
438  }
439  d->currentCmdLine->executable = QFileInfo(params().executablePath());
440  d->currentCmdLine->applicationDir = fileInfo.dir();
441 }
442 
444 {
445  for (const QString &pwad : params().pwadsPaths())
446  {
447  QFileInfo fi(pwad);
448  if (!fi.isFile())
449  {
450  QString error = tr("PWAD path error:\n\"%1\" doesn't exist or is a directory!").arg(pwad);
452  return false;
453  }
454  }
455  return true;
456 }