2014-12-02 12:00:16 -07:00
|
|
|
local _ENV = mkmodule('plugins.stockpiles')
|
|
|
|
|
|
|
|
--[[
|
|
|
|
|
|
|
|
Native functions:
|
|
|
|
|
|
|
|
* stockpiles_list_settings(dir_path), list files in directory
|
|
|
|
* stockpiles_load(file), with full path
|
|
|
|
* stockpiles_save(file), with full path
|
2014-12-05 05:27:58 -07:00
|
|
|
* isEnabled()
|
2014-12-02 12:00:16 -07:00
|
|
|
|
|
|
|
--]]
|
2014-12-04 03:52:38 -07:00
|
|
|
--
|
|
|
|
function safe_require(module)
|
|
|
|
local status, module = pcall(require, module)
|
|
|
|
return status and module or nil
|
|
|
|
end
|
|
|
|
|
2014-12-02 12:00:16 -07:00
|
|
|
|
|
|
|
local gui = require 'gui'
|
2014-12-05 06:49:40 -07:00
|
|
|
local widgets = require('gui.widgets')
|
|
|
|
local dlg = require('gui.dialogs')
|
2014-12-02 12:00:16 -07:00
|
|
|
local script = require 'gui.script'
|
2014-12-04 03:52:38 -07:00
|
|
|
local persist = safe_require('persist-table')
|
2014-12-02 12:00:16 -07:00
|
|
|
|
2014-12-05 06:49:40 -07:00
|
|
|
|
|
|
|
function ListFilterDialog(args)
|
|
|
|
args.text = args.prompt or 'Type or select an option'
|
|
|
|
args.text_pen = COLOR_WHITE
|
|
|
|
args.with_filter = true
|
|
|
|
args.icon_width = 2
|
|
|
|
|
|
|
|
local choices = {}
|
|
|
|
|
|
|
|
if not args.hide_none then
|
|
|
|
table.insert(choices, {
|
|
|
|
icon = '?', text = args.none_caption or 'none',
|
|
|
|
index = -1, name = -1
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
local filter = args.item_filter
|
|
|
|
|
|
|
|
for i,v in ipairs(args.items) do
|
|
|
|
if not filter or filter(v,-1) then
|
|
|
|
local name = v
|
|
|
|
local icon
|
|
|
|
table.insert(choices, {
|
2014-12-05 08:52:57 -07:00
|
|
|
icon = icon, search_key = string.lower(name), text = name, index = i
|
2014-12-05 06:49:40 -07:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
args.choices = choices
|
|
|
|
|
|
|
|
if args.on_select then
|
|
|
|
local cb = args.on_select
|
|
|
|
args.on_select = function(idx, obj)
|
|
|
|
return cb(obj.index, args.items[obj.index])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return dlg.ListBox(args)
|
|
|
|
end
|
|
|
|
|
|
|
|
function showFilterPrompt(title, list, text,item_filter,hide_none)
|
|
|
|
ListFilterDialog{
|
|
|
|
frame_title=title,
|
|
|
|
items=list,
|
|
|
|
prompt=text,
|
|
|
|
item_filter=item_filter,
|
|
|
|
hide_none=hide_none,
|
|
|
|
on_select=script.mkresume(true),
|
|
|
|
on_cancel=script.mkresume(false),
|
|
|
|
on_close=script.qresume(nil)
|
|
|
|
}:show()
|
|
|
|
|
|
|
|
return script.wait()
|
|
|
|
end
|
|
|
|
|
2014-12-02 12:23:12 -07:00
|
|
|
function init()
|
2014-12-04 03:52:38 -07:00
|
|
|
if persist == nil then return end
|
2014-12-02 12:23:12 -07:00
|
|
|
if dfhack.isMapLoaded() then
|
|
|
|
if persist.GlobalTable.stockpiles == nil then
|
|
|
|
persist.GlobalTable.stockpiles = {}
|
|
|
|
persist.GlobalTable.stockpiles['settings_path'] = './stocksettings'
|
|
|
|
end
|
|
|
|
end
|
2014-12-02 12:00:16 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function tablify(iterableObject)
|
|
|
|
t={}
|
|
|
|
for k,v in ipairs(iterableObject) do
|
|
|
|
t[k] = v~=nil and v or 'nil'
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
function load_settings()
|
2014-12-02 12:23:12 -07:00
|
|
|
init()
|
2014-12-04 03:52:38 -07:00
|
|
|
local path = get_path()
|
|
|
|
local ok, list = pcall(stockpiles_list_settings, path)
|
|
|
|
if not ok then
|
|
|
|
show_message_box("Stockpile Settings", "The stockpile settings folder doesn't exist.", true)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if #list == 0 then
|
|
|
|
show_message_box("Stockpile Settings", "There are no saved stockpile settings.", true)
|
|
|
|
return
|
|
|
|
end
|
2014-12-05 06:49:40 -07:00
|
|
|
|
|
|
|
local choice_list = {}
|
|
|
|
for i,v in ipairs(list) do
|
|
|
|
choice_list[i] = string.gsub(v, "/", "/ ")
|
|
|
|
choice_list[i] = string.gsub(choice_list[i], "-", " - ")
|
|
|
|
choice_list[i] = string.gsub(choice_list[i], "_", " ")
|
|
|
|
end
|
|
|
|
|
2014-12-02 12:00:16 -07:00
|
|
|
script.start(function()
|
2014-12-05 06:49:40 -07:00
|
|
|
local ok2,index,name=showFilterPrompt('Stockpile Settings', choice_list, 'Choose a stockpile', function(item) return true end, true)
|
|
|
|
if ok2 then
|
|
|
|
local filename = list[index];
|
2014-12-04 03:52:38 -07:00
|
|
|
stockpiles_load(path..'/'..filename)
|
2014-12-02 12:00:16 -07:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function save_settings(stockpile)
|
2014-12-02 12:23:12 -07:00
|
|
|
init()
|
2014-12-02 12:00:16 -07:00
|
|
|
script.start(function()
|
|
|
|
local suggested = stockpile.name
|
|
|
|
if #suggested == 0 then
|
|
|
|
suggested = 'Stock1'
|
|
|
|
end
|
2014-12-04 03:52:38 -07:00
|
|
|
local path = get_path()
|
2014-12-02 12:00:16 -07:00
|
|
|
local sok,filename = script.showInputPrompt('Stockpile Settings', 'Enter stockpile name', COLOR_WHITE, suggested)
|
|
|
|
if sok then
|
2014-12-04 03:52:38 -07:00
|
|
|
if filename == nil or filename == '' then
|
|
|
|
script.showMessage('Stockpile Settings', 'Invalid File Name', COLOR_RED)
|
|
|
|
else
|
2015-03-24 16:46:49 -06:00
|
|
|
if not dfhack.filesystem.exists(path) then
|
|
|
|
dfhack.filesystem.mkdir(path)
|
|
|
|
end
|
2014-12-04 03:52:38 -07:00
|
|
|
print("saving...", path..'/'..filename)
|
|
|
|
stockpiles_save(path..'/'..filename)
|
|
|
|
end
|
2014-12-02 12:00:16 -07:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function manage_settings(sp)
|
2014-12-02 12:23:12 -07:00
|
|
|
init()
|
2014-12-02 12:00:16 -07:00
|
|
|
if not guard() then return false end
|
|
|
|
script.start(function()
|
|
|
|
local list = {'Load', 'Save'}
|
|
|
|
local tok,i = script.showListPrompt('Stockpile Settings','Load or Save Settings?',COLOR_WHITE,tablify(list))
|
|
|
|
if tok then
|
|
|
|
if i == 1 then
|
|
|
|
load_settings()
|
|
|
|
else
|
|
|
|
save_settings(sp)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2014-12-04 03:52:38 -07:00
|
|
|
function show_message_box(title, msg, iserror)
|
|
|
|
local color = COLOR_WHITE
|
|
|
|
if iserror then
|
|
|
|
color = COLOR_RED
|
|
|
|
end
|
|
|
|
script.start(function()
|
|
|
|
script.showMessage(title, msg, color)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2014-12-02 12:00:16 -07:00
|
|
|
function guard()
|
|
|
|
if not string.match(dfhack.gui.getCurFocus(), '^dwarfmode/QueryBuilding/Some/Stockpile') then
|
|
|
|
qerror("This script requires a stockpile selected in the 'q' mode")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function set_path(path)
|
2014-12-02 12:23:12 -07:00
|
|
|
init()
|
2014-12-04 03:52:38 -07:00
|
|
|
if persist == nil then
|
|
|
|
qerror("This version of DFHack doesn't support setting the stockpile settings path. Sorry.")
|
|
|
|
return
|
|
|
|
end
|
2014-12-02 12:00:16 -07:00
|
|
|
persist.GlobalTable.stockpiles['settings_path'] = path
|
|
|
|
end
|
|
|
|
|
2014-12-02 12:23:12 -07:00
|
|
|
function get_path()
|
|
|
|
init()
|
2014-12-04 03:52:38 -07:00
|
|
|
if persist == nil then
|
|
|
|
return "stocksettings"
|
|
|
|
end
|
2014-12-02 12:23:12 -07:00
|
|
|
return persist.GlobalTable.stockpiles['settings_path']
|
|
|
|
end
|
|
|
|
|
2014-12-02 12:00:16 -07:00
|
|
|
return _ENV
|