Skip to content

Commit a320f66

Browse files
authored
Merge pull request #5377 from myk002/myk_furniture
[stockpiles] fix conversion of enum token to value
2 parents 12c0763 + 6d9cfda commit a320f66

File tree

2 files changed

+10
-21
lines changed

2 files changed

+10
-21
lines changed

docs/changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Template for new versions:
5656
## New Features
5757

5858
## Fixes
59+
- `stockpiles`: fix one-off error in item type when importing furniture stockpile settings
5960

6061
## Misc Improvements
6162
- `spectate`: show dwarves' activities (like prayer)

plugins/stockpiles/StockpileSerializer.cpp

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -210,24 +210,15 @@ bool StockpileSettingsSerializer::unserialize_from_file(color_ostream &out, cons
210210

211211
/**
212212
* Find an enum's value based off the string label.
213-
* @param traits the enum's trait struct
214213
* @param token the string value in key_table
215-
* @return the enum's value, -1 if not found
214+
* @return the enum's value, -1 if not found
216215
*/
217216
template <typename E>
218-
static typename df::enum_traits<E>::base_type linear_index(df::enum_traits<E> traits, const string& token) {
219-
auto j = traits.first_item_value;
220-
auto limit = traits.last_item_value;
221-
// sometimes enums start at -1, which is bad news for array indexing
222-
if (j < 0) {
223-
j += abs(traits.first_item_value);
224-
limit += abs(traits.first_item_value);
225-
}
226-
for (; j <= limit; ++j) {
227-
if (token.compare(traits.key_table[j]) == 0)
228-
return j;
229-
}
230-
return -1;
217+
static typename df::enum_traits<E>::base_type token_to_enum_val(const string& token) {
218+
E val;
219+
if (!find_enum_item(&val, token))
220+
return -1;
221+
return val;
231222
}
232223

233224
static bool matches_filter(color_ostream& out, const vector<string>& filters, const string& name) {
@@ -360,10 +351,9 @@ static void unserialize_list_quality(color_ostream& out, const char* subcat, boo
360351
}
361352

362353
using df::enums::item_quality::item_quality;
363-
df::enum_traits<item_quality> quality_traits;
364354
for (int i = 0; i < list_size; ++i) {
365355
const string quality = read_value(i);
366-
df::enum_traits<item_quality>::base_type idx = linear_index(quality_traits, quality);
356+
df::enum_traits<item_quality>::base_type idx = token_to_enum_val<item_quality>(quality);
367357
if (idx < 0) {
368358
WARN(log, out).print("invalid quality token: %s\n", quality.c_str());
369359
continue;
@@ -551,11 +541,9 @@ static void unserialize_list_item_type(color_ostream& out, const char* subcat, b
551541
}
552542

553543
using df::enums::item_type::item_type;
554-
df::enum_traits<item_type> type_traits;
555544
for (int i = 0; i < list_size; ++i) {
556545
const string token = read_value(i);
557-
// subtract one because item_type starts at -1
558-
const df::enum_traits<item_type>::base_type idx = linear_index(type_traits, token) - 1;
546+
const df::enum_traits<item_type>::base_type idx = token_to_enum_val<item_type>(token);
559547
if (!is_allowed((item_type)idx))
560548
continue;
561549
if (idx < 0 || size_t(idx) >= num_elems) {
@@ -1806,7 +1794,7 @@ void StockpileSettingsSerializer::read_furniture(color_ostream& out, Deserialize
18061794
} else {
18071795
for (int i = 0; i < bfurniture.type_size(); ++i) {
18081796
const string token = bfurniture.type(i);
1809-
df::enum_traits<furniture_type>::base_type idx = linear_index(type_traits, token);
1797+
df::enum_traits<furniture_type>::base_type idx = token_to_enum_val<furniture_type>(token);
18101798
if (idx < 0 || size_t(idx) >= pfurniture.type.size()) {
18111799
WARN(log, out).print("furniture type index invalid %s, idx=%d\n", token.c_str(), idx);
18121800
continue;

0 commit comments

Comments
 (0)