]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/server.cpp
Don't print an error for valid listring[] formspec
[dragonfireclient.git] / src / server.cpp
index 4e955e40a410b8044959cdd73ef0021aee37d069..7cd92ed65f5c9db61f33c958537976670dc300a6 100644 (file)
@@ -27,7 +27,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "environment.h"
 #include "map.h"
 #include "jthread/jmutexautolock.h"
-#include "main.h"
 #include "constants.h"
 #include "voxel.h"
 #include "config.h"
@@ -223,7 +222,7 @@ Server::Server(
        infostream<<"- game:   "<<m_gamespec.path<<std::endl;
 
        // Create world if it doesn't exist
-       if(!initializeWorld(m_path_world, m_gamespec.id))
+       if(!loadGameConfAndInitWorld(m_path_world, m_gamespec))
                throw ServerError("Failed to initialize world");
 
        // Create server thread
@@ -296,31 +295,37 @@ Server::Server(
 
        m_script = new GameScripting(this);
 
-       std::string scriptpath = getBuiltinLuaPath() + DIR_DELIM "init.lua";
+       std::string script_path = getBuiltinLuaPath() + DIR_DELIM "init.lua";
 
-       if (!m_script->loadScript(scriptpath))
-               throw ModError("Failed to load and run " + scriptpath);
+       if (!m_script->loadMod(script_path, BUILTIN_MOD_NAME)) {
+               throw ModError("Failed to load and run " + script_path);
+       }
 
-       // Print 'em
-       infostream<<"Server: Loading mods: ";
+       // Print mods
+       infostream << "Server: Loading mods: ";
        for(std::vector<ModSpec>::iterator i = m_mods.begin();
                        i != m_mods.end(); i++){
                const ModSpec &mod = *i;
-               infostream<<mod.name<<" ";
+               infostream << mod.name << " ";
        }
-       infostream<<std::endl;
+       infostream << std::endl;
        // Load and run "mod" scripts
-       for(std::vector<ModSpec>::iterator i = m_mods.begin();
-                       i != m_mods.end(); i++){
+       for (std::vector<ModSpec>::iterator i = m_mods.begin();
+                       i != m_mods.end(); i++) {
                const ModSpec &mod = *i;
-               std::string scriptpath = mod.path + DIR_DELIM + "init.lua";
-               infostream<<"  ["<<padStringRight(mod.name, 12)<<"] [\""
-                               <<scriptpath<<"\"]"<<std::endl;
-               bool success = m_script->loadMod(scriptpath, mod.name);
-               if(!success){
-                       errorstream<<"Server: Failed to load and run "
-                                       <<scriptpath<<std::endl;
-                       throw ModError("Failed to load and run "+scriptpath);
+               if (!string_allowed(mod.name, MODNAME_ALLOWED_CHARS)) {
+                       errorstream << "Error loading mod \"" << mod.name
+                                       << "\": mod_name does not follow naming conventions: "
+                                       << "Only chararacters [a-z0-9_] are allowed." << std::endl;
+                       throw ModError("Mod \"" + mod.name + "\" does not follow naming conventions.");
+               }
+               std::string script_path = mod.path + DIR_DELIM "init.lua";
+               infostream << "  [" << padStringRight(mod.name, 12) << "] [\""
+                               << script_path << "\"]" << std::endl;
+               if (!m_script->loadMod(script_path, mod.name)) {
+                       errorstream << "Server: Failed to load and run "
+                                       << script_path << std::endl;
+                       throw ModError("Failed to load and run " + script_path);
                }
        }
 
@@ -330,10 +335,18 @@ Server::Server(
        // Apply item aliases in the node definition manager
        m_nodedef->updateAliases(m_itemdef);
 
+       // Apply texture overrides from texturepack/override.txt
+       std::string texture_path = g_settings->get("texture_path");
+       if (texture_path != "" && fs::IsDir(texture_path))
+               m_nodedef->applyTextureOverrides(texture_path + DIR_DELIM + "override.txt");
+
        m_nodedef->setNodeRegistrationStatus(true);
 
        // Perform pending node name resolutions
-       m_nodedef->runNodeResolverCallbacks();
+       m_nodedef->runNodeResolveCallbacks();
+
+       // init the recipe hashes to speed up crafting
+       m_craftdef->initHashes(this);
 
        // Initialize Environment
        m_env = new ServerEnvironment(servermap, m_script, this, m_path_world);
@@ -479,13 +492,18 @@ void Server::step(float dtime)
                JMutexAutoLock lock(m_step_dtime_mutex);
                m_step_dtime += dtime;
        }
-       // Assert if fatal error occurred in thread
+       // Throw if fatal error occurred in thread
        std::string async_err = m_async_fatal_error.get();
        if(async_err != "") {
-               errorstream << "UNRECOVERABLE error occurred. Stopping server. "
-                               << "Please fix the following error:" << std::endl
-                               << async_err << std::endl;
-               assert(false);
+               if (m_simple_singleplayer_mode) {
+                       throw ServerError(async_err);
+               }
+               else {
+                       errorstream << "UNRECOVERABLE error occurred. Stopping server. "
+                                       << "Please fix the following error:" << std::endl
+                                       << async_err << std::endl;
+                       FATAL_ERROR(async_err.c_str());
+               }
        }
 }
 
@@ -848,7 +866,7 @@ void Server::AsyncRunStep(bool initial_step)
                        }
 
                        if(unreliable_data.size() > 0) {
-                               SendActiveObjectMessages(client->peer_id, unreliable_data);
+                               SendActiveObjectMessages(client->peer_id, unreliable_data, false);
                        }
                }
                m_clients.Unlock();
@@ -1013,10 +1031,11 @@ void Server::Receive()
        DSTACK(__FUNCTION_NAME);
        SharedBuffer<u8> data;
        u16 peer_id;
-       u32 datasize;
        try {
-               datasize = m_con.Receive(peer_id,data);
-               ProcessData(*data, datasize, peer_id);
+               NetworkPacket pkt;
+               m_con.Receive(&pkt);
+               peer_id = pkt.getPeerId();
+               ProcessData(&pkt);
        }
        catch(con::InvalidIncomingDataException &e) {
                infostream<<"Server::Receive(): "
@@ -1059,16 +1078,16 @@ PlayerSAO* Server::StageTwoClientInit(u16 peer_id)
                static_cast<RemotePlayer*>(m_env->getPlayer(playername.c_str()));
 
        // If failed, cancel
-       if((playersao == NULL) || (player == NULL)) {
-               if(player && player->peer_id != 0) {
-                       errorstream<<"Server: "<<playername<<": Failed to emerge player"
-                                       <<" (player allocated to an another client)"<<std::endl;
+       if ((playersao == NULL) || (player == NULL)) {
+               if (player && player->peer_id != 0) {
+                       actionstream << "Server: Failed to emerge player \"" << playername
+                                       << "\" (player allocated to an another client)" << std::endl;
                        DenyAccess_Legacy(peer_id, L"Another client is connected with this "
                                        L"name. If your client closed unexpectedly, try again in "
                                        L"a minute.");
                } else {
-                       errorstream<<"Server: "<<playername<<": Failed to emerge player"
-                                       <<std::endl;
+                       errorstream << "Server: " << playername << ": Failed to emerge player"
+                                       << std::endl;
                        DenyAccess_Legacy(peer_id, L"Could not allocate player.");
                }
                return NULL;
@@ -1144,13 +1163,14 @@ inline void Server::handleCommand(NetworkPacket* pkt)
        (this->*opHandle.handler)(pkt);
 }
 
-void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
+void Server::ProcessData(NetworkPacket *pkt)
 {
        DSTACK(__FUNCTION_NAME);
        // Environment is locked first.
        JMutexAutoLock envlock(m_env_mutex);
 
        ScopeProfiler sp(g_profiler, "Server::ProcessData");
+       u32 peer_id = pkt->getPeerId();
 
        try {
                Address address = getPeerAddress(peer_id);
@@ -1174,18 +1194,13 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                 * respond for some time, your server was overloaded or
                 * things like that.
                 */
-               infostream << "Server::ProcessData(): Cancelling: peer "
+               infostream << "Server::ProcessData(): Canceling: peer "
                                << peer_id << " not found" << std::endl;
                return;
        }
 
        try {
-               if(datasize < 2)
-                       return;
-
-               NetworkPacket pkt(data, datasize, peer_id);
-
-               ToServerCommand command = (ToServerCommand) pkt.getCommand();
+               ToServerCommand command = (ToServerCommand) pkt->getCommand();
 
                // Command must be handled into ToServerCommandHandler
                if (command >= TOSERVER_NUM_MSG_TYPES) {
@@ -1194,7 +1209,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                }
 
                if (toServerCommandTable[command].state == TOSERVER_STATE_NOT_CONNECTED) {
-                       handleCommand(&pkt);
+                       handleCommand(pkt);
                        return;
                }
 
@@ -1209,7 +1224,7 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
 
                /* Handle commands related to client startup */
                if (toServerCommandTable[command].state == TOSERVER_STATE_STARTUP) {
-                       handleCommand(&pkt);
+                       handleCommand(pkt);
                        return;
                }
 
@@ -1222,12 +1237,15 @@ void Server::ProcessData(u8 *data, u32 datasize, u16 peer_id)
                        return;
                }
 
-               handleCommand(&pkt);
-       }
-       catch(SendFailedException &e) {
+               handleCommand(pkt);
+       } catch (SendFailedException &e) {
                errorstream << "Server::ProcessData(): SendFailedException: "
                                << "what=" << e.what()
                                << std::endl;
+       } catch (PacketError &e) {
+               actionstream << "Server::ProcessData(): PacketError: "
+                               << "what=" << e.what()
+                               << std::endl;
        }
 }
 
@@ -1285,13 +1303,16 @@ Inventory* Server::getInventory(const InventoryLocation &loc)
        }
        return NULL;
 }
-void Server::setInventoryModified(const InventoryLocation &loc)
+void Server::setInventoryModified(const InventoryLocation &loc, bool playerSend)
 {
        switch(loc.type){
        case InventoryLocation::UNDEFINED:
                break;
        case InventoryLocation::PLAYER:
        {
+               if (!playerSend)
+                       return;
+
                Player *player = m_env->getPlayer(loc.name.c_str());
                if(!player)
                        return;
@@ -1483,7 +1504,7 @@ void Server::SendBreath(u16 peer_id, u16 breath)
        Send(&pkt);
 }
 
-void Server::SendAccessDenied(u16 peer_id, AccessDeniedCode reason, const std::wstring &custom_reason)
+void Server::SendAccessDenied(u16 peer_id, AccessDeniedCode reason, const std::string &custom_reason)
 {
        DSTACK(__FUNCTION_NAME);
 
@@ -1795,7 +1816,11 @@ void Server::SendPlayerHP(u16 peer_id)
 {
        DSTACK(__FUNCTION_NAME);
        PlayerSAO *playersao = getPlayerSAO(peer_id);
-       assert(playersao);
+       // In some rare case, if the player is disconnected
+       // while Lua call l_punch, for example, this can be NULL
+       if (!playersao)
+               return;
+
        SendHP(peer_id, playersao->getHP());
        m_script->player_event(playersao,"health_changed");
 
@@ -1890,19 +1915,23 @@ void Server::SendPlayerInventoryFormspec(u16 peer_id)
 
 u32 Server::SendActiveObjectRemoveAdd(u16 peer_id, const std::string &datas)
 {
-       NetworkPacket pkt(TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD, 0, peer_id);
+       NetworkPacket pkt(TOCLIENT_ACTIVE_OBJECT_REMOVE_ADD, datas.size(), peer_id);
        pkt.putRawString(datas.c_str(), datas.size());
        Send(&pkt);
        return pkt.getSize();
 }
 
-void Server::SendActiveObjectMessages(u16 peer_id, const std::string &datas)
+void Server::SendActiveObjectMessages(u16 peer_id, const std::string &datas, bool reliable)
 {
        NetworkPacket pkt(TOCLIENT_ACTIVE_OBJECT_MESSAGES,
-                       0, peer_id);
+                       datas.size(), peer_id);
 
        pkt.putRawString(datas.c_str(), datas.size());
-       Send(&pkt);
+
+       m_clients.send(pkt.getPeerId(),
+                       reliable ? clientCommandFactoryTable[pkt.getCommand()].channel : 1,
+                       &pkt, reliable);
+
 }
 
 s32 Server::playSound(const SimpleSoundSpec &spec,
@@ -2514,13 +2543,20 @@ void Server::RespawnPlayer(u16 peer_id)
 
        bool repositioned = m_script->on_respawnplayer(playersao);
        if(!repositioned){
-               v3f pos = findSpawnPos(m_env->getServerMap());
+               v3f pos = findSpawnPos();
                // setPos will send the new position to client
                playersao->setPos(pos);
        }
 }
+void Server::DenySudoAccess(u16 peer_id)
+{
+       DSTACK(__FUNCTION_NAME);
+
+       NetworkPacket pkt(TOCLIENT_DENY_SUDO_MODE, 0, peer_id);
+       Send(&pkt);
+}
 
-void Server::DenyAccess(u16 peer_id, AccessDeniedCode reason, const std::wstring &custom_reason)
+void Server::DenyAccess(u16 peer_id, AccessDeniedCode reason, const std::string &custom_reason)
 {
        DSTACK(__FUNCTION_NAME);
 
@@ -2540,6 +2576,37 @@ void Server::DenyAccess_Legacy(u16 peer_id, const std::wstring &reason)
        m_con.DisconnectPeer(peer_id);
 }
 
+void Server::acceptAuth(u16 peer_id, bool forSudoMode)
+{
+       DSTACK(__FUNCTION_NAME);
+
+       if (!forSudoMode) {
+               RemoteClient* client = getClient(peer_id, CS_Invalid);
+
+               NetworkPacket resp_pkt(TOCLIENT_AUTH_ACCEPT, 1 + 6 + 8 + 4, peer_id);
+
+               // Right now, the auth mechs don't change between login and sudo mode.
+               u32 sudo_auth_mechs = client->allowed_auth_mechs;
+               client->allowed_sudo_mechs = sudo_auth_mechs;
+
+               resp_pkt << v3f(0,0,0) << (u64) m_env->getServerMap().getSeed()
+                               << g_settings->getFloat("dedicated_server_step")
+                               << sudo_auth_mechs;
+
+               Send(&resp_pkt);
+               m_clients.event(peer_id, CSE_AuthAccept);
+       } else {
+               NetworkPacket resp_pkt(TOCLIENT_ACCEPT_SUDO_MODE, 1 + 6 + 8 + 4, peer_id);
+
+               // We only support SRP right now
+               u32 sudo_auth_mechs = AUTH_MECHANISM_FIRST_SRP;
+
+               resp_pkt << sudo_auth_mechs;
+               Send(&resp_pkt);
+               m_clients.event(peer_id, CSE_SudoSuccess);
+       }
+}
+
 void Server::DeleteClient(u16 peer_id, ClientDeletionReason reason)
 {
        DSTACK(__FUNCTION_NAME);
@@ -2675,7 +2742,7 @@ std::wstring Server::getStatusString()
        std::wostringstream os(std::ios_base::binary);
        os<<L"# Server: ";
        // Version
-       os<<L"version="<<narrow_to_wide(minetest_version_simple);
+       os<<L"version="<<narrow_to_wide(g_version_string);
        // Uptime
        os<<L", uptime="<<m_uptime.get();
        // Max lag estimate
@@ -2848,31 +2915,54 @@ bool Server::hudSetHotbarItemcount(Player *player, s32 hotbar_itemcount) {
        if (hotbar_itemcount <= 0 || hotbar_itemcount > HUD_HOTBAR_ITEMCOUNT_MAX)
                return false;
 
+       player->setHotbarItemcount(hotbar_itemcount);
        std::ostringstream os(std::ios::binary);
        writeS32(os, hotbar_itemcount);
        SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_ITEMCOUNT, os.str());
        return true;
 }
 
+s32 Server::hudGetHotbarItemcount(Player *player) {
+       if (!player)
+               return 0;
+       return player->getHotbarItemcount();
+}
+
 void Server::hudSetHotbarImage(Player *player, std::string name) {
        if (!player)
                return;
 
+       player->setHotbarImage(name);
        SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_IMAGE, name);
 }
 
+std::string Server::hudGetHotbarImage(Player *player) {
+       if (!player)
+               return "";
+       return player->getHotbarImage();
+}
+
 void Server::hudSetHotbarSelectedImage(Player *player, std::string name) {
        if (!player)
                return;
 
+       player->setHotbarSelectedImage(name);
        SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_SELECTED_IMAGE, name);
 }
 
+std::string Server::hudGetHotbarSelectedImage(Player *player) {
+       if (!player)
+               return "";
+
+       return player->getHotbarSelectedImage();
+}
+
 bool Server::setLocalPlayerAnimations(Player *player, v2s32 animation_frames[4], f32 frame_speed)
 {
        if (!player)
                return false;
 
+       player->setLocalAnimations(animation_frames, frame_speed);
        SendLocalPlayerAnimations(player->peer_id, animation_frames, frame_speed);
        return true;
 }
@@ -2882,6 +2972,8 @@ bool Server::setPlayerEyeOffset(Player *player, v3f first, v3f third)
        if (!player)
                return false;
 
+       player->eye_offset_first = first;
+       player->eye_offset_third = third;
        SendEyeOffset(player->peer_id, first, third);
        return true;
 }
@@ -2892,6 +2984,7 @@ bool Server::setSky(Player *player, const video::SColor &bgcolor,
        if (!player)
                return false;
 
+       player->setSky(bgcolor, type, params);
        SendSetSky(player->peer_id, bgcolor, type, params);
        return true;
 }
@@ -2902,6 +2995,7 @@ bool Server::overrideDayNightRatio(Player *player, bool do_override,
        if (!player)
                return false;
 
+       player->overrideDayNightRatio(do_override, ratio);
        SendOverrideDayNightRatio(player->peer_id, do_override, ratio);
        return true;
 }
@@ -3150,9 +3244,9 @@ IWritableCraftDefManager* Server::getWritableCraftDefManager()
        return m_craftdef;
 }
 
-const ModSpec* Server::getModSpec(const std::string &modname)
+const ModSpec* Server::getModSpec(const std::string &modname) const
 {
-       for(std::vector<ModSpec>::iterator i = m_mods.begin();
+       for(std::vector<ModSpec>::const_iterator i = m_mods.begin();
                        i != m_mods.end(); i++){
                const ModSpec &mod = *i;
                if(mod.name == modname)
@@ -3171,23 +3265,24 @@ std::string Server::getBuiltinLuaPath()
        return porting::path_share + DIR_DELIM + "builtin";
 }
 
-v3f findSpawnPos(ServerMap &map)
+v3f Server::findSpawnPos()
 {
-       //return v3f(50,50,50)*BS;
-
-       v3s16 nodepos;
+       ServerMap &map = m_env->getServerMap();
+       v3f nodeposf;
+       if (g_settings->getV3FNoEx("static_spawnpoint", nodeposf)) {
+               return nodeposf * BS;
+       }
 
-#if 0
-       nodepos = v2s16(0,0);
-       groundheight = 20;
-#endif
+       // Default position is static_spawnpoint
+       // We will return it if we don't found a good place
+       v3s16 nodepos(nodeposf.X, nodeposf.Y, nodeposf.Z);
 
-#if 1
        s16 water_level = map.getWaterLevel();
 
+       bool is_good = false;
+
        // Try to find a good place a few times
-       for(s32 i=0; i<1000; i++)
-       {
+       for(s32 i = 0; i < 1000 && !is_good; i++) {
                s32 range = 1 + i;
                // We're going to try to throw the player to this position
                v2s16 nodepos2d = v2s16(
@@ -3202,7 +3297,7 @@ v3f findSpawnPos(ServerMap &map)
                        continue;
 
                nodepos = v3s16(nodepos2d.X, groundheight, nodepos2d.Y);
-               bool is_good = false;
+
                s32 air_count = 0;
                for (s32 i = 0; i < 10; i++) {
                        v3s16 blockpos = getNodeBlockPos(nodepos);
@@ -3217,13 +3312,7 @@ v3f findSpawnPos(ServerMap &map)
                        }
                        nodepos.Y++;
                }
-               if(is_good){
-                       // Found a good place
-                       //infostream<<"Searched through "<<i<<" places."<<std::endl;
-                       break;
-               }
        }
-#endif
 
        return intToFloat(nodepos, BS);
 }
@@ -3266,7 +3355,7 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id)
                // Set player position
                infostream<<"Server: Finding spawn place for player \""
                                <<name<<"\""<<std::endl;
-               v3f pos = findSpawnPos(m_env->getServerMap());
+               v3f pos = findSpawnPos();
                player->setPosition(pos);
 
                // Make sure the player is saved
@@ -3340,5 +3429,3 @@ void dedicated_server_loop(Server &server, bool &kill)
                }
        }
 }
-
-