input_profiles.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <fmt/format.h>
  5. #include "common/fs/fs.h"
  6. #include "common/fs/path_util.h"
  7. #include "yuzu/configuration/config.h"
  8. #include "yuzu/configuration/input_profiles.h"
  9. namespace FS = Common::FS;
  10. namespace {
  11. bool ProfileExistsInFilesystem(std::string_view profile_name) {
  12. return FS::Exists(FS::GetYuzuPath(FS::YuzuPath::ConfigDir) / "input" /
  13. fmt::format("{}.ini", profile_name));
  14. }
  15. bool IsINI(const std::filesystem::path& filename) {
  16. return filename.extension() == ".ini";
  17. }
  18. std::filesystem::path GetNameWithoutExtension(std::filesystem::path filename) {
  19. return filename.replace_extension();
  20. }
  21. } // namespace
  22. InputProfiles::InputProfiles(Core::System& system_) : system{system_} {
  23. const auto input_profile_loc = FS::GetYuzuPath(FS::YuzuPath::ConfigDir) / "input";
  24. if (!FS::IsDir(input_profile_loc)) {
  25. return;
  26. }
  27. FS::IterateDirEntries(
  28. input_profile_loc,
  29. [this](const std::filesystem::path& full_path) {
  30. const auto filename = full_path.filename();
  31. const auto name_without_ext =
  32. Common::FS::PathToUTF8String(GetNameWithoutExtension(filename));
  33. if (IsINI(filename) && IsProfileNameValid(name_without_ext)) {
  34. map_profiles.insert_or_assign(
  35. name_without_ext, std::make_unique<Config>(system, name_without_ext,
  36. Config::ConfigType::InputProfile));
  37. }
  38. return true;
  39. },
  40. FS::DirEntryFilter::File);
  41. }
  42. InputProfiles::~InputProfiles() = default;
  43. std::vector<std::string> InputProfiles::GetInputProfileNames() {
  44. std::vector<std::string> profile_names;
  45. profile_names.reserve(map_profiles.size());
  46. for (const auto& [profile_name, config] : map_profiles) {
  47. if (!ProfileExistsInFilesystem(profile_name)) {
  48. DeleteProfile(profile_name);
  49. continue;
  50. }
  51. profile_names.push_back(profile_name);
  52. }
  53. return profile_names;
  54. }
  55. bool InputProfiles::IsProfileNameValid(std::string_view profile_name) {
  56. return profile_name.find_first_of("<>:;\"/\\|,.!?*") == std::string::npos;
  57. }
  58. bool InputProfiles::CreateProfile(const std::string& profile_name, std::size_t player_index) {
  59. if (ProfileExistsInMap(profile_name)) {
  60. return false;
  61. }
  62. map_profiles.insert_or_assign(
  63. profile_name,
  64. std::make_unique<Config>(system, profile_name, Config::ConfigType::InputProfile));
  65. return SaveProfile(profile_name, player_index);
  66. }
  67. bool InputProfiles::DeleteProfile(const std::string& profile_name) {
  68. if (!ProfileExistsInMap(profile_name)) {
  69. return false;
  70. }
  71. if (!ProfileExistsInFilesystem(profile_name) ||
  72. FS::RemoveFile(map_profiles[profile_name]->GetConfigFilePath())) {
  73. map_profiles.erase(profile_name);
  74. }
  75. return !ProfileExistsInMap(profile_name) && !ProfileExistsInFilesystem(profile_name);
  76. }
  77. bool InputProfiles::LoadProfile(const std::string& profile_name, std::size_t player_index) {
  78. if (!ProfileExistsInMap(profile_name)) {
  79. return false;
  80. }
  81. if (!ProfileExistsInFilesystem(profile_name)) {
  82. map_profiles.erase(profile_name);
  83. return false;
  84. }
  85. map_profiles[profile_name]->ReadControlPlayerValue(player_index);
  86. return true;
  87. }
  88. bool InputProfiles::SaveProfile(const std::string& profile_name, std::size_t player_index) {
  89. if (!ProfileExistsInMap(profile_name)) {
  90. return false;
  91. }
  92. map_profiles[profile_name]->SaveControlPlayerValue(player_index);
  93. return true;
  94. }
  95. bool InputProfiles::ProfileExistsInMap(const std::string& profile_name) const {
  96. return map_profiles.find(profile_name) != map_profiles.end();
  97. }