Skip to content

Commit

Permalink
Hide dmon implement
Browse files Browse the repository at this point in the history
  • Loading branch information
roeas committed Nov 14, 2023
1 parent dd0c661 commit c671fbd
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 80 deletions.
8 changes: 4 additions & 4 deletions Engine/Source/Editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<FileWatcher>();
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()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#pragma once

#include <cstdint>
#include <functional>
#include "FileWatcher.h"

#include "Log/Log.h"
#include "Path/Path.h"
Expand All @@ -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<uint32_t, std::string> 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,
Expand Down Expand Up @@ -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;
}

Expand All @@ -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<uint32_t, const char*> witchInfos)
{
m_witchInfos = cd::MoveTemp(witchInfos);
}

const char* FileWatcher::GetWatchingPath(uint32_t id) const
{
return m_witchInfos.at(id);
}

}
48 changes: 48 additions & 0 deletions Engine/Source/Editor/Resources/FileWatcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include <cstdint>
#include <map>

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<uint32_t, const char*>);
std::map<uint32_t, const char*>& GetWitchInfos() { return m_witchInfos; }
const std::map<uint32_t, const char*>& GetWitchInfos() const { return m_witchInfos; }
const char* GetWatchingPath(uint32_t id) const;

private:
std::map<uint32_t, const char*> m_witchInfos;
engine::RenderContext* m_pRenderContext;
engine::Window* m_pWindow;
};

}

0 comments on commit c671fbd

Please sign in to comment.