|  |  | @ -9,12 +9,19 @@ local _ENV = mkmodule('plugins.stockpiles') | 
			
		
	
		
		
			
				
					
					|  |  |  |  * stockpiles_save(file), with full path |  |  |  |  * stockpiles_save(file), with full path | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | --]] |  |  |  | --]] | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | -- | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | function safe_require(module) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     local status, module = pcall(require, module) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     return status and module or nil | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | end | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | local gui = require 'gui' |  |  |  | local gui = require 'gui' | 
			
		
	
		
		
			
				
					
					|  |  |  | local script = require 'gui.script' |  |  |  | local script = require 'gui.script' | 
			
		
	
		
		
			
				
					
					|  |  |  | local persist = require 'persist-table' |  |  |  | local persist = safe_require('persist-table') | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | function init() |  |  |  | function init() | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     if persist == nil then return end | 
			
		
	
		
		
			
				
					
					|  |  |  |     if dfhack.isMapLoaded() then |  |  |  |     if dfhack.isMapLoaded() then | 
			
		
	
		
		
			
				
					
					|  |  |  |         if persist.GlobalTable.stockpiles == nil then |  |  |  |         if persist.GlobalTable.stockpiles == nil then | 
			
		
	
		
		
			
				
					
					|  |  |  |             persist.GlobalTable.stockpiles = {} |  |  |  |             persist.GlobalTable.stockpiles = {} | 
			
		
	
	
		
		
			
				
					|  |  | @ -33,13 +40,21 @@ end | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | function load_settings() |  |  |  | function load_settings() | 
			
		
	
		
		
			
				
					
					|  |  |  |     init() |  |  |  |     init() | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     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 | 
			
		
	
		
		
			
				
					
					|  |  |  |     script.start(function() |  |  |  |     script.start(function() | 
			
		
	
		
		
			
				
					
					|  |  |  |         local path = persist.GlobalTable.stockpiles['settings_path'] |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         local list = stockpiles_list_settings(path) |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         local tok,i = script.showListPrompt('Stockpile Settings','Load which stockpile?',COLOR_WHITE,tablify(list)) |  |  |  |         local tok,i = script.showListPrompt('Stockpile Settings','Load which stockpile?',COLOR_WHITE,tablify(list)) | 
			
		
	
		
		
			
				
					
					|  |  |  |         if tok then |  |  |  |         if tok then | 
			
		
	
		
		
			
				
					
					|  |  |  |             local filename = list[i]; |  |  |  |             local filename = list[i]; | 
			
		
	
		
		
			
				
					
					|  |  |  |             stockpiles_load(path..'/'..filename); |  |  |  |             stockpiles_load(path..'/'..filename) | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |         end |  |  |  |         end | 
			
		
	
		
		
			
				
					
					|  |  |  |     end) |  |  |  |     end) | 
			
		
	
		
		
			
				
					
					|  |  |  | end |  |  |  | end | 
			
		
	
	
		
		
			
				
					|  |  | @ -47,15 +62,19 @@ end | 
			
		
	
		
		
			
				
					
					|  |  |  | function save_settings(stockpile) |  |  |  | function save_settings(stockpile) | 
			
		
	
		
		
			
				
					
					|  |  |  |     init() |  |  |  |     init() | 
			
		
	
		
		
			
				
					
					|  |  |  |     script.start(function() |  |  |  |     script.start(function() | 
			
		
	
		
		
			
				
					
					|  |  |  |         --local sp = dfhack.gui.geSelectedBuilding(true) |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         local suggested = stockpile.name |  |  |  |         local suggested = stockpile.name | 
			
		
	
		
		
			
				
					
					|  |  |  |         if #suggested == 0 then |  |  |  |         if #suggested == 0 then | 
			
		
	
		
		
			
				
					
					|  |  |  |             suggested = 'Stock1' |  |  |  |             suggested = 'Stock1' | 
			
		
	
		
		
			
				
					
					|  |  |  |         end |  |  |  |         end | 
			
		
	
		
		
			
				
					
					|  |  |  |         local path = persist.GlobalTable.stockpiles['settings_path'] |  |  |  |         local path = get_path() | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |         local sok,filename = script.showInputPrompt('Stockpile Settings', 'Enter stockpile name', COLOR_WHITE, suggested) |  |  |  |         local sok,filename = script.showInputPrompt('Stockpile Settings', 'Enter stockpile name', COLOR_WHITE, suggested) | 
			
		
	
		
		
			
				
					
					|  |  |  |         if sok then |  |  |  |         if sok then | 
			
		
	
		
		
			
				
					
					|  |  |  |             stockpiles_save(path..'/'..filename); |  |  |  |             if filename == nil or filename == '' then | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 script.showMessage('Stockpile Settings', 'Invalid File Name', COLOR_RED) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             else | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 print("saving...", path..'/'..filename) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 stockpiles_save(path..'/'..filename) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             end | 
			
		
	
		
		
			
				
					
					|  |  |  |         end |  |  |  |         end | 
			
		
	
		
		
			
				
					
					|  |  |  |     end) |  |  |  |     end) | 
			
		
	
		
		
			
				
					
					|  |  |  | end |  |  |  | end | 
			
		
	
	
		
		
			
				
					|  |  | @ -76,6 +95,16 @@ function manage_settings(sp) | 
			
		
	
		
		
			
				
					
					|  |  |  |     end) |  |  |  |     end) | 
			
		
	
		
		
			
				
					
					|  |  |  | end |  |  |  | end | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 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 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | function guard() |  |  |  | function guard() | 
			
		
	
		
		
			
				
					
					|  |  |  |     if not string.match(dfhack.gui.getCurFocus(), '^dwarfmode/QueryBuilding/Some/Stockpile') then |  |  |  |     if not string.match(dfhack.gui.getCurFocus(), '^dwarfmode/QueryBuilding/Some/Stockpile') then | 
			
		
	
		
		
			
				
					
					|  |  |  |         qerror("This script requires a stockpile selected in the 'q' mode") |  |  |  |         qerror("This script requires a stockpile selected in the 'q' mode") | 
			
		
	
	
		
		
			
				
					|  |  | @ -86,11 +115,18 @@ end | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | function set_path(path) |  |  |  | function set_path(path) | 
			
		
	
		
		
			
				
					
					|  |  |  |     init() |  |  |  |     init() | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     if persist == nil then | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         qerror("This version of DFHack doesn't support setting the stockpile settings path. Sorry.") | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         return | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     end | 
			
		
	
		
		
			
				
					
					|  |  |  |     persist.GlobalTable.stockpiles['settings_path'] = path |  |  |  |     persist.GlobalTable.stockpiles['settings_path'] = path | 
			
		
	
		
		
			
				
					
					|  |  |  | end |  |  |  | end | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | function get_path() |  |  |  | function get_path() | 
			
		
	
		
		
			
				
					
					|  |  |  |     init() |  |  |  |     init() | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     if persist == nil then | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         return "stocksettings" | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     end | 
			
		
	
		
		
			
				
					
					|  |  |  |     return persist.GlobalTable.stockpiles['settings_path'] |  |  |  |     return persist.GlobalTable.stockpiles['settings_path'] | 
			
		
	
		
		
			
				
					
					|  |  |  | end |  |  |  | end | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
	
		
		
			
				
					|  |  | 
 |