Skip to content

Commit f0141e4

Browse files
authored
Wait for lua and other systems (#421)
This PR makes it so that connections are denied if lua hasn't loaded yet, and makes it so that lua waits for the server to load before accessing then uninitialized memory. --- By creating this pull request, I understand that code that is AI generated or otherwise automatically generated may be rejected without further discussion. I declare that I fully understand all code I pushed into this PR, and wrote all this code myself and own the rights to this code.
2 parents 27d50fc + 00560f7 commit f0141e4

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/TLuaEngine.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ TEST_CASE("TLuaEngine ctor & dtor") {
8080

8181
void TLuaEngine::operator()() {
8282
RegisterThread("LuaEngine");
83-
Application::SetSubsystemStatus("LuaEngine", Application::Status::Good);
8483
// lua engine main thread
8584
beammp_infof("Lua v{}.{}.{}", LUA_VERSION_MAJOR, LUA_VERSION_MINOR, LUA_VERSION_RELEASE);
8685
CollectAndInitPlugins();
86+
87+
Application::SetSubsystemStatus("LuaEngine", Application::Status::Good);
8788
// now call all onInit's
8889
auto Futures = TriggerEvent("onInit", "");
8990
WaitForAll(Futures, std::chrono::seconds(5));

src/TNetwork.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ std::shared_ptr<TClient> TNetwork::Authentication(TConnection&& RawConnection) {
307307
Client->SetIdentifier("ip", ip);
308308
beammp_tracef("This thread is ip {} ({})", ip, RawConnection.SockAddr.address().to_v6().is_v4_mapped() ? "IPv4 mapped IPv6" : "IPv6");
309309

310+
if (Application::GetSubsystemStatuses().at("Main") == Application::Status::Starting) {
311+
ClientKick(*Client, "The server is still starting, please try joining again later.");
312+
return nullptr;
313+
}
314+
310315
beammp_info("Identifying new ClientConnection...");
311316

312317
auto Data = TCPRcv(*Client);

src/main.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,6 @@ int BeamMPServerMain(MainArguments Arguments) {
182182

183183
TServer Server(Arguments.List);
184184

185-
auto LuaEngine = std::make_shared<TLuaEngine>();
186-
LuaEngine->SetServer(&Server);
187-
Application::Console().InitializeLuaConsole(*LuaEngine);
188-
189185
RegisterThread("Main");
190186

191187
beammp_trace("Running in debug mode on a debug build");
@@ -194,13 +190,16 @@ int BeamMPServerMain(MainArguments Arguments) {
194190
TPPSMonitor PPSMonitor(Server);
195191
THeartbeatThread Heartbeat(ResourceManager, Server);
196192
TNetwork Network(Server, PPSMonitor, ResourceManager);
193+
194+
auto LuaEngine = std::make_shared<TLuaEngine>();
195+
LuaEngine->SetServer(&Server);
196+
Application::Console().InitializeLuaConsole(*LuaEngine);
197197
LuaEngine->SetNetwork(&Network);
198198
PPSMonitor.SetNetwork(Network);
199199
Application::CheckForUpdates();
200200

201201
TPluginMonitor PluginMonitor(fs::path(Application::Settings.getAsString(Settings::Key::General_ResourceFolder)) / "Server", LuaEngine);
202202

203-
Application::SetSubsystemStatus("Main", Application::Status::Good);
204203
RegisterThread("Main(Waiting)");
205204

206205
std::set<std::string> IgnoreSubsystems {
@@ -215,6 +214,10 @@ int BeamMPServerMain(MainArguments Arguments) {
215214
std::string SystemsBadList {};
216215
auto Statuses = Application::GetSubsystemStatuses();
217216
for (const auto& NameStatusPair : Statuses) {
217+
if (NameStatusPair.first == "Main") {
218+
continue;
219+
}
220+
218221
if (IgnoreSubsystems.count(NameStatusPair.first) > 0) {
219222
continue; // ignore
220223
}
@@ -228,6 +231,8 @@ int BeamMPServerMain(MainArguments Arguments) {
228231
// remove ", "
229232
SystemsBadList = SystemsBadList.substr(0, SystemsBadList.size() - 2);
230233
if (FullyStarted) {
234+
Application::SetSubsystemStatus("Main", Application::Status::Good);
235+
231236
if (!WithErrors) {
232237
beammp_info("ALL SYSTEMS STARTED SUCCESSFULLY, EVERYTHING IS OKAY");
233238
} else {

0 commit comments

Comments
 (0)