1
0
mirror of synced 2025-01-18 09:04:52 +01:00

fix: Not all recent providers showing up correctly

This commit is contained in:
WerWolv 2022-08-24 00:18:10 +02:00
parent cc09014e6e
commit 4f1f9a718c

View File

@ -19,7 +19,9 @@
#include <fonts/codicons_font.h> #include <fonts/codicons_font.h>
#include <string>
#include <list> #include <list>
#include <unordered_set>
namespace hex::plugin::builtin { namespace hex::plugin::builtin {
@ -37,33 +39,44 @@ namespace hex::plugin::builtin {
nlohmann::json data; nlohmann::json data;
bool operator==(const RecentProvider &other) const { bool operator==(const RecentProvider &other) const {
return displayName == other.displayName && type == other.type && data == other.data; return HashFunction()(*this) == HashFunction()(other);
}
struct HashFunction {
std::size_t operator()(const RecentProvider& provider) const {
return
(std::hash<std::string>()(provider.displayName)) ^
(std::hash<std::string>()(provider.type) << 1);
} }
}; };
static std::vector<RecentProvider> s_recentProviders;
};
static std::list<RecentProvider> s_recentProviders;
static void updateRecentProviders() { static void updateRecentProviders() {
s_recentProviders.clear(); s_recentProviders.clear();
// Query all recent providers // Query all recent providers
std::vector<std::fs::path> recentFiles; std::vector<std::fs::path> recentFilePaths;
for (const auto &folder : fs::getDefaultPaths(fs::ImHexPath::Recent)) { for (const auto &folder : fs::getDefaultPaths(fs::ImHexPath::Recent)) {
for (const auto &entry : std::fs::directory_iterator(folder)) { for (const auto &entry : std::fs::directory_iterator(folder)) {
if (entry.is_regular_file()) if (entry.is_regular_file())
recentFiles.push_back(entry.path()); recentFilePaths.push_back(entry.path());
} }
} }
// Sort recent provider files by last modified time // Sort recent provider files by last modified time
std::sort(recentFiles.begin(), recentFiles.end(), [](const auto &a, const auto &b) { 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); return std::fs::last_write_time(a) > std::fs::last_write_time(b);
}); });
for (u32 i = 0; i < recentFiles.size() && s_recentProviders.size() < 5; i++) { std::unordered_set<RecentProvider, RecentProvider::HashFunction> uniqueProviders;
auto &path = recentFiles[i]; for (u32 i = 0; i < recentFilePaths.size() && uniqueProviders.size() < 5; i++) {
auto &path = recentFilePaths[i];
try { try {
auto jsonData = nlohmann::json::parse(fs::File(path, fs::File::Mode::Read).readString()); auto jsonData = nlohmann::json::parse(fs::File(path, fs::File::Mode::Read).readString());
s_recentProviders.push_back(RecentProvider { uniqueProviders.insert(RecentProvider {
.displayName = jsonData["displayName"], .displayName = jsonData["displayName"],
.type = jsonData["type"], .type = jsonData["type"],
.filePath = path, .filePath = path,
@ -72,8 +85,7 @@ namespace hex::plugin::builtin {
} catch (...) { } } catch (...) { }
} }
// De-duplicate recent providers std::copy(uniqueProviders.begin(), uniqueProviders.end(), std::front_inserter(s_recentProviders));
s_recentProviders.erase(std::unique(s_recentProviders.begin(), s_recentProviders.end()), s_recentProviders.end());
} }
static void loadRecentProvider(const RecentProvider &recentProvider) { static void loadRecentProvider(const RecentProvider &recentProvider) {
@ -89,7 +101,6 @@ namespace hex::plugin::builtin {
updateRecentProviders(); updateRecentProviders();
} }
} }
static void loadDefaultLayout() { static void loadDefaultLayout() {