Merge pull request #3428 from myk002/myk_route_settings

[stockpiles] allow setting of hauling route stop settings
develop
Myk 2023-05-28 20:36:11 -07:00 committed by GitHub
commit 2b8a437a5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 345 additions and 234 deletions

@ -134,6 +134,9 @@ entire category, or with a filter, any matchable subset thereof::
cat_weapons
cat_wood
There is also an ``everything`` file that includes all the above categories,
including refuse and corpses.
For many of the categories, there are also flags and subcategory prefixes that
you can match with filters and convenient pre-made settings files that
manipulate interesting category subsets.

@ -101,7 +101,7 @@ function export_stockpile(name, opts)
stockpiles_export(name, get_sp_id(opts), includedElements)
end
function import_stockpile(name, opts)
local function normalize_name(name)
local is_library = false
if name:startswith('library/') then
name = name:sub(9)
@ -109,11 +109,19 @@ function import_stockpile(name, opts)
end
assert_safe_name(name)
if not is_library and dfhack.filesystem.exists(STOCKPILES_DIR .. '/' .. name .. '.dfstock') then
name = STOCKPILES_DIR .. '/' .. name
else
name = STOCKPILES_LIBRARY_DIR .. '/' .. name
return STOCKPILES_DIR .. '/' .. name
end
stockpiles_import(name, get_sp_id(opts), opts.mode, table.concat(opts.filters, ','))
return STOCKPILES_LIBRARY_DIR .. '/' .. name
end
function import_stockpile(name, opts)
name = normalize_name(name)
stockpiles_import(name, get_sp_id(opts), opts.mode, table.concat(opts.filters or {}, ','))
end
function import_route(name, route_id, stop_id, mode, filters)
name = normalize_name(name)
stockpiles_route_import(name, route_id, stop_id, mode, table.concat(filters or {}, ','))
end
local valid_includes = {general=true, categories=true, types=true}

File diff suppressed because it is too large Load Diff

@ -4,6 +4,7 @@
#include "df/itemdef.h"
#include "df/organic_mat_category.h"
#include "df/stockpile_settings.h"
#include "proto/stockpiles.pb.h"
@ -57,14 +58,14 @@ struct food_pair {
/**
* Class for serializing the stockpile_settings structure into a Google protobuf
*/
class StockpileSerializer {
class StockpileSettingsSerializer {
public:
/**
* @param stockpile stockpile to read or write settings to
* @param settings settings to read or write to
*/
StockpileSerializer(df::building_stockpilest* stockpile);
StockpileSettingsSerializer(df::stockpile_settings *settings);
~StockpileSerializer();
~StockpileSettingsSerializer();
/**
* Since we depend on protobuf-lite, not the full lib, we copy this function from
@ -88,20 +89,20 @@ public:
*/
bool unserialize_from_file(const std::string& file, DeserializeMode mode, const std::vector<std::string>& filters);
private:
df::building_stockpilest* mPile;
protected:
dfstockpiles::StockpileSettings mBuffer;
// read memory structures and serialize to protobuf
void write(uint32_t includedElements);
virtual void write(uint32_t includedElements);
// parse serialized data into ui indices
void read(DeserializeMode mode, const std::vector<std::string>& filters);
virtual void read(DeserializeMode mode, const std::vector<std::string>& filters);
void write_containers();
void read_containers(DeserializeMode mode);
void write_general();
void read_general(DeserializeMode mode);
virtual void write_general();
virtual void read_general(DeserializeMode mode);
private:
df::stockpile_settings *mSettings;
bool write_ammo(dfstockpiles::StockpileSettings::AmmoSet* ammo);
void read_ammo(DeserializeMode mode, const std::vector<std::string>& filters);
@ -139,3 +140,32 @@ private:
bool write_wood(dfstockpiles::StockpileSettings::WoodSet* wood);
void read_wood(DeserializeMode mode, const std::vector<std::string>& filters);
};
/**
* Class for serializing a stockpile into a Google protobuf
*/
class StockpileSerializer : public StockpileSettingsSerializer {
public:
/**
* @param stockpile stockpile to read or write settings to
*/
StockpileSerializer(df::building_stockpilest* stockpile);
~StockpileSerializer();
protected:
// read memory structures and serialize to protobuf
virtual void write(uint32_t includedElements);
// parse serialized data into ui indices
virtual void read(DeserializeMode mode, const std::vector<std::string>& filters);
virtual void write_general();
virtual void read_general(DeserializeMode mode);
private:
df::building_stockpilest* mPile;
void write_containers();
void read_containers(DeserializeMode mode);
};

@ -8,6 +8,8 @@
#include "df/building.h"
#include "df/building_stockpilest.h"
#include "df/hauling_route.h"
#include "df/hauling_stop.h"
#include <string>
#include <vector>
@ -147,8 +149,54 @@ static bool stockpiles_import(color_ostream& out, string fname, int id, string m
return true;
}
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;
}
DFHACK_PLUGIN_LUA_FUNCTIONS {
DFHACK_LUA_FUNCTION(stockpiles_export),
DFHACK_LUA_FUNCTION(stockpiles_import),
DFHACK_LUA_FUNCTION(stockpiles_route_import),
DFHACK_LUA_END
};