static int SendBinaryMessageToLua(int nHandler,const unsigned char* pTable,int nLength) { if (NULL == pTable || nHandler <= 0) { return 0; } if (NULL == ScriptEngineManager::getInstance()->getScriptEngine()) { return 0; } LuaStack *pStack = LuaEngine::getInstance()->getLuaStack(); if (NULL == pStack) { return 0; } lua_State *tolua_s = pStack->getLuaState(); if (NULL == tolua_s) { return 0; } int nRet = 0; LuaValueArray array; for (int i = 0 ; i < nLength; i++) { LuaValue value = LuaValue::intValue(pTable[i]); array.push_back(value); } pStack->pushLuaValueArray(array); nRet = pStack->executeFunctionByHandler(nHandler, 1); pStack->clean(); return nRet; }
LUA_STRING Crypto::encodingBase64Lua(bool isDecoding, const char* input, int inputLength) { LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->clean(); int bufferSize = isDecoding ? Base64decode_len(input) : Base64encode_len(inputLength); char *buffer = bufferSize ? (char*)malloc(bufferSize) : NULL; int size = 0; if (buffer) { size = isDecoding ? Base64decode(buffer, input) : Base64encode(buffer, input, inputLength); } if (size) { stack->pushString(buffer, size); } else { stack->pushNil(); } if (buffer) { free(buffer); } return 1; }
cocos2d::LUA_STRING Crypto::cryptAES256Lua(bool isDecrypt, const char* input, int inputLength, const char* key, int keyLength) { LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->clean(); if (getAES256KeyLength() == 0) { stack->pushNil(); return 1; } int bufferSize = inputLength + getAES256KeyLength(); void* buffer = malloc(bufferSize); int dataUsed = cryptAES256(isDecrypt, (unsigned char*)input, inputLength, (unsigned char*)buffer, bufferSize, (unsigned char*)key, keyLength); if (dataUsed > 0) { stack->pushString(static_cast<const char*>(buffer), dataUsed); } else { stack->pushNil(); } free(buffer); return 1; }
void MovieView::executeCallBack(int nHandle) { LuaEngine* pEngine = LuaEngine::getInstance(); LuaStack* pStack = pEngine->getLuaStack(); int nRet = pStack->executeFunctionByHandler(nHandle, 0); pStack->clean(); }
static int lua_cocos2dx_RadioButtonGroup_addEventListener(lua_State* L) { if (nullptr == L) return 0; int argc = 0; RadioButtonGroup* self = nullptr; #if COCOS2D_DEBUG >= 1 tolua_Error tolua_err; if (!tolua_isusertype(L,1,"ccui.RadioButtonGroup",0,&tolua_err)) goto tolua_lerror; #endif self = static_cast<RadioButtonGroup*>(tolua_tousertype(L,1,0)); #if COCOS2D_DEBUG >= 1 if (nullptr == self) { tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_RadioButtonGroup_addEventListener'\n", NULL); return 0; } #endif argc = lua_gettop(L) - 1; if (1 == argc) { #if COCOS2D_DEBUG >= 1 if (!toluafix_isfunction(L,2,"LUA_FUNCTION",0,&tolua_err)) { goto tolua_lerror; } #endif LUA_FUNCTION handler = ( toluafix_ref_function(L,2,0)); self->addEventListener([=](RadioButton* radioButton,int index,RadioButtonGroup::EventType eventType){ LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->pushObject(radioButton, "ccui.RadioButton"); stack->pushInt(index); stack->pushInt((int)eventType); stack->executeFunctionByHandler(handler, 3); stack->clean(); }); ScriptHandlerMgr::getInstance()->addCustomHandler((void*)self, handler); return 0; } luaL_error(L, "'addEventListener' function of RadioButtonGroup has wrong number of arguments: %d, was expecting %d\n", argc, 1); return 0; #if COCOS2D_DEBUG >= 1 tolua_lerror: tolua_error(L,"#ferror in function 'addEventListener'.",&tolua_err); return 0; #endif }
static int lua_cocos2dx_Widget_addClickEventListener(lua_State* L) { if (nullptr == L) return 0; int argc = 0; Widget* self = nullptr; #if COCOS2D_DEBUG >= 1 tolua_Error tolua_err; if (!tolua_isusertype(L,1,"ccui.Widget",0,&tolua_err)) goto tolua_lerror; #endif self = static_cast<Widget*>(tolua_tousertype(L,1,0)); #if COCOS2D_DEBUG >= 1 if (nullptr == self) { tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_Widget_addClickEventListener'\n", NULL); return 0; } #endif argc = lua_gettop(L) - 1; if (1 == argc) { #if COCOS2D_DEBUG >= 1 if (!toluafix_isfunction(L,2,"LUA_FUNCTION",0,&tolua_err)) { goto tolua_lerror; } #endif LUA_FUNCTION handler = ( toluafix_ref_function(L,2,0)); self->addClickEventListener([=](cocos2d::Ref* sender){ LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->pushObject(sender, "cc.Ref"); stack->executeFunctionByHandler(handler, 1); stack->clean(); }); ScriptHandlerMgr::getInstance()->addCustomHandler((void*)self, handler); return 0; } luaL_error(L, "'addClickEventListener' function of Widget has wrong number of arguments: %d, was expecting %d\n", argc, 1); return 0; #if COCOS2D_DEBUG >= 1 tolua_lerror: tolua_error(L,"#ferror in function 'addClickEventListener'.",&tolua_err); return 0; #endif }
int lua_game_manager_Http_get(lua_State* tolua_S) { int argc = 0; bool ok = true; #if COCOS2D_DEBUG >= 1 tolua_Error tolua_err; #endif game::manager::Http* cobj = (game::manager::Http*)tolua_tousertype(tolua_S,1,0); #if COCOS2D_DEBUG >= 1 //if (!tolua_isusertable(tolua_S,1,"game.manager.Http",0,&tolua_err)) goto tolua_lerror; #endif argc = lua_gettop(tolua_S) - 1; if (argc >= 1) { if(!ok) { tolua_error(tolua_S,"invalid arguments in function 'lua_game_manager_Http_get'", nullptr); return 0; } std::string url,postData; ok &= luaval_to_std_string(tolua_S, 2,&url, "game.manager.Http"); if(argc >= 2) { ok &= luaval_to_std_string(tolua_S, 3,&postData, "game.manager.Http"); } RequestCompleteCallBack completeCallback = nullptr; if (argc >= 3) { LUA_FUNCTION handler = ( toluafix_ref_function(tolua_S,4,0)); completeCallback = [=](const char* szBuffer){ LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->pushString(szBuffer); stack->executeFunctionByHandler(handler, 1); stack->clean(); }; } cobj->get(url,postData,completeCallback); return 1; } luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d\n ", "cc.Node:create",argc, 0); return 0; #if COCOS2D_DEBUG >= 1 tolua_lerror: tolua_error(tolua_S,"#ferror in function 'lua_game_manager_Http_get'.",&tolua_err); #endif return 0; }
static int handleUIEvent(int handler, cocos2d::Ref* sender, int eventType) { LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->pushObject(sender, "cc.Ref"); stack->pushInt(eventType); stack->executeFunctionByHandler(handler, 2); stack->clean(); return 0; }
void CNetDelegate::executeOnExceptionCaughtScriptHandler(CCSocketStatus eStatus) { if (m_nExceptionCaughtScriptHandler != 0) { LuaEngine* pEngine = LuaEngine::getInstance(); LuaStack* pStack = pEngine->getLuaStack(); pStack->pushInt((int)eStatus); pStack->executeFunctionByHandler(m_nExceptionCaughtScriptHandler, 1); pStack->clean(); } }
void CNetDelegate::executeOnMessageReceivedScriptHandler(CBuffer *oBuffer) { if (m_nMessageReceivedScriptHandler != 0) { LuaEngine* pEngine = LuaEngine::getInstance(); LuaStack* pStack = pEngine->getLuaStack(); pStack->pushObject(oBuffer, "CBuffer"); pStack->executeFunctionByHandler(m_nMessageReceivedScriptHandler, 1); pStack->clean(); } }
LUA_STRING Crypto::MD5FileLua(const char* path) { unsigned char buffer[MD5_BUFFER_LENGTH]; MD5File(path, buffer); LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->clean(); char* hex = bin2hex(buffer, MD5_BUFFER_LENGTH); stack->pushString(hex); delete[] hex; return 1; }
void CCMsgDelegate::executeOnMessageScriptHandler(unsigned int uMsg, Ref* pMsgObj) { if (m_nMessageScriptHandler != 0) { LuaEngine* pEngine = LuaEngine::getInstance(); LuaStack* pStack = pEngine->getLuaStack(); pStack->pushInt(uMsg); pStack->pushObject(pMsgObj, "Ref"); pStack->executeFunctionByHandler(m_nMessageScriptHandler, 2); pStack->clean(); } }
LUA_STRING Crypto::MD5StringLua(char* input, int inputLength) { unsigned char buffer[MD5_BUFFER_LENGTH]; MD5(static_cast<void*>(input), inputLength, buffer); LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->clean(); char* hex = bin2hex(buffer, MD5_BUFFER_LENGTH); stack->pushString(hex); delete[] hex; return 1; }
int HelperFunc::getFileData(const char *pPathFile) { unsigned long size; unsigned char* buf = HelperFunc::getFileData(pPathFile, "rb", &size); LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->clean(); if (buf) { stack->pushString((const char*)(buf), (int)size); delete buf; } else { stack->pushNil(); } return 1; }
void NBGCCBReader::completedAnimationSequenceNamed(const char *name) { if (luaHandler_animationCallback > 0) { auto engine = LuaEngine::getInstance(); LuaStack* stack = engine->getLuaStack(); stack->pushString(name); stack->executeFunctionByHandler(luaHandler_animationCallback, 1); stack->clean(); } if (animCompletedCallback != nullptr) { animCompletedCallback(name); } }
CSceneExtension* CSceneManager::loadScene(const char* pSceneName) { CCAssert(pSceneName && strlen(pSceneName), "should not null"); map<string, CSceneExtension*>::iterator mitr = m_mSceneCachePool.find(pSceneName); if( mitr != m_mSceneCachePool.end() ) { return mitr->second; } #if USING_LUA map<string,int>::iterator itr = m_mFnSceneCreateScriptFunc.find(pSceneName); if(itr!=m_mFnSceneCreateScriptFunc.end()) { LuaEngine* pEngine = LuaEngine::getInstance(); LuaStack* pStack = pEngine->getLuaStack(); __Array pRetArray; pRetArray.initWithCapacity(1); int nRet = pStack->executeFunctionReturnArray(itr->second, 0, 1, pRetArray); CCAssert(pRetArray.count() > 0, "return num = 0"); Ref* pReturnObject = pRetArray.getObjectAtIndex(0); pStack->clean(); CSceneExtension* pSceneExt = dynamic_cast<CSceneExtension*>(pReturnObject); pSceneExt->setClassName(pSceneName); return pSceneExt; } #else map<string, Fn_CreateSceneExtension>::iterator itr = m_mFnSceneCreatePointers.find(pSceneName); if( itr != m_mFnSceneCreatePointers.end() ) { CSceneExtension* pScene = (*itr->second)(); pScene->setClassName(pSceneName); pScene->init(); pScene->autorelease(); return pScene; } #endif return NULL; }
void CWidgetWindow::executeTouchCancelledAfterLongClickHandler(Ref* pSender, Touch *pTouch, float fDuration) { if( m_pTouchCancelledAfterLongClickListener && m_pTouchCancelledAfterLongClickHandler ) { (m_pTouchCancelledAfterLongClickListener->*m_pTouchCancelledAfterLongClickHandler)(pSender, pTouch, fDuration); } #if USING_LUA else if( m_pTouchCancelledAfterLongClickScriptHandler != 0 ) { LuaEngine* pEngine = LuaEngine::getInstance(); LuaStack* pStack = pEngine->getLuaStack(); pStack->pushObject(pSender, "Ref"); pStack->pushObject(pTouch, "Touch"); pStack->pushFloat(fDuration); int nRet = pStack->executeFunctionByHandler(m_pTouchCancelledAfterLongClickScriptHandler, 3); pStack->clean(); } #endif }
bool NBGCCBReader::onAssignCCBMemberVariable(Ref * pTarget, const char * pMemberVariableName, Node * node) { if (pTarget == this) { if (luaHandler_memberVarSetter > 0) { auto engine = LuaEngine::getInstance(); LuaStack* stack = engine->getLuaStack(); stack->pushString(pMemberVariableName); stack->pushObject(node, "cc.Node"); stack->executeFunctionByHandler(luaHandler_memberVarSetter, 2); stack->clean(); } if (assignMemVarCallback != nullptr) { return assignMemVarCallback(pTarget, pMemberVariableName, node); } } return false; }
LUA_STRING Crypto::decryptXXTEALua(const char* plaintext, int plaintextLength, const char* key, int keyLength) { LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->clean(); int resultLength; unsigned char* result = decryptXXTEA((unsigned char*)plaintext, plaintextLength, (unsigned char*)key, keyLength, &resultLength); if (resultLength <= 0) { lua_pushnil(stack->getLuaState()); } else { lua_pushlstring(stack->getLuaState(), (const char*)result, resultLength); free(result); } return 1; }
LUA_STRING Crypto::MD5Lua(const char* inputStr, bool isRawOutput) { unsigned char buffer[MD5_BUFFER_LENGTH]; char* input = (char*)inputStr; MD5(static_cast<void*>(input), (int)strlen(input), buffer); LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->clean(); if (isRawOutput) { stack->pushString((char*)buffer, MD5_BUFFER_LENGTH); } else { char* hex = bin2hex(buffer, MD5_BUFFER_LENGTH); stack->pushString(hex); delete[] hex; } return 1; }
static int tolua_cocos2dx_EventListenerPhysicsContact_registerScriptHandler(lua_State* tolua_S) { if (nullptr == tolua_S) return 0; int argc = 0; EventListenerPhysicsContact* self = nullptr; #if COCOS2D_DEBUG >= 1 tolua_Error tolua_err; if (!tolua_isusertype(tolua_S, 1, "cc.EventListenerPhysicsContact", 0, &tolua_err)) goto tolua_lerror; #endif self = static_cast<EventListenerPhysicsContact*>(tolua_tousertype(tolua_S,1,0)); #if COCOS2D_DEBUG >= 1 if (nullptr == self) { tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2dx_EventListenerPhysicsContact_registerScriptHandler'\n", nullptr); return 0; } #endif argc = lua_gettop(tolua_S) - 1; if (argc == 2) { #if COCOS2D_DEBUG >= 1 if (!toluafix_isfunction(tolua_S,2,"LUA_FUNCTION",0,&tolua_err) || !tolua_isnumber(tolua_S, 3, 0, &tolua_err)) { goto tolua_lerror; } #endif LUA_FUNCTION handler = toluafix_ref_function(tolua_S,2,0); ScriptHandlerMgr::HandlerType type = static_cast<ScriptHandlerMgr::HandlerType>((int)tolua_tonumber(tolua_S, 3, 0)); switch (type) { case ScriptHandlerMgr::HandlerType::EVENT_PHYSICS_CONTACT_BEGIN: { ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type); self->onContactBegin = [handler](PhysicsContact& contact) -> bool{ LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->pushObject(&contact, "cc.PhysicsContact"); bool ret = stack->executeFunctionByHandler(handler, 1); stack->clean(); return ret; }; } break; case ScriptHandlerMgr::HandlerType::EVENT_PHYSICS_CONTACT_PRESOLVE: { ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type); self->onContactPreSolve = [handler](PhysicsContact& contact, PhysicsContactPreSolve& solve) -> bool{ LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->pushObject(&contact, "cc.PhysicsContact"); tolua_pushusertype(stack->getLuaState(), &solve, "cc.PhysicsContactPreSolve"); bool ret = stack->executeFunctionByHandler(handler, 2); stack->clean(); return ret; }; } break; case ScriptHandlerMgr::HandlerType::EVENT_PHYSICS_CONTACT_POSTSOLVE: { ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type); self->onContactPostSolve = [handler](PhysicsContact& contact, const PhysicsContactPostSolve& solve){ LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->pushObject(&contact, "cc.PhysicsContact"); tolua_pushusertype(stack->getLuaState(), const_cast<PhysicsContactPostSolve*>(&solve), "cc.PhysicsContactPostSolve"); stack->executeFunctionByHandler(handler, 2); stack->clean(); }; } break; case ScriptHandlerMgr::HandlerType::EVENT_PHYSICS_CONTACT_SEPERATE: { ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type); self->onContactSeperate = [handler](PhysicsContact& contact){ LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->pushObject(&contact, "cc.PhysicsContact"); stack->executeFunctionByHandler(handler, 1); stack->clean(); }; } break; default: break; } return 0; } CCLOG("'registerScriptHandler' has wrong number of arguments: %d, was expecting %d\n", argc, 2); return 0; #if COCOS2D_DEBUG >= 1 tolua_lerror: tolua_error(tolua_S,"#ferror in function 'registerScriptHandler'.",&tolua_err); return 0; #endif }
static int SendSpineEventToLua(int nHandler, spine::SkeletonAnimation* node, int trackIndex, spEventType type, spEvent* event, int loopCount) { if (nHandler <= 0) { return 0; } if (NULL == ScriptEngineManager::getInstance()->getScriptEngine()) { return 0; } LuaStack *pStack = LuaEngine::getInstance()->getLuaStack(); if (NULL == pStack) { return 0; } lua_State *tolua_s = pStack->getLuaState(); if (NULL == tolua_s) { return 0; } int nRet = 0; spTrackEntry* entry = spAnimationState_getCurrent(node->state, trackIndex); std::string animationName = (entry && entry->animation) ? entry->animation->name : ""; std::string eventType = ""; switch (type) { case ANIMATION_START: eventType = "start"; break; case ANIMATION_END: eventType = "end"; break; case ANIMATION_COMPLETE: eventType = "complete"; break; case ANIMATION_EVENT: eventType = "event"; break; } LuaValueDict spineEvent; spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("type", LuaValue::stringValue(eventType))); spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("trackIndex", LuaValue::intValue(trackIndex))); spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("animation", LuaValue::stringValue(animationName))); spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("loopCount", LuaValue::intValue(loopCount))); if (NULL != event) { LuaValueDict eventData; eventData.insert(eventData.end(), LuaValueDict::value_type("name", LuaValue::stringValue(event->data->name))); eventData.insert(eventData.end(), LuaValueDict::value_type("intValue", LuaValue::intValue(event->intValue))); eventData.insert(eventData.end(), LuaValueDict::value_type("floatValue", LuaValue::floatValue(event->floatValue))); eventData.insert(eventData.end(), LuaValueDict::value_type("stringValue", LuaValue::stringValue(event->stringValue))); spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("eventData", LuaValue::dictValue(eventData))); } pStack->pushLuaValueDict(spineEvent); nRet = pStack->executeFunctionByHandler(nHandler, 1); pStack->clean(); return nRet; }
int lua_game_manager_Http_pushDownLoad(lua_State* tolua_S) { int argc = 0; bool ok = true; #if COCOS2D_DEBUG >= 1 tolua_Error tolua_err; #endif game::manager::Http* cobj = (game::manager::Http*)tolua_tousertype(tolua_S,1,0); #if COCOS2D_DEBUG >= 1 //if (!tolua_isusertable(tolua_S,1,"game.manager.Http",0,&tolua_err)) goto tolua_lerror; #endif argc = lua_gettop(tolua_S) - 1; if (argc >= 1) { if(!ok) { tolua_error(tolua_S,"invalid arguments in function 'lua_game_manager_Http_get'", nullptr); return 0; } std::string url; ok &= luaval_to_std_string(tolua_S, 2,&url, "game.manager.Http"); RequestCompleteCallBack completeCallback = nullptr; if (argc >= 2) { LUA_FUNCTION handler = ( toluafix_ref_function(tolua_S,3,0)); completeCallback = [=](const char* szBuffer){ LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->pushString(szBuffer); stack->executeFunctionByHandler(handler, 1); stack->clean(); }; } DownLoadProgressCallBack progressCallBack = nullptr; if (argc >= 3) { LUA_FUNCTION handler = ( toluafix_ref_function(tolua_S,4,0)); progressCallBack = [=](double total,double progress){ LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->pushFloat(total); stack->pushFloat(progress); stack->executeFunctionByHandler(handler, 2); stack->clean(); }; } DownErrorCallBack errorCallBack = nullptr; if (argc >= 3) { LUA_FUNCTION handler = ( toluafix_ref_function(tolua_S,5,0)); errorCallBack = [=](Downloader::Error error){ LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); stack->pushInt((int)error.code); stack->pushString(error.message.c_str(),error.message.size()); stack->executeFunctionByHandler(handler, 2); stack->clean(); }; } cobj->pushDownLoad(url,completeCallback,progressCallBack,errorCallBack); lua_settop(tolua_S, 1); return 1; } luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d\n ", "cc.Node:create",argc, 0); return 0; #if COCOS2D_DEBUG >= 1 tolua_lerror: tolua_error(tolua_S,"#ferror in function 'lua_game_manager_Http_get'.",&tolua_err); #endif return 0; }