Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cocos2d-x uses Luajit for encryption


May 12, 2021 Lua



The project requires that the lua script be encrypted, check the relevant information, learned that lua itself can use luac to compile the script into bytecode (bytecode) to achieve encryption, try, it is feasible. Here's how to compile the bytecode using the native lua interpreter:

1, create a new file .lua 1,000, with only one word print ("Hello Lua"), and create an empty .lua script file

2, start -- run -- cmd3, luac -o out.lua 1 .lua

Note: luac -o (compiled script name) (script name), if necessary, bring a script path, such as:

Cocos2d-x uses Luajit for encryption

(Compiled script name) ( script name) and, if necessary, bring the script path

Once you've returned, you'll .lua the outcode and you'll see compiled bytecodes, such as:

Cocos2d-x uses Luajit for encryption

Then experiment, execute this bytecode script, you can see that lua native interpreter can directly parse the luac compiled bytecode script, very convenient!

Cocos2d-x uses Luajit for encryption

Key Point: After doing the above series, I followed this method to compile the script in the project, and then used in the cocos2dx environment, found that no! So I looked up the data and found that 2dx was using luajit, and the bytecode and luajit compiled natively were incompatible, so the bytecode script compiled according to the above method could not be used in 2dx.

The solution to this problem is actually very simple, is to compile the lua script with 2dx's own luajit, attached to the method of luajit to compile bytecode:

1, in the cocos2d-x-2.2.3, scripting, lua, luajit, LuaJIT-2.0.1, src directory has a msvcbuild.bat batch file, need to first .exe luajit.exe this thing compiled.

2, open the visual studio command line tool, this as long as installed vs will have, in the installation directory can be found.

3, with vs command line tool cd to luajit's src directory

4, execute msvcbuild.bat batch files, compile luajit .exe

5, will be generated luajit .exe, lua51.dll, jit copied to the packaging tool's relative directory, so that in the tool can be called directly luajit -b source_file out_file (generally lua suffix, code does not have to change)

The next step is to compile the bytecode of the lua script using luajit.exe : luajit-b ( script name ) , and after execution , a jit.lua file that has been compiled into bytecode will be generated under the src directory. / ppimg src=" attachments="" image="" cimg="" 2016-02-22_56cb2ca40043a.jpg"="" alt="" p="" pimg="" jit.lua"); The result of the operation is: "" slt;"

Cocos2d-x uses Luajit for encryption

At this point, luajit compiled bytecode encryption is complete!

Serious note: In the example, I took the name of the script before and after compilation is different, in order to let everyone see the difference, in fact, when used in the project, the name of the script is best compiled before and after the same, otherwise in the script when require each other may have problems! O ne by one conversion feet is too much trouble to share a bat batch that can convert all lua files in a folder in bulk.

The code is as follows:

@echo off
if exist out rd /s /q out
mkdir out
:input
cls
set input=:
set /p input= 拖入要编译的lua文件夹:
set "input=%input:"=%"
if "%input%"==":" goto input
if not exist "%input%" goto input
for %%i in ("%input%") do if /i "%%~di"==%%i goto input
pushd %cd%
cd /d "%input%">nul 2>nul || exit
set cur_dir=%cd%
popd
set /a num = 0
for /f "delims=" %%i in ('dir /b /a-d /s "%input%"') do (set /a num += 1 & luajit -b %%~fsi out/%%~nxi & echo %%~nxi)
echo 编译脚本数量:%num%
ATTRIB out/*.* +R
pause

After compilation, all lua scripts in the folder will be bulk compiled into bytecodes and saved in the xxx?out directory, such as:

Cocos2d-x uses Luajit for encryption

Note: XXX is the path to package encrypted files

There are also tips: ios64 currently only supports lua, does not support the use of luajit to generate binary .lua.

Reference blog: http://jingyan.baidu.com/article/0a52e3f4179713bf62ed72f1.html