cocos2d-x lua调用c++

30 篇文章 0 订阅
27 篇文章 0 订阅

在appDelegate.cpp文件中注册lua

auto engine = LuaEngine::getInstance();
    ScriptEngineManager::getInstance()->setScriptEngine(engine);
    lua_State* L = engine->getLuaStack()->getLuaState();
    lua_module_register(L);
    tolua_net_open(L);
    register_all_packages();



// .h文件

#ifndef LuaTool_h
#define LuaTool_h

extern "C" {
#include "tolua++.h"
#include "tolua_fix.h"
}

#include <map>
#include <string>
#include "tolua_fix.h"

TOLUA_API int tolua_net_open(lua_State* tolua_S);


#endif /* LuaTool_h */


// .cpp文件


#include "LuaTool.h"

#include "cocos2d.h"
#include "cocos-ext.h"

#include "CCLuaValue.h"
#include "cocos-ext.h"
#include "string.h"

#include "CCLuaEngine.h"
#include "JniUtil.h"


#define LUA_STACK           cocos2d::LuaEngine::getInstance()->getLuaStack()

using namespace cocos2d;


// 应用计费点id priciePointId
// 计费点对应的金额 money
// 渠道号 qd

static int tolua_SendPayInfo( lua_State* tolua_S )
{
    CCLOG("tolua_Syssendjava");
    const char* priciePointId = tolua_tocppstring(tolua_S,1,0);
    const char* money = tolua_tocppstring(tolua_S,2,0);
    const char* qd = tolua_tocppstring(tolua_S,3,0);
    CCLOG("计费点:%s %s %s",priciePointId,money,qd);
    sendPayInfo(priciePointId,money,qd);
    return 0;
}

// 获取验证码 点击
static int tolua_SysSendPhoneCode( lua_State* tolua_S )
{
    const char* phoneNum = tolua_tocppstring(tolua_S,1,0);
    //CSysSendPhoneCode(phoneNum);
    
    return 0;
}

//绑定手机 点击

static int tolua_SysSendPhoneInformation( lua_State* tolua_S )
{
    const char* phone = tolua_tocppstring(tolua_S,1,0);
    const char* code = tolua_tocppstring(tolua_S,1,0);
    //CSysSendPhoneInformation(phone,code);
    
    return 0;
}

// 登陆
static int tolua_SysNetLoginGame( lua_State* tolua_S )

{
    int a =    tolua_tonumber(tolua_S,1,0);
    int b =    tolua_tonumber(tolua_S,2,0);
    const char* c =    tolua_tocppstring(tolua_S,3,0);
    
    CCLOG("SysNetLoginGame=======%d,%d,%s",a,b,c);
    const char* d =    tolua_tocppstring(tolua_S,4,0);
    //CSysNetLoginGame(a, b, c,d);
    
    return 0;
}
//数据类型
static int tolua_SetSysNetDataType(lua_State* tolua_S)
{
    
    const char* data =  tolua_tocppstring(tolua_S,1,0);
    const char* scoket_ip =  tolua_tocppstring(tolua_S,2,0);
    int port =  tolua_tonumber(tolua_S,3,0);
    
    //CSetSysNetDataType(data,scoket_ip,port);
    return 0;
}

//获取机器信息
static int tolua_SysMachineInfo(lua_State* tolua_S)
{
    //CSysMachineInfo();
    return 0;
}

//关闭socket
static int tolua_ColseSocket(lua_State* tolua_S)
{
    //CColseSocket();
    return 0;
}

static int tolua_getName( lua_State* tolua_S)
{
    CCLOG("tolua_getName");
    //std::string tolua_ret = "name";
    //tolua_pushcppstring(tolua_S,(const char*)tolua_ret);
    //return 1;
    
 
    
    
//    LuaEngine::getInstance()->getLuaStack()->pushInt(1);
//    LuaEngine::getInstance()->getLuaStack()->pushInt(2);
//    LUA_STACK->pushString("tangrenjie");
//    LUA_STACK->pushString("hahhahah");
//    LuaEngine::getInstance()->executeGlobalFunction("ConnectLoginCallback");
//    
//     return 1;

}
//MD5 加密
//
//static int tolua_makemd5(lua_State* tolua_S)
//{
//    std::string strString = "";
//#ifndef TOLUA_RELEASE
//    tolua_Error tolua_err;
//    if( !tolua_isstring(tolua_S,1,0,&tolua_err))
//    {
//        goto tolua_lerror;
//    }
//    else
//#endif
//    {
//        std::string keyStr = tolua_tocppstring(tolua_S,1,"");
//        //        const char* tolua_ret = g_MainApp.MakeMd5(keyStr);
//        //        tolua_pushstring(tolua_S,tolua_ret);
//        
//        char* data;
//        int len  =  keyStr.length();
//        data = (char *)malloc((len+1)*sizeof(char));
//        memset(data, 0, sizeof(data));
//        keyStr.copy(data, len, 0);
//        data[len] = '\0';
//        CCLOG("MakeMd5 %s", keyStr.c_str());
//        CCLOG("MakeMd5 %s", data);
//
//        
//        char buffer[33];
//        CMD5Encrypt::EncryptData(data, buffer);
//        tolua_pushstring(tolua_S, buffer);
//        //free(data);
//
//    }
//    return 1;
//#ifndef TOLUA_RELEASE
//tolua_lerror:
//    tolua_error(tolua_S,"#ferror in function 'tolua_makemd5'.",&tolua_err);
//    return 0;
//#endif
//}
// 解析json 文件
static int tolua_mainapp_loadstring(lua_State* tolua_S)
{
    int nRet = 0;
    std::string strString = "";
    
#ifndef TOLUA_RELEASE
    tolua_Error tolua_err;
    if(    !tolua_iscppstring(tolua_S, 1, 0, &tolua_err) )
    {
        goto tolua_lerror;
    }
    else
#endif
    {
        std::string strFileName = tolua_tocppstring(tolua_S,1,"");
        
        
        std::string fullfilename = CCFileUtils::sharedFileUtils()->fullPathForFilename(strFileName);
        
        ssize_t dwFileSize = 0;
        unsigned char* pBuffer = CCFileUtils::sharedFileUtils()->getFileData(fullfilename, "rb", &dwFileSize);
        if( (!pBuffer) || (dwFileSize<=0) )
            return 0;
        
        char* temp = new char[dwFileSize+1];
        memset(temp, 0, dwFileSize);
        memcpy(temp, pBuffer, dwFileSize);
        temp[dwFileSize] = '\0';
        
        strString = temp;
        
        CC_SAFE_DELETE_ARRAY(pBuffer);
        CC_SAFE_DELETE_ARRAY(temp);
        
        tolua_pushcppstring(tolua_S, strString);
        return 1;
    }
#ifndef TOLUA_RELEASE
tolua_lerror:
    tolua_error(tolua_S,"#ferror in function 'tolua_mainapp_loadstring'.",&tolua_err);
#endif
    
    tolua_pushcppstring(tolua_S, strString);
    return 1;}
TOLUA_API int tolua_net_open(lua_State* tolua_S)
{
    tolua_open(tolua_S);
    tolua_module(tolua_S,NULL,0);
    
    tolua_beginmodule(tolua_S,NULL);
         tolua_function(tolua_S,"SysSendPhoneCode",            tolua_SysSendPhoneCode);
         tolua_function(tolua_S,"SysSendPhoneInformation",            tolua_SysSendPhoneInformation);
         tolua_function(tolua_S,"SysNetLoginGame",            tolua_SysNetLoginGame);
         tolua_function(tolua_S,"SysSendPhoneInformation",            tolua_SysSendPhoneInformation);
         tolua_function(tolua_S,"SetSysNetDataType",            tolua_SetSysNetDataType);
         tolua_function(tolua_S,"SysMachineInfo",              tolua_SysMachineInfo);
         tolua_function(tolua_S, "ColseSocket", tolua_ColseSocket);
        // tolua_function(tolua_S, "MakeMD5", tolua_makemd5); tolua_Syssendjava
         tolua_function(tolua_S,"getName",            tolua_getName);
         tolua_function(tolua_S,"mainapp_loadstring",            tolua_mainapp_loadstring);
         tolua_function(tolua_S,"SendPayInfo",            tolua_SendPayInfo);
    
    tolua_endmodule(tolua_S);
    
    return 1;
    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值