-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCProjectManager.cpp
More file actions
296 lines (247 loc) · 7.45 KB
/
CProjectManager.cpp
File metadata and controls
296 lines (247 loc) · 7.45 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
#include "CProjectManager.h"
#include <rapidjson.h>
#include <document.h>
#include <stringbuffer.h>
#include <writer.h>
#include <cocos2d.h>
#include <QDir>
#include <QDebug>
#include <QCoreApplication>
CProjectManager::CProjectManager():
m_sProjectPath("")
{
m_sInstallPath = QCoreApplication::applicationDirPath();
QFile configFile(m_sInstallPath + "/prev.json");
this->ReadLastProject(configFile);
}
CProjectManager::CProjectManager(const CProjectManager &a_oProjectManager)
{
}
CProjectManager& CProjectManager::operator=(const CProjectManager& a_oProjecManager)
{
return *this;
}
CProjectManager::~CProjectManager()
{
}
void CProjectManager::ReadLastProject(QFile& a_fConfigFile)
{
if(!a_fConfigFile.exists())
{
return;
}
QString fileContent;
if (!a_fConfigFile.open(QFile::ReadOnly | QFile::Text))
{
qDebug("Cannot open file");
return;
}
else
{
fileContent = QString(a_fConfigFile.readAll());
}
rapidjson::Document doc;
doc.Parse(fileContent.toStdString().c_str());
if(doc.HasMember("last_projects") && doc["last_projects"].IsArray())
{
int size = doc["last_projects"].Size();
rapidjson::Value& lastProjectArray = doc["last_projects"];
for(int i = 0; i < size; i++)
{
if(lastProjectArray[i].HasMember("path"))
{
std::string currentValue = lastProjectArray[i]["path"].GetString();
if(!currentValue.empty())
{
m_vPreviousProjectPaths.push_back(currentValue);
}
}
}
}
}
void CProjectManager::EditPrevFile()
{
rapidjson::StringBuffer s;
rapidjson::Writer<rapidjson::StringBuffer> writer(s);
rapidjson::Document document;
document.SetObject();
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
rapidjson::Value last_projects(rapidjson::kArrayType);
int iMaxPreviousProjects = m_vPreviousProjectPaths.size() > 3 ? 3 : m_vPreviousProjectPaths.size();
for (int i = 0; i < iMaxPreviousProjects; ++i)
{
rapidjson::Value oProj(rapidjson::kObjectType);
// Force copy of the path, if using local variable, it will be destroyed before rapidjson writes the file.
oProj.AddMember("path",
rapidjson::Value(m_vPreviousProjectPaths.at(i).c_str(), m_vPreviousProjectPaths.at(i).length()), allocator);
last_projects.PushBack(oProj, allocator);
}
document.AddMember("last_projects", last_projects, allocator);
document.Accept(writer);
qDebug()<< s.GetString();
QFile file( m_sInstallPath + "/prev.json" );
QString jsonResult;
if ( file.open(QIODevice::ReadWrite | QFile::Truncate) )
{
QTextStream stream( &file );
stream.setCodec("UTF-8");
jsonResult = QString::fromUtf8(s.GetString());
stream << jsonResult;
stream.flush();
file.close();
}
}
std::size_t CProjectManager::GetProjectHash()
{
std::hash<std::string> hash;
std::size_t projectHash = hash(GetProjectJsonFile());
return projectHash;
}
CProjectManager* CProjectManager::Instance()
{
static CProjectManager instance;
return &instance;
}
void CProjectManager::SetProjectFile(const QString &a_sPath)
{
m_sProjectPath = a_sPath;
this->UpdateLastProject(m_sProjectPath);
this->EditPrevFile();
}
QString CProjectManager::QGetProjectPath()
{
QFileInfo temp(m_sProjectPath);
return temp.absolutePath() + "/";
}
QString CProjectManager::QGetInstallPath()
{
return m_sInstallPath;
}
QString CProjectManager::QGetAbsoluteWritablePath()
{
std::string pathHashDir = std::to_string(GetProjectHash());
#ifdef TARGET_OS_MAC
QDir writableDir(std::string(cocos2d::FileUtils::getInstance()->getWritablePath() + "/LudoMuseEditor/" + pathHashDir).c_str());
#else
QDir writableDir(std::string(cocos2d::FileUtils::getInstance()->getWritablePath() + "/" + pathHashDir).c_str());
#endif
if (!writableDir.exists())
{
writableDir.mkpath(writableDir.absolutePath());
}
return writableDir.absolutePath() + "/";
}
QString CProjectManager::QGetProjectName()
{
QFileInfo temp(m_sProjectPath);
return temp.baseName();
}
QString CProjectManager::QGetProjectJsonFile()
{
return m_sProjectPath;
}
std::string CProjectManager::GetProjectPath()
{
QFileInfo temp(m_sProjectPath);
return temp.absolutePath().toStdString() + "/";
}
std::string CProjectManager::GetInstallPath()
{
return m_sInstallPath.toStdString();
}
std::string CProjectManager::GetAbsoluteWritablePath()
{
return QGetAbsoluteWritablePath().toStdString();
}
std::string CProjectManager::GetRelativeWritablePath()
{
#ifdef TARGET_OS_MAC
return std::string("LudoMuseEditor/") + std::to_string(GetProjectHash());
#else
return std::to_string(GetProjectHash());
#endif
}
std::string CProjectManager::GetProjectName()
{
QFileInfo temp(m_sInstallPath);
return temp.baseName().toStdString();
}
std::string CProjectManager::GetProjectJsonFile()
{
return m_sProjectPath.toStdString();
}
std::string* CProjectManager::PushBackSource(const std::string& a_sSource)
{
std::vector<std::string*>::iterator it = std::find(m_vSources.begin(), m_vSources.end(), &a_sSource);
if(it != m_vSources.end())
{
return (*it);
}
else
{
return (*(m_vSources.insert(m_vSources.end(), new std::string(a_sSource))));
}
}
const std::string* CProjectManager::GetSourceAt(int a_iIndex)
{
return m_vSources.at(a_iIndex);
}
const std::vector<std::string>& CProjectManager::GetPreviousProjectPaths()
{
return m_vPreviousProjectPaths;
}
void CProjectManager::UpdateLastProject(const QString &a_sNewProject)
{
std::vector<std::string>::iterator it = find(m_vPreviousProjectPaths.begin(), m_vPreviousProjectPaths.end(), a_sNewProject.toStdString());
if(it != m_vPreviousProjectPaths.end())
{
// Moving path to first position in vector
std::vector<std::string> tempVector;
int count = 0;
for(std::string currentString : m_vPreviousProjectPaths)
{
if(currentString == a_sNewProject.toStdString())
{
tempVector.insert(tempVector.begin(), currentString);
}
else
{
tempVector.push_back(currentString);
}
if(++count == 3)
{
break;
}
}
m_vPreviousProjectPaths = tempVector;
}
else
{
// Adding path to first posititon in vector
m_vPreviousProjectPaths.insert(m_vPreviousProjectPaths.begin(), a_sNewProject.toStdString());
// limite size to 3
if(m_vPreviousProjectPaths.size() > 3)
{
m_vPreviousProjectPaths.erase(m_vPreviousProjectPaths.begin() + 3, m_vPreviousProjectPaths.end());
}
}
}
std::string CProjectManager::GetRelativePathForFile(std::string a_sSource)
{
std::string projectPath = CProjectManager::Instance()->GetProjectPath();
int index = a_sSource.find(projectPath);
if(index != std::string::npos)
{
a_sSource.erase(index, projectPath.length());
}
else
{
std::string templatePath = CProjectManager::Instance()->GetInstallPath() + "/templates/";
int index2 = a_sSource.find(templatePath);
if(index2 != std::string::npos)
{
a_sSource.erase(index2, templatePath.length());
}
}
return a_sSource;
}