2023-12-11 15:54:22 +01:00
|
|
|
#include <hex/api/workspace_manager.hpp>
|
|
|
|
#include <hex/api/layout_manager.hpp>
|
|
|
|
|
|
|
|
#include <hex/helpers/logger.hpp>
|
|
|
|
|
|
|
|
#include <wolv/io/file.hpp>
|
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
2023-12-12 00:16:21 +01:00
|
|
|
|
|
|
|
#include <imgui.h>
|
2023-12-11 15:54:22 +01:00
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
std::map<std::string, WorkspaceManager::Workspace> WorkspaceManager::s_workspaces;
|
2023-12-14 09:36:29 +01:00
|
|
|
decltype(WorkspaceManager::s_workspaces)::iterator WorkspaceManager::s_currentWorkspace = s_workspaces.end();
|
|
|
|
decltype(WorkspaceManager::s_workspaces)::iterator WorkspaceManager::s_previousWorkspace = s_workspaces.end();
|
2023-12-11 15:54:22 +01:00
|
|
|
|
|
|
|
void WorkspaceManager::createWorkspace(const std::string& name, const std::string &layout) {
|
|
|
|
s_workspaces[name] = Workspace {
|
|
|
|
.layout = layout.empty() ? LayoutManager::saveToString() : layout,
|
|
|
|
.path = {}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Workspaces)) {
|
2023-12-14 09:36:29 +01:00
|
|
|
if (exportToFile(path / (name + ".hexws")))
|
2023-12-11 15:54:22 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorkspaceManager::switchWorkspace(const std::string& name) {
|
2023-12-14 09:36:29 +01:00
|
|
|
const auto newWorkspace = s_workspaces.find(name);
|
2023-12-12 00:16:21 +01:00
|
|
|
if (newWorkspace != s_workspaces.end()) {
|
|
|
|
s_currentWorkspace = newWorkspace;
|
|
|
|
log::info("Switching to workspace '{}'", name);
|
2023-12-11 15:54:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorkspaceManager::importFromFile(const std::fs::path& path) {
|
|
|
|
wolv::io::File file(path, wolv::io::File::Mode::Read);
|
|
|
|
if (!file.isValid()) {
|
|
|
|
log::error("Failed to load workspace from file '{}'", path.string());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto content = file.readString();
|
|
|
|
try {
|
|
|
|
auto json = nlohmann::json::parse(content.begin(), content.end());
|
|
|
|
|
|
|
|
std::string name = json["name"];
|
|
|
|
std::string layout = json["layout"];
|
|
|
|
|
|
|
|
s_workspaces[name] = Workspace {
|
|
|
|
.layout = std::move(layout),
|
|
|
|
.path = path
|
|
|
|
};
|
|
|
|
} catch (nlohmann::json::exception &e) {
|
|
|
|
log::error("Failed to load workspace from file '{}': {}", path.string(), e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-14 09:36:29 +01:00
|
|
|
bool WorkspaceManager::exportToFile(std::fs::path path, std::string workspaceName) {
|
2023-12-11 15:54:22 +01:00
|
|
|
if (path.empty()) {
|
|
|
|
if (s_currentWorkspace == s_workspaces.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
path = s_currentWorkspace->second.path;
|
|
|
|
}
|
|
|
|
|
2023-12-14 09:36:29 +01:00
|
|
|
if (workspaceName.empty())
|
|
|
|
workspaceName = s_currentWorkspace->first;
|
|
|
|
|
2023-12-11 15:54:22 +01:00
|
|
|
wolv::io::File file(path, wolv::io::File::Mode::Create);
|
|
|
|
|
|
|
|
if (!file.isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
nlohmann::json json;
|
2023-12-14 09:36:29 +01:00
|
|
|
json["name"] = workspaceName;
|
|
|
|
json["layout"] = LayoutManager::saveToString();
|
2023-12-11 15:54:22 +01:00
|
|
|
|
|
|
|
file.writeString(json.dump(4));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-12 00:16:21 +01:00
|
|
|
void WorkspaceManager::process() {
|
|
|
|
if (s_previousWorkspace != s_currentWorkspace) {
|
|
|
|
if (s_previousWorkspace != s_workspaces.end())
|
2023-12-14 09:36:29 +01:00
|
|
|
exportToFile(s_previousWorkspace->second.path, s_previousWorkspace->first);
|
2023-12-12 00:16:21 +01:00
|
|
|
|
2023-12-24 14:51:47 +01:00
|
|
|
LayoutManager::closeAllViews();
|
2023-12-12 00:16:21 +01:00
|
|
|
ImGui::LoadIniSettingsFromMemory(s_currentWorkspace->second.layout.c_str());
|
|
|
|
|
|
|
|
s_previousWorkspace = s_currentWorkspace;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-11 15:54:22 +01:00
|
|
|
void WorkspaceManager::reset() {
|
|
|
|
s_workspaces.clear();
|
2023-12-14 09:36:29 +01:00
|
|
|
s_currentWorkspace = s_workspaces.end();
|
|
|
|
s_previousWorkspace = s_workspaces.end();
|
2023-12-11 15:54:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|