From a2cc6291537464777bbc96454e15e624a4dbfcce Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 14 Jul 2022 01:18:50 +0200 Subject: [PATCH] add onFileChanged (fixes #116) --- include/Common.h | 1 + include/TPluginMonitor.h | 2 +- src/TPluginMonitor.cpp | 24 ++++++++++-------------- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/include/Common.h b/include/Common.h index 313df2b..28b1ffd 100644 --- a/include/Common.h +++ b/include/Common.h @@ -220,6 +220,7 @@ void RegisterThread(const std::string& str); #define beammp_errorf(...) beammp_error(fmt::format(__VA_ARGS__)) #define beammp_infof(...) beammp_info(fmt::format(__VA_ARGS__)) + #define beammp_debugf(...) beammp_debug(fmt::format(__VA_ARGS__)) #define beammp_warnf(...) beammp_warn(fmt::format(__VA_ARGS__)) #define beammp_tracef(...) beammp_trace(fmt::format(__VA_ARGS__)) #define beammp_lua_errorf(...) beammp_lua_error(fmt::format(__VA_ARGS__)) diff --git a/include/TPluginMonitor.h b/include/TPluginMonitor.h index 3d82c4f..a617987 100644 --- a/include/TPluginMonitor.h +++ b/include/TPluginMonitor.h @@ -18,6 +18,6 @@ public: private: std::shared_ptr mEngine; fs::path mPath; - std::atomic_bool mShutdown; + std::atomic_bool mShutdown { false }; std::unordered_map mFileTimes; }; diff --git a/src/TPluginMonitor.cpp b/src/TPluginMonitor.cpp index 4cae6a0..7c84fec 100644 --- a/src/TPluginMonitor.cpp +++ b/src/TPluginMonitor.cpp @@ -5,6 +5,7 @@ TPluginMonitor::TPluginMonitor(const fs::path& Path, std::shared_ptr Engine) : mEngine(Engine) , mPath(Path) { + Application::SetSubsystemStatus("PluginMonitor", Application::Status::Starting); if (!fs::exists(mPath)) { fs::create_directories(mPath); } @@ -28,16 +29,17 @@ TPluginMonitor::TPluginMonitor(const fs::path& Path, std::shared_ptr void TPluginMonitor::operator()() { RegisterThread("PluginMonitor"); beammp_info("PluginMonitor started"); + Application::SetSubsystemStatus("PluginMonitor", Application::Status::Good); while (!mShutdown) { std::vector ToRemove; for (const auto& Pair : mFileTimes) { try { auto CurrentTime = fs::last_write_time(Pair.first); - if (CurrentTime != Pair.second) { + if (CurrentTime > Pair.second) { mFileTimes[Pair.first] = CurrentTime; // grandparent of the path should be Resources/Server if (fs::equivalent(fs::path(Pair.first).parent_path().parent_path(), mPath)) { - beammp_info("File \"" + Pair.first + "\" changed, reloading"); + beammp_infof("File \"{}\" changed, reloading", Pair.first); // is in root folder, so reload std::ifstream FileStream(Pair.first, std::ios::in | std::ios::binary); auto Size = std::filesystem::file_size(Pair.first); @@ -49,30 +51,24 @@ void TPluginMonitor::operator()() { auto Res = mEngine->EnqueueScript(StateID, Chunk); mEngine->AddResultToCheck(Res); mEngine->ReportErrors(mEngine->TriggerLocalEvent(StateID, "onInit")); + mEngine->ReportErrors(mEngine->TriggerEvent("onFileChanged", "", Pair.first)); } else { - // TODO: trigger onFileChanged event - beammp_trace("Change detected in file \"" + Pair.first + "\", event trigger not implemented yet"); - /* // is in subfolder, dont reload, just trigger an event - auto Results = mEngine.TriggerEvent("onFileChanged", "", Pair.first); - mEngine.WaitForAll(Results); - for (const auto& Result : Results) { - if (Result->Error) { - beammp_lua_error(Result->ErrorMessage); - } - }*/ + beammp_debugf("File \"{}\" changed, not reloading because it's in a subdirectory. Triggering 'onFileChanged' event instead", Pair.first); + mEngine->ReportErrors(mEngine->TriggerEvent("onFileChanged", "", Pair.first)); } } } catch (const std::exception& e) { ToRemove.push_back(Pair.first); } for (size_t i = 0; i < 3 && !mShutdown; ++i) { - std::this_thread::sleep_for(std::chrono::seconds(i)); + std::this_thread::sleep_for(std::chrono::seconds(1)); } } for (const auto& File : ToRemove) { mFileTimes.erase(File); - beammp_warn("file '" + File + "' couldn't be accessed, so it was removed from plugin hot reload monitor (probably got deleted)"); + beammp_warnf("File \"{}\" couldn't be accessed, so it was removed from plugin hot reload monitor (probably got deleted)", File); } } + Application::SetSubsystemStatus("PluginMonitor", Application::Status::Shutdown); }