int JLuaServer::FnSet( lua_State* pLua ) { if (lua_gettop( pLua ) < 3 || lua_gettop( pLua ) > 4) { rlog.err( "LUA: Incorrect command usage: <set>. " "Function takes 3 or 4 arguments (<objectRef>|<objectName>, <propName>, <val>, [tag]), but %d is provided", lua_gettop( pLua ) ); lua_settop( pLua, 0 ); lua_pushnil( pLua ); return 1; } JObject* pObj = NULL; const char* pPath = lua_tostring( pLua, 1 ); if (pPath) { JLuaThread* pThread = reinterpret_cast<JLuaThread*>( pLua->userdata ); assert( pThread->m_pLua == pLua ); JObject* pRootObj = pThread->m_pRootObj; pObj = g_pObjectServer->FindObject( pPath, NULL, pRootObj ); } else { pObj = reinterpret_cast<JObject*>( lua_touserdata( pLua, 1 ) ); } const char* pPropName = lua_tostring( pLua, 2 ); const char* pPropVal = lua_tostring( pLua, 3 ); if (!pPropVal && lua_isboolean( pLua, 3 )) { bool bVal = lua_toboolean( pLua, 3 ) == 0 ? false : true; pPropVal = bVal ? "true" : "false"; } int tag = -1; if (lua_isnumber( pLua, 4 )) { tag = (int)lua_tonumber( pLua, 4 ); } if (!pObj) { rlog.warn( "LUA: Trying to call <set> for the nil object." ); return 0; } bool bRes = pObj->SetProperty( pPropName, pPropVal, tag ); if (!bRes) { rlog.err( "LUA: Could not set property '%s' for object '%s' of type '%s'.", pPropName, pObj->GetName(), pObj->ClassName() ); } return 0; } // JLuaServer::FnSet
int JLuaServer::FnCall( lua_State* pLua ) { if (lua_gettop( pLua ) < 2 || lua_gettop( pLua ) > 3) { rlog.err( "LUA: Incorrect command usage: <call>. " "Function takes 2 to 3 arguments (object|objectName, funcName, [tag]), but %d is provided", lua_gettop( pLua ) ); lua_settop( pLua, 0 ); return 0; } JObject* pObj = NULL; const char* pPath = lua_tostring( pLua, 1 ); if (pPath) { JLuaThread* pThread = reinterpret_cast<JLuaThread*>( pLua->userdata ); assert( pThread->m_pLua == pLua ); JObject* pRootObj = pThread->m_pRootObj; pObj = g_pObjectServer->FindObject( pPath, NULL, pRootObj ); if (!pObj) { rlog.warn( "LUA: Method <call> refers to non-existing object: %s", pPath ); return NULL; } } else { pObj = reinterpret_cast<JObject*>( lua_touserdata( pLua, 1 ) ); if (!pObj) { rlog.warn( "LUA: Trying to call <call> for the nil object" ); return NULL; } } const char* pMethodName = lua_tostring( pLua, 2 ); int tag = -1; if (lua_isnumber( pLua, 3 )) { tag = (int)lua_tonumber( pLua, 3 ); } bool bRes = pObj->CallMethod( pMethodName, tag ); if (!bRes) { rlog.err( "LUA: Could not call method '%s' for object '%s' of type '%s'.", pMethodName, pObj->GetName(), pObj->ClassName() ); } return 0; } // JLuaServer::FnCall
int JLuaServer::FnGet( lua_State* pLua ) { if (lua_gettop( pLua ) < 2 || lua_gettop( pLua ) > 3) { rlog.err( "LUA: Incorrect command usage: <get>. " "Function takes 2 or 3 arguments (<objectRef>|<objectName>, <propName>, [tag]), but %d is provided", lua_gettop( pLua ) ); lua_settop( pLua, 0 ); lua_pushnil( pLua ); return 1; } JObject* pObj = NULL; const char* pPath = lua_tostring( pLua, 1 ); if (pPath) { JLuaThread* pThread = reinterpret_cast<JLuaThread*>( pLua->userdata ); assert( pThread->m_pLua == pLua ); JObject* pRootObj = pThread->m_pRootObj; pObj = g_pObjectServer->FindObject( pPath, NULL, pRootObj ); } else { pObj = reinterpret_cast<JObject*>( lua_touserdata( pLua, 1 ) ); } const char* pPropName = lua_tostring( pLua, 2 ); int tag = -1; if (lua_isnumber( pLua, 3 )) { tag = (int)lua_tonumber( pLua, 3 ); } if (!pObj) { rlog.warn( "LUA: Trying to call <get> for the nil object. Nil is returned." ); lua_pushnil( pLua ); return 1; } static JString val; bool bRes = pObj->GetProperty( pPropName, val, tag ); if (!bRes) { rlog.err( "LUA: Could not get property '%s' for object '%s' of type '%s'.", pPropName, pObj->GetName(), pObj->ClassName() ); } lua_pushstring( pLua, val.c_str() ); return 1; } // JLuaServer::FnGet