2014-12-02 12:00:16 -07:00
|
|
|
local _ENV = mkmodule('plugins.stockpiles')
|
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
local argparse = require('argparse')
|
2014-12-02 12:00:16 -07:00
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
local STOCKPILES_DIR = "dfhack-config/stockpiles";
|
|
|
|
local STOCKPILES_LIBRARY_DIR = "hack/data/stockpiles";
|
2014-12-02 12:00:16 -07:00
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
local function get_sp_name(name, num)
|
|
|
|
if #name > 0 then return name end
|
|
|
|
return ('Stockpile %d'):format(num)
|
2014-12-04 03:52:38 -07:00
|
|
|
end
|
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
local STATUS_FMT = '%6s %s'
|
|
|
|
local function print_status()
|
|
|
|
local sps = df.global.world.buildings.other.STOCKPILE
|
|
|
|
print(('Current stockpiles: %d'):format(#sps))
|
|
|
|
if #sps > 0 then
|
|
|
|
print()
|
|
|
|
print(STATUS_FMT:format('ID', 'Name'))
|
|
|
|
print(STATUS_FMT:format('------', '----------'))
|
2014-12-05 06:49:40 -07:00
|
|
|
end
|
2023-03-15 18:16:42 -06:00
|
|
|
for _,sp in ipairs(sps) do
|
|
|
|
print(STATUS_FMT:format(sp.id, get_sp_name(sp.name, sp.stockpile_number)))
|
2014-12-05 06:49:40 -07:00
|
|
|
end
|
2023-03-15 18:16:42 -06:00
|
|
|
end
|
2014-12-05 06:49:40 -07:00
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
local function list_dir(path, prefix, filters)
|
|
|
|
local paths = dfhack.filesystem.listdir_recursive(path, 0, false)
|
|
|
|
if not paths then
|
|
|
|
dfhack.printerr(('Cannot find stockpile settings directory: "%s"'):format(path))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local normalized_filters = {}
|
|
|
|
for _,filter in ipairs(filters or {}) do
|
|
|
|
table.insert(normalized_filters, filter:lower())
|
|
|
|
end
|
|
|
|
for _,v in ipairs(paths) do
|
|
|
|
local normalized_path = prefix .. v.path:lower()
|
|
|
|
if v.isdir or not normalized_path:endswith('.dfstock') then goto continue end
|
|
|
|
normalized_path = normalized_path:sub(1, -9)
|
|
|
|
if #normalized_filters > 0 then
|
|
|
|
local matched = false
|
|
|
|
for _,filter in ipairs(normalized_filters) do
|
|
|
|
if normalized_path:find(filter, 1, true) then
|
|
|
|
matched = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not matched then goto continue end
|
2014-12-05 06:49:40 -07:00
|
|
|
end
|
2023-03-15 18:16:42 -06:00
|
|
|
print(('%s%s'):format(prefix, v.path:sub(1, -9)))
|
|
|
|
::continue::
|
2014-12-05 06:49:40 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
local function list_settings_files(filters)
|
|
|
|
list_dir(STOCKPILES_DIR, '', filters)
|
|
|
|
list_dir(STOCKPILES_LIBRARY_DIR, 'library/', filters)
|
2014-12-05 06:49:40 -07:00
|
|
|
end
|
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
local function assert_safe_name(name)
|
|
|
|
if not name or #name == 0 then
|
|
|
|
qerror('name missing or empty')
|
2014-12-02 12:23:12 -07:00
|
|
|
end
|
2023-04-11 00:07:18 -06:00
|
|
|
if name:find('[^%w._]') then
|
2023-03-23 00:58:02 -06:00
|
|
|
qerror('name can only contain numbers, letters, periods, and underscores')
|
2014-12-02 12:00:16 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
local function get_sp_id(opts)
|
|
|
|
if opts.id then return opts.id end
|
|
|
|
local sp = dfhack.gui.getSelectedStockpile()
|
|
|
|
if sp then return sp.id end
|
|
|
|
return nil
|
2015-07-29 12:17:14 -06:00
|
|
|
end
|
|
|
|
|
2023-03-15 22:54:48 -06:00
|
|
|
local included_elements = {
|
|
|
|
containers=1,
|
|
|
|
general=2,
|
|
|
|
categories=4,
|
|
|
|
types=8,
|
|
|
|
}
|
|
|
|
|
2023-05-20 17:00:59 -06:00
|
|
|
function export_stockpile(name, opts)
|
2023-03-15 18:16:42 -06:00
|
|
|
assert_safe_name(name)
|
|
|
|
name = STOCKPILES_DIR .. '/' .. name
|
2023-03-15 22:54:48 -06:00
|
|
|
|
|
|
|
local includedElements = 0
|
|
|
|
for _,inc in ipairs(opts.includes) do
|
|
|
|
if included_elements[inc] then
|
|
|
|
includedElements = includedElements | included_elements[inc]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-20 01:32:19 -06:00
|
|
|
if includedElements == 0 then
|
|
|
|
for _,v in pairs(included_elements) do
|
|
|
|
includedElements = includedElements | v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-15 22:54:48 -06:00
|
|
|
stockpiles_export(name, get_sp_id(opts), includedElements)
|
2015-07-29 12:17:14 -06:00
|
|
|
end
|
|
|
|
|
2023-05-28 03:26:06 -06:00
|
|
|
local function normalize_name(name)
|
2023-03-15 18:16:42 -06:00
|
|
|
local is_library = false
|
|
|
|
if name:startswith('library/') then
|
|
|
|
name = name:sub(9)
|
|
|
|
is_library = true
|
2015-07-29 12:17:14 -06:00
|
|
|
end
|
2023-03-15 18:16:42 -06:00
|
|
|
assert_safe_name(name)
|
|
|
|
if not is_library and dfhack.filesystem.exists(STOCKPILES_DIR .. '/' .. name .. '.dfstock') then
|
2023-05-28 03:26:06 -06:00
|
|
|
return STOCKPILES_DIR .. '/' .. name
|
2023-03-15 18:16:42 -06:00
|
|
|
end
|
2023-05-28 03:26:06 -06:00
|
|
|
return STOCKPILES_LIBRARY_DIR .. '/' .. name
|
|
|
|
end
|
|
|
|
|
|
|
|
function import_stockpile(name, opts)
|
|
|
|
name = normalize_name(name)
|
|
|
|
stockpiles_import(name, get_sp_id(opts), opts.mode, table.concat(opts.filters or {}, ','))
|
|
|
|
end
|
|
|
|
|
|
|
|
function import_route(name, route_id, stop_id, mode, filters)
|
|
|
|
name = normalize_name(name)
|
|
|
|
stockpiles_route_import(name, route_id, stop_id, mode, table.concat(filters or {}, ','))
|
2023-03-15 22:54:48 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
local valid_includes = {general=true, categories=true, types=true}
|
|
|
|
|
|
|
|
local function parse_include(arg)
|
|
|
|
local includes = argparse.stringList(arg, 'include')
|
|
|
|
for _,v in ipairs(includes) do
|
|
|
|
if not valid_includes[v] then
|
|
|
|
qerror(('invalid included element: "%s"'):format(v))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return includes
|
|
|
|
end
|
|
|
|
|
|
|
|
local valid_modes = {set=true, enable=true, disable=true}
|
|
|
|
|
|
|
|
local function parse_mode(arg)
|
|
|
|
if not valid_modes[arg] then
|
|
|
|
qerror(('invalid mode: "%s"'):format(arg))
|
|
|
|
end
|
|
|
|
return arg
|
2015-07-29 12:17:14 -06:00
|
|
|
end
|
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
local function process_args(opts, args)
|
|
|
|
if args[1] == 'help' then
|
|
|
|
opts.help = true
|
2014-12-04 03:52:38 -07:00
|
|
|
return
|
|
|
|
end
|
2014-12-05 06:49:40 -07:00
|
|
|
|
2023-03-15 22:54:48 -06:00
|
|
|
opts.includes = {}
|
|
|
|
opts.mode = 'set'
|
2023-03-21 00:25:52 -06:00
|
|
|
opts.filters = {}
|
2023-03-15 22:54:48 -06:00
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
return argparse.processArgsGetopt(args, {
|
2023-03-20 01:32:19 -06:00
|
|
|
{'f', 'filter', hasArg=true,
|
2023-03-21 00:25:52 -06:00
|
|
|
handler=function(arg) opts.filters = argparse.stringList(arg) end},
|
2023-03-15 18:16:42 -06:00
|
|
|
{'h', 'help', handler=function() opts.help = true end},
|
2023-03-20 01:32:19 -06:00
|
|
|
{'i', 'include', hasArg=true,
|
2023-03-15 22:54:48 -06:00
|
|
|
handler=function(arg) opts.includes = parse_include(arg) end},
|
2023-03-20 01:32:19 -06:00
|
|
|
{'m', 'mode', hasArg=true,
|
2023-03-15 22:54:48 -06:00
|
|
|
handler=function(arg) opts.mode = parse_mode(arg) end},
|
2023-03-20 01:32:19 -06:00
|
|
|
{'s', 'stockpile', hasArg=true,
|
2023-03-15 22:54:48 -06:00
|
|
|
handler=function(arg) opts.id = argparse.nonnegativeInt(arg, 'stockpile') end},
|
2023-03-15 18:16:42 -06:00
|
|
|
})
|
2014-12-02 12:00:16 -07:00
|
|
|
end
|
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
function parse_commandline(args)
|
|
|
|
local opts = {}
|
|
|
|
local positionals = process_args(opts, args)
|
2014-12-04 03:52:38 -07:00
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
if opts.help or not positionals then
|
2014-12-02 12:00:16 -07:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
local command = table.remove(positionals, 1)
|
|
|
|
if not command or command == 'status' then
|
|
|
|
print_status()
|
|
|
|
elseif command == 'list' then
|
|
|
|
list_settings_files(positionals)
|
|
|
|
elseif command == 'export' then
|
|
|
|
export_stockpile(positionals[1], opts)
|
|
|
|
elseif command == 'import' then
|
|
|
|
import_stockpile(positionals[1], opts)
|
|
|
|
else
|
|
|
|
return false
|
2014-12-04 03:52:38 -07:00
|
|
|
end
|
2014-12-02 12:00:16 -07:00
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
return true
|
2014-12-02 12:23:12 -07:00
|
|
|
end
|
|
|
|
|
2014-12-02 12:00:16 -07:00
|
|
|
return _ENV
|