forked from EasyRPG/Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.cpp
More file actions
306 lines (251 loc) · 8.3 KB
/
project.cpp
File metadata and controls
306 lines (251 loc) · 8.3 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
/*
* This file is part of EasyRPG Editor.
*
* EasyRPG Editor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Editor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Editor. If not, see <http://www.gnu.org/licenses/>.
*/
#include "project.h"
#include "defines.h"
#include "common/filefinder.h"
#include "common/scope_guard.h"
#include <lcf/inireader.h>
#include <lcf/reader_util.h>
#include <lcf/ldb/reader.h>
#include <lcf/lmu/reader.h>
#include <lcf/lmt/reader.h>
#include <lcf/encoder.h>
#include <QDir>
#include <memory>
#include <fstream>
Project::ProjectList Project::enumerate(const QDir& path) {
ProjectList prj_list;
const auto& list = path.entryList(QDir::Dirs);
for (const QString& item: list) {
auto res = Project::load(FileFinder::CombinePath(path.absolutePath(), item));
if (res) {
prj_list.push_back(res);
}
}
return prj_list;
}
std::shared_ptr<Project> Project::load(const QString& path) {
return load(QDir(path));
}
std::shared_ptr<Project> Project::load(const QDir& dir) {
FileFinder::ProjectType project_type = FileFinder::GetProjectType(dir);
if (project_type == FileFinder::ProjectType::None) {
return nullptr;
}
auto p = std::make_shared<Project>();
p->setProjectType(project_type);
p->setProjectDir(dir);
p->setEncoding("utf-8");
QString cfg;
if (project_type == FileFinder::ProjectType::EasyRpg) {
cfg = FileFinder::Find(dir, EASY_CFG);
} else {
cfg = FileFinder::Find(dir, RM_INI);
}
if (!cfg.isNull()) {
lcf::INIReader ini(cfg.toStdString());
auto title = ini.GetString("RPG_RT", GAMETITLE, tr("Untitled").toStdString());
if (project_type == FileFinder::ProjectType::Legacy) {
// Check for game encoding
auto enc = ini.GetString("EasyRPG", "Encoding", "");
if (enc.empty()) {
// Only use the title for encoding detection
// This is called for all games in the "Open Project" list
// Upon project load a smarter detection is used
enc = lcf::ReaderUtil::DetectEncoding(title);
}
p->setEncoding(QString::fromStdString(std::string(enc)));
title = lcf::ReaderUtil::Recode(title, enc);
}
p->setGameTitle(QString::fromStdString(std::string(title)));
}
return p;
}
bool Project::loadDatabaseAndMapTree() {
if (projectType() == FileFinder::ProjectType::Legacy) {
detectEncoding();
auto db = lcf::LDB_Reader::Load(findFile(RM_DB).toStdString(), encoding().toStdString());
if (db == nullptr) {
return false;
}
auto treemap = lcf::LMT_Reader::Load(findFile(RM_MT).toStdString(), encoding().toStdString());
if (treemap == nullptr) {
return false;
}
m_data = { *this, *db, *treemap };
} else {
auto db = lcf::LDB_Reader::LoadXml(findFile(EASY_DB).toStdString());
if (db == nullptr) {
return false;
}
auto treemap = lcf::LMT_Reader::LoadXml(findFile(EASY_MT).toStdString());
if (treemap == nullptr) {
return false;
}
m_data = { *this, *db, *treemap };
}
return true;
}
std::unique_ptr<lcf::rpg::Map> Project::loadMap(int index) const {
QString ext = projectType() == FileFinder::ProjectType::EasyRpg ? "emu" : "lmu";
QString file = QString("Map%1.%2")
.arg(QString::number(index), 4, QLatin1Char('0')).arg(ext);
QString mapFile = findFile(file);
if (mapFile.isNull()) {
return nullptr;
}
if (projectType() == FileFinder::ProjectType::EasyRpg) {
return lcf::LMU_Reader::LoadXml(mapFile.toStdString());
} else {
return lcf::LMU_Reader::Load(mapFile.toStdString(), encoding().toStdString());
}
}
bool Project::saveMap(lcf::rpg::Map& map, int index, bool incSavecount) {
auto lcf_engine = lcf::GetEngineVersion(m_data.database());
QString ext = projectType() == FileFinder::ProjectType::EasyRpg ? "emu" : "lmu";
QString file = QString("Map%1.%2")
.arg(QString::number(index), 4, QLatin1Char('0')).arg(ext);
QString mapFile = findFileOrDefault(file);
if (incSavecount) {
lcf::LMU_Reader::PrepareSave(map);
}
if (projectType() == FileFinder::ProjectType::EasyRpg) {
return lcf::LMU_Reader::SaveXml(mapFile.toStdString(), map, lcf_engine);
} else {
return lcf::LMU_Reader::Save(mapFile.toStdString(), map, lcf_engine, encoding().toStdString());
}
}
void Project::relocate(const QDir& newDir, FileFinder::ProjectType newProjectType) {
setProjectDir(newDir);
setProjectType(newProjectType);
}
QString Project::findFile(const QString& filename, FileFinder::FileType type) const {
return FileFinder::Find(projectDir().absolutePath(), filename, type);
}
QString Project::findFile(const QString& dir, const QString& filename, FileFinder::FileType type) const {
return FileFinder::Find(projectDir().absolutePath(), dir, filename, type);
}
QString Project::findFileOrDefault(const QString& filename) const {
QString found = findFile(filename);
return found.isEmpty() ? FileFinder::CombinePath(projectDir().absolutePath(), filename) : found;
}
QString Project::findDirectory(const QString& dir) const {
return FileFinder::Find(projectDir().absolutePath(), dir, FileFinder::FileType::Default, QDir::Dirs);
}
QString Project::findDirectory(const QString& baseDir, const QString& dir) const {
return FileFinder::Find(projectDir().absolutePath(), baseDir, dir, FileFinder::FileType::Default, QDir::Dirs);
}
QString Project::detectEncoding() {
if (projectType() == FileFinder::ProjectType::EasyRpg) {
setEncoding("utf-8");
return encoding();
}
auto cfg = findFile(RM_INI);
std::string enc;
std::string title;
if (!cfg.isNull()) {
lcf::INIReader ini(cfg.toStdString());
title = ini.GetString("RPG_RT", GAMETITLE, tr("Untitled").toStdString());
enc = ini.GetString("EasyRPG", "Encoding", "");
}
if (enc.empty()) {
std::ifstream i(findFile(RM_DB).toStdString(), std::ios::binary);
if (i) {
auto db = lcf::LDB_Reader::Load(i);
if (db) {
std::string dbenc = lcf::ReaderUtil::DetectEncoding(*db);
lcf::Encoder encoder(dbenc);
if (encoder.IsOk()) {
enc = dbenc;
}
}
}
}
setEncoding(QString::fromStdString(enc));
setGameTitle(QString::fromStdString(lcf::ReaderUtil::Recode(title, enc)));
return encoding();
}
QString Project::encoding() const {
return m_encoding;
}
void Project::setEncoding(const QString& encoding) {
m_encoding = encoding;
}
QDir Project::projectDir() const {
return m_projectDir;
}
void Project::setProjectDir(const QDir& projectDir) {
m_projectDir = projectDir;
}
QString Project::gameTitle() const {
return m_gameTitle;
}
void Project::setGameTitle(const QString& gameTitle) {
m_gameTitle = gameTitle;
}
FileFinder::ProjectType Project::projectType() const {
return m_projectType;
}
void Project::setProjectType(FileFinder::ProjectType projectType) {
m_projectType = projectType;
}
ProjectData &Project::projectData() {
return m_data;
}
const ProjectData &Project::projectData() const {
return m_data;
}
lcf::rpg::Database& Project::database() {
return m_data.database();
}
const lcf::rpg::Database& Project::database() const {
return m_data.database();
}
lcf::rpg::TreeMap& Project::treeMap() {
return m_data.treeMap();
}
const lcf::rpg::TreeMap& Project::treeMap() const {
return m_data.treeMap();
}
bool Project::saveDatabase(bool inc_savecount) {
if (inc_savecount) {
lcf::LDB_Reader::PrepareSave(m_data.database());
}
if (projectType() == FileFinder::ProjectType::Legacy) {
if (!lcf::LDB_Reader::Save(findFileOrDefault(RM_DB).toStdString(), m_data.database(), encoding().toStdString())) {
return false;
}
} else {
if (!lcf::LDB_Reader::SaveXml(findFileOrDefault(EASY_DB).toStdString(), m_data.database())) {
return false;
}
}
return true;
}
bool Project::saveTreeMap() {
auto lcf_engine = lcf::GetEngineVersion(m_data.database());
if (projectType() == FileFinder::ProjectType::Legacy) {
if (!lcf::LMT_Reader::Save(findFileOrDefault(RM_MT).toStdString(), m_data.treeMap(), lcf_engine, encoding().toStdString())) {
return false;
}
} else {
if (!lcf::LMT_Reader::SaveXml(findFileOrDefault(EASY_MT).toStdString(), m_data.treeMap(), lcf_engine)) {
return false;
}
}
return true;
}