Bugfixes, resolved CTD issue.

develop
eamondo2 2023-01-23 13:35:26 -05:00
parent 44d8ce2bdc
commit 6592cadc09
2 changed files with 51 additions and 42 deletions

@ -57,8 +57,10 @@ static const string CONFIG_KEY = string(plugin_name) + "/config";
static const string STOCKPILE_CONFIG_KEY_PREFIX = string(plugin_name) + "/stockpile/"; static const string STOCKPILE_CONFIG_KEY_PREFIX = string(plugin_name) + "/stockpile/";
static PersistentDataItem config; static PersistentDataItem config;
static vector<PersistentDataItem> watched_stockpiles; // static vector<PersistentDataItem> watched_stockpiles;
static unordered_map<int, size_t> watched_stockpiles_indices; // static unordered_map<int, size_t> watched_stockpiles_indices;
static unordered_map<int32_t, PersistentDataItem> watched_stockpiles;
enum StockpileConfigValues enum StockpileConfigValues
{ {
@ -92,31 +94,31 @@ static void set_config_bool(PersistentDataItem &c, int index, bool value)
static PersistentDataItem &ensure_stockpile_config(color_ostream &out, int id) static PersistentDataItem &ensure_stockpile_config(color_ostream &out, int id)
{ {
if (watched_stockpiles_indices.count(id)) DEBUG(cycle,out).print("ensuring stockpile config id=%d\n", id);
return watched_stockpiles[watched_stockpiles_indices[id]]; if (watched_stockpiles.count(id)){
DEBUG(cycle,out).print("stockpile exists in watched_indices\n");
return watched_stockpiles[id];
}
string keyname = STOCKPILE_CONFIG_KEY_PREFIX + int_to_string(id); string keyname = STOCKPILE_CONFIG_KEY_PREFIX + int_to_string(id);
DEBUG(status, out).print("creating new persistent key for stockpile %d\n", id); DEBUG(status,out).print("creating new persistent key for stockpile %d\n", id);
watched_stockpiles.emplace_back(World::GetPersistentData(keyname, NULL)); watched_stockpiles.emplace(id, World::GetPersistentData(keyname, NULL));
size_t idx = watched_stockpiles.size() - 1; return watched_stockpiles[id];
watched_stockpiles_indices.emplace(id, idx);
return watched_stockpiles[idx];
} }
static void remove_stockpile_config(color_ostream &out, int id) static void remove_stockpile_config(color_ostream &out, int id)
{ {
if (!watched_stockpiles_indices.count(id)) if (!watched_stockpiles.count(id))
return; return;
DEBUG(status, out).print("removing persistent key for stockpile %d\n", id); DEBUG(status, out).print("removing persistent key for stockpile %d\n", id);
size_t idx = watched_stockpiles_indices[id]; World::DeletePersistentData(watched_stockpiles[id]);
World::DeletePersistentData(watched_stockpiles[idx]); watched_stockpiles.erase(id);
watched_stockpiles.erase(watched_stockpiles.begin() + idx);
watched_stockpiles_indices.erase(id);
} }
static void validate_stockpile_configs(color_ostream &out) static void validate_stockpile_configs(color_ostream &out)
{ {
for (auto &c : watched_stockpiles) { for (auto &c : watched_stockpiles) {
int id = get_config_val(c, STOCKPILE_CONFIG_ID); int id = get_config_val(c.second, STOCKPILE_CONFIG_ID);
if (!df::building::find(id)){ if (!df::building::find(id)){
remove_stockpile_config(out, id); remove_stockpile_config(out, id);
} }
@ -174,13 +176,14 @@ DFhackCExport command_result plugin_load_data(color_ostream &out)
} }
DEBUG(status, out).print("loading persisted enabled state: %s\n", is_enabled ? "true" : "false"); DEBUG(status, out).print("loading persisted enabled state: %s\n", is_enabled ? "true" : "false");
World::GetPersistentData(&watched_stockpiles, STOCKPILE_CONFIG_KEY_PREFIX, true); vector<PersistentDataItem> loaded_persist_data;
watched_stockpiles_indices.clear(); World::GetPersistentData(&loaded_persist_data, STOCKPILE_CONFIG_KEY_PREFIX, true);
const size_t num_watched_stockpiles = watched_stockpiles.size(); watched_stockpiles.clear();
const size_t num_watched_stockpiles = loaded_persist_data.size();
for (size_t idx = 0; idx < num_watched_stockpiles; ++idx) for (size_t idx = 0; idx < num_watched_stockpiles; ++idx)
{ {
auto &c = watched_stockpiles[idx]; auto &c = loaded_persist_data[idx];
watched_stockpiles_indices.emplace(get_config_val(c, STOCKPILE_CONFIG_ID), idx); watched_stockpiles.emplace(get_config_val(c, STOCKPILE_CONFIG_ID), c);
} }
validate_stockpile_configs(out); validate_stockpile_configs(out);
@ -460,9 +463,9 @@ static int32_t scan_stockpiles(color_ostream &out, bool should_melt, map<int32_t
//Parse all the watched piles //Parse all the watched piles
for (auto &c : watched_stockpiles) { for (auto &c : watched_stockpiles) {
int id = get_config_val(c, STOCKPILE_CONFIG_ID); int id = get_config_val(c.second, STOCKPILE_CONFIG_ID);
//Check monitor status //Check monitor status
bool monitored = get_config_bool(c, STOCKPILE_CONFIG_MONITORED); bool monitored = get_config_bool(c.second, STOCKPILE_CONFIG_MONITORED);
if (!monitored) continue; if (!monitored) continue;
@ -472,7 +475,7 @@ static int32_t scan_stockpiles(color_ostream &out, bool should_melt, map<int32_t
int32_t premarked_count = 0; int32_t premarked_count = 0;
int32_t marked = mark_all_in_stockpile(out, c, premarked_count, item_count, tracked_item_map, should_melt); int32_t marked = mark_all_in_stockpile(out, c.second, premarked_count, item_count, tracked_item_map, should_melt);
DEBUG(perf,out).print("post mark_all_in_stockpile premarked_count=%d\n", premarked_count); DEBUG(perf,out).print("post mark_all_in_stockpile premarked_count=%d\n", premarked_count);
@ -521,7 +524,7 @@ static int32_t scan_count_all(color_ostream &out, bool should_melt, int32_t &mar
marked_item_count_global = scan_all_melt_designated(out, tracked_item_map_piles); marked_item_count_global = scan_all_melt_designated(out, tracked_item_map_piles);
for (auto &i : watched_stockpiles) { for (auto &i : watched_stockpiles) {
int id = get_config_val(i, STOCKPILE_CONFIG_ID); int id = get_config_val(i.second, STOCKPILE_CONFIG_ID);
total_items_all_piles+= item_count_piles[id]; total_items_all_piles+= item_count_piles[id];
marked_total_count_all_piles += premarked_item_count_piles[id]; marked_total_count_all_piles += premarked_item_count_piles[id];
} }
@ -573,8 +576,8 @@ static PersistentDataItem *getSelectedStockpileConfig(color_ostream &out) {
validate_stockpile_configs(out); validate_stockpile_configs(out);
PersistentDataItem *c = NULL; PersistentDataItem *c = NULL;
if (watched_stockpiles_indices.count(bldg_id)) { if (watched_stockpiles.count(bldg_id)) {
c = &(watched_stockpiles[watched_stockpiles_indices[bldg_id]]); c = &(watched_stockpiles[bldg_id]);
return c; return c;
} else { } else {
DEBUG(status,out).print("No existing config\n"); DEBUG(status,out).print("No existing config\n");
@ -641,8 +644,8 @@ static void automelt_printStatus(color_ostream &out) {
bool monitored = false; bool monitored = false;
int32_t item_count = 0; int32_t item_count = 0;
int32_t marked_item_count = 0; int32_t marked_item_count = 0;
if (watched_stockpiles_indices.count(stockpile->id)) { if (watched_stockpiles.count(stockpile->id)) {
auto &c = watched_stockpiles[watched_stockpiles_indices[stockpile->id]]; auto &c = watched_stockpiles[stockpile->id];
monitored = get_config_bool(c, STOCKPILE_CONFIG_MONITORED); monitored = get_config_bool(c, STOCKPILE_CONFIG_MONITORED);
int id = get_config_val(c, STOCKPILE_CONFIG_ID); int id = get_config_val(c, STOCKPILE_CONFIG_ID);
item_count = item_count_piles[id]; item_count = item_count_piles[id];
@ -666,11 +669,12 @@ static void automelt_printStatus(color_ostream &out) {
} }
static void automelt_setStockpileConfig(color_ostream &out, int id, bool monitored) { static void automelt_setStockpileConfig(color_ostream &out, int id, bool monitored) {
DEBUG(status,out).print("entering automelt_setStockpileConfig\n"); DEBUG(status,out).print("entering automelt_setStockpileConfig for id=%d and monitored=%d\n", id, monitored);
validate_stockpile_configs(out); validate_stockpile_configs(out);
bool isInvalidStockpile = !df::building::find(id); bool isInvalidStockpile = !df::building::find(id) || !isStockpile(df::building::find(id));
bool hasNoData = !monitored; bool hasNoData = !monitored;
if (isInvalidStockpile || hasNoData) { if (isInvalidStockpile || hasNoData) {
DEBUG(cycle,out).print("calling remove_stockpile_config with id=%d monitored=%d\n", id, monitored);
remove_stockpile_config(out, id); remove_stockpile_config(out, id);
return; return;
} }
@ -703,14 +707,12 @@ static int automelt_getStockpileConfig(lua_State *L) {
id = stockpile->id; id = stockpile->id;
found = true; found = true;
break; break;
} }
} }
if (!found) if (!found)
return 0; return 0;
} else { } else {
const char * name = lua_tostring(L, -1); const char * name = lua_tostring(L, -1);
if (!name) if (!name)
@ -732,14 +734,13 @@ static int automelt_getStockpileConfig(lua_State *L) {
} }
} }
} }
if (!found) if (!found)
return 0; return 0;
} }
if (watched_stockpiles_indices.count(id)) { if (watched_stockpiles.count(id)) {
push_stockpile_config(L, watched_stockpiles[watched_stockpiles_indices[id]]); push_stockpile_config(L, watched_stockpiles[id]);
} else { } else {
push_stockpile_config(L, id, false); push_stockpile_config(L, id, false);
} }
@ -801,8 +802,11 @@ static int automelt_getItemCountsAndStockpileConfigs(lua_State *L) {
bldg_count++; bldg_count++;
int id = pile->id; int id = pile->id;
if (watched_stockpiles_indices.count(id)) { DEBUG(cycle,*out).print("id=%d\ncount_res=%d\n", id, watched_stockpiles.count(id));
push_stockpile_config(L, watched_stockpiles[watched_stockpiles_indices[id]]);
if (watched_stockpiles.count(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]);
} else { } else {
push_stockpile_config(L, id, false); push_stockpile_config(L, id, false);
} }

@ -62,12 +62,17 @@ function getItemCountsAndStockpileConfigs()
ret.premarked_item_counts = table.remove(data, 1) ret.premarked_item_counts = table.remove(data, 1)
ret.stockpile_configs = data ret.stockpile_configs = data
for _,c in ipairs(ret.stockpile_configs) do for _,c in ipairs(ret.stockpile_configs) do
c.name = df.building.find(c.id).name if not c.id or c.id == -1 then
if not c.name or c.name == '' then c.name = "ERROR"
c.name = (fmt):format(tostring(df.building.find(c.id).stockpile_number)) c.monitored = false
c.name = c.name.gsub(c.name, '^%s*(.-)%s*$', '%1') else
c.name = df.building.find(c.id).name
if not c.name or c.name == '' then
c.name = (fmt):format(tostring(df.building.find(c.id).stockpile_number))
end
c.monitored = c.monitored ~= 0
end end
c.monitored = c.monitored ~= 0
end end
return ret return ret
end end