summaryrefslogtreecommitdiff
path: root/src/citron/configuration/input_profiles.cpp
diff options
context:
space:
mode:
authorZephyron <zephyron@citron-emu.org>2024-12-31 16:19:25 +1000
committerZephyron <zephyron@citron-emu.org>2024-12-31 16:19:25 +1000
commit9427e27e24a7135880ee2881c3c44988e174b41a (patch)
tree83f0062a35be144f6b162eaa823c5b3c7620146e /src/citron/configuration/input_profiles.cpp
parentb35ae725d20960411e8588b11c12a2d55f86c9d0 (diff)
chore: update project branding to citron
Diffstat (limited to 'src/citron/configuration/input_profiles.cpp')
-rw-r--r--src/citron/configuration/input_profiles.cpp134
1 files changed, 134 insertions, 0 deletions
diff --git a/src/citron/configuration/input_profiles.cpp b/src/citron/configuration/input_profiles.cpp
new file mode 100644
index 000000000..ebebadc94
--- /dev/null
+++ b/src/citron/configuration/input_profiles.cpp
@@ -0,0 +1,134 @@
+// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <fmt/format.h>
+
+#include "common/fs/fs.h"
+#include "common/fs/path_util.h"
+#include "common/logging/log.h"
+#include "frontend_common/config.h"
+#include "yuzu/configuration/input_profiles.h"
+
+namespace FS = Common::FS;
+
+namespace {
+
+bool ProfileExistsInFilesystem(std::string_view profile_name) {
+ return FS::Exists(FS::GetYuzuPath(FS::YuzuPath::ConfigDir) / "input" /
+ fmt::format("{}.ini", profile_name));
+}
+
+bool IsINI(const std::filesystem::path& filename) {
+ return filename.extension() == ".ini";
+}
+
+std::filesystem::path GetNameWithoutExtension(std::filesystem::path filename) {
+ return filename.replace_extension();
+}
+
+} // namespace
+
+InputProfiles::InputProfiles() {
+ const auto input_profile_loc = FS::GetYuzuPath(FS::YuzuPath::ConfigDir) / "input";
+
+ if (!FS::IsDir(input_profile_loc)) {
+ return;
+ }
+
+ 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)) {
+ map_profiles.insert_or_assign(
+ name_without_ext,
+ std::make_unique<QtConfig>(name_without_ext, Config::ConfigType::InputProfile));
+ }
+
+ return true;
+ },
+ FS::DirEntryFilter::File);
+}
+
+InputProfiles::~InputProfiles() = default;
+
+std::vector<std::string> InputProfiles::GetInputProfileNames() {
+ std::vector<std::string> profile_names;
+ profile_names.reserve(map_profiles.size());
+
+ auto it = map_profiles.cbegin();
+ while (it != map_profiles.cend()) {
+ const auto& [profile_name, config] = *it;
+ if (!ProfileExistsInFilesystem(profile_name)) {
+ it = map_profiles.erase(it);
+ continue;
+ }
+
+ profile_names.push_back(profile_name);
+ ++it;
+ }
+
+ std::stable_sort(profile_names.begin(), profile_names.end());
+
+ 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<QtConfig>(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::RemoveFile(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;
+ }
+
+ LOG_INFO(Config, "Loading input profile `{}`", profile_name);
+
+ map_profiles[profile_name]->ReadQtControlPlayerValues(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]->SaveQtControlPlayerValues(player_index);
+ return true;
+}
+
+bool InputProfiles::ProfileExistsInMap(const std::string& profile_name) const {
+ return map_profiles.find(profile_name) != map_profiles.end();
+}