summaryrefslogtreecommitdiff
path: root/Source/Core/Common
diff options
context:
space:
mode:
authorJMC47 <JMC4789@gmail.com>2025-06-07 15:51:34 -0400
committerGitHub <noreply@github.com>2025-06-07 15:51:34 -0400
commit52fcdde4852ccab197092da37b00548e33844d85 (patch)
tree02b438f5d1fa1c127f73bdb0a0f12c2678965435 /Source/Core/Common
parent5eb61024c6e94b949ce21eac81003b43587fc155 (diff)
parentc3d3b8153388866117334cdc719889d1f676a16e (diff)
Merge pull request #13386 from iwubcode/resource_manager_system
VideoCommon: add resource manager, tracks resources to load assets in optimal way and manage memory
Diffstat (limited to 'Source/Core/Common')
-rw-r--r--Source/Core/Common/CMakeLists.txt3
-rw-r--r--Source/Core/Common/FilesystemWatcher.cpp67
-rw-r--r--Source/Core/Common/FilesystemWatcher.h47
3 files changed, 117 insertions, 0 deletions
diff --git a/Source/Core/Common/CMakeLists.txt b/Source/Core/Common/CMakeLists.txt
index bbc3721a19..6d1b6908f3 100644
--- a/Source/Core/Common/CMakeLists.txt
+++ b/Source/Core/Common/CMakeLists.txt
@@ -64,6 +64,8 @@ add_library(common
FatFsUtil.h
FileSearch.cpp
FileSearch.h
+ FilesystemWatcher.cpp
+ FilesystemWatcher.h
FileUtil.cpp
FileUtil.h
FixedSizeQueue.h
@@ -184,6 +186,7 @@ PRIVATE
FatFs
Iconv::Iconv
spng::spng
+ watcher
${VTUNE_LIBRARIES}
)
diff --git a/Source/Core/Common/FilesystemWatcher.cpp b/Source/Core/Common/FilesystemWatcher.cpp
new file mode 100644
index 0000000000..646e2467ed
--- /dev/null
+++ b/Source/Core/Common/FilesystemWatcher.cpp
@@ -0,0 +1,67 @@
+// Copyright 2025 Dolphin Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "Common/FilesystemWatcher.h"
+
+#include <wtr/watcher.hpp>
+
+#include "Common/Logging/Log.h"
+#include "Common/StringUtil.h"
+
+namespace Common
+{
+FilesystemWatcher::FilesystemWatcher() = default;
+FilesystemWatcher::~FilesystemWatcher() = default;
+
+void FilesystemWatcher::Watch(const std::string& path)
+{
+ const auto [iter, inserted] = m_watched_paths.try_emplace(path, nullptr);
+ if (inserted)
+ {
+ iter->second = std::make_unique<wtr::watch>(path, [this](wtr::event e) {
+ const auto watched_path = PathToString(e.path_name);
+ if (e.path_type == wtr::event::path_type::watcher)
+ {
+ if (watched_path.starts_with('e'))
+ ERROR_LOG_FMT(COMMON, "Filesystem watcher: '{}'", watched_path);
+ else if (watched_path.starts_with('w'))
+ WARN_LOG_FMT(COMMON, "Filesystem watcher: '{}'", watched_path);
+ return;
+ }
+
+ if (e.effect_type == wtr::event::effect_type::create)
+ {
+ const auto path = WithUnifiedPathSeparators(watched_path);
+ PathAdded(path);
+ }
+ else if (e.effect_type == wtr::event::effect_type::modify)
+ {
+ const auto path = WithUnifiedPathSeparators(watched_path);
+ PathModified(path);
+ }
+ else if (e.effect_type == wtr::event::effect_type::rename)
+ {
+ if (!e.associated)
+ {
+ WARN_LOG_FMT(COMMON, "Rename on path '{}' seen without association!", watched_path);
+ return;
+ }
+
+ const auto old_path = WithUnifiedPathSeparators(watched_path);
+ const auto new_path = WithUnifiedPathSeparators(PathToString(e.associated->path_name));
+ PathRenamed(old_path, new_path);
+ }
+ else if (e.effect_type == wtr::event::effect_type::destroy)
+ {
+ const auto path = WithUnifiedPathSeparators(watched_path);
+ PathDeleted(path);
+ }
+ });
+ }
+}
+
+void FilesystemWatcher::Unwatch(const std::string& path)
+{
+ m_watched_paths.erase(path);
+}
+} // namespace Common
diff --git a/Source/Core/Common/FilesystemWatcher.h b/Source/Core/Common/FilesystemWatcher.h
new file mode 100644
index 0000000000..ad1c822e2b
--- /dev/null
+++ b/Source/Core/Common/FilesystemWatcher.h
@@ -0,0 +1,47 @@
+// Copyright 2025 Dolphin Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include <map>
+#include <memory>
+#include <string>
+#include <string_view>
+
+namespace wtr
+{
+inline namespace watcher
+{
+class watch;
+}
+} // namespace wtr
+
+namespace Common
+{
+// A class that can watch a path and receive callbacks
+// when files or directories underneath that path receive events
+class FilesystemWatcher
+{
+public:
+ FilesystemWatcher();
+ virtual ~FilesystemWatcher();
+
+ void Watch(const std::string& path);
+ void Unwatch(const std::string& path);
+
+private:
+ // A new file or folder was added to one of the watched paths
+ virtual void PathAdded(std::string_view path) {}
+
+ // A file or folder was modified in one of the watched paths
+ virtual void PathModified(std::string_view path) {}
+
+ // A file or folder was renamed in one of the watched paths
+ virtual void PathRenamed(std::string_view old_path, std::string_view new_path) {}
+
+ // A file or folder was deleted in one of the watched paths
+ virtual void PathDeleted(std::string_view path) {}
+
+ std::map<std::string, std::unique_ptr<wtr::watch>> m_watched_paths;
+};
+} // namespace Common