2023-03-10 15:08:31 -07:00
|
|
|
#include "Debug.h"
|
2023-03-15 18:16:42 -06:00
|
|
|
#include "LuaTools.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 04:06:01 -07:00
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
#include "df/building.h"
|
|
|
|
#include "df/building_stockpilest.h"
|
2023-05-28 03:26:06 -06:00
|
|
|
#include "df/hauling_route.h"
|
|
|
|
#include "df/hauling_stop.h"
|
2023-03-15 18:16:42 -06:00
|
|
|
|
2014-12-02 04:06:01 -07:00
|
|
|
using std::string;
|
2023-03-15 18:16:42 -06:00
|
|
|
using std::vector;
|
2014-12-02 04:06:01 -07:00
|
|
|
|
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-04-18 09:22:52 -06:00
|
|
|
namespace DFHack
|
|
|
|
{
|
|
|
|
DBG_DECLARE(stockpiles, log, DebugCategory::LINFO);
|
2023-03-10 15:08:31 -07:00
|
|
|
}
|
|
|
|
|
2023-04-18 09:22:52 -06:00
|
|
|
static command_result do_command(color_ostream& out, vector<string>& parameters);
|
2023-03-15 18:16:42 -06:00
|
|
|
|
2023-04-18 09:22:52 -06:00
|
|
|
DFhackCExport command_result plugin_init(color_ostream &out, vector<PluginCommand> &commands) {
|
|
|
|
DEBUG(log, out).print("initializing %s\n", plugin_name);
|
2023-03-10 15:08:31 -07:00
|
|
|
|
2023-03-05 17:16:32 -07:00
|
|
|
commands.push_back(PluginCommand(
|
2023-03-15 18:16:42 -06:00
|
|
|
plugin_name,
|
|
|
|
"Import, export, or modify stockpile settings and features.",
|
|
|
|
do_command));
|
2014-12-04 02:47:17 -07:00
|
|
|
|
2014-12-02 04:06:01 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2023-05-04 02:29:07 -06:00
|
|
|
bool call_stockpiles_lua(color_ostream* out, const char* fn_name,
|
|
|
|
int nargs, int nres, Lua::LuaLambda&& args_lambda, Lua::LuaLambda&& res_lambda) {
|
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
DEBUG(log).print("calling stockpiles lua function: '%s'\n", fn_name);
|
|
|
|
|
|
|
|
CoreSuspender guard;
|
|
|
|
|
|
|
|
auto L = Lua::Core::State;
|
|
|
|
Lua::StackUnwinder top(L);
|
|
|
|
|
|
|
|
if (!out)
|
|
|
|
out = &Core::getInstance().getConsole();
|
|
|
|
|
|
|
|
return Lua::CallLuaModuleFunction(*out, L, "plugins.stockpiles", fn_name,
|
|
|
|
nargs, nres,
|
|
|
|
std::forward<Lua::LuaLambda&&>(args_lambda),
|
|
|
|
std::forward<Lua::LuaLambda&&>(res_lambda));
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
static command_result do_command(color_ostream &out, vector<string> ¶meters) {
|
|
|
|
CoreSuspender suspend;
|
|
|
|
|
|
|
|
bool show_help = false;
|
|
|
|
if (!call_stockpiles_lua(&out, "parse_commandline", 1, 1,
|
|
|
|
[&](lua_State *L) {
|
|
|
|
Lua::PushVector(L, parameters);
|
|
|
|
},
|
|
|
|
[&](lua_State *L) {
|
|
|
|
show_help = !lua_toboolean(L, -1);
|
|
|
|
})) {
|
|
|
|
return CR_FAILURE;
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
return show_help ? CR_WRONG_USAGE : CR_OK;
|
|
|
|
}
|
2014-12-02 04:06:01 -07:00
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
// Lua API
|
|
|
|
//
|
|
|
|
|
|
|
|
static df::building_stockpilest* get_stockpile(int id) {
|
|
|
|
return virtual_cast<df::building_stockpilest>(df::building::find(id));
|
|
|
|
}
|
|
|
|
|
2023-03-15 22:54:48 -06:00
|
|
|
static bool stockpiles_export(color_ostream& out, string fname, int id, uint32_t includedElements) {
|
2023-03-15 18:16:42 -06:00
|
|
|
df::building_stockpilest* sp = get_stockpile(id);
|
|
|
|
if (!sp) {
|
|
|
|
out.printerr("Specified building isn't a stockpile: %d.\n", id);
|
|
|
|
return false;
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
if (!is_dfstockfile(fname))
|
|
|
|
fname += ".dfstock";
|
2014-12-02 04:06:01 -07:00
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
try {
|
2023-03-15 18:16:42 -06:00
|
|
|
StockpileSerializer cereal(sp);
|
2023-05-04 02:29:07 -06:00
|
|
|
if (!cereal.serialize_to_file(out, fname, includedElements)) {
|
2023-03-15 18:16:42 -06:00
|
|
|
out.printerr("could not save to '%s'\n", fname.c_str());
|
|
|
|
return false;
|
2016-10-29 00:35:27 -06:00
|
|
|
}
|
|
|
|
}
|
2023-03-10 15:08:31 -07:00
|
|
|
catch (std::exception& e) {
|
|
|
|
out.printerr("serialization failed: protobuf exception: %s\n", e.what());
|
2023-03-15 18:16:42 -06:00
|
|
|
return false;
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
2016-10-29 00:35:27 -06:00
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
return true;
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
|
|
|
|
2023-03-15 22:54:48 -06:00
|
|
|
static bool stockpiles_import(color_ostream& out, string fname, int id, string mode_str, string filter) {
|
2023-03-15 18:16:42 -06:00
|
|
|
df::building_stockpilest* sp = get_stockpile(id);
|
2023-03-10 15:08:31 -07:00
|
|
|
if (!sp) {
|
2023-03-15 18:16:42 -06:00
|
|
|
out.printerr("Specified building isn't a stockpile: %d.\n", id);
|
|
|
|
return false;
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
if (!is_dfstockfile(fname))
|
|
|
|
fname += ".dfstock";
|
2014-12-02 04:06:01 -07:00
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
if (!Filesystem::exists(fname)) {
|
|
|
|
out.printerr("ERROR: file doesn't exist: '%s'\n", fname.c_str());
|
|
|
|
return false;
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
|
|
|
|
2023-03-15 22:54:48 -06:00
|
|
|
DeserializeMode mode = DESERIALIZE_MODE_SET;
|
|
|
|
if (mode_str == "enable")
|
|
|
|
mode = DESERIALIZE_MODE_ENABLE;
|
|
|
|
else if (mode_str == "disable")
|
|
|
|
mode = DESERIALIZE_MODE_DISABLE;
|
|
|
|
|
2023-03-21 00:25:52 -06:00
|
|
|
vector<string> filters;
|
|
|
|
split_string(&filters, filter, ",", true);
|
|
|
|
|
2023-03-10 15:08:31 -07:00
|
|
|
try {
|
2023-03-15 18:16:42 -06:00
|
|
|
StockpileSerializer cereal(sp);
|
2023-05-03 19:01:11 -06:00
|
|
|
if (!cereal.unserialize_from_file(out, fname, mode, filters)) {
|
2023-03-15 18:16:42 -06:00
|
|
|
out.printerr("deserialization failed: '%s'\n", fname.c_str());
|
|
|
|
return false;
|
2016-10-29 00:35:27 -06:00
|
|
|
}
|
|
|
|
}
|
2023-03-10 15:08:31 -07:00
|
|
|
catch (std::exception& e) {
|
2023-03-15 18:16:42 -06:00
|
|
|
out.printerr("deserialization failed: protobuf exception: %s\n", e.what());
|
|
|
|
return false;
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
2023-03-15 18:16:42 -06:00
|
|
|
|
|
|
|
return true;
|
2014-12-02 04:06:01 -07:00
|
|
|
}
|
2023-03-15 18:16:42 -06:00
|
|
|
|
2023-05-28 03:26:06 -06:00
|
|
|
static bool stockpiles_route_import(color_ostream& out, string fname, int route_id, int stop_id, string mode_str, string filter) {
|
|
|
|
auto route = df::hauling_route::find(route_id);
|
|
|
|
if (!route) {
|
|
|
|
out.printerr("Specified hauling route not found: %d.\n", route_id);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
df::hauling_stop *stop = binsearch_in_vector(route->stops, &df::hauling_stop::id, stop_id);
|
|
|
|
if (!stop) {
|
|
|
|
out.printerr("Specified hauling stop not found in route %d: %d.\n", route_id, stop_id);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_dfstockfile(fname))
|
|
|
|
fname += ".dfstock";
|
|
|
|
|
|
|
|
if (!Filesystem::exists(fname)) {
|
|
|
|
out.printerr("ERROR: file doesn't exist: '%s'\n", fname.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeserializeMode mode = DESERIALIZE_MODE_SET;
|
|
|
|
if (mode_str == "enable")
|
|
|
|
mode = DESERIALIZE_MODE_ENABLE;
|
|
|
|
else if (mode_str == "disable")
|
|
|
|
mode = DESERIALIZE_MODE_DISABLE;
|
|
|
|
|
|
|
|
vector<string> filters;
|
|
|
|
split_string(&filters, filter, ",", true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
StockpileSettingsSerializer cereal(&stop->settings);
|
|
|
|
if (!cereal.unserialize_from_file(fname, mode, filters)) {
|
|
|
|
out.printerr("deserialization failed: '%s'\n", fname.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
|
|
|
out.printerr("deserialization failed: protobuf exception: %s\n", e.what());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-15 18:16:42 -06:00
|
|
|
DFHACK_PLUGIN_LUA_FUNCTIONS {
|
|
|
|
DFHACK_LUA_FUNCTION(stockpiles_export),
|
|
|
|
DFHACK_LUA_FUNCTION(stockpiles_import),
|
2023-05-28 03:26:06 -06:00
|
|
|
DFHACK_LUA_FUNCTION(stockpiles_route_import),
|
2023-03-15 18:16:42 -06:00
|
|
|
DFHACK_LUA_END
|
|
|
|
};
|