From 67d51c35846e336721ba9e69ae0cb4e411047acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Thu, 10 Aug 2023 14:38:35 +0300 Subject: [PATCH 1/5] Use u8string for ScenarioIndexEntry Path --- src/openrct2/scenario/ScenarioRepository.cpp | 12 ++++++------ src/openrct2/scenario/ScenarioRepository.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/openrct2/scenario/ScenarioRepository.cpp b/src/openrct2/scenario/ScenarioRepository.cpp index 88be26c914..b563c3ede3 100644 --- a/src/openrct2/scenario/ScenarioRepository.cpp +++ b/src/openrct2/scenario/ScenarioRepository.cpp @@ -204,10 +204,10 @@ private: { auto& objRepository = OpenRCT2::GetContext()->GetObjectRepository(); auto importer = ParkImporter::CreateParkFile(objRepository); - importer->LoadScenario(path.c_str(), true); + importer->LoadScenario(path, true); if (importer->GetDetails(entry)) { - String::Set(entry->Path, sizeof(entry->Path), path.c_str()); + entry->Path = path; entry->Timestamp = timestamp; result = true; } @@ -225,10 +225,10 @@ private: try { auto s4Importer = ParkImporter::CreateS4(); - s4Importer->LoadScenario(path.c_str(), true); + s4Importer->LoadScenario(path, true); if (s4Importer->GetDetails(entry)) { - String::Set(entry->Path, sizeof(entry->Path), path.c_str()); + entry->Path = path; entry->Timestamp = timestamp; result = true; } @@ -273,7 +273,7 @@ private: ScenarioIndexEntry entry = {}; // Set new entry - String::Set(entry.Path, sizeof(entry.Path), path.c_str()); + entry.Path = path; entry.Timestamp = timestamp; entry.Category = s6Info->Category; entry.ObjectiveType = s6Info->ObjectiveType; @@ -562,7 +562,7 @@ private: if (existingEntry->Timestamp > entry.Timestamp) { // Existing entry is more recent - conflictPath = String::ToStd(existingEntry->Path); + conflictPath = existingEntry->Path; // Overwrite existing entry with this one *existingEntry = entry; diff --git a/src/openrct2/scenario/ScenarioRepository.h b/src/openrct2/scenario/ScenarioRepository.h index f887147ee0..ea309cef2a 100644 --- a/src/openrct2/scenario/ScenarioRepository.h +++ b/src/openrct2/scenario/ScenarioRepository.h @@ -42,7 +42,7 @@ enum class ScenarioSource : uint8_t struct ScenarioIndexEntry { - utf8 Path[MAX_PATH]; + u8string Path; uint64_t Timestamp; // Category / sequence From 39458a3df3e7f0504609baf4adfa2292a6cbc8aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Thu, 10 Aug 2023 14:39:38 +0300 Subject: [PATCH 2/5] Refactor ShortenPath --- src/openrct2/drawing/Drawing.String.cpp | 47 ++++++++----------------- src/openrct2/drawing/Drawing.h | 2 +- 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/src/openrct2/drawing/Drawing.String.cpp b/src/openrct2/drawing/Drawing.String.cpp index 971dec3907..45ad5dab29 100644 --- a/src/openrct2/drawing/Drawing.String.cpp +++ b/src/openrct2/drawing/Drawing.String.cpp @@ -1075,45 +1075,28 @@ void GfxDrawStringWithYOffsets( dpi.lastStringPos = { info.x, info.y }; } -void ShortenPath(utf8* buffer, size_t bufferSize, const utf8* path, int32_t availableWidth, FontStyle fontStyle) +u8string ShortenPath(const u8string& path, int32_t availableWidth, FontStyle fontStyle) { - size_t length = strlen(path); - - // Return full string if it fits - if (GfxGetStringWidth(const_cast(path), fontStyle) <= availableWidth) + if (GfxGetStringWidth(path, fontStyle) <= availableWidth) { - SafeStrCpy(buffer, path, bufferSize); - return; + return path; } - // Count path separators - int32_t path_separators = 0; - for (size_t x = 0; x < length; x++) + u8string shortenedPath = u8"..."; + + size_t begin = 0; + while (begin < path.size()) { - if (path[x] == *PATH_SEPARATOR || path[x] == '/') + begin = path.find_first_of(*PATH_SEPARATOR, begin + 1); + if (begin == path.npos) + break; + + shortenedPath = u8"..." + path.substr(begin); + if (GfxGetStringWidth(shortenedPath, fontStyle) <= availableWidth) { - path_separators++; + return shortenedPath; } } - // TODO: Replace with unicode ellipsis when supported - SafeStrCpy(buffer, "...", bufferSize); - - // Abbreviate beginning with xth separator - int32_t begin = -1; - for (int32_t x = 0; x < path_separators; x++) - { - do - { - begin++; - } while (path[begin] != *PATH_SEPARATOR && path[begin] != '/'); - - SafeStrCpy(buffer + 3, path + begin, bufferSize - 3); - if (GfxGetStringWidth(buffer, fontStyle) <= availableWidth) - { - return; - } - } - - SafeStrCpy(buffer, path, bufferSize); + return shortenedPath; } diff --git a/src/openrct2/drawing/Drawing.h b/src/openrct2/drawing/Drawing.h index 35da0cfd23..de2196b1d2 100644 --- a/src/openrct2/drawing/Drawing.h +++ b/src/openrct2/drawing/Drawing.h @@ -572,7 +572,7 @@ int32_t GfxGetStringWidthNewLined(std::string_view text, FontStyle fontStyle); int32_t GfxGetStringWidthNoFormatting(std::string_view text, FontStyle fontStyle); int32_t StringGetHeightRaw(std::string_view text, FontStyle fontStyle); int32_t GfxClipString(char* buffer, int32_t width, FontStyle fontStyle); -void ShortenPath(utf8* buffer, size_t bufferSize, const utf8* path, int32_t availableWidth, FontStyle fontStyle); +u8string ShortenPath(const u8string& path, int32_t availableWidth, FontStyle fontStyle); void TTFDrawString( DrawPixelInfo& dpi, const_utf8string text, int32_t colour, const ScreenCoordsXY& coords, bool noFormatting, FontStyle fontStyle, TextDarkness darkness); From 3ff527fb78c5869f22eed4d070681c94f8de1d1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Thu, 10 Aug 2023 14:39:47 +0300 Subject: [PATCH 3/5] Refactor uses of ShortenPath --- src/openrct2-ui/windows/LoadSave.cpp | 15 +++++---------- src/openrct2-ui/windows/ScenarioSelect.cpp | 7 ++----- src/openrct2-ui/windows/TrackList.cpp | 6 ++---- 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/openrct2-ui/windows/LoadSave.cpp b/src/openrct2-ui/windows/LoadSave.cpp index ac49ba3b02..6ab34cd7f3 100644 --- a/src/openrct2-ui/windows/LoadSave.cpp +++ b/src/openrct2-ui/windows/LoadSave.cpp @@ -106,7 +106,6 @@ static TrackDesign* _trackDesign; static std::vector _listItems; static char _directory[MAX_PATH]; -static char _shortenedDirectory[MAX_PATH]; static char _parentDirectory[MAX_PATH]; static u8string _extensionPattern; static u8string _defaultPath; @@ -527,7 +526,6 @@ public: SafeStrCpy(_directory, absoluteDirectory.c_str(), std::size(_directory)); // Note: This compares the pointers, not values _extensionPattern = extensionPattern; - _shortenedDirectory[0] = '\0'; _listItems.clear(); @@ -740,18 +738,15 @@ public: { DrawWidgets(dpi); - if (_shortenedDirectory[0] == '\0') - { - ShortenPath(_shortenedDirectory, sizeof(_shortenedDirectory), _directory, width - 8, FontStyle::Medium); - } + const auto shortPath = ShortenPath(_directory, width - 8, FontStyle::Medium); // Format text - thread_local std::string _buffer; - _buffer.assign("{BLACK}"); - _buffer += _shortenedDirectory; + std::string buffer; + buffer.assign("{BLACK}"); + buffer += shortPath; // Draw path text - const auto normalisedPath = Platform::StrDecompToPrecomp(_buffer.data()); + const auto normalisedPath = Platform::StrDecompToPrecomp(buffer.data()); const auto* normalisedPathC = normalisedPath.c_str(); auto ft = Formatter(); ft.Add(normalisedPathC); diff --git a/src/openrct2-ui/windows/ScenarioSelect.cpp b/src/openrct2-ui/windows/ScenarioSelect.cpp index 884a7bf5f2..17be7c2c3e 100644 --- a/src/openrct2-ui/windows/ScenarioSelect.cpp +++ b/src/openrct2-ui/windows/ScenarioSelect.cpp @@ -218,13 +218,10 @@ public: // Scenario path if (gConfigGeneral.DebuggingTools) { - utf8 path[MAX_PATH]; + const auto shortPath = ShortenPath(scenario->Path, width - 6 - TabWidth, FontStyle::Medium); - ShortenPath(path, sizeof(path), scenario->Path, width - 6 - TabWidth, FontStyle::Medium); - - const utf8* pathPtr = path; auto ft = Formatter(); - ft.Add(pathPtr); + ft.Add(shortPath.c_str()); DrawTextBasic(dpi, windowPos + ScreenCoordsXY{ TabWidth + 3, height - 3 - 11 }, STR_STRING, ft, { colours[1] }); } diff --git a/src/openrct2-ui/windows/TrackList.cpp b/src/openrct2-ui/windows/TrackList.cpp index 7c0cf9ae90..84fdaac1cd 100644 --- a/src/openrct2-ui/windows/TrackList.cpp +++ b/src/openrct2-ui/windows/TrackList.cpp @@ -470,11 +470,9 @@ public: // Show track file path (in debug mode) if (gConfigGeneral.DebuggingTools) { - utf8 pathBuffer[MAX_PATH]; - const utf8* pathPtr = pathBuffer; - ShortenPath(pathBuffer, sizeof(pathBuffer), path.c_str(), width, FontStyle::Medium); + const auto shortPath = ShortenPath(path, width, FontStyle::Medium); auto ft = Formatter(); - ft.Add(pathPtr); + ft.Add(shortPath.c_str()); DrawTextBasic( dpi, windowPos + ScreenCoordsXY{ 0, height - DEBUG_PATH_HEIGHT - 3 }, STR_STRING, ft, { colours[1] }); // TODO Check dpi From bdb3743897a62644325e14b24b6d7bfac2133486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Thu, 10 Aug 2023 14:45:07 +0300 Subject: [PATCH 4/5] Remove duplicate MAX_PATH, previously defined in Platform.h --- src/openrct2/common.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/openrct2/common.h b/src/openrct2/common.h index 74a5eccd37..ab373807eb 100644 --- a/src/openrct2/common.h +++ b/src/openrct2/common.h @@ -26,12 +26,6 @@ #include #include -// Define MAX_PATH for various headers that don't want to include system headers -// just for MAX_PATH -#ifndef MAX_PATH -# define MAX_PATH 260 -#endif - using colour_t = uint8_t; // Gets the name of a symbol as a C string From 97c99df341d5e4c5a97849a6efce7bc5794fb9cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Thu, 10 Aug 2023 14:45:27 +0300 Subject: [PATCH 5/5] Limit text input by TEXT_INPUT_SIZE and not MAX_PATH --- src/openrct2-ui/windows/NewRide.cpp | 2 +- src/openrct2-ui/windows/Scenery.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openrct2-ui/windows/NewRide.cpp b/src/openrct2-ui/windows/NewRide.cpp index 5a21a3cdb8..268531dcc8 100644 --- a/src/openrct2-ui/windows/NewRide.cpp +++ b/src/openrct2-ui/windows/NewRide.cpp @@ -367,7 +367,7 @@ public: SetPage(_currentTab); break; case WIDX_FILTER_TEXT_BOX: - WindowStartTextbox(*this, widgetIndex, STR_STRING, _filter.data(), MAX_PATH); + WindowStartTextbox(*this, widgetIndex, STR_STRING, _filter.data(), TEXT_INPUT_SIZE); break; case WIDX_FILTER_CLEAR_BUTTON: _filter.clear(); diff --git a/src/openrct2-ui/windows/Scenery.cpp b/src/openrct2-ui/windows/Scenery.cpp index f7b246a7ef..9fc7fc510f 100644 --- a/src/openrct2-ui/windows/Scenery.cpp +++ b/src/openrct2-ui/windows/Scenery.cpp @@ -271,7 +271,7 @@ public: Invalidate(); break; case WIDX_FILTER_TEXT_BOX: - WindowStartTextbox(*this, widgetIndex, STR_STRING, _filteredSceneryTab.Filter.data(), MAX_PATH); + WindowStartTextbox(*this, widgetIndex, STR_STRING, _filteredSceneryTab.Filter.data(), TEXT_INPUT_SIZE); break; case WIDX_FILTER_CLEAR_BUTTON: _tabEntries[_activeTabIndex].Filter.clear();