2022-03-04 11:44:11 +01:00
|
|
|
#include <hex/helpers/fs.hpp>
|
|
|
|
|
2022-02-18 22:34:54 +01:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2023-01-11 23:31:25 +01:00
|
|
|
#include <hex/api/project_file_manager.hpp>
|
2023-03-26 11:02:51 +02:00
|
|
|
#include <hex/helpers/logger.hpp>
|
2023-05-21 13:21:53 +02:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
2023-07-05 20:50:46 +02:00
|
|
|
#include <hex/helpers/utils_linux.hpp>
|
2023-01-11 23:31:25 +01:00
|
|
|
|
2021-12-03 00:00:25 +01:00
|
|
|
#include <xdg.hpp>
|
|
|
|
|
2021-09-03 02:33:15 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
#include <windows.h>
|
|
|
|
#include <shlobj.h>
|
2023-10-04 16:10:14 +02:00
|
|
|
#elif defined(OS_LINUX) || defined(OS_WEB)
|
2021-09-03 02:33:15 +02:00
|
|
|
#include <xdg.hpp>
|
2023-10-04 12:00:32 +02:00
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
|
|
|
|
2023-10-04 16:10:14 +02:00
|
|
|
#if defined(OS_WEB)
|
|
|
|
#include <emscripten.h>
|
2023-10-04 12:00:32 +02:00
|
|
|
#else
|
2023-10-04 16:10:14 +02:00
|
|
|
#include <nfd.hpp>
|
2021-09-03 02:33:15 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <filesystem>
|
2023-07-09 12:53:31 +02:00
|
|
|
#include <utility>
|
2021-09-03 02:33:15 +02:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <wolv/io/file.hpp>
|
2023-03-13 09:25:07 +01:00
|
|
|
#include <wolv/io/fs.hpp>
|
2023-07-09 12:53:31 +02:00
|
|
|
#include <wolv/utils/string.hpp>
|
2022-03-04 11:36:37 +01:00
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
namespace hex::fs {
|
2022-03-04 11:36:37 +01:00
|
|
|
|
2023-03-26 11:02:51 +02:00
|
|
|
static std::function<void(const std::string&)> s_fileBrowserErrorCallback;
|
|
|
|
void setFileBrowserErrorCallback(const std::function<void(const std::string&)> &callback) {
|
2022-09-20 15:47:59 +02:00
|
|
|
s_fileBrowserErrorCallback = callback;
|
|
|
|
}
|
|
|
|
|
2023-05-21 13:21:53 +02:00
|
|
|
// With help from https://github.com/owncloud/client/blob/cba22aa34b3677406e0499aadd126ce1d94637a2/src/gui/openfilemanager.cpp
|
|
|
|
void openFileExternal(const std::fs::path &filePath) {
|
2023-10-29 19:43:40 +01:00
|
|
|
// Make sure the file exists before trying to open it
|
2023-05-21 13:21:53 +02:00
|
|
|
if (!wolv::io::fs::exists(filePath))
|
|
|
|
return;
|
|
|
|
|
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
hex::unused(
|
|
|
|
ShellExecute(nullptr, "open", wolv::util::toUTF8String(filePath).c_str(), nullptr, nullptr, SW_SHOWNORMAL)
|
|
|
|
);
|
|
|
|
#elif defined(OS_MACOS)
|
|
|
|
hex::unused(system(
|
|
|
|
hex::format("open {}", wolv::util::toUTF8String(filePath)).c_str()
|
|
|
|
));
|
|
|
|
#elif defined(OS_LINUX)
|
2023-07-05 20:50:46 +02:00
|
|
|
executeCmd({"xdg-open", wolv::util::toUTF8String(filePath)});
|
2023-05-21 13:21:53 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void openFolderExternal(const std::fs::path &dirPath) {
|
2023-10-29 19:43:40 +01:00
|
|
|
// Make sure the folder exists before trying to open it
|
2023-05-21 13:21:53 +02:00
|
|
|
if (!wolv::io::fs::exists(dirPath))
|
|
|
|
return;
|
|
|
|
|
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
hex::unused(system(
|
|
|
|
hex::format("explorer.exe {}", wolv::util::toUTF8String(dirPath)).c_str()
|
|
|
|
));
|
|
|
|
#elif defined(OS_MACOS)
|
|
|
|
hex::unused(system(
|
|
|
|
hex::format("open {}", wolv::util::toUTF8String(dirPath)).c_str()
|
|
|
|
));
|
|
|
|
#elif defined(OS_LINUX)
|
2023-07-05 20:50:46 +02:00
|
|
|
executeCmd({"xdg-open", wolv::util::toUTF8String(dirPath)});
|
2023-05-21 13:21:53 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void openFolderWithSelectionExternal(const std::fs::path &selectedFilePath) {
|
2023-10-29 19:43:40 +01:00
|
|
|
// Make sure the file exists before trying to open it
|
2023-05-21 13:21:53 +02:00
|
|
|
if (!wolv::io::fs::exists(selectedFilePath))
|
|
|
|
return;
|
|
|
|
|
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
hex::unused(system(
|
|
|
|
hex::format(R"(explorer.exe /select,"{}")", wolv::util::toUTF8String(selectedFilePath)).c_str()
|
|
|
|
));
|
|
|
|
#elif defined(OS_MACOS)
|
|
|
|
hex::unused(system(
|
|
|
|
hex::format(
|
|
|
|
R"(osascript -e 'tell application "Finder" to reveal POSIX file "{}"')",
|
|
|
|
wolv::util::toUTF8String(selectedFilePath)
|
|
|
|
).c_str()
|
|
|
|
));
|
|
|
|
system(R"(osascript -e 'tell application "Finder" to activate')");
|
|
|
|
#elif defined(OS_LINUX)
|
2023-10-04 12:00:32 +02:00
|
|
|
// Fallback to only opening the folder for now
|
2023-05-21 13:21:53 +02:00
|
|
|
// TODO actually select the file
|
2023-07-05 20:50:46 +02:00
|
|
|
executeCmd({"xdg-open", wolv::util::toUTF8String(selectedFilePath.parent_path())});
|
2023-05-21 13:21:53 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
#if defined(OS_WEB)
|
|
|
|
|
|
|
|
std::function<void(std::fs::path)> currentCallback;
|
2023-03-26 11:02:51 +02:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
|
|
extern "C" void fileBrowserCallback(char* path) {
|
|
|
|
currentCallback(path);
|
2023-03-26 11:02:51 +02:00
|
|
|
}
|
2022-03-04 11:36:37 +01:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
EM_JS(int, callJs_saveFile, (const char *rawFilename), {
|
|
|
|
let filename = UTF8ToString(rawFilename) || "file.bin";
|
|
|
|
FS.createPath("/", "savedFiles");
|
|
|
|
|
|
|
|
if (FS.analyzePath(filename).exists) {
|
|
|
|
FS.unlink(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call callback that will write the file
|
|
|
|
Module._fileBrowserCallback(stringToNewUTF8("/savedFiles/" + filename));
|
|
|
|
|
|
|
|
let data = FS.readFile("/savedFiles/" + filename);
|
|
|
|
|
|
|
|
const reader = Object.assign(new FileReader(), {
|
|
|
|
onload: () => {
|
|
|
|
|
|
|
|
// Show popup to user to download
|
|
|
|
let saver = document.createElement('a');
|
|
|
|
saver.href = reader.result;
|
|
|
|
saver.download = filename;
|
|
|
|
saver.style = "display: none";
|
|
|
|
|
|
|
|
saver.click();
|
|
|
|
|
|
|
|
},
|
|
|
|
onerror: () => {
|
|
|
|
throw new Error(reader.error);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
reader.readAsDataURL(new File([data], "", { type: "application/octet-stream" }));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
EM_JS(int, callJs_openFile, (bool multiple), {
|
|
|
|
let selector = document.createElement("input");
|
|
|
|
selector.type = "file";
|
|
|
|
selector.style = "display: none";
|
|
|
|
if (multiple) {
|
|
|
|
selector.multiple = true;
|
|
|
|
}
|
|
|
|
selector.onchange = () => {
|
|
|
|
if (selector.files.length == 0) return;
|
|
|
|
|
|
|
|
FS.createPath("/", "openedFiles");
|
|
|
|
for (let file of selector.files) {
|
|
|
|
const fr = new FileReader();
|
|
|
|
fr.onload = () => {
|
|
|
|
let path = "/openedFiles/"+file.name;
|
|
|
|
if (FS.analyzePath(path).exists) {
|
|
|
|
FS.unlink(path);
|
|
|
|
}
|
|
|
|
FS.createDataFile("/openedFiles/", file.name, fr.result, true, true);
|
|
|
|
Module._fileBrowserCallback(stringToNewUTF8(path));
|
|
|
|
};
|
|
|
|
|
|
|
|
fr.readAsBinaryString(file);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
selector.click();
|
|
|
|
});
|
|
|
|
|
|
|
|
bool openFileBrowser(DialogMode mode, const std::vector<ItemFilter> &validExtensions, const std::function<void(std::fs::path)> &callback, const std::string &defaultPath, bool multiple) {
|
|
|
|
switch (mode) {
|
|
|
|
case DialogMode::Open: {
|
|
|
|
currentCallback = callback;
|
|
|
|
callJs_openFile(multiple);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DialogMode::Save: {
|
|
|
|
currentCallback = callback;
|
|
|
|
std::fs::path path;
|
|
|
|
|
|
|
|
if (!defaultPath.empty())
|
|
|
|
path = std::fs::path(defaultPath).filename();
|
|
|
|
else if (!validExtensions.empty())
|
|
|
|
path = "file." + validExtensions[0].spec;
|
|
|
|
|
|
|
|
callJs_saveFile(path.filename().string().c_str());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DialogMode::Folder: {
|
|
|
|
throw std::logic_error("Selecting a folder is not implemented");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
std::unreachable();
|
|
|
|
}
|
|
|
|
return true;
|
2022-03-04 11:36:37 +01:00
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
#else
|
|
|
|
|
|
|
|
bool openFileBrowser(DialogMode mode, const std::vector<ItemFilter> &validExtensions, const std::function<void(std::fs::path)> &callback, const std::string &defaultPath, bool multiple) {
|
2023-10-29 19:43:40 +01:00
|
|
|
// Turn the content of the ItemFilter objects into something NFD understands
|
2023-10-04 12:00:32 +02:00
|
|
|
std::vector<nfdfilteritem_t> validExtensionsNfd;
|
2023-10-26 17:28:36 +02:00
|
|
|
for (const auto &extension : validExtensions) {
|
|
|
|
validExtensionsNfd.emplace_back(nfdfilteritem_t{ extension.name.c_str(), extension.spec.c_str() });
|
2023-10-04 12:00:32 +02:00
|
|
|
}
|
2023-10-29 19:43:40 +01:00
|
|
|
|
|
|
|
// Clear errors from previous runs
|
2023-10-04 12:00:32 +02:00
|
|
|
NFD::ClearError();
|
|
|
|
|
2023-10-29 19:43:40 +01:00
|
|
|
// Try to initialize NFD
|
2023-10-04 12:00:32 +02:00
|
|
|
if (NFD::Init() != NFD_OKAY) {
|
2023-10-29 19:43:40 +01:00
|
|
|
// Handle errors if initialization failed
|
2023-10-04 12:00:32 +02:00
|
|
|
log::error("NFD init returned an error: {}", NFD::GetError());
|
2023-10-26 17:28:36 +02:00
|
|
|
if (s_fileBrowserErrorCallback != nullptr) {
|
|
|
|
auto error = NFD::GetError();
|
|
|
|
s_fileBrowserErrorCallback(error != nullptr ? error : "No details");
|
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NFD::UniquePathU8 outPath;
|
|
|
|
NFD::UniquePathSet outPaths;
|
2023-10-29 19:43:40 +01:00
|
|
|
nfdresult_t result = NFD_ERROR;
|
|
|
|
|
|
|
|
// Open the correct file dialog based on the mode
|
2023-10-04 12:00:32 +02:00
|
|
|
switch (mode) {
|
|
|
|
case DialogMode::Open:
|
|
|
|
if (multiple)
|
|
|
|
result = NFD::OpenDialogMultiple(outPaths, validExtensionsNfd.data(), validExtensionsNfd.size(), defaultPath.empty() ? nullptr : defaultPath.c_str());
|
|
|
|
else
|
|
|
|
result = NFD::OpenDialog(outPath, validExtensionsNfd.data(), validExtensionsNfd.size(), defaultPath.empty() ? nullptr : defaultPath.c_str());
|
|
|
|
break;
|
|
|
|
case DialogMode::Save:
|
|
|
|
result = NFD::SaveDialog(outPath, validExtensionsNfd.data(), validExtensionsNfd.size(), defaultPath.empty() ? nullptr : defaultPath.c_str());
|
|
|
|
break;
|
|
|
|
case DialogMode::Folder:
|
|
|
|
result = NFD::PickFolder(outPath, defaultPath.empty() ? nullptr : defaultPath.c_str());
|
|
|
|
break;
|
2022-09-20 15:47:59 +02:00
|
|
|
}
|
2023-10-04 12:00:32 +02:00
|
|
|
|
|
|
|
if (result == NFD_OKAY){
|
2023-10-29 19:43:40 +01:00
|
|
|
// Handle the path if the dialog was opened in single mode
|
|
|
|
if (outPath != nullptr) {
|
|
|
|
// Call the provided callback with the path
|
2023-10-04 12:00:32 +02:00
|
|
|
callback(reinterpret_cast<char8_t*>(outPath.get()));
|
|
|
|
}
|
2023-10-29 19:43:40 +01:00
|
|
|
|
|
|
|
// Handle multiple paths if the dialog was opened in multiple mode
|
2023-10-04 12:00:32 +02:00
|
|
|
if (outPaths != nullptr) {
|
|
|
|
nfdpathsetsize_t numPaths = 0;
|
|
|
|
if (NFD::PathSet::Count(outPaths, numPaths) == NFD_OKAY) {
|
2023-10-29 19:43:40 +01:00
|
|
|
// Loop over all returned paths and call the callback with each of them
|
2023-10-04 12:00:32 +02:00
|
|
|
for (size_t i = 0; i < numPaths; i++) {
|
|
|
|
NFD::UniquePathSetPath path;
|
|
|
|
if (NFD::PathSet::GetPath(outPaths, i, path) == NFD_OKAY)
|
|
|
|
callback(reinterpret_cast<char8_t*>(path.get()));
|
|
|
|
}
|
2023-01-28 00:01:53 +01:00
|
|
|
}
|
|
|
|
}
|
2023-10-04 12:00:32 +02:00
|
|
|
} else if (result == NFD_ERROR) {
|
2023-10-29 19:43:40 +01:00
|
|
|
// Handle errors that occurred during the file dialog call
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
log::error("Requested file dialog returned an error: {}", NFD::GetError());
|
2023-10-29 19:43:40 +01:00
|
|
|
|
|
|
|
if (s_fileBrowserErrorCallback != nullptr) {
|
|
|
|
auto error = NFD::GetError();
|
|
|
|
s_fileBrowserErrorCallback(error != nullptr ? error : "No details");
|
|
|
|
}
|
2023-01-28 00:01:53 +01:00
|
|
|
}
|
2022-03-04 11:36:37 +01:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
NFD::Quit();
|
2022-03-04 11:36:37 +01:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
return result == NFD_OKAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2022-03-04 11:36:37 +01:00
|
|
|
|
2023-03-26 11:02:23 +02:00
|
|
|
std::vector<std::fs::path> getDataPaths() {
|
2022-08-01 14:51:08 +02:00
|
|
|
std::vector<std::fs::path> paths;
|
2022-01-24 20:53:17 +01:00
|
|
|
|
2022-08-02 22:55:01 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
|
2023-10-29 19:43:40 +01:00
|
|
|
// In the portable Windows version, we just use the executable directory
|
|
|
|
// Prevent the use of the AppData folder here
|
2022-08-02 22:55:01 +02:00
|
|
|
if (!ImHexApi::System::isPortableVersion()) {
|
2022-08-01 14:51:08 +02:00
|
|
|
PWSTR wAppDataPath = nullptr;
|
2022-08-02 22:55:01 +02:00
|
|
|
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, nullptr, &wAppDataPath))) {
|
|
|
|
paths.emplace_back(wAppDataPath);
|
|
|
|
CoTaskMemFree(wAppDataPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(OS_MACOS)
|
2022-01-24 20:53:17 +01:00
|
|
|
|
2023-03-13 09:25:07 +01:00
|
|
|
paths.push_back(wolv::io::fs::getApplicationSupportDirectoryPath());
|
2022-03-22 09:34:26 +01:00
|
|
|
|
2023-10-04 16:10:14 +02:00
|
|
|
#elif defined(OS_LINUX) || defined(OS_WEB)
|
2022-01-24 20:53:17 +01:00
|
|
|
|
2022-08-03 19:52:02 +02:00
|
|
|
paths.push_back(xdg::DataHomeDir());
|
2022-08-02 22:55:01 +02:00
|
|
|
|
2022-08-03 19:52:02 +02:00
|
|
|
auto dataDirs = xdg::DataDirs();
|
2022-08-02 22:55:01 +02:00
|
|
|
std::copy(dataDirs.begin(), dataDirs.end(), std::back_inserter(paths));
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-08-04 08:56:58 +02:00
|
|
|
#if defined(OS_MACOS)
|
|
|
|
|
2023-03-13 09:25:07 +01:00
|
|
|
if (auto executablePath = wolv::io::fs::getExecutablePath(); executablePath.has_value())
|
2022-08-04 08:56:58 +02:00
|
|
|
paths.push_back(*executablePath);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2023-08-03 21:49:41 +02:00
|
|
|
for (auto &path : paths)
|
|
|
|
path = path / "imhex";
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
if (auto executablePath = wolv::io::fs::getExecutablePath(); executablePath.has_value())
|
2022-08-04 08:56:58 +02:00
|
|
|
paths.push_back(executablePath->parent_path());
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-08-02 22:55:01 +02:00
|
|
|
|
2023-10-29 19:43:40 +01:00
|
|
|
// Add additional data directories to the path
|
2022-08-02 22:55:01 +02:00
|
|
|
auto additionalDirs = ImHexApi::System::getAdditionalFolderPaths();
|
|
|
|
std::copy(additionalDirs.begin(), additionalDirs.end(), std::back_inserter(paths));
|
2022-03-22 09:34:26 +01:00
|
|
|
|
2023-10-29 19:43:40 +01:00
|
|
|
// Add the project file directory to the path, if one is loaded
|
2023-01-11 23:31:25 +01:00
|
|
|
if (ProjectFile::hasPath()) {
|
|
|
|
paths.push_back(ProjectFile::getPath().parent_path());
|
|
|
|
}
|
|
|
|
|
2022-08-02 22:55:01 +02:00
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::vector<std::fs::path> getConfigPaths() {
|
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
return getDataPaths();
|
|
|
|
#elif defined(OS_MACOS)
|
|
|
|
return getDataPaths();
|
2023-10-04 12:00:32 +02:00
|
|
|
#elif defined(OS_LINUX) || defined(OS_WEB)
|
2023-03-26 11:02:23 +02:00
|
|
|
return {xdg::ConfigHomeDir() / "imhex"};
|
2022-08-02 22:55:01 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-01-01 01:01:24 +01:00
|
|
|
std::vector<std::fs::path> appendPath(std::vector<std::fs::path> paths, const std::fs::path &folder) {
|
2022-08-04 20:37:57 +02:00
|
|
|
for (auto &path : paths)
|
|
|
|
path = path / folder;
|
2022-08-02 22:55:01 +02:00
|
|
|
|
2022-08-04 20:37:57 +02:00
|
|
|
return paths;
|
2022-10-02 17:30:26 +02:00
|
|
|
}
|
2022-08-04 20:37:57 +02:00
|
|
|
|
|
|
|
std::vector<std::fs::path> getPluginPaths() {
|
|
|
|
std::vector<std::fs::path> paths = getDataPaths();
|
2023-10-29 19:43:40 +01:00
|
|
|
|
|
|
|
// Add the system plugin directory to the path if one was provided at compile time
|
2022-08-04 20:37:57 +02:00
|
|
|
#if defined(OS_LINUX) && defined(SYSTEM_PLUGINS_LOCATION)
|
2023-10-29 19:43:40 +01:00
|
|
|
paths.push_back(SYSTEM_PLUGINS_LOCATION);
|
2022-08-04 20:37:57 +02:00
|
|
|
#endif
|
2023-10-29 19:43:40 +01:00
|
|
|
|
2022-08-04 20:37:57 +02:00
|
|
|
return paths;
|
|
|
|
}
|
2022-08-02 22:55:01 +02:00
|
|
|
|
|
|
|
|
2022-08-04 20:37:57 +02:00
|
|
|
std::vector<std::fs::path> getDefaultPaths(ImHexPath path, bool listNonExisting) {
|
|
|
|
std::vector<std::fs::path> result;
|
2022-01-24 20:53:17 +01:00
|
|
|
|
2023-10-29 19:43:40 +01:00
|
|
|
// Return the correct path based on the ImHexPath enum
|
2022-01-24 20:53:17 +01:00
|
|
|
switch (path) {
|
2022-10-02 14:18:40 +02:00
|
|
|
case ImHexPath::END:
|
|
|
|
return { };
|
2022-08-02 22:55:01 +02:00
|
|
|
case ImHexPath::Constants:
|
|
|
|
result = appendPath(getDataPaths(), "constants");
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
2022-08-02 22:55:01 +02:00
|
|
|
case ImHexPath::Config:
|
|
|
|
result = appendPath(getConfigPaths(), "config");
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
2022-08-02 22:55:01 +02:00
|
|
|
case ImHexPath::Encodings:
|
|
|
|
result = appendPath(getDataPaths(), "encodings");
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
2022-08-02 22:55:01 +02:00
|
|
|
case ImHexPath::Logs:
|
2023-03-26 11:02:23 +02:00
|
|
|
result = appendPath(getDataPaths(), "logs");
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
|
|
|
case ImHexPath::Plugins:
|
2022-08-04 20:37:57 +02:00
|
|
|
result = appendPath(getPluginPaths(), "plugins");
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
2023-01-07 10:32:01 +01:00
|
|
|
case ImHexPath::Libraries:
|
|
|
|
result = appendPath(getPluginPaths(), "lib");
|
|
|
|
break;
|
2022-02-01 22:09:44 +01:00
|
|
|
case ImHexPath::Resources:
|
2022-08-02 22:55:01 +02:00
|
|
|
result = appendPath(getDataPaths(), "resources");
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
2022-08-02 22:55:01 +02:00
|
|
|
case ImHexPath::Magic:
|
|
|
|
result = appendPath(getDataPaths(), "magic");
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
|
|
|
case ImHexPath::Patterns:
|
2022-08-02 22:55:01 +02:00
|
|
|
result = appendPath(getDataPaths(), "patterns");
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
|
|
|
case ImHexPath::PatternsInclude:
|
2022-08-02 22:55:01 +02:00
|
|
|
result = appendPath(getDataPaths(), "includes");
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
|
|
|
case ImHexPath::Yara:
|
2022-08-02 22:55:01 +02:00
|
|
|
result = appendPath(getDataPaths(), "yara");
|
2022-02-01 22:09:44 +01:00
|
|
|
break;
|
2022-08-14 10:07:45 +02:00
|
|
|
case ImHexPath::Recent:
|
|
|
|
result = appendPath(getConfigPaths(), "recent");
|
|
|
|
break;
|
2022-10-02 14:18:40 +02:00
|
|
|
case ImHexPath::Scripts:
|
|
|
|
result = appendPath(getDataPaths(), "scripts");
|
|
|
|
break;
|
|
|
|
case ImHexPath::Inspectors:
|
|
|
|
result = appendPath(getDefaultPaths(ImHexPath::Scripts), "inspectors");
|
|
|
|
break;
|
2023-02-09 23:07:04 +01:00
|
|
|
case ImHexPath::Nodes:
|
|
|
|
result = appendPath(getDefaultPaths(ImHexPath::Scripts), "nodes");
|
|
|
|
break;
|
2022-12-29 19:26:00 +01:00
|
|
|
case ImHexPath::Themes:
|
|
|
|
result = appendPath(getDataPaths(), "themes");
|
|
|
|
break;
|
2023-05-11 18:44:50 +02:00
|
|
|
case ImHexPath::Layouts:
|
|
|
|
result = appendPath(getDataPaths(), "layouts");
|
|
|
|
break;
|
2022-01-24 20:53:17 +01:00
|
|
|
}
|
2022-02-21 22:47:56 +01:00
|
|
|
|
2023-10-29 19:43:40 +01:00
|
|
|
// Remove all paths that don't exist if requested
|
2022-01-12 09:02:03 +01:00
|
|
|
if (!listNonExisting) {
|
2022-01-24 20:53:17 +01:00
|
|
|
result.erase(std::remove_if(result.begin(), result.end(), [](const auto &path) {
|
2023-03-12 18:27:29 +01:00
|
|
|
return !wolv::io::fs::isDirectory(path);
|
2022-08-02 22:55:01 +02:00
|
|
|
}), result.end());
|
2022-01-12 09:02:03 +01:00
|
|
|
}
|
2021-12-15 00:21:34 +01:00
|
|
|
|
|
|
|
return result;
|
2021-09-03 02:33:15 +02:00
|
|
|
}
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
bool isPathWritable(const std::fs::path &path) {
|
|
|
|
constexpr static auto TestFileName = "__imhex__tmp__";
|
2023-10-29 19:43:40 +01:00
|
|
|
|
|
|
|
// Try to open the __imhex__tmp__ file in the given path
|
|
|
|
// If one does exist already, try to delete it
|
2023-03-12 18:27:29 +01:00
|
|
|
{
|
|
|
|
wolv::io::File file(path / TestFileName, wolv::io::File::Mode::Read);
|
|
|
|
if (file.isValid()) {
|
|
|
|
if (!file.remove())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-29 19:43:40 +01:00
|
|
|
// Try to create a new file in the given path
|
|
|
|
// If that fails, or the file cannot be deleted anymore afterward; the path is not writable
|
2023-03-12 18:27:29 +01:00
|
|
|
wolv::io::File file(path / TestFileName, wolv::io::File::Mode::Create);
|
|
|
|
bool result = file.isValid();
|
|
|
|
if (!file.remove())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-06-30 19:39:06 +02:00
|
|
|
std::fs::path toShortPath(const std::fs::path &path) {
|
|
|
|
#if defined(OS_WINDOWS)
|
2023-10-29 19:43:40 +01:00
|
|
|
// Get the size of the short path
|
2022-10-30 13:39:40 +01:00
|
|
|
size_t size = GetShortPathNameW(path.c_str(), nullptr, 0);
|
2022-06-30 19:39:06 +02:00
|
|
|
if (size == 0)
|
|
|
|
return path;
|
|
|
|
|
2023-10-29 19:43:40 +01:00
|
|
|
// Get the short path
|
2022-06-30 19:39:06 +02:00
|
|
|
std::wstring newPath(size, 0x00);
|
|
|
|
GetShortPathNameW(path.c_str(), newPath.data(), newPath.size());
|
2022-10-30 13:39:40 +01:00
|
|
|
newPath.pop_back();
|
2022-06-30 19:39:06 +02:00
|
|
|
|
|
|
|
return newPath;
|
|
|
|
#else
|
2023-10-29 19:43:40 +01:00
|
|
|
// Other supported platforms don't have short paths
|
2022-06-30 19:39:06 +02:00
|
|
|
return path;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-18 22:34:54 +01:00
|
|
|
}
|