mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 21:47:40 -05:00
sys: Tons of long overdue cleanup
- std::string -> const std::string& where needed - Added a FileIO abstraction class - Fixed recent files not updating - Removed localization file from global include - Renamed lang to pattern_language/pl - Renamed EventFileDropped to RequestFileOpen
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
#include "helpers/encoding_file.hpp"
|
||||
|
||||
#include <ranges>
|
||||
#include <hex/helpers/utils.hpp>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace hex {
|
||||
|
||||
EncodingFile::EncodingFile(Type type, std::string_view path) {
|
||||
std::ifstream encodingFile(path.data());
|
||||
EncodingFile::EncodingFile(Type type, const std::string &path) {
|
||||
std::ifstream encodingFile(path.c_str());
|
||||
|
||||
switch (type) {
|
||||
case Type::Thingy: parseThingyFile(encodingFile); break;
|
||||
@@ -16,9 +17,7 @@ namespace hex {
|
||||
}
|
||||
|
||||
std::pair<std::string_view, size_t> EncodingFile::getEncodingFor(const std::vector<u8> &buffer) const {
|
||||
for (auto iter = this->m_mapping.rbegin(); iter != this->m_mapping.rend(); iter++) {
|
||||
auto &[size, mapping] = *iter;
|
||||
|
||||
for (const auto &[size, mapping] : this->m_mapping | std::views::reverse) {
|
||||
if (size > buffer.size()) continue;
|
||||
|
||||
auto key = std::vector<u8>(buffer.begin(), buffer.begin() + size);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#include <hex/helpers/paths.hpp>
|
||||
#include <hex/helpers/file.hpp>
|
||||
#include <hex/views/view.hpp>
|
||||
#include <hex/providers/provider.hpp>
|
||||
|
||||
@@ -177,7 +178,7 @@ namespace hex {
|
||||
return createStructureType("union", args);
|
||||
}
|
||||
|
||||
bool LoaderScript::processFile(std::string_view scriptPath) {
|
||||
bool LoaderScript::processFile(const std::string &scriptPath) {
|
||||
Py_SetProgramName(Py_DecodeLocale((SharedData::mainArgv)[0], nullptr));
|
||||
|
||||
for (const auto &dir : hex::getPath(ImHexPath::Python)) {
|
||||
@@ -218,10 +219,8 @@ namespace hex {
|
||||
PyList_Insert(sysPath, 0, path);
|
||||
}
|
||||
|
||||
FILE *scriptFile = fopen(scriptPath.data(), "r");
|
||||
PyRun_SimpleFile(scriptFile, scriptPath.data());
|
||||
|
||||
fclose(scriptFile);
|
||||
File scriptFile(scriptPath, File::Mode::Read);
|
||||
PyRun_SimpleFile(scriptFile.getHandle(), scriptPath.c_str());
|
||||
|
||||
Py_Finalize();
|
||||
|
||||
|
||||
@@ -8,10 +8,8 @@
|
||||
|
||||
namespace hex {
|
||||
|
||||
static void pushBytesBack(std::vector<u8> &buffer, const char* bytes) {
|
||||
std::string_view string(bytes);
|
||||
buffer.resize(buffer.size() + string.length());
|
||||
std::memcpy((&buffer.back() - string.length()) + 1, string.begin(), string.length());
|
||||
static void pushBytesBack(std::vector<u8> &buffer, const std::string &string) {
|
||||
std::copy(string.begin(), string.end(), std::back_inserter(buffer));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace hex {
|
||||
constexpr auto GetPluginDescriptionSymbol = "_ZN3hex6plugin{0}{1}8internal20getPluginDescriptionEv";
|
||||
constexpr auto SetImGuiContextSymbol = "_ZN3hex6plugin{0}{1}8internal15setImGuiContextEP12ImGuiContext";
|
||||
|
||||
Plugin::Plugin(std::string_view path) {
|
||||
Plugin::Plugin(const std::string &path) {
|
||||
this->m_handle = dlopen(path.data(), RTLD_LAZY);
|
||||
|
||||
if (this->m_handle == nullptr) {
|
||||
@@ -84,7 +84,7 @@ namespace hex {
|
||||
this->m_setImGuiContextFunction(ctx);
|
||||
}
|
||||
|
||||
bool PluginManager::load(std::string_view pluginFolder) {
|
||||
bool PluginManager::load(const std::string &pluginFolder) {
|
||||
if (!std::filesystem::exists(pluginFolder))
|
||||
return false;
|
||||
|
||||
|
||||
@@ -27,13 +27,13 @@ namespace hex {
|
||||
}
|
||||
|
||||
|
||||
bool ProjectFile::load(std::string_view filePath) {
|
||||
bool ProjectFile::load(const std::string &filePath) {
|
||||
ProjectFile::s_hasUnsavedChanged = false;
|
||||
|
||||
json projectFileData;
|
||||
|
||||
try {
|
||||
std::ifstream projectFile(filePath.data());
|
||||
std::ifstream projectFile(filePath.c_str());
|
||||
projectFile >> projectFileData;
|
||||
|
||||
ProjectFile::s_filePath = projectFileData["filePath"];
|
||||
@@ -56,7 +56,7 @@ namespace hex {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ProjectFile::store(std::string_view filePath) {
|
||||
bool ProjectFile::store(std::string filePath) {
|
||||
EventManager::post<EventProjectFileStore>();
|
||||
|
||||
json projectFileData;
|
||||
@@ -74,7 +74,7 @@ namespace hex {
|
||||
projectFileData["bookmarks"].push_back(bookmark);
|
||||
}
|
||||
|
||||
std::ofstream projectFile(filePath.data(), std::fstream::trunc);
|
||||
std::ofstream projectFile(filePath.c_str(), std::fstream::trunc);
|
||||
projectFile << projectFileData;
|
||||
} catch (json::exception &e) {
|
||||
return false;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
#include <hex/helpers/net.hpp>
|
||||
#include <hex/api/content_registry.hpp>
|
||||
#include <hex/lang/pattern_data.hpp>
|
||||
#include <hex/pattern_language/pattern_data.hpp>
|
||||
|
||||
#include <fontawesome_font.h>
|
||||
#include <codicons_font.h>
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include "init/splash_window.hpp"
|
||||
#include "init/tasks.hpp"
|
||||
|
||||
#include <hex/helpers/file.hpp>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
using namespace hex;
|
||||
|
||||
@@ -37,7 +39,7 @@ int main(int argc, char **argv) {
|
||||
if (argc == 1)
|
||||
; // No arguments provided
|
||||
else if (argc == 2)
|
||||
EventManager::post<EventFileDropped>(argv[1]);
|
||||
EventManager::post<RequestOpenFile>(argv[1]);
|
||||
else {
|
||||
hex::log::fatal("Usage: imhex [file_name]");
|
||||
return EXIT_FAILURE;
|
||||
|
||||
@@ -1,19 +1,15 @@
|
||||
#include "providers/file_provider.hpp"
|
||||
|
||||
#include <time.h>
|
||||
#include <ctime>
|
||||
#include <cstring>
|
||||
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#include <hex/helpers/file.hpp>
|
||||
#include "helpers/project_file_handler.hpp"
|
||||
|
||||
#if defined(OS_WINDOWS)
|
||||
#include <locale>
|
||||
#include <codecvt>
|
||||
#endif
|
||||
|
||||
namespace hex::prv {
|
||||
|
||||
FileProvider::FileProvider(std::string_view path) : Provider(), m_path(path) {
|
||||
FileProvider::FileProvider(std::string path) : Provider(), m_path(std::move(path)) {
|
||||
this->open();
|
||||
}
|
||||
|
||||
@@ -92,24 +88,20 @@ namespace hex::prv {
|
||||
}
|
||||
|
||||
void FileProvider::saveAs(const std::string &path) {
|
||||
FILE *file = fopen(path.c_str(), "wb");
|
||||
File file(path, File::Mode::Create);
|
||||
|
||||
if (file != nullptr) {
|
||||
std::vector<u8> buffer(0xFF'FFFF, 0x00);
|
||||
if (file.isValid()) {
|
||||
std::vector<u8> buffer(std::min<size_t>(0xFF'FFFF, file.getSize()), 0x00);
|
||||
size_t bufferSize = buffer.size();
|
||||
|
||||
fseek(file, 0, SEEK_SET);
|
||||
|
||||
auto provider = SharedData::currentProvider;
|
||||
for (u64 offset = 0; offset < provider->getActualSize(); offset += bufferSize) {
|
||||
if (bufferSize > provider->getActualSize() - offset)
|
||||
bufferSize = provider->getActualSize() - offset;
|
||||
|
||||
provider->readRelative(offset, buffer.data(), bufferSize);
|
||||
fwrite(buffer.data(), 1, bufferSize, file);
|
||||
file.write(buffer);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,7 +167,7 @@ namespace hex::prv {
|
||||
{
|
||||
auto length = this->m_path.length() + 1;
|
||||
auto wideLength = MultiByteToWideChar(CP_UTF8, 0, this->m_path.data(), length, 0, 0);
|
||||
wchar_t* buffer = new wchar_t[wideLength];
|
||||
auto buffer = new wchar_t[wideLength];
|
||||
MultiByteToWideChar(CP_UTF8, 0, this->m_path.data(), length, buffer, wideLength);
|
||||
widePath = buffer;
|
||||
delete[] buffer;
|
||||
|
||||
@@ -80,8 +80,8 @@ namespace hex {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<ViewCommandPalette::CommandResult> ViewCommandPalette::getCommandResults(std::string_view input) {
|
||||
constexpr auto MatchCommand = [](std::string_view currCommand, std::string_view commandToMatch) -> std::pair<MatchType, std::string_view> {
|
||||
std::vector<ViewCommandPalette::CommandResult> ViewCommandPalette::getCommandResults(const std::string &input) {
|
||||
constexpr auto MatchCommand = [](const std::string &currCommand, const std::string &commandToMatch) -> std::pair<MatchType, std::string_view> {
|
||||
if (currCommand.empty()) {
|
||||
return { MatchType::InfoMatch, "" };
|
||||
}
|
||||
@@ -114,7 +114,7 @@ namespace hex {
|
||||
if (match != MatchType::PerfectMatch)
|
||||
results.push_back({ command + " (" + LangEntry(unlocalizedDescription) + ")", "", AutoComplete });
|
||||
else {
|
||||
auto matchedCommand = input.substr(command.length()).data();
|
||||
auto matchedCommand = input.substr(command.length());
|
||||
results.push_back({ displayCallback(matchedCommand), matchedCommand, executeCallback });
|
||||
}
|
||||
}
|
||||
@@ -123,7 +123,7 @@ namespace hex {
|
||||
if (match != MatchType::PerfectMatch)
|
||||
results.push_back({ command + " (" + LangEntry(unlocalizedDescription) + ")", "", AutoComplete });
|
||||
else {
|
||||
auto matchedCommand = input.substr(command.length() + 1).data();
|
||||
auto matchedCommand = input.substr(command.length() + 1);
|
||||
results.push_back({ displayCallback(matchedCommand), matchedCommand, executeCallback });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -412,7 +412,7 @@ namespace hex {
|
||||
return output.dump();
|
||||
}
|
||||
|
||||
void ViewDataProcessor::loadNodes(std::string_view data) {
|
||||
void ViewDataProcessor::loadNodes(const std::string &data) {
|
||||
using json = nlohmann::json;
|
||||
|
||||
json input = json::parse(data);
|
||||
|
||||
@@ -87,7 +87,7 @@ namespace hex {
|
||||
}
|
||||
ImGui::NewLine();
|
||||
|
||||
const auto Link = [](std::string_view label, std::string_view url) {
|
||||
const auto Link = [](const std::string &label, const std::string &url) {
|
||||
if (ImGui::BulletHyperlink(label.data()))
|
||||
hex::openWebpage(url.data());
|
||||
};
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
#include <hex/api/imhex_api.hpp>
|
||||
#include <hex/providers/provider.hpp>
|
||||
#include <hex/helpers/crypto.hpp>
|
||||
#include <hex/lang/pattern_data.hpp>
|
||||
#include <hex/helpers/file.hpp>
|
||||
#include <hex/pattern_language/pattern_data.hpp>
|
||||
|
||||
#include "providers/file_provider.hpp"
|
||||
#include "helpers/patches.hpp"
|
||||
@@ -137,7 +138,7 @@ namespace hex {
|
||||
return { std::string(decoded), advance, color };
|
||||
};
|
||||
|
||||
EventManager::subscribe<EventFileDropped>(this, [this](const std::string &filePath) {
|
||||
EventManager::subscribe<RequestOpenFile>(this, [this](const std::string &filePath) {
|
||||
this->openFile(filePath);
|
||||
this->getWindowOpenState() = true;
|
||||
});
|
||||
@@ -159,7 +160,7 @@ namespace hex {
|
||||
});
|
||||
|
||||
EventManager::subscribe<EventProjectFileLoad>(this, []() {
|
||||
EventManager::post<EventFileDropped>(ProjectFile::getFilePath());
|
||||
EventManager::post<RequestOpenFile>(ProjectFile::getFilePath());
|
||||
});
|
||||
|
||||
EventManager::subscribe<EventWindowClosing>(this, [](GLFWwindow *window) {
|
||||
@@ -184,12 +185,13 @@ namespace hex {
|
||||
View::showErrorPopup("hex.view.hexeditor.error.create"_lang);
|
||||
return;
|
||||
}
|
||||
this->openFile(path);
|
||||
|
||||
EventManager::post<RequestOpenFile>(path);
|
||||
this->getWindowOpenState() = true;
|
||||
});
|
||||
} else if (name == "Open File") {
|
||||
hex::openFileBrowser("hex.view.hexeditor.open_file"_lang, DialogMode::Open, { }, [this](auto path) {
|
||||
this->openFile(path);
|
||||
EventManager::post<RequestOpenFile>(path);
|
||||
this->getWindowOpenState() = true;
|
||||
});
|
||||
} else if (name == "Open Project") {
|
||||
@@ -217,7 +219,7 @@ namespace hex {
|
||||
}
|
||||
|
||||
ViewHexEditor::~ViewHexEditor() {
|
||||
EventManager::unsubscribe<EventFileDropped>(this);
|
||||
EventManager::unsubscribe<RequestOpenFile>(this);
|
||||
EventManager::unsubscribe<RequestSelectionChange>(this);
|
||||
EventManager::unsubscribe<EventProjectFileLoad>(this);
|
||||
EventManager::unsubscribe<EventWindowClosing>(this);
|
||||
@@ -331,7 +333,7 @@ namespace hex {
|
||||
confirmButtons("hex.common.load"_lang, "hex.common.cancel"_lang,
|
||||
[this, &provider] {
|
||||
if (!this->m_loaderScriptScriptPath.empty() && !this->m_loaderScriptFilePath.empty()) {
|
||||
EventManager::post<EventFileDropped>(this->m_loaderScriptFilePath);
|
||||
EventManager::post<RequestOpenFile>(this->m_loaderScriptFilePath);
|
||||
LoaderScript::setFilePath(this->m_loaderScriptFilePath);
|
||||
LoaderScript::setDataProvider(provider);
|
||||
LoaderScript::processFile(this->m_loaderScriptScriptPath);
|
||||
@@ -391,14 +393,14 @@ namespace hex {
|
||||
if (ImGui::MenuItem("hex.view.hexeditor.menu.file.open_file"_lang, "CTRL + O")) {
|
||||
|
||||
hex::openFileBrowser("hex.view.hexeditor.open_file"_lang, DialogMode::Open, { }, [this](auto path) {
|
||||
EventManager::post<EventFileDropped>(path);
|
||||
EventManager::post<RequestOpenFile>(path);
|
||||
});
|
||||
}
|
||||
|
||||
if (ImGui::BeginMenu("hex.view.hexeditor.menu.file.open_recent"_lang, !SharedData::recentFilePaths.empty())) {
|
||||
for (auto &path : SharedData::recentFilePaths) {
|
||||
if (ImGui::MenuItem(std::filesystem::path(path).filename().string().c_str())) {
|
||||
EventManager::post<EventFileDropped>(path);
|
||||
EventManager::post<RequestOpenFile>(path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -475,7 +477,7 @@ namespace hex {
|
||||
if (ImGui::MenuItem("hex.view.hexeditor.menu.file.import.ips"_lang)) {
|
||||
|
||||
hex::openFileBrowser("hex.view.hexeditor.open_file"_lang, DialogMode::Open, { }, [this](auto path) {
|
||||
auto patchData = hex::readFile(path);
|
||||
auto patchData = File(path, File::Mode::Read).readBytes();
|
||||
auto patch = hex::loadIPSPatch(patchData);
|
||||
|
||||
for (auto &[address, value] : patch) {
|
||||
@@ -489,7 +491,7 @@ namespace hex {
|
||||
|
||||
if (ImGui::MenuItem("hex.view.hexeditor.menu.file.import.ips32"_lang)) {
|
||||
hex::openFileBrowser("hex.view.hexeditor.open_file"_lang, DialogMode::Open, { }, [this](auto path) {
|
||||
auto patchData = hex::readFile(path);
|
||||
auto patchData = File(path, File::Mode::Read).readBytes();
|
||||
auto patch = hex::loadIPS32Patch(patchData);
|
||||
|
||||
for (auto &[address, value] : patch) {
|
||||
@@ -610,7 +612,7 @@ namespace hex {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ViewHexEditor::createFile(std::string_view path) {
|
||||
bool ViewHexEditor::createFile(const std::string &path) {
|
||||
#if defined(OS_WINDOWS)
|
||||
std::wstring widePath;
|
||||
{
|
||||
@@ -644,7 +646,7 @@ namespace hex {
|
||||
return true;
|
||||
}
|
||||
|
||||
void ViewHexEditor::openFile(std::string_view path) {
|
||||
void ViewHexEditor::openFile(const std::string &path) {
|
||||
auto& provider = SharedData::currentProvider;
|
||||
|
||||
delete provider;
|
||||
@@ -669,41 +671,24 @@ namespace hex {
|
||||
|
||||
this->getWindowOpenState() = true;
|
||||
|
||||
EventManager::post<EventFileLoaded>(std::string(path));
|
||||
EventManager::post<EventFileLoaded>(path);
|
||||
EventManager::post<EventDataChanged>();
|
||||
EventManager::post<EventPatternChanged>();
|
||||
}
|
||||
|
||||
bool ViewHexEditor::saveToFile(std::string_view path, const std::vector<u8>& data) {
|
||||
FILE *file = fopen(path.data(), "wb");
|
||||
|
||||
if (file == nullptr)
|
||||
return false;
|
||||
|
||||
fwrite(data.data(), 1, data.size(), file);
|
||||
fclose(file);
|
||||
bool ViewHexEditor::saveToFile(const std::string &path, const std::vector<u8>& data) {
|
||||
File(path, File::Mode::Create).write(data);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ViewHexEditor::loadFromFile(std::string_view path, std::vector<u8>& data) {
|
||||
FILE *file = fopen(path.data(), "rb");
|
||||
|
||||
if (file == nullptr)
|
||||
return false;
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
size_t size = ftello64(file);
|
||||
rewind(file);
|
||||
|
||||
data.resize(size);
|
||||
fread(data.data(), 1, data.size(), file);
|
||||
fclose(file);
|
||||
bool ViewHexEditor::loadFromFile(const std::string &path, std::vector<u8>& data) {
|
||||
data = File(path, File::Mode::Read).readBytes();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ViewHexEditor::copyBytes() {
|
||||
void ViewHexEditor::copyBytes() const {
|
||||
auto provider = SharedData::currentProvider;
|
||||
|
||||
size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
||||
@@ -722,7 +707,7 @@ namespace hex {
|
||||
ImGui::SetClipboardText(str.c_str());
|
||||
}
|
||||
|
||||
void ViewHexEditor::pasteBytes() {
|
||||
void ViewHexEditor::pasteBytes() const {
|
||||
auto provider = SharedData::currentProvider;
|
||||
|
||||
size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
||||
@@ -764,7 +749,7 @@ namespace hex {
|
||||
provider->writeRelative(start, buffer.data(), std::min(end - start + 1, buffer.size()));
|
||||
}
|
||||
|
||||
void ViewHexEditor::copyString() {
|
||||
void ViewHexEditor::copyString() const {
|
||||
auto provider = SharedData::currentProvider;
|
||||
|
||||
size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
||||
@@ -779,7 +764,7 @@ namespace hex {
|
||||
ImGui::SetClipboardText(buffer.c_str());
|
||||
}
|
||||
|
||||
void ViewHexEditor::copyLanguageArray(Language language) {
|
||||
void ViewHexEditor::copyLanguageArray(Language language) const {
|
||||
auto provider = SharedData::currentProvider;
|
||||
|
||||
size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
||||
@@ -881,7 +866,7 @@ namespace hex {
|
||||
ImGui::SetClipboardText(str.c_str());
|
||||
}
|
||||
|
||||
void ViewHexEditor::copyHexView() {
|
||||
void ViewHexEditor::copyHexView() const {
|
||||
auto provider = SharedData::currentProvider;
|
||||
|
||||
size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
||||
@@ -928,7 +913,7 @@ namespace hex {
|
||||
ImGui::SetClipboardText(str.c_str());
|
||||
}
|
||||
|
||||
void ViewHexEditor::copyHexViewHTML() {
|
||||
void ViewHexEditor::copyHexViewHTML() const {
|
||||
auto provider = SharedData::currentProvider;
|
||||
|
||||
size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd);
|
||||
@@ -1067,7 +1052,7 @@ R"(
|
||||
*_this->m_lastSearchBuffer = _this->m_searchFunction(provider, data->Buf);
|
||||
_this->m_lastSearchIndex = 0;
|
||||
|
||||
if (_this->m_lastSearchBuffer->size() > 0)
|
||||
if (!_this->m_lastSearchBuffer->empty())
|
||||
_this->m_memoryEditor.GotoAddrAndSelect((*_this->m_lastSearchBuffer)[0].first, (*_this->m_lastSearchBuffer)[0].second);
|
||||
|
||||
return 0;
|
||||
@@ -1079,12 +1064,12 @@ R"(
|
||||
*this->m_lastSearchBuffer = this->m_searchFunction(provider, buffer);
|
||||
this->m_lastSearchIndex = 0;
|
||||
|
||||
if (this->m_lastSearchBuffer->size() > 0)
|
||||
if (!this->m_lastSearchBuffer->empty())
|
||||
this->m_memoryEditor.GotoAddrAndSelect((*this->m_lastSearchBuffer)[0].first, (*this->m_lastSearchBuffer)[0].second);
|
||||
};
|
||||
|
||||
static auto FindNext = [this]() {
|
||||
if (this->m_lastSearchBuffer->size() > 0) {
|
||||
if (!this->m_lastSearchBuffer->empty()) {
|
||||
++this->m_lastSearchIndex %= this->m_lastSearchBuffer->size();
|
||||
this->m_memoryEditor.GotoAddrAndSelect((*this->m_lastSearchBuffer)[this->m_lastSearchIndex].first,
|
||||
(*this->m_lastSearchBuffer)[this->m_lastSearchIndex].second);
|
||||
@@ -1092,7 +1077,7 @@ R"(
|
||||
};
|
||||
|
||||
static auto FindPrevious = [this]() {
|
||||
if (this->m_lastSearchBuffer->size() > 0) {
|
||||
if (!this->m_lastSearchBuffer->empty()) {
|
||||
this->m_lastSearchIndex--;
|
||||
|
||||
if (this->m_lastSearchIndex < 0)
|
||||
@@ -1134,7 +1119,7 @@ R"(
|
||||
if (ImGui::Button("hex.view.hexeditor.search.find"_lang))
|
||||
Find(currBuffer->data());
|
||||
|
||||
if (this->m_lastSearchBuffer->size() > 0) {
|
||||
if (!this->m_lastSearchBuffer->empty()) {
|
||||
if ((ImGui::Button("hex.view.hexeditor.search.find_next"_lang)))
|
||||
FindNext();
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "views/view_pattern_data.hpp"
|
||||
|
||||
#include <hex/providers/provider.hpp>
|
||||
#include <hex/lang/pattern_data.hpp>
|
||||
#include <hex/pattern_language/pattern_data.hpp>
|
||||
|
||||
namespace hex {
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace hex {
|
||||
EventManager::unsubscribe<EventPatternChanged>(this);
|
||||
}
|
||||
|
||||
static bool beginPatternDataTable(prv::Provider* &provider, const std::vector<lang::PatternData*> &patterns, std::vector<lang::PatternData*> &sortedPatterns) {
|
||||
static bool beginPatternDataTable(prv::Provider* &provider, const std::vector<pl::PatternData*> &patterns, std::vector<pl::PatternData*> &sortedPatterns) {
|
||||
if (ImGui::BeginTable("##patterndatatable", 6, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY)) {
|
||||
ImGui::TableSetupScrollFreeze(0, 1);
|
||||
ImGui::TableSetupColumn("hex.view.pattern_data.name"_lang, 0, -1, ImGui::GetID("name"));
|
||||
@@ -31,8 +31,8 @@ namespace hex {
|
||||
if (sortSpecs->SpecsDirty || sortedPatterns.empty()) {
|
||||
sortedPatterns = patterns;
|
||||
|
||||
std::sort(sortedPatterns.begin(), sortedPatterns.end(), [&sortSpecs, &provider](lang::PatternData* left, lang::PatternData* right) -> bool {
|
||||
return lang::PatternData::sortPatternDataTable(sortSpecs, provider, left, right);
|
||||
std::sort(sortedPatterns.begin(), sortedPatterns.end(), [&sortSpecs, &provider](pl::PatternData* left, pl::PatternData* right) -> bool {
|
||||
return pl::PatternData::sortPatternDataTable(sortSpecs, provider, left, right);
|
||||
});
|
||||
|
||||
for (auto &pattern : sortedPatterns)
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
#include "views/view_pattern_editor.hpp"
|
||||
|
||||
#include "helpers/project_file_handler.hpp"
|
||||
#include <hex/lang/preprocessor.hpp>
|
||||
#include <hex/lang/pattern_data.hpp>
|
||||
#include <hex/pattern_language/preprocessor.hpp>
|
||||
#include <hex/pattern_language/pattern_data.hpp>
|
||||
#include <hex/helpers/paths.hpp>
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#include <hex/helpers/file.hpp>
|
||||
|
||||
#include <hex/helpers/magic.hpp>
|
||||
#include <hex/helpers/literals.hpp>
|
||||
@@ -81,7 +82,7 @@ namespace hex {
|
||||
|
||||
|
||||
ViewPatternEditor::ViewPatternEditor() : View("hex.view.pattern.name") {
|
||||
this->m_patternLanguageRuntime = new lang::PatternLanguage();
|
||||
this->m_patternLanguageRuntime = new pl::PatternLanguage();
|
||||
|
||||
this->m_textEditor.SetLanguageDefinition(PatternLanguage());
|
||||
this->m_textEditor.SetShowWhitespaces(false);
|
||||
@@ -104,7 +105,7 @@ namespace hex {
|
||||
if (this->m_textEditor.GetText().find_first_not_of(" \f\n\r\t\v") != std::string::npos)
|
||||
return;
|
||||
|
||||
lang::Preprocessor preprocessor;
|
||||
pl::Preprocessor preprocessor;
|
||||
auto provider = SharedData::currentProvider;
|
||||
|
||||
if (provider == nullptr)
|
||||
@@ -130,20 +131,11 @@ namespace hex {
|
||||
if (!entry.is_regular_file())
|
||||
continue;
|
||||
|
||||
FILE *file = fopen(entry.path().string().c_str(), "r");
|
||||
|
||||
if (file == nullptr)
|
||||
File file(entry.path().string(), File::Mode::Read);
|
||||
if (!file.isValid())
|
||||
continue;
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
size_t size = ftell(file);
|
||||
rewind(file);
|
||||
|
||||
std::vector<char> patternBuffer( size + 1, 0x00);
|
||||
fread(patternBuffer.data(), 1, size, file);
|
||||
fclose(file);
|
||||
|
||||
preprocessor.preprocess(patternBuffer.data());
|
||||
preprocessor.preprocess(file.readString());
|
||||
|
||||
if (foundCorrectType)
|
||||
this->m_possiblePatternFiles.push_back(entry.path().string());
|
||||
@@ -220,16 +212,16 @@ namespace hex {
|
||||
if (ImGui::BeginChild("##console", consoleSize, true, ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
|
||||
for (auto &[level, message] : this->m_console) {
|
||||
switch (level) {
|
||||
case lang::LogConsole::Level::Debug:
|
||||
case pl::LogConsole::Level::Debug:
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, this->m_textEditor.GetPalette()[u32(TextEditor::PaletteIndex::Comment)]);
|
||||
break;
|
||||
case lang::LogConsole::Level::Info:
|
||||
case pl::LogConsole::Level::Info:
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, this->m_textEditor.GetPalette()[u32(TextEditor::PaletteIndex::Default)]);
|
||||
break;
|
||||
case lang::LogConsole::Level::Warning:
|
||||
case pl::LogConsole::Level::Warning:
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, this->m_textEditor.GetPalette()[u32(TextEditor::PaletteIndex::Preprocessor)]);
|
||||
break;
|
||||
case lang::LogConsole::Level::Error:
|
||||
case pl::LogConsole::Level::Error:
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, this->m_textEditor.GetPalette()[u32(TextEditor::PaletteIndex::ErrorMarker)]);
|
||||
break;
|
||||
default: continue;
|
||||
@@ -285,7 +277,7 @@ namespace hex {
|
||||
entries[i] = std::filesystem::path(this->m_possiblePatternFiles[i]).filename().string();
|
||||
}
|
||||
|
||||
ImGui::ListBox("hex.view.pattern.accept_pattern.patterns"_lang, &this->m_selectedPatternFile, [](void *data, int id, const char** outText) -> bool {
|
||||
ImGui::ListBox("hex.view.pattern.accept_pattern.pattern_language"_lang, &this->m_selectedPatternFile, [](void *data, int id, const char** outText) -> bool {
|
||||
auto &entries = *static_cast<std::vector<std::string>*>(data);
|
||||
|
||||
*outText = entries[id].c_str();
|
||||
@@ -311,8 +303,8 @@ namespace hex {
|
||||
}
|
||||
|
||||
|
||||
void ViewPatternEditor::loadPatternFile(std::string_view path) {
|
||||
FILE *file = fopen(path.data(), "rb");
|
||||
void ViewPatternEditor::loadPatternFile(const std::string &path) {
|
||||
FILE *file = fopen(path.c_str(), "rb");
|
||||
|
||||
if (file != nullptr) {
|
||||
char *buffer;
|
||||
@@ -340,7 +332,7 @@ namespace hex {
|
||||
delete data;
|
||||
|
||||
SharedData::patternData.clear();
|
||||
lang::PatternData::resetPalette();
|
||||
pl::PatternData::resetPalette();
|
||||
}
|
||||
|
||||
void ViewPatternEditor::parsePattern(char *buffer) {
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace hex {
|
||||
|
||||
auto drawTab = [this](auto title, ImHexPath pathType, auto &content) {
|
||||
if (ImGui::BeginTabItem(title)) {
|
||||
if (ImGui::BeginTable("##patterns", 3, ImGuiTableFlags_ScrollY | ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingStretchProp)) {
|
||||
if (ImGui::BeginTable("##pattern_language", 3, ImGuiTableFlags_ScrollY | ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingStretchProp)) {
|
||||
ImGui::TableSetupScrollFreeze(0, 1);
|
||||
ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_None, 1.0);
|
||||
ImGui::TableSetupColumn("Description", ImGuiTableColumnFlags_None, 3.0);
|
||||
@@ -157,7 +157,7 @@ namespace hex {
|
||||
}
|
||||
};
|
||||
|
||||
parseStoreEntries(json, "patterns", ImHexPath::Patterns, this->m_patterns);
|
||||
parseStoreEntries(json, "pattern_language", ImHexPath::Patterns, this->m_patterns);
|
||||
parseStoreEntries(json, "includes", ImHexPath::PatternsInclude, this->m_includes);
|
||||
parseStoreEntries(json, "magic", ImHexPath::Magic, this->m_magics);
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <hex/providers/provider.hpp>
|
||||
#include <hex/helpers/utils.hpp>
|
||||
#include <hex/helpers/file.hpp>
|
||||
|
||||
#include <yara.h>
|
||||
#include <filesystem>
|
||||
@@ -137,11 +138,10 @@ namespace hex {
|
||||
YR_COMPILER *compiler = nullptr;
|
||||
yr_compiler_create(&compiler);
|
||||
|
||||
FILE *file = fopen(this->m_rules[this->m_selectedRule].c_str(), "r");
|
||||
if (file == nullptr) return;
|
||||
ON_SCOPE_EXIT { fclose(file); };
|
||||
File file(this->m_rules[this->m_selectedRule], File::Mode::Read);
|
||||
if (!file.isValid()) return;
|
||||
|
||||
if (yr_compiler_add_file(compiler, file, nullptr, nullptr) != 0) {
|
||||
if (yr_compiler_add_file(compiler, file.getHandle(), nullptr, nullptr) != 0) {
|
||||
this->m_errorMessage.resize(0xFFFF);
|
||||
yr_compiler_get_error_message(compiler, this->m_errorMessage.data(), this->m_errorMessage.size());
|
||||
this->m_matching = false;
|
||||
|
||||
@@ -154,7 +154,7 @@ namespace hex {
|
||||
}
|
||||
});
|
||||
|
||||
EventManager::subscribe<EventFileLoaded>(this, [this](const std::string &path){
|
||||
EventManager::subscribe<EventFileLoaded>(this, [](const std::string &path){
|
||||
SharedData::recentFilePaths.push_front(path);
|
||||
|
||||
{
|
||||
@@ -167,7 +167,7 @@ namespace hex {
|
||||
exists = true;
|
||||
}
|
||||
|
||||
if (!exists)
|
||||
if (!exists && !file.empty())
|
||||
uniques.push_back(file);
|
||||
|
||||
if (uniques.size() > 5)
|
||||
@@ -184,6 +184,10 @@ namespace hex {
|
||||
}
|
||||
});
|
||||
|
||||
EventManager::subscribe<EventFileUnloaded>(this, []{
|
||||
EventManager::post<RequestChangeWindowTitle>("");
|
||||
});
|
||||
|
||||
EventManager::subscribe<RequestCloseImHex>(this, [this](bool noQuestions) {
|
||||
glfwSetWindowShouldClose(this->m_window, true);
|
||||
|
||||
@@ -193,11 +197,14 @@ namespace hex {
|
||||
|
||||
EventManager::subscribe<RequestChangeWindowTitle>(this, [this](std::string windowTitle) {
|
||||
std::string title = "ImHex";
|
||||
if (!windowTitle.empty())
|
||||
title += " - " + windowTitle;
|
||||
|
||||
if (ProjectFile::hasUnsavedChanges())
|
||||
title += " (*)";
|
||||
if (SharedData::currentProvider != nullptr) {
|
||||
if (!windowTitle.empty())
|
||||
title += " - " + windowTitle;
|
||||
|
||||
if (ProjectFile::hasUnsavedChanges())
|
||||
title += " (*)";
|
||||
}
|
||||
|
||||
this->m_windowTitle = title;
|
||||
glfwSetWindowTitle(this->m_window, title.c_str());
|
||||
@@ -250,8 +257,10 @@ namespace hex {
|
||||
|
||||
EventManager::unsubscribe<EventSettingsChanged>(this);
|
||||
EventManager::unsubscribe<EventFileLoaded>(this);
|
||||
EventManager::unsubscribe<EventFileUnloaded>(this);
|
||||
EventManager::unsubscribe<RequestCloseImHex>(this);
|
||||
EventManager::unsubscribe<RequestChangeWindowTitle>(this);
|
||||
EventManager::unsubscribe<EventAbnormalTermination>(this);
|
||||
|
||||
ImGui::UnloadImage(this->m_bannerTexture);
|
||||
ImGui::UnloadImage(this->m_logoTexture);
|
||||
@@ -533,7 +542,7 @@ namespace hex {
|
||||
if (!SharedData::recentFilePaths.empty()) {
|
||||
for (auto &path : SharedData::recentFilePaths) {
|
||||
if (ImGui::BulletHyperlink(std::filesystem::path(path).filename().string().c_str())) {
|
||||
EventManager::post<EventFileDropped>(path);
|
||||
EventManager::post<RequestOpenFile>(path);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -749,7 +758,7 @@ namespace hex {
|
||||
if (count != 1)
|
||||
return;
|
||||
|
||||
EventManager::post<EventFileDropped>(paths[0]);
|
||||
EventManager::post<RequestOpenFile>(paths[0]);
|
||||
});
|
||||
|
||||
glfwSetWindowCloseCallback(this->m_window, [](GLFWwindow *window) {
|
||||
|
||||
Reference in New Issue
Block a user