Skip to content

Commit f27cf47

Browse files
Desourceleron55
authored andcommitted
Properly handle mod-errors in on_shutdown
1 parent 3e5bce2 commit f27cf47

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/client/game.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,8 @@ bool Game::createSingleplayerServer(const std::string &map_dir,
13001300
return false;
13011301
}
13021302

1303-
server = new Server(map_dir, gamespec, simple_singleplayer_mode, bind_addr, false);
1303+
server = new Server(map_dir, gamespec, simple_singleplayer_mode, bind_addr,
1304+
false, nullptr, error_message);
13041305
server->start();
13051306

13061307
return true;

src/server.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ Server::Server(
213213
bool simple_singleplayer_mode,
214214
Address bind_addr,
215215
bool dedicated,
216-
ChatInterface *iface
216+
ChatInterface *iface,
217+
std::string *on_shutdown_errmsg
217218
):
218219
m_bind_addr(bind_addr),
219220
m_path_world(path_world),
@@ -232,6 +233,7 @@ Server::Server(
232233
m_thread(new ServerThread(this)),
233234
m_clients(m_con),
234235
m_admin_chat(iface),
236+
m_on_shutdown_errmsg(on_shutdown_errmsg),
235237
m_modchannel_mgr(new ModChannelMgr())
236238
{
237239
if (m_path_world.empty())
@@ -314,7 +316,18 @@ Server::~Server()
314316

315317
// Execute script shutdown hooks
316318
infostream << "Executing shutdown hooks" << std::endl;
317-
m_script->on_shutdown();
319+
try {
320+
m_script->on_shutdown();
321+
} catch (ModError &e) {
322+
errorstream << "ModError: " << e.what() << std::endl;
323+
if (m_on_shutdown_errmsg) {
324+
if (m_on_shutdown_errmsg->empty()) {
325+
*m_on_shutdown_errmsg = std::string("ModError: ") + e.what();
326+
} else {
327+
*m_on_shutdown_errmsg += std::string("\nModError: ") + e.what();
328+
}
329+
}
330+
}
318331

319332
infostream << "Server: Saving environment metadata" << std::endl;
320333
m_env->saveMeta();

src/server.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ class Server : public con::PeerHandler, public MapEventReceiver,
131131
bool simple_singleplayer_mode,
132132
Address bind_addr,
133133
bool dedicated,
134-
ChatInterface *iface = nullptr
134+
ChatInterface *iface = nullptr,
135+
std::string *on_shutdown_errmsg = nullptr
135136
);
136137
~Server();
137138
DISABLE_CLASS_COPY(Server);
@@ -596,6 +597,10 @@ class Server : public con::PeerHandler, public MapEventReceiver,
596597
ChatInterface *m_admin_chat;
597598
std::string m_admin_nick;
598599

600+
// if a mod-error occurs in the on_shutdown callback, the error message will
601+
// be written into this
602+
std::string *const m_on_shutdown_errmsg;
603+
599604
/*
600605
Map edit event queue. Automatically receives all map edits.
601606
The constructor of this class registers us to receive them through

0 commit comments

Comments
 (0)