add vctest (WIP)

This commit is contained in:
MihailRis
2024-12-07 15:49:23 +03:00
parent d9bd60f473
commit 5e8805f241
16 changed files with 439 additions and 52 deletions
+54
View File
@@ -25,6 +25,7 @@
#include "util/timeutil.hpp"
#include "voxels/Block.hpp"
#include "world/Level.hpp"
#include "interfaces/Process.hpp"
using namespace scripting;
@@ -71,6 +72,59 @@ void scripting::initialize(Engine* engine) {
load_script(fs::path("classes.lua"), true);
}
class LuaCoroutine : public Process {
lua::State* L;
int id;
bool alive = true;
public:
LuaCoroutine(lua::State* L, int id) : L(L), id(id) {
}
bool isActive() const override {
return alive;
}
void update() override {
if (lua::getglobal(L, "__vc_resume_coroutine")) {
lua::pushinteger(L, id);
if (lua::call(L, 1)) {
alive = lua::toboolean(L, -1);
lua::pop(L);
}
}
}
void waitForEnd() override {
while (isActive()) {
update();
}
}
void terminate() override {
if (lua::getglobal(L, "__vc_stop_coroutine")) {
lua::pushinteger(L, id);
lua::pop(L, lua::call(L, 1));
}
}
};
std::unique_ptr<Process> scripting::start_coroutine(
const std::filesystem::path& script
) {
auto L = lua::get_main_state();
if (lua::getglobal(L, "__vc_start_coroutine")) {
auto source = files::read_string(script);
lua::loadbuffer(L, 0, source, script.filename().u8string());
if (lua::call(L, 1)) {
int id = lua::tointeger(L, -1);
lua::pop(L, 2);
return std::make_unique<LuaCoroutine>(L, id);
}
lua::pop(L);
}
return nullptr;
}
[[nodiscard]] scriptenv scripting::get_root_environment() {
return std::make_shared<int>(0);
}