stockpiles: user friendly error handling

* display errors in the GUI when necessary
* handle older versions of DFHack without the persist module
develop
Casey Link 2014-12-04 11:52:38 +01:00
parent 2f4678eee5
commit 7e6066daf2
3 changed files with 75 additions and 9 deletions

@ -9,12 +9,19 @@ local _ENV = mkmodule('plugins.stockpiles')
* 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 script = require 'gui.script'
local persist = require 'persist-table'
local persist = safe_require('persist-table')
function init()
if persist == nil then return end
if dfhack.isMapLoaded() then
if persist.GlobalTable.stockpiles == nil then
persist.GlobalTable.stockpiles = {}
@ -33,13 +40,21 @@ end
function load_settings()
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()
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))
if tok then
local filename = list[i];
stockpiles_load(path..'/'..filename);
stockpiles_load(path..'/'..filename)
end
end)
end
@ -47,15 +62,19 @@ end
function save_settings(stockpile)
init()
script.start(function()
--local sp = dfhack.gui.geSelectedBuilding(true)
local suggested = stockpile.name
if #suggested == 0 then
suggested = 'Stock1'
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)
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
@ -76,6 +95,16 @@ function manage_settings(sp)
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()
if not string.match(dfhack.gui.getCurFocus(), '^dwarfmode/QueryBuilding/Some/Stockpile') then
qerror("This script requires a stockpile selected in the 'q' mode")
@ -86,11 +115,18 @@ end
function set_path(path)
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
end
function get_path()
init()
if persist == nil then
return "stocksettings"
end
return persist.GlobalTable.stockpiles['settings_path']
end

@ -87,7 +87,7 @@ bool StockpileSerializer::serialize_to_file ( const std::string & file )
std::fstream output ( file, std::ios::out | std::ios::binary | std::ios::trunc );
if ( output.fail() )
{
*mOut << "ERROR: failed to open file for writing: " << file << endl;
debug() << "ERROR: failed to open file for writing: " << file << endl;
return false;
}
return serialize_to_ostream ( &output );
@ -109,7 +109,7 @@ bool StockpileSerializer::unserialize_from_file ( const std::string & file )
std::fstream input ( file, std::ios::in | std::ios::binary );
if ( input.fail() )
{
*mOut << "ERROR: failed to open file for reading: " << file << endl;
debug() << "ERROR: failed to open file for reading: " << file << endl;
return false;
}
return parse_from_istream ( &input );

@ -13,6 +13,7 @@
#include "StockpileSerializer.h"
#include "modules/Filesystem.h"
#include "modules/Gui.h"
#include "modules/Filesystem.h"
@ -331,6 +332,30 @@ bool manage_settings ( building_stockpilest *sp )
return true;
}
bool show_message_box ( const std::string & title, const std::string & msg, bool is_error = false )
{
auto L = Lua::Core::State;
color_ostream_proxy out ( Core::getInstance().getConsole() );
CoreSuspendClaimer suspend;
Lua::StackUnwinder top ( L );
if ( !lua_checkstack ( L, 4 ) )
return false;
if ( !Lua::PushModulePublic ( out, L, "plugins.stockpiles", "show_message_box" ) )
return false;
Lua::Push ( L, title );
Lua::Push ( L, msg );
Lua::Push ( L, is_error );
if ( !Lua::SafeCall ( out, L, 3, 0 ) )
return false;
return true;
}
struct stockpiles_import_hook : public df::viewscreen_dwarfmodest
{
typedef df::viewscreen_dwarfmodest interpose_base;
@ -498,6 +523,8 @@ static void stockpiles_load ( color_ostream &out, std::string filename )
params.push_back ( filename );
command_result r = loadstock ( out, params );
out << " result = "<< r << endl;
if ( r != CR_OK )
show_message_box ( "Stockpile Settings Error", "Couldn't load. Does the folder exist?", true );
}
@ -509,6 +536,8 @@ static void stockpiles_save ( color_ostream &out, std::string filename )
params.push_back ( filename );
command_result r = savestock ( out, params );
out << " result = "<< r << endl;
if ( r != CR_OK )
show_message_box ( "Stockpile Settings Error", "Couldn't save. Does the folder exist?", true );
}
DFHACK_PLUGIN_LUA_FUNCTIONS
@ -526,3 +555,4 @@ DFHACK_PLUGIN_LUA_COMMANDS