1
0
mirror of synced 2025-02-17 18:59:21 +01:00

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

@ -99,7 +99,7 @@ namespace hex {
bool m_requiresRestart = false;
std::function<bool()> m_enabledCallback;
std::function<void(Widget&)> m_changedCallback;
std::optional<std::string> m_tooltip;
std::optional<UnlocalizedString> m_tooltip;
};
[[nodiscard]]
@ -113,7 +113,7 @@ namespace hex {
}
[[nodiscard]]
const std::optional<std::string>& getTooltip() const {
const std::optional<UnlocalizedString>& getTooltip() const {
return m_interface.m_tooltip;
}
@ -176,6 +176,21 @@ namespace hex {
float m_min, m_max;
};
class SliderDataSize : public Widget {
public:
SliderDataSize(u64 defaultValue, u64 min, u64 max) : m_value(defaultValue), m_min(min), m_max(max) { }
bool draw(const std::string &name) override;
void load(const nlohmann::json &data) override;
nlohmann::json store() override;
[[nodiscard]] i32 getValue() const { return m_value; }
protected:
u64 m_value;
u64 m_min, m_max;
};
class ColorPicker : public Widget {
public:
explicit ColorPicker(ImColor defaultColor);

View File

@ -312,6 +312,23 @@ namespace hex {
}
bool SliderDataSize::draw(const std::string &name) {
return ImGuiExt::SliderBytes(name.c_str(), &m_value, m_min, m_max);
}
void SliderDataSize::load(const nlohmann::json &data) {
if (data.is_number_integer()) {
m_value = data.get<u64>();
} else {
log::warn("Invalid data type loaded from settings for slider!");
}
}
nlohmann::json SliderDataSize::store() {
return m_value;
}
ColorPicker::ColorPicker(ImColor defaultColor) {
m_value = {
defaultColor.Value.x,

View File

@ -573,12 +573,16 @@ namespace ImGuiExt {
bool result = false;
if (IsItemHovered() && (currTime - lastMoveTime) >= 0.5 && hoveredID == lastHoveredID) {
if (!std::string_view(text).empty()) {
BeginTooltip();
if (isSeparator)
SeparatorText(text);
else
TextUnformatted(text);
EndTooltip();
const auto width = 300 * hex::ImHexApi::System::getGlobalScale();
ImGui::SetNextWindowSizeConstraints(ImVec2(width, 0), ImVec2(width, FLT_MAX));
if (BeginTooltip()) {
if (isSeparator)
SeparatorText(text);
else
TextFormattedWrapped("{}", text);
EndTooltip();
}
}
result = true;

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;