-
Notifications
You must be signed in to change notification settings - Fork 4
/
media_manager.cpp
249 lines (213 loc) · 6.45 KB
/
media_manager.cpp
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
#include "mcommon.h"
#include "file/media_manager.h"
#include "file/fileutils.h"
#include "config_app.h"
std::string g_AppFriendlyName = APPLICATION_NAME;
MediaManager& MediaManager::Instance()
{
static MediaManager manager;
return manager;
}
bool MediaManager::SetAssetPath(const fs::path& assetPath)
{
m_documentsPath = FileUtils::GetDocumentsPath() / fs::path(g_AppFriendlyName);
m_mediaPath = fs::canonical(fs::absolute(assetPath));
LOG(INFO) << "Media Path: " << m_mediaPath.string();
m_texturePaths.push_back(fs::path("textures"));
m_shaderPaths.push_back(fs::path("shaders"));
m_shaderPaths.push_back(fs::path("shaders") / fs::path("GL"));
m_modelPaths.push_back(fs::path("models"));
m_docPaths.push_back(fs::path("documents"));
m_modelPaths.push_back(fs::path("test") / fs::path("models"));
m_fontPaths.push_back(fs::path("fonts"));
m_textureExtensions.push_back(".dds");
m_textureExtensions.push_back(".png");
m_textureSubstringReplacements["_bump"] = "_normal";
return true;
}
bool MediaManager::SetProjectPath(const fs::path& project)
{
if (fs::exists(project))
{
m_projectPath = fs::canonical(fs::absolute(project));
return true;
}
try
{
fs::path newPath;
if (project.is_relative())
{
newPath = m_documentsPath / project;
}
else
{
newPath = project;
}
if (!fs::exists(newPath))
{
if (!fs::create_directories(newPath))
{
return false;
}
}
m_projectPath = fs::canonical(fs::absolute(newPath));
return true;
}
catch (fs::filesystem_error& err)
{
LOG(ERROR) << err.what();
}
return false;
}
MediaManager::MediaManager()
{
}
std::string MediaManager::LoadAsset(const char* pszPath, uint32_t mediaType, const fs::path* assetBase)
{
auto path = MediaManager::Instance().FindAsset(pszPath, mediaType, assetBase);
return FileUtils::ReadFile(path);
}
fs::path MediaManager::FindAsset(const char* pszPath, uint32_t mediaType, const fs::path* assetBase)
{
auto itrFound = m_foundAssets.find(pszPath);
if (itrFound != m_foundAssets.end())
{
return itrFound->second;
}
fs::path found = FindAssetInternal(pszPath, mediaType, assetBase);
if (!found.empty())
{
m_foundAssets[pszPath] = found;
}
return found;
}
fs::path MediaManager::FindAssetInternal(const char* pszPath, uint32_t mediaType, const fs::path* assetBase)
{
fs::path searchPath(pszPath);
fs::path parent;
if (assetBase)
{
parent = *assetBase;
}
// If an absolute path, start by checking it points at something
if (searchPath.is_absolute())
{
if (fs::exists(searchPath))
{
// Return clean path
LOG(DEBUG) << "Found path: " << searchPath.string();
return fs::canonical(searchPath);
}
searchPath = searchPath.filename();
// Absolute parent the default if not specified
if (parent.empty())
{
parent = searchPath.parent_path();
}
}
if (parent.empty())
{
// Otherwise, start at the base of the media path
parent = m_mediaPath;
}
auto searchPaths = [&](const fs::path& parentPath, const fs::path& fileSearch, const std::vector<fs::path>& searchPaths)
{
for (auto& currentSearch : searchPaths)
{
fs::path testPath = parentPath / currentSearch / fileSearch;
if (fs::exists(testPath))
{
return fs::canonical(fs::absolute(testPath));
}
}
fs::path parentChild = parentPath / fileSearch;
if (fs::exists(parentChild))
{
return fs::canonical(fs::absolute(parentChild));
}
return fs::path();
};
if (mediaType & MediaType::Local)
{
fs::path found(searchPath);
if (fs::exists(found))
{
LOG(DEBUG) << "Found local file: " << found.string();
return fs::canonical(fs::absolute(found));
}
}
if (mediaType & MediaType::Texture)
{
std::vector<fs::path> testFiles;
testFiles.push_back(searchPath);
for (auto& extension : m_textureExtensions)
{
testFiles.push_back(fs::path(searchPath).replace_extension(extension));
}
size_t index = 0;
size_t lastIndex = testFiles.size();
while(index < lastIndex)
{
for (auto& replacement : m_textureSubstringReplacements)
{
if (testFiles[index].string().find(replacement.first) != std::string::npos)
{
testFiles.push_back(StringUtils::ReplaceString(testFiles[index].string(), replacement.first, replacement.second));
}
}
index++;
}
for (auto& current : testFiles)
{
fs::path found = searchPaths(parent, current, m_texturePaths);
if (!found.empty())
{
LOG(DEBUG) << "Found texture: " << found.string();
return found;
}
}
LOG(DEBUG) << "Texture not found, searches: ";
for (auto& current : testFiles)
{
LOG(DEBUG) << current.string();
}
}
if (mediaType & MediaType::Model)
{
fs::path found = searchPaths(parent, searchPath, m_modelPaths);
if (!found.empty())
{
LOG(DEBUG) << "Found model: " << found.string();
return found;
}
}
if (mediaType & MediaType::Document)
{
fs::path found = searchPaths(parent, searchPath, m_docPaths);
if (!found.empty())
{
LOG(DEBUG) << "Found document: " << found.string();
return found;
}
}
if (mediaType & MediaType::Font)
{
fs::path found = searchPaths(parent, searchPath, m_fontPaths);
if (!found.empty())
{
LOG(DEBUG) << "Found font: " << found.string();
return found;
}
}
if (mediaType & MediaType::Shader)
{
fs::path found = searchPaths(parent, searchPath, m_shaderPaths);
if (!found.empty())
{
LOG(DEBUG) << "Found shader: " << found.string();
return found;
}
}
LOG(DEBUG) << "** File not found: " << searchPath.string();
return fs::path();
}