2014-12-02 04:06:01 -07:00
|
|
|
#include "PluginManager.h"
|
2014-12-04 02:47:17 -07:00
|
|
|
#include "StockpileUtils.h"
|
2014-12-02 04:06:01 -07:00
|
|
|
#include "StockpileSerializer.h"
|
|
|
|
|
2014-12-04 03:52:38 -07:00
|
|
|
#include "modules/Filesystem.h"
|
2014-12-02 12:00:16 -07:00
|
|
|
#include "modules/Gui.h"
|
2014-12-02 04:06:01 -07:00
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
|
|
|
|
2023-03-05 17:16:32 -07:00
|
|
|
using namespace DFHack;
|
2014-12-02 04:06:01 -07:00
|
|
|
|
2023-03-05 17:16:32 -07:00
|
|
|
DFHACK_PLUGIN("stockpiles");
|
2014-12-02 04:06:01 -07:00
|
|
|
|
|
|
|
static command_result savestock ( color_ostream &out, vector <string> & parameters );
|
|
|
|
static command_result loadstock ( color_ostream &out, vector <string> & parameters );
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands )
|
|
|
|
{
|
2023-03-05 17:16:32 -07:00
|
|
|
commands.push_back(PluginCommand(
|
|
|
|
"savestock",
|
|
|
|
"Save the active stockpile's settings to a file.",
|
|
|
|
savestock,
|
|
|
|
Gui::any_stockpile_hotkey));
|
|
|
|
commands.push_back(PluginCommand(
|
|
|
|
"loadstock",
|
|
|
|
"Load and apply stockpile settings from a file.",
|
|
|
|
loadstock,
|
|
|
|
Gui::any_stockpile_hotkey));
|
2014-12-04 02:47:17 -07:00
|
|
|
|
2014-12-02 04:06:01 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
|
|
|
|
{
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// exporting
|
|
|
|
static command_result savestock ( color_ostream &out, vector <string> & parameters )
|
|
|
|
{
|
2023-03-05 17:16:32 -07:00
|
|
|
df::building_stockpilest *sp = Gui::getSelectedStockpile(out, true);
|
2014-12-02 04:06:01 -07:00
|
|
|
if ( !sp )
|
|
|
|
{
|
|
|
|
out.printerr ( "Selected building isn't a stockpile.\n" );
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( parameters.size() > 2 )
|
|
|
|
{
|
|
|
|
out.printerr ( "Invalid parameters\n" );
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool debug = false;
|
|
|
|
std::string file;
|
|
|
|
for ( size_t i = 0; i < parameters.size(); ++i )
|
|
|
|
{
|
|
|
|
const std::string o = parameters.at ( i );
|
|
|
|
if ( o == "--debug" || o == "-d" )
|
|
|
|
debug = true;
|
|
|
|
else if ( !o.empty() && o[0] != '-' )
|
|
|
|
{
|
|
|
|
file = o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( file.empty() )
|
|
|
|
{
|
|
|
|
out.printerr ( "You must supply a valid filename.\n" );
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
StockpileSerializer cereal ( sp );
|
|
|
|
if ( debug )
|
|
|
|
cereal.enable_debug ( out );
|
|
|
|
|
|
|
|
if ( !is_dfstockfile ( file ) ) file += ".dfstock";
|
2016-10-29 00:35:27 -06:00
|
|
|
try
|
2014-12-02 04:06:01 -07:00
|
|
|
{
|
2016-10-29 00:35:27 -06:00
|
|
|
if ( !cereal.serialize_to_file ( file ) )
|
|
|
|
{
|
|
|
|
out.printerr ( "could not save to %s\n", file.c_str() );
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch ( std::exception &e )
|
|
|
|
{
|
|
|
|
out.printerr ( "serialization failed: protobuf exception: %s\n", e.what() );
|
2014-12-02 04:06:01 -07:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
2016-10-29 00:35:27 -06:00
|
|
|
|
2014-12-02 04:06:01 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// importing
|
|
|
|
static command_result loadstock ( color_ostream &out, vector <string> & parameters )
|
|
|
|
{
|
2023-03-05 17:16:32 -07:00
|
|
|
df::building_stockpilest *sp = Gui::getSelectedStockpile(out, true);
|
2014-12-02 04:06:01 -07:00
|
|
|
if ( !sp )
|
|
|
|
{
|
|
|
|
out.printerr ( "Selected building isn't a stockpile.\n" );
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( parameters.size() < 1 || parameters.size() > 2 )
|
|
|
|
{
|
|
|
|
out.printerr ( "Invalid parameters\n" );
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool debug = false;
|
|
|
|
std::string file;
|
|
|
|
for ( size_t i = 0; i < parameters.size(); ++i )
|
|
|
|
{
|
|
|
|
const std::string o = parameters.at ( i );
|
|
|
|
if ( o == "--debug" || o == "-d" )
|
|
|
|
debug = true;
|
|
|
|
else if ( !o.empty() && o[0] != '-' )
|
|
|
|
{
|
|
|
|
file = o;
|
|
|
|
}
|
|
|
|
}
|
2014-12-04 06:28:43 -07:00
|
|
|
if ( file.empty() ) {
|
|
|
|
out.printerr ( "ERROR: missing .dfstock file parameter\n");
|
|
|
|
return DFHack::CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
if ( !is_dfstockfile ( file ) )
|
|
|
|
file += ".dfstock";
|
|
|
|
if ( !Filesystem::exists ( file ) )
|
2014-12-02 04:06:01 -07:00
|
|
|
{
|
2014-12-04 06:28:43 -07:00
|
|
|
out.printerr ( "ERROR: the .dfstock file doesn't exist: %s\n", file.c_str());
|
2014-12-02 04:06:01 -07:00
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
StockpileSerializer cereal ( sp );
|
|
|
|
if ( debug )
|
|
|
|
cereal.enable_debug ( out );
|
2016-10-29 00:35:27 -06:00
|
|
|
try
|
2014-12-02 04:06:01 -07:00
|
|
|
{
|
2016-10-29 00:35:27 -06:00
|
|
|
if ( !cereal.unserialize_from_file ( file ) )
|
|
|
|
{
|
|
|
|
out.printerr ( "unserialization failed: %s\n", file.c_str() );
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch ( std::exception &e )
|
|
|
|
{
|
|
|
|
out.printerr ( "unserialization failed: protobuf exception: %s\n", e.what() );
|
2014-12-02 04:06:01 -07:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
return CR_OK;
|
|
|
|
}
|