scriptenv?
This commit is contained in:
parent
d213902648
commit
635e512142
@ -16,7 +16,6 @@
|
||||
#include "../content/Content.h"
|
||||
#include "../content/ContentPack.h"
|
||||
#include "../logic/scripting/scripting.h"
|
||||
#include "../logic/scripting/Environment.h"
|
||||
|
||||
static debug::Logger logger("assets-loader");
|
||||
|
||||
@ -69,7 +68,7 @@ bool AssetsLoader::loadNext() {
|
||||
}
|
||||
}
|
||||
|
||||
void addLayouts(int env, const std::string& prefix, const fs::path& folder, AssetsLoader& loader) {
|
||||
void addLayouts(scriptenv env, const std::string& prefix, const fs::path& folder, AssetsLoader& loader) {
|
||||
if (!fs::is_directory(folder)) {
|
||||
return;
|
||||
}
|
||||
@ -197,7 +196,7 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) {
|
||||
auto pack = entry.second.get();
|
||||
auto& info = pack->getInfo();
|
||||
fs::path folder = info.folder / fs::path("layouts");
|
||||
addLayouts(pack->getEnvironment()->getId(), info.id, folder, loader);
|
||||
addLayouts(pack->getEnvironment(), info.id, folder, loader);
|
||||
}
|
||||
}
|
||||
loader.add(AssetType::atlas, TEXTURES_FOLDER+"/blocks", "blocks");
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
|
||||
#include "Assets.h"
|
||||
#include "../interfaces/Task.h"
|
||||
#include "../typedefs.h"
|
||||
#include "../delegates.h"
|
||||
|
||||
#include <string>
|
||||
@ -35,9 +36,9 @@ struct AssetCfg {
|
||||
};
|
||||
|
||||
struct LayoutCfg : AssetCfg {
|
||||
int env;
|
||||
scriptenv env;
|
||||
|
||||
LayoutCfg(int env) : env(env) {}
|
||||
LayoutCfg(scriptenv env) : env(env) {}
|
||||
};
|
||||
|
||||
struct SoundCfg : AssetCfg {
|
||||
|
||||
@ -19,7 +19,6 @@
|
||||
|
||||
#include "ContentPack.h"
|
||||
#include "../logic/scripting/scripting.h"
|
||||
#include "../logic/scripting/Environment.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
@ -328,7 +327,7 @@ void ContentLoader::load(ContentBuilder& builder) {
|
||||
|
||||
auto runtime = new ContentPackRuntime(*pack, scripting::create_pack_environment(*pack));
|
||||
builder.add(runtime);
|
||||
env = runtime->getEnvironment()->getId();
|
||||
env = runtime->getEnvironment();
|
||||
ContentPackStats& stats = runtime->getStatsWriteable();
|
||||
|
||||
fixPackIndices();
|
||||
|
||||
@ -18,7 +18,7 @@ namespace dynamic {
|
||||
|
||||
class ContentLoader {
|
||||
const ContentPack* pack;
|
||||
int env = 0;
|
||||
scriptenv env;
|
||||
|
||||
void loadBlock(Block& def, std::string full, std::string name);
|
||||
void loadCustomBlockModel(Block& def, dynamic::Map* primitives);
|
||||
|
||||
@ -7,7 +7,6 @@
|
||||
#include "../files/files.h"
|
||||
#include "../files/engine_paths.h"
|
||||
#include "../data/dynamic.h"
|
||||
#include "../logic/scripting/Environment.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
@ -141,7 +140,7 @@ fs::path ContentPack::findPack(const EnginePaths* paths, fs::path worldDir, std:
|
||||
|
||||
ContentPackRuntime::ContentPackRuntime(
|
||||
ContentPack info,
|
||||
std::unique_ptr<scripting::Environment> env
|
||||
scriptenv env
|
||||
) : info(info), env(std::move(env))
|
||||
{
|
||||
}
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
#ifndef CONTENT_CONTENT_PACK_H_
|
||||
#define CONTENT_CONTENT_PACK_H_
|
||||
|
||||
#include "../typedefs.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stdexcept>
|
||||
@ -85,11 +87,11 @@ struct ContentPackStats {
|
||||
class ContentPackRuntime {
|
||||
ContentPack info;
|
||||
ContentPackStats stats {};
|
||||
std::unique_ptr<scripting::Environment> env;
|
||||
scriptenv env;
|
||||
public:
|
||||
ContentPackRuntime(
|
||||
ContentPack info,
|
||||
std::unique_ptr<scripting::Environment> env
|
||||
scriptenv env
|
||||
);
|
||||
~ContentPackRuntime();
|
||||
|
||||
@ -109,8 +111,8 @@ public:
|
||||
return info;
|
||||
}
|
||||
|
||||
inline scripting::Environment* getEnvironment() const {
|
||||
return env.get();
|
||||
inline scriptenv getEnvironment() const {
|
||||
return env;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -185,6 +185,7 @@ Engine::~Engine() {
|
||||
}
|
||||
content.reset();
|
||||
assets.reset();
|
||||
gui.reset();
|
||||
audio::close();
|
||||
scripting::close();
|
||||
Window::terminate();
|
||||
|
||||
@ -15,7 +15,6 @@
|
||||
#include "../items/Inventory.h"
|
||||
#include "../items/ItemDef.h"
|
||||
#include "../logic/scripting/scripting.h"
|
||||
#include "../logic/scripting/Environment.h"
|
||||
#include "../maths/voxmaths.h"
|
||||
#include "../objects/Player.h"
|
||||
#include "../util/stringutil.h"
|
||||
@ -372,7 +371,7 @@ void InventoryView::setInventory(std::shared_ptr<Inventory> inventory) {
|
||||
|
||||
static slotcallback readSlotFunc(InventoryView* view, gui::UiXmlReader& reader, xml::xmlelement& element, const std::string& attr) {
|
||||
auto consumer = scripting::create_int_array_consumer(
|
||||
reader.getEnvironment().getId(),
|
||||
reader.getEnvironment(),
|
||||
element->attr(attr).getText()
|
||||
);
|
||||
return [=](uint slot, ItemStack& stack) {
|
||||
|
||||
@ -10,8 +10,8 @@ UiDocument::UiDocument(
|
||||
std::string id,
|
||||
uidocscript script,
|
||||
std::shared_ptr<gui::UINode> root,
|
||||
std::unique_ptr<scripting::Environment> env
|
||||
) : id(id), script(script), root(root), env(std::move(env)) {
|
||||
scriptenv env
|
||||
) : id(id), script(script), root(root), env(env) {
|
||||
gui::UINode::getIndices(root, map);
|
||||
}
|
||||
|
||||
@ -47,19 +47,19 @@ const uidocscript& UiDocument::getScript() const {
|
||||
return script;
|
||||
}
|
||||
|
||||
int UiDocument::getEnvironment() const {
|
||||
return env->getId();
|
||||
scriptenv UiDocument::getEnvironment() const {
|
||||
return env;
|
||||
}
|
||||
|
||||
std::unique_ptr<UiDocument> UiDocument::read(int penv, std::string name, fs::path file) {
|
||||
std::unique_ptr<UiDocument> UiDocument::read(scriptenv penv, std::string name, fs::path file) {
|
||||
const std::string text = files::read_string(file);
|
||||
auto xmldoc = xml::parse(file.u8string(), text);
|
||||
|
||||
auto env = penv == -1
|
||||
? std::make_unique<scripting::Environment>(0)
|
||||
auto env = penv == nullptr
|
||||
? scripting::get_root_environment()
|
||||
: scripting::create_doc_environment(penv, name);
|
||||
|
||||
gui::UiXmlReader reader(*env);
|
||||
gui::UiXmlReader reader(env);
|
||||
InventoryView::createReaders(reader);
|
||||
auto view = reader.readXML(
|
||||
file.u8string(), xmldoc->getRoot()
|
||||
@ -68,12 +68,12 @@ std::unique_ptr<UiDocument> UiDocument::read(int penv, std::string name, fs::pat
|
||||
uidocscript script {};
|
||||
auto scriptFile = fs::path(file.u8string()+".lua");
|
||||
if (fs::is_regular_file(scriptFile)) {
|
||||
scripting::load_layout_script(env->getId(), name, scriptFile, script);
|
||||
scripting::load_layout_script(env, name, scriptFile, script);
|
||||
}
|
||||
return std::make_unique<UiDocument>(name, script, view, std::move(env));
|
||||
return std::make_unique<UiDocument>(name, script, view, env);
|
||||
}
|
||||
|
||||
std::shared_ptr<gui::UINode> UiDocument::readElement(fs::path file) {
|
||||
auto document = read(-1, file.filename().u8string(), file);
|
||||
auto document = read(nullptr, file.filename().u8string(), file);
|
||||
return document->getRoot();
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#ifndef FRONTEND_UI_DOCUMENT_H_
|
||||
#define FRONTEND_UI_DOCUMENT_H_
|
||||
|
||||
#include "../logic/scripting/Environment.h"
|
||||
#include "../typedefs.h"
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
@ -14,12 +14,7 @@ namespace gui {
|
||||
class UINode;
|
||||
}
|
||||
|
||||
namespace scripting {
|
||||
class Environment;
|
||||
}
|
||||
|
||||
struct uidocscript {
|
||||
int environment;
|
||||
bool onopen : 1;
|
||||
bool onclose : 1;
|
||||
};
|
||||
@ -31,13 +26,13 @@ class UiDocument {
|
||||
uidocscript script;
|
||||
uinodes_map map;
|
||||
std::shared_ptr<gui::UINode> root;
|
||||
std::unique_ptr<scripting::Environment> env;
|
||||
scriptenv env;
|
||||
public:
|
||||
UiDocument(
|
||||
std::string id,
|
||||
uidocscript script,
|
||||
std::shared_ptr<gui::UINode> root,
|
||||
std::unique_ptr<scripting::Environment> env
|
||||
scriptenv env
|
||||
);
|
||||
|
||||
void rebuildIndices();
|
||||
@ -48,9 +43,9 @@ public:
|
||||
const std::shared_ptr<gui::UINode> getRoot() const;
|
||||
const std::shared_ptr<gui::UINode> get(const std::string& id) const;
|
||||
const uidocscript& getScript() const;
|
||||
int getEnvironment() const;
|
||||
scriptenv getEnvironment() const;
|
||||
|
||||
static std::unique_ptr<UiDocument> read(int env, std::string name, fs::path file);
|
||||
static std::unique_ptr<UiDocument> read(scriptenv parent_env, std::string name, fs::path file);
|
||||
static std::shared_ptr<gui::UINode> readElement(fs::path file);
|
||||
};
|
||||
|
||||
|
||||
@ -41,7 +41,7 @@ void menus::create_menus(Engine* engine) {
|
||||
auto file = engine->getResPaths()->find("layouts/pages/"+name+".xml");
|
||||
auto fullname = "core:pages/"+name;
|
||||
|
||||
auto document = UiDocument::read(0, fullname, file).release();
|
||||
auto document = UiDocument::read(scripting::get_root_environment(), fullname, file).release();
|
||||
engine->getAssets()->store(document, fullname);
|
||||
scripting::on_ui_open(document, nullptr, glm::ivec3());
|
||||
return document->getRoot();
|
||||
|
||||
@ -16,7 +16,6 @@
|
||||
#include "../logic/LevelController.h"
|
||||
#include "../logic/scripting/scripting_hud.h"
|
||||
#include "../logic/scripting/scripting.h"
|
||||
#include "../logic/scripting/Environment.h"
|
||||
#include "../objects/Player.h"
|
||||
#include "../physics/Hitbox.h"
|
||||
#include "../util/stringutil.h"
|
||||
@ -113,7 +112,7 @@ LevelScreen::LevelScreen(Engine* engine, Level* level) : Screen(engine) {
|
||||
const ContentPack& info = pack->getInfo();
|
||||
fs::path scriptFile = info.folder/fs::path("scripts/hud.lua");
|
||||
if (fs::is_regular_file(scriptFile)) {
|
||||
scripting::load_hud_script(pack->getEnvironment()->getId(), info.id, scriptFile);
|
||||
scripting::load_hud_script(pack->getEnvironment(), info.id, scriptFile);
|
||||
}
|
||||
}
|
||||
scripting::on_frontend_init(hud.get());
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "../../logic/scripting/Environment.h"
|
||||
#include "../../logic/scripting/scripting.h"
|
||||
#include "../../frontend/locale/langs.h"
|
||||
#include "../../util/stringutil.h"
|
||||
#include "../../delegates.h"
|
||||
@ -32,12 +32,12 @@ std::shared_ptr<Button> guiutil::gotoButton(
|
||||
));
|
||||
}
|
||||
|
||||
std::shared_ptr<gui::UINode> guiutil::create(const std::string& source, int envid) {
|
||||
scripting::Environment env(envid);
|
||||
std::shared_ptr<gui::UINode> guiutil::create(const std::string& source, scriptenv env) {
|
||||
if (env == nullptr) {
|
||||
env = scripting::get_root_environment();
|
||||
}
|
||||
UiXmlReader reader(env);
|
||||
auto node = reader.readXML("<string>", source);
|
||||
env.release();
|
||||
return node;
|
||||
return reader.readXML("<string>", source);
|
||||
}
|
||||
|
||||
void guiutil::alert(GUI* gui, const std::wstring& text, runnable on_hidden) {
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "GUI.h"
|
||||
#include "../../typedefs.h"
|
||||
#include "../../delegates.h"
|
||||
|
||||
namespace gui {
|
||||
@ -23,7 +24,7 @@ namespace guiutil {
|
||||
|
||||
/// @brief Create element from XML
|
||||
/// @param source XML
|
||||
std::shared_ptr<gui::UINode> create(const std::string& source, int env=0);
|
||||
std::shared_ptr<gui::UINode> create(const std::string& source, scriptenv env=0);
|
||||
|
||||
void alert(
|
||||
gui::GUI* gui,
|
||||
|
||||
@ -13,7 +13,6 @@
|
||||
|
||||
#include "../../frontend/locale/langs.h"
|
||||
#include "../../logic/scripting/scripting.h"
|
||||
#include "../../logic/scripting/Environment.h"
|
||||
#include "../../util/stringutil.h"
|
||||
#include "../../window/Events.h"
|
||||
|
||||
@ -86,7 +85,7 @@ static void _readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& no
|
||||
}
|
||||
if (element->has("position-func")) {
|
||||
auto supplier = scripting::create_vec2_supplier(
|
||||
reader.getEnvironment().getId(),
|
||||
reader.getEnvironment(),
|
||||
element->attr("position-func").getText(),
|
||||
reader.getFilename()+".lua"
|
||||
);
|
||||
@ -111,7 +110,7 @@ static void _readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& no
|
||||
std::string text = element->attr("onclick").getText();
|
||||
if (!text.empty()) {
|
||||
auto callback = scripting::create_runnable(
|
||||
reader.getEnvironment().getId(),
|
||||
reader.getEnvironment(),
|
||||
text,
|
||||
reader.getFilename()
|
||||
);
|
||||
@ -210,7 +209,7 @@ static std::shared_ptr<UINode> readLabel(UiXmlReader& reader, xml::xmlelement el
|
||||
}
|
||||
if (element->has("supplier")) {
|
||||
auto supplier = scripting::create_wstring_supplier(
|
||||
reader.getEnvironment().getId(),
|
||||
reader.getEnvironment(),
|
||||
element->attr("supplier").getText(),
|
||||
reader.getFilename()
|
||||
);
|
||||
@ -264,7 +263,7 @@ static std::shared_ptr<UINode> readCheckBox(UiXmlReader& reader, xml::xmlelement
|
||||
|
||||
if (element->has("consumer")) {
|
||||
auto consumer = scripting::create_bool_consumer(
|
||||
reader.getEnvironment().getId(),
|
||||
reader.getEnvironment(),
|
||||
element->attr("consumer").getText(),
|
||||
reader.getFilename()
|
||||
);
|
||||
@ -273,7 +272,7 @@ static std::shared_ptr<UINode> readCheckBox(UiXmlReader& reader, xml::xmlelement
|
||||
|
||||
if (element->has("supplier")) {
|
||||
auto supplier = scripting::create_bool_supplier(
|
||||
reader.getEnvironment().getId(),
|
||||
reader.getEnvironment(),
|
||||
element->attr("supplier").getText(),
|
||||
reader.getFilename()
|
||||
);
|
||||
@ -299,7 +298,7 @@ static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, xml::xmlelement
|
||||
|
||||
if (element->has("consumer")) {
|
||||
auto consumer = scripting::create_wstring_consumer(
|
||||
reader.getEnvironment().getId(),
|
||||
reader.getEnvironment(),
|
||||
element->attr("consumer").getText(),
|
||||
reader.getFilename()
|
||||
);
|
||||
@ -308,7 +307,7 @@ static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, xml::xmlelement
|
||||
|
||||
if (element->has("supplier")) {
|
||||
auto supplier = scripting::create_wstring_supplier(
|
||||
reader.getEnvironment().getId(),
|
||||
reader.getEnvironment(),
|
||||
element->attr("supplier").getText(),
|
||||
reader.getFilename()
|
||||
);
|
||||
@ -322,7 +321,7 @@ static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, xml::xmlelement
|
||||
}
|
||||
if (element->has("validator")) {
|
||||
auto validator = scripting::create_wstring_validator(
|
||||
reader.getEnvironment().getId(),
|
||||
reader.getEnvironment(),
|
||||
element->attr("validator").getText(),
|
||||
reader.getFilename()
|
||||
);
|
||||
@ -348,7 +347,7 @@ static std::shared_ptr<UINode> readTrackBar(UiXmlReader& reader, xml::xmlelement
|
||||
_readUINode(reader, element, *bar);
|
||||
if (element->has("consumer")) {
|
||||
auto consumer = scripting::create_number_consumer(
|
||||
reader.getEnvironment().getId(),
|
||||
reader.getEnvironment(),
|
||||
element->attr("consumer").getText(),
|
||||
reader.getFilename()
|
||||
);
|
||||
@ -356,7 +355,7 @@ static std::shared_ptr<UINode> readTrackBar(UiXmlReader& reader, xml::xmlelement
|
||||
}
|
||||
if (element->has("supplier")) {
|
||||
auto supplier = scripting::create_number_supplier(
|
||||
reader.getEnvironment().getId(),
|
||||
reader.getEnvironment(),
|
||||
element->attr("supplier").getText(),
|
||||
reader.getFilename()
|
||||
);
|
||||
@ -381,7 +380,7 @@ static std::shared_ptr<UINode> readInputBindBox(UiXmlReader& reader, xml::xmlele
|
||||
return bindbox;
|
||||
}
|
||||
|
||||
UiXmlReader::UiXmlReader(const scripting::Environment& env) : env(env) {
|
||||
UiXmlReader::UiXmlReader(const scriptenv& env) : env(env) {
|
||||
contextStack.push("");
|
||||
add("image", readImage);
|
||||
add("label", readLabel);
|
||||
@ -460,6 +459,6 @@ const std::string& UiXmlReader::getFilename() const {
|
||||
return filename;
|
||||
}
|
||||
|
||||
const scripting::Environment& UiXmlReader::getEnvironment() const {
|
||||
const scriptenv& UiXmlReader::getEnvironment() const {
|
||||
return env;
|
||||
}
|
||||
|
||||
@ -9,10 +9,6 @@
|
||||
#include "GUI.h"
|
||||
#include "../../coders/xml.h"
|
||||
|
||||
namespace scripting {
|
||||
class Environment;
|
||||
}
|
||||
|
||||
namespace gui {
|
||||
class UiXmlReader;
|
||||
|
||||
@ -23,9 +19,9 @@ namespace gui {
|
||||
std::unordered_set<std::string> ignored;
|
||||
std::stack<std::string> contextStack;
|
||||
std::string filename;
|
||||
const scripting::Environment& env;
|
||||
const scriptenv& env;
|
||||
public:
|
||||
UiXmlReader(const scripting::Environment& env);
|
||||
UiXmlReader(const scriptenv& env);
|
||||
|
||||
void add(const std::string& tag, uinode_reader reader);
|
||||
bool hasReader(const std::string& tag) const;
|
||||
@ -56,7 +52,7 @@ namespace gui {
|
||||
);
|
||||
|
||||
const std::string& getContext() const;
|
||||
const scripting::Environment& getEnvironment() const;
|
||||
const scriptenv& getEnvironment() const;
|
||||
const std::string& getFilename() const;
|
||||
};
|
||||
}
|
||||
|
||||
@ -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_
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
#ifndef VOX_TYPEDEFS_H
|
||||
#define VOX_TYPEDEFS_H
|
||||
#ifndef TYPEDEFS_H_
|
||||
#define TYPEDEFS_H_
|
||||
|
||||
#include <memory>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <variant>
|
||||
|
||||
using scriptenv = std::shared_ptr<int>;
|
||||
|
||||
/// @brief dynamic integer type (64 bit signed integer)
|
||||
using integer_t = int64_t;
|
||||
@ -26,4 +28,4 @@ using itemcount_t = uint32_t;
|
||||
using blockstate_t = uint16_t;
|
||||
using light_t = uint16_t;
|
||||
|
||||
#endif
|
||||
#endif // TYPEDEFS_H_
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user