mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-04-24 00:46:59 +00:00
Lua: Implement Hot-Reload
This commit is contained in:
@@ -165,7 +165,7 @@ private:
|
|||||||
TPluginMonitor mPluginMonitor;
|
TPluginMonitor mPluginMonitor;
|
||||||
std::atomic_bool mShutdown { false };
|
std::atomic_bool mShutdown { false };
|
||||||
fs::path mResourceServerPath;
|
fs::path mResourceServerPath;
|
||||||
std::vector<TLuaPlugin*> mLuaPlugins;
|
std::vector<std::shared_ptr<TLuaPlugin>> mLuaPlugins;
|
||||||
std::unordered_map<TLuaStateId, std::unique_ptr<StateThreadData>> mLuaStates;
|
std::unordered_map<TLuaStateId, std::unique_ptr<StateThreadData>> mLuaStates;
|
||||||
std::recursive_mutex mLuaStatesMutex;
|
std::recursive_mutex mLuaStatesMutex;
|
||||||
std::unordered_map<std::string /* event name */, std::unordered_map<TLuaStateId, std::set<std::string>>> mLuaEvents;
|
std::unordered_map<std::string /* event name */, std::unordered_map<TLuaStateId, std::set<std::string>>> mLuaEvents;
|
||||||
|
|||||||
@@ -159,7 +159,9 @@ std::string ThreadName(bool DebugModeOverride) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void RegisterThread(const std::string& str) {
|
void RegisterThread(const std::string& str) {
|
||||||
|
#if defined(__linux)
|
||||||
beammp_trace(str + " is " + std::to_string(gettid()));
|
beammp_trace(str + " is " + std::to_string(gettid()));
|
||||||
|
#endif // __linux
|
||||||
auto Lock = std::unique_lock(ThreadNameMapMutex);
|
auto Lock = std::unique_lock(ThreadNameMapMutex);
|
||||||
threadNameMap[std::this_thread::get_id()] = str;
|
threadNameMap[std::this_thread::get_id()] = str;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -173,7 +173,8 @@ void TLuaEngine::InitializePlugin(const fs::path& Folder, const TLuaPluginConfig
|
|||||||
EnsureStateExists(Config.StateId, Folder.stem().string(), true);
|
EnsureStateExists(Config.StateId, Folder.stem().string(), true);
|
||||||
mLuaStates[Config.StateId]->AddPath(Folder); // add to cpath + path
|
mLuaStates[Config.StateId]->AddPath(Folder); // add to cpath + path
|
||||||
Lock.unlock();
|
Lock.unlock();
|
||||||
TLuaPlugin Plugin(*this, Config, Folder);
|
auto Plugin = std::make_shared<TLuaPlugin>(*this, Config, Folder);
|
||||||
|
mLuaPlugins.emplace_back(std::move(Plugin));
|
||||||
}
|
}
|
||||||
|
|
||||||
void TLuaEngine::FindAndParseConfig(const fs::path& Folder, TLuaPluginConfig& Config) {
|
void TLuaEngine::FindAndParseConfig(const fs::path& Folder, TLuaPluginConfig& Config) {
|
||||||
@@ -661,10 +662,12 @@ void TPluginMonitor::operator()() {
|
|||||||
while (!mShutdown) {
|
while (!mShutdown) {
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(3));
|
std::this_thread::sleep_for(std::chrono::seconds(3));
|
||||||
for (const auto& Pair : mFileTimes) {
|
for (const auto& Pair : mFileTimes) {
|
||||||
|
beammp_trace("checking for hot-reloadable files");
|
||||||
auto CurrentTime = fs::last_write_time(Pair.first);
|
auto CurrentTime = fs::last_write_time(Pair.first);
|
||||||
if (CurrentTime != Pair.second) {
|
if (CurrentTime != Pair.second) {
|
||||||
mFileTimes[Pair.first] = CurrentTime;
|
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");
|
beammp_info("File \"" + Pair.first + "\" changed, reloading");
|
||||||
// is in root folder, so reload
|
// is in root folder, so reload
|
||||||
std::ifstream FileStream(Pair.first, std::ios::in | std::ios::binary);
|
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());
|
TLuaChunk Chunk(Contents, Pair.first, fs::path(Pair.first).parent_path().string());
|
||||||
auto StateID = mEngine.GetStateIDForPlugin(fs::path(Pair.first).parent_path());
|
auto StateID = mEngine.GetStateIDForPlugin(fs::path(Pair.first).parent_path());
|
||||||
auto Res = mEngine.EnqueueScript(StateID, Chunk);
|
auto Res = mEngine.EnqueueScript(StateID, Chunk);
|
||||||
|
// TODO: call onInit
|
||||||
while (!Res->Ready) {
|
while (!Res->Ready) {
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||||
}
|
}
|
||||||
@@ -682,6 +686,7 @@ void TPluginMonitor::operator()() {
|
|||||||
beammp_lua_error(Res->ErrorMessage);
|
beammp_lua_error(Res->ErrorMessage);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// TODO: trigger onFileChanged event
|
||||||
beammp_trace("Change detected in file \"" + Pair.first + "\", event trigger not implemented yet");
|
beammp_trace("Change detected in file \"" + Pair.first + "\", event trigger not implemented yet");
|
||||||
/*
|
/*
|
||||||
// is in subfolder, dont reload, just trigger an event
|
// is in subfolder, dont reload, just trigger an event
|
||||||
|
|||||||
Reference in New Issue
Block a user