Skip to content

Commit 53a959c

Browse files
committed
Simplify the paths used.
1 parent 09ffc8b commit 53a959c

File tree

1 file changed

+99
-71
lines changed

1 file changed

+99
-71
lines changed

src/gui2/imhex_overrides/default_paths.cpp

Lines changed: 99 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,148 +7,176 @@
77
#include <algorithm>
88

99
#if defined(OS_WINDOWS)
10-
#include <windows.h>
11-
#include <shlobj.h>
10+
#include <windows.h>
11+
#include <shlobj.h>
1212
#elif defined(OS_LINUX) || defined(OS_WEB)
13-
#include <xdg.hpp>
14-
# endif
13+
#include <xdg.hpp>
14+
#endif
1515

16-
namespace hex::paths {
16+
namespace hex::paths
17+
{
1718
const char* ApplicationName = "fluxengine";
1819

19-
std::vector<std::fs::path> getDataPaths(bool includeSystemFolders) {
20+
std::vector<std::fs::path> getDataPaths(bool includeSystemFolders)
21+
{
2022
std::vector<std::fs::path> paths;
2123

22-
#if defined(OS_WINDOWS)
24+
#if defined(OS_WINDOWS)
2325

24-
// In the portable Windows version, we just use the executable directory
25-
// Prevent the use of the AppData folder here
26-
if (!ImHexApi::System::isPortableVersion()) {
27-
PWSTR wAppDataPath = nullptr;
28-
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, nullptr, &wAppDataPath))) {
29-
paths.emplace_back(wAppDataPath);
30-
CoTaskMemFree(wAppDataPath);
31-
}
26+
// In the portable Windows version, we just use the executable directory
27+
// Prevent the use of the AppData folder here
28+
if (!ImHexApi::System::isPortableVersion())
29+
{
30+
PWSTR wAppDataPath = nullptr;
31+
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData,
32+
KF_FLAG_CREATE,
33+
nullptr,
34+
&wAppDataPath)))
35+
{
36+
paths.emplace_back(wAppDataPath);
37+
CoTaskMemFree(wAppDataPath);
3238
}
39+
}
3340

34-
#elif defined(OS_MACOS)
35-
36-
paths.push_back(wolv::io::fs::getApplicationSupportDirectoryPath() / ApplicationName);
41+
#elif defined(OS_MACOS)
3742

38-
#elif defined(OS_LINUX) || defined(OS_WEB)
43+
paths.push_back(wolv::io::fs::getApplicationSupportDirectoryPath() /
44+
ApplicationName);
3945

40-
paths.push_back(xdg::DataHomeDir());
46+
#elif defined(OS_LINUX) || defined(OS_WEB)
4147

42-
auto dataDirs = xdg::DataDirs();
43-
std::copy(dataDirs.begin(), dataDirs.end(), std::back_inserter(paths));
48+
paths.push_back(xdg::DataHomeDir());
4449

45-
#endif
50+
#endif
4651

47-
#if defined(OS_MACOS)
52+
#if defined(OS_MACOS)
4853

49-
if (includeSystemFolders) {
50-
if (auto executablePath = wolv::io::fs::getExecutablePath(); executablePath.has_value()) {
51-
paths.push_back(executablePath->parent_path());
52-
}
54+
if (includeSystemFolders)
55+
{
56+
if (auto executablePath = wolv::io::fs::getExecutablePath();
57+
executablePath.has_value())
58+
{
59+
paths.push_back(executablePath->parent_path());
5360
}
61+
}
5462

55-
#else
63+
#else
5664

57-
for (auto &path : paths)
58-
path = path / ApplicationName;
65+
for (auto& path : paths)
66+
path = path / ApplicationName;
5967

60-
if (ImHexApi::System::isPortableVersion() || includeSystemFolders) {
61-
if (auto executablePath = wolv::io::fs::getExecutablePath(); executablePath.has_value())
62-
paths.push_back(executablePath->parent_path());
63-
}
64-
65-
#endif
68+
if (ImHexApi::System::isPortableVersion() || includeSystemFolders)
69+
{
70+
if (auto executablePath = wolv::io::fs::getExecutablePath();
71+
executablePath.has_value())
72+
paths.push_back(executablePath->parent_path());
73+
}
6674

75+
#endif
6776

6877
// Add additional data directories to the path
6978
auto additionalDirs = ImHexApi::System::getAdditionalFolderPaths();
7079
std::ranges::copy(additionalDirs, std::back_inserter(paths));
7180

7281
// Add the project file directory to the path, if one is loaded
73-
if (ProjectFile::hasPath()) {
82+
if (ProjectFile::hasPath())
83+
{
7484
paths.push_back(ProjectFile::getPath().parent_path());
7585
}
7686

7787
return paths;
7888
}
7989

80-
std::vector<std::fs::path> getConfigPaths(bool includeSystemFolders) {
81-
#if defined(OS_WINDOWS)
82-
return getDataPaths(includeSystemFolders);
83-
#elif defined(OS_MACOS)
84-
return getDataPaths(includeSystemFolders);
85-
#elif defined(OS_LINUX) || defined(OS_WEB)
86-
std::ignore = includeSystemFolders;
87-
return {xdg::ConfigHomeDir() / ApplicationName};
88-
#endif
90+
std::vector<std::fs::path> getConfigPaths(bool includeSystemFolders)
91+
{
92+
#if defined(OS_WINDOWS)
93+
return getDataPaths(includeSystemFolders);
94+
#elif defined(OS_MACOS)
95+
return getDataPaths(includeSystemFolders);
96+
#elif defined(OS_LINUX) || defined(OS_WEB)
97+
std::ignore = includeSystemFolders;
98+
return {xdg::ConfigHomeDir() / ApplicationName};
99+
#endif
89100
}
90101

91-
static std::vector<std::fs::path> appendPath(std::vector<std::fs::path> paths, std::fs::path folder) {
102+
static std::vector<std::fs::path> appendPath(
103+
std::vector<std::fs::path> paths, std::fs::path folder)
104+
{
92105
folder.make_preferred();
93-
94-
for (auto &path : paths)
106+
107+
for (auto& path : paths)
95108
path = path / folder;
96109

97110
return paths;
98111
}
99112

100-
static std::vector<std::fs::path> getPluginPaths() {
113+
static std::vector<std::fs::path> getPluginPaths()
114+
{
101115
std::vector<std::fs::path> paths = getDataPaths(true);
102116

103-
// Add the system plugin directory to the path if one was provided at compile time
104-
#if defined(OS_LINUX) && defined(SYSTEM_PLUGINS_LOCATION)
105-
paths.push_back(SYSTEM_PLUGINS_LOCATION);
106-
#endif
117+
// Add the system plugin directory to the path if one was provided at compile
118+
// time
119+
#if defined(OS_LINUX) && defined(SYSTEM_PLUGINS_LOCATION)
120+
paths.push_back(SYSTEM_PLUGINS_LOCATION);
121+
#endif
107122

108123
return paths;
109124
}
110125

111-
namespace impl {
126+
namespace impl
127+
{
112128

113-
std::vector<std::fs::path> DefaultPath::read() const {
129+
std::vector<std::fs::path> DefaultPath::read() const
130+
{
114131
auto result = this->all();
115132

116-
std::erase_if(result, [](const auto &entryPath) {
117-
return !wolv::io::fs::isDirectory(entryPath);
118-
});
133+
std::erase_if(result,
134+
[](const auto& entryPath)
135+
{
136+
return !wolv::io::fs::isDirectory(entryPath);
137+
});
119138

120139
return result;
121140
}
122141

123-
std::vector<std::fs::path> DefaultPath::write() const {
142+
std::vector<std::fs::path> DefaultPath::write() const
143+
{
124144
auto result = this->read();
125145

126-
std::erase_if(result, [](const auto &entryPath) {
127-
return !hex::fs::isPathWritable(entryPath);
128-
});
146+
std::erase_if(result,
147+
[](const auto& entryPath)
148+
{
149+
return !hex::fs::isPathWritable(entryPath);
150+
});
129151

130152
return result;
131153
}
132154

133-
std::vector<std::fs::path> ConfigPath::all() const {
155+
std::vector<std::fs::path> ConfigPath::all() const
156+
{
134157
return appendPath(getConfigPaths(false), m_postfix);
135158
}
136159

137-
std::vector<std::fs::path> DataPath::all() const {
160+
std::vector<std::fs::path> DataPath::all() const
161+
{
138162
return appendPath(getDataPaths(true), m_postfix);
139163
}
140164

141-
std::vector<std::fs::path> DataPath::write() const {
165+
std::vector<std::fs::path> DataPath::write() const
166+
{
142167
auto result = appendPath(getDataPaths(false), m_postfix);
143168

144-
std::erase_if(result, [](const auto &entryPath) {
145-
return !hex::fs::isPathWritable(entryPath);
146-
});
169+
std::erase_if(result,
170+
[](const auto& entryPath)
171+
{
172+
return !hex::fs::isPathWritable(entryPath);
173+
});
147174

148175
return result;
149176
}
150177

151-
std::vector<std::fs::path> PluginPath::all() const {
178+
std::vector<std::fs::path> PluginPath::all() const
179+
{
152180
return appendPath(getPluginPaths(), m_postfix);
153181
}
154182

0 commit comments

Comments
 (0)