gamedemo.cpp
1 //------------------------------------------------------------------------------
2 // gamedemo.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "gamedemo.h"
24 
25 #include "ini/ini.h"
26 #include "ini/settingsproviderqt.h"
27 #include "plugins/engineplugin.h"
29 #include "datapaths.h"
30 #include <cassert>
31 #include <QDateTime>
32 #include <QDir>
33 
34 GameDemo::GameDemo()
35 {
36  d.control = NoDemo;
37 }
38 
39 GameDemo::GameDemo(Control control)
40 {
41  d.control = control;
42 }
43 
44 QString GameDemo::mkDemoFullPath(Control control, const EnginePlugin &plugin)
45 {
46  switch (control)
47  {
48  case Managed:
49  return gDefaultDataPaths->demosDirectoryPath() + QDir::separator() + mkDemoName(plugin);
50  case Unmanaged:
51  return mkDemoName(plugin);
52  case NoDemo:
53  return QString();
54  default:
55  assert(0 && "Unknown demo control type");
56  return QString();
57  }
58 }
59 
60 QString GameDemo::mkDemoName(const EnginePlugin &plugin)
61 {
62  QString demoName = "";
63  demoName += QString("%1_%2").
64  arg(plugin.data()->name).
65  arg(QDateTime::currentDateTime().toString("dd.MM.yyyy_hh.mm.ss"));
66  if (!plugin.data()->demoExtensionAutomatic)
67  {
68  demoName += QString(".%1").arg(plugin.data()->demoExtension);
69  }
70  return demoName;
71 }
72 
73 void GameDemo::saveDemoMetaData(const QString &demoName, const EnginePlugin &plugin,
74  const QString &iwad, const QList<PWad> &pwads)
75 {
76  QString metaFileName;
77  // If the extension is automatic we need to add it here
78  if (plugin.data()->demoExtensionAutomatic)
79  {
80  metaFileName = QString("%1.%2.ini").arg(demoName)
81  .arg(plugin.data()->demoExtension);
82  }
83  else
84  {
85  metaFileName = demoName + ".ini";
86  }
87 
88  QSettings settings(metaFileName, QSettings::IniFormat);
89  SettingsProviderQt settingsProvider(&settings);
90  Ini metaFile(&settingsProvider);
91  IniSection metaSection = metaFile.section("meta");
92 
93  QStringList requiredPwads;
94  QStringList optionalPwads;
95 
96  foreach (const PWad &wad, pwads)
97  {
98  if (wad.isOptional())
99  {
100  optionalPwads << wad.name();
101  }
102  else
103  {
104  requiredPwads << wad.name();
105  }
106  }
107 
108  metaSection.createSetting("iwad", iwad.toLower());
109  metaSection.createSetting("pwads", requiredPwads.join(";"));
110  metaSection.createSetting("optionalPwads", optionalPwads);
111 }
112 
113 GameDemo::operator GameDemo::Control() const
114 {
115  return d.control;
116 }
IniVariable createSetting(const QString &name, const QVariant &data)
Inits specified variable with specified data.
Definition: inisection.cpp:57
PWAD hosted on a server.
Configuration handler.
Definition: ini.h:69
INI section representation.
Definition: inisection.h:40
bool isOptional() const
Is this WAD required to join the server?
const QString & name() const
File name of the WAD.