Skip to content

Commit

Permalink
fix: ImHex no longer behaves weirdly or crashes when no plugins are l…
Browse files Browse the repository at this point in the history
…oaded
  • Loading branch information
WerWolv committed Apr 21, 2021
1 parent 64e3588 commit d177d69
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion include/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace hex {

float m_globalScale = 1.0f, m_fontScale = 1.0f;
bool m_fpsVisible = false;
double m_targetFps;
double m_targetFps = 60.0;
bool m_demoWindowOpen = false;
bool m_layoutConfigured = false;

Expand Down
8 changes: 4 additions & 4 deletions plugins/libimhex/source/api/content_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ namespace hex {

if (!json.contains(unlocalizedCategory.data()))
json[unlocalizedCategory.data()] = nlohmann::json::object();
if (!json[unlocalizedCategory.data()].contains(unlocalizedName.data()))
json[unlocalizedCategory.data()][unlocalizedName.data()] = defaultValue;
if (!json[unlocalizedCategory.data()].contains(unlocalizedName.data()) || !json[unlocalizedCategory.data()][unlocalizedName.data()].is_number())
json[unlocalizedCategory.data()][unlocalizedName.data()] = int(defaultValue);
}

void ContentRegistry::Settings::add(std::string_view unlocalizedCategory, std::string_view unlocalizedName, std::string_view defaultValue, const std::function<bool(std::string_view, nlohmann::json&)> &callback) {
Expand All @@ -49,8 +49,8 @@ namespace hex {

if (!json.contains(unlocalizedCategory.data()))
json[unlocalizedCategory.data()] = nlohmann::json::object();
if (!json[unlocalizedCategory.data()].contains(unlocalizedName.data()))
json[unlocalizedCategory.data()][unlocalizedName.data()] = defaultValue;
if (!json[unlocalizedCategory.data()].contains(unlocalizedName.data()) || !json[unlocalizedCategory.data()][unlocalizedName.data()].is_string())
json[unlocalizedCategory.data()][unlocalizedName.data()] = std::string(defaultValue);
}

void ContentRegistry::Settings::write(std::string_view unlocalizedCategory, std::string_view unlocalizedName, s64 value) {
Expand Down
3 changes: 2 additions & 1 deletion source/views/view_hexeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ namespace hex {
EventManager::subscribe<EventSettingsChanged>(this, [this] {
auto alpha = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.highlight_alpha");

this->m_highlightAlpha = alpha;
if (alpha.is_number())
this->m_highlightAlpha = alpha;
});
}

Expand Down
30 changes: 18 additions & 12 deletions source/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ namespace hex {
if (argument == "update-available") {
this->m_availableUpdate = value;
} else if (argument == "no-plugins") {
View::showErrorPopup("No plugins are loaded, including the built-in functions plugin!\n"
"Make sure you got at least builtin.hexplug in your plugins folder.");
View::doLater([]{ ImGui::OpenPopup("No Plugins"); });
}
}
}
Expand Down Expand Up @@ -351,12 +350,15 @@ namespace hex {
}

if (this->m_fpsVisible) {
ImGui::SameLine(ImGui::GetWindowWidth() - ImGui::GetCursorPosX() - 3 * ImGui::GetFontSize());
ImGui::Text("%.1f FPS", ImGui::GetIO().Framerate);
std::string fps = hex::format("{:.1f} FPS ", ImGui::GetIO().Framerate);
ImGui::SameLine();
ImGui::SetCursorPosX(ImGui::GetWindowWidth() - fps.length() * ImGui::GetFontSize());
ImGui::TextUnformatted(fps.c_str());
} else {
#if defined(DEBUG)
ImGui::SameLine(ImGui::GetWindowWidth() - ImGui::GetCursorPosX());
ImGui::Text(ICON_FA_BUG, ImGui::GetIO().Framerate);
ImGui::SameLine();
ImGui::SetCursorPosX(ImGui::GetWindowWidth() - 2 * ImGui::GetFontSize());
ImGui::TextUnformatted(ICON_FA_BUG);
#endif
}

Expand All @@ -382,6 +384,15 @@ namespace hex {

}
ImGui::End();

// Popup for when no plugins were loaded. Intentionally left untranslated because localization isn't available
ImGui::SetNextWindowPos(ImGui::GetMainViewport()->GetCenter(), ImGuiCond_Appearing, ImVec2(0.5F, 0.5F));
if (ImGui::BeginPopupModal("No Plugins", nullptr, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove)) {
ImGui::TextUnformatted("No ImHex plugins loaded (including the built-in plugin)!");
ImGui::TextUnformatted("Make sure you at least got the builtin plugin in your plugins folder.");
ImGui::TextUnformatted("To find out where your plugin folder is, check ImHex' Readme.");
ImGui::EndPopup();
}
}

void Window::frameEnd() {
Expand Down Expand Up @@ -463,12 +474,7 @@ namespace hex {
{
const auto &plugins = PluginManager::getPlugins();

if (plugins.empty()) {
// Intentionally left untranslated so it will be readable even if no plugin with translations is loaded
ImGui::TextColored(ImVec4(0.92F, 0.25F, 0.2F, 1.0F),
"No plugins loaded! To use ImHex properly, "
"make sure at least the builtin plugin is in the /plugins folder next to the executable");
} else {
if (!plugins.empty()) {
if (ImGui::BeginTable("plugins", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY | ImGuiTableFlags_SizingFixedFit, ImVec2((ImGui::GetContentRegionAvail().x * 5) / 6, ImGui::GetTextLineHeightWithSpacing() * 5))) {
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("hex.welcome.plugins.plugin"_lang);
Expand Down

0 comments on commit d177d69

Please sign in to comment.