Skip to content

Commit 2e37ee9

Browse files
authoredNov 24, 2018
CSM: Don't create the client script environment if CSM is disabled (#7874)
Use the CSM death formspec when CSM is enabled and use the engine death formspec when CSM is disabled. Move the CSM death formspec code to a dedicated file.
1 parent a969635 commit 2e37ee9

File tree

7 files changed

+87
-51
lines changed

7 files changed

+87
-51
lines changed
 

Diff for: ‎builtin/client/death_formspec.lua

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- CSM death formspec. Only used when clientside modding is enabled, otherwise
2+
-- handled by the engine.
3+
4+
core.register_on_death(function()
5+
core.display_chat_message("You died.")
6+
local formspec = "size[11,5.5]bgcolor[#320000b4;true]" ..
7+
"label[4.85,1.35;" .. fgettext("You died") ..
8+
"]button_exit[4,3;3,0.5;btn_respawn;".. fgettext("Respawn") .."]"
9+
core.show_formspec("bultin:death", formspec)
10+
end)
11+
12+
core.register_on_formspec_input(function(formname, fields)
13+
if formname == "bultin:death" then
14+
core.send_respawn()
15+
end
16+
end)

Diff for: ‎builtin/client/init.lua

+1-13
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,4 @@ dofile(commonpath .. "after.lua")
88
dofile(commonpath .. "chatcommands.lua")
99
dofile(clientpath .. "chatcommands.lua")
1010
dofile(commonpath .. "vector.lua")
11-
12-
core.register_on_death(function()
13-
core.display_chat_message("You died.")
14-
local formspec = "size[11,5.5]bgcolor[#320000b4;true]" ..
15-
"label[4.85,1.35;" .. fgettext("You died.") .. "]button_exit[4,3;3,0.5;btn_respawn;".. fgettext("Respawn") .."]"
16-
core.show_formspec("bultin:death", formspec)
17-
end)
18-
19-
core.register_on_formspec_input(function(formname, fields)
20-
if formname == "bultin:death" then
21-
core.send_respawn()
22-
end
23-
end)
11+
dofile(clientpath .. "death_formspec.lua")

Diff for: ‎src/client.cpp

+22-21
Original file line numberDiff line numberDiff line change
@@ -110,36 +110,37 @@ Client::Client(
110110
m_cache_save_interval = g_settings->getU16("server_map_save_interval");
111111

112112
m_modding_enabled = g_settings->getBool("enable_client_modding");
113-
m_script = new ClientScripting(this);
114-
m_env.setScript(m_script);
115-
m_script->setEnv(&m_env);
116-
}
117-
118-
void Client::loadBuiltin()
119-
{
120-
// Load builtin
121-
scanModIntoMemory(BUILTIN_MOD_NAME, getBuiltinLuaPath());
122-
123-
m_script->loadModFromMemory(BUILTIN_MOD_NAME);
113+
// Only create the client script environment if client modding is enabled
114+
if (m_modding_enabled) {
115+
m_script = new ClientScripting(this);
116+
m_env.setScript(m_script);
117+
m_script->setEnv(&m_env);
118+
}
124119
}
125120

126121
void Client::loadMods()
127122
{
128-
// Don't permit to load mods twice
123+
// Don't load mods twice
129124
if (m_mods_loaded) {
130125
return;
131126
}
132127

133-
// If modding is not enabled or CSM restrictions disable it
134-
// don't load CSM mods, only builtin
128+
// If client modding is not enabled, don't load client-provided CSM mods or
129+
// builtin.
135130
if (!m_modding_enabled) {
136131
warningstream << "Client side mods are disabled by configuration." << std::endl;
137132
return;
138133
}
139134

135+
// Load builtin
136+
scanModIntoMemory(BUILTIN_MOD_NAME, getBuiltinLuaPath());
137+
m_script->loadModFromMemory(BUILTIN_MOD_NAME);
138+
139+
// If the server has disabled client-provided CSM mod loading, don't load
140+
// client-provided CSM mods. Builtin is loaded so needs verfying.
140141
if (checkCSMRestrictionFlag(CSMRestrictionFlags::CSM_RF_LOAD_CLIENT_MODS)) {
141142
warningstream << "Client side mods are disabled by server." << std::endl;
142-
// If mods loading is disabled and builtin integrity is wrong, disconnect user.
143+
// If builtin integrity is wrong, disconnect user
143144
if (!checkBuiltinIntegrity()) {
144145
// @TODO disconnect user
145146
}
@@ -227,8 +228,8 @@ const ModSpec* Client::getModSpec(const std::string &modname) const
227228
void Client::Stop()
228229
{
229230
m_shutdown = true;
230-
// Don't disable this part when modding is disabled, it's used in builtin
231-
m_script->on_shutdown();
231+
if (m_modding_enabled)
232+
m_script->on_shutdown();
232233
//request all client managed threads to stop
233234
m_mesh_update_thread.stop();
234235
// Save local server map
@@ -237,7 +238,8 @@ void Client::Stop()
237238
m_localdb->endSave();
238239
}
239240

240-
delete m_script;
241+
if (m_modding_enabled)
242+
delete m_script;
241243
}
242244

243245
bool Client::isShutdown()
@@ -1491,10 +1493,9 @@ void Client::typeChatMessage(const std::wstring &message)
14911493
if (message.empty())
14921494
return;
14931495

1494-
// If message was ate by script API, don't send it to server
1495-
if (m_script->on_sending_message(wide_to_utf8(message))) {
1496+
// If message was consumed by script API, don't send it to server
1497+
if (m_modding_enabled && m_script->on_sending_message(wide_to_utf8(message)))
14961498
return;
1497-
}
14981499

14991500
// Send to others
15001501
sendChatMessage(message);

Diff for: ‎src/client.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
141141
DISABLE_CLASS_COPY(Client);
142142

143143
// Load local mods into memory
144-
void loadBuiltin();
145144
void scanModSubfolder(const std::string &mod_name, const std::string &mod_path,
146145
std::string mod_subpath);
147146
inline void scanModIntoMemory(const std::string &mod_name, const std::string &mod_path)
@@ -400,6 +399,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
400399

401400
ClientScripting *getScript() { return m_script; }
402401
const bool moddingEnabled() const { return m_modding_enabled; }
402+
const bool modsLoaded() const { return m_mods_loaded; }
403403

404404
void pushToEventQueue(ClientEvent *event);
405405

Diff for: ‎src/clientenvironment.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,14 @@ void ClientEnvironment::step(float dtime)
233233
u8 damage = (u8)MYMIN(damage_f + 0.5, 255);
234234
if (damage != 0) {
235235
damageLocalPlayer(damage, true);
236-
m_client->getEventManager()->put(new SimpleTriggerEvent(MtEvent::PLAYER_FALLING_DAMAGE));
236+
m_client->getEventManager()->put(
237+
new SimpleTriggerEvent(MtEvent::PLAYER_FALLING_DAMAGE));
237238
}
238239
}
239240
}
240241

241-
if (m_client->moddingEnabled()) {
242+
if (m_client->modsLoaded())
242243
m_script->environment_step(dtime);
243-
}
244244

245245
// Update lighting on local player (used for wield item)
246246
u32 day_night_ratio = getDayNightRatio();

Diff for: ‎src/game.cpp

+40-9
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,13 @@ struct LocalFormspecHandler : public TextDest
177177
}
178178
}
179179

180-
// Don't disable this part when modding is disabled, it's used in builtin
181-
if (m_client && m_client->getScript())
180+
if (m_formname == "MT_DEATH_SCREEN") {
181+
assert(m_client != 0);
182+
m_client->sendRespawn();
183+
return;
184+
}
185+
186+
if (m_client && m_client->moddingEnabled())
182187
m_client->getScript()->on_formspec_input(m_formname, fields);
183188
}
184189

@@ -775,6 +780,7 @@ class Game {
775780
bool disable_camera_update = false;
776781
};
777782

783+
void showDeathFormspec();
778784
void showPauseMenu();
779785

780786
// ClientEvent handlers
@@ -1496,8 +1502,6 @@ bool Game::connectToServer(const std::string &playername,
14961502

14971503
fps_control.last_time = RenderingEngine::get_timer_time();
14981504

1499-
client->loadBuiltin();
1500-
15011505
while (RenderingEngine::run()) {
15021506

15031507
limitFps(&fps_control, &dtime);
@@ -1882,7 +1886,10 @@ void Game::processKeyInput()
18821886
} else if (wasKeyDown(KeyType::CMD)) {
18831887
openConsole(0.2, L"/");
18841888
} else if (wasKeyDown(KeyType::CMD_LOCAL)) {
1885-
openConsole(0.2, L".");
1889+
if (client->moddingEnabled())
1890+
openConsole(0.2, L".");
1891+
else
1892+
m_game_ui->showStatusText(wgettext("CSM is disabled"));
18861893
} else if (wasKeyDown(KeyType::CONSOLE)) {
18871894
openConsole(core::clamp(g_settings->getFloat("console_height"), 0.1f, 1.0f));
18881895
} else if (wasKeyDown(KeyType::FREEMOVE)) {
@@ -2532,12 +2539,15 @@ void Game::handleClientEvent_PlayerForceMove(ClientEvent *event, CameraOrientati
25322539

25332540
void Game::handleClientEvent_Deathscreen(ClientEvent *event, CameraOrientation *cam)
25342541
{
2535-
// This should be enabled for death formspec in builtin
2536-
client->getScript()->on_death();
2537-
2538-
LocalPlayer *player = client->getEnv().getLocalPlayer();
2542+
// If CSM enabled, deathscreen is handled by CSM code in
2543+
// builtin/client/init.lua
2544+
if (client->moddingEnabled())
2545+
client->getScript()->on_death();
2546+
else
2547+
showDeathFormspec();
25392548

25402549
/* Handle visualization */
2550+
LocalPlayer *player = client->getEnv().getLocalPlayer();
25412551
runData.damage_flash = 0;
25422552
player->hurt_tilt_timer = 0;
25432553
player->hurt_tilt_strength = 0;
@@ -4006,6 +4016,27 @@ void Game::extendedResourceCleanup()
40064016
<< " (note: irrlicht doesn't support removing renderers)" << std::endl;
40074017
}
40084018

4019+
void Game::showDeathFormspec()
4020+
{
4021+
static std::string formspec =
4022+
std::string(FORMSPEC_VERSION_STRING) +
4023+
SIZE_TAG
4024+
"bgcolor[#320000b4;true]"
4025+
"label[4.85,1.35;" + gettext("You died") + "]"
4026+
"button_exit[4,3;3,0.5;btn_respawn;" + gettext("Respawn") + "]"
4027+
;
4028+
4029+
/* Create menu */
4030+
/* Note: FormspecFormSource and LocalFormspecHandler *
4031+
* are deleted by guiFormSpecMenu */
4032+
FormspecFormSource *fs_src = new FormspecFormSource(formspec);
4033+
LocalFormspecHandler *txt_dst = new LocalFormspecHandler("MT_DEATH_SCREEN", client);
4034+
4035+
GUIFormSpecMenu::create(current_formspec, client, &input->joystick, fs_src,
4036+
txt_dst, client->getFormspecPrepend());
4037+
current_formspec->setFocus("btn_respawn");
4038+
}
4039+
40094040
#define GET_KEY_NAME(KEY) gettext(getKeySetting(#KEY).name())
40104041
void Game::showPauseMenu()
40114042
{

Diff for: ‎src/network/clientpackethandler.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,12 @@ void Client::handleCommand_ChatMessage(NetworkPacket *pkt)
384384
chatMessage->type = (ChatMessageType) message_type;
385385

386386
// @TODO send this to CSM using ChatMessage object
387-
if (!moddingEnabled() || !m_script->on_receiving_message(
387+
if (moddingEnabled() && m_script->on_receiving_message(
388388
wide_to_utf8(chatMessage->message))) {
389-
pushToChatQueue(chatMessage);
390-
} else {
391-
// Message was consumed by CSM and should not handled by client, destroying
389+
// Message was consumed by CSM and should not be handled by client
392390
delete chatMessage;
391+
} else {
392+
pushToChatQueue(chatMessage);
393393
}
394394
}
395395

0 commit comments

Comments
 (0)
Please sign in to comment.