有时候需要在Lua脚本中嵌入二进制文件,在需要的时候把文件释放出来进行加载
先写个python脚本将二进制文件转换为数组
import struct
f = open('function.so', 'rb')
fhex = open('hex.txt', 'w')
data = f.read()
for c in data:
fhex.write(str(struct.unpack('B', c)[0]) + ',')
Lua中的实现如下
fileData={4,0,0,0,0,0,0,0,195,......0,0,0,0,0,0,133,128,1,0,211,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,}
function save_so()
local file = io.open("function.so", "wb")
if file then
for i in pairs(fileData) do
file:write(string.char(fileData[i]))
end
file:close()
end
end