From e421e9077c61465b2073b0f6428cdb53e3bfbce8 Mon Sep 17 00:00:00 2001 From: faketruth Date: Thu, 11 Oct 2012 14:02:09 +0000 Subject: Can now send usertypes (cPlayer, cEntity, cCuboid) along with a plugin:Call() git-svn-id: http://mc-server.googlecode.com/svn/trunk@946 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/ManualBindings.cpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/source/ManualBindings.cpp b/source/ManualBindings.cpp index dc96800d3..9ddd1360e 100644 --- a/source/ManualBindings.cpp +++ b/source/ManualBindings.cpp @@ -737,8 +737,29 @@ static int copy_lua_values(lua_State * a_Source, lua_State * a_Destination, int tolua_pushnumber(a_Destination, d ); } break; + case LUA_TUSERDATA: + { + const char * type = 0; + if (lua_getmetatable(a_Source,i)) + { + lua_rawget(a_Source, LUA_REGISTRYINDEX); + type = lua_tostring(a_Source, -1); + lua_pop(a_Source, 1); // Pop.. something?! I don't knooow~~ T_T + } + + // don't need tolua_tousertype we already have the type + void * ud = tolua_touserdata(a_Source, i, 0); + LOGD("%i push usertype: %p of type '%s'", i, ud, type); + if( type == 0 ) + { + LOGERROR("Call(): Something went wrong when trying to get usertype name!"); + return 0; + } + tolua_pushusertype(a_Destination, ud, type); + } + break; default: /* other values */ - LOGERROR("Unsupported value: '%s'. Can only use numbers and strings!", lua_typename(a_Source, t)); + LOGERROR("Call(): Unsupported value: '%s'. Can only use numbers and strings!", lua_typename(a_Source, t)); return 0; } } @@ -781,12 +802,14 @@ static int tolua_cPlugin_Call(lua_State* tolua_S) int nresults = lua_gettop(targetState) - targetTop; LOGD("num results: %i", nresults); int ttop = lua_gettop(targetState); - if( copy_lua_values(targetState, tolua_S, 2, ttop) == 0 ) // Start at 2 and I have no idea why xD + if( copy_lua_values(targetState, tolua_S, targetTop+1, ttop) == 0 ) // Start at targetTop+1 and I have no idea why xD { // something went wrong, exit return 0; } + lua_pop(targetState, nresults+1); // I have no idea what I'm doing, but it works + return nresults; } -- cgit v1.2.3