208 lines
5.6 KiB
C++
208 lines
5.6 KiB
C++
#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
|