Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing Config class to resolve isFullscreen flag from CLI arguments #1349

Closed
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ string(TIMESTAMP BUILD_DATE "%Y-%m-%d %H:%M:%S")

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/common/scm_rev.cpp.in" "${CMAKE_CURRENT_SOURCE_DIR}/src/common/scm_rev.cpp" @ONLY)

find_package(Boost 1.84.0 CONFIG)
find_package(Boost 1.84.0 CONFIG COMPONENTS program_options)
find_package(FFmpeg 5.1.2 MODULE)
find_package(fmt 10.2.0 CONFIG)
find_package(glslang 14.2.0 CONFIG)
Expand Down Expand Up @@ -762,7 +762,7 @@ endif()
create_target_directory_groups(shadps4)

target_link_libraries(shadps4 PRIVATE magic_enum::magic_enum fmt::fmt toml11::toml11 tsl::robin_map xbyak::xbyak Tracy::TracyClient RenderDoc::API FFmpeg::ffmpeg Dear_ImGui gcn)
target_link_libraries(shadps4 PRIVATE Boost::headers GPUOpen::VulkanMemoryAllocator sirit Vulkan::Headers xxHash::xxhash Zydis::Zydis glslang::SPIRV glslang::glslang SDL3::SDL3 pugixml::pugixml)
target_link_libraries(shadps4 PRIVATE Boost::headers Boost::program_options GPUOpen::VulkanMemoryAllocator sirit Vulkan::Headers xxHash::xxhash Zydis::Zydis glslang::SPIRV glslang::glslang SDL3::SDL3 pugixml::pugixml)

target_compile_definitions(shadps4 PRIVATE IMGUI_USER_CONFIG="imgui/imgui_config.h")
target_compile_definitions(Dear_ImGui PRIVATE IMGUI_USER_CONFIG="${PROJECT_SOURCE_DIR}/src/imgui/imgui_config.h")
Expand Down
41 changes: 41 additions & 0 deletions src/common/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// SPDX-License-Identifier: GPL-2.0-or-later

#include <fstream>
#include <iostream>
#include <string>
#include <boost/program_options.hpp>
#include <common/version.h>
#include <fmt/core.h>
#include <fmt/xchar.h> // for wstring support
Expand All @@ -28,8 +30,13 @@ std::filesystem::path find_fs_path_or(const basic_value<TC>& v, const K& ky,
}
} // namespace toml

namespace po = boost::program_options;

namespace Config {

// Program options map
static po::variables_map optionMap;

static bool isNeo = false;
static bool isFullscreen = false;
static bool playBGM = false;
Expand Down Expand Up @@ -88,6 +95,10 @@ bool isNeoMode() {
}

bool isFullscreenMode() {
if (optionMap.count("fullscreen")) {
return true;
}

return isFullscreen;
}

Expand Down Expand Up @@ -139,6 +150,14 @@ std::string getUpdateChannel() {
return updateChannel;
}

std::string getPatchFile() {
if (optionMap.contains("patch-file")) {
return optionMap["patch-file"].as<std::string>();
}

return "";
}

std::string getBackButtonBehavior() {
return backButtonBehavior;
}
Expand Down Expand Up @@ -483,6 +502,7 @@ void load(const std::filesystem::path& path) {
}
isShowSplash = toml::find_or<bool>(general, "showSplash", true);
isAutoUpdate = toml::find_or<bool>(general, "autoUpdate", false);
backButtonBehavior = toml::find_or<std::string>(general, "backButtonBehavior", "left");
}

if (data.contains("Input")) {
Expand Down Expand Up @@ -565,6 +585,27 @@ void load(const std::filesystem::path& path) {
m_language = toml::find_or<int>(settings, "consoleLanguage", 1);
}
}

bool loadArgs(int argc, char* argv[]) {
// Declare the supported options.
std::string description = fmt::format("Usage: {} [options] <elf or eboot.bin path>", argv[0]);
po::options_description desc(description);
auto options = desc.add_options();
options("help", "Shows this help message");
options("patch-file,p", po::value<std::string>(), "Specifies the patch file");
options("fullscreen,f", "Switches the emulator to fullscreen mode");

po::store(po::parse_command_line(argc, argv, desc), optionMap);
po::notify(optionMap);

if (argc == 1 || optionMap.count("help")) {
std::cout << desc << "\n";
return false;
}

return true;
}

void save(const std::filesystem::path& path) {
toml::value data;

Expand Down
5 changes: 5 additions & 0 deletions src/common/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum HideCursorState : s16 { Never, Idle, Always };

void load(const std::filesystem::path& path);
void save(const std::filesystem::path& path);
bool loadArgs(int argc, char* argv[]);

bool isNeoMode();
bool isFullscreenMode();
Expand All @@ -24,6 +25,8 @@ std::string getLogFilter();
std::string getLogType();
std::string getUserName();
std::string getUpdateChannel();
std::string getPatchFile();
std::string getBackButtonBehavior();

s16 getCursorState();
int getCursorHideTimeout();
Expand Down Expand Up @@ -62,6 +65,8 @@ void setLanguage(u32 language);
void setNeoMode(bool enable);
void setUserName(const std::string& type);
void setUpdateChannel(const std::string& type);
void setPatchFile(const std::string& fileName);
void setBackButtonBehavior(const std::string& type);

void setCursorState(s16 cursorState);
void setCursorHideTimeout(int newcursorHideTimeout);
Expand Down
2 changes: 1 addition & 1 deletion src/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#include "common/logging/log.h"
#ifdef ENABLE_QT_GUI
#include <QtCore>
#include "common/memory_patcher.h"
#endif
#include "common/assert.h"
#include "common/discord_rpc_handler.h"
#include "common/elf_info.h"
#include "common/memory_patcher.h"
#include "common/ntapi.h"
#include "common/path_util.h"
#include "common/polyfill_thread.h"
Expand Down
18 changes: 9 additions & 9 deletions src/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 <fmt/core.h>
#include "common/config.h"
#include "common/memory_patcher.h"
#include "emulator.h"

Expand All @@ -14,6 +15,13 @@ int main(int argc, char* argv[]) {
SetConsoleOutputCP(CP_UTF8);
#endif

// Parse command line arguments
if (!Config::loadArgs(argc, argv)) {
return 0;
}

MemoryPatcher::patchFile = Config::getPatchFile();

if (argc == 1) {
fmt::print("Usage: {} <elf or eboot.bin path>\n", argv[0]);
return -1;
Expand All @@ -24,16 +32,8 @@ int main(int argc, char* argv[]) {
return -1;
}

for (int i = 0; i < argc; i++) {
std::string curArg = argv[i];
if (curArg == "-p") {
std::string patchFile = argv[i + 1];
MemoryPatcher::patchFile = patchFile;
}
}

Core::Emulator emulator;
emulator.Run(argv[1]);
emulator.Run(argv[argc - 1]);

return 0;
}
17 changes: 8 additions & 9 deletions src/qt_gui/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ int main(int argc, char* argv[]) {
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
#endif
// Parse command line arguments
if (!Config::loadArgs(argc, argv)) {
return 0;
}

QApplication a(argc, argv);
MemoryPatcher::patchFile = Config::getPatchFile();

// Load configurations and initialize Qt application
const auto user_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir);
Config::load(user_dir / "config.toml");

QApplication a(argc, argv);

// Check if elf or eboot.bin path was passed as a command line argument
bool has_command_line_argument = argc > 1;

Expand All @@ -45,14 +51,7 @@ int main(int argc, char* argv[]) {
// Check for command line arguments
if (has_command_line_argument) {
Core::Emulator emulator;
for (int i = 0; i < argc; i++) {
std::string curArg = argv[i];
if (curArg == "-p") {
std::string patchFile = argv[i + 1];
MemoryPatcher::patchFile = patchFile;
}
}
emulator.Run(argv[1]);
emulator.Run(argv[argc - 1]);
}

// Run the Qt application
Expand Down