Files
simian/src/HotReload.cpp
T
nick 3e23f7da8e
CI / build-and-test (push) Failing after 10m16s
CI / build-and-test (pull_request) Successful in 3m2s
feat: added physFS
2026-03-11 16:43:36 +13:00

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;
}