summaryrefslogtreecommitdiff
path: root/generate.lua (plain)
blob: fa11767e2a340d87fad6784aa3a758c11dcfc33a
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360

------------------------------------------------------------------------------
-- BIN2C HELPER
------------------------------------------------------------------------------

-- This can be used to create a C program that populates the
-- package.preload table, similarly to the bin2c utility.

function Bin2C(arg)
  local function dump_bin(str)
    io.write'{'
    for i = 1, #str do io.write(str:byte(i), ',') end
    io.write'0}'
  end
  local id = arg[1]:gsub('[^A-Za-z0-9_%.]', '_')
  local cid = id:gsub('%.', '_')
  local src = assert(io.read'*a')
  -- compiled-in settings
  local settings = ''
  for i = 2, #arg do
    local key = arg[i]:match('^[^=]*=')
    if key then
      settings = settings .. ', ' .. key:sub(1, -2):gsub('[^A-Za-z0-9_]', '_')
        .. ' = "' .. arg[i]:sub(#key+1):gsub('[^#-Z^-~]',
        function (c) return string.format('\\%03d', c:byte()) end).. '"'
    end
  end
  src = src:gsub('settings or { }', 'settings or {'..settings:sub(2)..' }')
  local output = string.dump(assert(loadstring(src, '='..id)))
  io.write'/* auto-generated by generate.lua'
  for i = 1, #arg do io.write(' "', arg[i]:gsub('%*/', '* /'), '"') end
  io.write(' */\nconst char _chunk_', cid, '[] = ')
  dump_bin(output)
  io.write(';\nCHUNK(', cid, ', "', id, '")\n')
end

------------------------------------------------------------------------------
-- PAK FILE GENERATOR
------------------------------------------------------------------------------

Pack = {}
Pack.__index = Pack

Pack.stub = [[
local SDL = require 'SDL'
local zlib = require 'zlib'
local rw, loader = ...
local base = rw:seek()
setmetatable(loader, FS.Loader)
function loader:_open(filename)
  filename = filename:gsub('^/', '')
  if not files[filename] then return nil, "no such file" end
  local fileRw = SDL.RWFromNewMem(files[filename].length)
  assert(rw:seek('set', base + files[filename].offset))
  if files[filename].compressed then
    zlib.uncompress(fileRw:baseptr(), files[filename].length,
      assert(rw:read(files[filename].compressed)), files[filename].compressed)
  else
    -- TODO avoid having to use a separate RW when loading images etc.
    -- TODO this probably puts the decompressed data into memory twice -
    -- as a Lua string returned by read() and as a RW (fileRw)
    assert(fileRw:write(assert(rw:read(files[filename].length))))
    assert(fileRw:seek('set'))
  end
  return fileRw
end
function loader:_list(dirname)
  dirname = dirname:gsub('^/', ''):gsub('/$', '')
  if indexes[dirname] then return indexes[dirname] end
  return nil, "no such directory"
end
loader:appendToActiveList()
]]

function Pack:new(o)
  o = o or {}
  o.indexes = { [''] = {} }
  o.files = {}
  return setmetatable(o or {}, Pack)
end

function Pack:addToIndex(filename, mode)
  if filename == '' then return end
  filename = filename:gsub('/*$', '')
  local parent = filename:gsub('/*[^/]*$', '')
  self:addToIndex(parent, 'directory')
  self.indexes[parent][filename:match('[^/]*$')] = mode
  if mode == 'directory' then self.indexes[filename] = {} end
end

function Pack:loadDirectory(dir, mountpoint)
  mountpoint = mountpoint:gsub('/*$', '/'):gsub('^/+', '')
  self:addToIndex(mountpoint, 'directory')
  for file in lfs.dir(dir) do if file ~= '.' and file ~= '..' then
    local mode = assert(lfs.attributes(dir..'/'..file, 'mode'))
    if mode == 'directory' then
      self:loadDirectory(dir..'/'..file, mountpoint..file)
    else
      self:addToIndex(mountpoint..file, mode)
      local content = assert(SDL.RWFromFile(dir..'/'..file, 'r'):read())
      local fileInfo = { length = #content, data = content }
      if not content:find('\0', 1, true) or
          file:match('%.ttf$') or file:match('%.otf') then
        local size = zlib.compressBound(#content)
        local buf = SDL.RWFromNewMem(size)
        size = zlib.compress(buf:baseptr(), size, content, #content)
        fileInfo.data = assert(buf:read(size))
        fileInfo.compressed = size
      end
      self.files[mountpoint..file] = fileInfo
    end
  end end
  return self   -- allow chaining calls
end

function Pack:write()
  local data = {}
  local position = 0
  for name,info in pairs(self.files) do
    info.offset = position
    position = position + (info.compressed or info.length)
    data[#data+1] = info.data; info.data = nil
  end
  local pp; function pp(o)
    if type(o) == 'string' then
      return ('%q'):format(o):gsub('\\\n', '\\n') end
    if type(o) ~= 'table' then return tostring(o) end
    local r = ''
    for k,v in pairs(o) do r = r .. '['..pp(k)..'] = '..pp(v)..', ' end
    return '{ '..r..'}'
  end
  local stub = 'local files = '..pp(self.files)..'\n'..
    'local indexes = '..pp(self.indexes)..'\n'..self.stub
  return FS.makeSFXStub(stub, true) .. table.concat(data)
end


------------------------------------------------------------------------------
-- C/LUA BINDING GENERATOR (GENGL)
------------------------------------------------------------------------------

check_funcs = {}

return_codes = {}

Arg = {}

function Arg.Counter(bound, narg)
  local atype = bound.info.argtypes[narg]
  bound.add.vars = atype .. ' c'..narg..' = ' ..
    (bound.info[narg].check_func or check_funcs[atype]):gsub('@n', narg)..';\n'
  return 'c'..narg
end

function Arg.Buffer(bound, narg)
  local atype = bound.info.argtypes[narg]
  return '('..atype..')check_'..(atype:match('^const') and 'const_' or '')..
    'ptr(L, '..narg..')'
end

local function arg_size(bound, narg, size, defaults)
  local atype = bound.info.argtypes[narg]
  local base = atype:gsub('const', ''):gsub('[* ]', '')
  
  if type(size) == 'string' then   -- dynamic size
    bound.add.vars = base..' *a'..narg..';\n'
    bound.add.argget = 'a'..narg..' = ('..base..' *)malloc('..
      size..' * sizeof('..base..'));\n'..
      'if(a'..narg..' == NULL) luaL_error(L, "out of memory");\n'..
      'memset(a'..narg..', 0, '..size..' * sizeof('..base..'));\n'
    bound.add.cleanup = 'free((void *)a'..narg..');\n'
  else   -- static size
    if defaults == true then   -- prepare defaults
      defaults = '{ '..string.rep('0, ', size):sub(1, -3)..' }' end
    bound.add.vars = base..' a'..narg..'['..size..']'..
      (defaults and ' = '..defaults or '')..';\n'
  end
end

function Arg.Returns(bound, narg)
  local size = bound.info[narg].Returns
  local base = bound.info.argtypes[narg]:gsub('const', ''):gsub('[* ]', '')
  local push = return_codes[base]
  arg_size(bound, narg, size, true)
  
  bound.ints.i = true
  bound.ints['r'..narg] = 'luaL_optinteger(L, '..narg..', '..size..')'
  bound.add.argput =
    'if(r'..narg..' == 1) {\n'..
    '  '..push:gsub('@b', 'a'..narg..'[0]')..'\n}\n'..
    'else {\n'..
    '  lua_createtable(L, r'..narg..', 0);\n'..
    '  for(i = 0; i < r'..narg..'; i++) {\n'..
    '    '..push:gsub('@b', 'a'..narg..'[i]')..'\n'..
    '    lua_rawseti(L, -2, i+1);\n'..
    '  }\n}\n'
  bound.num_ret = bound.num_ret + 1
  return 'a'..narg
end

function Arg.Array(bound, narg)
  local size = bound.info[narg].Array
  local defaults = bound.info[narg].defaults
  local base = bound.info.argtypes[narg]:gsub('const', ''):gsub('[* ]', '')
  arg_size(bound, narg, size, defaults)
  
  bound.ints.i = true
  
  if narg == #bound.info.argtypes then
    -- last array arg (can be used unpacked)
    bound.add.argget =
      'if(lua_istable(L, '..narg..') || lua_isnoneornil(L, '..narg..')) {\n'..
      '  luaL_checktype(L, '..narg..', LUA_TTABLE);\n'..
      '  for(i = 0; i < '..size..'; i++) {\n'..
      '    lua_rawgeti(L, '..narg..', i+1);\n'..
      '    '..(defaults and 'if(!lua_isnil(L, -1)) ' or '')..
          'a'..narg..'[i] = '..check_funcs[base]:gsub('@n', '-1')..';\n'..
      '    lua_pop(L, 1);\n  }\n'..
      '}\nelse {\n'..
      '  for(i = 0; i < '..size..(defaults and
          ' && !lua_isnoneornil(L, i+'..narg..')' or '')..'; i++)\n'..
      '    a'..narg..'[i] = '..check_funcs[base]:gsub('@n', 'i+'..narg)..';\n'..
      '}\n'
  else
    bound.add.argget =
      'luaL_checktype(L, '..narg..', LUA_TTABLE);\n'..
      'for(i = 0; i < '..size..'; i++) {\n'..
      '  lua_rawgeti(L, '..narg..', i+1);\n'..
      '  '..(defaults and 'if(!lua_isnil(L, -1)) ' or '')..
          'a'..narg..'[i] = '..check_funcs[base]:gsub('@n', '-1')..';\n'..
      '  lua_pop(L, 1);\n}\n'
  end
  return 'a'..narg
end


function Bind(fn, info)
  bindlist[#bindlist+1] = fn
  boundinfo[fn] = info
  
  setmetatable(info, { __index = assert(prototypes[fn] or info.prototype,
    fn.."(): unknown function prototype") })
  local bound = { add = {}, info = info, num_ret = 0, ints = {},
    vars = '', argget = '', argput = '', cleanup = '' }
  setmetatable(bound.add, { __newindex = function (t,k,v)
    bound[k] = bound[k] .. v end })
  
  -- parse arguments
  local args = {}
  for narg = 1, #info.argtypes do
    local atype = info.argtypes[narg]
    local argstr = nil
    bound.narg = narg
    bound.argerr = fn.."() arg "..narg..": "
    
    -- implicit argument info
    if info.argtypes[narg]:match('%*') and not info[narg] then
      info[narg] = info end
    if not info[narg] then info[narg] = {} end
    
    local check_fn = info[narg].check_func or check_funcs[atype]
    if check_fn then argstr = check_fn:gsub('@n', narg) end
    if check_fn and info[narg].opt then argstr =
      '(lua_isnoneornil(L, '..narg..') ? '..info[narg].opt..' : '..argstr..')' end
    
    -- execute Arg.* functions to handle the argument
    for k,v in pairs(info[narg]) do
      if Arg[k] then argstr = Arg[k](bound, narg) end
    end
    
    args[#args+1] = assert(argstr, bound.argerr.."no handler defined for "..
      tostring(info.argtypes[narg]))
  end
  
  -- create base call
  local base_call = (info.base_call or fn..'(@a)'):
    gsub('@a', table.concat(args, ', ')):gsub('@A(%d+)',
    function (m) return args[tonumber(m)] end)
  local return_code = assert(info.return_code or return_codes[info.return_type],
    fn.."(): undefined return_code - return type "..tostring(info.return_type))
  if not return_code:match('@v') then bound.num_ret = bound.num_ret+1 end
  base_call = return_code:gsub('@[vb]', base_call)
  bound.add.cleanup = 'return ' .. bound.num_ret .. ';\n'
  
  local ints = {}
  for k,_ in pairs(bound.ints) do ints[#ints+1] = k end
  table.sort(ints)
  for i,k in ipairs(ints) do if bound.ints[k] ~= true then
    ints[i] = k..' = '..bound.ints[k] end end
  ints = (#ints > 0 and 'int '..table.concat(ints, ', ')..';\n' or '')
  
  local result_str = bound.vars .. ints .. bound.argget ..
      base_call..'\n' .. bound.argput .. bound.cleanup
  return 'int b_'..fn..'(lua_State *L) {\n'..
    '  '..result_str:gsub('\n', '\n  '):gsub(' *$', '')..'}'
end

function parse_headers(content)
  bindlist = {}    -- an array of function names to bind.
  prototypes = {}  -- a table with prototype info about each function.
  boundinfo = {}   -- a table with binding info about bound functions.

  -- parse the headers to get function prototypes.
  Input = '\n' .. content:gsub('\\\n', ''):gsub('/%*.-%*/', '')
  for proto in Input:gsub('#.-\n', ''):gsub('%s+', ' '):gsub(' +%*', '*')
      :gsub('extern +"C" +(%b{})', function (m) return m:sub(2, -2) end)
      :gsub('(struct) *[\w_]* *%b{}', '%1 '):gsub('(enum) *[\w_]* *%b{}', '%1 ')
      :gsub('(union) *[\w_]* *%b{}', '%1 '):gsub('%b{}', ';'):gsub('^', '\n')
      :gsub('%s*;%s*', ';\n'):gmatch('\n[^(\n]+%b();') do
    local args = proto:match('%((.*)%);?$'):gsub('^ *void *$', '')
    local atypes = {}
    for arg in args:gmatch('[^,]+') do
      arg = arg:gsub('^ +', ''):gsub(' +$', '')
      local atype = arg:match('^ *(%S+)')
      if atype == 'const' then atype = arg:match('^ *(const %S+)') end
      if arg:match('%*') then atype = arg:gsub('%*.*', '*') end
      atypes[#atypes+1] = atype
    end
    local header = (' '..proto:match('^[^(]*'):gsub('%s+', '  ')..' ')
      :gsub(' extern ', ' '):gsub(' static ', ' '):gsub(' [A-Z_]+ ', ' ')
      :gsub(' _*inline_* ', ' '):gsub(' _*INLINE_* ', ' '):gsub(' +', ' ')
    if header:match('%S') then
      prototypes[header:match('(%S+) *$')] = { argtypes = atypes,
        return_type = header:gsub(' *(%S+) *$', ''):gsub('^%s+', '') } end
  end
end

function parse_include_path_args(arg)
  local result = {}
  for _,v in ipairs(arg) do if v:sub(1,2) == '-I' then
    result[#result+1] = v:sub(3)
  end end
  return result
end

function try_file_locations(filenames)
  local errors = {}
  for i,v in ipairs(filenames) do
    local r, f,err = nil, io.open(v)
    if f then r,err = f:read'*a'; f:close() end
    if r then return r .. '\n' end
    errors[#errors+1] = err
  end
  return nil, errors[1] and table.concat(errors, '\n') or "no filenames given"
end

------------------------------------------------------------------------------
-- DISPATCHER
------------------------------------------------------------------------------

-- when ran from standard Lua CLI and not from require()
if ... ~= 'generate' and not FS then
  if arg[1] and arg[1]:sub(1, 2) == '--' then
    print'usage 1: lua generate.lua --pak output.pak sourcedir/'
    print'usage 2: lua generate.lua module.name setting=value... <source.lua >>chunks.h'
  else
    Bin2C(arg)
  end
end