-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_lua_module.lua
More file actions
80 lines (72 loc) · 2.6 KB
/
binary_lua_module.lua
File metadata and controls
80 lines (72 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
-- Dumps a provided lua chunk/function to a string and then writes that string in a #define
local args = ...
-- args.fn -- function to compile
-- args.strip -- if truthy, strip debug info when compiling
-- args.symbol -- #define symbol.
-- args.line_length -- max chars that will be output on a single line (not including quotes, indents, etc). Default 72.
-- args.deflate -- if truthy, deflate the compiled data using zlib
local table = table
local string = string
local safe_chars = {
[' ']=' ', ['!']='!', ['$']='$', ['&']='&',
["'"]="'", ['(']='(', [')']=')', ['*']='*',
[',']=',', ['-']='-', ['.']='.', ['/']='/',
[';']=';', ['<']='<', ['=']='=', ['>']='>',
['@']='@', ['[']='[', [']']=']', ['^']='^',
['_']='_', ['`']='`', ['{']='{', ['|']='|',
['}']='}', ['~']='~', ['?']='\\?', ['\\']='\\\\',
['\a']='\\a', ['\b']='\\b', ['\f']='\\f', ['\n']='\\n',
['\r']='\\r', ['\t']='\\t', ['\v']='\\v'
}
local compiled = string.dump(args.fn, args.strip)
local length = #compiled
local uncompressed_length
if args.deflate then
uncompressed_length = length
compiled = be.util.deflate(compiled, 9)
length = #compiled
end
local line_length = args.line_length or 72
local line = { { } }
local n = 1
local c = 1
local line_available = line_length
local last_was_escape = false
for i = 1, length do
local b = string.byte(compiled, i)
if b >= 65 and b <= 90 or b >= 97 and b <= 122 then -- [A-Za-z]
b = string.char(b)
last_was_escape = false
elseif b >= 48 and b <= 57 then -- [0-9]
if not last_was_escape then
b = string.char(b)
else
local nextb = string.byte(compiled, i + 1)
if nextb ~= nil and nextb >= 48 and nextb <= 57 then -- if the next char is also a digit, don't encode this one as an octal escape.
b = '""' .. string.char(b)
last_was_escape = false
else
b = string.format('\\%o', b)
last_was_escape = true
end
end
elseif safe_chars[string.char(b)] then
b = safe_chars[string.char(b)]
last_was_escape = false
else
b = string.format('\\%o', b)
last_was_escape = true
end
if #b > line_available then
line_available = line_length
line[n] = table.concat(line[n])
n = n + 1
line[n] = { }
c = 1
end
line[n][c] = b
c = c + 1
line_available = line_available - #b
end
line[n] = table.concat(line[n])
write_template('common/templates/string_resource', { symbol = args.symbol, length = length, uncompressed_length = uncompressed_length, line = line })