add 'test' library

This commit is contained in:
MihailRis 2024-12-07 22:16:50 +03:00
parent bbb9987140
commit 59402b6607
10 changed files with 56 additions and 12 deletions

View File

@ -1 +1,3 @@
print("Hello from the example test!")
test.sleep(1)
print("2")

View File

@ -9,6 +9,11 @@ function sleep(timesec)
end
end
if test then
test.sleep = sleep
test.name = __VC_TEST_NAME
end
------------------------------------------------
------------------- Events ---------------------
------------------------------------------------

View File

@ -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<float>(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> 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;
}

View File

@ -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;
};

View File

@ -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[];

View File

@ -0,0 +1,5 @@
#include "api_lua.hpp"
const luaL_Reg testlib[] = {
{NULL, NULL}
};

View File

@ -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<l_time_uptime>},
{"delta", lua::wrap<l_time_delta>},
{NULL, NULL}};
{"uptime", lua::wrap<l_uptime>},
{"delta", lua::wrap<l_delta>},
{NULL, NULL}
};

View File

@ -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<LuaVoxelFragment>(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() {

View File

@ -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(

View File

@ -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);