Skip to content

Commit

Permalink
Game Browser: Support going up from the startup directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghabry committed Apr 1, 2024
1 parent 4bdfb75 commit 5f0c5d1
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/filefinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ FilesystemView FileFinder::Root() {
root_fs = std::make_unique<RootFilesystem>();
}

return root_fs->Subtree("");
return *root_fs;
}

std::string FileFinder::MakePath(StringView dir, StringView name) {
Expand Down
14 changes: 14 additions & 0 deletions src/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,20 @@ FilesystemView FilesystemView::Subtree(StringView sub_path) const {
return FilesystemView(fs, MakePath(sub_path));
}

bool FilesystemView::CanGoUp() const {
return static_cast<bool>(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()) {
Expand Down
13 changes: 13 additions & 0 deletions src/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
28 changes: 18 additions & 10 deletions src/main_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@
#include "system.h"
#include "output.h"

#ifndef _WIN32
#ifdef _WIN32
# include <windows.h>
#else
# include <unistd.h>
#endif

#if defined(USE_SDL) && defined(__ANDROID__)
# include <jni.h>
# include <SDL_system.h>
Expand Down Expand Up @@ -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
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/scene_gamebrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ void Scene_GameBrowser::CreateWindows() {
command_window->SetIndex(0);

gamelist_window = std::make_unique<Window_GameList>(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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/window_gamelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> error_msg = {
#ifdef EMSCRIPTEN
Expand All @@ -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."
Expand Down
4 changes: 0 additions & 4 deletions tests/filefinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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));
}
Expand Down
1 change: 0 additions & 1 deletion tests/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 5f0c5d1

Please sign in to comment.