Skip to content

Commit

Permalink
feat(lua): lua systems work (#39)
Browse files Browse the repository at this point in the history
* feat(lua): start scripted systems

* feat(lua): lua systems work

* feat(lua): fix working directory

* feat(lua): more coverage
  • Loading branch information
Milerius authored Sep 21, 2019
1 parent 481407f commit 9ce5251
Show file tree
Hide file tree
Showing 15 changed files with 288 additions and 23 deletions.
6 changes: 4 additions & 2 deletions modules/ecs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ add_library(antara_ecs_shared_sources STATIC)
target_sources(antara_ecs_shared_sources PRIVATE
antara/gaming/ecs/base.system.cpp
antara/gaming/ecs/system.manager.cpp
antara/gaming/ecs/component.position.cpp)
antara/gaming/ecs/component.position.cpp
antara/gaming/ecs/event.add.base.system.cpp)
target_include_directories(antara_ecs_shared_sources PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(antara_ecs_shared_sources PUBLIC antara::core EnTT strong_type expected range-v3 antara::default_settings antara::timer antara::event doom::meta)
add_library(antara::ecs ALIAS antara_ecs_shared_sources)
Expand All @@ -14,7 +15,8 @@ target_sources(antara_ecs_tests PUBLIC
antara/gaming/ecs/antara.ecs.tests.cpp
antara/gaming/ecs/antara.ecs.system.tests.cpp
antara/gaming/ecs/antara.ecs.system.manager.tests.cpp
antara/gaming/ecs/antara.ecs.component.position.tests.cpp)
antara/gaming/ecs/antara.ecs.component.position.tests.cpp
antara/gaming/ecs/antara.ecs.event.add.base.system.tests.cpp)
target_link_libraries(antara_ecs_tests PRIVATE doctest PUBLIC antara::ecs)
set_target_properties(antara_ecs_tests
PROPERTIES
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#include <doctest/doctest.h>
#include "antara/gaming/ecs/event.add.base.system.hpp"

namespace antara::gaming::ecs::tests
{
TEST_SUITE ("test event add base system")
{
TEST_CASE ("default constructible")
{
antara::gaming::ecs::event::add_base_system evt{};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ namespace antara::gaming::ecs::tests
manager.create_system<logic_concrete_system>();
CHECK_EQ(manager.nb_systems(), 1u);
CHECK_EQ(manager.nb_systems(logic_concrete_system::get_system_type()), 1u);

//! evt
ecs::event::add_base_system evt(std::make_unique<pre_concrete_system>(registry, dispatcher));
manager.receive_add_base_system(evt);
CHECK_EQ(manager.nb_systems(), 2u);
CHECK(manager.mark_system<pre_concrete_system>());
manager.update();
CHECK_EQ(manager.nb_systems(), 1u);
}

TEST_CASE("remove system")
Expand Down
28 changes: 28 additions & 0 deletions modules/ecs/antara/gaming/ecs/event.add.base.system.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#include <utility> //! std::move
#include "antara/gaming/ecs/event.add.base.system.hpp"

namespace antara::gaming::ecs
{

event::add_base_system::add_base_system(std::unique_ptr<ecs::base_system> system_ptr_) noexcept : system_ptr(
std::move(system_ptr_))
{

}
}
29 changes: 29 additions & 0 deletions modules/ecs/antara/gaming/ecs/event.add.base.system.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#pragma once

#include <memory> // std::unique_ptr
#include "base.system.hpp"

namespace antara::gaming::ecs::event
{
struct add_base_system
{
add_base_system(std::unique_ptr<ecs::base_system> system_ptr_ = nullptr) noexcept;
std::unique_ptr<ecs::base_system> system_ptr{nullptr};
};
}
8 changes: 7 additions & 1 deletion modules/ecs/antara/gaming/ecs/system.manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace antara::gaming::ecs
system_manager::system_manager(entt::registry &registry, entt::dispatcher &dispatcher) noexcept : entity_registry_(
registry), dispatcher_(dispatcher)
{

this->dispatcher_.sink<event::add_base_system>().connect<&system_manager::receive_add_base_system>(*this);
}

std::size_t system_manager::nb_systems(system_type sys_type) const noexcept
Expand Down Expand Up @@ -88,4 +88,10 @@ namespace antara::gaming::ecs
}
return nb_systems_updated;
}

void system_manager::receive_add_base_system(const ecs::event::add_base_system &evt) noexcept
{
ecs::system_type sys_type = evt.system_ptr->get_system_type_rtti();
this->add_system_(std::move(const_cast<event::add_base_system &>(evt).system_ptr), sys_type);
}
}
8 changes: 6 additions & 2 deletions modules/ecs/antara/gaming/ecs/system.manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "antara/gaming/ecs/base.system.hpp"
#include "antara/gaming/ecs/system.hpp"
#include "antara/gaming/ecs/system.type.hpp"
#include "antara/gaming/ecs/event.add.base.system.hpp"
#include "antara/gaming/timer/time.step.hpp"
#include "antara/gaming/event/fatal.error.hpp"

Expand All @@ -49,6 +50,9 @@ namespace antara::gaming::ecs
*/
explicit system_manager(entt::registry &registry, entt::dispatcher &dispatcher) noexcept;

//! Callback
void receive_add_base_system(const ecs::event::add_base_system& evt) noexcept;

/**
* \return number of systems which are successfully updated
* \note This is the function that update your systems.
Expand Down Expand Up @@ -222,7 +226,7 @@ namespace antara::gaming::ecs
const TSystem &system_manager::get_system() const noexcept
{
const auto ret = get_system_<TSystem>().or_else([this](const std::error_code &ec) {
this->dispatcher_.trigger<event::fatal_error>(ec);
this->dispatcher_.trigger<gaming::event::fatal_error>(ec);
});
return (*ret).get();
}
Expand All @@ -231,7 +235,7 @@ namespace antara::gaming::ecs
TSystem &system_manager::get_system() noexcept
{
auto ret = get_system_<TSystem>().or_else([this](const std::error_code &ec) {
this->dispatcher_.trigger<event::fatal_error>(ec);
this->dispatcher_.trigger<gaming::event::fatal_error>(ec);
});
return (*ret).get();
}
Expand Down
7 changes: 3 additions & 4 deletions modules/lua/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ set_target_properties(antara_lua_tests
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/unit_tests"
)

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/lua_assets DESTINATION ${CMAKE_BINARY_DIR}/bin/unit_tests/)
if (WIN32)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/lua_assets DESTINATION ${CMAKE_BINARY_DIR}/bin/unit_tests/${CMAKE_BUILD_TYPE})
endif ()
add_custom_command(TARGET antara_lua_tests PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_CURRENT_SOURCE_DIR}/assets $<TARGET_FILE_DIR:antara_lua_tests>)

target_enable_coverage(antara_lua_tests)
target_enable_tsan(antara_lua_tests)
Expand Down
21 changes: 18 additions & 3 deletions modules/lua/antara/gaming/lua/antara.lua.system.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ namespace antara::gaming::lua::tests
entt::registry entity_registry;
entt::dispatcher dispatcher;
antara::gaming::lua::scripting_system scripting_system{entity_registry, dispatcher,
std::filesystem::current_path() / "lua_assets" / "scripts"};
std::filesystem::current_path() / "lua_assets" / "scripts",
std::filesystem::current_path() / "lua_assets" / "scripts" / "systems"};
auto &state = scripting_system.get_state();
TEST_CASE ("register a type")
{
Expand Down Expand Up @@ -163,11 +164,25 @@ namespace antara::gaming::lua::tests

TEST_CASE("update entities")
{
bool res = scripting_system.execute_safe_function("my_get_res", "player_table");
bool res = scripting_system.execute_safe_function("my_get_res", "player_table").value();
CHECK_FALSE(res);
scripting_system.update();
res = scripting_system.execute_safe_function("my_get_res", "player_table");
res = scripting_system.execute_safe_function("my_get_res", "player_table").value();
CHECK(res);
entity_registry.reset();
}

TEST_CASE("load scripted system")
{
CHECK(scripting_system.load_scripted_system("pre_update_system.lua"));
}

TEST_CASE("call function")
{
scripting_system.execute_safe_function("print", "");
scripting_system.execute_safe_function("nonexistent", "");
scripting_system.execute_safe_function("foo", "entity_registry");
scripting_system.execute_safe_function("my_get_res", "player_table", 1);
}
}
}
73 changes: 73 additions & 0 deletions modules/lua/antara/gaming/lua/details/lua.scripted.system.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#pragma once

#include <utility>
#include <sol/state.hpp>
#include "antara/gaming/ecs/system.hpp"

namespace antara::gaming::lua::details
{
template<typename SystemType>
class scripted_system final : public ecs::system<scripted_system<SystemType>, SystemType>
{
public:
using TSystem = ecs::system<scripted_system<SystemType>, SystemType>;

scripted_system(entt::registry &entity_registry, entt::dispatcher &dispatcher, std::string table_name,
sol::state &state) noexcept
: TSystem::system(
entity_registry, dispatcher), table_name_(std::move(table_name)), state_(state)
{
safe_function_("on_construct");
}

~scripted_system() noexcept final
{
safe_function_("on_destruct");
}

void update() noexcept final
{
safe_function_("update");
}

private:
template<typename ... Args>
void safe_function_(const std::string &function, Args &&... args) noexcept
{
try {
sol::optional<sol::function> f = state_[table_name_][function];
if (f && f.value() != sol::lua_nil) {
f.value()(std::forward<Args>(args)...);
}
}
catch (const std::exception &error) {
std::cerr << error.what() << std::endl;
}
}

std::string table_name_;
sol::state &state_;
};

using lua_post_scripted_system = scripted_system<ecs::st_system_post_update>;
using lua_pre_scripted_system = scripted_system<ecs::st_system_pre_update>;
using lua_logic_scripted_system = scripted_system<ecs::st_system_logic_update>;
}

REFL_AUTO(template((typename SystemType), (antara::gaming::lua::details::scripted_system<SystemType>)))
41 changes: 38 additions & 3 deletions modules/lua/antara/gaming/lua/lua.system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
#include "antara/gaming/core/version.hpp"
#include "antara/gaming/core/reflection.entity.registry.hpp"
#include "antara/gaming/ecs/all.components.hpp"
#include "antara/gaming/ecs/event.add.base.system.hpp"
#include "antara/gaming/input/keyboard.hpp"
#include "antara/gaming/lua/lua.system.hpp"
#include "antara/gaming/lua/component.lua.hpp"
#include "antara/gaming/lua/details/lua.scripted.system.hpp"

namespace antara::gaming::lua
{
Expand All @@ -31,8 +33,10 @@ namespace antara::gaming::lua
}

scripting_system::scripting_system(entt::registry &entity_registry, entt::dispatcher &dispatcher,
std::filesystem::path script_directory) noexcept : system(
entity_registry, dispatcher), directory_path_(std::move(script_directory))
std::filesystem::path script_directory,
std::filesystem::path systems_directory) noexcept : system(
entity_registry, dispatcher), directory_path_(std::move(script_directory)), systems_directory_path_(
std::move(systems_directory))
{
lua_state_.open_libraries();
register_entity_registry();
Expand Down Expand Up @@ -170,7 +174,7 @@ namespace antara::gaming::lua
};

lua_state_["entity_registry"]["for_each_runtime"] = [](entt::registry &self,
const std::vector<entt::component>& components,
std::vector<entt::component> components,
sol::function functor) {
return self.runtime_view(std::cbegin(components), std::cend(components)).each(
[func = std::move(functor)](auto entity) {
Expand Down Expand Up @@ -214,4 +218,35 @@ namespace antara::gaming::lua
});
return res;
}

bool scripting_system::load_scripted_system(const std::string &script_name) noexcept
{
bool res = load_script(script_name, systems_directory_path_);
if (not res)
return false;
auto table_name = std::filesystem::path(script_name).stem().string() + "_table";
ecs::system_type sys_type = this->lua_state_[table_name]["system_type"];
switch (sys_type) {
case ecs::pre_update:
this->dispatcher_.trigger<ecs::event::add_base_system>(
std::make_unique<details::lua_pre_scripted_system>(entity_registry_, dispatcher_, table_name,
this->lua_state_));
break;
case ecs::logic_update:
this->dispatcher_.trigger<ecs::event::add_base_system>(
std::make_unique<details::lua_logic_scripted_system>(entity_registry_, dispatcher_,
table_name, this->lua_state_));
break;
case ecs::post_update:
this->dispatcher_.trigger<ecs::event::add_base_system>(
std::make_unique<details::lua_post_scripted_system>(entity_registry_, dispatcher_, table_name,
this->lua_state_));
break;
case ecs::size:
break;
default:
break;
}
return res;
}
}
Loading

0 comments on commit 9ce5251

Please sign in to comment.