Skip to content

Commit 5066e49

Browse files
authored
Virtual POF fixes (#5984)
* Fix wrong filename being used * Fix replacement map not used for collision check * Fix poly count and incorrect submodel numbers
1 parent 2403f13 commit 5066e49

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

code/model/modelinterp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3294,7 +3294,7 @@ void bsp_polygon_data::replace_textures_used(const SCP_map<int, int>& replacemen
32943294
Num_verts[it->first] -= poly.Num_verts;
32953295
Num_verts[it->second] += poly.Num_verts;
32963296
--Num_polies[it->first];
3297-
++Num_polies[it->first];
3297+
++Num_polies[it->second];
32983298
}
32993299
}
33003300
}

code/model/modelreplace.cpp

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ bool read_virtual_model_file(polymodel* pm, const SCP_string& filename, model_pa
127127
const auto& virtual_pof = virtual_pof_it->second[depthLocal];
128128
depthLocal++;
129129

130-
read_model_file(pm, filename.c_str(), ferror, deferredTasks, depth);
130+
read_model_file(pm, virtual_pof.basePOF.c_str(), ferror, deferredTasks, depth);
131131

132132
for (const auto& operation : virtual_pof.operationList)
133133
operation->process(pm, deferredTasks, depth, virtual_pof);
@@ -262,30 +262,23 @@ int reallocate_and_copy_array_vmalloc(T*& array, int& size, size_t to_add) {
262262
return size_before;
263263
}
264264

265-
#define REPLACE_IF_EQ(data) if ((data) == source) (data) = dest;
266-
#define REPLACE_IF_STRIEQ(data) if (stricmp(data, source.c_str()) == 0) strncpy_s(data, dest.c_str(), dest.size());
267-
#define REPLACE_IF_SCPSTRIEQ(data) if (stricmp(data.c_str(), source.c_str()) == 0) data = dest;
265+
#define REPLACE_IF_EQ(data) { auto iter = replace.find(data); if (iter != replace.end()) (data) = iter->second; }
266+
#define REPLACE_IF_STRIEQ(data) { auto iter = replace.find(data); if (iter != replace.end()) strncpy_s(data, iter->second.c_str(), iter->second.size()); }
268267

269268
//Generates one function for replacing data in a type, which is a map entry of which the key may be replaced. Takes an rvalue reference, used for making a copy and modifying the temporary to then assign it somewhere
270269
#define CHANGE_HELPER_MAP_KEY(name, intype, argtype) template<typename map_t> static typename std::enable_if<std::is_same<typename map_t::value_type, std::pair<const argtype, argtype>>::value, intype>::type name(intype&& pass, map_t replace){ \
271270
const auto it = replace.find(pass.first); \
272-
intype input = { (it == replace.end() ? pass.first : it->second), pass.second }; \
273-
for (const auto& replacement : replace) { \
274-
const argtype& source = replacement.first; \
275-
const argtype& dest = replacement.second;
276-
#define CHANGE_HELPER_MAP_KEY_END } return input; }
271+
intype input = { (it == replace.end() ? pass.first : it->second), pass.second };
272+
#define CHANGE_HELPER_MAP_KEY_END return input; }
277273

278274
//Generates two functions for replacing data in a type. One that takes an rvalue reference, used for making a copy and modifying the temporary to then assign it somewhere, and one which takes an lvalue reference for modifying in-place
279275
#define CHANGE_HELPER(name, intype, argtype) template<typename map_t> static typename std::enable_if<std::is_same<typename map_t::value_type, std::pair<const argtype, argtype>>::value>::type name(intype& input, map_t replace); \
280276
template<typename map_t> inline static typename std::enable_if<std::is_same<typename map_t::value_type, std::pair<const argtype, argtype>>::value, intype>::type name(intype&& input, map_t replace){ \
281277
name<map_t>(input, replace); \
282278
return std::move(input); \
283279
} \
284-
template<typename map_t> static typename std::enable_if<std::is_same<typename map_t::value_type, std::pair<const argtype, argtype>>::value>::type name(intype& input, map_t replace) {\
285-
for (const auto& replacement : replace) { \
286-
const argtype& source = replacement.first; \
287-
const argtype& dest = replacement.second;
288-
#define CHANGE_HELPER_END }}
280+
template<typename map_t> static typename std::enable_if<std::is_same<typename map_t::value_type, std::pair<const argtype, argtype>>::value>::type name(intype& input, map_t replace) {
281+
#define CHANGE_HELPER_END }
289282

290283
//Change submodel numbers in a submodel
291284
CHANGE_HELPER(change_submodel_numbers, bsp_info, int)
@@ -324,19 +317,15 @@ CHANGE_HELPER_END
324317

325318
//Change submodel name in a subsystem definition
326319
CHANGE_HELPER_MAP_KEY(change_submodel_name, model_read_deferred_tasks::model_subsystem_pair, SCP_string)
327-
SCP_UNUSED(source);
328-
SCP_UNUSED(dest);
329320
CHANGE_HELPER_MAP_KEY_END
330321

331322
//Change subsystem name in an engine subsys definition
332323
CHANGE_HELPER(change_submodel_name, model_read_deferred_tasks::engine_subsystem_pair, SCP_string)
333-
REPLACE_IF_SCPSTRIEQ(input.second.subsystem_name)
324+
REPLACE_IF_EQ(input.second.subsystem_name)
334325
CHANGE_HELPER_END
335326

336327
//Change engine numbers in an engine subsys definition
337328
CHANGE_HELPER_MAP_KEY(change_engine_numbers, model_read_deferred_tasks::engine_subsystem_pair, int)
338-
SCP_UNUSED(source);
339-
SCP_UNUSED(dest);
340329
CHANGE_HELPER_MAP_KEY_END
341330

342331

@@ -392,15 +381,17 @@ void VirtualPOFOperationAddSubmodel::process(polymodel* pm, model_read_deferred_
392381
SCP_vector<int> to_copy_submodels;
393382
bool has_name_collision = false;
394383
if (copyChildren) {
395-
model_iterate_submodel_tree(appendingPM, src_subobj_no, [&to_copy_submodels, &has_name_collision, pm, appendingPM](int submodel, int /*level*/, bool /*isLeaf*/) {
384+
model_iterate_submodel_tree(appendingPM, src_subobj_no, [&to_copy_submodels, &has_name_collision, &renameMap, pm, appendingPM](int submodel, int /*level*/, bool /*isLeaf*/) {
396385
to_copy_submodels.emplace_back(submodel);
397-
if(model_find_submodel_index(pm, appendingPM->submodel[submodel].name) != -1)
386+
auto replace_with_name_it = renameMap.find(appendingPM->submodel[submodel].name);
387+
if(model_find_submodel_index(pm, replace_with_name_it == renameMap.end() ? appendingPM->submodel[submodel].name : replace_with_name_it->second.c_str()) != -1)
398388
has_name_collision = true;
399389
});
400390
}
401391
else {
402392
to_copy_submodels.emplace_back(src_subobj_no);
403-
if (model_find_submodel_index(pm, appendingPM->submodel[src_subobj_no].name) != -1)
393+
auto replace_with_name_it = renameMap.find(appendingPM->submodel[src_subobj_no].name);
394+
if(model_find_submodel_index(pm, replace_with_name_it == renameMap.end() ? appendingPM->submodel[src_subobj_no].name : replace_with_name_it->second.c_str()) != -1)
404395
has_name_collision = true;
405396
}
406397

@@ -650,7 +641,7 @@ void VirtualPOFOperationAddEngine::process(polymodel* pm, model_read_deferred_ta
650641

651642
if (subsystemName) {
652643
if (renameSubsystem) {
653-
const SCP_unordered_map<SCP_string, SCP_string> thrusterSubsysRenameMap{ {*subsystemName, *renameSubsystem} };
644+
const VirtualPOFNameReplacementMap thrusterSubsysRenameMap{ {*subsystemName, *renameSubsystem} };
654645
deferredTasks.engine_subsystems.emplace(change_submodel_name(
655646
change_engine_numbers(model_read_deferred_tasks::engine_subsystem_pair(*it), thrusterReplacementMap),
656647
thrusterSubsysRenameMap));

0 commit comments

Comments
 (0)