Skip to content

Commit 5a68ed4

Browse files
committed
update(async): Execute events on main thread
1 parent 8926832 commit 5a68ed4

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

src/plugins/core/scripting/database.cpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "../scripting.h"
2+
#include "../../../entrypoint.h"
3+
#include "../../../player/PlayerManager.h"
24

35
#include <deque>
46
#include <thread>
@@ -29,6 +31,8 @@ void DatabaseQueryThread()
2931
DatabaseQueryQueue queue = queryQueue.front();
3032

3133
if (!queue.db->IsConnected()) {
34+
PRINT("The following query has been skipped due to the database not being connected.\n");
35+
PRINTF("Query: \"%s\".\n", queue.query.c_str());
3236
queryQueue.pop_front();
3337
continue;
3438
}
@@ -51,31 +55,40 @@ void DatabaseQueryThread()
5155
tbl.push_back(rowTbl);
5256
}
5357

54-
luabridge::LuaRef ref = *(luabridge::LuaRef*)queue.callback;
55-
try
56-
{
57-
std::string error = queue.db->GetError();
58-
if (error == "MySQL server has gone away") {
59-
if (queue.db->Connect())
60-
{
61-
delete callStack;
62-
continue;
58+
std::string error = queue.db->GetError();
59+
if (error == "MySQL server has gone away") {
60+
if (queue.db->Connect())
61+
{
62+
delete callStack;
63+
continue;
64+
}
65+
else
66+
error = queue.db->GetError();
67+
}
68+
69+
luabridge::LuaRef* ref = (luabridge::LuaRef*)queue.callback;
70+
auto ExecuteCallback = [error, ref, state, tbl]() -> void {
71+
try
72+
{
73+
if (ref != nullptr) {
74+
if (ref->isFunction())
75+
ref->operator()(error.size() == 0 ? luabridge::LuaRef(state) : error, tbl);
76+
77+
delete ref;
6378
}
64-
else
65-
error = queue.db->GetError();
6679
}
80+
catch (luabridge::LuaException& e)
81+
{
82+
PRINTF("An error has occured: %s\n", e.what());
83+
}
84+
};
6785

68-
if (ref.isFunction())
69-
ref(error.size() == 0 ? luabridge::LuaRef(state) : error, tbl);
70-
}
71-
catch (luabridge::LuaException& e)
72-
{
73-
PRINTF("An error has occured: %s\n", e.what());
74-
}
86+
if (g_playerManager->GetPlayers() > 0) g_Plugin.NextFrame(ExecuteCallback);
87+
else ExecuteCallback();
7588
}
7689

7790
delete callStack;
78-
delete ((luabridge::LuaRef*)queue.callback);
91+
7992
queryQueue.pop_front();
8093
}
8194
std::this_thread::sleep_for(std::chrono::milliseconds(300));

src/plugins/core/scripting/http.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "../scripting.h"
2+
#include "../../../player/PlayerManager.h"
23

34
#include <thread>
45

@@ -127,12 +128,14 @@ void RunCurlRequest(std::string url, std::string data, std::map<std::string, std
127128
requestUUID
128129
});
129130

130-
g_Plugin.NextFrame([&]() -> void {
131+
auto ExecuteCallback = [&]() -> void {
131132
PluginEvent* event = new PluginEvent("core", nullptr, nullptr);
132133
g_pluginManager->ExecuteEvent("core", "OnHTTPActionPerformed", eventPayload, event);
133134
delete event;
134-
});
135+
};
135136

137+
if (g_playerManager->GetPlayers() > 0) g_Plugin.NextFrame(ExecuteCallback);
138+
else ExecuteCallback();
136139

137140
if (headersList)
138141
curl_slist_free_all(headersList);

0 commit comments

Comments
 (0)