Add editor Assets watcher refresh loop

This commit is contained in:
2026-04-10 21:16:17 +08:00
parent 87ad489bfd
commit 2338e306bf
5 changed files with 471 additions and 0 deletions

View File

@@ -70,6 +70,7 @@ add_executable(${PROJECT_NAME} WIN32
src/Theme.cpp
src/Core/UndoManager.cpp
src/Core/PlaySessionController.cpp
src/Core/ProjectAssetWatcher.cpp
src/ComponentEditors/ComponentEditorRegistry.cpp
src/Managers/SceneManager.cpp
src/Managers/ProjectManager.cpp

View File

@@ -3,6 +3,7 @@
#include "Commands/SceneCommands.h"
#include "Core/IEditorContext.h"
#include "Core/PlaySessionController.h"
#include "Core/ProjectAssetWatcher.h"
#include "Layout/DockLayoutController.h"
#include "panels/ConsolePanel.h"
#include "panels/GameViewPanel.h"
@@ -39,12 +40,14 @@ public:
m_projectPanel->Initialize(context.GetProjectPath());
Commands::LoadStartupScene(context);
m_playSessionController.Attach(context);
m_projectAssetWatcher.Attach(context.GetProjectPath());
m_dockLayoutController->Attach(context);
m_panels.AttachAll();
}
void Detach(IEditorContext& context) {
m_playSessionController.Detach(context);
m_projectAssetWatcher.Detach();
Commands::SaveDirtySceneWithFallback(context, BuildFallbackScenePath(context));
if (m_dockLayoutController) {
@@ -61,6 +64,7 @@ public:
void Update(float dt) {
::XCEngine::Resources::ResourceManager::Get().UpdateAsyncLoads();
if (IEditorContext* context = m_panels.GetContext()) {
m_projectAssetWatcher.Update(*context, dt);
m_playSessionController.Update(*context, dt);
}
m_panels.UpdateAll(dt);
@@ -96,6 +100,7 @@ private:
ProjectPanel* m_projectPanel = nullptr;
std::unique_ptr<DockLayoutController> m_dockLayoutController;
PlaySessionController m_playSessionController;
ProjectAssetWatcher m_projectAssetWatcher;
};
} // namespace Editor

View File

@@ -0,0 +1,207 @@
#include "Core/ProjectAssetWatcher.h"
#include "Core/IProjectManager.h"
#include <XCEngine/Core/Asset/ResourceManager.h>
#include <XCEngine/Debug/Logger.h>
#include <algorithm>
#include <cctype>
#include <system_error>
namespace XCEngine {
namespace Editor {
namespace fs = std::filesystem;
namespace {
constexpr float kScanIntervalSeconds = 0.75f;
constexpr float kDebounceWindowSeconds = 0.35f;
std::string NormalizePathKey(const fs::path& path) {
std::string key = path.lexically_normal().generic_string();
std::transform(key.begin(), key.end(), key.begin(), [](unsigned char ch) {
return static_cast<char>(std::tolower(ch));
});
return key;
}
std::uint64_t GetFileSizeValue(const fs::path& path, bool isDirectory) {
if (isDirectory) {
return 0;
}
std::error_code ec;
const auto fileSize = fs::file_size(path, ec);
return ec ? 0 : static_cast<std::uint64_t>(fileSize);
}
std::uint64_t GetFileWriteTimeValue(const fs::path& path) {
std::error_code ec;
const auto writeTime = fs::last_write_time(path, ec);
if (ec) {
return 0;
}
return static_cast<std::uint64_t>(writeTime.time_since_epoch().count());
}
} // namespace
void ProjectAssetWatcher::Attach(const std::string& projectPath) {
RefreshProjectBinding(projectPath);
}
void ProjectAssetWatcher::Detach() {
m_projectPath.clear();
m_assetsRoot.clear();
m_snapshot.clear();
m_pendingChanges.Clear();
m_scanCooldownSeconds = 0.0f;
m_debounceSeconds = 0.0f;
m_hasPendingRefresh = false;
}
void ProjectAssetWatcher::Update(IEditorContext& context, float dt) {
RefreshProjectBinding(context.GetProjectPath());
if (m_assetsRoot.empty()) {
return;
}
m_scanCooldownSeconds -= dt;
if (m_scanCooldownSeconds <= 0.0f) {
ScanForChanges();
m_scanCooldownSeconds = kScanIntervalSeconds;
}
if (!m_hasPendingRefresh) {
return;
}
m_debounceSeconds -= dt;
if (m_debounceSeconds <= 0.0f) {
FlushPendingRefresh(context);
}
}
void ProjectAssetWatcher::RefreshProjectBinding(const std::string& projectPath) {
if (projectPath == m_projectPath) {
return;
}
m_projectPath = projectPath;
m_assetsRoot = projectPath.empty() ? fs::path() : (fs::path(projectPath) / "Assets");
m_snapshot.clear();
m_pendingChanges.Clear();
m_scanCooldownSeconds = kScanIntervalSeconds;
m_debounceSeconds = 0.0f;
m_hasPendingRefresh = false;
if (!m_assetsRoot.empty()) {
BuildSnapshot(m_assetsRoot, m_snapshot);
}
}
void ProjectAssetWatcher::ScanForChanges() {
Snapshot currentSnapshot;
BuildSnapshot(m_assetsRoot, currentSnapshot);
const ChangeStats changes = DiffSnapshots(m_snapshot, currentSnapshot);
m_snapshot = std::move(currentSnapshot);
if (!changes.HasChanges()) {
return;
}
m_pendingChanges.Accumulate(changes);
m_hasPendingRefresh = true;
m_debounceSeconds = kDebounceWindowSeconds;
}
void ProjectAssetWatcher::FlushPendingRefresh(IEditorContext& context) {
if (!m_hasPendingRefresh) {
return;
}
Resources::ResourceManager::Get().RefreshProjectAssets();
context.GetProjectManager().RefreshCurrentFolder();
Debug::Logger::Get().Info(
Debug::LogCategory::FileSystem,
(std::string("[ProjectAssetWatcher] Refreshed project assets after external changes. added=") +
std::to_string(m_pendingChanges.added) +
" modified=" +
std::to_string(m_pendingChanges.modified) +
" removed=" +
std::to_string(m_pendingChanges.removed)).c_str());
m_pendingChanges.Clear();
m_debounceSeconds = 0.0f;
m_hasPendingRefresh = false;
}
void ProjectAssetWatcher::BuildSnapshot(const fs::path& assetsRoot, Snapshot& outSnapshot) {
outSnapshot.clear();
std::error_code ec;
if (assetsRoot.empty() || !fs::exists(assetsRoot, ec)) {
return;
}
for (fs::recursive_directory_iterator it(
assetsRoot,
fs::directory_options::skip_permission_denied,
ec),
end;
!ec && it != end;
it.increment(ec)) {
const fs::path entryPath = it->path();
std::error_code relativeEc;
const fs::path relativePath = fs::relative(entryPath, assetsRoot, relativeEc);
if (relativeEc || relativePath.empty()) {
continue;
}
std::error_code typeEc;
const bool isDirectory = it->is_directory(typeEc);
if (typeEc) {
continue;
}
AssetEntryState entryState;
entryState.isDirectory = isDirectory;
entryState.fileSize = GetFileSizeValue(entryPath, isDirectory);
entryState.writeTime = GetFileWriteTimeValue(entryPath);
outSnapshot.emplace(NormalizePathKey(relativePath), entryState);
}
}
ProjectAssetWatcher::ChangeStats ProjectAssetWatcher::DiffSnapshots(
const Snapshot& previousSnapshot,
const Snapshot& currentSnapshot) {
ChangeStats changes;
for (const auto& [pathKey, currentState] : currentSnapshot) {
const auto previousIt = previousSnapshot.find(pathKey);
if (previousIt == previousSnapshot.end()) {
++changes.added;
continue;
}
if (!(previousIt->second == currentState)) {
++changes.modified;
}
}
for (const auto& [pathKey, previousState] : previousSnapshot) {
(void)previousState;
if (currentSnapshot.find(pathKey) == currentSnapshot.end()) {
++changes.removed;
}
}
return changes;
}
} // namespace Editor
} // namespace XCEngine

View File

@@ -0,0 +1,72 @@
#pragma once
#include "Core/IEditorContext.h"
#include <cstdint>
#include <filesystem>
#include <string>
#include <unordered_map>
namespace XCEngine {
namespace Editor {
class ProjectAssetWatcher {
public:
void Attach(const std::string& projectPath);
void Detach();
void Update(IEditorContext& context, float dt);
private:
struct AssetEntryState {
bool isDirectory = false;
std::uint64_t fileSize = 0;
std::uint64_t writeTime = 0;
bool operator==(const AssetEntryState& other) const {
return isDirectory == other.isDirectory &&
fileSize == other.fileSize &&
writeTime == other.writeTime;
}
};
struct ChangeStats {
std::uint32_t added = 0;
std::uint32_t modified = 0;
std::uint32_t removed = 0;
bool HasChanges() const {
return added > 0 || modified > 0 || removed > 0;
}
void Clear() {
added = 0;
modified = 0;
removed = 0;
}
void Accumulate(const ChangeStats& other) {
added += other.added;
modified += other.modified;
removed += other.removed;
}
};
using Snapshot = std::unordered_map<std::string, AssetEntryState>;
void RefreshProjectBinding(const std::string& projectPath);
void ScanForChanges();
void FlushPendingRefresh(IEditorContext& context);
static void BuildSnapshot(const std::filesystem::path& assetsRoot, Snapshot& outSnapshot);
static ChangeStats DiffSnapshots(const Snapshot& previousSnapshot, const Snapshot& currentSnapshot);
std::string m_projectPath;
std::filesystem::path m_assetsRoot;
Snapshot m_snapshot;
ChangeStats m_pendingChanges;
float m_scanCooldownSeconds = 0.0f;
float m_debounceSeconds = 0.0f;
bool m_hasPendingRefresh = false;
};
} // namespace Editor
} // namespace XCEngine