diff --git a/dev/tests/example.lua b/dev/tests/example.lua index 3b724d47..f030222d 100644 --- a/dev/tests/example.lua +++ b/dev/tests/example.lua @@ -1 +1,3 @@ print("Hello from the example test!") +test.sleep(1) +print("2") diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index d5f8eed1..9a5aa08a 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -9,6 +9,11 @@ function sleep(timesec) end end +if test then + test.sleep = sleep + test.name = __VC_TEST_NAME +end + ------------------------------------------------ ------------------- Events --------------------- ------------------------------------------------ diff --git a/src/engine.cpp b/src/engine.cpp index 49386b65..8f970892 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -190,9 +190,15 @@ void Engine::runTest() { logger.info() << "nothing to do"; return; } + int tps = 20; + logger.info() << "starting test " << params.testFile; auto process = scripting::start_coroutine(params.testFile); while (process->isActive()) { + frame++; + delta = 1.0f / static_cast(tps); + lastTime += delta; + process->update(); } logger.info() << "test finished"; @@ -466,6 +472,10 @@ double Engine::getDelta() const { return delta; } +double Engine::getUptime() const { + return lastTime; +} + void Engine::setScreen(std::shared_ptr screen) { // reset audio channels (stop all sources) audio::reset_channel(audio::get_channel_index("regular")); @@ -534,3 +544,7 @@ SettingsHandler& Engine::getSettingsHandler() { network::Network& Engine::getNetwork() { return *network; } + +const CoreParameters& Engine::getCoreParameters() const { + return params; +} diff --git a/src/engine.hpp b/src/engine.hpp index c3559fa8..a0d4a8e9 100644 --- a/src/engine.hpp +++ b/src/engine.hpp @@ -125,6 +125,8 @@ public: /// @brief Get current frame delta-time double getDelta() const; + double getUptime() const; + /// @brief Get active assets storage instance Assets* getAssets(); @@ -166,4 +168,6 @@ public: SettingsHandler& getSettingsHandler(); network::Network& getNetwork(); + + const CoreParameters& getCoreParameters() const; }; diff --git a/src/logic/scripting/lua/libs/api_lua.hpp b/src/logic/scripting/lua/libs/api_lua.hpp index a3b2d152..23f3db66 100644 --- a/src/logic/scripting/lua/libs/api_lua.hpp +++ b/src/logic/scripting/lua/libs/api_lua.hpp @@ -37,6 +37,7 @@ extern const luaL_Reg packlib[]; extern const luaL_Reg particleslib[]; // gfx.particles extern const luaL_Reg playerlib[]; extern const luaL_Reg quatlib[]; +extern const luaL_Reg testlib[]; extern const luaL_Reg text3dlib[]; // gfx.text3d extern const luaL_Reg timelib[]; extern const luaL_Reg tomllib[]; diff --git a/src/logic/scripting/lua/libs/libtest.cpp b/src/logic/scripting/lua/libs/libtest.cpp new file mode 100644 index 00000000..034a72fb --- /dev/null +++ b/src/logic/scripting/lua/libs/libtest.cpp @@ -0,0 +1,5 @@ +#include "api_lua.hpp" + +const luaL_Reg testlib[] = { + {NULL, NULL} +}; diff --git a/src/logic/scripting/lua/libs/libtime.cpp b/src/logic/scripting/lua/libs/libtime.cpp index 93708dda..45ed4288 100644 --- a/src/logic/scripting/lua/libs/libtime.cpp +++ b/src/logic/scripting/lua/libs/libtime.cpp @@ -2,15 +2,18 @@ #include "window/Window.hpp" #include "api_lua.hpp" -static int l_time_uptime(lua::State* L) { - return lua::pushnumber(L, Window::time()); +using namespace scripting; + +static int l_uptime(lua::State* L) { + return lua::pushnumber(L, engine->getUptime()); } -static int l_time_delta(lua::State* L) { - return lua::pushnumber(L, scripting::engine->getDelta()); +static int l_delta(lua::State* L) { + return lua::pushnumber(L, engine->getDelta()); } const luaL_Reg timelib[] = { - {"uptime", lua::wrap}, - {"delta", lua::wrap}, - {NULL, NULL}}; + {"uptime", lua::wrap}, + {"delta", lua::wrap}, + {NULL, NULL} +}; diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index 8f477710..0a4e2259 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -9,6 +9,7 @@ #include "util/stringutil.hpp" #include "libs/api_lua.hpp" #include "lua_custom_types.hpp" +#include "engine.hpp" static debug::Logger logger("lua-state"); static lua::State* main_thread = nullptr; @@ -57,7 +58,10 @@ static void create_libs(State* L, StateType stateType) { openlib(L, "vec3", vec3lib); openlib(L, "vec4", vec4lib); - if (stateType == StateType::BASE) { + if (stateType == StateType::TEST) { + openlib(L, "test", testlib); + } + if (stateType == StateType::BASE || stateType == StateType::TEST) { openlib(L, "gui", guilib); openlib(L, "input", inputlib); openlib(L, "inventory", inventorylib); @@ -110,11 +114,15 @@ void lua::init_state(State* L, StateType stateType) { newusertype(L); } -void lua::initialize(const EnginePaths& paths) { +void lua::initialize(const EnginePaths& paths, const CoreParameters& params) { logger.info() << LUA_VERSION; logger.info() << LUAJIT_VERSION; - main_thread = create_state(paths, StateType::BASE); + main_thread = create_state( + paths, params.headless ? StateType::TEST : StateType::BASE + ); + lua::pushstring(main_thread, params.testFile.stem().u8string()); + lua::setglobal(main_thread, "__VC_TEST_NAME"); } void lua::finalize() { diff --git a/src/logic/scripting/lua/lua_engine.hpp b/src/logic/scripting/lua/lua_engine.hpp index dd1e794d..a70f5948 100644 --- a/src/logic/scripting/lua/lua_engine.hpp +++ b/src/logic/scripting/lua/lua_engine.hpp @@ -8,14 +8,16 @@ #include "lua_util.hpp" class EnginePaths; +struct CoreParameters; namespace lua { enum class StateType { BASE, + TEST, GENERATOR, }; - void initialize(const EnginePaths& paths); + void initialize(const EnginePaths& paths, const CoreParameters& params); void finalize(); bool emit_event( diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 9abe534c..ec26bfcf 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -66,7 +66,7 @@ int scripting::load_script( void scripting::initialize(Engine* engine) { scripting::engine = engine; - lua::initialize(*engine->getPaths()); + lua::initialize(*engine->getPaths(), engine->getCoreParameters()); load_script(fs::path("stdlib.lua"), true); load_script(fs::path("classes.lua"), true);