input_profiles.cpp 3.8 KB

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