From 9f968d392409916eb5d39a52d9b8c8b12e95fe35 Mon Sep 17 00:00:00 2001 From: CocoSimone Date: Fri, 23 Dec 2022 18:42:28 +0100 Subject: [PATCH] Use thread for GameList's searching --- src/frontend/imgui/GameList.cpp | 93 +++++++++++++++++++-------------- src/frontend/imgui/GameList.hpp | 2 +- 2 files changed, 56 insertions(+), 39 deletions(-) diff --git a/src/frontend/imgui/GameList.cpp b/src/frontend/imgui/GameList.cpp index 1786d879..b7dc50a4 100644 --- a/src/frontend/imgui/GameList.cpp +++ b/src/frontend/imgui/GameList.cpp @@ -4,60 +4,77 @@ #include #include #include -#include +#include #include +#include using namespace nlohmann; namespace fs = std::filesystem; GameList::GameList(const std::string& path) { if(!path.empty()) { - std::ifstream gameDbFile("resources/db.json"); - json gameDb = json::parse(gameDbFile); - std::vector rom{}; + std::thread searchThread([path, this]() { + std::ifstream gameDbFile("resources/db.json"); + json gameDb = json::parse(gameDbFile); + std::vector rom{}; + for(const auto& p : fs::recursive_directory_iterator{path}) { + const auto filename = p.path().string(); + if(p.path().extension() == ".n64" || p.path().extension() == ".z64" || p.path().extension() == ".v64" || + p.path().extension() == ".N64" || p.path().extension() == ".Z64" || p.path().extension() == ".V64") { + std::ifstream file(filename, std::ios::binary); + file.unsetf(std::ios::skipws); - for(const auto& p : fs::directory_iterator{path}) { - const auto filename = p.path().string(); - if(p.path().extension() == ".n64" || p.path().extension() == ".z64" || p.path().extension() == ".v64" || - p.path().extension() == ".N64" || p.path().extension() == ".Z64" || p.path().extension() == ".V64") { - std::ifstream file(filename, std::ios::binary); - file.unsetf(std::ios::skipws); + if(!file.is_open()) { + util::panic("Unable to open {}!", filename); + } - if(!file.is_open()) { - util::panic("Unable to open {}!", filename); - } + file.seekg(0, std::ios::end); + auto size = file.tellg(); + auto sizeAdjusted = util::NextPow2(size); + file.seekg(0, std::ios::beg); - file.seekg(0, std::ios::end); - auto size = file.tellg(); - auto sizeAdjusted = util::NextPow2(size); - file.seekg(0, std::ios::beg); + std::fill(rom.begin(), rom.end(), 0); + rom.resize(sizeAdjusted); + rom.insert(rom.begin(), std::istream_iterator(file), std::istream_iterator()); + file.close(); - std::fill(rom.begin(), rom.end(), 0); - rom.resize(sizeAdjusted); - rom.insert(rom.begin(), std::istream_iterator(file), std::istream_iterator()); - file.close(); + u32 crc{}; + util::GetRomCRC(sizeAdjusted, rom.data(), crc); - u32 crc{}; - util::SwapN64RomJustCRC(sizeAdjusted, rom.data(), crc); + bool found = false; - for(const auto& item : gameDb["items"]) { - const auto& crcEntry = item["crc"]; - if(!crcEntry.empty()) { - if(crcEntry.template get() == fmt::format("{:08X}", crc)) { - gamesList.push_back({ - item["name"].template get(), - item["region"].template get(), - fmt::format("{:.2f} MiB", float(size) / 1024 / 1024), - "Good", - p.path().string() - }); + for(const auto& item : gameDb["items"]) { + const auto& crcEntry = item["crc"]; + if(!crcEntry.empty()) { + if(crcEntry.get() == fmt::format("{:08X}", crc)) { + found = true; + gamesList.push_back({ + item["name"].get(), + item["region"].get(), + fmt::format("{:.2f} MiB", float(size) / 1024 / 1024), + "Good", + p.path().string() + }); + } } } - }; - } - }; - gameDbFile.close(); + if(!found) { + gamesList.push_back({ + p.path().stem(), + "Unknown", + fmt::format("{:.2f} MiB", float(size) / 1024 / 1024), + "Unknown", + p.path().string() + }); + } + } + }; + + gameDbFile.close(); + }); + + searchThread.detach(); } } diff --git a/src/frontend/imgui/GameList.hpp b/src/frontend/imgui/GameList.hpp index 89358b2f..b041905c 100644 --- a/src/frontend/imgui/GameList.hpp +++ b/src/frontend/imgui/GameList.hpp @@ -12,7 +12,7 @@ struct GameList { bool RenderWidget(float, std::string&); - std::vector GetGamesList() const { return gamesList; } + [[nodiscard]] std::vector GetGamesList() const { return gamesList; } private: std::vector gamesList{}, notMatch{}; };