Skip to content

Commit

Permalink
Added --add-game-folder argument
Browse files Browse the repository at this point in the history
Also added "treat the last argument as the game, if it isn't found already" option to the qt version
  • Loading branch information
kalaposfos13 authored and ngoquang2708 committed Dec 28, 2024
1 parent 9652164 commit cd0d9e7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
28 changes: 28 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
#include "functional"
#include "iostream"
#include "string"
#include "system_error"
#include "unordered_map"

#include <fmt/core.h>
#include "common/config.h"
#include "common/memory_patcher.h"
#include "common/path_util.h"
#include "core/file_sys/fs.h"
#include "emulator.h"

#ifdef _WIN32
Expand Down Expand Up @@ -38,6 +40,7 @@ int main(int argc, char* argv[]) {
" -p, --patch <patch_file> Apply specified patch file\n"
" -f, --fullscreen <true|false> Specify window initial fullscreen "
"state. Does not overwrite the config file.\n"
" --add-game-folder <folder> Adds a new game folder to the config.\n"
" -h, --help Display this help message\n";
exit(0);
}},
Expand Down Expand Up @@ -86,6 +89,25 @@ int main(int argc, char* argv[]) {
Config::setFullscreenMode(is_fullscreen);
}},
{"--fullscreen", [&](int& i) { arg_map["-f"](i); }},
{"--add-game-folder",
[&](int& i) {
if (++i >= argc) {
std::cerr << "Error: Missing argument for --add-game-folder\n";
exit(1);
}
std::string config_dir(argv[i]);
std::filesystem::path config_path = std::filesystem::path(config_dir);
std::error_code discard;
if (!std::filesystem::exists(config_path, discard)) {
std::cerr << "Error: File does not exist: " << config_path << "\n";
exit(1);
}

Config::addGameInstallDir(config_path);
Config::save(Common::FS::GetUserPath(Common::FS::PathType::UserDir) / "config.toml");
std::cout << "Game folder successfully saved.\n";
exit(0);
}},
};

if (argc == 1) {
Expand All @@ -110,6 +132,12 @@ int main(int argc, char* argv[]) {
}
}

// If no game directory is set and no command line argument, prompt for it
if (Config::getGameInstallDirs().empty()) {
std::cout << "Warning: No game folder set, please set it by calling shadps4"
" with the --add-game-folder <folder_name> argument";
}

if (!has_game_argument) {
std::cerr << "Error: Please provide a game path or ID.\n";
exit(1);
Expand Down
35 changes: 30 additions & 5 deletions src/qt_gui/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later

#include "iostream"
#include "system_error"
#include "unordered_map"

#include "common/config.h"
Expand Down Expand Up @@ -31,7 +32,7 @@ int main(int argc, char* argv[]) {

bool has_command_line_argument = argc > 1;
bool show_gui = false, has_game_argument = false;
std::string gamePath;
std::string game_path;

// Map of argument strings to lambda functions
std::unordered_map<std::string, std::function<void(int&)>> arg_map = {
Expand All @@ -46,6 +47,7 @@ int main(int argc, char* argv[]) {
" -s, --show-gui Show the GUI\n"
" -f, --fullscreen <true|false> Specify window initial fullscreen "
"state. Does not overwrite the config file.\n"
" --add-game-folder <folder> Adds a new game folder to the config.\n"
" -h, --help Display this help message\n";
exit(0);
}},
Expand All @@ -57,7 +59,7 @@ int main(int argc, char* argv[]) {
{"-g",
[&](int& i) {
if (i + 1 < argc) {
gamePath = argv[++i];
game_path = argv[++i];
has_game_argument = true;
} else {
std::cerr << "Error: Missing argument for -g/--game\n";
Expand Down Expand Up @@ -98,6 +100,25 @@ int main(int argc, char* argv[]) {
Config::setFullscreenMode(is_fullscreen);
}},
{"--fullscreen", [&](int& i) { arg_map["-f"](i); }},
{"--add-game-folder",
[&](int& i) {
if (++i >= argc) {
std::cerr << "Error: Missing argument for --add-game-folder\n";
exit(1);
}
std::string config_dir(argv[i]);
std::filesystem::path config_path = std::filesystem::path(config_dir);
std::error_code discard;
if (!std::filesystem::is_directory(config_path, discard)) {
std::cerr << "Error: Directory does not exist: " << config_path << "\n";
exit(1);
}

Config::addGameInstallDir(config_path);
Config::save(Common::FS::GetUserPath(Common::FS::PathType::UserDir) / "config.toml");
std::cout << "Game folder successfully saved.\n";
exit(0);
}},
};

// Parse command-line arguments using the map
Expand All @@ -106,6 +127,10 @@ int main(int argc, char* argv[]) {
auto it = arg_map.find(cur_arg);
if (it != arg_map.end()) {
it->second(i); // Call the associated lambda function
} else if (i == argc - 1 && !has_game_argument) {
// Assume the last argument is the game file if not specified via -g/--game
game_path = argv[i];
has_game_argument = true;
} else {
std::cerr << "Unknown argument: " << cur_arg << ", see --help for info.\n";
return 1;
Expand Down Expand Up @@ -134,22 +159,22 @@ int main(int argc, char* argv[]) {

// Process game path or ID if provided
if (has_game_argument) {
std::filesystem::path game_file_path(gamePath);
std::filesystem::path game_file_path(game_path);

// Check if the provided path is a valid file
if (!std::filesystem::exists(game_file_path)) {
// If not a file, treat it as a game ID and search in install directories
bool game_found = false;
for (const auto& install_dir : Config::getGameInstallDirs()) {
auto potential_game_path = install_dir / gamePath / "eboot.bin";
auto potential_game_path = install_dir / game_path / "eboot.bin";
if (std::filesystem::exists(potential_game_path)) {
game_file_path = potential_game_path;
game_found = true;
break;
}
}
if (!game_found) {
std::cerr << "Error: Game ID or file path not found: " << gamePath << std::endl;
std::cerr << "Error: Game ID or file path not found: " << game_path << std::endl;
return 1;
}
}
Expand Down

0 comments on commit cd0d9e7

Please sign in to comment.