add onFileChanged (fixes #116)

This commit is contained in:
Lion Kortlepel 2022-07-14 01:18:50 +02:00
parent ad414ec5c9
commit a2cc629153
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
3 changed files with 12 additions and 15 deletions

View File

@ -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__))

View File

@ -18,6 +18,6 @@ public:
private:
std::shared_ptr<TLuaEngine> mEngine;
fs::path mPath;
std::atomic_bool mShutdown;
std::atomic_bool mShutdown { false };
std::unordered_map<std::string, fs::file_time_type> mFileTimes;
};

View File

@ -5,6 +5,7 @@
TPluginMonitor::TPluginMonitor(const fs::path& Path, std::shared_ptr<TLuaEngine> 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<TLuaEngine>
void TPluginMonitor::operator()() {
RegisterThread("PluginMonitor");
beammp_info("PluginMonitor started");
Application::SetSubsystemStatus("PluginMonitor", Application::Status::Good);
while (!mShutdown) {
std::vector<std::string> 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);
}