Added per-game config

This commit is contained in:
kalaposfos13 2024-10-18 17:25:44 +02:00
parent 2c51aeef1c
commit e0b288ac68
2 changed files with 36 additions and 21 deletions

View File

@ -15,6 +15,7 @@
#include <SDL3/SDL_video.h> #include <SDL3/SDL_video.h>
#include "common/assert.h" #include "common/assert.h"
#include "common/config.h" #include "common/config.h"
#include "common/elf_info.h"
#include "common/io_file.h" #include "common/io_file.h"
#include "common/path_util.h" #include "common/path_util.h"
#include "common/version.h" #include "common/version.h"
@ -210,6 +211,7 @@ std::string getDefaultKeyboardConfig() {
## SPDX-License-Identifier: GPL-2.0-or-later ## SPDX-License-Identifier: GPL-2.0-or-later
#Default controller button mappings #Default controller button mappings
#I will update this later
#Taken keys: #Taken keys:
#F11 : fullscreen #F11 : fullscreen
@ -339,25 +341,36 @@ typename std::map<KeyBinding, T>::const_iterator FindKeyAllowingOnlyNoModifiers(
return map.end(); // Return end if no match is found return map.end(); // Return end if no match is found
} }
void WindowSDL::parseInputConfig(const std::string& filename) { void WindowSDL::parseInputConfig() {
// Read configuration file. // Read configuration file of the game, and if it doesn't exist, generate it from default
const auto config_file = Common::FS::GetUserPath(Common::FS::PathType::UserDir) / filename; // If that doesn't exist either, generate that from getDefaultConfig() and try again
// If even the folder is missing, we start with that.
const auto config_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir) / "kbmConfig";
const auto config_file =
config_dir / (std::string(Common::ElfInfo::Instance().GameSerial()) + ".ini");
const auto default_config_file = config_dir / "default.ini";
// Ensure the config directory exists
if (!std::filesystem::exists(config_dir)) {
std::filesystem::create_directories(config_dir);
}
// Try loading the game-specific config file
if (!std::filesystem::exists(config_file)) { if (!std::filesystem::exists(config_file)) {
// create it // If game-specific config doesn't exist, check for the default config
std::ofstream file; if (!std::filesystem::exists(default_config_file)) {
file.open(config_file, std::ios::out); // If the default config is also missing, create it from getDefaultConfig()
if (file.is_open()) { const auto default_config = getDefaultKeyboardConfig();
file << getDefaultKeyboardConfig(); std::ofstream default_config_stream(default_config_file);
file.close(); if (default_config_stream) {
std::cout << "Config file generated.\n"; default_config_stream << default_config;
} else {
std::cerr << "Error creating file!\n";
} }
} }
std::ifstream file(config_file);
if (!file.is_open()) { // If default config now exists, copy it to the game-specific config file
std::cerr << "Error opening file: " << filename << std::endl; if (std::filesystem::exists(default_config_file)) {
return; std::filesystem::copy(default_config_file, config_file);
}
} }
// we reset these here so in case the user fucks up or doesn't include this we can fall back to // we reset these here so in case the user fucks up or doesn't include this we can fall back to
@ -369,6 +382,8 @@ void WindowSDL::parseInputConfig(const std::string& filename) {
axis_map.clear(); axis_map.clear();
key_to_modkey_toggle_map.clear(); key_to_modkey_toggle_map.clear();
int lineCount = 0; int lineCount = 0;
std::ifstream file(config_file);
std::string line = ""; std::string line = "";
while (std::getline(file, line)) { while (std::getline(file, line)) {
lineCount++; lineCount++;
@ -600,7 +615,7 @@ WindowSDL::WindowSDL(s32 width_, s32 height_, Input::GameController* controller_
window_info.render_surface = SDL_Metal_GetLayer(SDL_Metal_CreateView(window)); window_info.render_surface = SDL_Metal_GetLayer(SDL_Metal_CreateView(window));
#endif #endif
// initialize kbm controls // initialize kbm controls
parseInputConfig("keyboardInputConfig.ini"); parseInputConfig();
// Start polling the mouse // Start polling the mouse
if (mouse_polling_id == 0) { if (mouse_polling_id == 0) {
mouse_polling_id = SDL_AddTimer(33, mousePolling, (void*)this); mouse_polling_id = SDL_AddTimer(33, mousePolling, (void*)this);
@ -706,7 +721,7 @@ void WindowSDL::onKeyboardMouseEvent(const SDL_Event* event) {
if (event->type == SDL_EVENT_KEY_DOWN) { if (event->type == SDL_EVENT_KEY_DOWN) {
// Reparse kbm inputs // Reparse kbm inputs
if (binding.key == SDLK_F8) { if (binding.key == SDLK_F8) {
parseInputConfig("keyboardInputConfig.ini"); parseInputConfig();
} }
// Toggle mouse capture and movement input // Toggle mouse capture and movement input
else if (binding.key == SDLK_F9) { else if (binding.key == SDLK_F9) {

View File

@ -103,7 +103,7 @@ private:
static Uint32 keyRepeatCallback(void* param, Uint32 id, Uint32 interval); static Uint32 keyRepeatCallback(void* param, Uint32 id, Uint32 interval);
static Uint32 mousePolling(void* param, Uint32 id, Uint32 interval); static Uint32 mousePolling(void* param, Uint32 id, Uint32 interval);
void parseInputConfig(const std::string& filename); void parseInputConfig();
private: private:
s32 width; s32 width;