diff --git a/include/TLuaEngine.h b/include/TLuaEngine.h index ee51eb1..0c26d3a 100644 --- a/include/TLuaEngine.h +++ b/include/TLuaEngine.h @@ -34,6 +34,8 @@ private: TTCPServer& mTCPServer; TUDPServer& mUDPServer; TServer& mServer; + std::string mPath; + bool mShutdown { false }; TSetOfLuaFile mLuaFiles; }; diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index 4a0574e..2c9c5e7 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -2,6 +2,7 @@ #include "TLuaFile.h" #include +#include namespace fs = std::filesystem; @@ -17,12 +18,34 @@ TLuaEngine::TLuaEngine(TServer& Server, TTCPServer& TCPServer, TUDPServer& UDPSe fs::create_directory(Path); } FolderList(Path, false); + mPath = Path; + Application::RegisterShutdownHandler([&] { mShutdown = true; }); Start(); } void TLuaEngine::operator()() { info("Lua system online"); - // thread main + while (!mShutdown) { + if (!mLuaFiles.empty()) { + for (auto& Script : mLuaFiles) { + struct stat Info { }; + if (stat(Script->GetFileName().c_str(), &Info) != 0) { + Script->SetStopThread(true); + mLuaFiles.erase(Script); + info(("[HOTSWAP] Removed removed script due to delete")); + break; + } + if (Script->GetLastWrite() != fs::last_write_time(Script->GetFileName())) { + Script->SetStopThread(true); + info(("[HOTSWAP] Updated Scripts due to edit")); + Script->SetLastWrite(fs::last_write_time(Script->GetFileName())); + Script->Reload(); + } + } + } + FolderList(mPath, true); + std::this_thread::sleep_for(std::chrono::seconds(2)); + } } std::optional> TLuaEngine::GetScript(lua_State* L) {