From 0c9f1e0af498678656e759725c25ee44ec17a31d Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Sat, 20 Oct 2012 21:01:22 +0400 Subject: [PATCH] Check the item type against job_item_vector_id when matching to jobs. This will prevent setting an invalid item type via the job command. --- library/include/modules/Items.h | 4 +++- library/modules/Items.cpp | 26 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/library/include/modules/Items.h b/library/include/modules/Items.h index 92329980a..f9871669a 100644 --- a/library/include/modules/Items.h +++ b/library/include/modules/Items.h @@ -40,6 +40,7 @@ distribution. #include "df/building_actual.h" #include "df/body_part_raw.h" #include "df/unit_inventory_item.h" +#include "df/job_item_vector_id.h" namespace df { @@ -86,7 +87,8 @@ namespace DFHack bool find(const std::string &token); - bool matches(const df::job_item &item, MaterialInfo *mat = NULL); + bool matches(df::job_item_vector_id vec_id); + bool matches(const df::job_item &item, MaterialInfo *mat = NULL, bool skip_vector = false); }; inline bool operator== (const ItemTypeInfo &a, const ItemTypeInfo &b) { diff --git a/library/modules/Items.cpp b/library/modules/Items.cpp index 7af8c549b..0a78c727f 100644 --- a/library/modules/Items.cpp +++ b/library/modules/Items.cpp @@ -245,7 +245,28 @@ bool Items::isCasteMaterial(df::item_type itype) return ENUM_ATTR(item_type, is_caste_mat, itype); } -bool ItemTypeInfo::matches(const df::job_item &item, MaterialInfo *mat) +bool ItemTypeInfo::matches(df::job_item_vector_id vec_id) +{ + auto other_id = ENUM_ATTR(job_item_vector_id, other, vec_id); + + auto explicit_item = ENUM_ATTR(items_other_id, item, other_id); + if (explicit_item != item_type::NONE && type != explicit_item) + return false; + + auto generic_item = ENUM_ATTR(items_other_id, generic_item, other_id); + if (generic_item.size > 0) + { + for (size_t i = 0; i < generic_item.size; i++) + if (generic_item.items[i] == type) + return true; + + return false; + } + + return true; +} + +bool ItemTypeInfo::matches(const df::job_item &item, MaterialInfo *mat, bool skip_vector) { using namespace df::enums::item_type; @@ -255,6 +276,9 @@ bool ItemTypeInfo::matches(const df::job_item &item, MaterialInfo *mat) if (Items::isCasteMaterial(type) && mat && !mat->isNone()) return false; + if (!skip_vector && !matches(item.vector_id)) + return false; + df::job_item_flags1 ok1, mask1, item_ok1, item_mask1; df::job_item_flags2 ok2, mask2, item_ok2, item_mask2, xmask2; df::job_item_flags3 ok3, mask3, item_ok3, item_mask3;