Skip to content

Commit

Permalink
fix: update on added files (doodlum#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse authored Apr 4, 2024
1 parent 9a2d242 commit f773793
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
50 changes: 32 additions & 18 deletions src/ShaderCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1883,6 +1883,29 @@ namespace SIE
GetHumanTime(GetEta() + totalMs));
}

void UpdateListener::UpdateCache(const std::filesystem::path& filePath, SIE::ShaderCache& cache, bool& clearCache, bool& fileDone)
{
std::string extension = filePath.extension().string();
std::string parentDir = filePath.parent_path().string();
std::string shaderTypeString = filePath.stem().string();
std::chrono::time_point<std::chrono::system_clock> modifiedTime{};
auto shaderType = magic_enum::enum_cast<RE::BSShader::Type>(shaderTypeString, magic_enum::case_insensitive);
fileDone = true;
if (std::filesystem::exists(filePath))
modifiedTime = std::chrono::clock_cast<std::chrono::system_clock>(std::filesystem::last_write_time(filePath));
else // if file doesn't exist, don't do anything
return;
if (!std::filesystem::is_directory(filePath) && extension.starts_with(".hlsl") && parentDir.ends_with("Shaders") && shaderType.has_value()) { // TODO: Case insensitive checks
// Shader types, so only invalidate specific shader type (e.g,. Lighting)
cache.InsertModifiedShaderMap(shaderTypeString, modifiedTime);
cache.Clear(shaderType.value());
} else if (!std::filesystem::is_directory(filePath) && extension.starts_with(".hlsl")) { // TODO: Case insensitive checks
// all other shaders, since we don't know what is using it, clear everything
clearCache = true;
}
fileDone = false;
}

void UpdateListener::processQueue()
{
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
Expand All @@ -1894,36 +1917,27 @@ namespace SIE
bool clearCache = false;
for (fileAction fAction : queue) {
const std::filesystem::path filePath = std::filesystem::path(std::format("{}\\{}", fAction.dir, fAction.filename));
std::chrono::time_point<std::chrono::system_clock> modifiedTime{};
std::string extension = filePath.extension().string();
std::string parentDir = filePath.parent_path().string();
std::string shaderTypeString = filePath.stem().string();
auto shaderType = magic_enum::enum_cast<RE::BSShader::Type>(shaderTypeString, magic_enum::case_insensitive);
bool fileDone = false;
switch (fAction.action) {
case efsw::Actions::Add:
logger::debug("Detected Added path {}", filePath.string());
UpdateCache(filePath, cache, clearCache, fileDone);
break;
case efsw::Actions::Delete:
logger::debug("Detected Deleted path {}", filePath.string());
break;
case efsw::Actions::Modified:
logger::debug("Detected changed path {}", filePath.string());
if (std::filesystem::exists(filePath))
modifiedTime = std::chrono::clock_cast<std::chrono::system_clock>(std::filesystem::last_write_time(filePath));
else // if file doesn't exist, don't do anything
return;
if (!std::filesystem::is_directory(filePath) && extension.starts_with(".hlsl") && parentDir.ends_with("Shaders") && shaderType.has_value()) { // TODO: Case insensitive checks
// Shader types, so only invalidate specific shader type (e.g,. Lighting)
cache.InsertModifiedShaderMap(shaderTypeString, modifiedTime);
cache.Clear(shaderType.value());
} else if (!std::filesystem::is_directory(filePath) && extension.starts_with(".hlsl")) { // TODO: Case insensitive checks
// all other shaders, since we don't know what is using it, clear everything
clearCache = true;
}
logger::debug("Detected Changed path {}", filePath.string());
UpdateCache(filePath, cache, clearCache, fileDone);
break;
case efsw::Actions::Moved:
logger::debug("Detected Moved path {}", filePath.string());
break;
default:
logger::error("Filewatcher received invalid action {}", magic_enum::enum_name(fAction.action));
}
if (fileDone)
continue;
}
if (clearCache) {
cache.DeleteDiskCache();
Expand Down
1 change: 1 addition & 0 deletions src/ShaderCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ namespace SIE
class UpdateListener : public efsw::FileWatchListener
{
public:
void UpdateCache(const std::filesystem::path& filePath, SIE::ShaderCache& cache, bool& clearCache, bool& retFlag);
void processQueue();
void handleFileAction(efsw::WatchID, const std::string& dir, const std::string& filename, efsw::Action action, std::string) override;

Expand Down

0 comments on commit f773793

Please sign in to comment.