From 5f0c5d10998ab8d22737d84dc24b9906c4bf43bf Mon Sep 17 00:00:00 2001 From: Ghabry Date: Mon, 1 Apr 2024 15:06:20 +0200 Subject: [PATCH] Game Browser: Support going up from the startup directory --- src/filefinder.cpp | 2 +- src/filesystem.cpp | 14 ++++++++++++++ src/filesystem.h | 13 +++++++++++++ src/main_data.cpp | 28 ++++++++++++++++++---------- src/scene_gamebrowser.cpp | 18 ++++++++++++------ src/window_gamelist.cpp | 8 +++++++- tests/filefinder.cpp | 4 ---- tests/output.cpp | 1 - 8 files changed, 65 insertions(+), 23 deletions(-) diff --git a/src/filefinder.cpp b/src/filefinder.cpp index cc6b9004cc..bc08b96770 100644 --- a/src/filefinder.cpp +++ b/src/filefinder.cpp @@ -143,7 +143,7 @@ FilesystemView FileFinder::Root() { root_fs = std::make_unique(); } - return root_fs->Subtree(""); + return *root_fs; } std::string FileFinder::MakePath(StringView dir, StringView name) { diff --git a/src/filesystem.cpp b/src/filesystem.cpp index 132558f60c..a6757392a6 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -382,6 +382,20 @@ FilesystemView FilesystemView::Subtree(StringView sub_path) const { return FilesystemView(fs, MakePath(sub_path)); } +bool FilesystemView::CanGoUp() const { + return static_cast(GoUp()); +} + +FilesystemView FilesystemView::GoUp() const { + if (GetSubPath().empty() || GetSubPath() == "/") { + return fs->GetParent(); + } + + auto [path, file] = FileFinder::GetPathAndFilename(GetSubPath()); + + return FilesystemView(fs, path); +} + std::string FilesystemView::Describe() const { assert(fs); if (GetSubPath().empty()) { diff --git a/src/filesystem.h b/src/filesystem.h index 02936dcdee..736d476016 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -475,6 +475,19 @@ class FilesystemView { */ FilesystemView Subtree(StringView sub_path) const; + /** + * @return Whether it is possible to go up from the current view + */ + bool CanGoUp() const; + + /** + * From the current view goes up by one. + * Returns an invalid view when going up is not possible (no parent). + * + * @return View that is rooted at the parent. + */ + FilesystemView GoUp() const; + /** @return human readable representation of this filesystem for debug purposes */ std::string Describe() const; diff --git a/src/main_data.cpp b/src/main_data.cpp index 86d4a8cc31..33a6a8ff01 100644 --- a/src/main_data.cpp +++ b/src/main_data.cpp @@ -41,9 +41,12 @@ #include "system.h" #include "output.h" -#ifndef _WIN32 +#ifdef _WIN32 +# include +#else # include #endif + #if defined(USE_SDL) && defined(__ANDROID__) # include # include @@ -89,22 +92,27 @@ void Main_Data::Init() { // Set to current directory project_path = ""; -#if defined(PLAYER_AMIGA) - // Working directory not correctly handled - char working_dir[256]; - getcwd(working_dir, 255); - project_path = std::string(working_dir); -#elif defined(__APPLE__) && TARGET_OS_OSX +#ifdef _WIN32 + wchar_t working_dir[MAX_PATH]; + if (GetCurrentDirectory(MAX_PATH, working_dir) != 0) { + project_path = Utils::FromWideString(working_dir); + } +#else + char working_dir[PATH_MAX]; + if (getcwd(working_dir, sizeof(working_dir))) { + project_path = std::string(working_dir); + } + +# if defined(__APPLE__) && TARGET_OS_OSX // Apple Finder does not set the working directory // It points to HOME instead. When it is HOME change it to // the application directory instead char* home = getenv("HOME"); - char current_dir[PATH_MAX] = { 0 }; - getcwd(current_dir, sizeof(current_dir)); - if (strcmp(current_dir, "/") == 0 || strcmp(current_dir, home) == 0) { + if (strcmp(working_dir, "/") == 0 || strcmp(working_dir, home) == 0) { project_path = MacOSUtils::GetBundleDir(); } +# endif #endif } } diff --git a/src/scene_gamebrowser.cpp b/src/scene_gamebrowser.cpp index 5a0b700569..06b731b8f5 100644 --- a/src/scene_gamebrowser.cpp +++ b/src/scene_gamebrowser.cpp @@ -99,9 +99,9 @@ void Scene_GameBrowser::CreateWindows() { command_window->SetIndex(0); gamelist_window = std::make_unique(0, 64, Player::screen_width, Player::screen_height - 64); - gamelist_window->Refresh(stack.back().filesystem, false); + gamelist_window->Refresh(stack.back().filesystem, stack.back().filesystem.CanGoUp()); - if (stack.size() == 1 && !gamelist_window->HasValidEntry()) { + if (stack.size() == 1 && !stack.back().filesystem.CanGoUp() && !gamelist_window->HasValidEntry()) { command_window->DisableItem(0); } @@ -140,7 +140,7 @@ void Scene_GameBrowser::UpdateCommand() { switch (menu_index) { case GameList: - if (stack.size() == 1 && !gamelist_window->HasValidEntry()) { + if (!command_window->IsItemEnabled(0)) { return; } command_window->SetActive(false); @@ -177,11 +177,17 @@ void Scene_GameBrowser::UpdateGameListSelection() { } void Scene_GameBrowser::BootGame() { - if (stack.size() > 1 && gamelist_window->GetIndex() == 0) { + if (stack.back().filesystem.CanGoUp() && gamelist_window->GetIndex() == 0) { // ".." -> Go one level up int index = stack.back().index; - stack.pop_back(); - gamelist_window->Refresh(stack.back().filesystem, stack.size() > 1); + + if (stack.size() == 1) { + stack.back() = {stack.back().filesystem.GoUp(), 0}; + } else { + stack.pop_back(); + } + + gamelist_window->Refresh(stack.back().filesystem, stack.back().filesystem.CanGoUp()); gamelist_window->SetIndex(index); load_window->SetVisible(false); game_loading = false; diff --git a/src/window_gamelist.cpp b/src/window_gamelist.cpp index bf6b58fed0..7a0d2c7ce0 100644 --- a/src/window_gamelist.cpp +++ b/src/window_gamelist.cpp @@ -111,6 +111,12 @@ void Window_GameList::DrawItem(int index) { contents->TextDraw(rect.x, rect.y, Font::ColorDefault, game_directories[index]); } +#ifdef HAVE_LHASA +#define LZH_STR "/LZH" +#else +#define LZH_STR "" +#endif + void Window_GameList::DrawErrorText(bool show_dotdot) { std::vector error_msg = { #ifdef EMSCRIPTEN @@ -128,7 +134,7 @@ void Window_GameList::DrawErrorText(bool show_dotdot) { "with RPG Maker 2000 and RPG Maker 2003.", "", "These games have an RPG_RT.ldb and they can be", - "extracted or in ZIP archives.", + "extracted or in ZIP" LZH_STR " archives.", "", "Newer engines such as RPG Maker XP, VX, MV and MZ", "are not supported." diff --git a/tests/filefinder.cpp b/tests/filefinder.cpp index 60cabf16d8..b40f931c04 100644 --- a/tests/filefinder.cpp +++ b/tests/filefinder.cpp @@ -8,8 +8,6 @@ TEST_SUITE_BEGIN("FileFinder"); TEST_CASE("IsRPG2kProject") { - Main_Data::Init(); - Player::escape_symbol = "\\"; auto fs = FileFinder::Root().Subtree(EP_TEST_PATH "/game"); @@ -19,8 +17,6 @@ TEST_CASE("IsRPG2kProject") { } TEST_CASE("IsNotRPG2kProject") { - Main_Data::Init(); - auto fs = FileFinder::Root().Subtree(EP_TEST_PATH "/notagame"); CHECK(!FileFinder::IsRPG2kProject(fs)); } diff --git a/tests/output.cpp b/tests/output.cpp index 0305c06768..33bf759247 100644 --- a/tests/output.cpp +++ b/tests/output.cpp @@ -7,7 +7,6 @@ TEST_SUITE_BEGIN("Output"); TEST_CASE("Message Output") { Graphics::Init(); - Main_Data::Init(); Output::Debug("Test {}", "debg"); Output::Warning("Test {}", "test"); Output::Info("Test {}", "info");