Skip to content

Commit

Permalink
Autorun a specific game
Browse files Browse the repository at this point in the history
If a game file ends with `.easyrpg` and there's only one game in the folder, the player will autorun it.
  • Loading branch information
jetrotal authored and Ghabry committed Dec 5, 2023
1 parent ec039e6 commit 6533162
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
34 changes: 34 additions & 0 deletions src/filefinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,40 @@ bool FileFinder::IsRPG2kProjectWithRenames(const FilesystemView& fs) {
return !FileExtGuesser::GetRPG2kProjectWithRenames(fs).Empty();
}

bool FileFinder::OpenViewToEasyRpgFile(FilesystemView& fs) {
auto files = fs.ListDirectory();
if (!files) {
return false;
}

int items = 0;
std::string filename;

for (auto& file : *files) {
if (StringView(file.second.name).ends_with(".easyrpg")) {
++items;
if (items == 2) {
// Contains more than one game
return false;
}
filename = file.second.name;
}
}

if (filename.empty()) {
return false;
}

// One candidate to check
auto ep_fs = fs.Create(filename);
if (FileFinder::IsValidProject(ep_fs)) {
fs = ep_fs;
return true;
} else {
return false;
}
}

bool FileFinder::HasSavegame() {
return GetSavegames() > 0;
}
Expand Down
17 changes: 13 additions & 4 deletions src/filefinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,19 @@ namespace FileFinder {
bool IsSupportedArchiveExtension(std::string path);

/**
* @param p tree Tree to check
* @param p fs Tree to check
* @return Whether the tree contains a valid RPG2k(3) or EasyRPG project
*/
bool IsValidProject(const FilesystemView& fs);

/**
* @param p tree Tree to check
* @param p fs Tree to check
* @return Whether the tree contains a valid RPG2k(3) project
*/
bool IsRPG2kProject(const FilesystemView& fs);

/**
* @param p tree Tree to check
* @param p fs Tree to check
* @return Whether the tree contains a valid EasyRPG project
*/
bool IsEasyRpgProject(const FilesystemView& fs);
Expand All @@ -274,11 +274,20 @@ namespace FileFinder {
* Determines if the directory in question represents an RPG2k project with non-standard
* database, map tree, or map file names.
*
* @param tree The directory tree in question
* @param fs The directory tree in question
* @return true if this is likely an RPG2k project; false otherwise
*/
bool IsRPG2kProjectWithRenames(const FilesystemView& fs);

/**
* Determines if the directory contains a single file/directory ending in ".easyrpg" for use in the
* autostart feature.
*
* @param fs The directory tree to check. Is replaced with a view to the game for autorun.
* @return true when autorun is possible, fs contains the new view; when false fs is not modified
*/
bool OpenViewToEasyRpgFile(FilesystemView& fs);

/**
* Checks whether the save directory contains any savegame with name
* SaveXX.lsd (XX from 00 to 15).
Expand Down
2 changes: 1 addition & 1 deletion src/scene_gamebrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void Scene_GameBrowser::BootGame() {
return;
}

if (!FileFinder::IsValidProject(fs)) {
if (!FileFinder::IsValidProject(fs) && !FileFinder::OpenViewToEasyRpgFile(fs)) {
// Not a game: Open as directory
load_window->SetVisible(false);
game_loading = false;
Expand Down
2 changes: 1 addition & 1 deletion src/scene_logo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ bool Scene_Logo::DetectGame() {
}
#endif

if (FileFinder::IsValidProject(fs)) {
if (FileFinder::IsValidProject(fs) || FileFinder::OpenViewToEasyRpgFile(fs)) {
Player::CreateGameObjects();
detected_game = true;
}
Expand Down

0 comments on commit 6533162

Please sign in to comment.