cocos2dx toLua 没有PyYAML的解决办法

原创 2015年07月11日 10:29:27

What's new

  • Add a prebuilt libclang3.4 in libclang/for mac, linux and windows.
  • If you want use the prebuilt licbclang3.4 work with Android NDK, then only the NDK r9b is can work corrently with it.

Requirements

  • python2.7
  • py-yaml
  • cheetah (for target language templates)
  • libclang, from clang 3.3 or later

Usage

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

Basically, you specify a target vm (spidermonkey is the only current target vm) and the section from the .ini file you want to generate code for.

Run the simple test with prebuilt libclang3.4

Included in this repository is a simple test. Use this to confirm the generator is working and that your environment is set up correctly.

NOTE

  • The test uses the prebuilt 3.4 libclang, so you should use Android NDK r9b.
  • The test uses <string> and <stdint.h> so you need a C++ implementation that provides these
  • Currently, the test script is setup to use the Android NDK's GNU libstdc++

Mac OS X

  • The OSX 10.9 has a built-in python2.7 and if your os don't have python2.7 then use Homebrewto install the python and use pip install the python dependencies.

    brew install python
    
  • Install python dependices by pip.

    sudo easy_install pip
    sudo pip install PyYAML
    sudo pip install Cheetah
    
  • Download 64bit ndk-r9b-x86_64 from google

  • If you are using python installed from other way, copy user.cfg.sample and rename it as user.cfg then set the absolute path to python PYTHON_BIN in user.cfg
  • Run follow command, it will generate a userconf.ini, and check the values in it if it occorus any error.
    export NDK_ROOT=/path/to/android-ndk-r9b
    ./test.sh
    

Ubuntu Linux 12.04 64bit

  • Install python
    sudo apt-get install python2.7
    
  • Install python dependices by pip.
    sudo apt-get install python-pip
    sudo pip install PyYAML
    sudo pip install Cheetah
    
  • Download 64bit ndk-r9b-x86_64 from google
  • If you are using python installed from other way, copy user.cfg.sample and rename it as user.cfg then set the absolute path to python PYTHON_BIN in user.cfg
  • Run follow command, it will generate a userconf.ini, and check the values in it if it occorus any error.
    export NDK_ROOT=/path/to/android-ndk-r9b
    ./test.sh
    

Windows 7 64bit

Expected output

Upon running the test you might see some warnings but should not see any errors.

The test will create a directory named simple_test_bindings that contains 3 files

  • A .hpp header file for the bindings class
  • A .cpp file implementing the bindings class
  • A .js file that documents how to call (from JavaScript) the methods the C++ class exposes

The .ini file

The .ini file is a simple text file specifying the settings for the code generator. Here's the default one, used for cocos2d-x

[cocos2d-x]
prefix = cocos2dx
events  = CCNode#onEnter CCNode#onExit
extra_arguments = -I../../cocos2dx/include -I../../cocos2dx/platform -I../../cocos2dx/platform/ios -I../../cocos2dx -I../../cocos2dx/kazmath/include -arch i386 -DTARGET_OS_IPHONE -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk -x c++
headers = ../../cocos2dx/include/cocos2d.h
classes = CCSprite
functions = my_free_function

Required sections

  • prefix: the prefix for the project. Must be a valid identifier for the language of the target vm. Most of the time, the name will be intermixed between the class name and the function name, since all generated (probably) will be free functions, we do that in order to avoid name collition. The script will generate ${prefix}.cpp and ${prefix}.hpp as a result.
  • events: a list of identifiers in the form of ClassName#functionName that are events to be called from the native world to the target vm.
  • extra_arguments: extra arguments to pass to the clang interface. Basically you can think of this as the arguments to pass to the "compiler", so add as many as you need here. If you're targetting C++, make sure you add "-x c++" as the last argument to force C++ mode on a ".h" file. Otherwise, name your header files as ".hpp".
  • headers: list of headers to parse. Usually you add a single header that in turn #includes the rest of the files.
  • classes: the classes that will be parsed. Right not is just a string, but it will accept regular expressions
  • functions: space-separated list of free functions to be binded. Same as with classes, it will support regular expressions.
  • skip: a space-separated list of Classes::functions or just functions to not generate any code.

The templates

The generator is using Cheetah templates to create a more flexible generator. The way it was thought, is that for every target environment, you should provide with a way to generate the same C/C++ functionality. Every template has access to the proper meta information for the code or generator (function, classes, etc.)

Right now it's separated in the following set of templates:

  • prelude.c/.h: The header of the generated files.
  • ifunction.c/.h: The template for an instance function
  • ifunction_overloaded.c: The template for the implementation of an overloaded function. An overloaded function is exactly the same as a function, but it has an array of functions sharing the same name. The current implementation for spidermonkey only works if the overloading is with different number of arguments.
  • sfunction.c/.h: The template for a static function
  • sfunction_overloaded.c: The template for an overloaded static function
  • register.c: Here you should add the constructor/finalizer, the registration function (if needed) and the footer of the header file. This is the last chunk being generated

Templates are stored in the templates/${target} directory and follow the naming specified above.

One final part of the puzzle is the ${target}.yaml file, that contains specific type conversion snippets to be used by the templates. For instance, for spidermonkey, this is the place where we specify the conversion routines for the native types (to and from int, float, string, etc.)

Limitations

Currently the generator is leveraging clang in order to get information about the C/C++ code, so we can only get as much information as clang give us. Known list of things that won't work:

  • variable number of arguments. Solution: write a manual wrapper

Xcode环境下tolua++的使用方法

Xcode环境:cocos2dx-lua中如何利用tolua++工具调用自定义类 参考文档:http://www.cocos2dev.com/?p=405 感觉自己...
  • u013368288
  • u013368288
  • 2014年03月10日 18:09
  • 888

cocos2dx tolua ++ 使用 常见得错误

步骤: 1、安装必要的库和工具包,以及配置相关环境变量,请按照cocos2d-x-3.0rc0\tools\tolua\README.mdown说得去做 (注意:尽量用README.mdown说...
  • liqiangxo
  • liqiangxo
  • 2017年03月06日 14:02
  • 483

cocos2dx tolua 再见了pkg

3.x的版本就不说,小伙伴们自己看文档,都能搞定         主要说下2.x版本的tolua吧         本人用的2.2.6版本。。。好吧。。表示蛋疼,以前用的斗士3.x版本,tolua分分...
  • u010080775
  • u010080775
  • 2016年08月16日 10:55
  • 315

cocos2d-x 3.15.1 Win10 tolua++环境配置以及遇到的坑

1.      Python版本 V2.7.x,最新为v2.7.13 2.      安装pip pip 9.0.1 https://pypi.python.org/pypi/pip ...
  • sonikk
  • sonikk
  • 2017年06月15日 20:27
  • 801

cocos2d-x lua tolua++ 面向对象 / cocos2dx3.0 lua C++交互

cocos2d-x lua 中集成了tolua++,所以在cocos2d-x lua实际开发中,可以直接用利用其面向对象特性,包括 封装 和 继承,无多态类实例User.lualocal User =...
  • qq_26437925
  • qq_26437925
  • 2016年07月06日 21:15
  • 1632

cocos2dx tolua传递参数分析

cocos2dx tolua传递参数分析: tolua_Cocos2d_CCNode_addChild00 == void CCNode::addChild(CCNode *child) tolua...
  • tianxiawuzhei
  • tianxiawuzhei
  • 2015年03月31日 10:51
  • 1280

cocos2dx tolua c++

E:\source\client\core\frameworks\runtime-src\Classes\lua-stub里面的stub.cpp就是给lua调用的  Stub.cpp Stub.h...
  • ningcaicheng
  • ningcaicheng
  • 2015年09月01日 14:05
  • 802

入手cocos2dx3.9笔记 3 tolua++解析

因为需求,最近又重新回忆了下tolua++的相关流程, 以 3.9的 lua stack 的初始化相关代码开始解析: _state = lua_open(); luaL_openli...
  • zh4222011990
  • zh4222011990
  • 2015年12月02日 00:01
  • 2796

Cocos2d-Lua 3.12 生成绑定 导出C++类给LUA调用( tolua genbindings.py 的使用)

工具:Python、pyyaml、pyCheetah都用32位版本 (可避免很多错误) (ps:tolua目录下README.mdown里有下载链接) frameworks\cocos2d-x...
  • linyiquan
  • linyiquan
  • 2017年04月29日 17:51
  • 989

tolua 一些可以用的函数(测试过)

tolua 提供几个 C++ 与 Lua 进行数据交换的工具函数。 ~~ tolua.type 返回一个 C++ 对象的类型描述字符串。 local node =...
  • zky1347888
  • zky1347888
  • 2014年06月11日 18:52
  • 7391
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:cocos2dx toLua 没有PyYAML的解决办法
举报原因:
原因补充:

(最多只允许输入30个字)