Fixes the lua stack smashing issue

develop
eamondo2 2023-02-08 14:01:32 -05:00
parent 5002427bec
commit 02a249fdee
4 changed files with 50 additions and 21 deletions

@ -803,6 +803,33 @@ static void push_burrow_config(lua_State *L, PersistentDataItem &c) {
get_config_bool(c, BURROW_CONFIG_PROTECT_COOKABLE)); get_config_bool(c, BURROW_CONFIG_PROTECT_COOKABLE));
} }
static void emplace_bulk_burrow_config(lua_State *L, map<int32_t, map<string, int32_t>> &burrows, int id, bool chop = false,
bool clearcut = false, bool protect_brewable = false,
bool protect_edible = false, bool protect_cookable = false) {
map<string, int32_t> burrow_config;
burrow_config.emplace("id", id);
burrow_config.emplace("chop", chop);
burrow_config.emplace("clearcut", clearcut);
burrow_config.emplace("protect_brewable", protect_brewable);
burrow_config.emplace("protect_edible", protect_edible);
burrow_config.emplace("protect_cookable", protect_cookable);
burrows.emplace(id, burrow_config);
}
static void emplace_bulk_burrow_config(lua_State *L, map<int32_t, map<string, int32_t>> &burrows, PersistentDataItem &c) {
emplace_bulk_burrow_config(L, burrows, get_config_val(c, BURROW_CONFIG_ID),
get_config_bool(c, BURROW_CONFIG_CHOP),
get_config_bool(c, BURROW_CONFIG_CLEARCUT),
get_config_bool(c, BURROW_CONFIG_PROTECT_BREWABLE),
get_config_bool(c, BURROW_CONFIG_PROTECT_EDIBLE),
get_config_bool(c, BURROW_CONFIG_PROTECT_COOKABLE));
}
static int autochop_getTreeCountsAndBurrowConfigs(lua_State *L) { static int autochop_getTreeCountsAndBurrowConfigs(lua_State *L) {
color_ostream *out = Lua::GetOutput(L); color_ostream *out = Lua::GetOutput(L);
if (!out) if (!out)
@ -818,6 +845,9 @@ static int autochop_getTreeCountsAndBurrowConfigs(lua_State *L) {
&designated_trees, &accessible_yield, &tree_counts, &designated_tree_counts); &designated_trees, &accessible_yield, &tree_counts, &designated_tree_counts);
map<string, int32_t> summary; map<string, int32_t> summary;
map<int32_t, map<string, int32_t>> burrow_config_map;
summary.emplace("accessible_trees", accessible_trees); summary.emplace("accessible_trees", accessible_trees);
summary.emplace("inaccessible_trees", inaccessible_trees); summary.emplace("inaccessible_trees", inaccessible_trees);
summary.emplace("designated_trees", designated_trees); summary.emplace("designated_trees", designated_trees);
@ -831,13 +861,17 @@ static int autochop_getTreeCountsAndBurrowConfigs(lua_State *L) {
for (auto &burrow : plotinfo->burrows.list) { for (auto &burrow : plotinfo->burrows.list) {
int id = burrow->id; int id = burrow->id;
if (watched_burrows_indices.count(id)) { if (watched_burrows_indices.count(id)) {
push_burrow_config(L, watched_burrows[watched_burrows_indices[id]]); // push_burrow_config(L, watched_burrows[watched_burrows_indices[id]]);
emplace_bulk_burrow_config(L, burrow_config_map, watched_burrows[watched_burrows_indices[id]]);
} else { } else {
push_burrow_config(L, id); // push_burrow_config(L, id);
emplace_bulk_burrow_config(L, burrow_config_map, id);
} }
} }
return 3 + plotinfo->burrows.list.size(); Lua::Push(L, burrow_config_map);
return 4;
} }
static int autochop_getBurrowConfig(lua_State *L) { static int autochop_getBurrowConfig(lua_State *L) {

@ -567,9 +567,8 @@ static void push_stockpile_config(lua_State *L, PersistentDataItem &c) {
get_config_bool(c, STOCKPILE_CONFIG_MONITORED)); get_config_bool(c, STOCKPILE_CONFIG_MONITORED));
} }
static void emplace_bulk_stockpile_configs(lua_State *L, PersistentDataItem &c, map<int32_t, map<string, int32_t>> &stockpiles) { static void emplace_bulk_stockpile_config(lua_State *L, int id, bool monitored, map<int32_t, map<string, int32_t>> &stockpiles) {
int32_t id = get_config_val(c, STOCKPILE_CONFIG_ID);
bool monitored = get_config_bool(c, STOCKPILE_CONFIG_MONITORED);
map<string, int32_t> stockpile_config; map<string, int32_t> stockpile_config;
stockpile_config.emplace("id", id); stockpile_config.emplace("id", id);
stockpile_config.emplace("monitored", monitored); stockpile_config.emplace("monitored", monitored);
@ -577,13 +576,10 @@ static void emplace_bulk_stockpile_configs(lua_State *L, PersistentDataItem &c,
stockpiles.emplace(id, stockpile_config); stockpiles.emplace(id, stockpile_config);
} }
static void emplace_bulk_stockpile_configs(lua_State *L, int id, bool monitored, map<int32_t, map<string, int32_t>> &stockpiles) { static void emplace_bulk_stockpile_config(lua_State *L, PersistentDataItem &c, map<int32_t, map<string, int32_t>> &stockpiles) {
int32_t id = get_config_val(c, STOCKPILE_CONFIG_ID);
map<string, int32_t> stockpile_config; bool monitored = get_config_bool(c, STOCKPILE_CONFIG_MONITORED);
stockpile_config.emplace("id", id); emplace_bulk_stockpile_config(L, id, monitored, stockpiles);
stockpile_config.emplace("monitored", monitored);
stockpiles.emplace(id, stockpile_config);
} }
static void automelt_designate(color_ostream &out) { static void automelt_designate(color_ostream &out) {
@ -792,17 +788,15 @@ static int automelt_getItemCountsAndStockpileConfigs(lua_State *L) {
int id = pile->id; int id = pile->id;
if (watched_stockpiles.count(id)) { if (watched_stockpiles.count(id)) {
DEBUG(cycle,*out).print("indexed_id=%d\n", get_config_val(watched_stockpiles[id], STOCKPILE_CONFIG_ID)); emplace_bulk_stockpile_config(L, watched_stockpiles[id], stockpile_config_map);
emplace_bulk_stockpile_configs(L, watched_stockpiles[id], stockpile_config_map);
} else { } else {
emplace_bulk_stockpile_configs(L, id, false, stockpile_config_map); emplace_bulk_stockpile_config(L, id, false, stockpile_config_map);
} }
} }
Lua::Push(L, stockpile_config_map); Lua::Push(L, stockpile_config_map);
DEBUG(cycle, *out).print("confmap_length: %d\n", stockpile_config_map.size());
DEBUG(perf, *out).print("exit automelt_getItemCountsAndStockpileConfigs\n"); DEBUG(perf, *out).print("exit automelt_getItemCountsAndStockpileConfigs\n");

@ -96,14 +96,17 @@ function getTreeCountsAndBurrowConfigs()
ret.summary = table.remove(data, 1) ret.summary = table.remove(data, 1)
ret.tree_counts = table.remove(data, 1) ret.tree_counts = table.remove(data, 1)
ret.designated_tree_counts = table.remove(data, 1) ret.designated_tree_counts = table.remove(data, 1)
ret.burrow_configs = data local unparsed_burrow_configs = table.remove(data, 1)
for _,c in ipairs(ret.burrow_configs) do
ret.burrow_configs = {}
for idx,c in pairs(unparsed_burrow_configs) do
c.name = df.burrow.find(c.id).name c.name = df.burrow.find(c.id).name
c.chop = c.chop ~= 0 c.chop = c.chop ~= 0
c.clearcut = c.clearcut ~= 0 c.clearcut = c.clearcut ~= 0
c.protect_brewable = c.protect_brewable ~= 0 c.protect_brewable = c.protect_brewable ~= 0
c.protect_edible = c.protect_edible ~= 0 c.protect_edible = c.protect_edible ~= 0
c.protect_cookable = c.protect_cookable ~= 0 c.protect_cookable = c.protect_cookable ~= 0
table.insert(ret.burrow_configs, c)
end end
return ret return ret
end end

@ -68,8 +68,6 @@ function getItemCountsAndStockpileConfigs()
ret.stockpile_configs = {} ret.stockpile_configs = {}
for idx,c in pairs(unparsed_stockpile_configs) do for idx,c in pairs(unparsed_stockpile_configs) do
print(c.id)
print(c.monitored)
if not c.id or c.id == -1 then if not c.id or c.id == -1 then
c.name = "ERROR" c.name = "ERROR"
c.monitored = false c.monitored = false