gamedemo.cpp
1 //------------------------------------------------------------------------------
2 // gamedemo.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) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "gamedemo.h"
24 
25 #include "datapaths.h"
26 #include "ini/ini.h"
27 #include "ini/settingsproviderqt.h"
28 #include "plugins/engineplugin.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  demoName += QString(".%1").arg(plugin.data()->demoExtension);
68  return demoName;
69 }
70 
71 void GameDemo::saveDemoMetaData(const QString &demoName, const EnginePlugin &plugin,
72  const QString &iwad, const QList<PWad> &pwads)
73 {
74  QString metaFileName;
75  // If the extension is automatic we need to add it here
76  if (plugin.data()->demoExtensionAutomatic)
77  {
78  metaFileName = QString("%1.%2.ini").arg(demoName)
79  .arg(plugin.data()->demoExtension);
80  }
81  else
82  metaFileName = demoName + ".ini";
83 
84  QSettings settings(metaFileName, QSettings::IniFormat);
85  SettingsProviderQt settingsProvider(&settings);
86  Ini metaFile(&settingsProvider);
87  IniSection metaSection = metaFile.section("meta");
88 
89  QStringList requiredPwads;
90  QStringList optionalPwads;
91 
92  for (const PWad &wad : pwads)
93  {
94  if (wad.isOptional())
95  optionalPwads << wad.name();
96  else
97  requiredPwads << wad.name();
98  }
99 
100  metaSection.createSetting("iwad", iwad.toLower());
101  metaSection.createSetting("pwads", requiredPwads.join(";"));
102  metaSection.createSetting("optionalPwads", optionalPwads);
103 }
104 
105 GameDemo::operator GameDemo::Control() const
106 {
107  return d.control;
108 }