52 lines
1.4 KiB
C++
52 lines
1.4 KiB
C++
#include "HotReload.h"
|
|
#include "PhysFSManager.h"
|
|
#include <iostream>
|
|
|
|
HotReload::HotReload(const std::string& pathToWatch) : path(pathToWatch), lastWriteTime(0) {
|
|
UpdateLastWriteTime();
|
|
}
|
|
|
|
bool HotReload::CheckForChanges() {
|
|
std::time_t current = GetLatestWriteTimeInPath();
|
|
if (current != lastWriteTime) {
|
|
lastWriteTime = current;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void HotReload::UpdateLastWriteTime() {
|
|
lastWriteTime = GetLatestWriteTimeInPath();
|
|
}
|
|
|
|
std::time_t HotReload::GetFileWriteTime(const std::string& filename) {
|
|
return static_cast<std::time_t>(PhysFSManager::GetModTime(filename));
|
|
}
|
|
|
|
std::time_t HotReload::GetLatestWriteTimeInPath() {
|
|
if (PhysFSManager::Exists(path) && !PhysFSManager::IsDirectory(path)) {
|
|
return GetFileWriteTime(path);
|
|
}
|
|
|
|
if (PhysFSManager::IsDirectory(path)) {
|
|
std::time_t newest = 0;
|
|
auto entries = PhysFSManager::ListFiles(path);
|
|
for (const auto& entry : entries) {
|
|
if (entry == "." || entry == "..") continue;
|
|
std::string child = path;
|
|
if (!child.empty() && child.back() != '/') {
|
|
child.push_back('/');
|
|
}
|
|
child += entry;
|
|
if (PhysFSManager::IsDirectory(child)) {
|
|
continue;
|
|
}
|
|
std::time_t t = GetFileWriteTime(child);
|
|
if (t > newest) newest = t;
|
|
}
|
|
return newest;
|
|
}
|
|
|
|
return 0;
|
|
}
|