diff --git a/include/TLuaEngine.h b/include/TLuaEngine.h index 9e59964..1798f3e 100644 --- a/include/TLuaEngine.h +++ b/include/TLuaEngine.h @@ -27,12 +27,13 @@ public: private: void FolderList(const std::string& Path, bool HotSwap); - void RegisterFiles(const std::string& Path, bool HotSwap); - bool NewFile(const std::string& Path); + void RegisterFiles(const fs::path& Path, bool HotSwap); + bool IsNewFile(const std::string& Path); TNetwork& mNetwork; TServer& mServer; std::string mPath; bool mShutdown { false }; TSetOfLuaFile mLuaFiles; + std::mutex mListMutex; }; diff --git a/include/TLuaFile.h b/include/TLuaFile.h index a94a6b8..43620a9 100644 --- a/include/TLuaFile.h +++ b/include/TLuaFile.h @@ -54,6 +54,7 @@ private: bool mStopThread = false; bool mConsole = false; void Load(); + std::mutex mInitMutex; }; std::any TriggerLuaEvent(const std::string& Event, bool local, TLuaFile* Caller, std::shared_ptr arg, bool Wait); diff --git a/src/Common.cpp b/src/Common.cpp index 01cc123..630b21b 100644 --- a/src/Common.cpp +++ b/src/Common.cpp @@ -10,8 +10,8 @@ #include #include -#include "Http.h" #include "CustomAssert.h" +#include "Http.h" std::unique_ptr Application::mConsole = std::make_unique(); @@ -65,7 +65,7 @@ void Application::CheckForUpdates() { std::string RealVersionString = std::to_string(RemoteVersion[0]) + "."; RealVersionString += std::to_string(RemoteVersion[1]) + "."; RealVersionString += std::to_string(RemoteVersion[2]); - warn( "NEW VERSION OUT! There's a new version (v" + RealVersionString + ") of the BeamMP-Server available! For info on how to update your server, visit https://wiki.beammp.com/en/home/server-maintenance#updating-the-server."); + warn(std::string(ANSI_YELLOW_BOLD) + "NEW VERSION OUT! There's a new version (v" + RealVersionString + ") of the BeamMP-Server available! For info on how to update your server, visit https://wiki.beammp.com/en/home/server-maintenance#updating-the-server." + std::string(ANSI_RESET)); } else { info("Server up-to-date!"); } @@ -127,8 +127,10 @@ std::string DeComp(std::string Compressed) { // thread name stuff std::map threadNameMap; +std::mutex ThreadNameMapMutex; std::string ThreadName(bool DebugModeOverride) { + auto Lock = std::unique_lock(ThreadNameMapMutex); if (DebugModeOverride || Application::Settings.DebugModeEnabled) { auto id = std::this_thread::get_id(); if (threadNameMap.find(id) != threadNameMap.end()) { @@ -140,6 +142,7 @@ std::string ThreadName(bool DebugModeOverride) { } void RegisterThread(const std::string str) { + auto Lock = std::unique_lock(ThreadNameMapMutex); threadNameMap[std::this_thread::get_id()] = str; } diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index 6db855a..29b78ef 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -66,37 +66,47 @@ std::optional> TLuaEngine::GetScript(lua_State* } void TLuaEngine::FolderList(const std::string& Path, bool HotSwap) { + auto Lock = std::unique_lock(mListMutex); for (const auto& entry : fs::directory_iterator(Path)) { - auto pos = entry.path().filename().string().find('.'); - if (pos == std::string::npos) { - RegisterFiles(entry.path().string(), HotSwap); + if (fs::is_directory(entry)) { + RegisterFiles(entry.path(), HotSwap); } } } -void TLuaEngine::RegisterFiles(const std::string& Path, bool HotSwap) { - std::string Name = Path.substr(Path.find_last_of('\\') + 1); +void TLuaEngine::RegisterFiles(const fs::path& Path, bool HotSwap) { + std::string Name = Path.filename(); if (!HotSwap) info(("Loading plugin : ") + Name); + std::vector Entries; for (const auto& entry : fs::directory_iterator(Path)) { - auto pos = entry.path().string().find((".lua")); - if (pos != std::string::npos && entry.path().string().length() - pos == 4) { - if (!HotSwap || NewFile(entry.path().string())) { - auto FileName = entry.path().string(); - std::unique_ptr ScriptToInsert(new TLuaFile(*this)); - auto& Script = *ScriptToInsert; - mLuaFiles.insert(std::move(ScriptToInsert)); - Script.Init(Name, FileName, fs::last_write_time(FileName)); - if (HotSwap) - info(("[HOTSWAP] Added : ") + Script.GetFileName().substr(Script.GetFileName().find('\\'))); - } + if (entry.path().extension() == ".lua") { + Entries.push_back(entry); + } + } + std::sort(Entries.begin(), Entries.end(), [](const fs::path& first, const fs::path& second) { + auto firstStr = first.string(); + auto secondStr = second.string(); + std::transform(firstStr.begin(), firstStr.end(), firstStr.begin(), ::tolower); + std::transform(secondStr.begin(), secondStr.end(), secondStr.begin(), ::tolower); + return firstStr < secondStr; + }); + for (const fs::path& Entry : Entries) { + if (!HotSwap || IsNewFile(Entry.string())) { + auto FileName = Entry.string(); + std::unique_ptr ScriptToInsert(new TLuaFile(*this)); + auto& Script = *ScriptToInsert; + mLuaFiles.insert(std::move(ScriptToInsert)); + Script.Init(Name, FileName, fs::last_write_time(FileName)); + if (HotSwap) + info(("[HOTSWAP] Added : ") + Script.GetFileName().substr(Script.GetFileName().find('\\'))); } } } -bool TLuaEngine::NewFile(const std::string& Path) { +bool TLuaEngine::IsNewFile(const std::string& Path) { for (auto& Script : mLuaFiles) { - if (Path == Script->GetFileName()) + if (fs::absolute(Path) == fs::absolute(Script->GetFileName())) return false; } return true; diff --git a/src/TLuaFile.cpp b/src/TLuaFile.cpp index 0d78b67..2a25853 100644 --- a/src/TLuaFile.cpp +++ b/src/TLuaFile.cpp @@ -110,7 +110,13 @@ bool ConsoleCheck(lua_State* L, int r) { bool CheckLua(lua_State* L, int r) { if (r != LUA_OK) { - std::string msg = lua_tostring(L, -1); + std::string msg = "Unknown"; + if (lua_isstring(L, -1)) { + auto MsgMaybe = lua_tostring(L, -1); + if (MsgMaybe) { + msg = MsgMaybe; + } + } auto MaybeS = Engine().GetScript(L); if (MaybeS.has_value()) { TLuaFile& S = MaybeS.value(); @@ -623,6 +629,7 @@ int lua_Print(lua_State* L) { int lua_TempFix(lua_State* L); void TLuaFile::Init(const std::string& PluginName, const std::string& FileName, fs::file_time_type LastWrote) { + auto Lock = std::unique_lock(mInitMutex); // set global engine for lua_* functions if (!TheEngine) { TheEngine = &mEngine;