scripting.h fix + input library
This commit is contained in:
@@ -127,13 +127,14 @@ void lua::LuaState::createLibs() {
|
||||
openlib("core", corelib, 0);
|
||||
openlib("file", filelib, 0);
|
||||
openlib("gui", guilib, 0);
|
||||
openlib("input", inputlib, 0);
|
||||
openlib("inventory", inventorylib, 0);
|
||||
openlib("item", itemlib, 0);
|
||||
openlib("json", jsonlib, 0);
|
||||
openlib("pack", packlib, 0);
|
||||
openlib("player", playerlib, 0);
|
||||
openlib("time", timelib, 0);
|
||||
openlib("world", worldlib, 0);
|
||||
openlib("json", jsonlib, 0);
|
||||
|
||||
addfunc("print", lua_wrap_errors<l_print>);
|
||||
}
|
||||
@@ -426,6 +427,17 @@ void lua::LuaState::removeEnvironment(int id) {
|
||||
logger.debug() << "removed environment " << envName(id);
|
||||
}
|
||||
|
||||
bool lua::LuaState::emit_event(const std::string &name, std::function<int(lua::LuaState *)> args) {
|
||||
getglobal("events");
|
||||
getfield("emit");
|
||||
pushstring(name);
|
||||
callNoThrow(args(this) + 1);
|
||||
bool result = toboolean(-1);
|
||||
pop(2);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void lua::LuaState::dumpStack() {
|
||||
int top = gettop();
|
||||
for (int i = 1; i <= top; i++) {
|
||||
|
||||
@@ -67,6 +67,7 @@ namespace lua {
|
||||
int createEnvironment(int parent);
|
||||
void removeEnvironment(int id);
|
||||
const std::string storeAnonymous();
|
||||
bool emit_event(const std::string& name, std::function<int(lua::LuaState*)> args=[](auto*){return 0;});
|
||||
|
||||
void dumpStack();
|
||||
};
|
||||
|
||||
@@ -18,7 +18,7 @@ extern const luaL_Reg playerlib [];
|
||||
extern const luaL_Reg timelib [];
|
||||
extern const luaL_Reg worldlib [];
|
||||
extern const luaL_Reg jsonlib [];
|
||||
|
||||
extern const luaL_Reg inputlib [];
|
||||
|
||||
// Lua Overrides
|
||||
extern int l_print(lua_State* L);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "lua_commons.h"
|
||||
#include "api_lua.h"
|
||||
#include "LuaState.h"
|
||||
|
||||
#include "../../../engine.h"
|
||||
#include "../../../files/settings_io.hpp"
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
#include "api_lua.h"
|
||||
#include "lua_commons.h"
|
||||
#include "../scripting.h"
|
||||
|
||||
#include "../../../window/input.h"
|
||||
#include "../../../window/Events.h"
|
||||
#include "../../../frontend/screens/Screen.hpp"
|
||||
#include "../../../engine.h"
|
||||
|
||||
#include "LuaState.h"
|
||||
|
||||
namespace scripting {
|
||||
extern lua::LuaState* state;
|
||||
}
|
||||
|
||||
static int l_keycode(lua_State* L) {
|
||||
const char* name = lua_tostring(L, 1);
|
||||
lua_pushinteger(L, static_cast<int>(input_util::keycode_from(name)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
const luaL_Reg inputlib [] = {
|
||||
{"keycode", lua_wrap_errors<l_keycode>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@@ -110,25 +110,25 @@ void scripting::on_world_load(LevelController* controller) {
|
||||
load_script("world.lua");
|
||||
|
||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||
emit_event(pack.id + ".worldopen");
|
||||
state->emit_event(pack.id + ".worldopen");
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_world_tick() {
|
||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||
emit_event(pack.id + ".worldtick");
|
||||
state->emit_event(pack.id + ".worldtick");
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_world_save() {
|
||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||
emit_event(pack.id + ".worldsave");
|
||||
state->emit_event(pack.id + ".worldsave");
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::on_world_quit() {
|
||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||
emit_event(pack.id + ".worldquit");
|
||||
state->emit_event(pack.id + ".worldquit");
|
||||
}
|
||||
|
||||
state->getglobal("pack");
|
||||
@@ -151,7 +151,7 @@ void scripting::on_world_quit() {
|
||||
|
||||
void scripting::on_blocks_tick(const Block* block, int tps) {
|
||||
std::string name = block->name + ".blockstick";
|
||||
emit_event(name, [tps] (lua::LuaState* state) {
|
||||
state->emit_event(name, [tps] (lua::LuaState* state) {
|
||||
state->pushinteger(tps);
|
||||
return 1;
|
||||
});
|
||||
@@ -159,7 +159,7 @@ void scripting::on_blocks_tick(const Block* block, int tps) {
|
||||
|
||||
void scripting::update_block(const Block* block, int x, int y, int z) {
|
||||
std::string name = block->name + ".update";
|
||||
emit_event(name, [x, y, z] (lua::LuaState* state) {
|
||||
state->emit_event(name, [x, y, z] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
return 3;
|
||||
});
|
||||
@@ -167,7 +167,7 @@ void scripting::update_block(const Block* block, int x, int y, int z) {
|
||||
|
||||
void scripting::random_update_block(const Block* block, int x, int y, int z) {
|
||||
std::string name = block->name + ".randupdate";
|
||||
emit_event(name, [x, y, z] (lua::LuaState* state) {
|
||||
state->emit_event(name, [x, y, z] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
return 3;
|
||||
});
|
||||
@@ -175,7 +175,7 @@ void scripting::random_update_block(const Block* block, int x, int y, int z) {
|
||||
|
||||
void scripting::on_block_placed(Player* player, const Block* block, int x, int y, int z) {
|
||||
std::string name = block->name + ".placed";
|
||||
emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->pushinteger(player->getId());
|
||||
return 4;
|
||||
@@ -184,7 +184,7 @@ void scripting::on_block_placed(Player* player, const Block* block, int x, int y
|
||||
|
||||
void scripting::on_block_broken(Player* player, const Block* block, int x, int y, int z) {
|
||||
std::string name = block->name + ".broken";
|
||||
emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->pushinteger(player->getId());
|
||||
return 4;
|
||||
@@ -193,7 +193,7 @@ void scripting::on_block_broken(Player* player, const Block* block, int x, int y
|
||||
|
||||
bool scripting::on_block_interact(Player* player, const Block* block, int x, int y, int z) {
|
||||
std::string name = block->name + ".interact";
|
||||
return emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
return state->emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->pushinteger(player->getId());
|
||||
return 4;
|
||||
@@ -202,7 +202,7 @@ bool scripting::on_block_interact(Player* player, const Block* block, int x, int
|
||||
|
||||
bool scripting::on_item_use(Player* player, const ItemDef* item) {
|
||||
std::string name = item->name + ".use";
|
||||
return emit_event(name, [player] (lua::LuaState* state) {
|
||||
return state->emit_event(name, [player] (lua::LuaState* state) {
|
||||
state->pushinteger(player->getId());
|
||||
return 1;
|
||||
});
|
||||
@@ -210,7 +210,7 @@ bool scripting::on_item_use(Player* player, const ItemDef* item) {
|
||||
|
||||
bool scripting::on_item_use_on_block(Player* player, const ItemDef* item, int x, int y, int z) {
|
||||
std::string name = item->name + ".useon";
|
||||
return emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
return state->emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->pushinteger(player->getId());
|
||||
return 4;
|
||||
@@ -219,7 +219,7 @@ bool scripting::on_item_use_on_block(Player* player, const ItemDef* item, int x,
|
||||
|
||||
bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z) {
|
||||
std::string name = item->name + ".blockbreakby";
|
||||
return emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
return state->emit_event(name, [x, y, z, player] (lua::LuaState* state) {
|
||||
state->pushivec3(x, y, z);
|
||||
state->pushinteger(player->getId());
|
||||
return 4;
|
||||
@@ -232,7 +232,7 @@ void scripting::on_ui_open(
|
||||
) {
|
||||
auto argsptr = std::make_shared<std::vector<std::unique_ptr<dynamic::Value>>>(std::move(args));
|
||||
std::string name = layout->getId() + ".open";
|
||||
emit_event(name, [=] (lua::LuaState* state) {
|
||||
state->emit_event(name, [=] (lua::LuaState* state) {
|
||||
for (const auto& value : *argsptr) {
|
||||
state->pushvalue(*value);
|
||||
}
|
||||
@@ -242,7 +242,7 @@ void scripting::on_ui_open(
|
||||
|
||||
void scripting::on_ui_progress(UiDocument* layout, int workDone, int workTotal) {
|
||||
std::string name = layout->getId() + ".progress";
|
||||
emit_event(name, [=] (lua::LuaState* state) {
|
||||
state->emit_event(name, [=] (lua::LuaState* state) {
|
||||
state->pushinteger(workDone);
|
||||
state->pushinteger(workTotal);
|
||||
return 2;
|
||||
@@ -251,7 +251,7 @@ void scripting::on_ui_progress(UiDocument* layout, int workDone, int workTotal)
|
||||
|
||||
void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) {
|
||||
std::string name = layout->getId() + ".close";
|
||||
emit_event(name, [inventory] (lua::LuaState* state) {
|
||||
state->emit_event(name, [inventory] (lua::LuaState* state) {
|
||||
state->pushinteger(inventory == nullptr ? 0 : inventory->getId());
|
||||
return 1;
|
||||
});
|
||||
@@ -278,16 +278,6 @@ bool scripting::register_event(int env, const std::string& name, const std::stri
|
||||
return false;
|
||||
}
|
||||
|
||||
bool scripting::emit_event(const std::string &name, std::function<int(lua::LuaState *)> args) {
|
||||
state->getglobal("events");
|
||||
state->getfield("emit");
|
||||
state->pushstring(name);
|
||||
state->callNoThrow(args(state) + 1);
|
||||
bool result = state->toboolean(-1);
|
||||
state->pop(2);
|
||||
return result;
|
||||
}
|
||||
|
||||
void scripting::load_block_script(scriptenv senv, std::string prefix, fs::path file, block_funcs_set& funcsset) {
|
||||
int env = *senv;
|
||||
std::string src = files::read_string(file);
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#include "../../typedefs.h"
|
||||
#include "../../delegates.h"
|
||||
|
||||
#include "lua/LuaState.h"
|
||||
#include "scripting_functional.h"
|
||||
|
||||
#include <vector>
|
||||
@@ -41,9 +40,6 @@ namespace scripting {
|
||||
|
||||
extern bool register_event(int env, const std::string& name, const std::string& id);
|
||||
|
||||
static inline int noargs(lua::LuaState *) { return 0; }
|
||||
extern bool emit_event(const std::string& name, std::function<int(lua::LuaState* state)> args = noargs);
|
||||
|
||||
scriptenv get_root_environment();
|
||||
scriptenv create_pack_environment(const ContentPack& pack);
|
||||
scriptenv create_doc_environment(scriptenv parent, const std::string& name);
|
||||
|
||||
@@ -25,7 +25,7 @@ void scripting::on_frontend_init(Hud* hud) {
|
||||
scripting::state->openlib("hud", hudlib, 0);
|
||||
|
||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||
emit_event(pack.id + ".hudopen", [&] (lua::LuaState* state) {
|
||||
state->emit_event(pack.id + ".hudopen", [&] (lua::LuaState* state) {
|
||||
state->pushinteger(hud->getPlayer()->getId());
|
||||
return 1;
|
||||
});
|
||||
@@ -34,7 +34,7 @@ void scripting::on_frontend_init(Hud* hud) {
|
||||
|
||||
void scripting::on_frontend_close() {
|
||||
for (auto& pack : scripting::engine->getContentPacks()) {
|
||||
emit_event(pack.id + ".hudclose", [&] (lua::LuaState* state) {
|
||||
state->emit_event(pack.id + ".hudclose", [&] (lua::LuaState* state) {
|
||||
state->pushinteger(hud->getPlayer()->getId());
|
||||
return 1;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user