diff --git a/Engine/Source/Editor/EditorApp.cpp b/Engine/Source/Editor/EditorApp.cpp index 9d1d052c..841b51ad 100644 --- a/Engine/Source/Editor/EditorApp.cpp +++ b/Engine/Source/Editor/EditorApp.cpp @@ -31,7 +31,7 @@ #include "Rendering/TerrainRenderer.h" #include "Rendering/WorldRenderer.h" #include "Rendering/ParticleRenderer.h" -#include "Resources/FileWatcher.hpp" +#include "Resources/FileWatcher.h" #include "Resources/ResourceBuilder.h" #include "Resources/ShaderBuilder.h" #include "Scene/SceneDatabase.h" @@ -349,10 +349,10 @@ void EditorApp::InitFileWatcher() { constexpr const char* watchPath = CDENGINE_BUILTIN_SHADER_PATH "shaders/"; - FileWatchCallbackWrapper::SetRenderContext(m_pRenderContext.get()); - FileWatchCallbackWrapper::SetWindow(GetMainWindow()); m_pFileWatcher = std::make_unique(); - m_pFileWatcher->Watch(watchPath, FileWatchCallbackWrapper::Callback, 0, nullptr); + m_pFileWatcher->SetRenderContext(m_pRenderContext.get()); + m_pFileWatcher->SetWindow(GetMainWindow()); + m_pFileWatcher->WatchShaders(watchPath); } void EditorApp::ShaderHotModifyDetec() diff --git a/Engine/Source/Editor/Resources/FileWatcher.hpp b/Engine/Source/Editor/Resources/FileWatcher.cpp similarity index 50% rename from Engine/Source/Editor/Resources/FileWatcher.hpp rename to Engine/Source/Editor/Resources/FileWatcher.cpp index 96dfc51c..ec225827 100644 --- a/Engine/Source/Editor/Resources/FileWatcher.hpp +++ b/Engine/Source/Editor/Resources/FileWatcher.cpp @@ -1,7 +1,4 @@ -#pragma once - -#include -#include +#include "FileWatcher.h" #include "Log/Log.h" #include "Path/Path.h" @@ -20,70 +17,18 @@ namespace editor using WatchID = dmon_watch_id; using WatchAction = dmon_action; -class FileWatcher final +namespace { -public: - FileWatcher(const FileWatcher&) = delete; - FileWatcher& operator=(const FileWatcher&) = delete; - FileWatcher(FileWatcher&&) = delete; - FileWatcher& operator=(FileWatcher&&) = delete; - - FileWatcher() - { - Init(); - } - - ~FileWatcher() - { - Deinit(); - } - - void Init() - { - dmon_init(); - } - - void Deinit() - { - dmon_deinit(); - } - - WatchID Watch( - const char* rootDir, - void (*callback)( - WatchID watchID, WatchAction action, - const char* dirName, const char* filename, - const char* oldName, void* user), - uint32_t flags, void* userData) - { - WatchID watchID = dmon_watch(rootDir, callback, flags, userData); - - CD_INFO("Start watching {0}", rootDir); - CD_INFO(" Watch ID {0}", watchID.id); - - m_witchInfos[watchID.id] = rootDir; - - return watchID; - } - - void UnWatch(WatchID watchID) - { - dmon_unwatch(watchID); - } - - std::map m_witchInfos; -}; - -class FileWatchCallbackWrapper final +class ShaderHotModifyCallbackWrapper final { public: - FileWatchCallbackWrapper() = default; - FileWatchCallbackWrapper(const FileWatchCallbackWrapper&) = delete; - FileWatchCallbackWrapper& operator=(const FileWatchCallbackWrapper&) = delete; - FileWatchCallbackWrapper(FileWatchCallbackWrapper&&) = delete; - FileWatchCallbackWrapper& operator=(FileWatchCallbackWrapper&&) = delete; - ~FileWatchCallbackWrapper() = default; + ShaderHotModifyCallbackWrapper() = default; + ShaderHotModifyCallbackWrapper(const ShaderHotModifyCallbackWrapper&) = delete; + ShaderHotModifyCallbackWrapper& operator=(const ShaderHotModifyCallbackWrapper&) = delete; + ShaderHotModifyCallbackWrapper(ShaderHotModifyCallbackWrapper&&) = delete; + ShaderHotModifyCallbackWrapper& operator=(ShaderHotModifyCallbackWrapper&&) = delete; + ~ShaderHotModifyCallbackWrapper() = default; static void Callback( WatchID id, WatchAction action, @@ -111,6 +56,8 @@ class FileWatchCallbackWrapper final if (m_pWindow->GetInputFocus() && engine::Path::GetExtension(filePath) != ".sc") { + // Returns when the window does not get focus. + // Returns when a non-shader file is detected. return; } @@ -130,21 +77,64 @@ class FileWatchCallbackWrapper final } } - static void SetRenderContext(engine::RenderContext* pRenderContext) - { - m_pRenderContext = pRenderContext; - } - - static void SetWindow(engine::Window* pWindow) - { - m_pWindow = pWindow; - } - static engine::RenderContext* m_pRenderContext; static engine::Window* m_pWindow; }; -engine::RenderContext* FileWatchCallbackWrapper::m_pRenderContext = nullptr; -engine::Window* FileWatchCallbackWrapper::m_pWindow = nullptr; +engine::RenderContext* ShaderHotModifyCallbackWrapper::m_pRenderContext = nullptr; +engine::Window* ShaderHotModifyCallbackWrapper::m_pWindow = nullptr; + +} + +FileWatcher::FileWatcher() +{ + Init(); +} + +FileWatcher::~FileWatcher() +{ + Deinit(); +} + +void FileWatcher::Init() const +{ + dmon_init(); +} + +void FileWatcher::Deinit() const +{ + dmon_deinit(); +} + +uint32_t FileWatcher::WatchShaders(const char* rootDir) +{ + ShaderHotModifyCallbackWrapper::m_pRenderContext = m_pRenderContext; + ShaderHotModifyCallbackWrapper::m_pWindow = m_pWindow; + + WatchID watchID = dmon_watch(rootDir, ShaderHotModifyCallbackWrapper::Callback, 0, nullptr); + + CD_INFO("Start watching {0}", rootDir); + CD_INFO(" Watch ID {0}", watchID.id); + + m_witchInfos[watchID.id] = rootDir; + + return watchID.id; +} + +void FileWatcher::UnWatch(uint32_t watchID) +{ + dmon_unwatch(WatchID{ watchID }); + m_witchInfos.erase(watchID); +} + +void FileWatcher::SetWitchInfos(std::map witchInfos) +{ + m_witchInfos = cd::MoveTemp(witchInfos); +} + +const char* FileWatcher::GetWatchingPath(uint32_t id) const +{ + return m_witchInfos.at(id); +} } \ No newline at end of file diff --git a/Engine/Source/Editor/Resources/FileWatcher.h b/Engine/Source/Editor/Resources/FileWatcher.h new file mode 100644 index 00000000..6b18d752 --- /dev/null +++ b/Engine/Source/Editor/Resources/FileWatcher.h @@ -0,0 +1,48 @@ +#pragma once + +#include +#include + +namespace engine +{ + +class RenderContext; +class Window; + +} + +namespace editor +{ + +class FileWatcher final +{ + +public: + FileWatcher(const FileWatcher&) = delete; + FileWatcher& operator=(const FileWatcher&) = delete; + FileWatcher(FileWatcher&&) = delete; + FileWatcher& operator=(FileWatcher&&) = delete; + + FileWatcher(); + ~FileWatcher(); + + void Init() const; + void SetRenderContext(engine::RenderContext* pRenderContext) { m_pRenderContext = pRenderContext; } + void SetWindow(engine::Window* pWindow) { m_pWindow = pWindow; } + void Deinit() const; + + uint32_t WatchShaders(const char* rootDir); + void UnWatch(uint32_t watchID); + + void SetWitchInfos(std::map); + std::map& GetWitchInfos() { return m_witchInfos; } + const std::map& GetWitchInfos() const { return m_witchInfos; } + const char* GetWatchingPath(uint32_t id) const; + +private: + std::map m_witchInfos; + engine::RenderContext* m_pRenderContext; + engine::Window* m_pWindow; +}; + +} \ No newline at end of file