Current attempt to fix lua stack smashing

develop
eamondo2 2023-02-08 01:59:57 -05:00
parent b2ecb8aaab
commit be0cec9520
2 changed files with 40 additions and 8 deletions

@ -567,6 +567,25 @@ 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) {
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;
stockpile_config.emplace("id", id);
stockpile_config.emplace("monitored", monitored);
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) {
map<string, int32_t> stockpile_config;
stockpile_config.emplace("id", id);
stockpile_config.emplace("monitored", monitored);
stockpiles.emplace(id, stockpile_config);
}
static void automelt_designate(color_ostream &out) { static void automelt_designate(color_ostream &out) {
DEBUG(status, out).print("entering automelt designate\n"); DEBUG(status, out).print("entering automelt designate\n");
out.print("designated %d item(s) for melting\n", do_cycle(out)); out.print("designated %d item(s) for melting\n", do_cycle(out));
@ -736,6 +755,8 @@ static int automelt_getSelectedStockpileConfig(lua_State *L){
return 1; return 1;
} }
static int automelt_getItemCountsAndStockpileConfigs(lua_State *L) { static int automelt_getItemCountsAndStockpileConfigs(lua_State *L) {
color_ostream *out = Lua::GetOutput(L); color_ostream *out = Lua::GetOutput(L);
if (!out) if (!out)
@ -762,24 +783,30 @@ static int automelt_getItemCountsAndStockpileConfigs(lua_State *L) {
Lua::Push(L, item_count_piles); Lua::Push(L, item_count_piles);
Lua::Push(L, marked_item_count_piles); Lua::Push(L, marked_item_count_piles);
Lua::Push(L, premarked_item_count_piles); Lua::Push(L, premarked_item_count_piles);
int32_t bldg_count = 0;
map<int32_t, map<string, int32_t>> stockpile_config_map;
for (auto pile : world->buildings.other.STOCKPILE) { for (auto pile : world->buildings.other.STOCKPILE) {
if (!isStockpile(pile)) if (!isStockpile(pile))
continue; continue;
bldg_count++;
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)); DEBUG(cycle,*out).print("indexed_id=%d\n", get_config_val(watched_stockpiles[id], STOCKPILE_CONFIG_ID));
push_stockpile_config(L, watched_stockpiles[id]); emplace_bulk_stockpile_configs(L, watched_stockpiles[id], stockpile_config_map);
} else { } else {
push_stockpile_config(L, id, false); emplace_bulk_stockpile_configs(L, id, false, 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");
return 4+bldg_count; return 5;
} }
DFHACK_PLUGIN_LUA_FUNCTIONS{ DFHACK_PLUGIN_LUA_FUNCTIONS{

@ -64,8 +64,12 @@ function getItemCountsAndStockpileConfigs()
ret.item_counts = table.remove(data, 1) ret.item_counts = table.remove(data, 1)
ret.marked_item_counts = table.remove(data, 1) ret.marked_item_counts = table.remove(data, 1)
ret.premarked_item_counts = table.remove(data, 1) ret.premarked_item_counts = table.remove(data, 1)
ret.stockpile_configs = data local unparsed_stockpile_configs = table.remove(data, 1)
for _,c in ipairs(ret.stockpile_configs) do ret.stockpile_configs = {}
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
@ -76,6 +80,7 @@ function getItemCountsAndStockpileConfigs()
end end
c.monitored = c.monitored ~= 0 c.monitored = c.monitored ~= 0
end end
table.insert(ret.stockpile_configs, c)
end end
return ret return ret