Various fixes

This commit is contained in:
Lion Kortlepel 2021-08-13 16:50:42 +02:00 committed by Lion
parent 003a8269aa
commit d054214b7f
5 changed files with 45 additions and 23 deletions

View File

@ -27,12 +27,13 @@ public:
private: private:
void FolderList(const std::string& Path, bool HotSwap); void FolderList(const std::string& Path, bool HotSwap);
void RegisterFiles(const std::string& Path, bool HotSwap); void RegisterFiles(const fs::path& Path, bool HotSwap);
bool NewFile(const std::string& Path); bool IsNewFile(const std::string& Path);
TNetwork& mNetwork; TNetwork& mNetwork;
TServer& mServer; TServer& mServer;
std::string mPath; std::string mPath;
bool mShutdown { false }; bool mShutdown { false };
TSetOfLuaFile mLuaFiles; TSetOfLuaFile mLuaFiles;
std::mutex mListMutex;
}; };

View File

@ -54,6 +54,7 @@ private:
bool mStopThread = false; bool mStopThread = false;
bool mConsole = false; bool mConsole = false;
void Load(); void Load();
std::mutex mInitMutex;
}; };
std::any TriggerLuaEvent(const std::string& Event, bool local, TLuaFile* Caller, std::shared_ptr<TLuaArg> arg, bool Wait); std::any TriggerLuaEvent(const std::string& Event, bool local, TLuaFile* Caller, std::shared_ptr<TLuaArg> arg, bool Wait);

View File

@ -10,8 +10,8 @@
#include <thread> #include <thread>
#include <zlib.h> #include <zlib.h>
#include "Http.h"
#include "CustomAssert.h" #include "CustomAssert.h"
#include "Http.h"
std::unique_ptr<TConsole> Application::mConsole = std::make_unique<TConsole>(); std::unique_ptr<TConsole> Application::mConsole = std::make_unique<TConsole>();
@ -65,7 +65,7 @@ void Application::CheckForUpdates() {
std::string RealVersionString = std::to_string(RemoteVersion[0]) + "."; std::string RealVersionString = std::to_string(RemoteVersion[0]) + ".";
RealVersionString += std::to_string(RemoteVersion[1]) + "."; RealVersionString += std::to_string(RemoteVersion[1]) + ".";
RealVersionString += std::to_string(RemoteVersion[2]); 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 { } else {
info("Server up-to-date!"); info("Server up-to-date!");
} }
@ -127,8 +127,10 @@ std::string DeComp(std::string Compressed) {
// thread name stuff // thread name stuff
std::map<std::thread::id, std::string> threadNameMap; std::map<std::thread::id, std::string> threadNameMap;
std::mutex ThreadNameMapMutex;
std::string ThreadName(bool DebugModeOverride) { std::string ThreadName(bool DebugModeOverride) {
auto Lock = std::unique_lock(ThreadNameMapMutex);
if (DebugModeOverride || Application::Settings.DebugModeEnabled) { if (DebugModeOverride || Application::Settings.DebugModeEnabled) {
auto id = std::this_thread::get_id(); auto id = std::this_thread::get_id();
if (threadNameMap.find(id) != threadNameMap.end()) { if (threadNameMap.find(id) != threadNameMap.end()) {
@ -140,6 +142,7 @@ std::string ThreadName(bool DebugModeOverride) {
} }
void RegisterThread(const std::string str) { void RegisterThread(const std::string str) {
auto Lock = std::unique_lock(ThreadNameMapMutex);
threadNameMap[std::this_thread::get_id()] = str; threadNameMap[std::this_thread::get_id()] = str;
} }

View File

@ -66,37 +66,47 @@ std::optional<std::reference_wrapper<TLuaFile>> TLuaEngine::GetScript(lua_State*
} }
void TLuaEngine::FolderList(const std::string& Path, bool HotSwap) { void TLuaEngine::FolderList(const std::string& Path, bool HotSwap) {
auto Lock = std::unique_lock(mListMutex);
for (const auto& entry : fs::directory_iterator(Path)) { for (const auto& entry : fs::directory_iterator(Path)) {
auto pos = entry.path().filename().string().find('.'); if (fs::is_directory(entry)) {
if (pos == std::string::npos) { RegisterFiles(entry.path(), HotSwap);
RegisterFiles(entry.path().string(), HotSwap);
} }
} }
} }
void TLuaEngine::RegisterFiles(const std::string& Path, bool HotSwap) { void TLuaEngine::RegisterFiles(const fs::path& Path, bool HotSwap) {
std::string Name = Path.substr(Path.find_last_of('\\') + 1); std::string Name = Path.filename();
if (!HotSwap) if (!HotSwap)
info(("Loading plugin : ") + Name); info(("Loading plugin : ") + Name);
std::vector<fs::path> Entries;
for (const auto& entry : fs::directory_iterator(Path)) { for (const auto& entry : fs::directory_iterator(Path)) {
auto pos = entry.path().string().find((".lua")); if (entry.path().extension() == ".lua") {
if (pos != std::string::npos && entry.path().string().length() - pos == 4) { Entries.push_back(entry);
if (!HotSwap || NewFile(entry.path().string())) { }
auto FileName = entry.path().string(); }
std::unique_ptr<TLuaFile> ScriptToInsert(new TLuaFile(*this)); std::sort(Entries.begin(), Entries.end(), [](const fs::path& first, const fs::path& second) {
auto& Script = *ScriptToInsert; auto firstStr = first.string();
mLuaFiles.insert(std::move(ScriptToInsert)); auto secondStr = second.string();
Script.Init(Name, FileName, fs::last_write_time(FileName)); std::transform(firstStr.begin(), firstStr.end(), firstStr.begin(), ::tolower);
if (HotSwap) std::transform(secondStr.begin(), secondStr.end(), secondStr.begin(), ::tolower);
info(("[HOTSWAP] Added : ") + Script.GetFileName().substr(Script.GetFileName().find('\\'))); return firstStr < secondStr;
} });
for (const fs::path& Entry : Entries) {
if (!HotSwap || IsNewFile(Entry.string())) {
auto FileName = Entry.string();
std::unique_ptr<TLuaFile> 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) { for (auto& Script : mLuaFiles) {
if (Path == Script->GetFileName()) if (fs::absolute(Path) == fs::absolute(Script->GetFileName()))
return false; return false;
} }
return true; return true;

View File

@ -110,7 +110,13 @@ bool ConsoleCheck(lua_State* L, int r) {
bool CheckLua(lua_State* L, int r) { bool CheckLua(lua_State* L, int r) {
if (r != LUA_OK) { 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); auto MaybeS = Engine().GetScript(L);
if (MaybeS.has_value()) { if (MaybeS.has_value()) {
TLuaFile& S = MaybeS.value(); TLuaFile& S = MaybeS.value();
@ -623,6 +629,7 @@ int lua_Print(lua_State* L) {
int lua_TempFix(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) { 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 // set global engine for lua_* functions
if (!TheEngine) { if (!TheEngine) {
TheEngine = &mEngine; TheEngine = &mEngine;