add block.reload_script, item.reload_script
This commit is contained in:
parent
1bb25e99c6
commit
4400f7fbfb
@ -121,6 +121,14 @@ public:
|
|||||||
return *found->second;
|
return *found->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
T& require(const std::string& id) {
|
||||||
|
const auto& found = defs.find(id);
|
||||||
|
if (found == defs.end()) {
|
||||||
|
throw std::runtime_error("missing content unit " + id);
|
||||||
|
}
|
||||||
|
return *found->second;
|
||||||
|
}
|
||||||
|
|
||||||
const auto& getDefs() const {
|
const auto& getDefs() const {
|
||||||
return defs;
|
return defs;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -853,26 +853,40 @@ void ContentLoader::load() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
static void load_scripts(Content& content, ContentUnitDefs<T>& units) {
|
static void load_script(const Content& content, T& def) {
|
||||||
for (const auto& [name, def] : units.getDefs()) {
|
const auto& name = def.name;
|
||||||
size_t pos = name.find(':');
|
size_t pos = name.find(':');
|
||||||
if (pos == std::string::npos) {
|
if (pos == std::string::npos) {
|
||||||
throw std::runtime_error("invalid content unit name");
|
throw std::runtime_error("invalid content unit name");
|
||||||
}
|
|
||||||
const auto runtime = content.getPackRuntime(name.substr(0, pos));
|
|
||||||
const auto& pack = runtime->getInfo();
|
|
||||||
const auto& folder = pack.folder;
|
|
||||||
auto scriptfile = folder / ("scripts/" + def->scriptName + ".lua");
|
|
||||||
if (io::is_regular_file(scriptfile)) {
|
|
||||||
scripting::load_content_script(
|
|
||||||
runtime->getEnvironment(),
|
|
||||||
name,
|
|
||||||
scriptfile,
|
|
||||||
def->scriptFile,
|
|
||||||
def->rt.funcsset
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
const auto runtime = content.getPackRuntime(name.substr(0, pos));
|
||||||
|
const auto& pack = runtime->getInfo();
|
||||||
|
const auto& folder = pack.folder;
|
||||||
|
auto scriptfile = folder / ("scripts/" + def.scriptName + ".lua");
|
||||||
|
if (io::is_regular_file(scriptfile)) {
|
||||||
|
scripting::load_content_script(
|
||||||
|
runtime->getEnvironment(),
|
||||||
|
name,
|
||||||
|
scriptfile,
|
||||||
|
def.scriptFile,
|
||||||
|
def.rt.funcsset
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
static void load_scripts(const Content& content, ContentUnitDefs<T>& units) {
|
||||||
|
for (const auto& [_, def] : units.getDefs()) {
|
||||||
|
load_script(content, *def);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ContentLoader::reloadScript(const Content& content, Block& block) {
|
||||||
|
load_script(content, block);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ContentLoader::reloadScript(const Content& content, ItemDef& item) {
|
||||||
|
load_script(content, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ContentLoader::loadScripts(Content& content) {
|
void ContentLoader::loadScripts(Content& content) {
|
||||||
|
|||||||
@ -77,4 +77,6 @@ public:
|
|||||||
void load();
|
void load();
|
||||||
|
|
||||||
static void loadScripts(Content& content);
|
static void loadScripts(Content& content);
|
||||||
|
static void reloadScript(const Content& content, Block& block);
|
||||||
|
static void reloadScript(const Content& content, ItemDef& item);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -484,6 +484,10 @@ const Content* Engine::getContent() const {
|
|||||||
return content.get();
|
return content.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Content* Engine::getWriteableContent() {
|
||||||
|
return content.get();
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<ContentPack> Engine::getAllContentPacks() {
|
std::vector<ContentPack> Engine::getAllContentPacks() {
|
||||||
auto packs = getContentPacks();
|
auto packs = getContentPacks();
|
||||||
packs.insert(packs.begin(), ContentPack::createCore(paths));
|
packs.insert(packs.begin(), ContentPack::createCore(paths));
|
||||||
|
|||||||
@ -151,6 +151,8 @@ public:
|
|||||||
/// @brief Get current Content instance
|
/// @brief Get current Content instance
|
||||||
const Content* getContent() const;
|
const Content* getContent() const;
|
||||||
|
|
||||||
|
Content* getWriteableContent();
|
||||||
|
|
||||||
/// @brief Get selected content packs
|
/// @brief Get selected content packs
|
||||||
std::vector<ContentPack>& getContentPacks();
|
std::vector<ContentPack>& getContentPacks();
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
#include "content/Content.hpp"
|
#include "content/Content.hpp"
|
||||||
|
#include "content/ContentLoader.hpp"
|
||||||
#include "lighting/Lighting.hpp"
|
#include "lighting/Lighting.hpp"
|
||||||
#include "logic/BlocksController.hpp"
|
#include "logic/BlocksController.hpp"
|
||||||
#include "logic/LevelController.hpp"
|
#include "logic/LevelController.hpp"
|
||||||
@ -12,6 +13,7 @@
|
|||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
#include "maths/voxmaths.hpp"
|
#include "maths/voxmaths.hpp"
|
||||||
#include "data/StructLayout.hpp"
|
#include "data/StructLayout.hpp"
|
||||||
|
#include "engine/Engine.hpp"
|
||||||
#include "api_lua.hpp"
|
#include "api_lua.hpp"
|
||||||
|
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
@ -617,6 +619,17 @@ static int l_set_field(lua::State* L) {
|
|||||||
return set_field(L, dst, *field, index, dataStruct, value);
|
return set_field(L, dst, *field, index, dataStruct, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int l_reload_script(lua::State* L) {
|
||||||
|
auto name = lua::require_string(L, 1);
|
||||||
|
if (content == nullptr) {
|
||||||
|
throw std::runtime_error("content is not initialized");
|
||||||
|
}
|
||||||
|
auto& writeableContent = *engine->getWriteableContent();
|
||||||
|
auto& def = writeableContent.blocks.require(name);
|
||||||
|
ContentLoader::reloadScript(writeableContent, def);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
const luaL_Reg blocklib[] = {
|
const luaL_Reg blocklib[] = {
|
||||||
{"index", lua::wrap<l_index>},
|
{"index", lua::wrap<l_index>},
|
||||||
{"name", lua::wrap<l_get_def>},
|
{"name", lua::wrap<l_get_def>},
|
||||||
@ -652,5 +665,6 @@ const luaL_Reg blocklib[] = {
|
|||||||
{"decompose_state", lua::wrap<l_decompose_state>},
|
{"decompose_state", lua::wrap<l_decompose_state>},
|
||||||
{"get_field", lua::wrap<l_get_field>},
|
{"get_field", lua::wrap<l_get_field>},
|
||||||
{"set_field", lua::wrap<l_set_field>},
|
{"set_field", lua::wrap<l_set_field>},
|
||||||
|
{"reload_script", lua::wrap<l_reload_script>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
#include "content/Content.hpp"
|
#include "content/Content.hpp"
|
||||||
|
#include "content/ContentLoader.hpp"
|
||||||
#include "items/ItemDef.hpp"
|
#include "items/ItemDef.hpp"
|
||||||
#include "api_lua.hpp"
|
#include "api_lua.hpp"
|
||||||
|
#include "engine/Engine.hpp"
|
||||||
|
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
|
|
||||||
@ -87,6 +89,17 @@ static int l_uses(lua::State* L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int l_reload_script(lua::State* L) {
|
||||||
|
auto name = lua::require_string(L, 1);
|
||||||
|
if (content == nullptr) {
|
||||||
|
throw std::runtime_error("content is not initialized");
|
||||||
|
}
|
||||||
|
auto& writeableContent = *engine->getWriteableContent();
|
||||||
|
auto& def = writeableContent.items.require(name);
|
||||||
|
ContentLoader::reloadScript(writeableContent, def);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
const luaL_Reg itemlib[] = {
|
const luaL_Reg itemlib[] = {
|
||||||
{"index", lua::wrap<l_index>},
|
{"index", lua::wrap<l_index>},
|
||||||
{"name", lua::wrap<l_name>},
|
{"name", lua::wrap<l_name>},
|
||||||
@ -98,5 +111,6 @@ const luaL_Reg itemlib[] = {
|
|||||||
{"model_name", lua::wrap<l_model_name>},
|
{"model_name", lua::wrap<l_model_name>},
|
||||||
{"emission", lua::wrap<l_emission>},
|
{"emission", lua::wrap<l_emission>},
|
||||||
{"uses", lua::wrap<l_uses>},
|
{"uses", lua::wrap<l_uses>},
|
||||||
|
{"reload_script", lua::wrap<l_reload_script>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user