scriptenv?
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
#ifndef LOGIC_SCRIPTING_ENVIRONMENT_H_
|
||||
#define LOGIC_SCRIPTING_ENVIRONMENT_H_
|
||||
|
||||
namespace scripting {
|
||||
/// @brief Lua environment wrapper for automatic deletion
|
||||
class Environment {
|
||||
int env;
|
||||
public:
|
||||
Environment(int env);
|
||||
~Environment();
|
||||
|
||||
int getId() const;
|
||||
|
||||
// @brief Release namespace control
|
||||
void release();
|
||||
};
|
||||
}
|
||||
|
||||
#endif // LOGIC_SCRIPTING_ENVIRONMENT_H_
|
||||
@@ -348,6 +348,9 @@ int lua::LuaState::createEnvironment(int parent) {
|
||||
|
||||
|
||||
void lua::LuaState::removeEnvironment(int id) {
|
||||
if (id == 0) {
|
||||
return;
|
||||
}
|
||||
lua_pushnil(L);
|
||||
setglobal(envName(id));
|
||||
}
|
||||
|
||||
@@ -341,7 +341,7 @@ static int l_gui_get_env(lua_State* L) {
|
||||
if (doc == nullptr) {
|
||||
luaL_error(L, "document '%s' not found", name);
|
||||
}
|
||||
lua_getglobal(L, lua::LuaState::envName(doc->getEnvironment()).c_str());
|
||||
lua_getglobal(L, lua::LuaState::envName(*doc->getEnvironment()).c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
#include "../../logic/LevelController.h"
|
||||
#include "../../frontend/UiDocument.h"
|
||||
#include "../../engine.h"
|
||||
#include "Environment.h"
|
||||
#include "lua/LuaState.h"
|
||||
#include "../../util/stringutil.h"
|
||||
#include "../../util/timeutil.h"
|
||||
@@ -38,23 +37,6 @@ const ContentIndices* scripting::indices = nullptr;
|
||||
BlocksController* scripting::blocks = nullptr;
|
||||
LevelController* scripting::controller = nullptr;
|
||||
|
||||
Environment::Environment(int env) : env(env) {
|
||||
}
|
||||
|
||||
Environment::~Environment() {
|
||||
if (env) {
|
||||
state->removeEnvironment(env);
|
||||
}
|
||||
}
|
||||
|
||||
int Environment::getId() const {
|
||||
return env;
|
||||
}
|
||||
|
||||
void Environment::release() {
|
||||
env = 0;
|
||||
}
|
||||
|
||||
void load_script(fs::path name) {
|
||||
auto paths = scripting::engine->getPaths();
|
||||
fs::path file = paths->getResources()/fs::path("scripts")/name;
|
||||
@@ -71,11 +53,11 @@ void scripting::initialize(Engine* engine) {
|
||||
load_script(fs::path("stdlib.lua"));
|
||||
}
|
||||
|
||||
std::unique_ptr<Environment> scripting::create_environment(int parent) {
|
||||
return std::make_unique<Environment>(state->createEnvironment(parent));
|
||||
scriptenv scripting::get_root_environment() {
|
||||
return std::make_shared<int>(0);
|
||||
}
|
||||
|
||||
std::unique_ptr<Environment> scripting::create_pack_environment(const ContentPack& pack) {
|
||||
scriptenv scripting::create_pack_environment(const ContentPack& pack) {
|
||||
int id = state->createEnvironment(0);
|
||||
state->pushenv(id);
|
||||
state->pushvalue(-1);
|
||||
@@ -83,11 +65,13 @@ std::unique_ptr<Environment> scripting::create_pack_environment(const ContentPac
|
||||
state->pushstring(pack.id);
|
||||
state->setfield("PACK_ID");
|
||||
state->pop();
|
||||
return std::make_unique<Environment>(id);
|
||||
return std::shared_ptr<int>(new int(id), [=](int* id) {
|
||||
state->removeEnvironment(*id);
|
||||
});
|
||||
}
|
||||
|
||||
std::unique_ptr<Environment> scripting::create_doc_environment(int parent, const std::string& name) {
|
||||
int id = state->createEnvironment(parent);
|
||||
scriptenv scripting::create_doc_environment(scriptenv parent, const std::string& name) {
|
||||
int id = state->createEnvironment(*parent);
|
||||
state->pushenv(id);
|
||||
state->pushvalue(-1);
|
||||
state->setfield("DOC_ENV");
|
||||
@@ -104,7 +88,9 @@ std::unique_ptr<Environment> scripting::create_doc_environment(int parent, const
|
||||
state->pop();
|
||||
}
|
||||
state->pop();
|
||||
return std::make_unique<Environment>(id);
|
||||
return std::shared_ptr<int>(new int(id), [=](int* id) {
|
||||
state->removeEnvironment(*id);
|
||||
});
|
||||
}
|
||||
|
||||
void scripting::process_post_runnables() {
|
||||
@@ -286,7 +272,8 @@ bool scripting::emit_event(const std::string &name, std::function<int(lua::LuaSt
|
||||
return result;
|
||||
}
|
||||
|
||||
void scripting::load_block_script(int env, std::string prefix, fs::path file, block_funcs_set& funcsset) {
|
||||
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);
|
||||
logger.info() << "script (block) " << file.u8string();
|
||||
state->execute(env, src, file.u8string());
|
||||
@@ -299,7 +286,8 @@ void scripting::load_block_script(int env, std::string prefix, fs::path file, bl
|
||||
funcsset.onblockstick = register_event(env, "on_blocks_tick", prefix+".blockstick");
|
||||
}
|
||||
|
||||
void scripting::load_item_script(int env, std::string prefix, fs::path file, item_funcs_set& funcsset) {
|
||||
void scripting::load_item_script(scriptenv senv, std::string prefix, fs::path file, item_funcs_set& funcsset) {
|
||||
int env = *senv;
|
||||
std::string src = files::read_string(file);
|
||||
logger.info() << "script (item) " << file.u8string();
|
||||
state->execute(env, src, file.u8string());
|
||||
@@ -310,7 +298,9 @@ void scripting::load_item_script(int env, std::string prefix, fs::path file, ite
|
||||
funcsset.on_block_break_by = register_event(env, "on_block_break_by", prefix+".blockbreakby");
|
||||
}
|
||||
|
||||
void scripting::load_world_script(int env, std::string prefix, fs::path file) {
|
||||
void scripting::load_world_script(scriptenv senv, std::string prefix, fs::path file) {
|
||||
int env = *senv;
|
||||
|
||||
std::string src = files::read_string(file);
|
||||
logger.info() << "loading world script for " << prefix;
|
||||
|
||||
@@ -324,11 +314,12 @@ void scripting::load_world_script(int env, std::string prefix, fs::path file) {
|
||||
register_event(env, "on_world_quit", prefix+".worldquit");
|
||||
}
|
||||
|
||||
void scripting::load_layout_script(int env, std::string prefix, fs::path file, uidocscript& script) {
|
||||
void scripting::load_layout_script(scriptenv senv, std::string prefix, fs::path file, uidocscript& script) {
|
||||
int env = *senv;
|
||||
|
||||
std::string src = files::read_string(file);
|
||||
logger.info() << "loading script " << file.u8string();
|
||||
|
||||
script.environment = env;
|
||||
state->loadbuffer(env, src, file.u8string());
|
||||
state->callNoThrow(0);
|
||||
script.onopen = register_event(env, "on_open", prefix+".open");
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <filesystem>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "../../typedefs.h"
|
||||
#include "../../delegates.h"
|
||||
|
||||
#include "lua/LuaState.h"
|
||||
@@ -33,8 +34,6 @@ namespace scripting {
|
||||
extern BlocksController* blocks;
|
||||
extern LevelController* controller;
|
||||
|
||||
class Environment;
|
||||
|
||||
void initialize(Engine* engine);
|
||||
|
||||
extern bool register_event(int env, const std::string& name, const std::string& id);
|
||||
@@ -42,9 +41,9 @@ namespace scripting {
|
||||
static inline int noargs(lua::LuaState *) { return 0; }
|
||||
extern bool emit_event(const std::string& name, std::function<int(lua::LuaState* state)> args = noargs);
|
||||
|
||||
std::unique_ptr<Environment> create_environment(int parent=0);
|
||||
std::unique_ptr<Environment> create_pack_environment(const ContentPack& pack);
|
||||
std::unique_ptr<Environment> create_doc_environment(int parent, const std::string& name);
|
||||
scriptenv get_root_environment();
|
||||
scriptenv create_pack_environment(const ContentPack& pack);
|
||||
scriptenv create_doc_environment(scriptenv parent, const std::string& name);
|
||||
|
||||
void process_post_runnables();
|
||||
|
||||
@@ -78,31 +77,31 @@ namespace scripting {
|
||||
void on_ui_close(UiDocument* layout, Inventory* inventory);
|
||||
|
||||
/// @brief Load script associated with a Block
|
||||
/// @param env environment id
|
||||
/// @param env environment
|
||||
/// @param prefix pack id
|
||||
/// @param file item script file
|
||||
/// @param funcsset block callbacks set
|
||||
void load_block_script(int env, std::string prefix, fs::path file, block_funcs_set& funcsset);
|
||||
void load_block_script(scriptenv env, std::string prefix, fs::path file, block_funcs_set& funcsset);
|
||||
|
||||
/// @brief Load script associated with an Item
|
||||
/// @param env environment id
|
||||
/// @param env environment
|
||||
/// @param prefix pack id
|
||||
/// @param file item script file
|
||||
/// @param funcsset item callbacks set
|
||||
void load_item_script(int env, std::string prefix, fs::path file, item_funcs_set& funcsset);
|
||||
void load_item_script(scriptenv env, std::string prefix, fs::path file, item_funcs_set& funcsset);
|
||||
|
||||
/// @brief Load package-specific world script
|
||||
/// @param env environment id
|
||||
/// @param env environment
|
||||
/// @param packid content-pack id
|
||||
/// @param file script file path
|
||||
void load_world_script(int env, std::string packid, fs::path file);
|
||||
void load_world_script(scriptenv env, std::string packid, fs::path file);
|
||||
|
||||
/// @brief Load script associated with an UiDocument
|
||||
/// @param env environment id
|
||||
/// @param env environment
|
||||
/// @param prefix pack id
|
||||
/// @param file item script file
|
||||
/// @param script document script info
|
||||
void load_layout_script(int env, std::string prefix, fs::path file, uidocscript& script);
|
||||
void load_layout_script(scriptenv env, std::string prefix, fs::path file, uidocscript& script);
|
||||
|
||||
/// @brief Finalize lua state. Using scripting after will lead to Lua panic
|
||||
void close();
|
||||
|
||||
@@ -12,13 +12,13 @@ namespace scripting {
|
||||
using namespace scripting;
|
||||
|
||||
runnable scripting::create_runnable(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
return [=](){
|
||||
try {
|
||||
state->execute(env, src, file);
|
||||
state->execute(*env, src, file);
|
||||
} catch (const lua::luaerror& err) {
|
||||
std::cerr << err.what() << std::endl;
|
||||
}
|
||||
@@ -26,12 +26,12 @@ runnable scripting::create_runnable(
|
||||
}
|
||||
|
||||
static bool processCallback(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
try {
|
||||
return state->eval(env, src, file) != 0;
|
||||
return state->eval(*env, src, file) != 0;
|
||||
} catch (lua::luaerror& err) {
|
||||
std::cerr << err.what() << std::endl;
|
||||
return false;
|
||||
@@ -39,7 +39,7 @@ static bool processCallback(
|
||||
}
|
||||
|
||||
wstringconsumer scripting::create_wstring_consumer(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
@@ -52,7 +52,7 @@ wstringconsumer scripting::create_wstring_consumer(
|
||||
}
|
||||
|
||||
wstringsupplier scripting::create_wstring_supplier(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
@@ -69,7 +69,7 @@ wstringsupplier scripting::create_wstring_supplier(
|
||||
}
|
||||
|
||||
wstringchecker scripting::create_wstring_validator(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
@@ -84,7 +84,7 @@ wstringchecker scripting::create_wstring_validator(
|
||||
}
|
||||
|
||||
boolconsumer scripting::create_bool_consumer(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
@@ -97,7 +97,7 @@ boolconsumer scripting::create_bool_consumer(
|
||||
}
|
||||
|
||||
boolsupplier scripting::create_bool_supplier(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
@@ -114,7 +114,7 @@ boolsupplier scripting::create_bool_supplier(
|
||||
}
|
||||
|
||||
doubleconsumer scripting::create_number_consumer(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
@@ -127,7 +127,7 @@ doubleconsumer scripting::create_number_consumer(
|
||||
}
|
||||
|
||||
doublesupplier scripting::create_number_supplier(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
@@ -144,7 +144,7 @@ doublesupplier scripting::create_number_supplier(
|
||||
}
|
||||
|
||||
int_array_consumer scripting::create_int_array_consumer(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
@@ -159,7 +159,7 @@ int_array_consumer scripting::create_int_array_consumer(
|
||||
}
|
||||
|
||||
vec2supplier scripting::create_vec2_supplier(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file
|
||||
) {
|
||||
|
||||
@@ -3,65 +3,66 @@
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <string>
|
||||
#include "../../typedefs.h"
|
||||
#include "../../delegates.h"
|
||||
|
||||
namespace scripting {
|
||||
runnable create_runnable(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
wstringconsumer create_wstring_consumer(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
wstringsupplier create_wstring_supplier(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
wstringchecker create_wstring_validator(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
boolconsumer create_bool_consumer(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
boolsupplier create_bool_supplier(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
doubleconsumer create_number_consumer(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
doublesupplier create_number_supplier(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
int_array_consumer create_int_array_consumer(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
vec2supplier create_vec2_supplier(
|
||||
int env,
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file="<string>"
|
||||
);
|
||||
|
||||
@@ -42,7 +42,8 @@ void scripting::on_frontend_close() {
|
||||
scripting::hud = nullptr;
|
||||
}
|
||||
|
||||
void scripting::load_hud_script(int env, std::string packid, fs::path file) {
|
||||
void scripting::load_hud_script(scriptenv senv, std::string packid, fs::path file) {
|
||||
int env = *senv;
|
||||
std::string src = files::read_string(file);
|
||||
logger.info() << "loading script " << file.u8string();
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef LOGIC_SCRIPTING_SCRIPTING_HUD_H_
|
||||
#define LOGIC_SCRIPTING_SCRIPTING_HUD_H_
|
||||
|
||||
#include "../../typedefs.h"
|
||||
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
@@ -20,7 +22,7 @@ namespace scripting {
|
||||
* @param packid content-pack id
|
||||
* @param file script file path
|
||||
*/
|
||||
void load_hud_script(int env, std::string packid, fs::path file);
|
||||
void load_hud_script(scriptenv env, std::string packid, fs::path file);
|
||||
}
|
||||
|
||||
#endif // LOGIC_SCRIPTING_SCRIPTING_HUD_H_
|
||||
|
||||
Reference in New Issue
Block a user