From 02a249fdee4181318a24672c14b4213ccaec5824 Mon Sep 17 00:00:00 2001 From: eamondo2 Date: Wed, 8 Feb 2023 14:01:32 -0500 Subject: [PATCH] Fixes the lua stack smashing issue --- plugins/autochop.cpp | 40 +++++++++++++++++++++++++++++++++++++--- plugins/automelt.cpp | 22 ++++++++-------------- plugins/lua/autochop.lua | 7 +++++-- plugins/lua/automelt.lua | 2 -- 4 files changed, 50 insertions(+), 21 deletions(-) diff --git a/plugins/autochop.cpp b/plugins/autochop.cpp index bf7540e29..7a16a79b4 100644 --- a/plugins/autochop.cpp +++ b/plugins/autochop.cpp @@ -803,6 +803,33 @@ static void push_burrow_config(lua_State *L, PersistentDataItem &c) { get_config_bool(c, BURROW_CONFIG_PROTECT_COOKABLE)); } +static void emplace_bulk_burrow_config(lua_State *L, map> &burrows, int id, bool chop = false, + bool clearcut = false, bool protect_brewable = false, + bool protect_edible = false, bool protect_cookable = false) { + + map 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> &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) { color_ostream *out = Lua::GetOutput(L); if (!out) @@ -818,6 +845,9 @@ static int autochop_getTreeCountsAndBurrowConfigs(lua_State *L) { &designated_trees, &accessible_yield, &tree_counts, &designated_tree_counts); map summary; + + map> burrow_config_map; + summary.emplace("accessible_trees", accessible_trees); summary.emplace("inaccessible_trees", inaccessible_trees); summary.emplace("designated_trees", designated_trees); @@ -831,13 +861,17 @@ static int autochop_getTreeCountsAndBurrowConfigs(lua_State *L) { for (auto &burrow : plotinfo->burrows.list) { int id = burrow->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 { - 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) { diff --git a/plugins/automelt.cpp b/plugins/automelt.cpp index 1df7f9a68..ca0cf1626 100644 --- a/plugins/automelt.cpp +++ b/plugins/automelt.cpp @@ -567,9 +567,8 @@ static void push_stockpile_config(lua_State *L, PersistentDataItem &c) { get_config_bool(c, STOCKPILE_CONFIG_MONITORED)); } -static void emplace_bulk_stockpile_configs(lua_State *L, PersistentDataItem &c, map> &stockpiles) { - int32_t id = get_config_val(c, STOCKPILE_CONFIG_ID); - bool monitored = get_config_bool(c, STOCKPILE_CONFIG_MONITORED); +static void emplace_bulk_stockpile_config(lua_State *L, int id, bool monitored, map> &stockpiles) { + map stockpile_config; stockpile_config.emplace("id", id); 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); } -static void emplace_bulk_stockpile_configs(lua_State *L, int id, bool monitored, map> &stockpiles) { - - map stockpile_config; - stockpile_config.emplace("id", id); - stockpile_config.emplace("monitored", monitored); - - stockpiles.emplace(id, stockpile_config); +static void emplace_bulk_stockpile_config(lua_State *L, PersistentDataItem &c, map> &stockpiles) { + int32_t id = get_config_val(c, STOCKPILE_CONFIG_ID); + bool monitored = get_config_bool(c, STOCKPILE_CONFIG_MONITORED); + emplace_bulk_stockpile_config(L, id, monitored, stockpiles); } static void automelt_designate(color_ostream &out) { @@ -792,17 +788,15 @@ static int automelt_getItemCountsAndStockpileConfigs(lua_State *L) { int id = pile->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_configs(L, watched_stockpiles[id], stockpile_config_map); + emplace_bulk_stockpile_config(L, watched_stockpiles[id], stockpile_config_map); } 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); - DEBUG(cycle, *out).print("confmap_length: %d\n", stockpile_config_map.size()); DEBUG(perf, *out).print("exit automelt_getItemCountsAndStockpileConfigs\n"); diff --git a/plugins/lua/autochop.lua b/plugins/lua/autochop.lua index 6b8c9f943..e9bad2eef 100644 --- a/plugins/lua/autochop.lua +++ b/plugins/lua/autochop.lua @@ -96,14 +96,17 @@ function getTreeCountsAndBurrowConfigs() ret.summary = table.remove(data, 1) ret.tree_counts = table.remove(data, 1) ret.designated_tree_counts = table.remove(data, 1) - ret.burrow_configs = data - for _,c in ipairs(ret.burrow_configs) do + local unparsed_burrow_configs = table.remove(data, 1) + + ret.burrow_configs = {} + for idx,c in pairs(unparsed_burrow_configs) do c.name = df.burrow.find(c.id).name c.chop = c.chop ~= 0 c.clearcut = c.clearcut ~= 0 c.protect_brewable = c.protect_brewable ~= 0 c.protect_edible = c.protect_edible ~= 0 c.protect_cookable = c.protect_cookable ~= 0 + table.insert(ret.burrow_configs, c) end return ret end diff --git a/plugins/lua/automelt.lua b/plugins/lua/automelt.lua index 535c056e6..395934867 100644 --- a/plugins/lua/automelt.lua +++ b/plugins/lua/automelt.lua @@ -68,8 +68,6 @@ function getItemCountsAndStockpileConfigs() 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 c.name = "ERROR" c.monitored = false