feat: Added option to specify max file size to load into memory

This commit is contained in:
WerWolv
2024-05-19 15:10:22 +02:00
parent e9b492a287
commit 71c1bcde0d
8 changed files with 64 additions and 16 deletions

View File

@@ -11,8 +11,6 @@ namespace hex::plugin::builtin {
class FileProvider : public hex::prv::Provider {
public:
constexpr static u64 MaxMemoryFileSize = 128 * 1024 * 1024;
FileProvider() = default;
~FileProvider() override = default;

View File

@@ -459,6 +459,8 @@
"hex.builtin.setting.general.auto_backup_time.format.extended": "Every {0}m {1}s",
"hex.builtin.setting.general.auto_load_patterns": "Auto-load supported pattern",
"hex.builtin.setting.general.server_contact": "Enable update checks and usage statistics",
"hex.builtin.setting.general.max_mem_file_size": "Maximum size of file to load into memory",
"hex.builtin.setting.general.max_mem_file_size.desc": "Small files are loaded into memory to prevent them from being modified directly on disk.\n\nIncreasing this size allows larger files to be loaded into memory before ImHex resorts streaming in data from disk.",
"hex.builtin.setting.general.network_interface": "Enable network interface",
"hex.builtin.setting.general.save_recent_providers": "Save recently used providers",
"hex.builtin.setting.general.show_tips": "Show tips on startup",

View File

@@ -1,11 +1,13 @@
#include "content/providers/file_provider.hpp"
#include "content/providers/memory_file_provider.hpp"
#include <hex/api/content_registry.hpp>
#include <hex/api/imhex_api.hpp>
#include <hex/api/localization_manager.hpp>
#include <hex/api/project_file_manager.hpp>
#include <hex/api/task_manager.hpp>
#include <popups/popup_question.hpp>
#include <toasts/toast_notification.hpp>
#include <hex/helpers/utils.hpp>
@@ -13,10 +15,10 @@
#include <fmt/chrono.h>
#include <wolv/utils/string.hpp>
#include <wolv/literals.hpp>
#include <nlohmann/json.hpp>
#include <cstring>
#include <popups/popup_question.hpp>
#if defined(OS_WINDOWS)
#include <windows.h>
@@ -24,6 +26,8 @@
namespace hex::plugin::builtin {
using namespace wolv::literals;
std::set<FileProvider*> FileProvider::s_openedFiles;
bool FileProvider::isAvailable() const {
@@ -223,8 +227,10 @@ namespace hex::plugin::builtin {
}
}
size_t maxMemoryFileSize = ContentRegistry::Settings::read<u64>("hex.builtin.setting.general", "hex.builtin.setting.general.max_mem_file_size", 128_MiB);
if (m_writable) {
if (m_fileSize < MaxMemoryFileSize) {
if (m_fileSize < maxMemoryFileSize) {
m_data = m_file.readVectorAtomic(0x00, m_fileSize);
if (!m_data.empty()) {
m_changeTracker = wolv::io::ChangeTracker(m_file);

View File

@@ -4,6 +4,7 @@
#include <hex/api/theme_manager.hpp>
#include <hex/api/shortcut_manager.hpp>
#include <hex/api/event_manager.hpp>
#include <hex/api/layout_manager.hpp>
#include <hex/helpers/http_requests.hpp>
#include <hex/helpers/utils.hpp>
@@ -12,14 +13,17 @@
#include <hex/ui/imgui_imhex_extensions.h>
#include <fonts/codicons_font.h>
#include <wolv/literals.hpp>
#include <wolv/utils/string.hpp>
#include <nlohmann/json.hpp>
#include <utility>
#include <hex/api/layout_manager.hpp>
#include <wolv/utils/string.hpp>
namespace hex::plugin::builtin {
using namespace wolv::literals;
namespace {
/*
@@ -683,6 +687,8 @@ namespace hex::plugin::builtin {
ContentRegistry::Settings::add<Widgets::Checkbox>("hex.builtin.setting.general", "", "hex.builtin.setting.general.show_tips", false);
ContentRegistry::Settings::add<Widgets::Checkbox>("hex.builtin.setting.general", "", "hex.builtin.setting.general.save_recent_providers", true);
ContentRegistry::Settings::add<AutoBackupWidget>("hex.builtin.setting.general", "", "hex.builtin.setting.general.auto_backup_time");
ContentRegistry::Settings::add<Widgets::SliderDataSize>("hex.builtin.setting.general", "", "hex.builtin.setting.general.max_mem_file_size", 128_MiB, 0_bytes, 32_GiB)
.setTooltip("hex.builtin.setting.general.max_mem_file_size.desc");
ContentRegistry::Settings::add<Widgets::Checkbox>("hex.builtin.setting.general", "hex.builtin.setting.general.patterns", "hex.builtin.setting.general.auto_load_patterns", true);
ContentRegistry::Settings::add<Widgets::Checkbox>("hex.builtin.setting.general", "hex.builtin.setting.general.patterns", "hex.builtin.setting.general.sync_pattern_source", false);
ContentRegistry::Settings::add<Widgets::Checkbox>("hex.builtin.setting.general", "hex.builtin.setting.general.network", "hex.builtin.setting.general.network_interface", false);
@@ -854,7 +860,7 @@ namespace hex::plugin::builtin {
EventImHexStartupFinished::subscribe([]{
for (const auto &[name, experiment] : ContentRegistry::Experiments::impl::getExperiments()) {
ContentRegistry::Settings::add<Widgets::Checkbox>("hex.builtin.setting.experiments", "", experiment.unlocalizedName, false)
.setTooltip(Lang(experiment.unlocalizedDescription))
.setTooltip(experiment.unlocalizedDescription)
.setChangedCallback([name](Widgets::Widget &widget) {
auto checkBox = static_cast<Widgets::Checkbox *>(&widget);

View File

@@ -106,7 +106,7 @@ namespace hex::plugin::builtin {
ImGui::PopItemWidth();
ImGui::EndDisabled();
if (auto tooltip = setting.widget->getTooltip(); tooltip.has_value() && ImGui::IsItemHovered())
if (const auto &tooltip = setting.widget->getTooltip(); tooltip.has_value() && ImGui::IsItemHovered())
ImGuiExt::InfoTooltip(Lang(tooltip.value()));
auto &widget = setting.widget;