2022-08-08 21:23:52 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
#include <hex/api/imhex_api.hpp>
|
|
|
|
#include <hex/api/event.hpp>
|
|
|
|
|
|
|
|
#include <hex/helpers/fs.hpp>
|
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
#include <hex/helpers/concepts.hpp>
|
|
|
|
#include <hex/helpers/tar.hpp>
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
/**
|
|
|
|
* @brief Project file manager
|
|
|
|
*
|
|
|
|
* The project file manager is used to load and store project files. It is used by all features of ImHex
|
|
|
|
* that want to store any data to a Project File.
|
|
|
|
*
|
|
|
|
*/
|
2022-08-08 21:23:52 +02:00
|
|
|
class ProjectFile {
|
|
|
|
public:
|
|
|
|
struct Handler {
|
|
|
|
using Function = std::function<bool(const std::fs::path &, Tar &tar)>;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
std::fs::path basePath; //< Base path for where to store the files in the project file
|
|
|
|
bool required; //< If true, ImHex will display an error if this handler fails to load or store data
|
|
|
|
Function load, store; //< Functions to load and store data
|
2022-08-08 21:23:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ProviderHandler {
|
|
|
|
using Function = std::function<bool(prv::Provider *provider, const std::fs::path &, Tar &tar)>;
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
std::fs::path basePath; //< Base path for where to store the files in the project file
|
|
|
|
bool required; //< If true, ImHex will display an error if this handler fails to load or store data
|
|
|
|
Function load, store; //< Functions to load and store data
|
2022-08-08 21:23:52 +02:00
|
|
|
};
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
/**
|
|
|
|
* @brief Load a project file
|
|
|
|
*
|
|
|
|
* @param filePath Path to the project file
|
|
|
|
* @return true if the project file was loaded successfully
|
|
|
|
* @return false if the project file was not loaded successfully
|
|
|
|
*/
|
2022-08-08 21:23:52 +02:00
|
|
|
static bool load(const std::fs::path &filePath);
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Store a project file
|
|
|
|
*
|
|
|
|
* @param filePath Path to the project file
|
|
|
|
* @return true if the project file was stored successfully
|
|
|
|
* @return false if the project file was not stored successfully
|
|
|
|
*/
|
2022-08-08 21:23:52 +02:00
|
|
|
static bool store(std::optional<std::fs::path> filePath = std::nullopt);
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
/**
|
|
|
|
* @brief Check if a project file is currently loaded
|
|
|
|
*
|
|
|
|
* @return true if a project file is currently loaded
|
|
|
|
* @return false if no project file is currently loaded
|
|
|
|
*/
|
2022-11-25 10:47:11 +01:00
|
|
|
static bool hasPath();
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Clear the currently loaded project file
|
|
|
|
*/
|
2023-01-07 17:31:22 +01:00
|
|
|
static void clearPath();
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the path to the currently loaded project file
|
|
|
|
* @return Path to the currently loaded project file
|
|
|
|
*/
|
2023-01-07 17:31:22 +01:00
|
|
|
static std::fs::path getPath();
|
2023-03-21 15:33:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the path to the currently loaded project file
|
|
|
|
* @param path Path to the currently loaded project file
|
|
|
|
*/
|
2023-02-12 17:52:09 +01:00
|
|
|
static void setPath(const std::fs::path &path);
|
2022-11-25 10:47:11 +01:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
/**
|
|
|
|
* @brief Register a handler for storing and loading global data from a project file
|
|
|
|
*
|
|
|
|
* @param handler The handler to register
|
|
|
|
*/
|
2022-08-08 21:23:52 +02:00
|
|
|
static void registerHandler(const Handler &handler) {
|
|
|
|
getHandlers().push_back(handler);
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
/**
|
|
|
|
* @brief Register a handler for storing and loading per-provider data from a project file
|
|
|
|
*
|
|
|
|
* @param handler The handler to register
|
|
|
|
*/
|
2022-08-08 21:23:52 +02:00
|
|
|
static void registerPerProviderHandler(const ProviderHandler &handler) {
|
|
|
|
getProviderHandlers().push_back(handler);
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
/**
|
|
|
|
* @brief Get the list of registered handlers
|
|
|
|
* @return List of registered handlers
|
|
|
|
*/
|
2022-08-08 21:23:52 +02:00
|
|
|
static std::vector<Handler>& getHandlers() {
|
|
|
|
return s_handlers;
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
/**
|
|
|
|
* @brief Get the list of registered per-provider handlers
|
|
|
|
* @return List of registered per-provider handlers
|
|
|
|
*/
|
2022-08-08 21:23:52 +02:00
|
|
|
static std::vector<ProviderHandler>& getProviderHandlers() {
|
|
|
|
return s_providerHandlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ProjectFile() = default;
|
|
|
|
|
|
|
|
static std::fs::path s_currProjectPath;
|
|
|
|
static std::vector<Handler> s_handlers;
|
|
|
|
static std::vector<ProviderHandler> s_providerHandlers;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|