Skip to content

Commit

Permalink
fix: Replace old defaults path system with a new one
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Jun 22, 2024
1 parent beef0ff commit b60a262
Show file tree
Hide file tree
Showing 41 changed files with 461 additions and 356 deletions.
1 change: 1 addition & 0 deletions lib/libimhex/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ set(LIBIMHEX_SOURCES
source/helpers/logger.cpp
source/helpers/tar.cpp
source/helpers/debugging.cpp
source/helpers/default_paths.cpp

source/test/tests.cpp

Expand Down
111 changes: 111 additions & 0 deletions lib/libimhex/include/hex/helpers/default_paths.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#pragma once

#include <hex/helpers/fs.hpp>

#include <vector>

namespace hex::paths {

namespace impl {

class DefaultPath {
protected:
constexpr DefaultPath() = default;
virtual ~DefaultPath() = default;

public:
DefaultPath(const DefaultPath&) = delete;
DefaultPath(DefaultPath&&) = delete;
DefaultPath& operator=(const DefaultPath&) = delete;
DefaultPath& operator=(DefaultPath&&) = delete;

virtual std::vector<std::fs::path> all() const = 0;
virtual std::vector<std::fs::path> read() const;
virtual std::vector<std::fs::path> write() const;
};

class ConfigPath : public DefaultPath {
public:
explicit ConfigPath(std::fs::path postfix) : m_postfix(std::move(postfix)) {}

std::vector<std::fs::path> all() const override;

private:
std::fs::path m_postfix;
};

class DataPath : public DefaultPath {
public:
explicit DataPath(std::fs::path postfix) : m_postfix(std::move(postfix)) {}

std::vector<std::fs::path> all() const override;
std::vector<std::fs::path> write() const override;

private:
std::fs::path m_postfix;
};

class PluginPath : public DefaultPath {
public:
explicit PluginPath(std::fs::path postfix) : m_postfix(std::move(postfix)) {}

std::vector<std::fs::path> all() const override;

private:
std::fs::path m_postfix;
};

}

std::vector<std::fs::path> getDataPaths(bool includeSystemFolders);
std::vector<std::fs::path> getConfigPaths(bool includeSystemFolders);

const static inline impl::ConfigPath Config("config");
const static inline impl::ConfigPath Recent("recent");

const static inline impl::PluginPath Libraries("lib");
const static inline impl::PluginPath Plugins("plugins");

const static inline impl::DataPath Patterns("patterns");
const static inline impl::DataPath PatternsInclude("patterns/include");
const static inline impl::DataPath Magic("magic");
const static inline impl::DataPath Yara("yara");
const static inline impl::DataPath YaraAdvancedAnalysis("yara/advanced_analysis");
const static inline impl::DataPath Backups("backups");
const static inline impl::DataPath Resources("resources");
const static inline impl::DataPath Constants("constants");
const static inline impl::DataPath Encodings("encodings");
const static inline impl::DataPath Logs("logs");
const static inline impl::DataPath Scripts("scripts");
const static inline impl::DataPath Inspectors("scripts/inspectors");
const static inline impl::DataPath Themes("themes");
const static inline impl::DataPath Nodes("scripts/nodes");
const static inline impl::DataPath Layouts("layouts");
const static inline impl::DataPath Workspaces("workspaces");

constexpr static inline std::array<const impl::DefaultPath*, 20> All = {
&Config,
&Recent,

&Libraries,
&Plugins,

&Patterns,
&PatternsInclude,
&Magic,
&Yara,
&YaraAdvancedAnalysis,
&Backups,
&Resources,
&Constants,
&Encodings,
&Logs,
&Scripts,
&Inspectors,
&Themes,
&Nodes,
&Layouts,
&Workspaces,
};

}
31 changes: 0 additions & 31 deletions lib/libimhex/include/hex/helpers/fs.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include <hex.hpp>

#include <string>
#include <vector>
#include <filesystem>
Expand Down Expand Up @@ -31,36 +29,7 @@ namespace hex::fs {
void openFolderExternal(const std::fs::path &dirPath);
void openFolderWithSelectionExternal(const std::fs::path &selectedFilePath);

enum class ImHexPath : u32 {
Patterns = 0,
PatternsInclude,
Magic,
Plugins,
Yara,
YaraAdvancedAnalysis,
Config,
Backups,
Resources,
Constants,
Encodings,
Logs,
Recent,
Scripts,
Inspectors,
Themes,
Libraries,
Nodes,
Layouts,
Workspaces,

END
};

bool isPathWritable(const std::fs::path &path);

std::vector<std::fs::path> getDefaultPaths(ImHexPath path, bool listNonExisting = false);

// Temporarily expose these for the migration function
std::vector<std::fs::path> getDataPaths();
std::vector<std::fs::path> appendPath(std::vector<std::fs::path> paths, const std::fs::path &folder);
}
5 changes: 3 additions & 2 deletions lib/libimhex/source/api/achievement_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <hex/api/event_manager.hpp>

#include <hex/helpers/auto_reset.hpp>
#include <hex/helpers/default_paths.hpp>

#include <nlohmann/json.hpp>

Expand Down Expand Up @@ -197,7 +198,7 @@ namespace hex {
constexpr static auto AchievementsFile = "achievements.json";

void AchievementManager::loadProgress() {
for (const auto &directory : fs::getDefaultPaths(fs::ImHexPath::Config)) {
for (const auto &directory : paths::Config.read()) {
auto path = directory / AchievementsFile;

if (!wolv::io::fs::exists(path)) {
Expand Down Expand Up @@ -246,7 +247,7 @@ namespace hex {
if (json.empty())
return;

for (const auto &directory : fs::getDefaultPaths(fs::ImHexPath::Config)) {
for (const auto &directory : paths::Config.write()) {
auto path = directory / AchievementsFile;

wolv::io::File file(path, wolv::io::File::Mode::Create);
Expand Down
9 changes: 5 additions & 4 deletions lib/libimhex/source/api/content_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <hex/helpers/fs.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/helpers/auto_reset.hpp>
#include <hex/helpers/default_paths.hpp>

#include <hex/ui/view.hpp>
#include <hex/data_processor/node.hpp>
Expand Down Expand Up @@ -101,7 +102,7 @@ namespace hex {

void load() {
bool loaded = false;
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) {
for (const auto &dir : paths::Config.read()) {
wolv::io::File file(dir / SettingsFile, wolv::io::File::Mode::Read);

if (file.isValid()) {
Expand Down Expand Up @@ -142,7 +143,7 @@ namespace hex {
if (result.empty()) {
return;
}
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) {
for (const auto &dir : paths::Config.write()) {
wolv::io::File file(dir / SettingsFile, wolv::io::File::Mode::Create);

if (file.isValid()) {
Expand All @@ -153,7 +154,7 @@ namespace hex {
}

void clear() {
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) {
for (const auto &dir : paths::Config.write()) {
wolv::io::fs::remove(dir / SettingsFile);
}
}
Expand Down Expand Up @@ -595,7 +596,7 @@ namespace hex {
);
}

runtime.setIncludePaths(fs::getDefaultPaths(fs::ImHexPath::PatternsInclude) | fs::getDefaultPaths(fs::ImHexPath::Patterns));
runtime.setIncludePaths(paths::PatternsInclude.read() | paths::Patterns.read());

for (const auto &[ns, name, paramCount, callback, dangerous] : impl::getFunctions()) {
if (dangerous)
Expand Down
13 changes: 6 additions & 7 deletions lib/libimhex/source/api/layout_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include <hex/api/layout_manager.hpp>

#include <hex/api/content_registry.hpp>
#include <hex/ui/view.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/helpers/auto_reset.hpp>
#include <hex/helpers/default_paths.hpp>

#include <wolv/utils/string.hpp>

#include <imgui.h>
#include <hex/api/content_registry.hpp>
#include <hex/ui/view.hpp>

namespace hex {

Expand Down Expand Up @@ -40,10 +42,7 @@ namespace hex {
fileName += ".hexlyt";

std::fs::path layoutPath;
for (const auto &path : hex::fs::getDefaultPaths(fs::ImHexPath::Layouts)) {
if (!hex::fs::isPathWritable(path))
continue;

for (const auto &path : paths::Layouts.write()) {
layoutPath = path / fileName;
}

Expand Down Expand Up @@ -109,7 +108,7 @@ namespace hex {
void LayoutManager::reload() {
s_layouts->clear();

for (const auto &directory : hex::fs::getDefaultPaths(fs::ImHexPath::Layouts)) {
for (const auto &directory : paths::Layouts.read()) {
for (const auto &entry : std::fs::directory_iterator(directory)) {
const auto &path = entry.path();

Expand Down
3 changes: 2 additions & 1 deletion lib/libimhex/source/api/plugin_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <hex/helpers/fmt.hpp>
#include <hex/helpers/auto_reset.hpp>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/default_paths.hpp>

#include <wolv/utils/string.hpp>

Expand Down Expand Up @@ -306,7 +307,7 @@ namespace hex {

bool PluginManager::loadLibraries() {
bool success = true;
for (const auto &loadPath : fs::getDefaultPaths(fs::ImHexPath::Libraries))
for (const auto &loadPath : paths::Libraries.read())
success = PluginManager::loadLibraries(loadPath) && success;

return success;
Expand Down
5 changes: 3 additions & 2 deletions lib/libimhex/source/api/workspace_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <hex/helpers/logger.hpp>
#include <hex/helpers/auto_reset.hpp>
#include <hex/helpers/default_paths.hpp>

#include <wolv/io/file.hpp>

Expand All @@ -25,7 +26,7 @@ namespace hex {
.builtin = false
}).first;

for (const auto &workspaceFolder : fs::getDefaultPaths(fs::ImHexPath::Workspaces)) {
for (const auto &workspaceFolder : paths::Workspaces.write()) {
const auto workspacePath = workspaceFolder / (name + ".hexws");
if (exportToFile(workspacePath)) {
s_currentWorkspace->second.path = workspacePath;
Expand Down Expand Up @@ -157,7 +158,7 @@ namespace hex {
void WorkspaceManager::reload() {
WorkspaceManager::reset();

for (const auto &defaultPath : fs::getDefaultPaths(fs::ImHexPath::Workspaces)) {
for (const auto &defaultPath : paths::Workspaces.read()) {
for (const auto &entry : std::fs::directory_iterator(defaultPath)) {
if (!entry.is_regular_file()) {
continue;
Expand Down
Loading

0 comments on commit b60a262

Please sign in to comment.