cocos2dx3.x tolua

请确保你运行引擎自带的\frameworks\cocos2d-x\tools\tolua\genbindings.py 是成功的

目录结构

1.你必须知道你的目录结构,下面说说我的目录结构
cocos_root: F:\LuaFrameWork_3.13.1\frameworks\cocos2d-x
Class: F:\LuaFrameWork_3.13.1\frameworks\runtime-src\Classes

下面这个是我的演示目录,是py文件和ini和需要转换成lua的cpp文件都丢到这里面
F:\LuaFrameWork_3.13.1\frameworks\runtime-src\Classes\AsioNet\tolua3.13

编写py文件

到 $(cocos_root)\tools\tolua 下拷贝genbindings.py 和 cocos2dx_ui.ini(随意吧,如果出问题建议和我一样)
修改命名 genbindings.py -》lua_asio_genbindings.py
cocos2dx_ui.ini -》lua_asio.ini

讲解 你可以在代码里面找到对应的代码,请注意看注释

// 这里本意是cocos_root路径,
而我的演示目录在F:\LuaFrameWork_3.13.1\frameworks\runtime-src\Classes\AsioNet\tolua3.13
所以我需要将project_root 索引到cocosx路径

os.path.join(os.path.dirname(_file_) 这个等于py的路径,就是你编写的py所存放的路径,
相当于我的目录F:\LuaFrameWork_3.13.1\frameworks\runtime-src\Classes\AsioNet\tolua3.13

project_root = os.path.abspath(os.path.join(os.path.dirname(file), ‘..’, ‘..’)) // 源代码

 //修改后的代码, 这是你需要的,这是你需要修改的第一个位置
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', "cocos2d-x"))
cocos_root = os.path.abspath(os.path.join(project_root, ''))


cxx_generator_root = os.path.abspath(os.path.join(project_root, 'tools/bindings-generator'))

# 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', 'extra_flags', '')




 try:


    tolua_root = '%s/tools/tolua' % project_root// 源代码
    output_dir = '%s/cocos/scripting/lua-bindings/auto' % project_root//源代码
    // 第2个位置
        // project_root相当于cocos-x的目录,
        tolua_root = '%s/../runtime-src/Classes/AsioNet/tolua3.13' % project_root// 你需要转换的cpp文件路径
        output_dir = '%s/../runtime-src/Classes/AsioNet/tolua3.13' % project_root// 生成后的lua目录
    //修改后的
    cmd_args = {'lua_asio.ini' : ('lua_asio', 'tolua_asio') }

    target = 'lua'
    generator_py = '%s/generator.py' % cxx_generator_root

运行你的py文件

运行py文件需注意

在编写ini文件

[lua_asio] // 需修改成ini的文件名
# the prefix to be added to the generated functions. You might or might not use this in your own
# templates
prefix = lua_asio// 需修改成ini的文件名

# create a target namespace (in javascript, this would create some code like the equiv. to `ns = ns || {}`)
# all classes will be embedded in that namespace
target_namespace = 

# the native namespace in which this module locates, this parameter is used for avoid conflict of the same class name in different modules, as "cocos2d::Label" <-> "cocos2d::ui::Label".
cpp_namespace = 

android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/include
android_flags = -D_SIZE_T_DEFINED_ 

clang_headers = -I%(clangllvmdir)s/lib/clang/%(clang_version)s/include 
clang_flags = -nostdinc -x c++ -std=c++11 -U __SSE__

cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s/cocos/platform/android

cocos_flags = -DANDROID

cxxgenerator_headers = 

# extra arguments for clang
extra_arguments = %(android_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s %(clang_flags)s %(cocos_flags)s %(extra_flags)s 

# what headers to parse
// 需要生成所在的cpp的路径, cocosdir =cocos2d-x路径,我这里生成是是NetMessage 文件
headers = %(cocosdir)s/../runtime-src/Classes/AsioNet/tolua3.13/NetMessage.h

# what classes to produce code for. You can use regular expressions here. When testing the regular
# expression, it will be enclosed in "^$", like this: "^Menu*$".
classes = CNetMessage// 类名,这个要注意了,你在C++的类名,而不是文件名!!是类名,注意,我的c++里面是有C的,
                    //如果少了个C则生成的文件是,错误的,可能不会报错!!!!!!!!!!

# what should we skip? in the format ClassName::[function function]
# ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also
# regular expressions, they will not be surrounded by "^$". If you want to skip a whole class, just
# add a single "*" as functions. See bellow for several examples. A special class name is "*", which
# will apply to all class names. This is a convenience wildcard to be able to skip similar named
# functions from all classes.
// 不导出的方法格式为 ClassName::[function function],这个一般没用
skip = 

rename_functions = 

rename_classes =

# for all class names, should we remove something when registering in the target VM?
remove_prefix = 

# classes for which there will be no "parent" lookup
classes_have_no_parents = 

# base classes which will be skipped when their sub-classes found them.
base_classes_to_skip =

# classes that create no constructor
# Set is special and we will use a hand-written constructor
abstract_classes = 

# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'.
script_control_cpp = no

如果你看不懂,你可以参考其他人的配置,但是我注释的地方,你可以参考下

踩过的坑

这里写图片描述

这里写图片描述

raise ValueError(“no path specified”) 如果报这个错误,可能是你headers写了头文件 而classes没写

如果你的导出文件包含很多外部头文件,可能会一直报错,这时候你最好是在封装一层,这样就可以轻松导出
比如你原本要导出A文件,但是一直报第三方的库的头文件错误,这时候你在把A封装到B,保证B的.h文件不使用到第三方的文件即可。

http://www.cocoachina.com/bbs/read.php?tid=196037 官方的帖子

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值