2020-12-28 16:15:37 +01:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2021-05-08 09:49:31 +02:00
|
|
|
#include "common/fs/fs.h"
|
|
|
|
#include "common/fs/path_util.h"
|
2020-12-28 16:15:37 +01:00
|
|
|
#include "yuzu/configuration/config.h"
|
|
|
|
#include "yuzu/configuration/input_profiles.h"
|
|
|
|
|
|
|
|
namespace FS = Common::FS;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool ProfileExistsInFilesystem(std::string_view profile_name) {
|
2021-05-08 09:49:31 +02:00
|
|
|
return FS::Exists(FS::GetYuzuPath(FS::YuzuPath::ConfigDir) / "input" /
|
|
|
|
fmt::format("{}.ini", profile_name));
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
2021-05-08 09:49:31 +02:00
|
|
|
bool IsINI(const std::filesystem::path& filename) {
|
|
|
|
return filename.extension() == ".ini";
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
2021-05-08 09:49:31 +02:00
|
|
|
std::filesystem::path GetNameWithoutExtension(std::filesystem::path filename) {
|
|
|
|
return filename.replace_extension();
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
InputProfiles::InputProfiles() {
|
2021-05-08 09:49:31 +02:00
|
|
|
const auto input_profile_loc = FS::GetYuzuPath(FS::YuzuPath::ConfigDir) / "input";
|
2020-12-28 16:15:37 +01:00
|
|
|
|
2021-05-08 21:25:29 +02:00
|
|
|
if (!FS::IsDir(input_profile_loc)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-08 09:49:31 +02:00
|
|
|
FS::IterateDirEntries(
|
|
|
|
input_profile_loc,
|
|
|
|
[this](const std::filesystem::path& full_path) {
|
|
|
|
const auto filename = full_path.filename();
|
|
|
|
const auto name_without_ext =
|
|
|
|
Common::FS::PathToUTF8String(GetNameWithoutExtension(filename));
|
|
|
|
|
|
|
|
if (IsINI(filename) && IsProfileNameValid(name_without_ext)) {
|
2020-12-28 16:15:37 +01:00
|
|
|
map_profiles.insert_or_assign(
|
2021-05-08 09:49:31 +02:00
|
|
|
name_without_ext,
|
|
|
|
std::make_unique<Config>(name_without_ext, Config::ConfigType::InputProfile));
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
2021-05-08 09:49:31 +02:00
|
|
|
|
2020-12-28 16:15:37 +01:00
|
|
|
return true;
|
2021-05-08 09:49:31 +02:00
|
|
|
},
|
|
|
|
FS::DirEntryFilter::File);
|
2020-12-28 16:15:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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) ||
|
2021-05-08 09:49:31 +02:00
|
|
|
FS::RemoveFile(map_profiles[profile_name]->GetConfigFilePath())) {
|
2020-12-28 16:15:37 +01:00
|
|
|
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();
|
|
|
|
}
|