update lua chunks sources to the engine format

This commit is contained in:
MihailRis 2024-11-18 14:05:45 +03:00
parent 801650824e
commit e3cb736519
9 changed files with 95 additions and 26 deletions

View File

@ -172,7 +172,9 @@ assetload::postfunc assetload::layout(
return [=](auto assets) {
try {
auto cfg = std::dynamic_pointer_cast<LayoutCfg>(config);
assets->store(UiDocument::read(cfg->env, name, file), name);
assets->store(
UiDocument::read(cfg->env, name, file, "abs:" + file), name
);
} catch (const parsing_error& err) {
throw std::runtime_error(
"failed to parse layout XML '" + file + "':\n" + err.errorLog()

View File

@ -485,7 +485,13 @@ void ContentLoader::loadBlock(
auto scriptfile = folder / fs::path("scripts/" + def.scriptName + ".lua");
if (fs::is_regular_file(scriptfile)) {
scripting::load_block_script(env, full, scriptfile, def.rt.funcsset);
scripting::load_block_script(
env,
full,
scriptfile,
pack->id + ":scripts/" + def.scriptName + ".lua",
def.rt.funcsset
);
}
if (!def.hidden) {
auto& item = builder.items.create(full + BLOCK_ITEM_SUFFIX);
@ -511,7 +517,13 @@ void ContentLoader::loadItem(
auto scriptfile = folder / fs::path("scripts/" + def.scriptName + ".lua");
if (fs::is_regular_file(scriptfile)) {
scripting::load_item_script(env, full, scriptfile, def.rt.funcsset);
scripting::load_item_script(
env,
full,
scriptfile,
pack->id + ":scripts/" + def.scriptName + ".lua",
def.rt.funcsset
);
}
}
@ -720,7 +732,11 @@ void ContentLoader::load() {
fs::path scriptFile = folder / fs::path("scripts/world.lua");
if (fs::is_regular_file(scriptFile)) {
scripting::load_world_script(
env, pack->id, scriptFile, runtime->worldfuncsset
env,
pack->id,
scriptFile,
pack->id + ":scripts/world.lua",
runtime->worldfuncsset
);
}
@ -795,7 +811,11 @@ void ContentLoader::load() {
fs::path componentsDir = folder / fs::u8path("scripts/components");
foreach_file(componentsDir, [this](const fs::path& file) {
auto name = pack->id + ":" + file.stem().u8string();
scripting::load_entity_component(name, file);
scripting::load_entity_component(
name,
file,
pack->id + ":scripts/components/" + file.stem().u8string()
);
});
// Process content.json and load defined content units

View File

@ -53,7 +53,12 @@ scriptenv UiDocument::getEnvironment() const {
return env;
}
std::unique_ptr<UiDocument> UiDocument::read(const scriptenv& penv, const std::string& name, const fs::path& file) {
std::unique_ptr<UiDocument> UiDocument::read(
const scriptenv& penv,
const std::string& name,
const fs::path& file,
const std::string& fileName
) {
const std::string text = files::read_string(file);
auto xmldoc = xml::parse(file.u8string(), text);
@ -69,12 +74,16 @@ std::unique_ptr<UiDocument> UiDocument::read(const scriptenv& penv, const std::s
uidocscript script {};
auto scriptFile = fs::path(file.u8string()+".lua");
if (fs::is_regular_file(scriptFile)) {
scripting::load_layout_script(env, name, scriptFile, script);
scripting::load_layout_script(
env, name, scriptFile, fileName + ".lua", script
);
}
return std::make_unique<UiDocument>(name, script, view, env);
}
std::shared_ptr<gui::UINode> UiDocument::readElement(const fs::path& file) {
auto document = read(nullptr, file.filename().u8string(), file);
std::shared_ptr<gui::UINode> UiDocument::readElement(
const fs::path& file, const std::string& fileName
) {
auto document = read(nullptr, file.filename().u8string(), file, fileName);
return document->getRoot();
}

View File

@ -45,6 +45,13 @@ public:
const uidocscript& getScript() const;
scriptenv getEnvironment() const;
static std::unique_ptr<UiDocument> read(const scriptenv& parent_env, const std::string& name, const fs::path& file);
static std::shared_ptr<gui::UINode> readElement(const fs::path& file);
static std::unique_ptr<UiDocument> read(
const scriptenv& parent_env,
const std::string& name,
const fs::path& file,
const std::string& fileName
);
static std::shared_ptr<gui::UINode> readElement(
const fs::path& file, const std::string& fileName
);
};

View File

@ -62,7 +62,10 @@ gui::page_loader_func menus::create_page_loader(Engine* engine) {
auto fullname = "core:pages/"+name;
auto document_ptr = UiDocument::read(
scripting::get_root_environment(), fullname, file
scripting::get_root_environment(),
fullname,
file,
"core:layout/pages/" + name
);
auto document = document_ptr.get();
engine->getAssets()->store(std::move(document_ptr), fullname);
@ -110,7 +113,7 @@ UiDocument* menus::show(Engine* engine, const std::string& name, std::vector<dv:
auto fullname = "core:layouts/"+name;
auto document_ptr = UiDocument::read(
scripting::get_root_environment(), fullname, file
scripting::get_root_environment(), fullname, file, "core:layouts/"+name
);
auto document = document_ptr.get();
engine->getAssets()->store(std::move(document_ptr), fullname);

View File

@ -144,7 +144,8 @@ State* lua::create_state(const EnginePaths& paths, StateType stateType) {
init_state(L, stateType);
auto resDir = paths.getResourcesFolder();
auto src = files::read_string(resDir / fs::u8path("scripts/stdmin.lua"));
lua::pop(L, lua::execute(L, 0, src, "<stdmin>"));
auto file = resDir / fs::u8path("scripts/stdmin.lua");
auto src = files::read_string(file);
lua::pop(L, lua::execute(L, 0, src, "core:scripts/stdmin.lua"));
return L;
}

View File

@ -44,7 +44,7 @@ void scripting::load_script(const fs::path& name, bool throwable) {
fs::path file = paths->getResourcesFolder() / fs::path("scripts") / name;
std::string src = files::read_string(file);
auto L = lua::get_main_state();
lua::loadbuffer(L, 0, src, file.u8string());
lua::loadbuffer(L, 0, src, "core:scripts/"+name.u8string());
if (throwable) {
lua::call(L, 0, 0);
} else {
@ -53,11 +53,14 @@ void scripting::load_script(const fs::path& name, bool throwable) {
}
int scripting::load_script(
int env, const std::string& type, const fs::path& file
int env,
const std::string& type,
const fs::path& file,
const std::string& fileName
) {
std::string src = files::read_string(file);
logger.info() << "script (" << type << ") " << file.u8string();
return lua::execute(lua::get_main_state(), env, src, file.u8string());
return lua::execute(lua::get_main_state(), env, src, fileName);
}
void scripting::initialize(Engine* engine) {
@ -657,10 +660,11 @@ void scripting::load_block_script(
const scriptenv& senv,
const std::string& prefix,
const fs::path& file,
const std::string& fileName,
block_funcs_set& funcsset
) {
int env = *senv;
lua::pop(lua::get_main_state(), load_script(env, "block", file));
lua::pop(lua::get_main_state(), load_script(env, "block", file, fileName));
funcsset.init = register_event(env, "init", prefix + ".init");
funcsset.update = register_event(env, "on_update", prefix + ".update");
funcsset.randupdate =
@ -677,10 +681,11 @@ void scripting::load_item_script(
const scriptenv& senv,
const std::string& prefix,
const fs::path& file,
const std::string& fileName,
item_funcs_set& funcsset
) {
int env = *senv;
lua::pop(lua::get_main_state(), load_script(env, "item", file));
lua::pop(lua::get_main_state(), load_script(env, "item", file, fileName));
funcsset.init = register_event(env, "init", prefix + ".init");
funcsset.on_use = register_event(env, "on_use", prefix + ".use");
funcsset.on_use_on_block =
@ -690,7 +695,7 @@ void scripting::load_item_script(
}
void scripting::load_entity_component(
const std::string& name, const fs::path& file
const std::string& name, const fs::path& file, const std::string& fileName
) {
auto L = lua::get_main_state();
std::string src = files::read_string(file);
@ -703,10 +708,11 @@ void scripting::load_world_script(
const scriptenv& senv,
const std::string& prefix,
const fs::path& file,
const std::string& fileName,
world_funcs_set& funcsset
) {
int env = *senv;
lua::pop(lua::get_main_state(), load_script(env, "world", file));
lua::pop(lua::get_main_state(), load_script(env, "world", file, fileName));
register_event(env, "init", prefix + ".init");
register_event(env, "on_world_open", prefix + ":.worldopen");
register_event(env, "on_world_tick", prefix + ":.worldtick");
@ -724,11 +730,12 @@ void scripting::load_layout_script(
const scriptenv& senv,
const std::string& prefix,
const fs::path& file,
const std::string& fileName,
uidocscript& script
) {
int env = *senv;
lua::pop(lua::get_main_state(), load_script(env, "layout", file));
lua::pop(lua::get_main_state(), load_script(env, "layout", file, fileName));
script.onopen = register_event(env, "on_open", prefix + ".open");
script.onprogress =
register_event(env, "on_progress", prefix + ".progress");

View File

@ -125,11 +125,13 @@ namespace scripting {
/// @param env environment
/// @param prefix pack id
/// @param file item script file
/// @param fileName script file path using the engine format
/// @param funcsset block callbacks set
void load_block_script(
const scriptenv& env,
const std::string& prefix,
const fs::path& file,
const std::string& fileName,
block_funcs_set& funcsset
);
@ -137,15 +139,25 @@ namespace scripting {
/// @param env environment
/// @param prefix pack id
/// @param file item script file
/// @param fileName script file path using the engine format
/// @param funcsset item callbacks set
void load_item_script(
const scriptenv& env,
const std::string& prefix,
const fs::path& file,
const std::string& fileName,
item_funcs_set& funcsset
);
void load_entity_component(const std::string& name, const fs::path& file);
/// @brief Load component script
/// @param name component full name (packid:name)
/// @param file component script file path
/// @param fileName script file path using the engine format
void load_entity_component(
const std::string& name,
const fs::path& file,
const std::string& fileName
);
std::unique_ptr<GeneratorScript> load_generator(
const GeneratorDef& def,
@ -157,10 +169,12 @@ namespace scripting {
/// @param env environment
/// @param packid content-pack id
/// @param file script file path
/// @param fileName script file path using the engine format
void load_world_script(
const scriptenv& env,
const std::string& packid,
const fs::path& file,
const std::string& fileName,
world_funcs_set& funcsset
);
@ -168,11 +182,13 @@ namespace scripting {
/// @param env environment
/// @param prefix pack id
/// @param file item script file
/// @param fileName script file path using the engine format
/// @param script document script info
void load_layout_script(
const scriptenv& env,
const std::string& prefix,
const fs::path& file,
const std::string& fileName,
uidocscript& script
);

View File

@ -6,6 +6,10 @@
namespace scripting {
void load_script(const std::filesystem::path& name, bool throwable);
[[nodiscard]]
int load_script(int env, const std::string& type, const std::filesystem::path& file);
[[nodiscard]] int load_script(
int env,
const std::string& type,
const std::filesystem::path& file,
const std::string& fileName
);
}