-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathgame.cpp
More file actions
327 lines (254 loc) · 8.68 KB
/
game.cpp
File metadata and controls
327 lines (254 loc) · 8.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "game.h"
#include "settingsmanager.h"
#include <QHash>
Game::Game(const QString& path)
{
this->fs = new ExternalFilesystem(path);
this->path = path;
}
Game::~Game()
{
delete fs;
}
Tileset* Game::getTileset(QString name)
{
QString path = name;
path.prepend("/Unit/");
if (!path.endsWith(".sarc"))
path.append(".sarc");
if (!fs->fileExists(path))
throw std::runtime_error("Tileset File does not exist.");
return new Tileset(this, name);
}
LevelManager* Game::getLevelManager(WindowBase* parent, QString path)
{
path.prepend("/Course/");
if (!path.endsWith(".sarc"))
path.append(".sarc");
if (!fs->fileExists(path))
throw std::runtime_error("Level File does not exist.");
return new LevelManager(parent, this, path);
}
QStandardItemModel* Game::getCourseModel()
{
QStandardItemModel* model = new QStandardItemModel();
QList<QString> dirList;
fs->directoryContents("/Course/", QDir::Dirs, dirList);
dirList.push_front("");
QMap<int, QString> levelNames;
QFile ln(SettingsManager::getInstance()->getFilePath("levelnames.txt"));
if (ln.open(QIODevice::ReadOnly))
{
QTextStream in(&ln);
in.setEncoding(QStringConverter::Utf8);
levelNames.clear();
while(!in.atEnd())
{
QStringList parts = in.readLine().split(":");
if (parts.count() == 2)
levelNames.insert(parts[0].toInt(), parts[1]);
}
ln.close();
}
QMap<int, QString> worldNames;
QFile wn(SettingsManager::getInstance()->getFilePath("worldnames.txt"));
if (wn.open(QIODevice::ReadOnly))
{
QTextStream in(&wn);
in.setEncoding(QStringConverter::Utf8);
worldNames.clear();
while(!in.atEnd())
{
QStringList parts = in.readLine().split(":");
if (parts.count() == 2)
worldNames.insert(parts[0].toInt(), parts[1]);
}
wn.close();
}
foreach (QString dir, dirList)
{
// Sub Directories
QList<QString> courseList;
QStandardItem* dirItem = new QStandardItem(dir);
if (dir == "")
{
fs->directoryContents("/Course/", QDir::Files, courseList);
delete dirItem;
if (courseList.isEmpty()) {
continue;
}
dirItem = model->invisibleRootItem();
}
else
{
fs->directoryContents("/Course/" + dir, QDir::Files, courseList);
if (courseList.isEmpty()) {
delete dirItem;
continue;
}
model->invisibleRootItem()->appendRow(dirItem);
}
QStandardItem* worldItemPtr = nullptr;
foreach(QString filename, courseList)
{
QRegularExpression re("^\\d+-\\d+\\.sarc$");
if (!re.match(filename, 0, QRegularExpression::NormalMatch).hasMatch())
continue;
QString worldName = worldNames.value(filename.split(".")[0].split("-")[0].toInt());
QString worldNum = filename.split(".")[0].split("-")[0];
if (worldName.isEmpty())
worldName = worldNum;
// World Categories
if (worldItemPtr == nullptr || worldItemPtr->text() != QString(worldName))
{
QStandardItem* subItem = new QStandardItem(worldName);
worldItemPtr = subItem;
dirItem->appendRow(subItem);
}
// Levels
if (worldItemPtr != nullptr)
{
QString levelName = levelNames.value(filename.split(".")[0].split("-")[1].toInt()).arg(worldName);
if (levelName.isEmpty())
levelName = filename;
QStandardItem* subItem = new QStandardItem(levelName);
if (dir != "")
subItem->setData(dir + "/" + filename);
else
subItem->setData(filename);
worldItemPtr->appendRow(subItem);
}
}
}
model->sort(0, Qt::AscendingOrder);
return model;
}
QStandardItemModel* Game::getTilesetModel()
{
QStandardItemModel* model = new QStandardItemModel();
model->setColumnCount(2);
QStringList headers;
headers << QObject::tr("Tileset") << QObject::tr("Filename");
model->setHorizontalHeaderLabels(headers);
QFile inputFile(SettingsManager::getInstance()->dataPath("tilesetnames.txt"));
if (!inputFile.open(QIODevice::ReadOnly))
return model;
QHash<QString, QString> defaultNames;
QTextStream in(&inputFile);
in.setEncoding(QStringConverter::Utf8);
while (!in.atEnd())
{
QStringList parts = in.readLine().split(':');
if (parts.length() < 2)
break;
defaultNames.insert(parts[0], parts[1]);
}
inputFile.close();
QStandardItem* standardSuite = new QStandardItem(QObject::tr("Standard"));
QStandardItem* stageSuite = new QStandardItem(QObject::tr("Stage"));
QStandardItem* backgroundSuite = new QStandardItem(QObject::tr("Background"));
QStandardItem* interactiveSuite = new QStandardItem(QObject::tr("Interactive"));
model->appendRow(standardSuite);
model->appendRow(stageSuite);
model->appendRow(backgroundSuite);
model->appendRow(interactiveSuite);
model->setItem(0, 1, new QStandardItem());
model->setItem(1, 1, new QStandardItem());
model->setItem(2, 1, new QStandardItem());
model->setItem(3, 1, new QStandardItem());
QList<QString> tilesetfiles;
fs->directoryContents("/Unit", QDir::Files, tilesetfiles);
for (int i = 0; i < tilesetfiles.length(); i++)
{
QString fileName = tilesetfiles[i];
fileName.chop(5);
QString tilesetname;
tilesetname = defaultNames.value(fileName, fileName);
QList<QStandardItem*> items;
QStandardItem* tileset = new QStandardItem(tilesetname);
tileset->setData(fileName);
items.append(tileset);
QStandardItem* fileNameItem = new QStandardItem(fileName);
fileNameItem->setData(fileName);
items.append(fileNameItem);
if (fileName.startsWith("J_"))
{
standardSuite->appendRow(items);
}
else if (fileName.startsWith("M_"))
{
stageSuite->appendRow(items);
}
else if (fileName.startsWith("S1_"))
{
backgroundSuite->appendRow(items);
}
else if (fileName.startsWith("S2_"))
{
interactiveSuite->appendRow(items);
}
}
return model;
}
QStandardItemModel* Game::getTilesetModel(int id, bool includeNoneItem)
{
QStandardItemModel* model = new QStandardItemModel();
model->setColumnCount(2);
QStringList headers;
headers << QObject::tr("Tileset")<< QObject::tr("Filename");
model->setHorizontalHeaderLabels(headers);
QFile inputFile(SettingsManager::getInstance()->getFilePath("tilesetnames.txt"));
if (!inputFile.open(QIODevice::ReadOnly))
return model;
if (includeNoneItem)
{
QList<QStandardItem*> items;
QStandardItem* tileset = new QStandardItem("<none>");
tileset->setData("<none>");
items.append(tileset);
QStandardItem* fileNameItem = new QStandardItem("");
fileNameItem->setData("");
items.append(fileNameItem);
model->appendRow(items);
}
QHash<QString, QString> defaultNames;
QTextStream in(&inputFile);
while (!in.atEnd())
{
QStringList parts = in.readLine().split(':');
if (parts.length() < 2)
break;
defaultNames.insert(parts[0], parts[1]);
}
inputFile.close();
QList<QString> tilesetfiles;
fs->directoryContents("/Unit", QDir::Files, tilesetfiles);
for (int i = 0; i < tilesetfiles.length(); i++)
{
QString fileName = tilesetfiles[i];
fileName.chop(5);
if (id == 0 && !fileName.startsWith("J_")){
continue;
}
else if (id == 1 && !fileName.startsWith("M_")){
continue;
}
else if (id == 2 && !fileName.startsWith("S1_")){
continue;
}
else if (id == 3 && !fileName.startsWith("S2_")){
continue;
}
QString tilesetname;
tilesetname = defaultNames.value(fileName, fileName);
QList<QStandardItem*> items;
QStandardItem* tileset = new QStandardItem(tilesetname);
tileset->setData(fileName);
items.append(tileset);
QStandardItem* fileNameItem = new QStandardItem(fileName);
fileNameItem->setData(fileName);
items.append(fileNameItem);
model->appendRow(items);
}
return model;
}