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,60 +4,77 @@
#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::ifstream gameDbFile("resources/db.json"); std::thread searchThread([path, this]() {
json gameDb = json::parse(gameDbFile); std::ifstream gameDbFile("resources/db.json");
std::vector<u8> rom{}; json gameDb = json::parse(gameDbFile);
std::vector<u8> 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}) { if(!file.is_open()) {
const auto filename = p.path().string(); util::panic("Unable to open {}!", filename);
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()) { file.seekg(0, std::ios::end);
util::panic("Unable to open {}!", filename); auto size = file.tellg();
} auto sizeAdjusted = util::NextPow2(size);
file.seekg(0, std::ios::beg);
file.seekg(0, std::ios::end); std::fill(rom.begin(), rom.end(), 0);
auto size = file.tellg(); rom.resize(sizeAdjusted);
auto sizeAdjusted = util::NextPow2(size); rom.insert(rom.begin(), std::istream_iterator<u8>(file), std::istream_iterator<u8>());
file.seekg(0, std::ios::beg); file.close();
std::fill(rom.begin(), rom.end(), 0); u32 crc{};
rom.resize(sizeAdjusted); util::GetRomCRC(sizeAdjusted, rom.data(), crc);
rom.insert(rom.begin(), std::istream_iterator<u8>(file), std::istream_iterator<u8>());
file.close();
u32 crc{}; bool found = false;
util::SwapN64RomJustCRC(sizeAdjusted, rom.data(), crc);
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)) {
gamesList.push_back({ found = true;
item["name"].template get<std::string>(), gamesList.push_back({
item["region"].template get<std::string>(), item["name"].get<std::string>(),
fmt::format("{:.2f} MiB", float(size) / 1024 / 1024), item["region"].get<std::string>(),
"Good", fmt::format("{:.2f} MiB", float(size) / 1024 / 1024),
p.path().string() "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();
} }
} }

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{};
}; };