-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIconPack.cpp
More file actions
132 lines (94 loc) · 2.48 KB
/
Copy pathIconPack.cpp
File metadata and controls
132 lines (94 loc) · 2.48 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
#include "StdAfx.h"
#include "IconPack.h"
#include <Serialization/Qt.h>
#include <QDir>
#include <CrySerialization/IArchiveHost.h>
#include "QResource"
#include <QFileInfo>
TIconPackPtr IconPack::LoadIconPack(QString& packPath)
{
auto filePath = QDir::cleanPath(packPath);
auto pack = std::make_unique<IconPack>(filePath);
if (!Serialization::LoadJsonFile(*pack, filePath.toLocal8Bit().constData()))
return nullptr;
if (!pack->CleanAndValidate())
return nullptr;
pack->LoadResources();
return pack;
}
IconPack::IconPack(QString& basePath)
: m_basePath(QFileInfo(basePath).absolutePath())
{
}
IconPack::~IconPack()
{
UnloadResources();
}
bool IconPack::CleanAndValidate()
{
QDir basePath(m_basePath);
for (auto iter = m_iconPackResources.begin(); iter != m_iconPackResources.end();)
{
QString relativePath = QDir::cleanPath(iter->filePath);
QFileInfo testFile(basePath.absoluteFilePath(relativePath));
if (testFile.exists()) {
iter->filePath = testFile.absoluteFilePath();
++iter;
continue;
}
CryLogAlways("Could not open resource file: %s", iter->filePath.toStdString().c_str());
iter = m_iconPackResources.erase(iter);
}
return (!m_iconPackResources.empty() || m_bIsIconFolder);
}
void IconPack::LoadResources()
{
for (auto& pack : m_iconPackResources) {
QResource::registerResource(pack.filePath);
}
}
void IconPack::UnloadResources()
{
for (auto& pack : m_iconPackResources) {
QResource::unregisterResource(pack.filePath);
}
}
bool IconPack::Serialize(Serialization::IArchive& ar)
{
ar(m_bIsIconFolder, "isIconFolder", "IsIconFolder");
ar(m_iconPackResources, "resources", "Resources");
return true;
}
void IconPack::AddToSearchPath(QStringList& searhPaths)
{
if (m_bIsIconFolder)
searhPaths.append(m_basePath);
for (auto &pack : m_iconPackResources)
searhPaths.append(QString(':').append(pack.prefix));
}
bool IconPack::TryReplacePackPath(QString& path, const char* replaceWith)
{
if (path.startsWith(":/"))
{
for (auto& pack : m_iconPackResources)
{
auto pathPrefix = QString(":").append(pack.prefix);
if(!path.startsWith(pathPrefix))
continue;
path.replace(0, pathPrefix.length() + 1, replaceWith);
return true;
}
}
else if (m_bIsIconFolder && path.startsWith(m_basePath))
{
path.replace(0, m_basePath.length() + 1, replaceWith);
return true;
}
return false;
}
bool IconPackResource::Serialize(Serialization::IArchive& ar)
{
ar(prefix, "prefix", "Prefix");
ar(filePath, "file", "File");
return true;
}