diff --git a/src/assets/AssetsLoader.cpp b/src/assets/AssetsLoader.cpp index 85dc5641..6f2e64fc 100644 --- a/src/assets/AssetsLoader.cpp +++ b/src/assets/AssetsLoader.cpp @@ -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"); diff --git a/src/assets/AssetsLoader.h b/src/assets/AssetsLoader.h index e03b5871..a32e03bf 100644 --- a/src/assets/AssetsLoader.h +++ b/src/assets/AssetsLoader.h @@ -3,6 +3,7 @@ #include "Assets.h" #include "../interfaces/Task.h" +#include "../typedefs.h" #include "../delegates.h" #include @@ -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 { diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 60557ef0..20043c76 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -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(); diff --git a/src/content/ContentLoader.h b/src/content/ContentLoader.h index b176b861..df968565 100644 --- a/src/content/ContentLoader.h +++ b/src/content/ContentLoader.h @@ -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); diff --git a/src/content/ContentPack.cpp b/src/content/ContentPack.cpp index 15509dee..2cd2fc72 100644 --- a/src/content/ContentPack.cpp +++ b/src/content/ContentPack.cpp @@ -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 env + scriptenv env ) : info(info), env(std::move(env)) { } diff --git a/src/content/ContentPack.h b/src/content/ContentPack.h index a9cfd875..8571b34f 100644 --- a/src/content/ContentPack.h +++ b/src/content/ContentPack.h @@ -1,6 +1,8 @@ #ifndef CONTENT_CONTENT_PACK_H_ #define CONTENT_CONTENT_PACK_H_ +#include "../typedefs.h" + #include #include #include @@ -85,11 +87,11 @@ struct ContentPackStats { class ContentPackRuntime { ContentPack info; ContentPackStats stats {}; - std::unique_ptr env; + scriptenv env; public: ContentPackRuntime( ContentPack info, - std::unique_ptr 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; } }; diff --git a/src/engine.cpp b/src/engine.cpp index 052c3e90..3821fafa 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -185,6 +185,7 @@ Engine::~Engine() { } content.reset(); assets.reset(); + gui.reset(); audio::close(); scripting::close(); Window::terminate(); diff --git a/src/frontend/InventoryView.cpp b/src/frontend/InventoryView.cpp index 869a227e..9096e33a 100644 --- a/src/frontend/InventoryView.cpp +++ b/src/frontend/InventoryView.cpp @@ -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) { 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) { diff --git a/src/frontend/UiDocument.cpp b/src/frontend/UiDocument.cpp index 217da699..e241ebcf 100644 --- a/src/frontend/UiDocument.cpp +++ b/src/frontend/UiDocument.cpp @@ -10,8 +10,8 @@ UiDocument::UiDocument( std::string id, uidocscript script, std::shared_ptr root, - std::unique_ptr 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::read(int penv, std::string name, fs::path file) { +std::unique_ptr 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(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::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(name, script, view, std::move(env)); + return std::make_unique(name, script, view, env); } std::shared_ptr UiDocument::readElement(fs::path file) { - auto document = read(-1, file.filename().u8string(), file); + auto document = read(nullptr, file.filename().u8string(), file); return document->getRoot(); } diff --git a/src/frontend/UiDocument.h b/src/frontend/UiDocument.h index 8fa6dd6c..b71a1747 100644 --- a/src/frontend/UiDocument.h +++ b/src/frontend/UiDocument.h @@ -1,7 +1,7 @@ #ifndef FRONTEND_UI_DOCUMENT_H_ #define FRONTEND_UI_DOCUMENT_H_ -#include "../logic/scripting/Environment.h" +#include "../typedefs.h" #include #include @@ -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 root; - std::unique_ptr env; + scriptenv env; public: UiDocument( std::string id, uidocscript script, std::shared_ptr root, - std::unique_ptr env + scriptenv env ); void rebuildIndices(); @@ -48,9 +43,9 @@ public: const std::shared_ptr getRoot() const; const std::shared_ptr get(const std::string& id) const; const uidocscript& getScript() const; - int getEnvironment() const; + scriptenv getEnvironment() const; - static std::unique_ptr read(int env, std::string name, fs::path file); + static std::unique_ptr read(scriptenv parent_env, std::string name, fs::path file); static std::shared_ptr readElement(fs::path file); }; diff --git a/src/frontend/menu.cpp b/src/frontend/menu.cpp index 8797c56a..edd81a63 100644 --- a/src/frontend/menu.cpp +++ b/src/frontend/menu.cpp @@ -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(); diff --git a/src/frontend/screens.cpp b/src/frontend/screens.cpp index 9fd0ad4d..5e13a65e 100644 --- a/src/frontend/screens.cpp +++ b/src/frontend/screens.cpp @@ -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()); diff --git a/src/graphics/ui/gui_util.cpp b/src/graphics/ui/gui_util.cpp index abd63dd1..40a17e88 100644 --- a/src/graphics/ui/gui_util.cpp +++ b/src/graphics/ui/gui_util.cpp @@ -6,7 +6,7 @@ #include -#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