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


点击确定保存
- 测试 python和pip是否可使用
打开cmd ,输入 :
python --version
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配置文件

这里我编写了一个自定义的配置文件
[doh_data] #section名
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
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".
cpp_namespace = doh_data #C++中的命名空间
android_headers =
#这是安卓编译的一些指令,不需要修改,照抄就行
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
clang_headers =
#这是Clang的编译指令,不需要修改,照抄就行
clang_flags = -nostdinc -x c++ -std=c++11 -fsigned-char -U__SSE__
#自定义C++代码的搜索目录 其中dohdir是在userconf.ini中添加的
doh_headers = -I%(dohdir)s/Classes -I%(dohdir)s/doh-proto -I%(dohdir)s/dependencies
#cocos的头文件搜索目录
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
cocos_flags = -DANDROID
cxxgenerator_headers =
# extra arguments for clang
#所有需要用到的文件路径
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
# what headers to parse
#这是需要解析的头文件 会从这个文件中识别所有include的头文件,并解析其中的类, 可以填多个文件
headers = %(dohdir)s/Classes/data_mgr/data_include.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 = DataManager DataHandler ^(\w)*Info$ ^(\w)*Manager$
# 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的配置文件
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
写好配置文件之后,记得在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++类中的命名空间,类似于这样

本文介绍了如何在cocos2d-x 4.0项目中接入lua热更新功能,通过cocos、tolua、lua和luacocos2d工具链,将C++代码转换为tolua接口。详细步骤包括环境配置(Python 2.7、NDK)、tolua代码生成工具的使用、配置文件编写以及解决编译过程中可能遇到的问题。

910

被折叠的 条评论
为什么被折叠?



