reworking the scripting module

This commit is contained in:
Hartmut Seichter 2019-01-22 16:40:01 +01:00
parent 803af8c37a
commit a497ac1f7d
11 changed files with 77 additions and 46 deletions

View file

@ -15,14 +15,10 @@ public:
int run(const std::string& s); int run(const std::string& s);
struct state {
virtual int run(const std::string& script) = 0;
virtual ~state() = default;
};
protected: protected:
std::unique_ptr<state> _state; struct state;
std::unique_ptr<state> _state;
}; };

View file

@ -2,59 +2,76 @@
#include "scripting.hpp" #include "scripting.hpp"
#include "script_core.hpp" //#include "script_core.hpp"
#include "script_system.hpp" //#include "script_system.hpp"
#include "script_scene.hpp" //#include "script_scene.hpp"
#include "script_visual.hpp" //#include "script_visual.hpp"
#include "pw/core/debug.hpp"
namespace pw { namespace pw {
struct lua_state : public script::state { struct script::state {
scripting::state _state; sol::state _state;
scripting::table _namespace; sol::table _namespace;
void load_modules(); void load_modules();
void load(const std::string& name);
lua_state() { state() {
// create global pw namespace // create global pw namespace
_namespace = _state.create_named_table("pw"); _namespace = _state.create_named_table("pw");
// add the script as a user type // add the script as a user type
_namespace.new_usertype<lua_state>("script", _namespace.new_usertype<state>("script",
"initialize",&lua_state::load_modules "initialize",&state::load_modules,
); "load",&state::load
);
// add this instance as "script" // add this instance as "script"
_namespace.set("script",this); _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<int>(); return _state.script(s.c_str()).get<int>();
} }
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 // open all libraries
_state.open_libraries(); _state.open_libraries();
script_core::load(_namespace); for (auto lib_reg : lua_runtime::register_function_list) {
script_system::load(_namespace); lib_reg.second(_state,_namespace);
script_scene::load(_namespace); debug::d() << lib_reg.first;
script_visual::load(_namespace); }
// script_core::load(_namespace);
// script_system::load(_namespace);
// script_scene::load(_namespace);
// script_visual::load(_namespace);
} }
//
//
//
script::script() script::script()
: _state(make_unique<script::state>())
{ {
_state.reset(new lua_state());
} }
script::~script() script::~script()

View file

@ -9,9 +9,11 @@
#include "pw/core/timer.hpp" #include "pw/core/timer.hpp"
#include "pw/core/mesh.hpp" #include "pw/core/mesh.hpp"
#include "scripting.hpp"
namespace pw { namespace pw {
void script_core::load(sol::table &ns) void lua_register_core(sol::state& lua,sol::table& ns)
{ {
typedef real_t Scalar; typedef real_t Scalar;
@ -42,7 +44,7 @@ void script_core::load(sol::table &ns)
"w", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::w), [](quaternion& v,quaternion::value_type val){ v.w() = val;}), "w", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::w), [](quaternion& v,quaternion::value_type val){ v.w() = val;}),
"identity",sol::readonly_property(&quaternion::identity), "identity",sol::readonly_property(&quaternion::identity),
"dot",&quaternion::dot, "dot",&quaternion::dot,
"inverse",scripting::readonly_property(&quaternion::inverse), "inverse",sol::readonly_property(&quaternion::inverse),
"normalized",&quaternion::normalized, "normalized",&quaternion::normalized,
"lerp",&quaternion::lerp, "lerp",&quaternion::lerp,
"slerp",&quaternion::slerp "slerp",&quaternion::slerp
@ -51,8 +53,8 @@ void script_core::load(sol::table &ns)
ns.new_usertype<axisangle> ns.new_usertype<axisangle>
("axisangle", ("axisangle",
sol::constructors<axisangle(), axisangle(vector3,Scalar)>(), sol::constructors<axisangle(), axisangle(vector3,Scalar)>(),
"axis",scripting::property(&axisangle::axis,&axisangle::set_axis), "axis",sol::property(&axisangle::axis,&axisangle::set_axis),
"angle",scripting::property(&axisangle::angle,&axisangle::set_angle) "angle",sol::property(&axisangle::angle,&axisangle::set_angle)
); );
@ -90,13 +92,21 @@ void script_core::load(sol::table &ns)
sol::constructors<mesh()>() sol::constructors<mesh()>()
); );
// ns.new_enum<mesh::topology_type> // lua["mesh"].new_enum()
// ( // (
// "mesh", // "mesh",
// sol::constructors<mesh()>() // sol::constructors<mesh()>()
// ); // );
} }
struct _register_core {
static _register_core& get() {
lua_runtime::register_function_list["core"] = lua_register_core;
static _register_core instance;
return instance;
}
};

View file

@ -7,7 +7,7 @@ namespace pw {
struct script_core { struct script_core {
static void load(scripting::table& ns); static void load(sol::table& ns);
}; };

View file

@ -4,7 +4,7 @@
namespace pw { namespace pw {
void script_scene::load(sol::table &ns) void lua_register_scene(sol::table &ns)
{ {
ns.new_usertype<node>("node", ns.new_usertype<node>("node",
@ -16,7 +16,7 @@ void script_scene::load(sol::table &ns)
"is_leaf", sol::readonly_property(&node::is_leaf), "is_leaf", sol::readonly_property(&node::is_leaf),
"is_root", sol::readonly_property(&node::is_root), "is_root", sol::readonly_property(&node::is_root),
// "share",scripting::property(scripting::resolve<std::shared_ptr<node>(std::make_shared<node>)) // "share",scripting::property(scripting::resolve<std::shared_ptr<node>(std::make_shared<node>))
"name",scripting::property(&node::name,&node::set_name) "name",sol::property(&node::name,&node::set_name)
); );
} }

View file

@ -5,11 +5,11 @@
namespace pw { namespace pw {
struct script_scene { //struct script_scene {
static void load(scripting::table& ns); // static void load(scripting::table& ns);
}; //};
} }

View file

@ -8,7 +8,7 @@
namespace pw { namespace pw {
void script_system::load(sol::table &ns) void lua_register_system(sol::table &ns)
{ {
ns.new_usertype<window>("window", ns.new_usertype<window>("window",
"update",&window::update, "update",&window::update,

View file

@ -5,11 +5,11 @@
namespace pw { namespace pw {
struct script_system { //struct script_system {
static void load(scripting::table& ns); // static void load(scripting::table& ns);
}; //};
} }

View file

@ -5,7 +5,7 @@
namespace pw { namespace pw {
void script_visual::load(sol::table &ns) void lua_register_visual(sol::table &ns)
{ {
ns.new_usertype<pipeline>("pipeline", ns.new_usertype<pipeline>("pipeline",

View file

@ -5,11 +5,11 @@
namespace pw { namespace pw {
struct script_visual { //struct script_visual {
static void load(scripting::table& ns); // static void load(scripting::table& ns);
}; //};
} }

View file

@ -3,11 +3,19 @@
#include "sol/sol.hpp" #include "sol/sol.hpp"
#include <lua.hpp> #include <lua.hpp>
#include <map>
namespace pw { namespace pw {
// include external namespace of sol struct lua_runtime {
namespace scripting = sol;
typedef std::function<void(sol::state&,sol::table&)> register_function_t;
typedef std::map<std::string,register_function_t> 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();
} }