diff --git a/src/scripting/include/pw/scripting/script.hpp b/src/scripting/include/pw/scripting/script.hpp index 5eb8924..6bd813d 100644 --- a/src/scripting/include/pw/scripting/script.hpp +++ b/src/scripting/include/pw/scripting/script.hpp @@ -15,14 +15,10 @@ public: int run(const std::string& s); - struct state { - virtual int run(const std::string& script) = 0; - virtual ~state() = default; - }; - protected: - std::unique_ptr _state; + struct state; + std::unique_ptr _state; }; diff --git a/src/scripting/src/script.cpp b/src/scripting/src/script.cpp index c48ff00..3ee1169 100644 --- a/src/scripting/src/script.cpp +++ b/src/scripting/src/script.cpp @@ -2,59 +2,76 @@ #include "scripting.hpp" -#include "script_core.hpp" -#include "script_system.hpp" -#include "script_scene.hpp" -#include "script_visual.hpp" +//#include "script_core.hpp" +//#include "script_system.hpp" +//#include "script_scene.hpp" +//#include "script_visual.hpp" + +#include "pw/core/debug.hpp" namespace pw { -struct lua_state : public script::state { +struct script::state { - scripting::state _state; - scripting::table _namespace; + sol::state _state; + sol::table _namespace; void load_modules(); + void load(const std::string& name); - lua_state() { + state() { // create global pw namespace _namespace = _state.create_named_table("pw"); // add the script as a user type - _namespace.new_usertype("script", - "initialize",&lua_state::load_modules - ); + _namespace.new_usertype("script", + "initialize",&state::load_modules, + "load",&state::load + ); // add this instance as "script" _namespace.set("script",this); } - virtual int run(const std::string &s); + int run(const std::string &s); }; -int lua_state::run(const std::string &s) +int script::state::run(const std::string &s) { return _state.script(s.c_str()).get(); } -void lua_state::load_modules() { +void script::state::load(const std::string& name) +{ + lua_runtime::register_function_list[name](_state,_namespace); +} + +void script::state::load_modules() { // open all libraries _state.open_libraries(); - script_core::load(_namespace); - script_system::load(_namespace); - script_scene::load(_namespace); - script_visual::load(_namespace); + for (auto lib_reg : lua_runtime::register_function_list) { + lib_reg.second(_state,_namespace); + debug::d() << lib_reg.first; + } + +// script_core::load(_namespace); +// script_system::load(_namespace); +// script_scene::load(_namespace); +// script_visual::load(_namespace); } +// +// +// script::script() + : _state(make_unique()) { - _state.reset(new lua_state()); } script::~script() diff --git a/src/scripting/src/script_core.cpp b/src/scripting/src/script_core.cpp index d26e81a..5b92b9f 100644 --- a/src/scripting/src/script_core.cpp +++ b/src/scripting/src/script_core.cpp @@ -9,9 +9,11 @@ #include "pw/core/timer.hpp" #include "pw/core/mesh.hpp" +#include "scripting.hpp" + namespace pw { -void script_core::load(sol::table &ns) +void lua_register_core(sol::state& lua,sol::table& ns) { typedef real_t Scalar; @@ -42,7 +44,7 @@ void script_core::load(sol::table &ns) "w", sol::property(sol::resolve(&quaternion::w), [](quaternion& v,quaternion::value_type val){ v.w() = val;}), "identity",sol::readonly_property(&quaternion::identity), "dot",&quaternion::dot, - "inverse",scripting::readonly_property(&quaternion::inverse), + "inverse",sol::readonly_property(&quaternion::inverse), "normalized",&quaternion::normalized, "lerp",&quaternion::lerp, "slerp",&quaternion::slerp @@ -51,8 +53,8 @@ void script_core::load(sol::table &ns) ns.new_usertype ("axisangle", sol::constructors(), - "axis",scripting::property(&axisangle::axis,&axisangle::set_axis), - "angle",scripting::property(&axisangle::angle,&axisangle::set_angle) + "axis",sol::property(&axisangle::axis,&axisangle::set_axis), + "angle",sol::property(&axisangle::angle,&axisangle::set_angle) ); @@ -90,13 +92,21 @@ void script_core::load(sol::table &ns) sol::constructors() ); -// ns.new_enum +// lua["mesh"].new_enum() // ( // "mesh", // sol::constructors() // ); } +struct _register_core { + static _register_core& get() { + lua_runtime::register_function_list["core"] = lua_register_core; + static _register_core instance; + return instance; + } +}; + diff --git a/src/scripting/src/script_core.hpp b/src/scripting/src/script_core.hpp index 9784ab3..458ec78 100644 --- a/src/scripting/src/script_core.hpp +++ b/src/scripting/src/script_core.hpp @@ -7,7 +7,7 @@ namespace pw { struct script_core { - static void load(scripting::table& ns); + static void load(sol::table& ns); }; diff --git a/src/scripting/src/script_scene.cpp b/src/scripting/src/script_scene.cpp index eda1d87..21e6232 100644 --- a/src/scripting/src/script_scene.cpp +++ b/src/scripting/src/script_scene.cpp @@ -4,7 +4,7 @@ namespace pw { -void script_scene::load(sol::table &ns) +void lua_register_scene(sol::table &ns) { ns.new_usertype("node", @@ -16,7 +16,7 @@ void script_scene::load(sol::table &ns) "is_leaf", sol::readonly_property(&node::is_leaf), "is_root", sol::readonly_property(&node::is_root), // "share",scripting::property(scripting::resolve(std::make_shared)) - "name",scripting::property(&node::name,&node::set_name) + "name",sol::property(&node::name,&node::set_name) ); } diff --git a/src/scripting/src/script_scene.hpp b/src/scripting/src/script_scene.hpp index dbe5ba9..9dc7b0d 100644 --- a/src/scripting/src/script_scene.hpp +++ b/src/scripting/src/script_scene.hpp @@ -5,11 +5,11 @@ namespace pw { -struct script_scene { +//struct script_scene { - static void load(scripting::table& ns); +// static void load(scripting::table& ns); -}; +//}; } diff --git a/src/scripting/src/script_system.cpp b/src/scripting/src/script_system.cpp index bbc45cc..6a4551a 100644 --- a/src/scripting/src/script_system.cpp +++ b/src/scripting/src/script_system.cpp @@ -8,7 +8,7 @@ namespace pw { -void script_system::load(sol::table &ns) +void lua_register_system(sol::table &ns) { ns.new_usertype("window", "update",&window::update, diff --git a/src/scripting/src/script_system.hpp b/src/scripting/src/script_system.hpp index f3a39d5..77ef544 100644 --- a/src/scripting/src/script_system.hpp +++ b/src/scripting/src/script_system.hpp @@ -5,11 +5,11 @@ namespace pw { -struct script_system { +//struct script_system { - static void load(scripting::table& ns); +// static void load(scripting::table& ns); -}; +//}; } diff --git a/src/scripting/src/script_visual.cpp b/src/scripting/src/script_visual.cpp index 57e3ef4..2eb2281 100644 --- a/src/scripting/src/script_visual.cpp +++ b/src/scripting/src/script_visual.cpp @@ -5,7 +5,7 @@ namespace pw { -void script_visual::load(sol::table &ns) +void lua_register_visual(sol::table &ns) { ns.new_usertype("pipeline", diff --git a/src/scripting/src/script_visual.hpp b/src/scripting/src/script_visual.hpp index c720209..b40e2d0 100644 --- a/src/scripting/src/script_visual.hpp +++ b/src/scripting/src/script_visual.hpp @@ -5,11 +5,11 @@ namespace pw { -struct script_visual { +//struct script_visual { - static void load(scripting::table& ns); +// static void load(scripting::table& ns); -}; +//}; } diff --git a/src/scripting/src/scripting.hpp b/src/scripting/src/scripting.hpp index 6055cb5..ff67368 100644 --- a/src/scripting/src/scripting.hpp +++ b/src/scripting/src/scripting.hpp @@ -3,11 +3,19 @@ #include "sol/sol.hpp" #include +#include namespace pw { -// include external namespace of sol -namespace scripting = sol; +struct lua_runtime { + + typedef std::function register_function_t; + typedef std::map register_func_list_t; + static register_func_list_t register_function_list; + +}; + +lua_runtime::register_func_list_t lua_runtime::register_function_list = lua_runtime::register_func_list_t(); }