From fb192bba0e13d2b77782f395fdc99593fe5ec5c0 Mon Sep 17 00:00:00 2001 From: Ted John Date: Tue, 19 Apr 2016 18:39:30 +0100 Subject: [PATCH] prevent adding duplicate footpath entries --- src/core/List.hpp | 23 +++++++++++++++++++++++ src/rct1/S4Importer.cpp | 27 +++++++++++++++++++-------- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/core/List.hpp b/src/core/List.hpp index 14cbb0aad6..013cfd23d8 100644 --- a/src/core/List.hpp +++ b/src/core/List.hpp @@ -111,4 +111,27 @@ public: } return SIZE_MAX; } + + size_t IndexOf(T item, std::function comparer) + { + for (size_t i = 0; i < this->size(); i++) + { + T element = std::vector::operator[](i); + if (comparer(item, element)) + { + return i; + } + } + return SIZE_MAX; + } + + bool Contains(std::function predicate) + { + return IndexOf(predicate) != SIZE_MAX; + } + + bool Contains(T item, std::function comparer) + { + return IndexOf(item, comparer) != SIZE_MAX; + } }; diff --git a/src/rct1/S4Importer.cpp b/src/rct1/S4Importer.cpp index 354f989b78..df4971724e 100644 --- a/src/rct1/S4Importer.cpp +++ b/src/rct1/S4Importer.cpp @@ -28,6 +28,11 @@ extern "C" #include "../world/scenery.h" } +static bool ObjectNameComparer(const char * a, const char * b) +{ + return String::Equals(a, b, true); +} + void S4Importer::LoadSavedGame(const utf8 * path) { if (!rct1_read_sv4(path, &_s4)) { @@ -106,7 +111,7 @@ void S4Importer::Initialise() void S4Importer::CreateAvailableObjectMappings() { - // Add defaults + // Add default scenery groups _sceneryGroupEntries.AddRange({ "SCGTREES", "SCGPATHX", @@ -116,6 +121,7 @@ void S4Importer::CreateAvailableObjectMappings() "SCGWALLS" }); + // Add default footpaths _pathEntries.AddRange({ "PATHASH ", "PATHCRZY", @@ -159,11 +165,7 @@ void S4Importer::CreateAvailableObjectMappings() break; } - size_t index = entries->IndexOf([objectName](const char * x) -> bool - { - return String::Equals(x, objectName, true); - }); - if (index == SIZE_MAX) + if (!entries->Contains(objectName, ObjectNameComparer)) { entries->Add(objectName); } @@ -358,8 +360,17 @@ void S4Importer::AddEntryForPath(uint8 pathType) if (_pathTypeToEntryMap[pathType] == 255) { const char * entryName = RCT1::GetPathObject(pathType); - _pathTypeToEntryMap[pathType] = (uint8)_pathEntries.GetCount(); - _pathEntries.Add(entryName); + + size_t index = _pathEntries.IndexOf(entryName, ObjectNameComparer); + if (index != SIZE_MAX) + { + _pathTypeToEntryMap[pathType] = (uint8)index; + } + else + { + _pathTypeToEntryMap[pathType] = (uint8)_pathEntries.GetCount(); + _pathEntries.Add(entryName); + } } }