兩種方式:一種用luac,一種用luajit
luac加密:
1、lua本身可以使用luac將腳本編譯為字節碼(bytecode)從而實現加密,去官網下載Lua源代碼包(http://www.lua.org/ftp/),下好解壓,目錄如下
2、啟動一個Visual Studio 命令行工具,用32位命令行會生成32位版本,用64位則生成64位版本
3、在命令行中使用cd 進入lua目錄(剛才解壓后的目錄),然后輸入 "etc\luavs.bat" 回車
4、如果沒有問題的話,會顯示生成過程,(64位下還會有幾個warning C4334)、完成后會在src目錄下找到生成結果,4個主要文件一個.exp的中間文件,luac.exe生成完畢,
然后把luac.exe所在存在的目錄設為環境變量,如果不設置的話,那么執行生成字節碼的dos命令的時候就要轉到luac.exe所在的目錄才能執行此dos命令了,都懂得,不多說
5、(1)、新建一個名為1.lua的文件,里面只有一句話print("Hello Lua"),新建一個空的out.lua腳本文件
(2)、開始--運行--cmd
(3)、luac -o out.lua 1.lua
注: luac -o [編譯后腳本名] [腳本名],必要時帶上腳本路徑,如:
然后實驗一下,執行這個字節碼腳本,可以看到lua原生的解釋器可以直接解析luac編譯出來的bytecode腳本,很方便!
luajit加密:
1、官網下載luajit(http://luajit.org/luajit.html),解壓,目錄如下
2、打開命令行
3、在命令行中使用cd 進入luajit下src的目錄,然后執行輸入msvcbuild.bat,執行此批處理文件,編譯出luajit.exe
4、接下來就可以使用luajit.exe編譯lua腳本的bytecode了:luajit -b [腳本名] [編譯后的腳本名],執行完后會在src目錄下生成一個已經編譯成bytecode的lua文件
此時即使把此路徑加入環境變量是沒用的,進行luajit加密命令的時候必須使用cd進入此目錄(C:\Users\hengjingjing\Desktop\LuaJIT-2.0.4\src),因為luajit加密,必須是luajit.exe和src下的jit文件夾配合使用才管用!
如果unity項目用的是基於ulua擴展的其他lua版本(cstolua、slua),或者本身用的就是ulua,那么要用luajit加密方式,因為ulua底層就是用的luajit
兩者比較:
http://lua-users.org/lists/lua-l/2011-06/msg01052.html
LuaJIT is open source software, released under the MIT/X license. LuaJIT builds out-of-the-box on most x86 or x64 operating systems (Linux, Windows, OSX etc.) and can be cross-compiled for embedded systems based on ARM or PPC/e500 CPUs.
The LuaJIT bytecode format is portable and ~40% smaller than Lua bytecode. The LuaJIT bytecode loader is 10x faster than the Lua bytecode loader. And LuaJIT bytecode loads 30x-40x faster than source code. Note that this is unrelated to the _execution_ speed of the code itself -- it doesn't matter how the code was loaded.
可以看出:Luajit跨平台,滿足於32位和64位,並且加密后的bytecode比luac生成的bytecode至少小40%,速度也是它的10倍多,總之luajit就是比lua原生的好