2012-08-02 10:52:32 +00:00
|
|
|
#!/usr/bin/env luajit
|
2012-07-08 21:47:06 +00:00
|
|
|
|
|
|
|
-- Generic map iterator.
|
|
|
|
|
|
|
|
-- The first cmdline arg is a name of a lua file (may be sans .lua) which must
|
|
|
|
-- be a module and is `require'd into this script, e.g. "stats" or "stats.lua".
|
2012-07-08 21:47:11 +00:00
|
|
|
-- First, a key named .init is looked up in the loaded module and if it exists,
|
|
|
|
-- it is run like .init(arg) (thus allowing it to parse the command-line
|
|
|
|
-- arguments and then potentially remove the ones it used).
|
|
|
|
-- If .init returns non-nil, this script aborts.
|
|
|
|
-- Otherwise, for each 2nd and following argument, if map loading succeeds,
|
|
|
|
-- .success(map, filename) is run, otherwise
|
|
|
|
-- .failure(filename, errmsg) is run if that key exists, or a standard error
|
|
|
|
-- message is printed to stderr.
|
|
|
|
-- Finally, if there is a .finish field in the module, it is run with no args.
|
2012-07-08 21:47:06 +00:00
|
|
|
|
|
|
|
|
2012-07-08 21:47:11 +00:00
|
|
|
local B = require "build"
|
|
|
|
local string = require "string"
|
|
|
|
local io = require "io"
|
|
|
|
local os = require "os"
|
2012-07-08 21:47:06 +00:00
|
|
|
|
|
|
|
if (#arg < 1) then
|
2012-07-08 21:47:11 +00:00
|
|
|
io.stdout:write("Usage: luajit ./foreachmap <module[.lua]> [init args...] <filename.map> ...\n\n")
|
|
|
|
return
|
2012-07-08 21:47:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local modname = string.gsub(arg[1], "%.lua$", "")
|
|
|
|
local mod = require(modname)
|
|
|
|
|
2012-07-08 21:47:11 +00:00
|
|
|
if (mod.init) then
|
|
|
|
if (mod.init(arg) ~= nil) then
|
|
|
|
os.exit(1)
|
|
|
|
end
|
|
|
|
end
|
2012-07-08 21:47:06 +00:00
|
|
|
|
|
|
|
for i=2,#arg do
|
|
|
|
local fn = arg[i]
|
|
|
|
local map, errmsg = B.loadboard(fn)
|
|
|
|
|
|
|
|
if (map ~= nil) then
|
|
|
|
mod.success(map, fn)
|
|
|
|
else
|
2012-07-08 21:47:11 +00:00
|
|
|
if (mod.failure) then
|
|
|
|
mod.failure(fn, errmsg)
|
|
|
|
else
|
|
|
|
io.stderr:write(string.format("--- %s: %s\n", fn, errmsg))
|
|
|
|
end
|
2012-07-08 21:47:06 +00:00
|
|
|
end
|
|
|
|
end
|
2012-07-08 21:47:11 +00:00
|
|
|
|
|
|
|
if (mod.finish) then
|
|
|
|
mod.finish()
|
|
|
|
end
|