1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-22 17:18:39 +00:00

General code refactoring/improvements in server, treegen and connection

This commit is contained in:
sfan5 2024-03-12 14:13:24 +01:00
parent 24f2c38093
commit bc4ab8b99e
34 changed files with 330 additions and 439 deletions

View file

@ -20,17 +20,29 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include "server.h"
#include "server/mods.h"
#include "scripting_server.h"
class MockServer : public Server
{
public:
/* Set path_world to a real existing folder if you plan to initialize scripting! */
/* Set `path_world` to a real existing folder if you plan to initialize scripting! */
MockServer(const std::string &path_world = "fakepath") :
Server(path_world, SubgameSpec("fakespec", "fakespec"), true,
Address(), true, nullptr
)
{}
/*
* Use this in unit tests to create scripting.
* Note that you still need to call script->loadBuiltin() and don't forget
* a try-catch for `ModError`.
*/
void createScripting() {
m_script = std::make_unique<ServerScripting>(this);
m_modmgr = std::make_unique<ServerModManager>(nullptr);
}
void start() = delete;
void stop() = delete;

View file

@ -22,8 +22,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mock_inventorymanager.h"
#include "mock_server.h"
#include "mock_serveractiveobject.h"
#include "scripting_server.h"
#include "server/mods.h"
class TestMoveAction : public TestBase
{
@ -53,27 +51,21 @@ void TestMoveAction::runTests(IGameDef *gamedef)
{
MockServer server(getTestTempDirectory());
ServerScripting server_scripting(&server);
server.createScripting();
try {
// FIXME: When removing the line below, the unittest does NOT crash
// (but it should) when running all unittests in order or registration.
// Some Lua API functions used in builtin require the Mgr to be present.
server.m_modmgr = std::make_unique<ServerModManager>(server.m_path_world);
std::string builtin = Server::getBuiltinLuaPath() + DIR_DELIM;
server_scripting.loadBuiltin();
server_scripting.loadMod(builtin + "game" DIR_DELIM "tests" DIR_DELIM "test_moveaction.lua", BUILTIN_MOD_NAME);
auto script = server.getScriptIface();
script->loadBuiltin();
script->loadMod(builtin + "game" DIR_DELIM "tests" DIR_DELIM "test_moveaction.lua", BUILTIN_MOD_NAME);
} catch (ModError &e) {
// Print backtrace in case of syntax errors
rawstream << e.what() << std::endl;
num_tests_failed = 1;
return;
}
server.m_script = &server_scripting;
MetricsBackend mb;
ServerEnvironment server_env(nullptr, &server_scripting, &server, "", &mb);
auto null_map = std::unique_ptr<ServerMap>();
ServerEnvironment server_env(std::move(null_map), &server, &mb);
MockServerActiveObject obj(&server_env);
TEST(testMove, &obj, gamedef);
@ -88,8 +80,6 @@ void TestMoveAction::runTests(IGameDef *gamedef)
TEST(testCallbacks, &obj, &server);
TEST(testCallbacksSwap, &obj, &server);
server.m_script = nullptr; // Do not free stack memory
}
static ItemStack parse_itemstack(const char *s)

View file

@ -20,7 +20,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "test.h"
#include "mock_server.h"
#include "scripting_server.h"
#include "server/luaentity_sao.h"
#include "emerge.h"
@ -73,10 +72,11 @@ void TestSAO::runTests(IGameDef *gamedef)
ofs2 << "backend = dummy\n";
}
ServerScripting server_scripting(&server);
server.createScripting();
try {
server_scripting.loadBuiltin();
server_scripting.loadMod(helper_lua, BUILTIN_MOD_NAME);
auto script = server.getScriptIface();
script->loadBuiltin();
script->loadMod(helper_lua, BUILTIN_MOD_NAME);
} catch (ModError &e) {
rawstream << e.what() << std::endl;
num_tests_failed = 1;
@ -88,8 +88,8 @@ void TestSAO::runTests(IGameDef *gamedef)
// EmergeManager should become mockable
MetricsBackend mb;
EmergeManager emerge(&server, &mb);
auto *map = new ServerMap(server.getWorldPath(), gamedef, &emerge, &mb);
ServerEnvironment env(map, &server_scripting, &server, "", &mb);
auto map = std::make_unique<ServerMap>(server.getWorldPath(), gamedef, &emerge, &mb);
ServerEnvironment env(std::move(map), &server, &mb);
env.loadMeta();
m_step_interval = std::max(