13 changed files with 509 additions and 130 deletions
-
2src/yuzu/CMakeLists.txt
-
271src/yuzu/configuration/config.cpp
-
20src/yuzu/configuration/config.h
-
6src/yuzu/configuration/configure_debug_controller.cpp
-
6src/yuzu/configuration/configure_debug_controller.h
-
30src/yuzu/configuration/configure_input.cpp
-
4src/yuzu/configuration/configure_input.h
-
111src/yuzu/configuration/configure_input_player.cpp
-
21src/yuzu/configuration/configure_input_player.h
-
3src/yuzu/configuration/configure_per_game.cpp
-
131src/yuzu/configuration/input_profiles.cpp
-
32src/yuzu/configuration/input_profiles.h
-
2src/yuzu/main.cpp
@ -0,0 +1,131 @@ |
|||||
|
// Copyright 2020 yuzu Emulator Project
|
||||
|
// Licensed under GPLv2 or any later version
|
||||
|
// Refer to the license.txt file included.
|
||||
|
|
||||
|
#include <fmt/format.h>
|
||||
|
|
||||
|
#include "common/common_paths.h"
|
||||
|
#include "common/file_util.h"
|
||||
|
#include "yuzu/configuration/config.h"
|
||||
|
#include "yuzu/configuration/input_profiles.h"
|
||||
|
|
||||
|
namespace FS = Common::FS; |
||||
|
|
||||
|
namespace { |
||||
|
|
||||
|
bool ProfileExistsInFilesystem(std::string_view profile_name) { |
||||
|
return FS::Exists(fmt::format("{}input" DIR_SEP "{}.ini", |
||||
|
FS::GetUserPath(FS::UserPath::ConfigDir), profile_name)); |
||||
|
} |
||||
|
|
||||
|
bool IsINI(std::string_view filename) { |
||||
|
const std::size_t index = filename.rfind('.'); |
||||
|
|
||||
|
if (index == std::string::npos) { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
return filename.substr(index) == ".ini"; |
||||
|
} |
||||
|
|
||||
|
std::string GetNameWithoutExtension(const std::string& filename) { |
||||
|
const std::size_t index = filename.rfind('.'); |
||||
|
|
||||
|
if (index == std::string::npos) { |
||||
|
return filename; |
||||
|
} |
||||
|
|
||||
|
return filename.substr(0, index); |
||||
|
} |
||||
|
|
||||
|
} // namespace
|
||||
|
|
||||
|
InputProfiles::InputProfiles() { |
||||
|
const std::string input_profile_loc = |
||||
|
fmt::format("{}input", FS::GetUserPath(FS::UserPath::ConfigDir)); |
||||
|
|
||||
|
FS::ForeachDirectoryEntry( |
||||
|
nullptr, input_profile_loc, |
||||
|
[this](u64* entries_out, const std::string& directory, const std::string& filename) { |
||||
|
if (IsINI(filename) && IsProfileNameValid(GetNameWithoutExtension(filename))) { |
||||
|
map_profiles.insert_or_assign( |
||||
|
GetNameWithoutExtension(filename), |
||||
|
std::make_unique<Config>(GetNameWithoutExtension(filename), |
||||
|
Config::ConfigType::InputProfile)); |
||||
|
} |
||||
|
return true; |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
InputProfiles::~InputProfiles() = default; |
||||
|
|
||||
|
std::vector<std::string> InputProfiles::GetInputProfileNames() { |
||||
|
std::vector<std::string> profile_names; |
||||
|
profile_names.reserve(map_profiles.size()); |
||||
|
|
||||
|
for (const auto& [profile_name, config] : map_profiles) { |
||||
|
if (!ProfileExistsInFilesystem(profile_name)) { |
||||
|
DeleteProfile(profile_name); |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
profile_names.push_back(profile_name); |
||||
|
} |
||||
|
|
||||
|
return profile_names; |
||||
|
} |
||||
|
|
||||
|
bool InputProfiles::IsProfileNameValid(std::string_view profile_name) { |
||||
|
return profile_name.find_first_of("<>:;\"/\\|,.!?*") == std::string::npos; |
||||
|
} |
||||
|
|
||||
|
bool InputProfiles::CreateProfile(const std::string& profile_name, std::size_t player_index) { |
||||
|
if (ProfileExistsInMap(profile_name)) { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
map_profiles.insert_or_assign( |
||||
|
profile_name, std::make_unique<Config>(profile_name, Config::ConfigType::InputProfile)); |
||||
|
|
||||
|
return SaveProfile(profile_name, player_index); |
||||
|
} |
||||
|
|
||||
|
bool InputProfiles::DeleteProfile(const std::string& profile_name) { |
||||
|
if (!ProfileExistsInMap(profile_name)) { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
if (!ProfileExistsInFilesystem(profile_name) || |
||||
|
FS::Delete(map_profiles[profile_name]->GetConfigFilePath())) { |
||||
|
map_profiles.erase(profile_name); |
||||
|
} |
||||
|
|
||||
|
return !ProfileExistsInMap(profile_name) && !ProfileExistsInFilesystem(profile_name); |
||||
|
} |
||||
|
|
||||
|
bool InputProfiles::LoadProfile(const std::string& profile_name, std::size_t player_index) { |
||||
|
if (!ProfileExistsInMap(profile_name)) { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
if (!ProfileExistsInFilesystem(profile_name)) { |
||||
|
map_profiles.erase(profile_name); |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
map_profiles[profile_name]->ReadControlPlayerValue(player_index); |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
bool InputProfiles::SaveProfile(const std::string& profile_name, std::size_t player_index) { |
||||
|
if (!ProfileExistsInMap(profile_name)) { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
map_profiles[profile_name]->SaveControlPlayerValue(player_index); |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
bool InputProfiles::ProfileExistsInMap(const std::string& profile_name) const { |
||||
|
return map_profiles.find(profile_name) != map_profiles.end(); |
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
// Copyright 2020 yuzu Emulator Project |
||||
|
// Licensed under GPLv2 or any later version |
||||
|
// Refer to the license.txt file included. |
||||
|
|
||||
|
#pragma once |
||||
|
|
||||
|
#include <string> |
||||
|
#include <string_view> |
||||
|
#include <unordered_map> |
||||
|
|
||||
|
class Config; |
||||
|
|
||||
|
class InputProfiles { |
||||
|
|
||||
|
public: |
||||
|
explicit InputProfiles(); |
||||
|
virtual ~InputProfiles(); |
||||
|
|
||||
|
std::vector<std::string> GetInputProfileNames(); |
||||
|
|
||||
|
static bool IsProfileNameValid(std::string_view profile_name); |
||||
|
|
||||
|
bool CreateProfile(const std::string& profile_name, std::size_t player_index); |
||||
|
bool DeleteProfile(const std::string& profile_name); |
||||
|
bool LoadProfile(const std::string& profile_name, std::size_t player_index); |
||||
|
bool SaveProfile(const std::string& profile_name, std::size_t player_index); |
||||
|
|
||||
|
private: |
||||
|
bool ProfileExistsInMap(const std::string& profile_name) const; |
||||
|
|
||||
|
std::unordered_map<std::string, std::unique_ptr<Config>> map_profiles; |
||||
|
}; |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue