Optimizations (are they?)

This commit is contained in:
CocoSimone
2023-02-17 17:16:56 +01:00
parent 2bc2227b84
commit 55f792d995
18 changed files with 430 additions and 449 deletions

View File

@@ -1,167 +0,0 @@
#include <GameList.hpp>
#include <filesystem>
#include <imgui.h>
#include <fstream>
#include <nlohmann/json.hpp>
#include <fmt/format.h>
#include <RomHelpers.hpp>
#include <File.hpp>
#include <thread>
#include <algorithm>
using namespace nlohmann;
namespace fs = std::filesystem;
GameList::GameList(const std::string& path) {
Create(path);
}
void GameList::Create(const std::string &path) {
threadDone = false;
if(!path.empty()) {
std::thread searchThread([path, this]() {
std::ifstream gameDbFile("resources/db.json");
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(threadDone) {
gamesList.clear();
break;
}
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);
}
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<u8>(file), std::istream_iterator<u8>());
file.close();
u32 crc{};
Util::GetRomCRC(sizeAdjusted, rom.data(), crc);
bool found = false;
for(const auto& item : gameDb["items"]) {
const auto& crcEntry = item["crc"];
if(!crcEntry.empty()) {
if(crcEntry.get<std::string>() == fmt::format("{:08X}", crc)) {
found = true;
gamesList.push_back(GameInfo{
item["name"].get<std::string>(),
item["region"].get<std::string>(),
fmt::format("{:.2f} MiB", float(size) / 1024 / 1024),
"Good",
p.path().string()
});
}
}
}
if(!found) {
gamesList.push_back(GameInfo{
p.path().stem().string(),
"Unknown",
fmt::format("{:.2f} MiB", float(size) / 1024 / 1024),
"Unknown",
p.path().string()
});
}
}
};
gameDbFile.close();
threadDone = true;
});
searchThread.detach();
}
}
bool GameList::RenderWidget(float mainMenuBarHeight, std::string& rom) {
const auto windowSize = ImGui::GetIO().DisplaySize;
ImGui::SetNextWindowPos(ImVec2(0, mainMenuBarHeight));
ImGui::SetNextWindowSize(ImVec2(windowSize.x, windowSize.y - mainMenuBarHeight));
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.f, 0.f));
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.f);
ImGui::Begin(
"Games list",
nullptr,
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoBringToFrontOnFocus
);
static ImGuiTableFlags flags =
ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Sortable | ImGuiTableFlags_SortMulti
| ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_NoBordersInBody
| ImGuiTableFlags_ScrollY;
bool toOpen = false;
if (ImGui::BeginTable("Games List", 4, flags)) {
ImGui::TableSetupColumn("Title");
ImGui::TableSetupColumn("Region");
ImGui::TableSetupColumn("Status");
ImGui::TableSetupColumn("Size");
ImGui::TableSetupScrollFreeze(0, 1); // Make row always visible
ImGui::TableHeadersRow();
int i = 0;
for (const auto& entry : gamesList) {
ImGui::TableNextRow(ImGuiTableRowFlags_None);
ImGui::PushID(i);
ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, ImVec2(0.0f, 0.5f));
ImGui::TableSetColumnIndex(0);
if (ImGui::Selectable(entry.name.c_str(), false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap, ImVec2(0.0f, 20.f))) {
toOpen = true;
rom = entry.path;
}
ImGui::TableSetColumnIndex(1);
if (ImGui::Selectable(entry.region.c_str(), false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap, ImVec2(0.0f, 20.f))) {
toOpen = true;
rom = entry.path;
}
ImGui::TableSetColumnIndex(2);
if (ImGui::Selectable(entry.status.c_str(), false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap, ImVec2(0.0f, 20.f))) {
toOpen = true;
rom = entry.path;
}
ImGui::TableSetColumnIndex(3);
if (ImGui::Selectable(entry.size.c_str(), false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap, ImVec2(0.0f, 20.f))) {
toOpen = true;
rom = entry.path;
}
ImGui::PopStyleVar();
ImGui::PopID();
i++;
}
ImGui::EndTable();
}
ImGui::End();
ImGui::PopStyleVar();
ImGui::PopStyleVar();
return toOpen;
}

View File

@@ -1,21 +0,0 @@
#pragma once
#include <vector>
#include <string>
#include <atomic>
struct GameInfo {
std::string name, region, size, status, path;
};
struct GameList {
GameList(const std::string&);
~GameList() = default;
void Create(const std::string&);
bool RenderWidget(float, std::string&);
[[nodiscard]] std::vector<GameInfo> GetGamesList() const { return gamesList; }
std::atomic_bool threadDone = false;
private:
std::vector<GameInfo> gamesList{}, notMatch{};
};

View File

@@ -12,7 +12,7 @@
VkInstance instance{};
namespace fs = std::filesystem;
Window::Window(n64::Core& core) : settings(core), gameList(settings.GetGamesDir()) {
Window::Window(n64::Core& core) : settings(core) {
InitSDL();
InitParallelRDP(core.mem.GetRDRAM(), window);
InitImgui();
@@ -156,7 +156,6 @@ ImDrawData* Window::Present(n64::Core& core) {
void Window::LoadROM(n64::Core& core, const std::string &path) {
if(!path.empty()) {
gameList.threadDone = true;
n64::CartInfo cartInfo = core.LoadROM(path);
std::ifstream gameDbFile("resources/db.json");
json gameDb = json::parse(gameDbFile);
@@ -181,7 +180,6 @@ void Window::LoadROM(n64::Core& core, const std::string &path) {
Util::UpdateRPC(Util::Playing, gameName);
windowTitle = "Gadolinium - " + gameName;
shadowWindowTitle = windowTitle;
renderGameList = false;
SDL_SetWindowTitle(window, windowTitle.c_str());
gameDbFile.close();
@@ -190,7 +188,6 @@ void Window::LoadROM(n64::Core& core, const std::string &path) {
void Window::RenderMainMenuBar(n64::Core &core) {
ImGui::BeginMainMenuBar();
mainMenuBarHeight = ImGui::GetWindowSize().y;
if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("Open", "O")) {
@@ -222,12 +219,10 @@ void Window::RenderMainMenuBar(n64::Core &core) {
LoadROM(core, core.rom);
}
if (ImGui::MenuItem("Stop")) {
renderGameList = true;
windowTitle = "Gadolinium";
core.rom.clear();
Util::UpdateRPC(Util::Idling);
SDL_SetWindowTitle(window, windowTitle.c_str());
gameList.Create(settings.GetGamesDir());
core.Stop();
}
if (ImGui::MenuItem(core.pause ? "Resume" : "Pause", nullptr, false, core.romLoaded)) {
@@ -267,13 +262,6 @@ void Window::Render(n64::Core& core) {
RenderMainMenuBar(core);
}
static std::string rom{};
if(renderGameList && gameList.RenderWidget(mainMenuBarHeight, rom)) {
LoadROM(core, rom);
renderGameList = false;
}
mainMenuBarHeight = 0;
settings.RenderWidget(showSettings);
ImGui::PopFont();

View File

@@ -7,7 +7,6 @@
#include <backend/Core.hpp>
#include <vector>
#include <frontend/imgui/Settings.hpp>
#include <frontend/imgui/GameList.hpp>
struct Window {
explicit Window(n64::Core& core);
@@ -17,12 +16,9 @@ struct Window {
[[nodiscard]] bool gotClosed(SDL_Event event);
ImFont *uiFont{};
Settings settings;
GameList gameList;
void LoadROM(n64::Core& core, const std::string& path);
private:
bool renderGameList = true;
bool showSettings = false;
float mainMenuBarHeight = 0;
SDL_Window* window{};
std::string windowTitle{"Gadolinium"};
std::string shadowWindowTitle{windowTitle};