1
0
mirror of synced 2024-12-04 12:07:57 +01:00
ImHex/lib/libimhex/source/helpers/project_file_handler.cpp

115 lines
3.8 KiB
C++
Raw Normal View History

2021-12-07 22:47:41 +01:00
#include <hex/helpers/project_file_handler.hpp>
2020-11-30 00:03:12 +01:00
build/plugins: Added initial support for Rust plugins (#327) * build: Added initial support for Rust plugins * github: Install correct rust version * github: Fixed rustup command * github: Fix swapped win/linux commands * github: Install linux rust toolchain on Linux * github: Add rustup parameters to correct command * build: libimhex-rust -> hex * rust-plugins: Disable optimization to export functions correctly * build: Use cdylib instead of dylib * build: Fixed rust building and artifact copying * build: Fixed installing plugins * build: Fix copying and installing on Windows * github: Added windows debugging * github: Use curl instead of wget * github: Added debug on failure * github: Update path variable with rust toolchain path * build/github: Set rust location so cmake can find it * build: Remove leftovers * api: Added rust wrappers for the ImHexAPI * rust: Fixed compile flags with older gcc/clang * build: Enable concepts for cxx.rs * build: Explicitly set compiler for cxx.rs * rust: Added imgui-rs to libimhex-rust * rust: Export functions with double underscore prefix on mac * rust: Export functions adjusted for ABI * Add Rust target folder to gitignore * Add vendored imgui-rs copy * Add Context::current() to vendored imgui-rs * Fix libimhex not exporting cimgui symbols * Simplify plugin export mangling * build: Fixed cimgui linking * build: Only specify --export-all-symbols on Windows * Add context setting to Rust plugins * rust: Cleanup * deps: Update curl Co-authored-by: jam1garner <8260240+jam1garner@users.noreply.github.com>
2021-10-16 11:37:29 +02:00
#include <hex/api/imhex_api.hpp>
2020-11-30 00:03:12 +01:00
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace hex {
fs::path ProjectFile::s_currProjectFilePath;
2021-12-16 10:07:31 +01:00
bool ProjectFile::s_hasUnsavedChanged = false;
fs::path ProjectFile::s_filePath;
2021-12-16 10:07:31 +01:00
std::string ProjectFile::s_pattern;
Patches ProjectFile::s_patches;
std::list<ImHexApi::Bookmarks::Entry> ProjectFile::s_bookmarks;
std::string ProjectFile::s_dataProcessorContent;
void to_json(json &j, const ImHexApi::Bookmarks::Entry &b) {
j = json {
{"address", b.region.address},
{ "size", b.region.size },
{ "name", b.name.data() },
{ "comment", b.comment.data()},
{ "locked", b.locked },
{ "color", b.color }
};
2020-11-30 00:03:12 +01:00
}
void from_json(const json &j, ImHexApi::Bookmarks::Entry &b) {
2020-11-30 00:03:12 +01:00
std::string name, comment;
2021-12-16 10:07:31 +01:00
if (j.contains("address")) j.at("address").get_to(b.region.address);
if (j.contains("size")) j.at("size").get_to(b.region.size);
if (j.contains("name")) j.at("name").get_to(name);
if (j.contains("comment")) j.at("comment").get_to(comment);
if (j.contains("locked")) j.at("locked").get_to(b.locked);
if (j.contains("color")) j.at("color").get_to(b.color);
2020-11-30 00:03:12 +01:00
std::copy(name.begin(), name.end(), std::back_inserter(b.name));
b.name.push_back('\0');
2020-11-30 00:03:12 +01:00
std::copy(comment.begin(), comment.end(), std::back_inserter(b.comment));
b.comment.push_back('\0');
2020-11-30 00:03:12 +01:00
}
bool ProjectFile::load(const fs::path &filePath) {
2020-11-30 00:03:12 +01:00
ProjectFile::s_hasUnsavedChanged = false;
json projectFileData;
try {
std::ifstream projectFile(filePath.c_str());
2020-11-30 00:03:12 +01:00
projectFile >> projectFileData;
ProjectFile::s_filePath = fs::path(projectFileData["filePath"].get<std::string>());
ProjectFile::s_pattern = projectFileData["pattern"];
ProjectFile::s_patches = projectFileData["patches"].get<Patches>();
ProjectFile::s_dataProcessorContent = projectFileData["dataProcessor"];
2020-11-30 00:03:12 +01:00
ProjectFile::s_bookmarks.clear();
2020-11-30 00:03:12 +01:00
for (auto &element : projectFileData["bookmarks"].items()) {
build/plugins: Added initial support for Rust plugins (#327) * build: Added initial support for Rust plugins * github: Install correct rust version * github: Fixed rustup command * github: Fix swapped win/linux commands * github: Install linux rust toolchain on Linux * github: Add rustup parameters to correct command * build: libimhex-rust -> hex * rust-plugins: Disable optimization to export functions correctly * build: Use cdylib instead of dylib * build: Fixed rust building and artifact copying * build: Fixed installing plugins * build: Fix copying and installing on Windows * github: Added windows debugging * github: Use curl instead of wget * github: Added debug on failure * github: Update path variable with rust toolchain path * build/github: Set rust location so cmake can find it * build: Remove leftovers * api: Added rust wrappers for the ImHexAPI * rust: Fixed compile flags with older gcc/clang * build: Enable concepts for cxx.rs * build: Explicitly set compiler for cxx.rs * rust: Added imgui-rs to libimhex-rust * rust: Export functions with double underscore prefix on mac * rust: Export functions adjusted for ABI * Add Rust target folder to gitignore * Add vendored imgui-rs copy * Add Context::current() to vendored imgui-rs * Fix libimhex not exporting cimgui symbols * Simplify plugin export mangling * build: Fixed cimgui linking * build: Only specify --export-all-symbols on Windows * Add context setting to Rust plugins * rust: Cleanup * deps: Update curl Co-authored-by: jam1garner <8260240+jam1garner@users.noreply.github.com>
2021-10-16 11:37:29 +02:00
ImHexApi::Bookmarks::Entry entry;
from_json(element.value(), entry);
ProjectFile::s_bookmarks.push_back(entry);
2020-11-30 00:03:12 +01:00
}
} catch (json::exception &e) {
return false;
} catch (std::ofstream::failure &e) {
return false;
}
ProjectFile::s_currProjectFilePath = filePath;
EventManager::post<EventProjectFileLoad>();
2020-11-30 00:03:12 +01:00
return true;
}
bool ProjectFile::store(fs::path filePath) {
2021-08-17 13:41:19 +02:00
EventManager::post<EventProjectFileStore>();
2020-11-30 00:03:12 +01:00
json projectFileData;
if (filePath.empty())
filePath = ProjectFile::s_currProjectFilePath;
try {
projectFileData["filePath"] = ProjectFile::s_filePath;
projectFileData["pattern"] = ProjectFile::s_pattern;
projectFileData["patches"] = ProjectFile::s_patches;
projectFileData["dataProcessor"] = ProjectFile::s_dataProcessorContent;
2020-11-30 00:03:12 +01:00
for (auto &bookmark : ProjectFile::s_bookmarks) {
build/plugins: Added initial support for Rust plugins (#327) * build: Added initial support for Rust plugins * github: Install correct rust version * github: Fixed rustup command * github: Fix swapped win/linux commands * github: Install linux rust toolchain on Linux * github: Add rustup parameters to correct command * build: libimhex-rust -> hex * rust-plugins: Disable optimization to export functions correctly * build: Use cdylib instead of dylib * build: Fixed rust building and artifact copying * build: Fixed installing plugins * build: Fix copying and installing on Windows * github: Added windows debugging * github: Use curl instead of wget * github: Added debug on failure * github: Update path variable with rust toolchain path * build/github: Set rust location so cmake can find it * build: Remove leftovers * api: Added rust wrappers for the ImHexAPI * rust: Fixed compile flags with older gcc/clang * build: Enable concepts for cxx.rs * build: Explicitly set compiler for cxx.rs * rust: Added imgui-rs to libimhex-rust * rust: Export functions with double underscore prefix on mac * rust: Export functions adjusted for ABI * Add Rust target folder to gitignore * Add vendored imgui-rs copy * Add Context::current() to vendored imgui-rs * Fix libimhex not exporting cimgui symbols * Simplify plugin export mangling * build: Fixed cimgui linking * build: Only specify --export-all-symbols on Windows * Add context setting to Rust plugins * rust: Cleanup * deps: Update curl Co-authored-by: jam1garner <8260240+jam1garner@users.noreply.github.com>
2021-10-16 11:37:29 +02:00
to_json(projectFileData["bookmarks"].emplace_back(), bookmark);
2020-11-30 00:03:12 +01:00
}
std::ofstream projectFile(filePath.c_str(), std::fstream::trunc);
2020-11-30 00:03:12 +01:00
projectFile << projectFileData;
} catch (json::exception &e) {
return false;
} catch (std::ifstream::failure &e) {
return false;
}
2021-08-17 13:41:19 +02:00
ProjectFile::s_hasUnsavedChanged = false;
2020-11-30 00:03:12 +01:00
ProjectFile::s_currProjectFilePath = filePath;
return true;
}
}