From f3c17f6d031ca032744092abd844b3534fd5a9c3 Mon Sep 17 00:00:00 2001
From: Hartmut Seichter <hartmut@technotecture.com>
Date: Fri, 11 Dec 2020 22:54:27 +0100
Subject: [PATCH] revert back to sol2

---
 src/binding/CMakeLists.txt                  |   5 +-
 src/binding/include/pw/scripting/script.hpp |   2 +-
 src/binding/src/runtime_lua.cpp             |   4 +-
 src/binding/src/script.cpp                  |  36 ++++-
 src/binding/src/script_core.cpp             | 166 ++++++++++----------
 src/binding/src/script_io.cpp               |   2 +-
 src/binding/src/script_scene.cpp            |  18 ++-
 src/binding/src/script_system.cpp           |  12 +-
 src/core/include/pw/core/debug.hpp          |   1 +
 src/core/src/core.cpp                       |   7 -
 src/core/src/debug.cpp                      |   6 +
 src/scene/include/pw/scene/entity.hpp       |   2 +-
 src/scene/include/pw/scene/scene.hpp        |   3 +
 src/scene/src/scene.cpp                     |  12 ++
 src/scene/tests/pwscene_test_scene.cpp      |   8 +
 src/scripts/demos/simple_001.lua            |  18 ++-
 16 files changed, 184 insertions(+), 118 deletions(-)

diff --git a/src/binding/CMakeLists.txt b/src/binding/CMakeLists.txt
index d14876b..4ca806f 100644
--- a/src/binding/CMakeLists.txt
+++ b/src/binding/CMakeLists.txt
@@ -25,7 +25,7 @@ target_include_directories(
     pwbinding
     PRIVATE
     ${CMAKE_SOURCE_DIR}/src/deps/lua-5.3.5/src
-	${CMAKE_SOURCE_DIR}/src/deps/sol2-3.2.2/include
+	${CMAKE_SOURCE_DIR}/src/deps/sol2-2.20.6
     PUBLIC
     include
     )
@@ -36,4 +36,5 @@ target_link_libraries(pwbinding
     pwsystem
     pwio
     pwscene
-    pwvisual)
+	pwvisual
+	)
diff --git a/src/binding/include/pw/scripting/script.hpp b/src/binding/include/pw/scripting/script.hpp
index 8123757..7110ecc 100644
--- a/src/binding/include/pw/scripting/script.hpp
+++ b/src/binding/include/pw/scripting/script.hpp
@@ -34,7 +34,7 @@ public:
     script();
     ~script();
 
-	int eval(const std::string& s);
+    int eval(const std::string_view &s);
 
 	static void initialize();
 
diff --git a/src/binding/src/runtime_lua.cpp b/src/binding/src/runtime_lua.cpp
index 9446f40..5366647 100644
--- a/src/binding/src/runtime_lua.cpp
+++ b/src/binding/src/runtime_lua.cpp
@@ -8,7 +8,9 @@ runtime_lua &runtime_lua::get()
 	return instance;
 }
 
-void runtime_lua::add(const std::string &name, runtime_lua::register_function_t f) {
+void runtime_lua::add(const std::string &name
+                      ,runtime_lua::register_function_t f)
+{
 	_register_function_list[name] = f;
 }
 
diff --git a/src/binding/src/script.cpp b/src/binding/src/script.cpp
index c4c68fe..75cd034 100644
--- a/src/binding/src/script.cpp
+++ b/src/binding/src/script.cpp
@@ -14,6 +14,24 @@ PW_REGISTER_DECL_LUA(io)
 PW_REGISTER_DECL_LUA(scene)
 PW_REGISTER_DECL_LUA(visual)
 
+
+void static_example()
+{
+
+
+
+
+    // in SOL 2.20.6
+
+
+
+    // in SOL 3.2.2
+
+
+
+
+}
+
 struct script::state {
 
 	sol::state _state;
@@ -24,13 +42,19 @@ struct script::state {
 	void load_all();
 	void load(const std::string& name);
 
-	int run(const std::string &s);
+    int eval(const std::string_view &code);
 
 };
 
-int script::state::run(const std::string &s)
+int script::state::eval(const std::string_view &code)
 {
-	return _state.script(s.c_str()).get<int>();
+    auto results = _state.safe_script(code);
+
+    return 0;
+
+
+    // sol2
+    // return _state.script(s.c_str()).get<int>();
 }
 
 void script::state::load(const std::string& name)
@@ -80,20 +104,18 @@ script::~script()
 {
 }
 
-int script::eval(const std::string &s)
+int script::eval(const std::string_view &s)
 {
-	return _state->run(s);
+    return _state->eval(s);
 }
 
 void script::initialize()
 {
-
     PW_REGISTER_USE_LUA(core)
     PW_REGISTER_USE_LUA(system)
     PW_REGISTER_USE_LUA(io)
     PW_REGISTER_USE_LUA(scene)
     PW_REGISTER_USE_LUA(visual)
-
 }
 
 
diff --git a/src/binding/src/script_core.cpp b/src/binding/src/script_core.cpp
index ac44524..31e11f0 100644
--- a/src/binding/src/script_core.cpp
+++ b/src/binding/src/script_core.cpp
@@ -13,9 +13,9 @@
 
 // seems CRTP magic doesnt work with SOL
 namespace sol {
-    template <> struct is_automagical<pw::matrix4x4> : std::false_type {};
-    template <> struct is_automagical<pw::vector3> : std::false_type {};
-    template <> struct is_automagical<pw::quaternion> : std::false_type {};
+template <> struct is_automagical<pw::matrix4x4> : std::false_type {};
+template <> struct is_automagical<pw::vector3> : std::false_type {};
+template <> struct is_automagical<pw::quaternion> : std::false_type {};
 }
 
 namespace pw {
@@ -23,109 +23,113 @@ namespace pw {
 void register_core_function(sol::state& lua,sol::table& ns)
 {
 
-	typedef real_t Scalar;
+    typedef real_t Scalar;
 
-	ns.set("pi",pw::pi<Scalar>());
+    ns.set("pi",pw::pi<Scalar>());
 
 
-	ns.new_usertype<matrix4x4>("matrix4x4"
-							   , sol::constructors<matrix4x4()>()
-							   , "row",&matrix4x4::row
-							   , "column",&matrix4x4::column
-							   , "set_identity",&matrix4x4::set_identity
-							   , "inverse",&matrix4x4::inverse
-							   );
+    ns.new_usertype<matrix4x4>("matrix4x4"
+                               , sol::constructors<matrix4x4()>()
+                               , "row",&matrix4x4::row
+                               , "column",&matrix4x4::column
+                               , "set_identity",&matrix4x4::set_identity
+                               , "inverse",&matrix4x4::inverse
+                               );
 
-	ns.new_usertype<vector3>("vector3"
-							 ,sol::constructors<vector3(),vector3(Scalar,Scalar,Scalar)>()
-							 ,"x", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::x), [](vector3& v,vector3::value_type val){ v.x() = val;})
-							 ,"y", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::y), [](vector3& v,vector3::value_type val){ v.y() = val;})
-							 ,"z", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::z), [](vector3& v,vector3::value_type val){ v.z() = val;})
-							 ,"cross",&vector3::cross
-							 ,"transposed",&vector3::transposed
-							 ,"lerp",&vector3::lerp
-							 );
+    ns.new_usertype<vector3>("vector3"
+                            ,sol::constructors<vector3(),vector3(Scalar,Scalar,Scalar)>()
+                            ,"x", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::x), [](vector3& v,vector3::value_type val){ v.x() = val;})
+                            ,"y", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::y), [](vector3& v,vector3::value_type val){ v.y() = val;})
+                            ,"z", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::z), [](vector3& v,vector3::value_type val){ v.z() = val;})
+                            ,"cross",&vector3::cross
+                            ,"transposed",&vector3::transposed
+                            ,"lerp",&vector3::lerp
+                            );
 
     ns.new_usertype<quaternion>("quaternion"
-                                , sol::constructors<quaternion(), quaternion(quaternion::value_type,quaternion::value_type,quaternion::value_type,quaternion::value_type)>()
+                                ,sol::constructors<quaternion(), quaternion(quaternion::value_type,quaternion::value_type,quaternion::value_type,quaternion::value_type)>()
                                 ,"x", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::x), [](quaternion& v,quaternion::value_type val){ v.x() = val;})
                                 ,"y", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::y), [](quaternion& v,quaternion::value_type val){ v.y() = val;})
                                 ,"z", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::z), [](quaternion& v,quaternion::value_type val){ v.z() = 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)
                                 ,"dot",&quaternion::dot
-								,"inverse",sol::readonly_property(&quaternion::inverse)
-								,"normalized",&quaternion::normalized
-								,"lerp",&quaternion::lerp
-								,"slerp",&quaternion::slerp
+                                ,"inverse",sol::readonly_property(&quaternion::inverse)
+                                ,"normalized",&quaternion::normalized
+                                ,"lerp",&quaternion::lerp
+                                ,"slerp",&quaternion::slerp
                                 ,"matrix",&quaternion::to_matrix
-								);
+                                );
 
-	ns.new_usertype<axisangle>
-			("axisangle",
-			 sol::constructors<axisangle(), axisangle(vector3,Scalar)>(),
-			 "axis",&axisangle::axis,
-			 "angle",&axisangle::angle,
-			 "from_matrix",&axisangle::from_matrix,
-			 "to_matrix",&axisangle::to_matrix
-			 );
+    ns.new_usertype<axisangle>
+            ("axisangle",
+             sol::constructors<axisangle(), axisangle(vector3,Scalar)>(),
+             "axis",&axisangle::axis,
+             "angle",&axisangle::angle,
+             "from_matrix",&axisangle::from_matrix,
+             "to_matrix",&axisangle::to_matrix
+             );
 
 
-	ns.new_usertype<size>("size"
-						  , sol::constructors<size(),size(Scalar,Scalar)>()
-						  , "width",&size::width
-						  , "height",&size::height
-						  );
+    ns.new_usertype<size>("size"
+                          , sol::constructors<size(),size(Scalar,Scalar)>()
+                          , "width",&size::width
+                          , "height",&size::height
+                          );
 
     ns.new_usertype<sizei>("sizei"
-                          , sol::constructors<sizei(),sizei(int,int)>()
-                          , "width",&sizei::width
-                          , "height",&sizei::height
+                           , sol::constructors<sizei(),sizei(int,int)>()
+                           , "width",&sizei::width
+                           , "height",&sizei::height
+                           );
+
+    ns.new_usertype<point>("point",
+                           sol::constructors<point(),point(Scalar,Scalar)>(),
+                           "x",&point::x,
+                           "y",&point::y
+                           );
+
+
+    ns.new_usertype<debug>("debug",
+                           "new",sol::no_constructor,
+                           "get",&debug::get,
+                           "write",&debug::write
+                           //						   "none",sol::debug::level::none
+                           );
+
+
+    ns.new_usertype<time>("time",
+                          "now",sol::readonly_property(&time::now),
+                          "elapsed",sol::readonly_property(&time::elapsed),
+                          "reset",&time::reset
                           );
-
-	ns.new_usertype<point>("point",
-						   sol::constructors<point(),point(Scalar,Scalar)>(),
-						   "x",&point::x,
-						   "y",&point::y
-						   );
+    ns.new_usertype<geometry>("geometry"
+                              , sol::constructors<geometry()>()
+                              , "topology", sol::property(&geometry::topology,&geometry::set_topology)
+                              , "vertices", sol::property(&geometry::vertices,&geometry::set_vertices)
+                              , "indices", sol::property(&geometry::indices,&geometry::set_indices)
+                              ).new_enum<false>("topology_type"
+                                                ,"points", geometry::topology_type::points
+                                                , "lines", geometry::topology_type::lines
+                                                , "line_strip", geometry::topology_type::line_strip);
 
 
-	ns.new_usertype<debug>("debug",
-						   "new",sol::no_constructor,
-						   "get",&debug::get,
-						   "write",&debug::write
-						   //						   "none",sol::debug::level::none
-						   );
+    // SOL3
+    //    geoom_type["type"] = lua.create_table_with(
+    //                  "points", geometry::topology_type::points
+    //                , "lines", geometry::topology_type::lines
+    //                , "line_strip", geometry::topology_type::line_strip
+    //                );
 
-
-	ns.new_usertype<time>("time",
-						  "now",sol::readonly_property(&time::now),
-						  "elapsed",sol::readonly_property(&time::elapsed),
-						  "reset",&time::reset
-                          );
-
-    auto geoom_type = ns.new_usertype<geometry>("geometry"
-                                           , sol::constructors<geometry()>()
-                                           , "topology", sol::property(&geometry::topology,&geometry::set_topology)
-                                           , "vertices", sol::property(&geometry::vertices,&geometry::set_vertices)
-                                           , "indices", sol::property(&geometry::indices,&geometry::set_indices)
-                                           );
-    geoom_type["type"] = lua.create_table_with(
-                  "points", geometry::topology_type::points
-                , "lines", geometry::topology_type::lines
-                , "line_strip", geometry::topology_type::line_strip
-                );
-
-    auto image_type = ns.new_usertype<image>("image"
+    ns.new_usertype<image>("image"
+                           , sol::constructors<image()>()
                            ,"create",&image::create
                            ,"size",sol::readonly_property(&image::size)
                            ,"change_count",sol::property(&image::change_count,&image::set_change_count)
-                           );
-
-//    image_type.new_enum("layout"
-//                                      ,"rgb8", image::RGB8
-//                                      ,"rgb32", image::RGBA8
-//                                      ,"gray", image::LUM);
+                           ).new_enum<false>("layout"
+                                             ,"rgb8", image::RGB8
+                                             ,"rgb32", image::RGBA8
+                                             ,"gray", image::LUM);
 
 
 }
diff --git a/src/binding/src/script_io.cpp b/src/binding/src/script_io.cpp
index 1250d47..7746fac 100644
--- a/src/binding/src/script_io.cpp
+++ b/src/binding/src/script_io.cpp
@@ -12,7 +12,7 @@ void register_io_function(sol::state&,sol::table& ns)
                               ,"new", sol::no_constructor
                               ,"get",&image_io::get
                               ,"read",&image_io::read
-                              , "write",&image_io::write
+                              ,"write",&image_io::write
                               );
 }
 
diff --git a/src/binding/src/script_scene.cpp b/src/binding/src/script_scene.cpp
index 4e2c3a7..c68e458 100644
--- a/src/binding/src/script_scene.cpp
+++ b/src/binding/src/script_scene.cpp
@@ -6,19 +6,27 @@
 
 #include "runtime_lua.hpp"
 
+namespace sol {
+    template <> struct is_automagical<pw::entity> : std::false_type {};
+}
+
 namespace pw {
 
+
 void register_scene_function(sol::state&,sol::table &ns)
 {
 
     ns.new_usertype<scene>("scene",
-                           sol::constructors<scene()>());
+                           sol::constructors<scene()>()
+                           ,"count_all_entities",sol::readonly_property(&scene::count_all_enties)
+                           ,"count_alive_entities",sol::readonly_property(&scene::count_alive_enties)
+                           );
 
 
-    ns.new_usertype<entity>("entity",
-                            sol::constructors</*entity(),*/entity(scene&)>(),
-                            "add_child",&entity::add_child,
-                            "child_count",sol::readonly_property(&entity::child_count)
+    ns.new_usertype<entity>("entity"
+                            ,sol::constructors<entity(),entity(const entity&),entity(scene&)>()
+                            ,"add_child",&entity::add_child
+                            ,"child_count",sol::readonly_property(&entity::child_count)
                             );
 
 
diff --git a/src/binding/src/script_system.cpp b/src/binding/src/script_system.cpp
index e257c45..a291142 100644
--- a/src/binding/src/script_system.cpp
+++ b/src/binding/src/script_system.cpp
@@ -39,12 +39,12 @@ void register_system_function(sol::state&, sol::table &ns)
                              "name",sol::readonly_property(&display::name)
                              );
 
-	ns.new_usertype<path>("path"
-						  ,"new", sol::no_constructor
-						  ,"get",&path::get
-						  ,"separator",sol::readonly_property(&path::separator)
-						  ,"executable_path",sol::readonly_property(&path::executable_path)
-						  );
+    ns.new_usertype<path>("path"
+                          ,"new", sol::no_constructor
+                          ,"get",&path::get
+                          ,"separator",sol::readonly_property(&path::separator)
+                          ,"executable_path",sol::readonly_property(&path::executable_path)
+                          );
 }
 
 
diff --git a/src/core/include/pw/core/debug.hpp b/src/core/include/pw/core/debug.hpp
index e0923cb..601770b 100644
--- a/src/core/include/pw/core/debug.hpp
+++ b/src/core/include/pw/core/debug.hpp
@@ -62,6 +62,7 @@ public:
         stream& operator << (const bool &value);
         stream& operator << (const char *value);
         stream& operator << (const std::string& value);		///! log a string
+        stream& operator << (const std::string_view& value); ///! log a string_view
 
         stream& operator << (const float &value);			///! log a float value
         stream& operator << (const double &value);			///! log a double value
diff --git a/src/core/src/core.cpp b/src/core/src/core.cpp
index 0a76adb..2fdb849 100644
--- a/src/core/src/core.cpp
+++ b/src/core/src/core.cpp
@@ -22,13 +22,6 @@ template <typename T> struct module {
 template <typename T> typename module<T>::proxy module<T>::_proxy;
 
 
-struct stuff {
-
-};
-
-
-module<stuff> mod;
-
 
 }
 
diff --git a/src/core/src/debug.cpp b/src/core/src/debug.cpp
index 141e363..fcae0be 100644
--- a/src/core/src/debug.cpp
+++ b/src/core/src/debug.cpp
@@ -140,6 +140,12 @@ debug::stream &debug::stream::operator <<(const std::string &value)
     return *this;
 }
 
+debug::stream &debug::stream::operator <<(const std::string_view &value)
+{
+    _line.append(value.data());
+    return *this;
+}
+
 debug::stream &debug::stream::operator <<(const float &value)
 {
     std::stringstream ss;
diff --git a/src/scene/include/pw/scene/entity.hpp b/src/scene/include/pw/scene/entity.hpp
index 982f6dd..707aaf1 100644
--- a/src/scene/include/pw/scene/entity.hpp
+++ b/src/scene/include/pw/scene/entity.hpp
@@ -64,7 +64,7 @@ public:
     }
 
     template<typename T>
-    bool has_component() const { return _registry->valid(_entity);}
+    bool has_component() const { return _registry->has<T>(_entity);}
 
     void destroy();
 
diff --git a/src/scene/include/pw/scene/scene.hpp b/src/scene/include/pw/scene/scene.hpp
index e3fdee5..625adf7 100644
--- a/src/scene/include/pw/scene/scene.hpp
+++ b/src/scene/include/pw/scene/scene.hpp
@@ -38,6 +38,9 @@ public:
     scene();
     ~scene() = default;
 
+    size_t count_all_enties() const;
+    size_t count_alive_enties() const;
+
 protected:
 
     std::shared_ptr<entt::registry> _registry;
diff --git a/src/scene/src/scene.cpp b/src/scene/src/scene.cpp
index 8c03e16..a8908b0 100644
--- a/src/scene/src/scene.cpp
+++ b/src/scene/src/scene.cpp
@@ -8,5 +8,17 @@ scene::scene()
 {
 }
 
+size_t scene::count_all_enties() const
+{
+    return _registry->size();
+}
+
+size_t scene::count_alive_enties() const
+{
+    return _registry->alive();
+}
+
+
+
 
 }
diff --git a/src/scene/tests/pwscene_test_scene.cpp b/src/scene/tests/pwscene_test_scene.cpp
index 0e67722..ce0d100 100644
--- a/src/scene/tests/pwscene_test_scene.cpp
+++ b/src/scene/tests/pwscene_test_scene.cpp
@@ -42,6 +42,7 @@ void test_stack()
     e.add_child(e2);
     e.add_child(e3);
 
+
     std::cout << e.child_count() << std::endl;
 
 }
@@ -55,6 +56,13 @@ void test_heap()
 
     auto e = std::make_unique<entity>(*s);
 
+    auto e2 = std::make_unique<entity>(*s);
+
+    e->add_child(*e2);
+
+    pw::debug::d() << e->child_count();
+
+
     auto t = e->add_component<test>(112);
 
     std::cout << t.val << std::endl;
diff --git a/src/scripts/demos/simple_001.lua b/src/scripts/demos/simple_001.lua
index 0ebd3d4..783b44a 100644
--- a/src/scripts/demos/simple_001.lua
+++ b/src/scripts/demos/simple_001.lua
@@ -2,20 +2,26 @@
 -- small demonstrator for Lua binding on pixwerx
 --
 
--- loading our libraries
 pw.script:load_all()
 
 print("hello pixwerx!")
 
 local s = pw.scene.new()
 
-e = pw.entity.new(s)
-e2 = pw.entity.new(s)
+print(s)
+
+local e = pw.entity.new(s)
+c = pw.entity.new(s)
 
 print(e)
-e2.add_child(e)
+print(e.child_count)
 
--- print(s)
--- print(e2.child_count)
+-- add child
+e.add_child(c)
 
 
+print(e.child_count)
+
+print("bye, bye pixwerx!")
+
+return 1