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 "common/assert.h"
#include "common/config.h"
#include "common/elf_info.h"
#include "common/io_file.h"
#include "common/path_util.h"
#include "common/version.h"
@ -210,6 +211,7 @@ std::string getDefaultKeyboardConfig() {
## SPDX-License-Identifier: GPL-2.0-or-later
#Default controller button mappings
#I will update this later
#Taken keys:
#F11 : fullscreen
@ -339,25 +341,36 @@ typename std::map<KeyBinding, T>::const_iterator FindKeyAllowingOnlyNoModifiers(
return map.end(); // Return end if no match is found
}
void WindowSDL::parseInputConfig(const std::string& filename) {
// Read configuration file.
const auto config_file = Common::FS::GetUserPath(Common::FS::PathType::UserDir) / filename;
if (!std::filesystem::exists(config_file)) {
// create it
std::ofstream file;
file.open(config_file, std::ios::out);
if (file.is_open()) {
file << getDefaultKeyboardConfig();
file.close();
std::cout << "Config file generated.\n";
} else {
std::cerr << "Error creating file!\n";
}
void WindowSDL::parseInputConfig() {
// Read configuration file of the game, and if it doesn't exist, generate it from default
// 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);
}
std::ifstream file(config_file);
if (!file.is_open()) {
std::cerr << "Error opening file: " << filename << std::endl;
return;
// Try loading the game-specific config file
if (!std::filesystem::exists(config_file)) {
// If game-specific config doesn't exist, check for the default config
if (!std::filesystem::exists(default_config_file)) {
// If the default config is also missing, create it from getDefaultConfig()
const auto default_config = getDefaultKeyboardConfig();
std::ofstream default_config_stream(default_config_file);
if (default_config_stream) {
default_config_stream << default_config;
}
}
// If default config now exists, copy it to the game-specific config file
if (std::filesystem::exists(default_config_file)) {
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
@ -369,6 +382,8 @@ void WindowSDL::parseInputConfig(const std::string& filename) {
axis_map.clear();
key_to_modkey_toggle_map.clear();
int lineCount = 0;
std::ifstream file(config_file);
std::string line = "";
while (std::getline(file, line)) {
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));
#endif
// initialize kbm controls
parseInputConfig("keyboardInputConfig.ini");
parseInputConfig();
// Start polling the mouse
if (mouse_polling_id == 0) {
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) {
// Reparse kbm inputs
if (binding.key == SDLK_F8) {
parseInputConfig("keyboardInputConfig.ini");
parseInputConfig();
}
// Toggle mouse capture and movement input
else if (binding.key == SDLK_F9) {

View File

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