2023-05-27 16:59:30 +02:00
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
#include <imgui.h>
|
|
|
|
|
|
|
|
#include <hex/api/event.hpp>
|
|
|
|
#include <hex/api/content_registry.hpp>
|
|
|
|
#include <hex/api/project_file_manager.hpp>
|
|
|
|
#include <hex/api/task.hpp>
|
|
|
|
#include <hex/helpers/fmt.hpp>
|
|
|
|
#include <hex/providers/provider.hpp>
|
|
|
|
|
|
|
|
#include <wolv/utils/guards.hpp>
|
2023-07-09 12:53:31 +02:00
|
|
|
#include <wolv/utils/string.hpp>
|
2023-05-27 16:59:30 +02:00
|
|
|
|
|
|
|
#include <content/recent.hpp>
|
|
|
|
#include <content/popups/popup_notification.hpp>
|
|
|
|
#include <fonts/codicons_font.h>
|
|
|
|
|
|
|
|
namespace hex::plugin::builtin::recent {
|
|
|
|
|
|
|
|
constexpr static auto MaxRecentEntries = 5;
|
2023-06-02 11:03:44 +02:00
|
|
|
constexpr static auto BackupFileName = "crash_backup.hexproj";
|
2023-05-27 16:59:30 +02:00
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
static std::atomic_bool s_recentEntriesUpdating = false;
|
2023-05-27 16:59:30 +02:00
|
|
|
static std::list<RecentEntry> s_recentEntries;
|
|
|
|
|
|
|
|
void registerEventHandlers() {
|
|
|
|
// Save every opened provider as a "recent" shortcut
|
2023-11-10 20:47:08 +01:00
|
|
|
(void)EventManager::subscribe<EventProviderOpened>([](const prv::Provider *provider) {
|
2023-10-21 23:07:33 +02:00
|
|
|
if (ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.save_recent_providers", true)) {
|
2023-05-27 16:59:30 +02:00
|
|
|
auto fileName = hex::format("{:%y%m%d_%H%M%S}.json", fmt::gmtime(std::chrono::system_clock::now()));
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Do not save to recents if the provider is part of a project
|
2023-11-08 11:54:21 +01:00
|
|
|
if (ProjectFile::hasPath())
|
|
|
|
return;
|
2023-05-27 16:59:30 +02:00
|
|
|
|
2023-11-08 11:54:21 +01:00
|
|
|
// Do not save to recents if the provider doesn't want it
|
|
|
|
if (!provider->isSavableAsRecent())
|
|
|
|
return;
|
2023-08-25 15:35:15 +02:00
|
|
|
|
2023-05-27 16:59:30 +02:00
|
|
|
// The recent provider is saved to every "recent" directory
|
|
|
|
for (const auto &recentPath : fs::getDefaultPaths(fs::ImHexPath::Recent)) {
|
|
|
|
wolv::io::File recentFile(recentPath / fileName, wolv::io::File::Mode::Create);
|
|
|
|
if (!recentFile.isValid())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
{
|
|
|
|
auto path = ProjectFile::getPath();
|
|
|
|
ProjectFile::clearPath();
|
|
|
|
|
|
|
|
if (auto settings = provider->storeSettings(); !settings.is_null())
|
|
|
|
recentFile.writeString(settings.dump(4));
|
|
|
|
|
|
|
|
ProjectFile::setPath(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRecentEntries();
|
|
|
|
});
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Save opened projects as a "recent" shortcut
|
2023-05-27 16:59:30 +02:00
|
|
|
(void)EventManager::subscribe<EventProjectOpened>([] {
|
2023-10-21 23:07:33 +02:00
|
|
|
if (ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.save_recent_providers", true)) {
|
2023-05-27 16:59:30 +02:00
|
|
|
auto fileName = hex::format("{:%y%m%d_%H%M%S}.json", fmt::gmtime(std::chrono::system_clock::now()));
|
|
|
|
|
2023-06-02 11:03:44 +02:00
|
|
|
auto projectFileName = ProjectFile::getPath().filename();
|
|
|
|
if (projectFileName == BackupFileName)
|
|
|
|
return;
|
|
|
|
|
2023-05-27 16:59:30 +02:00
|
|
|
// The recent provider is saved to every "recent" directory
|
|
|
|
for (const auto &recentPath : fs::getDefaultPaths(fs::ImHexPath::Recent)) {
|
|
|
|
wolv::io::File recentFile(recentPath / fileName, wolv::io::File::Mode::Create);
|
|
|
|
if (!recentFile.isValid())
|
|
|
|
continue;
|
|
|
|
|
2023-06-02 11:03:44 +02:00
|
|
|
std::string displayName = hex::format("[{}] {}", "hex.builtin.common.project"_lang, wolv::util::toUTF8String(projectFileName));
|
2023-05-27 16:59:30 +02:00
|
|
|
|
|
|
|
nlohmann::json recentEntry {
|
|
|
|
{"type", "project"},
|
|
|
|
{"displayName", displayName},
|
2023-05-28 11:36:08 +02:00
|
|
|
{"path", wolv::util::toUTF8String(ProjectFile::getPath())}
|
2023-05-27 16:59:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
recentFile.writeString(recentEntry.dump(4));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRecentEntries();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateRecentEntries() {
|
|
|
|
TaskManager::createBackgroundTask("Updating recent files", [](auto&){
|
|
|
|
if (s_recentEntriesUpdating)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s_recentEntriesUpdating = true;
|
|
|
|
ON_SCOPE_EXIT { s_recentEntriesUpdating = false; };
|
|
|
|
|
|
|
|
s_recentEntries.clear();
|
|
|
|
|
|
|
|
// Query all recent providers
|
|
|
|
std::vector<std::fs::path> recentFilePaths;
|
|
|
|
for (const auto &folder : fs::getDefaultPaths(fs::ImHexPath::Recent)) {
|
|
|
|
for (const auto &entry : std::fs::directory_iterator(folder)) {
|
|
|
|
if (entry.is_regular_file())
|
|
|
|
recentFilePaths.push_back(entry.path());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort recent provider files by last modified time
|
|
|
|
std::sort(recentFilePaths.begin(), recentFilePaths.end(), [](const auto &a, const auto &b) {
|
|
|
|
return std::fs::last_write_time(a) > std::fs::last_write_time(b);
|
|
|
|
});
|
|
|
|
|
|
|
|
std::unordered_set<RecentEntry, RecentEntry::HashFunction> uniqueProviders;
|
|
|
|
for (u32 i = 0; i < recentFilePaths.size() && uniqueProviders.size() < MaxRecentEntries; i++) {
|
|
|
|
auto &path = recentFilePaths[i];
|
|
|
|
try {
|
|
|
|
auto jsonData = nlohmann::json::parse(wolv::io::File(path, wolv::io::File::Mode::Read).readString());
|
|
|
|
uniqueProviders.insert(RecentEntry {
|
|
|
|
.displayName = jsonData.at("displayName"),
|
|
|
|
.type = jsonData.at("type"),
|
|
|
|
.entryFilePath = path,
|
|
|
|
.data = jsonData
|
|
|
|
});
|
|
|
|
} catch (...) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all recent provider files that are not in the list
|
|
|
|
for (const auto &path : recentFilePaths) {
|
|
|
|
bool found = false;
|
|
|
|
for (const auto &provider : uniqueProviders) {
|
|
|
|
if (path == provider.entryFilePath) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
wolv::io::fs::remove(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::copy(uniqueProviders.begin(), uniqueProviders.end(), std::front_inserter(s_recentEntries));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void loadRecentEntry(const RecentEntry &recentEntry) {
|
|
|
|
if (recentEntry.type == "project") {
|
|
|
|
std::fs::path projectPath = recentEntry.data["path"].get<std::string>();
|
|
|
|
ProjectFile::load(projectPath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto *provider = ImHexApi::Provider::createProvider(recentEntry.type, true);
|
|
|
|
if (provider != nullptr) {
|
|
|
|
provider->loadSettings(recentEntry.data);
|
|
|
|
|
|
|
|
if (!provider->open() || !provider->isAvailable()) {
|
|
|
|
PopupError::open(hex::format("hex.builtin.provider.error.open"_lang, provider->getErrorMessage()));
|
|
|
|
TaskManager::doLater([provider] { ImHexApi::Provider::remove(provider); });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventManager::post<EventProviderOpened>(provider);
|
|
|
|
|
|
|
|
updateRecentEntries();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void draw() {
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::BeginSubWindow("hex.builtin.welcome.start.recent"_lang, ImVec2(), ImGuiChildFlags_AutoResizeX);
|
2023-05-27 16:59:30 +02:00
|
|
|
{
|
|
|
|
if (!s_recentEntriesUpdating) {
|
2023-11-16 09:32:24 +01:00
|
|
|
|
|
|
|
for (auto it = s_recentEntries.begin(); it != s_recentEntries.end();) {
|
2023-05-27 16:59:30 +02:00
|
|
|
const auto &recentEntry = *it;
|
|
|
|
bool shouldRemove = false;
|
|
|
|
|
2023-11-16 13:23:28 +01:00
|
|
|
const bool isProject = recentEntry.type == "project";
|
|
|
|
|
2023-05-27 16:59:30 +02:00
|
|
|
ImGui::PushID(&recentEntry);
|
|
|
|
ON_SCOPE_EXIT { ImGui::PopID(); };
|
|
|
|
|
|
|
|
const char* icon;
|
2023-11-16 13:23:28 +01:00
|
|
|
if (isProject) {
|
2023-05-27 16:59:30 +02:00
|
|
|
icon = ICON_VS_PROJECT;
|
|
|
|
} else {
|
|
|
|
icon = ICON_VS_FILE_BINARY;
|
|
|
|
}
|
2023-11-16 22:24:06 +01:00
|
|
|
|
|
|
|
if (ImGuiExt::Hyperlink(hex::format("{} {}", icon, hex::limitStringLength(recentEntry.displayName, 32)).c_str())) {
|
2023-05-27 16:59:30 +02:00
|
|
|
loadRecentEntry(recentEntry);
|
|
|
|
break;
|
|
|
|
}
|
2023-11-16 13:23:28 +01:00
|
|
|
if (!isProject)
|
|
|
|
ImGui::SetItemTooltip("%s", LangEntry(recentEntry.type).get().c_str());
|
2023-05-27 16:59:30 +02:00
|
|
|
|
|
|
|
// Detect right click on recent provider
|
2023-11-16 09:32:24 +01:00
|
|
|
std::string popupID = hex::format("RecentEntryMenu.{}", recentEntry.getHash());
|
2023-05-27 16:59:30 +02:00
|
|
|
if (ImGui::IsMouseReleased(1) && ImGui::IsItemHovered()) {
|
|
|
|
ImGui::OpenPopup(popupID.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::BeginPopup(popupID.c_str())) {
|
2023-11-16 13:23:28 +01:00
|
|
|
if (ImGui::MenuItem("hex.builtin.common.remove"_lang)) {
|
2023-05-27 16:59:30 +02:00
|
|
|
shouldRemove = true;
|
|
|
|
}
|
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Handle deletion from vector and on disk
|
2023-05-27 16:59:30 +02:00
|
|
|
if (shouldRemove) {
|
|
|
|
wolv::io::fs::remove(recentEntry.entryFilePath);
|
|
|
|
it = s_recentEntries.erase(it);
|
|
|
|
} else {
|
2023-11-10 20:47:08 +01:00
|
|
|
++it;
|
2023-05-27 16:59:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::EndSubWindow();
|
2023-05-27 16:59:30 +02:00
|
|
|
}
|
|
|
|
|
2023-11-17 15:54:38 +01:00
|
|
|
void addMenuItems() {
|
2023-05-27 16:59:30 +02:00
|
|
|
ContentRegistry::Interface::addMenuItemSubMenu({ "hex.builtin.menu.file" }, 1200, [] {
|
|
|
|
if (ImGui::BeginMenu("hex.builtin.menu.file.open_recent"_lang, !recent::s_recentEntriesUpdating && !s_recentEntries.empty())) {
|
|
|
|
// Copy to avoid changing list while iteration
|
|
|
|
auto recentEntries = s_recentEntries;
|
|
|
|
for (auto &recentEntry : recentEntries) {
|
|
|
|
if (ImGui::MenuItem(recentEntry.displayName.c_str())) {
|
|
|
|
loadRecentEntry(recentEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::Separator();
|
|
|
|
if (ImGui::MenuItem("hex.builtin.menu.file.clear_recent"_lang)) {
|
|
|
|
s_recentEntries.clear();
|
|
|
|
|
|
|
|
// Remove all recent files
|
|
|
|
for (const auto &recentPath : fs::getDefaultPaths(fs::ImHexPath::Recent))
|
|
|
|
for (const auto &entry : std::fs::directory_iterator(recentPath))
|
|
|
|
std::fs::remove(entry.path());
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|