lua_bindings: Use Luabind where handy

master
Perttu Ahola 2014-10-19 12:29:12 +03:00
parent fff7682d10
commit 9fb6eae4a0
3 changed files with 45 additions and 155 deletions

View File

@ -3,6 +3,7 @@
#include "lua_bindings/init.h"
#include "core/log.h"
#include "interface/fs.h"
#include <luabind/luabind.hpp>
#define MODULE "lua_bindings"
namespace lua_bindings {
@ -16,6 +17,8 @@ extern void init_misc_urho3d(lua_State *L);
void init(lua_State *L)
{
luabind::open(L);
init_misc(L);
init_cereal(L);
init_voxel(L);

View File

@ -6,8 +6,11 @@
#include "interface/mesh.h"
#include "interface/voxel_volume.h"
#include "interface/worker_thread.h"
#include <tolua++.h>
#include <c55/os.h>
#include <tolua++.h>
#include <luabind/luabind.hpp>
#include <luabind/adopt_policy.hpp>
#include <luabind/pointer_traits.hpp>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#include <Scene.h>
@ -32,7 +35,7 @@ namespace lua_bindings {
#define GET_TOLUA_STUFF(result_name, index, type) \
if(!tolua_isusertype(L, index, #type, 0, &tolua_err)){ \
tolua_error(L, __PRETTY_FUNCTION__, &tolua_err); \
return 0; \
throw Exception("Expected \"" #type "\""); \
} \
type *result_name = (type*)tolua_tousertype(L, index, 0);
#define TRY_GET_TOLUA_STUFF(result_name, index, type) \
@ -43,15 +46,13 @@ namespace lua_bindings {
// NOTE: This API is designed this way because otherwise ownership management of
// objects sucks
// set_simple_voxel_model(node, w, h, d, buffer: VectorBuffer)
static int l_set_simple_voxel_model(lua_State *L)
void set_simple_voxel_model(const luabind::object &node_o,
int w, int h, int d, const luabind::object &buffer_o)
{
lua_State *L = node_o.interpreter();
tolua_Error tolua_err;
GET_TOLUA_STUFF(node, 1, Node);
int w = lua_tointeger(L, 2);
int h = lua_tointeger(L, 3);
int d = lua_tointeger(L, 4);
TRY_GET_TOLUA_STUFF(buf, 5, const VectorBuffer);
log_d(MODULE, "set_simple_voxel_model(): node=%p", node);
@ -64,9 +65,8 @@ static int l_set_simple_voxel_model(lua_State *L)
data.assign((const char*)&buf->GetBuffer()[0], buf->GetBuffer().Size());
if((int)data.size() != w * h * d){
log_e(MODULE, "set_simple_voxel_model(): Data size does not match "
"with dimensions (%zu vs. %i)", data.size(), w*h*d);
return 0;
throw Exception(ss_()+"set_simple_voxel_model(): Data size does not match"
" with dimensions ("+cs(data.size())+" vs. "+cs(w*h*d)+")");
}
lua_getfield(L, LUA_REGISTRYINDEX, "__buildat_app");
@ -79,19 +79,15 @@ static int l_set_simple_voxel_model(lua_State *L)
StaticModel *object = node->GetOrCreateComponent<StaticModel>();
object->SetModel(fromScratchModel);
return 0;
}
// set_8bit_voxel_geometry(node, w, h, d, buffer: VectorBuffer)
static int l_set_8bit_voxel_geometry(lua_State *L)
void set_8bit_voxel_geometry(const luabind::object &node_o,
int w, int h, int d, const luabind::object &buffer_o)
{
lua_State *L = node_o.interpreter();
tolua_Error tolua_err;
GET_TOLUA_STUFF(node, 1, Node);
int w = lua_tointeger(L, 2);
int h = lua_tointeger(L, 3);
int d = lua_tointeger(L, 4);
TRY_GET_TOLUA_STUFF(buf, 5, const VectorBuffer);
log_d(MODULE, "set_8bit_voxel_geometry(): node=%p", node);
@ -104,9 +100,8 @@ static int l_set_8bit_voxel_geometry(lua_State *L)
data.assign((const char*)&buf->GetBuffer()[0], buf->GetBuffer().Size());
if((int)data.size() != w * h * d){
log_e(MODULE, "set_8bit_voxel_geometry(): Data size does not match "
"with dimensions (%zu vs. %i)", data.size(), w*h*d);
return 0;
throw Exception(ss_()+"set_8bit_voxel_geometry(): Data size does not match"
" with dimensions ("+cs(data.size())+" vs. "+cs(w*h*d)+")");
}
lua_getfield(L, LUA_REGISTRYINDEX, "__buildat_app");
@ -121,13 +116,8 @@ static int l_set_8bit_voxel_geometry(lua_State *L)
interface::mesh::set_8bit_voxel_geometry(cg, context, w, h, d, data,
voxel_reg, atlas_reg);
// Maybe appropriate
cg->SetOccluder(true);
// TODO: Don't do this here; allow the caller to do this
cg->SetCastShadows(true);
return 0;
}
#ifdef DEBUG_LOG_TIMING
@ -197,20 +187,21 @@ struct SetVoxelGeometryTask: public interface::worker_thread::Task
}
};
// set_voxel_geometry(node, buffer: VectorBuffer)
static int l_set_voxel_geometry(lua_State *L)
void set_voxel_geometry(const luabind::object &node_o,
const luabind::object &buffer_o)
{
lua_State *L = node_o.interpreter();
tolua_Error tolua_err;
GET_TOLUA_STUFF(node, 1, Node);
TRY_GET_TOLUA_STUFF(buf, 2, const VectorBuffer);
log_d(MODULE, "set_voxel_geometry(): node=%p", node);
TRY_GET_TOLUA_STUFF(buf, 2, const VectorBuffer);
log_d(MODULE, "set_voxel_geometry(): buf=%p", buf);
ss_ data;
if(buf == nullptr)
data = lua_tocppstring(L, 2);
data = lua_checkcppstring(L, 2);
else
data.assign((const char*)&buf->GetBuffer()[0], buf->GetBuffer().Size());
@ -227,8 +218,6 @@ static int l_set_voxel_geometry(lua_State *L)
auto *thread_pool = buildat_app->get_thread_pool();
thread_pool->add_task(std::move(task));
return 0;
}
struct SetVoxelLodGeometryTask: public interface::worker_thread::Task
@ -287,12 +276,12 @@ struct SetVoxelLodGeometryTask: public interface::worker_thread::Task
}
};
// set_voxel_lod_geometry(lod: number, node: Node, buffer: VectorBuffer)
static int l_set_voxel_lod_geometry(lua_State *L)
void set_voxel_lod_geometry(int lod, const luabind::object &node_o,
const luabind::object &buffer_o)
{
lua_State *L = node_o.interpreter();
tolua_Error tolua_err;
int lod = lua_tointeger(L, 1);
GET_TOLUA_STUFF(node, 2, Node);
TRY_GET_TOLUA_STUFF(buf, 3, const VectorBuffer);
@ -319,13 +308,11 @@ static int l_set_voxel_lod_geometry(lua_State *L)
auto *thread_pool = buildat_app->get_thread_pool();
thread_pool->add_task(std::move(task));
return 0;
}
// clear_voxel_geometry(node: Node)
static int l_clear_voxel_geometry(lua_State *L)
void clear_voxel_geometry(const luabind::object &node_o)
{
lua_State *L = node_o.interpreter();
tolua_Error tolua_err;
GET_TOLUA_STUFF(node, 1, Node);
@ -337,8 +324,6 @@ static int l_clear_voxel_geometry(lua_State *L)
node->RemoveComponent(cg);
//cg->Clear();
//cg->Commit();
return 0;
}
struct SetPhysicsBoxesTask: public interface::worker_thread::Task
@ -413,9 +398,10 @@ struct SetPhysicsBoxesTask: public interface::worker_thread::Task
}
};
// set_voxel_physics_boxes(node, buffer: VectorBuffer)
static int l_set_voxel_physics_boxes(lua_State *L)
void set_voxel_physics_boxes(const luabind::object &node_o,
const luabind::object &buffer_o)
{
lua_State *L = node_o.interpreter();
tolua_Error tolua_err;
GET_TOLUA_STUFF(node, 1, Node);
@ -442,13 +428,11 @@ static int l_set_voxel_physics_boxes(lua_State *L)
auto *thread_pool = buildat_app->get_thread_pool();
thread_pool->add_task(std::move(task));
return 0;
}
// clear_voxel_physics_boxes(node)
static int l_clear_voxel_physics_boxes(lua_State *L)
void clear_voxel_physics_boxes(const luabind::object &node_o)
{
lua_State *L = node_o.interpreter();
tolua_Error tolua_err;
GET_TOLUA_STUFF(node, 1, Node);
@ -463,23 +447,21 @@ static int l_clear_voxel_physics_boxes(lua_State *L)
node->GetComponents<CollisionShape>(previous_shapes);
for(size_t i = 0; i < previous_shapes.Size(); i++)
node->RemoveComponent(previous_shapes[i]);
return 0;
}
void init_mesh(lua_State *L)
{
#define DEF_BUILDAT_FUNC(name){ \
lua_pushcfunction(L, l_##name); \
lua_setglobal(L, "__buildat_" #name); \
}
DEF_BUILDAT_FUNC(set_simple_voxel_model);
DEF_BUILDAT_FUNC(set_8bit_voxel_geometry);
DEF_BUILDAT_FUNC(set_voxel_geometry);
DEF_BUILDAT_FUNC(set_voxel_lod_geometry);
DEF_BUILDAT_FUNC(clear_voxel_geometry);
DEF_BUILDAT_FUNC(set_voxel_physics_boxes);
DEF_BUILDAT_FUNC(clear_voxel_physics_boxes);
#define LUABIND_FUNC(name) def("__buildat_" #name, name)
using namespace luabind;
module(L)[
LUABIND_FUNC(set_simple_voxel_model),
LUABIND_FUNC(set_8bit_voxel_geometry),
LUABIND_FUNC(set_voxel_geometry),
LUABIND_FUNC(set_voxel_lod_geometry),
LUABIND_FUNC(clear_voxel_geometry),
LUABIND_FUNC(set_voxel_physics_boxes),
LUABIND_FUNC(clear_voxel_physics_boxes)
];
}
} // namespace lua_bindingss

View File

@ -16,109 +16,14 @@
using interface::VoxelInstance;
using interface::VoxelRegistry;
/*namespace luabind {
namespace detail {
namespace has_get_pointer_ {
template<class T>
T * get_pointer(std::shared_ptr<T> const& p) { return p.get(); }
} // has_get_pointer_
} // detail
} // luabind*/
namespace lua_bindings {
/*struct LuaVoxelRegistry
{
static constexpr const char *class_name = "VoxelRegistry";
sp_<VoxelRegistry> internal;
LuaVoxelRegistry():
internal(interface::createVoxelRegistry())
{}
static int gc_object(lua_State *L){
delete *(LuaVoxelRegistry**)(lua_touserdata(L, 1));
return 0;
}
static int l_serialize(lua_State *L){
LuaVoxelRegistry *o = internal_checkobject(L, 1);
std::ostringstream os(std::ios::binary);
o->internal->serialize(os);
ss_ s = os.str();
lua_pushlstring(L, s.c_str(), s.size());
return 1;
}
static int l_deserialize(lua_State *L){
LuaVoxelRegistry *o = internal_checkobject(L, 1);
ss_ s = lua_checkcppstring(L, 2);
std::istringstream is(s, std::ios::binary);
o->internal->deserialize(is);
return 0;
}
static LuaVoxelRegistry* internal_checkobject(lua_State *L, int narg){
luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, class_name);
if(!ud) luaL_typerror(L, narg, class_name);
return *(LuaVoxelRegistry**)ud;
}
static int l_create(lua_State *L){
LuaVoxelRegistry *o = new LuaVoxelRegistry();
*(void**)(lua_newuserdata(L, sizeof(void*))) = o;
luaL_getmetatable(L, class_name);
lua_setmetatable(L, -2);
return 1;
}
static void register_metatable(lua_State *L){
lua_newtable(L);
int method_table_L = lua_gettop(L);
luaL_newmetatable(L, class_name);
int metatable_L = lua_gettop(L);
// hide metatable from Lua getmetatable()
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, method_table_L);
lua_settable(L, metatable_L);
lua_pushliteral(L, "__index");
lua_pushvalue(L, method_table_L);
lua_settable(L, metatable_L);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable_L);
lua_pop(L, 1); // drop metatable_L
// fill method_table_L
DEF_METHOD(serialize);
DEF_METHOD(deserialize);
// drop method_table_L
lua_pop(L, 1);
}
};
static int l_VoxelRegistry(lua_State *L)
{
return LuaVoxelRegistry::l_create(L);
}*/
void init_voxel(lua_State *L)
{
#define DEF_BUILDAT_FUNC(name){ \
lua_pushcfunction(L, l_##name); \
lua_setglobal(L, "__buildat_" #name); \
}
//LuaVoxelRegistry::register_metatable(L);
//DEF_BUILDAT_FUNC(VoxelRegistry);
using namespace luabind;
luabind::open(L);
module(L)[
class_<VoxelRegistry, bases<>, sp_<VoxelRegistry>>("VoxelRegistry")
//.def("create", &interface::createVoxelRegistry, adopt_policy<0>())
.def("serialize", (ss_(VoxelRegistry::*)())
&VoxelRegistry::serialize)
.def("deserialize", (void(VoxelRegistry::*)(const ss_&))