UiDocument
This commit is contained in:
@@ -51,6 +51,16 @@ bool lua::LuaState::getglobal(const std::string& name) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool lua::LuaState::hasglobal(const std::string& name) {
|
||||
lua_getglobal(L, name.c_str());
|
||||
if (lua_isnil(L, lua_gettop(L))) {
|
||||
lua_pop(L, lua_gettop(L));
|
||||
return false;
|
||||
}
|
||||
lua_pop(L, lua_gettop(L));
|
||||
return true;
|
||||
}
|
||||
|
||||
void lua::LuaState::setglobal(const std::string& name) {
|
||||
lua_setglobal(L, name.c_str());
|
||||
}
|
||||
@@ -196,6 +206,18 @@ int lua::LuaState::createEnvironment() {
|
||||
return id;
|
||||
}
|
||||
|
||||
void lua::LuaState::removeEnvironment(int id) {
|
||||
if (currentEnvironment == id) {
|
||||
setEnvironment(0);
|
||||
}
|
||||
lua_pushnil(L);
|
||||
setglobal("_N"+util::mangleid(id));
|
||||
}
|
||||
|
||||
int lua::LuaState::getEnvironment() const {
|
||||
return currentEnvironment;
|
||||
}
|
||||
|
||||
void lua::LuaState::setEnvironment(int id) {
|
||||
if (id == 0) {
|
||||
getglobal(":G");
|
||||
|
||||
@@ -41,10 +41,13 @@ namespace lua {
|
||||
void addfunc(const std::string& name, lua_CFunction func);
|
||||
bool getglobal(const std::string& name);
|
||||
void setglobal(const std::string& name);
|
||||
bool hasglobal(const std::string& name);
|
||||
bool rename(const std::string& from, const std::string& to);
|
||||
void remove(const std::string& name);;
|
||||
void createFuncs();
|
||||
int createEnvironment();
|
||||
void removeEnvironment(int id);
|
||||
int getEnvironment() const;
|
||||
void setEnvironment(int id);
|
||||
|
||||
const std::string storeAnonymous();
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "../../voxels/Block.h"
|
||||
#include "../../items/ItemDef.h"
|
||||
#include "../../logic/BlocksController.h"
|
||||
#include "../../frontend/UiDocument.h"
|
||||
#include "../../engine.h"
|
||||
#include "LuaState.h"
|
||||
#include "../../util/stringutil.h"
|
||||
@@ -28,6 +29,19 @@ const Content* scripting::content = nullptr;
|
||||
const ContentIndices* scripting::indices = nullptr;
|
||||
BlocksController* scripting::blocks = nullptr;
|
||||
|
||||
Environment::Environment(int env) : env(env) {
|
||||
}
|
||||
|
||||
Environment::~Environment() {
|
||||
if (env) {
|
||||
state->removeEnvironment(env);
|
||||
}
|
||||
}
|
||||
|
||||
int Environment::getId() const {
|
||||
return env;
|
||||
}
|
||||
|
||||
void load_script(fs::path name) {
|
||||
auto paths = scripting::engine->getPaths();
|
||||
fs::path file = paths->getResources()/fs::path("scripts")/name;
|
||||
@@ -54,6 +68,19 @@ runnable scripting::create_runnable(
|
||||
};
|
||||
}
|
||||
|
||||
runnable scripting::create_runnable(
|
||||
const std::string& file,
|
||||
const std::string& src,
|
||||
const Environment& env
|
||||
) {
|
||||
return [=](){
|
||||
int previous = state->getEnvironment();
|
||||
state->setEnvironment(env.getId());
|
||||
state->execute(src, file);
|
||||
state->setEnvironment(previous);
|
||||
};
|
||||
}
|
||||
|
||||
wstringconsumer scripting::create_wstring_consumer(
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
@@ -75,6 +102,10 @@ wstringconsumer scripting::create_wstring_consumer(
|
||||
};
|
||||
}
|
||||
|
||||
std::unique_ptr<Environment> scripting::create_environment() {
|
||||
return std::make_unique<Environment>(state->createEnvironment());
|
||||
}
|
||||
|
||||
void scripting::on_world_load(Level* level, BlocksController* blocks) {
|
||||
scripting::level = level;
|
||||
scripting::content = level->content;
|
||||
@@ -186,27 +217,27 @@ bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x,
|
||||
return false;
|
||||
}
|
||||
|
||||
void scripting::load_block_script(std::string prefix, fs::path file, block_funcs_set* funcsset) {
|
||||
void scripting::load_block_script(std::string prefix, fs::path file, block_funcs_set& funcsset) {
|
||||
std::string src = files::read_string(file);
|
||||
std::cout << "loading script " << file.u8string() << std::endl;
|
||||
state->execute(src, file.u8string());
|
||||
|
||||
funcsset->init=state->rename("init", prefix+".init");
|
||||
funcsset->update=state->rename("on_update", prefix+".update");
|
||||
funcsset->randupdate=state->rename("on_random_update", prefix+".randupdate");
|
||||
funcsset->onbroken=state->rename("on_broken", prefix+".broken");
|
||||
funcsset->onplaced=state->rename("on_placed", prefix+".placed");
|
||||
funcsset->oninteract=state->rename("on_interact", prefix+".oninteract");
|
||||
funcsset->onblockstick=state->rename("on_blocks_tick", prefix+".blockstick");
|
||||
funcsset.init=state->rename("init", prefix+".init");
|
||||
funcsset.update=state->rename("on_update", prefix+".update");
|
||||
funcsset.randupdate=state->rename("on_random_update", prefix+".randupdate");
|
||||
funcsset.onbroken=state->rename("on_broken", prefix+".broken");
|
||||
funcsset.onplaced=state->rename("on_placed", prefix+".placed");
|
||||
funcsset.oninteract=state->rename("on_interact", prefix+".oninteract");
|
||||
funcsset.onblockstick=state->rename("on_blocks_tick", prefix+".blockstick");
|
||||
}
|
||||
|
||||
void scripting::load_item_script(std::string prefix, fs::path file, item_funcs_set* funcsset) {
|
||||
void scripting::load_item_script(std::string prefix, fs::path file, item_funcs_set& funcsset) {
|
||||
std::string src = files::read_string(file);
|
||||
std::cout << "loading script " << file.u8string() << std::endl;
|
||||
state->execute(src, file.u8string());
|
||||
funcsset->init=state->rename("init", prefix+".init");
|
||||
funcsset->on_use_on_block=state->rename("on_use_on_block", prefix+".useon");
|
||||
funcsset->on_block_break_by=state->rename("on_block_break_by", prefix+".blockbreakby");
|
||||
funcsset.init=state->rename("init", prefix+".init");
|
||||
funcsset.on_use_on_block=state->rename("on_use_on_block", prefix+".useon");
|
||||
funcsset.on_block_break_by=state->rename("on_block_break_by", prefix+".blockbreakby");
|
||||
}
|
||||
|
||||
void scripting::load_world_script(std::string prefix, fs::path file) {
|
||||
@@ -222,6 +253,17 @@ void scripting::load_world_script(std::string prefix, fs::path file) {
|
||||
state->rename("on_world_quit", prefix+".worldquit");
|
||||
}
|
||||
|
||||
void scripting::load_layout_script(fs::path file, uidocscript& script) {
|
||||
std::string src = files::read_string(file);
|
||||
std::cout << "loading script " << file.u8string() << std::endl;
|
||||
|
||||
script.environment = state->createEnvironment();
|
||||
state->loadbuffer(src, file.u8string());
|
||||
state->callNoThrow(0);
|
||||
script.onopen = state->hasglobal("on_open");
|
||||
script.onclose = state->hasglobal("on_close");
|
||||
}
|
||||
|
||||
void scripting::close() {
|
||||
delete state;
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ class Player;
|
||||
class ItemDef;
|
||||
struct block_funcs_set;
|
||||
struct item_funcs_set;
|
||||
struct uidocscript;
|
||||
class BlocksController;
|
||||
|
||||
namespace scripting {
|
||||
@@ -25,18 +26,35 @@ namespace scripting {
|
||||
extern Level* level;
|
||||
extern BlocksController* blocks;
|
||||
|
||||
class Environment {
|
||||
int env;
|
||||
public:
|
||||
Environment(int env);
|
||||
~Environment();
|
||||
|
||||
int getId() const;
|
||||
};
|
||||
|
||||
void initialize(Engine* engine);
|
||||
|
||||
runnable create_runnable(
|
||||
const std::string& filename,
|
||||
const std::string& source
|
||||
);
|
||||
|
||||
runnable create_runnable(
|
||||
const std::string& filename,
|
||||
const std::string& source,
|
||||
const Environment& env
|
||||
);
|
||||
|
||||
wstringconsumer create_wstring_consumer(
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
std::unique_ptr<Environment> create_environment();
|
||||
|
||||
void on_world_load(Level* level, BlocksController* blocks);
|
||||
void on_world_save();
|
||||
void on_world_quit();
|
||||
@@ -48,8 +66,9 @@ namespace scripting {
|
||||
bool on_block_interact(Player* player, const Block* block, int x, int y, int z);
|
||||
bool on_item_use_on_block(Player* player, const ItemDef* item, int x, int y, int z);
|
||||
bool on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z);
|
||||
void load_block_script(std::string prefix, fs::path file, block_funcs_set* funcsset);
|
||||
void load_item_script(std::string prefix, fs::path file, item_funcs_set* funcsset);
|
||||
void load_block_script(std::string prefix, fs::path file, block_funcs_set& funcsset);
|
||||
void load_item_script(std::string prefix, fs::path file, item_funcs_set& funcsset);
|
||||
void load_world_script(std::string prefix, fs::path file);
|
||||
void load_layout_script(fs::path file, uidocscript& script);
|
||||
void close();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user