mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-21 01:49:44 +00:00
Merge branch 'MihailRis:main' into main
This commit is contained in:
commit
469edd1e9d
66 changed files with 1717 additions and 538 deletions
|
|
@ -1,6 +1,6 @@
|
|||
#include "objects/rigging.hpp"
|
||||
#include "libentity.hpp"
|
||||
|
||||
#include "animation/rigging.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "graphics/render/WorldRenderer.hpp"
|
||||
#include "graphics/render/NamedSkeletons.hpp"
|
||||
|
|
@ -42,7 +42,7 @@ static int l_get_model(lua::State* L) {
|
|||
auto& rigConfig = *skeleton->config;
|
||||
auto index = index_range_check(*skeleton, lua::tointeger(L, 2));
|
||||
const auto& modelOverride = skeleton->modelOverrides[index];
|
||||
if (modelOverride.model) {
|
||||
if (modelOverride.model.expired()) {
|
||||
return lua::pushstring(L, modelOverride.name);
|
||||
}
|
||||
return lua::pushstring(L, rigConfig.getBones()[index]->model.name);
|
||||
|
|
@ -55,9 +55,9 @@ static int l_set_model(lua::State* L) {
|
|||
auto index = index_range_check(*skeleton, lua::tointeger(L, 2));
|
||||
auto& modelOverride = skeleton->modelOverrides[index];
|
||||
if (lua::isnoneornil(L, 3)) {
|
||||
modelOverride = {"", nullptr, true};
|
||||
modelOverride = {"", {}, true};
|
||||
} else {
|
||||
modelOverride = {lua::require_string(L, 3), nullptr, true};
|
||||
modelOverride = {lua::require_string(L, 3), {}, true};
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
|
@ -66,19 +66,34 @@ static int l_set_model(lua::State* L) {
|
|||
static int l_get_matrix(lua::State* L) {
|
||||
if (auto skeleton = get_skeleton(L)) {
|
||||
auto index = index_range_check(*skeleton, lua::tointeger(L, 2));
|
||||
return lua::pushmat4(L, skeleton->pose.matrices[index]);
|
||||
if (lua::istable(L, 3)) {
|
||||
lua::setmat4(L, 3, skeleton->pose.matrices[index]);
|
||||
} else {
|
||||
return lua::pushmat4(L, skeleton->pose.matrices[index]);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_set_matrix(lua::State* L) {
|
||||
if (auto skeleton = get_skeleton(L)) {
|
||||
if (lua::isnoneornil(L, 2)) {
|
||||
return 0;
|
||||
}
|
||||
auto index = index_range_check(*skeleton, lua::tointeger(L, 2));
|
||||
skeleton->pose.matrices[index] = lua::tomat4(L, 3);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_reset_pose(lua::State* L) {
|
||||
if (auto skeleton = get_skeleton(L)) {
|
||||
auto& pose = skeleton->pose;
|
||||
std::fill(pose.matrices.begin(), pose.matrices.end(), glm::mat4(1.0f));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_get_texture(lua::State* L) {
|
||||
if (auto skeleton = get_skeleton(L)) {
|
||||
const auto& found = skeleton->textures.find(lua::require_string(L, 2));
|
||||
|
|
@ -169,6 +184,7 @@ const luaL_Reg skeletonlib[] = {
|
|||
{"set_model", lua::wrap<l_set_model>},
|
||||
{"get_matrix", lua::wrap<l_get_matrix>},
|
||||
{"set_matrix", lua::wrap<l_set_matrix>},
|
||||
{"reset_pose", lua::wrap<l_reset_pose>},
|
||||
{"get_texture", lua::wrap<l_get_texture>},
|
||||
{"set_texture", lua::wrap<l_set_texture>},
|
||||
{"index", lua::wrap<l_index>},
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
#include "graphics/commons/Model.hpp"
|
||||
#include "graphics/core/Atlas.hpp"
|
||||
#include "graphics/core/Texture.hpp"
|
||||
#include "io/io.hpp"
|
||||
#include "util/Buffer.hpp"
|
||||
|
||||
using namespace scripting;
|
||||
|
|
@ -66,6 +67,20 @@ static int l_load_texture(lua::State* L) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int l_parse_animation(lua::State* L) {
|
||||
auto format = lua::require_lstring(L, 1);
|
||||
auto string = lua::require_lstring(L, 2);
|
||||
std::string name = lua::require_string(L, 3);
|
||||
|
||||
if (format != "vca") {
|
||||
throw std::runtime_error(
|
||||
"unknown format " + util::quote(std::string(format))
|
||||
);
|
||||
}
|
||||
scripting::load_vca_animation(name, &string, name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_parse_model(lua::State* L) {
|
||||
auto& assets = engine->requireAssets();
|
||||
|
||||
|
|
@ -161,6 +176,7 @@ static int l_to_canvas(lua::State* L) {
|
|||
const luaL_Reg assetslib[] = {
|
||||
{"request_texture", lua::wrap<l_request_texture>},
|
||||
{"load_texture", lua::wrap<l_load_texture>},
|
||||
{"parse_animation", lua::wrap<l_parse_animation>},
|
||||
{"parse_model", lua::wrap<l_parse_model>},
|
||||
{"to_canvas", lua::wrap<l_to_canvas>},
|
||||
{nullptr, nullptr}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,20 @@
|
|||
#include "libentity.hpp"
|
||||
|
||||
#include "animation/rigging.hpp"
|
||||
#include "assets/Assets.hpp"
|
||||
#include "content/Content.hpp"
|
||||
#include "content/ContentPack.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "engine/EnginePaths.hpp"
|
||||
#include "objects/Entities.hpp"
|
||||
#include "objects/EntityDef.hpp"
|
||||
#include "objects/Entity.hpp"
|
||||
#include "objects/Rigidbody.hpp"
|
||||
#include "objects/EntityDef.hpp"
|
||||
#include "objects/Player.hpp"
|
||||
#include "objects/rigging.hpp"
|
||||
#include "objects/Rigidbody.hpp"
|
||||
#include "physics/Hitbox.hpp"
|
||||
#include "voxels/Chunks.hpp"
|
||||
#include "voxels/Block.hpp"
|
||||
#include "voxels/blocks_agent.hpp"
|
||||
#include "voxels/Chunks.hpp"
|
||||
#include "window/Camera.hpp"
|
||||
|
||||
#include <set>
|
||||
|
|
@ -119,6 +119,10 @@ static int l_set_skeleton(lua::State* L) {
|
|||
return 0;
|
||||
}
|
||||
if (auto entity = get_entity(L, 1)) {
|
||||
if (lua::isnil(L, 2)) {
|
||||
entity->setRig(nullptr);
|
||||
return 0;
|
||||
}
|
||||
std::string skeletonName = lua::require_string(L, 2);
|
||||
auto rigConfig = assets->getShared<rigging::SkeletonConfig>(skeletonName);
|
||||
if (rigConfig == nullptr) {
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ static void create_libs(State* L, StateType stateType) {
|
|||
createtable(L, 0, 0);
|
||||
pushvalue(L, -1);
|
||||
setglobal(L, "__vc_internals");
|
||||
setregistry(L, "internals");
|
||||
setregistry(L, lua::INTERNALS_TABLE);
|
||||
|
||||
if (stateType == StateType::SCRIPT) {
|
||||
getregistry(L, "app");
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ namespace lua {
|
|||
inline std::string CHUNKS_TABLE = "$C"; // precompiled lua chunks
|
||||
inline std::string PACK_ENVS_TABLE = "$P";
|
||||
inline std::string ENVS_TABLE = "$E";
|
||||
inline std::string INTERNALS_TABLE = "$I";
|
||||
extern std::unordered_map<std::type_index, std::string> usertypeNames;
|
||||
int userdata_destructor(lua::State* L);
|
||||
|
||||
|
|
@ -635,6 +636,13 @@ namespace lua {
|
|||
int call(lua::State*, int argc, int nresults = -1);
|
||||
int call_nothrow(lua::State*, int argc, int nresults = 1);
|
||||
|
||||
inline void call_internal(lua::State* L, const std::string& name) {
|
||||
if (lua::get_from_registry(L, lua::INTERNALS_TABLE, name, true)) {
|
||||
lua::call_nothrow(L, 0, 0);
|
||||
pop(L);
|
||||
}
|
||||
}
|
||||
|
||||
inline int eval(
|
||||
lua::State* L,
|
||||
int env,
|
||||
|
|
|
|||
|
|
@ -65,14 +65,13 @@ int scripting::load_script(
|
|||
return lua::execute(lua::get_main_state(), env, src, fileName);
|
||||
}
|
||||
|
||||
void scripting::initialize(Engine* engine) {
|
||||
scripting::engine = engine;
|
||||
scripting::content_control = &engine->getContentControl();
|
||||
lua::initialize(engine->getPaths(), engine->getCoreParameters());
|
||||
void scripting::initialize(Engine& engine) {
|
||||
scripting::engine = &engine;
|
||||
scripting::content_control = &engine.getContentControl();
|
||||
lua::initialize(engine.getPaths(), engine.getCoreParameters());
|
||||
|
||||
load_script(io::path("stdlib.lua"), true);
|
||||
load_script(io::path("classes.lua"), true);
|
||||
load_script(io::path("internal_events.lua"), true);
|
||||
}
|
||||
|
||||
class LuaCoroutine : public Process {
|
||||
|
|
@ -244,9 +243,7 @@ std::unique_ptr<Process> scripting::start_app_script(const io::path& script) {
|
|||
|
||||
void scripting::process_post_runnables() {
|
||||
auto L = lua::get_main_state();
|
||||
if (lua::getglobal(L, "__vc__process_post_runnables")) {
|
||||
lua::call_nothrow(L, 0, 0);
|
||||
}
|
||||
lua::call_internal(L, "process_post_runnables");
|
||||
}
|
||||
|
||||
template <class T, typename IdType>
|
||||
|
|
@ -263,6 +260,14 @@ static int push_properties_tables(
|
|||
return 1;
|
||||
}
|
||||
|
||||
void scripting::on_assets_loading() {
|
||||
lua::call_internal(lua::get_main_state(), "backup_and_clear_animation");
|
||||
}
|
||||
|
||||
void scripting::revert_assets_loading() {
|
||||
lua::call_internal(lua::get_main_state(), "restore_animation_backup");
|
||||
}
|
||||
|
||||
void scripting::on_content_load(Content* content) {
|
||||
scripting::content = content;
|
||||
scripting::indices = content->getIndices();
|
||||
|
|
@ -291,13 +296,15 @@ void scripting::on_content_load(Content* content) {
|
|||
|
||||
lua::getregistry(L, "app");
|
||||
lua::setglobal(L, "__vc_app");
|
||||
lua::getregistry(L, "internals");
|
||||
lua::getregistry(L, lua::INTERNALS_TABLE);
|
||||
lua::setglobal(L, "__vc_internals");
|
||||
try {
|
||||
load_script("post_content.lua", true);
|
||||
} catch (const std::exception&) {
|
||||
lua::pushnil(L);
|
||||
lua::setglobal(L, "__vc_app");
|
||||
lua::pushnil(L);
|
||||
lua::setglobal(L, "__vc_internals");
|
||||
throw;
|
||||
}
|
||||
lua::pushnil(L);
|
||||
|
|
@ -319,9 +326,7 @@ void scripting::on_world_load(LevelController* controller) {
|
|||
scripting::controller = controller;
|
||||
|
||||
auto L = lua::get_main_state();
|
||||
if (lua::getglobal(L, "__vc_on_world_open")) {
|
||||
lua::call_nothrow(L, 0, 0);
|
||||
}
|
||||
lua::call_internal(L, "on_world_open");
|
||||
|
||||
for (auto& pack : content_control->getAllContentPacks()) {
|
||||
lua::emit_event(L, pack.id + ":.worldopen", [](auto L) {
|
||||
|
|
@ -334,9 +339,10 @@ void scripting::on_world_load(LevelController* controller) {
|
|||
|
||||
void scripting::on_world_tick(int tps) {
|
||||
auto L = lua::get_main_state();
|
||||
if (lua::getglobal(L, "__vc_on_world_tick")) {
|
||||
if (lua::get_from_registry(L, lua::INTERNALS_TABLE, "on_world_tick", true)) {
|
||||
lua::pushinteger(L, tps);
|
||||
lua::call_nothrow(L, 1, 0);
|
||||
lua::pop(L);
|
||||
}
|
||||
for (auto& pack : content_control->getAllContentPacks()) {
|
||||
lua::emit_event(L, pack.id + ":.worldtick");
|
||||
|
|
@ -348,16 +354,12 @@ void scripting::on_world_save() {
|
|||
for (auto& pack : content_control->getAllContentPacks()) {
|
||||
lua::emit_event(L, pack.id + ":.worldsave");
|
||||
}
|
||||
if (lua::getglobal(L, "__vc_on_world_save")) {
|
||||
lua::call_nothrow(L, 0, 0);
|
||||
}
|
||||
lua::call_internal(L, "on_world_save");
|
||||
}
|
||||
|
||||
void scripting::process_before_quit() {
|
||||
auto L = lua::get_main_state();
|
||||
if (lua::getglobal(L, "__vc_process_before_quit")) {
|
||||
lua::call_nothrow(L, 0, 0);
|
||||
}
|
||||
lua::call_internal(L, "process_before_quit");
|
||||
}
|
||||
|
||||
void scripting::on_world_quit() {
|
||||
|
|
@ -365,9 +367,7 @@ void scripting::on_world_quit() {
|
|||
for (auto& pack : content_control->getAllContentPacks()) {
|
||||
lua::emit_event(L, pack.id + ":.worldquit");
|
||||
}
|
||||
if (lua::getglobal(L, "__vc_on_world_quit")) {
|
||||
lua::call_nothrow(L, 0, 0);
|
||||
}
|
||||
lua::call_internal(L, "on_world_quit");
|
||||
scripting::level = nullptr;
|
||||
scripting::content = nullptr;
|
||||
scripting::indices = nullptr;
|
||||
|
|
@ -842,6 +842,27 @@ void scripting::load_layout_script(
|
|||
lua::pop(L);
|
||||
}
|
||||
|
||||
void scripting::load_vca_animation(
|
||||
const io::path& file,
|
||||
const std::string_view* content,
|
||||
const std::string& identifier
|
||||
) {
|
||||
auto L = lua::get_main_state();
|
||||
if (lua::get_from_registry(L, lua::INTERNALS_TABLE, "load_vca_animation", true)) {
|
||||
lua::pushlstring(L, file.string());
|
||||
if (content) {
|
||||
lua::pushlstring(L, *content);
|
||||
} else {
|
||||
lua::pushnil(L);
|
||||
}
|
||||
lua::pushlstring(L, identifier);
|
||||
if (lua::call(L, 3, 0)) {
|
||||
lua::pop(L);
|
||||
}
|
||||
lua::pop(L);
|
||||
}
|
||||
}
|
||||
|
||||
void scripting::close() {
|
||||
lua::finalize();
|
||||
content = nullptr;
|
||||
|
|
|
|||
|
|
@ -48,7 +48,10 @@ namespace scripting {
|
|||
extern std::ostream* output_stream;
|
||||
extern std::ostream* error_stream;
|
||||
|
||||
void initialize(Engine* engine);
|
||||
void initialize(Engine& engine);
|
||||
|
||||
void on_assets_loading();
|
||||
void revert_assets_loading();
|
||||
|
||||
void on_content_load(Content* content);
|
||||
void on_content_reset();
|
||||
|
|
@ -251,6 +254,12 @@ namespace scripting {
|
|||
const std::string& fileName,
|
||||
UiDocScript& script
|
||||
);
|
||||
|
||||
void load_vca_animation(
|
||||
const io::path& file,
|
||||
const std::string_view* content,
|
||||
const std::string& identifier
|
||||
);
|
||||
|
||||
/// @brief Finalize lua state. Using scripting after will lead to Lua panic
|
||||
void close();
|
||||
|
|
|
|||
|
|
@ -37,6 +37,11 @@ static inline const std::string STDCOMP = "stdcomp";
|
|||
lua::setfield(L, name);
|
||||
lua::pop(L);
|
||||
}
|
||||
if (lua::getfield(L, "ordered_components")) {
|
||||
lua::pushenv(L, id);
|
||||
lua::rawseti(L, lua::objlen(L, -2) + 1);
|
||||
lua::pop(L);
|
||||
}
|
||||
lua::pop(L);
|
||||
|
||||
return std::shared_ptr<int>(new int(id), [=](int* id) { //-V508
|
||||
|
|
|
|||
|
|
@ -47,9 +47,7 @@ void scripting::on_frontend_init(
|
|||
|
||||
load_script("hud_classes.lua");
|
||||
|
||||
if (lua::getglobal(L, "__vc_on_hud_open")) {
|
||||
lua::call_nothrow(L, 0, 0);
|
||||
}
|
||||
lua::call_internal(L, "on_hud_open");
|
||||
|
||||
for (auto& pack : content_control->getAllContentPacks()) {
|
||||
lua::emit_event(
|
||||
|
|
@ -63,12 +61,13 @@ void scripting::on_frontend_init(
|
|||
}
|
||||
|
||||
void scripting::on_frontend_render() {
|
||||
auto L = lua::get_main_state();
|
||||
|
||||
lua::call_internal(L, "on_render");
|
||||
for (auto& pack : content_control->getAllContentPacks()) {
|
||||
lua::emit_event(
|
||||
lua::get_main_state(),
|
||||
pack.id + ":.hudrender",
|
||||
[](lua::State* L) { return 0; }
|
||||
);
|
||||
lua::emit_event(L, pack.id + ":.hudrender", [](lua::State* L) {
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue