Fixed lua crash caused by lion with optimizations

This commit is contained in:
Anonymous275 2021-02-23 00:29:15 +02:00 committed by Anonymous275
parent f52308c439
commit 9b1bf071a8
11 changed files with 29 additions and 29 deletions

View File

@ -12,7 +12,7 @@
class TLuaEngine : public IThreaded { class TLuaEngine : public IThreaded {
public: public:
explicit TLuaEngine(TServer& Server, TTCPServer& TCPServer, TUDPServer& UDPServer); explicit TLuaEngine(TServer& Server, TTCPServer& TCPServer, TUDPServer& UDPServer);
~TLuaEngine(); //~TLuaEngine();
using TSetOfLuaFile = std::set<std::unique_ptr<TLuaFile>>; using TSetOfLuaFile = std::set<std::unique_ptr<TLuaFile>>;

View File

@ -20,7 +20,7 @@ class TLuaEngine;
class TLuaFile { class TLuaFile {
public: public:
void Init(); void Load();
void RegisterEvent(const std::string& Event, const std::string& FunctionName); void RegisterEvent(const std::string& Event, const std::string& FunctionName);
void UnRegisterEvent(const std::string& Event); void UnRegisterEvent(const std::string& Event);
void SetLastWrite(fs::file_time_type time); void SetLastWrite(fs::file_time_type time);
@ -33,7 +33,7 @@ public:
std::string GetOrigin(); std::string GetOrigin();
std::mutex Lock; std::mutex Lock;
void Reload(); void Reload();
TLuaFile(TLuaEngine& Engine, const std::string& PluginName, const std::string& FileName, fs::file_time_type LastWrote, bool Console = false); void Init(const std::string& PluginName, const std::string& FileName, fs::file_time_type LastWrote);
explicit TLuaFile(TLuaEngine& Engine, bool Console = false); explicit TLuaFile(TLuaEngine& Engine, bool Console = false);
~TLuaFile(); ~TLuaFile();
void SetStopThread(bool StopThread) { mStopThread = StopThread; } void SetStopThread(bool StopThread) { mStopThread = StopThread; }

View File

@ -18,14 +18,14 @@ public:
TServer(int argc, char** argv); TServer(int argc, char** argv);
void InsertClient(std::shared_ptr<TClient> Ptr); void InsertClient(const std::shared_ptr<TClient>& Ptr);
std::weak_ptr<TClient> InsertNewClient(); std::weak_ptr<TClient> InsertNewClient();
void RemoveClient(std::weak_ptr<TClient>); void RemoveClient(const std::weak_ptr<TClient>&);
// in Fn, return true to continue, return false to break // in Fn, return true to continue, return false to break
void ForEachClient(const std::function<bool(std::weak_ptr<TClient>)>& Fn); void ForEachClient(const std::function<bool(std::weak_ptr<TClient>)>& Fn);
size_t ClientCount() const; size_t ClientCount() const;
static void GlobalParser(std::weak_ptr<TClient> Client, std::string Packet, TPPSMonitor& PPSMonitor, TUDPServer& UDPServer, TTCPServer& TCPServer); static void GlobalParser(const std::weak_ptr<TClient>& Client, std::string Packet, TPPSMonitor& PPSMonitor, TUDPServer& UDPServer, TTCPServer& TCPServer);
static void HandleEvent(TClient& c, const std::string& Data); static void HandleEvent(TClient& c, const std::string& Data);
private: private:

View File

@ -1,7 +1,6 @@
#include "Common.h" #include "Common.h"
#include "TConsole.h" #include "TConsole.h"
#include <algorithm>
#include <array> #include <array>
#include <iostream> #include <iostream>
#include <zlib.h> #include <zlib.h>

View File

@ -10,7 +10,7 @@ void THeartbeatThread::operator()() {
std::string T; std::string T;
// these are "hot-change" related variables // these are "hot-change" related variables
static std::string Last = ""; static std::string Last;
static std::chrono::high_resolution_clock::time_point LastNormalUpdateTime = std::chrono::high_resolution_clock::now(); static std::chrono::high_resolution_clock::time_point LastNormalUpdateTime = std::chrono::high_resolution_clock::now();
bool isAuth = false; bool isAuth = false;

View File

@ -83,10 +83,11 @@ void TLuaEngine::RegisterFiles(const std::string& Path, bool HotSwap) {
if (pos != std::string::npos && entry.path().string().length() - pos == 4) { if (pos != std::string::npos && entry.path().string().length() - pos == 4) {
if (!HotSwap || NewFile(entry.path().string())) { if (!HotSwap || NewFile(entry.path().string())) {
auto FileName = entry.path().string(); auto FileName = entry.path().string();
std::unique_ptr<TLuaFile> ScriptToInsert(new TLuaFile(*this, Name, FileName, fs::last_write_time(FileName))); std::unique_ptr<TLuaFile> ScriptToInsert(new TLuaFile(*this));
auto& Script = *ScriptToInsert; auto& Script = *ScriptToInsert;
mLuaFiles.insert(std::move(ScriptToInsert)); mLuaFiles.insert(std::move(ScriptToInsert));
Script.Init(); Script.Init(Name, FileName, fs::last_write_time(FileName));
//Script.Load();
if (HotSwap) if (HotSwap)
info(("[HOTSWAP] Added : ") + Script.GetFileName().substr(Script.GetFileName().find('\\'))); info(("[HOTSWAP] Added : ") + Script.GetFileName().substr(Script.GetFileName().find('\\')));
} }
@ -102,5 +103,5 @@ bool TLuaEngine::NewFile(const std::string& Path) {
return true; return true;
} }
TLuaEngine::~TLuaEngine() { /*TLuaEngine::~TLuaEngine() {
} }*/

View File

@ -131,7 +131,7 @@ int lua_RegisterEvent(lua_State* L) {
if (Args == 2 && lua_isstring(L, 1) && lua_isstring(L, 2)) { if (Args == 2 && lua_isstring(L, 1) && lua_isstring(L, 2)) {
Script.RegisterEvent(lua_tostring(L, 1), lua_tostring(L, 2)); Script.RegisterEvent(lua_tostring(L, 1), lua_tostring(L, 2));
} else } else
SendError(Engine(), L, ("RegisterEvent invalid argument count expected 2 got ") + std::to_string(Args)); SendError(Engine(), L, "RegisterEvent invalid argument count expected 2 got " + std::to_string(Args));
return 0; return 0;
} }
int lua_TriggerEventL(lua_State* L) { int lua_TriggerEventL(lua_State* L) {
@ -308,7 +308,7 @@ int lua_GetGuest(lua_State* L) {
} }
int lua_GetAllPlayers(lua_State* L) { int lua_GetAllPlayers(lua_State* L) {
lua_newtable(L); lua_newtable(L);
Engine().Server().ForEachClient([&](std::weak_ptr<TClient> ClientPtr) -> bool { Engine().Server().ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
if (ClientPtr.expired()) if (ClientPtr.expired())
return true; return true;
auto Client = ClientPtr.lock(); auto Client = ClientPtr.lock();
@ -575,9 +575,8 @@ int lua_Print(lua_State* L) {
int lua_TempFix(lua_State* L); int lua_TempFix(lua_State* L);
TLuaFile::TLuaFile(TLuaEngine& Engine, const std::string& PluginName, const std::string& FileName, fs::file_time_type LastWrote, bool Console)
: mEngine(Engine) void TLuaFile::Init(const std::string& PluginName, const std::string& FileName, fs::file_time_type LastWrote){
, mLuaState(luaL_newstate()) {
// set global engine for lua_* functions // set global engine for lua_* functions
if (!TheEngine) { if (!TheEngine) {
TheEngine = &mEngine; TheEngine = &mEngine;
@ -590,14 +589,16 @@ TLuaFile::TLuaFile(TLuaEngine& Engine, const std::string& PluginName, const std:
SetFileName(FileName); SetFileName(FileName);
} }
SetLastWrite(LastWrote); SetLastWrite(LastWrote);
Init(); Load();
} }
TLuaFile::TLuaFile(TLuaEngine& Engine, bool Console) TLuaFile::TLuaFile(TLuaEngine& Engine, bool Console)
: mEngine(Engine) : mEngine(Engine)
, mLuaState(luaL_newstate()) { , mLuaState(luaL_newstate()) {
mConsole = Console; if(Console) {
Init(); mConsole = Console;
Load();
}
} }
void TLuaFile::Execute(const std::string& Command) { void TLuaFile::Execute(const std::string& Command) {
@ -642,7 +643,7 @@ void TLuaFile::SetFileName(const std::string& Name) {
mFileName = Name; mFileName = Name;
} }
void TLuaFile::Init() { void TLuaFile::Load() {
Assert(mLuaState); Assert(mLuaState);
luaL_openlibs(mLuaState); luaL_openlibs(mLuaState);
lua_register(mLuaState, "TriggerGlobalEvent", lua_TriggerEventG); lua_register(mLuaState, "TriggerGlobalEvent", lua_TriggerEventG);

View File

@ -29,7 +29,7 @@ void TPPSMonitor::operator()() {
Application::SetPPS("-"); Application::SetPPS("-");
continue; continue;
} }
mServer.ForEachClient([&](std::weak_ptr<TClient> ClientPtr) -> bool { mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
if (!ClientPtr.expired()) { if (!ClientPtr.expired()) {
auto c = ClientPtr.lock(); auto c = ClientPtr.lock();
if (c->GetCarCount() > 0) { if (c->GetCarCount() > 0) {

View File

@ -18,8 +18,8 @@ TServer::TServer(int argc, char** argv) {
if (argc > 1) { if (argc > 1) {
Application::Settings.CustomIP = argv[1]; Application::Settings.CustomIP = argv[1];
size_t n = std::count(Application::Settings.CustomIP.begin(), Application::Settings.CustomIP.end(), '.'); size_t n = std::count(Application::Settings.CustomIP.begin(), Application::Settings.CustomIP.end(), '.');
auto p = Application::Settings.CustomIP.find_first_not_of((".0123456789")); auto p = Application::Settings.CustomIP.find_first_not_of(".0123456789");
if (p != std::string::npos || n != 3 || Application::Settings.CustomIP.substr(0, 3) == ("127")) { if (p != std::string::npos || n != 3 || Application::Settings.CustomIP.substr(0, 3) == "127") {
Application::Settings.CustomIP.clear(); Application::Settings.CustomIP.clear();
warn("IP Specified is invalid! Ignoring"); warn("IP Specified is invalid! Ignoring");
} else { } else {
@ -28,7 +28,7 @@ TServer::TServer(int argc, char** argv) {
} }
} }
void TServer::RemoveClient(std::weak_ptr<TClient> WeakClientPtr) { void TServer::RemoveClient(const std::weak_ptr<TClient>& WeakClientPtr) {
if (!WeakClientPtr.expired()) { if (!WeakClientPtr.expired()) {
TClient& Client = *WeakClientPtr.lock(); TClient& Client = *WeakClientPtr.lock();
debug("removing client " + Client.GetName() + " (" + std::to_string(ClientCount()) + ")"); debug("removing client " + Client.GetName() + " (" + std::to_string(ClientCount()) + ")");
@ -59,7 +59,7 @@ size_t TServer::ClientCount() const {
return mClients.size(); return mClients.size();
} }
void TServer::GlobalParser(std::weak_ptr<TClient> Client, std::string Packet, TPPSMonitor& PPSMonitor, TUDPServer& UDPServer, TTCPServer& TCPServer) { void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::string Packet, TPPSMonitor& PPSMonitor, TUDPServer& UDPServer, TTCPServer& TCPServer) {
if (Packet.find("Zp") != std::string::npos && Packet.size() > 500) { if (Packet.find("Zp") != std::string::npos && Packet.size() > 500) {
abort(); abort();
} }
@ -271,7 +271,7 @@ void TServer::Apply(TClient& c, int VID, const std::string& pckt) {
c.SetCarData(VID, Header + Buffer.GetString()); c.SetCarData(VID, Header + Buffer.GetString());
} }
void TServer::InsertClient(std::shared_ptr<TClient> NewClient) { void TServer::InsertClient(const std::shared_ptr<TClient>& NewClient) {
debug("inserting client (" + std::to_string(ClientCount()) + ")"); debug("inserting client (" + std::to_string(ClientCount()) + ")");
WriteLock Lock(mClientsMutex); WriteLock Lock(mClientsMutex);
(void)mClients.insert(NewClient); (void)mClients.insert(NewClient);

View File

@ -113,7 +113,7 @@ void TTCPServer::Authentication(SOCKET TCPSock) {
debug("Name -> " + Client->GetName() + ", Guest -> " + std::to_string(Client->IsGuest()) + ", Roles -> " + Client->GetRoles()); debug("Name -> " + Client->GetName() + ", Guest -> " + std::to_string(Client->IsGuest()) + ", Roles -> " + Client->GetRoles());
debug("There are " + std::to_string(mServer.ClientCount()) + " known clients"); debug("There are " + std::to_string(mServer.ClientCount()) + " known clients");
mServer.ForEachClient([&](std::weak_ptr<TClient> ClientPtr) -> bool { mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
if (!ClientPtr.expired()) { if (!ClientPtr.expired()) {
auto Cl = ClientPtr.lock(); auto Cl = ClientPtr.lock();
info("Client Iteration: Name -> " + Client->GetName() + ", Guest -> " + std::to_string(Client->IsGuest()) + ", Roles -> " + Client->GetRoles()); info("Client Iteration: Name -> " + Client->GetName() + ", Guest -> " + std::to_string(Client->IsGuest()) + ", Roles -> " + Client->GetRoles());

View File

@ -6,7 +6,6 @@
#include "TResourceManager.h" #include "TResourceManager.h"
#include "TServer.h" #include "TServer.h"
#include "TUDPServer.h" #include "TUDPServer.h"
#include <iostream>
#include <thread> #include <thread>
#include <TTCPServer.h> #include <TTCPServer.h>