[PYTHON] [Cocos2d-x] How to make Script Binding (Part 2)

It is a continuation of part 1 [Cocos2d-x] How to make Script Binding (Part 1)

Now let's see how to implement bindings-generator.

Create a file for Lang language

First, create a directory for the Lang language under the tools directory of Cococ2d-x. It's hard to create from scratch, so I'll copy the Lua directory.

cp -r tools/tolua tools/lang
cp -r tools/bindings-generator/targets/lua tools/bindings-generator/targets/lang

First, let's take a look at the files in the * tools / lang * directory. I think there are some Python scripts called * genbindings.py * and some ini files that start with "cocos2dx".

Open * genbindings.py * and rewrite the following part of the try ~ except clause.

try:

    lang_root = '%s/tools/lang' % project_root
    output_dir = '%s/cocos/scripting/lang-bindings/auto' % project_root

    cmd_args = {'cocos2dx.ini' : ('cocos2d-x', 'lang_cocos2dx_auto'), \
                }
    target = 'lang'
    generator_py = '%s/generator.py' % cxx_generator_root
    for key in cmd_args.keys():
        args = cmd_args[key]
        cfg = '%s/%s' % (lang_root, key)
        print 'Generating bindings for %s...' % (key[:-4])
        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)

    if platform == 'win32':
        with _pushd(output_dir):
            _run_cmd('dos2unix *')

    print '---------------------------------'
    print 'Generating lang bindings succeeds.'
    print '---------------------------------'

except Exception as e:
    if e.__class__.__name__ == 'CmdError':
        print '---------------------------------'
        print 'Generating lang bindings fails.'
        print '---------------------------------'
        sys.exit(1)
    else:
        raise

I feel like this.

In cmd_args, describe the source to be generated and its configuration file. Since it is difficult to handle everything from the beginning, we will limit it to the most basic "cocos2d-x.ini". When "cocos2d-x.ini" is perfect, I think it's a good idea to increase the supported files one by one.

Next, let's take a look at the contents of "cocos2d-x.ini". There are various settings, but I think the items that are likely to be rewritten are as follows.

The C ++ header to parse. Analyze by following include starting from the specified header file.

The class to be generated.

Describe the method to be excluded in the class specified by classes. For example, * Sprite :: [getQuad] * excludes the getQuad method of the Sprite class. [*] Means all methods.

Specify when the method name created on the Lang language is different from the C ++ method name. Shorten long method names.

Specify when the class name created on the Lang language is different from the C ++ method name.

Specify an abstract class. (Do not make a constructor)

After checking the contents of the ini file, close it without changing anything.

Now let's run * bindings-generator * in this state to generate the glue code. Execute * genbindings.py * that you rewrote earlier. After some logs, if "Generating lang bindings succeeds." Is displayed, it is successful. If successful, the following files should have been generated:

cocos2d-x/cocos/scripting/lang-bindings/auto/lang_cocos2dx_auto.hpp cocos2d-x/cocos/scripting/lang-bindings/auto/lang_cocos2dx_auto.cpp

I haven't changed anything in the generated code yet, so the contents are still written for Lua.

Edit template file for glue code generation

To change the generated code, change the template file under the following directory. Note that the bindings-generator uses a Python template engine called Cheetah. You will write according to Cheetah's grammar.

cocos2d-x/tools/bindings-generator/targets/lang/templates/

Below the templates directory are the following template files:

Output at the beginning of the generated .hpp file. Write #include etc. The prototype of the register_all_cocos2dx function is written here.

Output at the beginning of the generated .cpp file. Write #include etc.

Output at the end of the generated .hpp file.

Output at the end of the generated .cpp file. Implement the register_all_cocos2dx function and Write the process to call the register method defined for each class.

Output at the beginning of a group for each class.

Output at the beginning of a group for each class. Write any code you want to generate for each class.

Implement the lang_register_cocos2dx_ * function. A collection of binding registration functions for each class.

Output for each member function.

Output for each member function. If it is overloaded, this will be output.

Output for each static member function.

Output for each static member function. If it is overloaded, this will be output.

Output when binding std :: function.

Output as a .hpp file for each member function. (* function * .c common file)

If the function is overloaded, it will be in a separate file, but if it is overloaded, this will be a separate file. You need to determine which overloaded function to execute based on the number and type of arguments passed from the Lang language. This is because the process becomes complicated. Also, I think there is a template called apidoc_ *, but this is for apidoc It is for outputting the signature of the function generated on the Lang language as a comment.

You need to edit the following Yaml file related to the template. cocos2d-x/tools/bindings-generator/targets/lang/conversions.yaml

As you can see from the contents, it is a file that defines the type conversion method. Use the type conversion function between C ++ and Lang languages to define the conversion function to be called for each type. "to_native" is Lang language-> C ++, from_native is the opposite. In addition, I think that there are some key strings that start with "@", but the strings that start with "@" are treated as regular expressions.

Edit generator.py

The following script is used to analyze the C ++ code. cocos2d-x/tools/bindings-generator/clang/cindex.py This is Clang's Python binding, which comes with the Clang source code.

Finally

I think that there are many places where the explanation is insufficient, but for the time being, this is the end. As for the correspondence of the cocos command, I am currently implementing it, so It may be written as (Part 3) when it is organized to some extent. I'm currently making a little bit of bindings for the language Squirrel.

Tomorrow's Advent Calendar is giginet. giginet will publish [Cocos2d-x book] on December 24th (http://giginet.hateblo.jp/entry/2014/11/26/185855). Congratulations! I have already booked on Amazon.

that's all. (December 4th was over while I was writing. I'm sorry ...)

Recommended Posts

[Cocos2d-x] How to make Script Binding (Part 2)
[Cocos2d-x] How to make Script Binding (Part 1)
[Cocos2d-x 3.0] How to automate Script Binding with binding-generator
How to make a shooting game with toio (Part 1)
How to make unit tests Part.2 Class design for tests
How to make a hacking lab-Kali Linux (2020.1) VirtualBox 64-bit Part 2-
How to make a Japanese-English translation
How to make a slack bot
How to make a crawler --Advanced
How to make a recursive function
How to make a deadman's switch
[Blender] How to make a Blender plugin
[Blender] How to make Blender scripts multilingual
How to make a crawler --Basic
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to make a unit test Part.1 Design pattern for introduction
How to make Word Cloud characters monochromatic
[Blender] How to set shape_key with script
How to make Selenium as light as possible
How to use cybozu.com developer network (Part 2)
[Python] How to make a class iterable
How to use Tweepy ~ Part 1 ~ [Getting Tweet]
[Ubuntu] How to execute a shell script
How to make multi-boot USB (Windows 10 compatible)
How to make a Backtrader custom indicator
How to make a Pelican site map
How to run a Maya Python script
How to make a dialogue system dedicated to beginners
How to make an embedded Linux device driver (11)
How to make WTForms TextArea correspond to file drop
How to make an embedded Linux device driver (8)
How to make Spigot plugin (for Java beginners)
How to make an embedded Linux device driver (1)
How to make multiple kernels selectable on Jupyter
How to make a dictionary with a hierarchical structure.
How to make an embedded Linux device driver (7)
How to make an embedded Linux device driver (2)
How to run some script regularly in Django
How to make scrapy JSON output into Japanese
How to make an embedded Linux device driver (3)
How to use Tweepy ~ Part 2 ~ [Follow, like, etc.]
How to measure execution time with Python Part 1
I read "How to make a hacking lab"
[Blender x Python] How to make an animation
How to make an embedded Linux device driver (6)
How to make Substance Painter Python plugin (Introduction)
[Blender x Python] How to make vertex animation
How to make an embedded Linux device driver (5)
How to make an embedded Linux device driver (10)
How to make Python faster for beginners [numpy]
How to make Python Interpreter changes in Pycharm
How to make Linux compatible with Japanese keyboard
How to make an embedded Linux device driver (9)
Inspired by "How to make pure functional JavaScript"
How to measure execution time with Python Part 2
How to make AWS rekognition recognize local image files
How to add help to HDA (with Python script bonus)
[Continued] Inspired by "How to make pure functional JavaScript"
Explain in detail how to make sounds with python
How to upload with Heroku, Flask, Python, Git (Part 3)