From 49b414a40e8ab7c1b46d181cceb0857a1ca06f06 Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Sat, 26 Feb 2022 17:20:07 +0100 Subject: [PATCH 1/5] Replace calls to path_get_filename() --- .../windows/EditorObjectSelection.cpp | 7 ++++--- src/openrct2-ui/windows/LoadSave.cpp | 7 ++----- src/openrct2-ui/windows/TitleCommandEditor.cpp | 3 ++- src/openrct2-ui/windows/TitleEditor.cpp | 16 ++++++++-------- src/openrct2/Game.cpp | 6 ++---- src/openrct2/interface/Screenshot.cpp | 4 +++- src/openrct2/rct1/S4Importer.cpp | 2 +- src/openrct2/scenario/ScenarioRepository.cpp | 2 +- src/openrct2/scenario/ScenarioRepository.h | 2 +- src/openrct2/util/Util.cpp | 2 +- src/openrct2/util/Util.h | 1 - 11 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/openrct2-ui/windows/EditorObjectSelection.cpp b/src/openrct2-ui/windows/EditorObjectSelection.cpp index a766157493..f78617e643 100644 --- a/src/openrct2-ui/windows/EditorObjectSelection.cpp +++ b/src/openrct2-ui/windows/EditorObjectSelection.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -1236,12 +1237,12 @@ private: DrawTextBasic(dpi, screenPos, stringId, {}, { COLOUR_WHITE, TextAlignment::RIGHT }); screenPos.y += LIST_ROW_HEIGHT; - // Draw object dat name + // Draw object filename { - const char* path = path_get_filename(listItem->repositoryItem->Path.c_str()); + auto path = Path::GetFileName(listItem->repositoryItem->Path); auto ft = Formatter(); ft.Add(STR_STRING); - ft.Add(path); + ft.Add(path.c_str()); DrawTextBasic( dpi, { windowPos.x + this->width - 5, screenPos.y }, STR_WINDOW_COLOUR_2_STRINGID, ft, { COLOUR_BLACK, TextAlignment::RIGHT }); diff --git a/src/openrct2-ui/windows/LoadSave.cpp b/src/openrct2-ui/windows/LoadSave.cpp index ebcd19d187..659939f68e 100644 --- a/src/openrct2-ui/windows/LoadSave.cpp +++ b/src/openrct2-ui/windows/LoadSave.cpp @@ -966,15 +966,12 @@ static void SetAndSaveConfigPath(u8string& config_str, u8string_view path) static bool IsValidPath(const char* path) { - char filename[MAX_PATH]; - safe_strcpy(filename, path_get_filename(path), sizeof(filename)); - // HACK This is needed because tracks get passed through with td? // I am sure this will change eventually to use the new FileScanner // which handles multiple patterns - path_remove_extension(filename); + auto filename = Path::GetFileNameWithoutExtension(path); - return filename_valid_characters(filename); + return filename_valid_characters(filename.c_str()); } static void WindowLoadsaveSelect(rct_window* w, const char* path) diff --git a/src/openrct2-ui/windows/TitleCommandEditor.cpp b/src/openrct2-ui/windows/TitleCommandEditor.cpp index e58f588862..2d8e223bf9 100644 --- a/src/openrct2-ui/windows/TitleCommandEditor.cpp +++ b/src/openrct2-ui/windows/TitleCommandEditor.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -143,7 +144,7 @@ static void ScenarioSelectCallback(const utf8* path) { if (_command.Type == TitleScript::LoadSc) { - const utf8* fileName = path_get_filename(path); + const auto fileName = Path::GetFileName(path); auto scenario = GetScenarioRepository()->GetByFilename(fileName); safe_strcpy(_command.Scenario, scenario->internal_name, sizeof(_command.Scenario)); } diff --git a/src/openrct2-ui/windows/TitleEditor.cpp b/src/openrct2-ui/windows/TitleEditor.cpp index e5dcdd1f5c..53a76b5aa0 100644 --- a/src/openrct2-ui/windows/TitleEditor.cpp +++ b/src/openrct2-ui/windows/TitleEditor.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -182,7 +183,7 @@ static bool _isSequenceReadOnly; static std::unique_ptr _editingTitleSequence; static const utf8* _sequenceName; -static utf8* _renameSavePath = nullptr; +static u8string _renameSavePath = u8string(); static int16_t _window_title_editor_highlighted_index; @@ -247,7 +248,7 @@ static void WindowTitleEditorClose(rct_window* w) _editingTitleSequence = nullptr; _sequenceName = nullptr; - SafeFree(_renameSavePath); + _renameSavePath.clear(); } static void WindowTitleEditorMouseup(rct_window* w, rct_widgetindex widgetIndex) @@ -1120,19 +1121,18 @@ static void WindowTitleEditorAddParkCallback(int32_t result, const utf8* path) if (extension != FileExtension::SV4 && extension != FileExtension::SV6 && extension != FileExtension::PARK) return; - const utf8* filename = path_get_filename(path); - if (SaveFilenameExists(filename)) + const auto filename = Path::GetFileName(path); + if (SaveFilenameExists(filename.c_str())) { - free(_renameSavePath); - _renameSavePath = _strdup(filename); + _renameSavePath = filename; rct_window* w = window_find_by_class(WC_TITLE_EDITOR); WindowTextInputOpen( w, WIDX_TITLE_EDITOR_RENAME_SAVE, STR_FILEBROWSER_RENAME_SAVE_TITLE, STR_ERROR_EXISTING_NAME, {}, STR_STRING, - reinterpret_cast(_renameSavePath), 52 - 1); + reinterpret_cast(_renameSavePath.c_str()), 52 - 1); return; } - TitleSequenceAddPark(*_editingTitleSequence, path, filename); + TitleSequenceAddPark(*_editingTitleSequence, path, filename.c_str()); } static void WindowTitleEditorRenamePark(size_t index, const utf8* name) diff --git a/src/openrct2/Game.cpp b/src/openrct2/Game.cpp index 5880f2aae1..0b9098bdd5 100644 --- a/src/openrct2/Game.cpp +++ b/src/openrct2/Game.cpp @@ -578,13 +578,11 @@ void save_game_with_name(u8string_view name) void* create_save_game_as_intent() { - char name[MAX_PATH]; - safe_strcpy(name, path_get_filename(gScenarioSavePath.c_str()), MAX_PATH); - path_remove_extension(name); + auto name = Path::GetFileNameWithoutExtension(gScenarioSavePath); Intent* intent = new Intent(WC_LOADSAVE); intent->putExtra(INTENT_EXTRA_LOADSAVE_TYPE, LOADSAVETYPE_SAVE | LOADSAVETYPE_GAME); - intent->putExtra(INTENT_EXTRA_PATH, std::string{ name }); + intent->putExtra(INTENT_EXTRA_PATH, name); return intent; } diff --git a/src/openrct2/interface/Screenshot.cpp b/src/openrct2/interface/Screenshot.cpp index e3f49ac55e..d01e431238 100644 --- a/src/openrct2/interface/Screenshot.cpp +++ b/src/openrct2/interface/Screenshot.cpp @@ -20,6 +20,7 @@ #include "../core/Console.hpp" #include "../core/File.h" #include "../core/Imaging.h" +#include "../core/Path.hpp" #include "../drawing/Drawing.h" #include "../drawing/X8DrawingEngine.h" #include "../localisation/Formatter.h" @@ -415,9 +416,10 @@ void screenshot_giant() WriteDpiToFile(path.value(), &dpi, gPalette); // Show user that screenshot saved successfully + const auto filename = Path::GetFileName(path.value()); Formatter ft; ft.Add(STR_STRING); - ft.Add(path_get_filename(path->c_str())); + ft.Add(filename.c_str()); context_show_error(STR_SCREENSHOT_SAVED_AS, STR_NONE, ft); } catch (const std::exception& e) diff --git a/src/openrct2/rct1/S4Importer.cpp b/src/openrct2/rct1/S4Importer.cpp index eb6748a685..24543e0fd1 100644 --- a/src/openrct2/rct1/S4Importer.cpp +++ b/src/openrct2/rct1/S4Importer.cpp @@ -340,7 +340,7 @@ namespace RCT1 return ""; } - return path_get_filename(scenarioEntry->path); + return Path::GetFileName(scenarioEntry->path); } void InitialiseEntryMaps() diff --git a/src/openrct2/scenario/ScenarioRepository.cpp b/src/openrct2/scenario/ScenarioRepository.cpp index 54dd575e06..d79a6d6513 100644 --- a/src/openrct2/scenario/ScenarioRepository.cpp +++ b/src/openrct2/scenario/ScenarioRepository.cpp @@ -383,7 +383,7 @@ public: return result; } - const scenario_index_entry* GetByFilename(const utf8* filename) const override + const scenario_index_entry* GetByFilename(u8string_view filename) const override { for (const auto& scenario : _scenarios) { diff --git a/src/openrct2/scenario/ScenarioRepository.h b/src/openrct2/scenario/ScenarioRepository.h index c9ff1e12c7..9266471d7d 100644 --- a/src/openrct2/scenario/ScenarioRepository.h +++ b/src/openrct2/scenario/ScenarioRepository.h @@ -75,7 +75,7 @@ struct IScenarioRepository virtual size_t GetCount() const abstract; virtual const scenario_index_entry* GetByIndex(size_t index) const abstract; - virtual const scenario_index_entry* GetByFilename(const utf8* filename) const abstract; + virtual const scenario_index_entry* GetByFilename(u8string_view filename) const abstract; /** * Does not return custom scenarios due to the fact that they may have the same name. */ diff --git a/src/openrct2/util/Util.cpp b/src/openrct2/util/Util.cpp index 861033004c..bbebe2170e 100644 --- a/src/openrct2/util/Util.cpp +++ b/src/openrct2/util/Util.cpp @@ -81,7 +81,7 @@ utf8* path_get_directory(const utf8* path) return directory; } -const char* path_get_filename(const utf8* path) +static const char* path_get_filename(const utf8* path) { // Find last slash or backslash in the path char* filename = const_cast(strrchr(path, *PATH_SEPARATOR)); diff --git a/src/openrct2/util/Util.h b/src/openrct2/util/Util.h index 3e70ac10d8..e62f285baf 100644 --- a/src/openrct2/util/Util.h +++ b/src/openrct2/util/Util.h @@ -26,7 +26,6 @@ int32_t mph_to_dmps(int32_t mph); bool filename_valid_characters(const utf8* filename); char* path_get_directory(const utf8* path); -const char* path_get_filename(const utf8* path); void path_set_extension(utf8* path, const utf8* newExtension, size_t size); void path_append_extension(utf8* path, const utf8* newExtension, size_t size); void path_remove_extension(utf8* path); From 376c79408a2d3a6fbc8a2158d02ab9447d7c6995 Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Sat, 26 Feb 2022 17:33:12 +0100 Subject: [PATCH 2/5] Replace path_set_extension with Path::WithExtension() --- src/openrct2-ui/windows/LoadSave.cpp | 3 ++- src/openrct2/core/Path.cpp | 5 +++++ src/openrct2/core/Path.hpp | 1 + src/openrct2/util/Util.cpp | 9 --------- src/openrct2/util/Util.h | 1 - 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/openrct2-ui/windows/LoadSave.cpp b/src/openrct2-ui/windows/LoadSave.cpp index 659939f68e..bc46faf9c1 100644 --- a/src/openrct2-ui/windows/LoadSave.cpp +++ b/src/openrct2-ui/windows/LoadSave.cpp @@ -1087,7 +1087,8 @@ static void WindowLoadsaveSelect(rct_window* w, const char* path) { SetAndSaveConfigPath(gConfigGeneral.last_save_track_directory, pathBuffer); - path_set_extension(pathBuffer, "td6", sizeof(pathBuffer)); + const auto withExtension = Path::WithExtension(pathBuffer, "td6"); + String::Set(pathBuffer, sizeof(pathBuffer), withExtension.c_str()); RCT2::T6Exporter t6Export{ _trackDesign }; diff --git a/src/openrct2/core/Path.cpp b/src/openrct2/core/Path.cpp index 303e45cef0..4814ad7574 100644 --- a/src/openrct2/core/Path.cpp +++ b/src/openrct2/core/Path.cpp @@ -80,6 +80,11 @@ namespace Path return u8path(path).extension().u8string(); } + u8string WithExtension(u8string_view path, u8string_view newExtension) + { + return u8path(path).replace_extension(u8path(newExtension)).u8string(); + } + u8string GetAbsolute(u8string_view relative) { std::error_code ec; diff --git a/src/openrct2/core/Path.hpp b/src/openrct2/core/Path.hpp index d3179ae50b..b32ab85d27 100644 --- a/src/openrct2/core/Path.hpp +++ b/src/openrct2/core/Path.hpp @@ -30,6 +30,7 @@ namespace Path u8string GetFileName(u8string_view origPath); u8string GetFileNameWithoutExtension(u8string_view path); u8string GetExtension(u8string_view path); + u8string WithExtension(u8string_view path, u8string_view newExtension); u8string GetAbsolute(u8string_view relative); bool Equals(u8string_view a, u8string_view b); diff --git a/src/openrct2/util/Util.cpp b/src/openrct2/util/Util.cpp index bbebe2170e..1b4594bc79 100644 --- a/src/openrct2/util/Util.cpp +++ b/src/openrct2/util/Util.cpp @@ -101,15 +101,6 @@ static const char* path_get_filename(const utf8* path) return filename; } -void path_set_extension(utf8* path, const utf8* newExtension, size_t size) -{ - // Remove existing extension (check first if there is one) - if (!Path::GetExtension(path).empty()) - path_remove_extension(path); - // Append new extension - path_append_extension(path, newExtension, size); -} - void path_append_extension(utf8* path, const utf8* newExtension, size_t size) { // Skip to the dot if the extension starts with a pattern (starts with "*.") diff --git a/src/openrct2/util/Util.h b/src/openrct2/util/Util.h index e62f285baf..327a282716 100644 --- a/src/openrct2/util/Util.h +++ b/src/openrct2/util/Util.h @@ -26,7 +26,6 @@ int32_t mph_to_dmps(int32_t mph); bool filename_valid_characters(const utf8* filename); char* path_get_directory(const utf8* path); -void path_set_extension(utf8* path, const utf8* newExtension, size_t size); void path_append_extension(utf8* path, const utf8* newExtension, size_t size); void path_remove_extension(utf8* path); void path_end_with_separator(utf8* path, size_t size); From 33edc5988375252dd737c2751ae25bab11972912 Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Sat, 26 Feb 2022 17:40:49 +0100 Subject: [PATCH 3/5] Replace path_remove_extension() --- src/openrct2/Game.cpp | 11 ++--------- src/openrct2/util/Util.cpp | 10 ---------- src/openrct2/util/Util.h | 1 - 3 files changed, 2 insertions(+), 20 deletions(-) diff --git a/src/openrct2/Game.cpp b/src/openrct2/Game.cpp index 0b9098bdd5..1e3d0447ca 100644 --- a/src/openrct2/Game.cpp +++ b/src/openrct2/Game.cpp @@ -533,11 +533,7 @@ void save_game() { if (!gFirstTimeSaving) { - char savePath[MAX_PATH]; - safe_strcpy(savePath, gScenarioSavePath.c_str(), MAX_PATH); - path_remove_extension(savePath); - path_append_extension(savePath, ".park", MAX_PATH); - + const auto savePath = Path::WithExtension(gScenarioSavePath, ".park"); save_game_with_name(savePath); } else @@ -550,10 +546,7 @@ void save_game_cmd(u8string_view name /* = {} */) { if (name.empty()) { - char savePath[MAX_PATH]; - safe_strcpy(savePath, gScenarioSavePath.c_str(), MAX_PATH); - path_remove_extension(savePath); - path_append_extension(savePath, ".park", MAX_PATH); + const auto savePath = Path::WithExtension(gScenarioSavePath, ".park"); save_game_with_name(savePath); } diff --git a/src/openrct2/util/Util.cpp b/src/openrct2/util/Util.cpp index 1b4594bc79..e2c0ca2ac6 100644 --- a/src/openrct2/util/Util.cpp +++ b/src/openrct2/util/Util.cpp @@ -115,16 +115,6 @@ void path_append_extension(utf8* path, const utf8* newExtension, size_t size) safe_strcat(path, newExtension, size); } -void path_remove_extension(utf8* path) -{ - // Find last dot in filename, and replace it with a null-terminator - char* lastDot = const_cast(strrchr(path_get_filename(path), '.')); - if (lastDot != nullptr) - *lastDot = '\0'; - else - log_warning("No extension found. (path = %s)", path); -} - void path_end_with_separator(utf8* path, size_t size) { size_t length = strnlen(path, size); diff --git a/src/openrct2/util/Util.h b/src/openrct2/util/Util.h index 327a282716..12932b0f34 100644 --- a/src/openrct2/util/Util.h +++ b/src/openrct2/util/Util.h @@ -27,7 +27,6 @@ bool filename_valid_characters(const utf8* filename); char* path_get_directory(const utf8* path); void path_append_extension(utf8* path, const utf8* newExtension, size_t size); -void path_remove_extension(utf8* path); void path_end_with_separator(utf8* path, size_t size); bool sse41_available(); From 060c430c64da2b2cd56a8f21aa296fa8eb6b7883 Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Sat, 26 Feb 2022 17:51:05 +0100 Subject: [PATCH 4/5] Replace path_get_directory() --- src/openrct2/CmdlineSprite.cpp | 6 ++---- src/openrct2/util/Util.cpp | 39 ---------------------------------- src/openrct2/util/Util.h | 1 - 3 files changed, 2 insertions(+), 44 deletions(-) diff --git a/src/openrct2/CmdlineSprite.cpp b/src/openrct2/CmdlineSprite.cpp index b3a68580a7..cc2301841e 100644 --- a/src/openrct2/CmdlineSprite.cpp +++ b/src/openrct2/CmdlineSprite.cpp @@ -579,7 +579,7 @@ int32_t cmdline_for_sprite(const char** argv, int32_t argc) const char* spriteFilePath = argv[1]; const char* spriteDescriptionPath = argv[2]; - char* directoryPath = path_get_directory(spriteDescriptionPath); + const auto directoryPath = Path::GetDirectory(spriteDescriptionPath); json_t jsonSprites = Json::ReadFromFile(spriteDescriptionPath); if (jsonSprites.is_null()) @@ -630,7 +630,7 @@ int32_t cmdline_for_sprite(const char** argv, int32_t argc) : ImageImporter::Palette::OpenRCT2; bool forceBmp = !jsonSprite["palette"].is_null() && Json::GetBoolean(jsonSprite["forceBmp"]); - auto imagePath = Path::GetAbsolute(std::string(directoryPath) + "/" + strPath); + auto imagePath = Path::GetAbsolute(directoryPath + u8"/" + strPath); auto importResult = SpriteImageImport( imagePath.c_str(), Json::GetNumber(x_offset), Json::GetNumber(y_offset), palette, forceBmp, @@ -653,8 +653,6 @@ int32_t cmdline_for_sprite(const char** argv, int32_t argc) return -1; } - free(directoryPath); - fprintf(stdout, "Finished\n"); return 1; } diff --git a/src/openrct2/util/Util.cpp b/src/openrct2/util/Util.cpp index e2c0ca2ac6..cfe793ea19 100644 --- a/src/openrct2/util/Util.cpp +++ b/src/openrct2/util/Util.cpp @@ -62,45 +62,6 @@ bool filename_valid_characters(const utf8* filename) return true; } -utf8* path_get_directory(const utf8* path) -{ - // Find the last slash or backslash in the path - char* filename = const_cast(strrchr(path, *PATH_SEPARATOR)); - char* filename_posix = const_cast(strrchr(path, '/')); - filename = filename < filename_posix ? filename_posix : filename; - - // If the path is invalid (e.g. just a file name), return NULL - if (filename == nullptr) - { - return nullptr; - } - - char* directory = _strdup(path); - safe_strtrunc(directory, strlen(path) - strlen(filename) + 2); - - return directory; -} - -static const char* path_get_filename(const utf8* path) -{ - // Find last slash or backslash in the path - char* filename = const_cast(strrchr(path, *PATH_SEPARATOR)); - char* filename_posix = const_cast(strchr(path, '/')); - filename = filename < filename_posix ? filename_posix : filename; - - // Checks if the path is valid (e.g. not just a file name) - if (filename == nullptr) - { - // Return the input string to keep things working - return path; - } - - // Increase pointer by one, to get rid of the slashes - filename++; - - return filename; -} - void path_append_extension(utf8* path, const utf8* newExtension, size_t size) { // Skip to the dot if the extension starts with a pattern (starts with "*.") diff --git a/src/openrct2/util/Util.h b/src/openrct2/util/Util.h index 12932b0f34..5491f65ac4 100644 --- a/src/openrct2/util/Util.h +++ b/src/openrct2/util/Util.h @@ -25,7 +25,6 @@ int32_t mph_to_dmps(int32_t mph); bool filename_valid_characters(const utf8* filename); -char* path_get_directory(const utf8* path); void path_append_extension(utf8* path, const utf8* newExtension, size_t size); void path_end_with_separator(utf8* path, size_t size); From f52c83962ed13baa5498ad70714243020017688d Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Wed, 2 Mar 2022 20:01:04 +0100 Subject: [PATCH 5/5] Use Path::Combine() instead of hardcoded slash --- src/openrct2/CmdlineSprite.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openrct2/CmdlineSprite.cpp b/src/openrct2/CmdlineSprite.cpp index cc2301841e..2258c90bd3 100644 --- a/src/openrct2/CmdlineSprite.cpp +++ b/src/openrct2/CmdlineSprite.cpp @@ -630,7 +630,7 @@ int32_t cmdline_for_sprite(const char** argv, int32_t argc) : ImageImporter::Palette::OpenRCT2; bool forceBmp = !jsonSprite["palette"].is_null() && Json::GetBoolean(jsonSprite["forceBmp"]); - auto imagePath = Path::GetAbsolute(directoryPath + u8"/" + strPath); + auto imagePath = Path::GetAbsolute(Path::Combine(directoryPath, strPath)); auto importResult = SpriteImageImport( imagePath.c_str(), Json::GetNumber(x_offset), Json::GetNumber(y_offset), palette, forceBmp,