Use thread for GameList's searching

This commit is contained in:
CocoSimone
2022-12-23 18:42:28 +01:00
parent 815075986b
commit 9f968d3924
2 changed files with 56 additions and 39 deletions

View File

@@ -4,19 +4,20 @@
#include <fstream> #include <fstream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <fmt/format.h> #include <fmt/format.h>
#include <MemoryHelpers.hpp> #include <RomHelpers.hpp>
#include <File.hpp> #include <File.hpp>
#include <thread>
using namespace nlohmann; using namespace nlohmann;
namespace fs = std::filesystem; namespace fs = std::filesystem;
GameList::GameList(const std::string& path) { GameList::GameList(const std::string& path) {
if(!path.empty()) { if(!path.empty()) {
std::thread searchThread([path, this]() {
std::ifstream gameDbFile("resources/db.json"); std::ifstream gameDbFile("resources/db.json");
json gameDb = json::parse(gameDbFile); json gameDb = json::parse(gameDbFile);
std::vector<u8> rom{}; std::vector<u8> rom{};
for(const auto& p : fs::recursive_directory_iterator{path}) {
for(const auto& p : fs::directory_iterator{path}) {
const auto filename = p.path().string(); const auto filename = p.path().string();
if(p.path().extension() == ".n64" || p.path().extension() == ".z64" || p.path().extension() == ".v64" || 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") { p.path().extension() == ".N64" || p.path().extension() == ".Z64" || p.path().extension() == ".V64") {
@@ -38,26 +39,42 @@ GameList::GameList(const std::string& path) {
file.close(); file.close();
u32 crc{}; u32 crc{};
util::SwapN64RomJustCRC(sizeAdjusted, rom.data(), crc); util::GetRomCRC(sizeAdjusted, rom.data(), crc);
bool found = false;
for(const auto& item : gameDb["items"]) { for(const auto& item : gameDb["items"]) {
const auto& crcEntry = item["crc"]; const auto& crcEntry = item["crc"];
if(!crcEntry.empty()) { if(!crcEntry.empty()) {
if(crcEntry.template get<std::string>() == fmt::format("{:08X}", crc)) { if(crcEntry.get<std::string>() == fmt::format("{:08X}", crc)) {
found = true;
gamesList.push_back({ gamesList.push_back({
item["name"].template get<std::string>(), item["name"].get<std::string>(),
item["region"].template get<std::string>(), item["region"].get<std::string>(),
fmt::format("{:.2f} MiB", float(size) / 1024 / 1024), fmt::format("{:.2f} MiB", float(size) / 1024 / 1024),
"Good", "Good",
p.path().string() p.path().string()
}); });
} }
} }
}; }
if(!found) {
gamesList.push_back({
p.path().stem(),
"Unknown",
fmt::format("{:.2f} MiB", float(size) / 1024 / 1024),
"Unknown",
p.path().string()
});
}
} }
}; };
gameDbFile.close(); gameDbFile.close();
});
searchThread.detach();
} }
} }

View File

@@ -12,7 +12,7 @@ struct GameList {
bool RenderWidget(float, std::string&); bool RenderWidget(float, std::string&);
std::vector<GameInfo> GetGamesList() const { return gamesList; } [[nodiscard]] std::vector<GameInfo> GetGamesList() const { return gamesList; }
private: private:
std::vector<GameInfo> gamesList{}, notMatch{}; std::vector<GameInfo> gamesList{}, notMatch{};
}; };