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(const GameFile &other)
45 {
46  this->d = other.d;
47 }
48 
49 GameFile::~GameFile()
50 {
51 }
52 
53 GameFile &GameFile::operator=(const GameFile &other)
54 {
55  if (this != &other)
56  this->d = other.d;
57  return *this;
58 }
59 
60 const QString &GameFile::configName() const
61 {
62  return d->configName;
63 }
64 
65 GameFile &GameFile::setConfigName(const QString &name)
66 {
67  d->configName = name;
68  return *this;
69 }
70 
71 const QString &GameFile::fileName() const
72 {
73  return d->fileName;
74 }
75 
76 GameFile &GameFile::setFileName(const QString &name)
77 {
78  d->fileName = name;
79  return *this;
80 }
81 
83 {
84  return d->executable;
85 }
86 
87 GameFile &GameFile::setExecutable(int flags)
88 {
89  d->executable = flags;
90  return *this;
91 }
92 
93 bool GameFile::isSameFile(const QString &otherFileName)
94 {
95  QString thisFile = fileName();
96  #if defined(Q_OS_WIN32)
97  if (executable() != 0)
98  {
99  if (!thisFile.endsWith(".exe"))
100  {
101  thisFile += ".exe";
102  }
103  }
104  #endif
105  return thisFile.compare(otherFileName, FileUtils::comparisonSensitivity()) == 0;
106 }
107 
108 bool GameFile::isValid() const
109 {
110  return !configName().isEmpty();
111 }
112 
113 const QString &GameFile::niceName() const
114 {
115  return d->niceName;
116 }
117 
118 GameFile &GameFile::setNiceName(const QString &name)
119 {
120  d->niceName = name;
121  return *this;
122 }
123 
124 QStringList &GameFile::searchSuffixes() const
125 {
126  return d->searchSuffixes;
127 }
128 
129 GameFile &GameFile::setSearchSuffixes(const QStringList &suffixes)
130 {
131  d->searchSuffixes = suffixes;
132  return *this;
133 }
134 
136 
137 DClass<GameFileList>
138 {
139 public:
140  QList<GameFile> list;
141 };
142 
143 DPointered(GameFileList)
144 
145 
147 {
148 }
149 
150 GameFileList::~GameFileList()
151 {
152 }
153 
154 GameFileList &GameFileList::append(const GameFile &gameFile)
155 {
156  d->list << gameFile;
157  return *this;
158 }
159 
160 GameFileList &GameFileList::append(const GameFileList &list)
161 {
162  for (const GameFile &other : list.asQList())
163  {
164  append(other);
165  }
166  return *this;
167 }
168 
169 QList<GameFile> GameFileList::asQList() const
170 {
171  return d->list;
172 }
173 
174 void GameFileList::clear()
175 {
176  d->list.clear();
177 }
178 
179 GameFile GameFileList::first() const
180 {
181  for (const GameFile &file : d->list)
182  {
183  if (file.isValid())
184  {
185  return file;
186  }
187  }
188  return GameFile();
189 }
190 
191 GameFile GameFileList::findByConfigName(const QString &configName)
192 {
193  for (const GameFile &file : d->list)
194  {
195  if (file.configName() == configName)
196  {
197  return file;
198  }
199  }
200  return GameFile();
201 }
202 
203 bool GameFileList::isEmpty() const
204 {
205  return d->list.isEmpty();
206 }
207 
208 GameFileList &GameFileList::prepend(const GameFile &gameFile)
209 {
210  d->list.prepend(gameFile);
211  return *this;
212 }
213 
214 GameFileList &operator<<(GameFileList &list, const GameFile &gameFile)
215 {
216  list.append(gameFile);
217  return list;
218 }
219 
220 GameFileList &operator<<(GameFileList &list, const GameFileList &other)
221 {
222  list.append(other);
223  return list;
224 }
225 
227 
228 GameFileList GameFiles::allCreateGameExecutables(const GameFileList &list)
229 {
230  return allFlagMatchExecutables(list, GameFile::CreateGame);
231 }
232 
233 GameFileList GameFiles::allClientExecutables(const GameFileList &list)
234 {
235  return allFlagMatchExecutables(list, GameFile::Client);
236 }
237 
238 GameFileList GameFiles::allServerExecutables(const GameFileList &list)
239 {
240  return allFlagMatchExecutables(list, GameFile::Server);
241 }
242 
243 GameFileList GameFiles::allFlagMatchExecutables(const GameFileList &list, int execs)
244 {
245  GameFileList result;
246  for (const GameFile &file : list.asQList())
247  {
248  if (file.isValid() && (execs & file.executable()))
249  {
250  result << file;
251  }
252  }
253  return result;
254 }
255 
256 GameFile GameFiles::defaultClientExecutable(const GameFileList &list)
257 {
258  for (const GameFile &file : list.asQList())
259  {
260  if (file.isValid() && (file.executable() & GameFile::Client)
261  && file.configName() == "BinaryPath")
262  {
263  return file;
264  }
265  }
266  return GameFile();
267 }
268 
269 GameFile GameFiles::defaultOfflineExecutable(const GameFileList &list)
270 {
271  for (const GameFile &file : list.asQList())
272  {
273  if (file.isValid() && (file.executable() & GameFile::Offline)
274  && file.configName() == "BinaryPath")
275  {
276  return file;
277  }
278  }
279  return GameFile();
280 }
281 
282 GameFile GameFiles::defaultServerExecutable(const GameFileList &list)
283 {
284  for (const GameFile &file : list.asQList())
285  {
286  if (file.isValid() && (file.executable() & GameFile::Server)
287  && (file.configName() == "ServerBinaryPath" || file.configName() == "BinaryPath"))
288  {
289  return file;
290  }
291  }
292  return GameFile();
293 }
294 
295 GameFile GameFiles::preferredOfflineExecutable(const GameFileList &list)
296 {
297  GameFile candidate = defaultOfflineExecutable(list);
298  if (candidate.isValid())
299  {
300  return candidate;
301  }
302  for (const GameFile &file : list.asQList())
303  {
304  if (file.isValid() && (file.executable() & GameFile::Offline))
305  {
306  return file;
307  }
308  }
309  return GameFile();
310 }