11#include " ShipTextureReplacementDialogModel.h"
22
33#include " mission/object.h"
4-
5- // Sub-texture type suffixes, mirroring the strcat_s calls in modelread.cpp.
6- // Used both when detecting sub-texture slots in initSubTypes and when parsing
7- // new_texture strings on dialog reload.
8- static const SCP_string SUBTEXTURE_SUFFIXES [] = { " misc" , " shine" , " glow" , " normal" , " height" , " ao" , " reflect" };
4+ #include " model/model.h"
5+
6+ namespace {
7+ const SCP_vector<SCP_string>& get_replaceable_texture_types ()
8+ {
9+ static const SCP_vector<SCP_string> types = []() {
10+ SCP_vector<SCP_string> out;
11+ out.reserve (MODEL_TEXTURE_SUFFIXES .size ());
12+ for (const auto & suffix : MODEL_TEXTURE_SUFFIXES ) {
13+ out.emplace_back (suffix.second .substr (1 )); // strip leading '-'
14+ }
15+ return out;
16+ }();
17+ return types;
18+ }
19+
20+ bool is_known_subtexture_type (const SCP_string& type)
21+ {
22+ return std::any_of (get_replaceable_texture_types ().begin (),
23+ get_replaceable_texture_types ().end (),
24+ [&type](const SCP_string& knownType) { return lcase_equal (type, knownType); });
25+ }
26+ }
927
1028namespace fso {
1129 namespace fred {
@@ -122,7 +140,7 @@ namespace fso {
122140 // Only treat the suffix as a type if it's a known sub-texture type.
123141 // Texture names themselves can contain hyphens (e.g. "fighter01-01a"),
124142 // so we must not blindly strip the last segment.
125- for (const auto & kt : SUBTEXTURE_SUFFIXES ) {
143+ for (const auto & kt : get_replaceable_texture_types () ) {
126144 if (lcase_equal (possibleType, kt)) {
127145 type = possibleType;
128146 newText = newText.substr (0 , npos);
@@ -188,37 +206,12 @@ namespace fso {
188206 }
189207 void ShipTextureReplacementDialogModel::initSubTypes (polymodel* model, int MapNum)
190208 {
191- subTypesAvailable[MapNum].insert (std::pair<SCP_string, bool >(" misc" , false ));
192- subTypesAvailable[MapNum].insert (std::pair<SCP_string, bool >(" shine" , false ));
193- subTypesAvailable[MapNum].insert (std::pair<SCP_string, bool >(" glow" , false ));
194- subTypesAvailable[MapNum].insert (std::pair<SCP_string, bool >(" normal" , false ));
195- subTypesAvailable[MapNum].insert (std::pair<SCP_string, bool >(" height" , false ));
196- subTypesAvailable[MapNum].insert (std::pair<SCP_string, bool >(" ao" , false ));
197- subTypesAvailable[MapNum].insert (std::pair<SCP_string, bool >(" reflect" , false ));
198-
199- currentTextures[MapNum].insert (std::pair<SCP_string, SCP_string>(" misc" , " " ));
200- currentTextures[MapNum].insert (std::pair<SCP_string, SCP_string>(" shine" , " " ));
201- currentTextures[MapNum].insert (std::pair<SCP_string, SCP_string>(" glow" , " " ));
202- currentTextures[MapNum].insert (std::pair<SCP_string, SCP_string>(" normal" , " " ));
203- currentTextures[MapNum].insert (std::pair<SCP_string, SCP_string>(" height" , " " ));
204- currentTextures[MapNum].insert (std::pair<SCP_string, SCP_string>(" ao" , " " ));
205- currentTextures[MapNum].insert (std::pair<SCP_string, SCP_string>(" reflect" , " " ));
206-
207- replaceMap[MapNum].insert (std::pair<SCP_string, bool >(" misc" , false ));
208- replaceMap[MapNum].insert (std::pair<SCP_string, bool >(" shine" , false ));
209- replaceMap[MapNum].insert (std::pair<SCP_string, bool >(" glow" , false ));
210- replaceMap[MapNum].insert (std::pair<SCP_string, bool >(" normal" , false ));
211- replaceMap[MapNum].insert (std::pair<SCP_string, bool >(" height" , false ));
212- replaceMap[MapNum].insert (std::pair<SCP_string, bool >(" ao" , false ));
213- replaceMap[MapNum].insert (std::pair<SCP_string, bool >(" reflect" , false ));
214-
215- inheritMap[MapNum].insert (std::pair<SCP_string, bool >(" misc" , true ));
216- inheritMap[MapNum].insert (std::pair<SCP_string, bool >(" shine" , true ));
217- inheritMap[MapNum].insert (std::pair<SCP_string, bool >(" glow" , true ));
218- inheritMap[MapNum].insert (std::pair<SCP_string, bool >(" normal" , true ));
219- inheritMap[MapNum].insert (std::pair<SCP_string, bool >(" height" , true ));
220- inheritMap[MapNum].insert (std::pair<SCP_string, bool >(" ao" , true ));
221- inheritMap[MapNum].insert (std::pair<SCP_string, bool >(" reflect" , true ));
209+ for (const auto & type : get_replaceable_texture_types ()) {
210+ subTypesAvailable[MapNum].insert (std::pair<SCP_string, bool >(type, false ));
211+ currentTextures[MapNum].insert (std::pair<SCP_string, SCP_string>(type, " " ));
212+ replaceMap[MapNum].insert (std::pair<SCP_string, bool >(type, false ));
213+ inheritMap[MapNum].insert (std::pair<SCP_string, bool >(type, true ));
214+ }
222215 char subMap[MAX_FILENAME_LEN ];
223216 // init saftly, probly not necessary
224217 for (int j = 1 ; j < TM_NUM_TYPES ; j++) {
@@ -240,18 +233,12 @@ namespace fso {
240233 continue ;
241234 }
242235 if (!type.empty ()) {
243- if (type == " trans " ) {
236+ if (lcase_equal ( type, MODEL_TEXTURE_SUFFIX_TRANS . substr ( 1 )) ) {
244237 // transparency map, not a replaceable subtype
245238 } else {
246- bool known = false ;
247- for (const auto & kt : SUBTEXTURE_SUFFIXES ) {
248- if (lcase_equal (type, kt)) {
249- subTypesAvailable[MapNum][kt] = true ;
250- known = true ;
251- break ;
252- }
253- }
254- if (!known) {
239+ if (is_known_subtexture_type (type)) {
240+ subTypesAvailable[MapNum][type] = true ;
241+ } else {
255242 error_display (1 , " Invalid Map type %s. Check your model's texture names or get a programmer" , type.c_str ());
256243 }
257244 }
0 commit comments