2023-03-10 15:08:31 -07:00
|
|
|
#include "Debug.h"
|
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
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
REQUIRE_GLOBAL(world);
|
2014-12-02 04:06:01 -07:00
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
namespace DFHack {
|
|
|
|
DBG_DECLARE(stockpiles, log, DebugCategory::LINFO);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
DFhackCExport command_result plugin_shutdown(color_ostream& out) {
|
2014-12-02 04:06:01 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// exporting
|
2023-03-10 15:08:31 -07:00
|
|
|
static command_result savestock(color_ostream& out, vector <string>& parameters) {
|
|
|
|
df::building_stockpilest* sp = Gui::getSelectedStockpile(out, true);
|
|
|
|
if (!sp) {
|
|
|
|
out.printerr("Selected building isn't a stockpile.\n");
|
2014-12-02 04:06:01 -07:00
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
if (parameters.size() > 2) {
|
|
|
|
out.printerr("Invalid parameters\n");
|
2014-12-02 04:06:01 -07:00
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string file;
|
2023-03-10 15:08:31 -07:00
|
|
|
for (size_t i = 0; i < parameters.size(); ++i) {
|
|
|
|
const std::string o = parameters.at(i);
|
|
|
|
if (!o.empty() && o[0] != '-') {
|
2014-12-02 04:06:01 -07:00
|
|
|
file = o;
|
|
|
|
}
|
|
|
|
}
|
2023-03-10 15:08:31 -07:00
|
|
|
if (file.empty()) {
|
|
|
|
out.printerr("You must supply a valid filename.\n");
|
2014-12-02 04:06:01 -07:00
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
StockpileSerializer cereal(sp);
|
2014-12-02 04:06:01 -07:00
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
if (!is_dfstockfile(file)) file += ".dfstock";
|
|
|
|
try {
|
|
|
|
if (!cereal.serialize_to_file(file)) {
|
|
|
|
out.printerr("could not save to %s\n", file.c_str());
|
2016-10-29 00:35:27 -06:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2023-03-10 15:08:31 -07:00
|
|
|
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
|
2023-03-10 15:08:31 -07:00
|
|
|
static command_result loadstock(color_ostream& out, vector <string>& parameters) {
|
|
|
|
df::building_stockpilest* sp = Gui::getSelectedStockpile(out, true);
|
|
|
|
if (!sp) {
|
|
|
|
out.printerr("Selected building isn't a stockpile.\n");
|
2014-12-02 04:06:01 -07:00
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
if (parameters.size() < 1 || parameters.size() > 2) {
|
|
|
|
out.printerr("Invalid parameters\n");
|
2014-12-02 04:06:01 -07:00
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string file;
|
2023-03-10 15:08:31 -07:00
|
|
|
for (size_t i = 0; i < parameters.size(); ++i) {
|
|
|
|
const std::string o = parameters.at(i);
|
|
|
|
if (!o.empty() && o[0] != '-') {
|
2014-12-02 04:06:01 -07:00
|
|
|
file = o;
|
|
|
|
}
|
|
|
|
}
|
2023-03-10 15:08:31 -07:00
|
|
|
if (file.empty()) {
|
|
|
|
out.printerr("ERROR: missing .dfstock file parameter\n");
|
2014-12-04 06:28:43 -07:00
|
|
|
return DFHack::CR_WRONG_USAGE;
|
|
|
|
}
|
2023-03-10 15:08:31 -07:00
|
|
|
if (!is_dfstockfile(file))
|
2014-12-04 06:28:43 -07:00
|
|
|
file += ".dfstock";
|
2023-03-10 15:08:31 -07:00
|
|
|
if (!Filesystem::exists(file)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
StockpileSerializer cereal(sp);
|
|
|
|
try {
|
|
|
|
if (!cereal.unserialize_from_file(file)) {
|
|
|
|
out.printerr("unserialization failed: %s\n", file.c_str());
|
2016-10-29 00:35:27 -06:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2023-03-10 15:08:31 -07:00
|
|
|
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;
|
|
|
|
}
|