From 9d0caf2c7dede0a87c41890486f9089a3c91bb2b Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Tue, 21 Sep 2021 16:17:40 +0200 Subject: [PATCH] Lua: Implement Hot-Reload --- include/TLuaEngine.h | 2 +- src/Common.cpp | 2 ++ src/TLuaEngine.cpp | 9 +++++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/include/TLuaEngine.h b/include/TLuaEngine.h index e6bd085..1b1608b 100644 --- a/include/TLuaEngine.h +++ b/include/TLuaEngine.h @@ -165,7 +165,7 @@ private: TPluginMonitor mPluginMonitor; std::atomic_bool mShutdown { false }; fs::path mResourceServerPath; - std::vector mLuaPlugins; + std::vector> mLuaPlugins; std::unordered_map> mLuaStates; std::recursive_mutex mLuaStatesMutex; std::unordered_map>> mLuaEvents; diff --git a/src/Common.cpp b/src/Common.cpp index 1fe02d6..f1bcfab 100644 --- a/src/Common.cpp +++ b/src/Common.cpp @@ -159,7 +159,9 @@ std::string ThreadName(bool DebugModeOverride) { } void RegisterThread(const std::string& str) { +#if defined(__linux) beammp_trace(str + " is " + std::to_string(gettid())); +#endif // __linux auto Lock = std::unique_lock(ThreadNameMapMutex); threadNameMap[std::this_thread::get_id()] = str; } diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index e678f54..b967bdf 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -173,7 +173,8 @@ void TLuaEngine::InitializePlugin(const fs::path& Folder, const TLuaPluginConfig EnsureStateExists(Config.StateId, Folder.stem().string(), true); mLuaStates[Config.StateId]->AddPath(Folder); // add to cpath + path Lock.unlock(); - TLuaPlugin Plugin(*this, Config, Folder); + auto Plugin = std::make_shared(*this, Config, Folder); + mLuaPlugins.emplace_back(std::move(Plugin)); } void TLuaEngine::FindAndParseConfig(const fs::path& Folder, TLuaPluginConfig& Config) { @@ -661,10 +662,12 @@ void TPluginMonitor::operator()() { while (!mShutdown) { std::this_thread::sleep_for(std::chrono::seconds(3)); for (const auto& Pair : mFileTimes) { + beammp_trace("checking for hot-reloadable files"); auto CurrentTime = fs::last_write_time(Pair.first); if (CurrentTime != Pair.second) { mFileTimes[Pair.first] = CurrentTime; - if (fs::equivalent(fs::path(Pair.first), mPath / "Server")) { + // 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"); // is in root folder, so reload std::ifstream FileStream(Pair.first, std::ios::in | std::ios::binary); @@ -675,6 +678,7 @@ void TPluginMonitor::operator()() { TLuaChunk Chunk(Contents, Pair.first, fs::path(Pair.first).parent_path().string()); auto StateID = mEngine.GetStateIDForPlugin(fs::path(Pair.first).parent_path()); auto Res = mEngine.EnqueueScript(StateID, Chunk); + // TODO: call onInit while (!Res->Ready) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); } @@ -682,6 +686,7 @@ void TPluginMonitor::operator()() { beammp_lua_error(Res->ErrorMessage); } } 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