diff --git a/src/openttd.cpp b/src/openttd.cpp index 792cd110ab..da2a98e2de 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1863,6 +1863,14 @@ void CheckCaches(bool force_check, std::function log, CheckC if (tv->Next()) assert_msg(tv->Next()->Prev() == tv, "%u", tv->index); } + { + extern std::string ValidateTemplateReplacementCaches(); + std::string template_validation_result = ValidateTemplateReplacementCaches(); + if (!template_validation_result.empty()) { + CCLOG("Template replacement cache validation failed: %s", template_validation_result.c_str()); + } + } + if (!TraceRestrictSlot::ValidateVehicleIndex()) CCLOG("Trace restrict slot vehicle index validation failed"); TraceRestrictSlot::ValidateSlotOccupants(log); diff --git a/src/tbtr_template_vehicle.cpp b/src/tbtr_template_vehicle.cpp index 98fa7069ea..2310594d1b 100644 --- a/src/tbtr_template_vehicle.cpp +++ b/src/tbtr_template_vehicle.cpp @@ -35,6 +35,8 @@ #include "3rdparty/robin_hood/robin_hood.h" +#include + #include "safeguards.h" TemplatePool _template_pool("TemplatePool"); @@ -330,3 +332,21 @@ void ReindexTemplateReplacementsRecursive() } } } + +std::string ValidateTemplateReplacementCaches() +{ + robin_hood::unordered_flat_map saved_template_replacement_index = std::move(_template_replacement_index); + robin_hood::unordered_flat_map saved_template_replacement_index_recursive = std::move(_template_replacement_index_recursive); + + ReindexTemplateReplacements(); + + bool match = (saved_template_replacement_index == _template_replacement_index); + bool match_recursive = (saved_template_replacement_index_recursive == _template_replacement_index_recursive); + _template_replacement_index = std::move(saved_template_replacement_index); + _template_replacement_index_recursive = std::move(saved_template_replacement_index_recursive); + + if (!match) return "Index cache does not match"; + if (!match_recursive) return "Recursive index cache does not match"; + + return ""; +}