Developers Manual > Cocos2d-x > Cocos2d-console > Cocos luacompile

How to use cocos luacompile

Overview

Compile the .lua files to .luac to achieve the encryption function.

Set Up Environment

For cocos2d-x V3.3 or higher version, you need to set up environment about cocos console(Please refer to https://github.com/cocos2d/cocos2d-x/blob/v3/README.md)

For cocos2d-x V2.2.6, you should install the Python, then set up environment variables about Python.

Usage

For cocos2d-x V3.3 or higher version, use cocos luacompile [arguments] in the terminal.

For cocos2d-x V2.2.6, First cd director to thetools/cocos2d-console/console, and then use ./cocos2d.py luacompile [arguments] in the terminal.

Available Arguments

arg available value sample description necessary
-h, --help - - Show the help message and exit no
-v, --verbose - - verbose output no
-s, --src source directory ./projects/MyLuaGame/src Specify source directory of lua files needed to be compiled,support mutiple source directory yes
-d, --dst destination directory ./projects/MyLuaGame/dst Specify destination directory which bytecode files to be stored. yes
-e, --encrypt bool True Whether or not to encrypt lua files. no
-k, --encryptkey any string MyLuaKey Specify the encrypt key for encrypting lua scripts. It's only take effect when -e, --encrypt is enabled. Default value is 2dxLua. no
-b, --encryptsign any string MyLuaSign Specify the encrypt sign for encrypting lua scripts. It's only take effect when --encrypt is enabled. Default value is XXTEA. no
--disable-compile bool True Whether or not to compile lua scripts no

Samples

The ways to use luacompile command have the following several forms.

form cocos2d-x version description
cocos luacompile -h cocos2d-x V3.3 or higher version Show the help message
cocos luacompile -s src_dir -d dst_dir cocos2d-x V3.3 or higher version Precompile the Lua files in the src_dir directory to bytecode files which luajit supports, then store them in the dst_dir directory by the same directory structure.
cocos luacompile -s src_dir -d dst_dir -e True cocos2d-x V3.3 or higher version Precompile the Lua files in the src_dir directory to bytecode files which luajit supports, then encrypt this bytecoed files by xxtea.The key and sign for encrypting are 2dxLua and XXTEA by default. By adding -k xxx -b xxx' to the command, developers can change thekeyandsign,-k xxxset the key value and-b xxx` set the sign value. The encrypted files are stored in the dst_dir directory by the same directory structure.
cocos luacompile -s src_dir -d dst_dir -e True --disable-compile True cocos2d-x V3.3 or higher version Encrypt the Lua files in the src_dir by xxtea, then store them in the dst_dir directory by the same directory structure. The key and sign for encrypting are 2dxLua and XXTEA by default. By adding -k xxx -b xxx' to the command, developers can change thekeyandsign,-k xxxset the key value and-b xxx` set the sign value. This command is mainly used to make a set of encrypted files run both 32bit and 64bit iOS devices at the same time. Because Apple demand new app must support 64bit,but the stable luajit doesn't support the arm64bit, so providing this command to encrypt the lua files directly.
./cocos2d.py luacompile -h cocos2d-x V2.2.6 Show the help message
./cocos2d.py luacompile -s src_dir -d dst_dir cocos2d-x V2.2.6 Precompile the Lua files in the src_dir directory to bytecode files which luajit supports, then store them in the dst_dir directory by the same directory structure.
./cocos2d.py luacompile -s src_dir -d dst_dir -e True cocos2d-x V2.2.6 Precompile the Lua files in the src_dir directory to bytecode files which luajit supports, then encrypt this bytecoed files by xxtea.TThe key and sign for encrypting are 2dxLua and XXTEA by default. By adding -k xxx -b xxx' to the command, developers can change thekeyandsign,-k xxxset the key value and-b xxx` set the sign value. The encrypted files are stored in the dst_dir directory by the same directory structure.
./cocos2d.py luacompile -s src_dir -d dst_dir -e True --disable-compile True cocos2d-x V2.2.6 Encrypt the Lua files in the src_dir by xxtea, then store them in the dst_dir directory by the same directory structure. The key and sign for encrypting are 2dxLua and XXTEA by default, by adding -k xxx -b xxx' to the command, developers can change thekeyandsign,-k xxxset the key value and-b xxx` set the sign value. This command is mainly used to make a set of encrypted files run both 32bit and 64bit iOS devices at the same time.

Note: If you want to use the files encrypted by xxtea in the game, you should call the setXXTEAKeyAndSign of LuaStack in the native cpp code to set the key and sign to parse the encrypted files.

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.