2022-08-08 21:23:52 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex/api/imhex_api.hpp>
|
|
|
|
#include <hex/api/event.hpp>
|
|
|
|
|
|
|
|
#include <hex/helpers/fs.hpp>
|
|
|
|
#include <hex/helpers/utils.hpp>
|
|
|
|
#include <hex/helpers/tar.hpp>
|
|
|
|
|
2023-06-21 20:07:36 +02: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
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
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-06-21 20:07:36 +02:00
|
|
|
/**
|
|
|
|
* @brief Set implementations for loading and restoring a project
|
|
|
|
*
|
|
|
|
* @param loadFun function to use to load a project in ImHex
|
|
|
|
* @param storeFun function to use to store a project to disk
|
|
|
|
*/
|
|
|
|
static void setProjectFunctions(
|
|
|
|
const std::function<bool(const std::fs::path&)> &loadFun,
|
2023-07-01 12:32:28 +02:00
|
|
|
const std::function<bool(std::optional<std::fs::path>, bool)> &storeFun
|
2023-06-21 20:07:36 +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
|
2023-07-01 12:32:28 +02:00
|
|
|
* @param updateLocation update the project location so subssequent saves will save there
|
2023-03-21 15:33:43 +01:00
|
|
|
* @return true if the project file was stored successfully
|
|
|
|
* @return false if the project file was not stored successfully
|
|
|
|
*/
|
2023-07-01 12:32:28 +02:00
|
|
|
static bool store(std::optional<std::fs::path> filePath = std::nullopt, bool updateLocation = true);
|
2022-08-08 21:23:52 +02:00
|
|
|
|
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
|
|
|
|
*/
|
2023-07-26 13:50:51 +02:00
|
|
|
static std::vector<Handler>& getHandlers();
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-03-21 15:33:43 +01:00
|
|
|
/**
|
|
|
|
* @brief Get the list of registered per-provider handlers
|
|
|
|
* @return List of registered per-provider handlers
|
|
|
|
*/
|
2023-07-26 13:50:51 +02:00
|
|
|
static std::vector<ProviderHandler>& getProviderHandlers();
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
ProjectFile() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|