找回密码
 立即注册
查看: 331|回复: 0

cocos2dx之使用bindings-generator 工具生成C++类为ToLua接口

[复制链接]
发表于 2023-4-9 23:17 | 显示全部楼层 |阅读模式
前言
原项目使用的是cocos2d-x4.0开发,但是中途想加入热更新特性.提到热更新,首先想到的是Lua.Lua作为一门脚本语言,运行效率也是极高的,并且语法简单,易于使用.
为了在不影响当前项目的情况下接入Lua,我考虑使用cocos官方提供的一套工具链, cocos(C++) + tolua + lua +luacocos2d
如何将luacocos2d接入现有cocos2d-cpp项目中,可以参考我的其他文章 , 本篇仅介绍如何使用工具将c++代码生成为tolua接口代码
一、环境
    windows10python2.7cocos2d-x 4.0NDK r16 +vscode
二、安装python与环境配置
    在python官网下载python2.7.18,这里一定要选32位的,不能选64位,因为NDK包里的 libclang.dll 是32位的. 默认安装就行,我的安装目录是 C:\Python27添加python环境变量量



点击确定保存
    测试 python和pip是否可使用
    打开cmd ,输入 :
  1. python --version
  2. pip --version
复制代码
能正确显示版本信息就表示环境配置成功



三、安装 NDK
    从谷歌或者国内镜像网站下载NDK解压之后,添加NDK环境变量


    安装vscode和 cocos2dx在这里就不多说,官网下载安装/解压即可
    进行完以上步骤,就可以开始我们的tolua代码生成了
正式使用代码生成工具
    使用vscode打开tolua文件目录
    我的是 D:\cocos2d-x-4.0\tools\tolua,这一步很重要,如果打开目录错误的话,执行脚本的时候会报错,我也是踩了好多坑,[手动哭]


可以看到文件目录下有一个genbindings.py ,这个就是我们要运行的python文件,接下来我们配置一个vscode运行python的调试配置
调试配置
打开运行与调试,添加一个调试配置



新建一个test.py,打开并输入 print ‘helloworld’,按F5,可以看到我们的python脚本运行起来了,在终端中输出了helloworld



接下来我们就可以执行我们的genbindings.py,顺利的话就会出现如下输出





编写生成配置文件
打开D:\cocos2d-x-4.0\cocos\scripting\lua-bindings\auto目录,可以看到生成的所有cocos2dx的tolua接口,接下来我们创建自己的配置文件,生成自定义的C++类.
回到vscod,从genbindings.py中我们可以看到,整个脚本依赖于一些ini配置文件



这里我编写了一个自定义的配置文件
  1. [doh_data]#section名
  2. prefix = doh_data  #输出的前缀,会连接到tolua类型的函数名之前,例如 int doh_data_initWithFile(lua_State* tolua_S);# 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
  3. target_namespace = doh_data  #所有生成的类都属于这个表下# 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".
  4. cpp_namespace = doh_data  #C++中的命名空间
  5. android_headers =#这是安卓编译的一些指令,不需要修改,照抄就行
  6. android_flags = -target armv7-none-linux-androideabi -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -DANDROID -D__ANDROID_API__=14 -gcc-toolchain %(gcc_toolchain_dir)s --sysroot=%(androidndkdir)s/platforms/android-14/arch-arm  -idirafter %(androidndkdir)s/sources/android/support/include -idirafter %(androidndkdir)s/sysroot/usr/include -idirafter %(androidndkdir)s/sysroot/usr/include/arm-linux-androideabi -idirafter %(clangllvmdir)s/lib64/clang/5.0/include -I%(androidndkdir)s/sources/cxx-stl/llvm-libc++/include
  7. clang_headers =#这是Clang的编译指令,不需要修改,照抄就行
  8. clang_flags = -nostdinc -x c++ -std=c++11 -fsigned-char -U__SSE__
  9. #自定义C++代码的搜索目录 其中dohdir是在userconf.ini中添加的
  10. doh_headers = -I%(dohdir)s/Classes -I%(dohdir)s/doh-proto -I%(dohdir)s/dependencies
  11. #cocos的头文件搜索目录
  12. cocos_headers = -I%(cocosdir)s -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s/cocos/platform/android -I%(cocosdir)s/external -I%(cocosdir)s/extensions
  13. cocos_flags = -DANDROID
  14. cxxgenerator_headers =# extra arguments for clang#所有需要用到的文件路径
  15. extra_arguments = %(doh_headers)s  %(android_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s %(clang_flags)s %(cocos_flags)s %(extra_flags)s
  16. # what headers to parse#这是需要解析的头文件 会从这个文件中识别所有include的头文件,并解析其中的类, 可以填多个文件
  17. headers = %(dohdir)s/Classes/data_mgr/data_include.h
  18. # 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*$".#需要生成哪些类的接口,这里支持正则表达式
  19. classes = DataManager DataHandler ^(\w)*Info$ ^(\w)*Manager$
  20. # 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.#跳过哪些类和函数,支持正则表达式,可以借鉴cocos的配置文件
  21. skip =
  22. rename_functions =
  23. rename_classes =# for all class names, should we remove something when registering in the target VM?
  24. remove_prefix =# classes for which there will be no "parent" lookup
  25. classes_have_no_parents =# base classes which will be skipped when their sub-classes found them.
  26. base_classes_to_skip =# classes that create no constructor# Set is special and we will use a hand-written constructor#哪些类没有创建构造函数,需要手动重写构造函数
  27. 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'.#这个没有用到
  28. script_control_cpp = no
复制代码
写好配置文件之后,记得在genbindings.py文件中修改 cmd_args ,按F5运行python脚本
遇到问题
在编译中可能会遇到以下问题
    details = “xxx/xxx.h” file not found
    例如:


    这是因为在配置文件中没有添加对应的文件路径
    解决办法: 在ini文件中添加缺失文件对应的目录即可
    The namespace (xxx::xxx) conversion wasn’t set in ‘ns_map’ section of the conversions.yaml
    这是因为生成脚本没有找到对应的命名空间配置
    解决办法: 在tolua文件同目录有个bindings-generator目录,打开 bindings-generator/targets/lua/conversions.yaml,在ns_map下按照指定格式添加C++类中的命名空间,类似于这样



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-22 19:49 , Processed in 0.089690 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表