gamefile.cpp
1 //------------------------------------------------------------------------------
2 // gamefile.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) 2015 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "gamefile.h"
24 
25 #include "fileutils.h"
26 
27 DClass<GameFile>
28 {
29 public:
30  QString configName;
31  int executable;
32  QString fileName;
33  QString niceName;
34  QStringList searchSuffixes;
35 };
36 
37 DPointered(GameFile)
38 
40 {
41  d->executable = 0;
42 }
43 
44 GameFile::~GameFile()
45 {
46 }
47 
48 const QString &GameFile::configName() const
49 {
50  return d->configName;
51 }
52 
53 GameFile &GameFile::setConfigName(const QString &name)
54 {
55  d->configName = name;
56  return *this;
57 }
58 
59 const QString &GameFile::fileName() const
60 {
61  return d->fileName;
62 }
63 
64 GameFile &GameFile::setFileName(const QString &name)
65 {
66  d->fileName = name;
67  return *this;
68 }
69 
71 {
72  return d->executable;
73 }
74 
75 GameFile &GameFile::setExecutable(int flags)
76 {
77  d->executable = flags;
78  return *this;
79 }
80 
81 bool GameFile::isSameFile(const QString &otherFileName)
82 {
83  QString thisFile = fileName();
84 #if defined(Q_OS_WIN32)
85  if (executable() != 0)
86  {
87  if (!thisFile.endsWith(".exe"))
88  {
89  thisFile += ".exe";
90  }
91  }
92 #endif
93  return thisFile.compare(otherFileName, FileUtils::comparisonSensitivity()) == 0;
94 }
95 
96 bool GameFile::isValid() const
97 {
98  return !configName().isEmpty();
99 }
100 
101 const QString &GameFile::niceName() const
102 {
103  return d->niceName;
104 }
105 
106 GameFile &GameFile::setNiceName(const QString &name)
107 {
108  d->niceName = name;
109  return *this;
110 }
111 
112 QStringList &GameFile::searchSuffixes() const
113 {
114  return d->searchSuffixes;
115 }
116 
117 GameFile &GameFile::setSearchSuffixes(const QStringList &suffixes)
118 {
119  d->searchSuffixes = suffixes;
120  return *this;
121 }
122 
124 
125 DClass<GameFileList>
126 {
127 public:
128  QList<GameFile> list;
129 };
130 
131 DPointered(GameFileList)
132 
133 
134 GameFileList::GameFileList()
135 {
136 }
137 
138 GameFileList::~GameFileList()
139 {
140 }
141 
142 GameFileList &GameFileList::append(const GameFile &gameFile)
143 {
144  d->list << gameFile;
145  return *this;
146 }
147 
148 GameFileList &GameFileList::append(const GameFileList &list)
149 {
150  foreach (const GameFile &other, list.asQList())
151  {
152  append(other);
153  }
154  return *this;
155 }
156 
157 QList<GameFile> GameFileList::asQList() const
158 {
159  return d->list;
160 }
161 
162 void GameFileList::clear()
163 {
164  d->list.clear();
165 }
166 
167 GameFile GameFileList::first() const
168 {
169  foreach (const GameFile &file, d->list)
170  {
171  if (file.isValid())
172  {
173  return file;
174  }
175  }
176  return GameFile();
177 }
178 
179 GameFile GameFileList::findByConfigName(const QString &configName)
180 {
181  foreach (const GameFile &file, d->list)
182  {
183  if (file.configName() == configName)
184  {
185  return file;
186  }
187  }
188  return GameFile();
189 }
190 
191 bool GameFileList::isEmpty() const
192 {
193  return d->list.isEmpty();
194 }
195 
196 GameFileList &GameFileList::prepend(const GameFile &gameFile)
197 {
198  d->list.prepend(gameFile);
199  return *this;
200 }
201 
202 GameFileList& operator<<(GameFileList &list, const GameFile &gameFile)
203 {
204  list.append(gameFile);
205  return list;
206 }
207 
208 GameFileList& operator<<(GameFileList &list, const GameFileList &other)
209 {
210  list.append(other);
211  return list;
212 }
213 
215 
216 GameFileList GameFiles::allCreateGameExecutables(const GameFileList &list)
217 {
218  return allFlagMatchExecutables(list, GameFile::CreateGame);
219 }
220 
221 GameFileList GameFiles::allClientExecutables(const GameFileList &list)
222 {
223  return allFlagMatchExecutables(list, GameFile::Client);
224 }
225 
226 GameFileList GameFiles::allServerExecutables(const GameFileList &list)
227 {
228  return allFlagMatchExecutables(list, GameFile::Server);
229 }
230 
231 GameFileList GameFiles::allFlagMatchExecutables(const GameFileList &list, int execs)
232 {
233  GameFileList result;
234  foreach (const GameFile &file, list.asQList())
235  {
236  if (file.isValid() && (execs & file.executable()))
237  {
238  result << file;
239  }
240  }
241  return result;
242 }
243 
244 GameFile GameFiles::defaultClientExecutable(const GameFileList &list)
245 {
246  foreach (const GameFile &file, list.asQList())
247  {
248  if (file.isValid() && (file.executable() & GameFile::Client)
249  && file.configName() == "BinaryPath")
250  {
251  return file;
252  }
253  }
254  return GameFile();
255 }
256 
257 GameFile GameFiles::defaultOfflineExecutable(const GameFileList &list)
258 {
259  foreach (const GameFile &file, list.asQList())
260  {
261  if (file.isValid() && (file.executable() & GameFile::Offline)
262  && file.configName() == "BinaryPath")
263  {
264  return file;
265  }
266  }
267  return GameFile();
268 }
269 
270 GameFile GameFiles::defaultServerExecutable(const GameFileList &list)
271 {
272  foreach (const GameFile &file, list.asQList())
273  {
274  if (file.isValid() && (file.executable() & GameFile::Server)
275  && (file.configName() == "ServerBinaryPath" || file.configName() == "BinaryPath"))
276  {
277  return file;
278  }
279  }
280  return GameFile();
281 }
282 
283 GameFile GameFiles::preferredOfflineExecutable(const GameFileList &list)
284 {
285  GameFile candidate = defaultOfflineExecutable(list);
286  if (candidate.isValid())
287  {
288  return candidate;
289  }
290  foreach (const GameFile &file, list.asQList())
291  {
292  if (file.isValid() && (file.executable() & GameFile::Offline))
293  {
294  return file;
295  }
296  }
297  return GameFile();
298 }
GameFile collection.
Definition: gamefile.h:145
const QString & configName() const
Setting name where path will be stored in plugin&#39;s IniSection.
Definition: gamefile.cpp:48
const QString & niceName() const
Descriptive name, ie. "client executable", "server executable", etc.
Definition: gamefile.cpp:101
int executable() const
Executable bit flags mod that compares to ExecType.
Definition: gamefile.cpp:70
bool isSameFile(const QString &otherFileName)
Guesses by file name if this is the same file.
Definition: gamefile.cpp:81
QStringList & searchSuffixes() const
Path suffixes that help in automatically finding this file.
Definition: gamefile.cpp:112
Game file definition allows to browse this file in configuration box.
Definition: gamefile.h:72
const QString & fileName() const
Name of the file on disk.
Definition: gamefile.cpp:59
bool isValid() const
A valid file has configName().
Definition: gamefile.cpp:96