cocos:C++ 导出到lua, genbindings.py修改

1. 准备

把tools目录下的cocos2dx_extension.ini, genbindings.py, userconf.ini拷贝到

一个新的目录下,作为修改模板

2. 修改genbindings.py -> build.py

2.1

     NDK_ROOT = "/Users/staff/Documents/worksoft/android-ndk-r9" 
        必须是r9的不能用最新的r10

2.2 三个目录,都是绝对路径

1、 工程目录,根据自己的实际情况获取
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
2. cocos_root 目录,也就是cococ2dx所在的目录
cocos_root = os.path.abspath(os.path.join(project_root, '../cocos2dx'))
3. cxx_generator_root目录,tools/bindings-generator目录,也就是generator.py所在目录,tolua的工具路径
cxx_generator_root = os.path.abspath(os.path.join(project_root, 'tools/bindings-generator'))

2.3 保存设置到userconf.ini文件,所以这个文件不需要拷贝

保存的配置信息都是上面获取,基本保持和genbindings.py一样,这里值添加了一个zqdir目录,也就是我们的工程目录,这里的配置是在cocos2dx_extension.ini这样的配置文件中使用的
 # save config to file 
    config = ConfigParser.ConfigParser()
    config.set('DEFAULT', 'androidndkdir', ndk_root)
    config.set('DEFAULT', 'clangllvmdir', llvm_path)
    config.set('DEFAULT', 'cocosdir', cocos_root)
    config.set('DEFAULT', 'cxxgeneratordir', cxx_generator_root)
    config.set('DEFAULT', "zqdir", project_root)
    config.set('DEFAULT', 'extra_flags', '')


    conf_ini_file = os.path.abspath(os.path.join(os.path.dirname(__file__), 'userconf.ini'))

    print 'generating userconf.ini...'
    with open(conf_ini_file, 'w') as configfile:
      config.write(configfile)

2.4 两个目录,都是绝对路径

1. tolua_root类似于tools/tolua目录,也就是cocos2dx_extension.ini文件所在目录
 tolua_root = '%s/auto_build' % project_root
2. output_dir 输出目录,也就生成的.h 和 .cpp文件的目录
 output_dir = '%s/bindings' % project_root

2.5 cmd_args 配置

     //zq.ini配置文件名
        //'zq' -s SECTION   sets a specific section to be converted, 我们知道ini的配置文件都需要有section,每个section下面有很多配置项,这里对应zq.ini中的[zq] section,zq.ini中 
>>[zq] //zq section
>>the prefix to be added to the generated functions. You might or might not 
>>use this in your own
>>templates
>>prefix = zq
        
        //lua_zq_auto 生成的.h 和 .cpp文件名
 cmd_args = {
            'zq.ini': ('zq', 'lua_zq_auto')
        }
        
        //下面这些没有改动
        target = 'lua' //生成目标
        
        //generator.py文件
        generator_py = '%s/generator.py' % cxx_generator_root  
        
        //循环生成       
        for key in cmd_args.keys():
            args = cmd_args[key]
            cfg = '%s/%s' % (tolua_root, key) //配置文件zq.ini文件
            print 'Generating bindings for %s...' % (key[:-4])
            //执行generator.py [options] {configfile} 命令
//generator.py --help
//              Usage: generator.py [options] {configfile}
                
//              Options:
//                -h, --help   show this help message and exit
//                -s SECTION   sets a specific section to be converted
//                -t TARGET    specifies the target vm. Will search for TARGET.yaml
//                -o OUTDIR    specifies the output directory for generated C++ code
//                -n OUT_FILE  specifcies the name of the output file, defaults to the prefix
                               in the .ini file

            command = '%s %s %s -s %s -t %s -o %s -n %s' % (python_bin, generator_py, cfg, args[0], target, output_dir, args[1])
            _run_cmd(command)