The Cocos2d-x uses Luajit to compile the Lua script into bytecode for encryption update.
The project requires that the lua script be encrypted. After checking the relevant information, we know that lua can use luac to compile the script into a bytecode to implement encryption. I tried it, it is feasible.
The following describes how to use the native lua interpreter to compile bytecode:
1. Create a new file named 1. lua with only one print ("Hello Lua") and create an empty out. lua script file.
2. Start -- run -- cmd
3. luac-o out. lua 1.lua
Note: luac-o [Script Name After compilation] [script name], with the script path when necessary
Press enter and open out. lua to see the compiled bytecode.
Run the bytecode script. You can see that lua's native interpreter can directly parse the bytecode script compiled by luac, which is very convenient!
Important:
After completing the above series, I used this method to compile the scripts in the project and use them in the cocos2dx environment! So I checked the information and found that 2dx uses luajit. the bytecode compiled by lua is incompatible with luajit, therefore, the bytecode script compiled according to the above method cannot be used in 2dx.
To solve this problem, it is actually very easy to compile the lua script with the luajit of 2dx. The following describes how to compile bytecode with luajit:
1. There is a msvcbuild.batbatch file under the cocos2d-x-2.2.3 \ scripting \ lua \ luajit \ LuaJIT-2.0.1 \ srcdirectory, first compile the luajit.exe.
2. Open the command line tool of visual studio, which will be available if vs is installed. It can be found in the installation directory.
3. Use the vs command line tool cd to the src directory of luajit
4. Execute msvcbuild.batbatch file to compile luajit.exe
After compilation, a series of files will be generated in the srcdirectory, with a luajit.exe
To the Directory: lib \ cocos2d-x \ scripting \ lua \ luajit \ LuaJIT-2.0.2 \ src
Run the following command: luajit.exe-B test. lua test. lua
You can use luajit.exe to compile the bytecode Of The lua Script: luajit-B [Script Name] [compiled Script Name]. After execution, a jit compiled into bytecode will be generated in the src directory. lua File
The compiled jit. put lua in the 2dx pilot test, based on the HelloLua project, put jit. put lua under \ samples \ Lua \ HelloLua \ Resources and modify AppDelegate. the lua call in cpp is std: string path = CCFileUtils: sharedFileUtils ()-> fullPathForFilename ("jit. lua ");
The running result is:
So far, luajit compilation bytecode encryption has been completed!
Note: In this example, the names of the scripts before and after compilation are different to show the differences, it is recommended that the script name be consistent before and after compilation. Otherwise, problems may occur when the script require each other! The bytecode file is in the format of lua.