From 99ef97a0f8ba36f1c59050e67890f4fcdbc85161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 19 Mar 2023 11:14:55 +0100 Subject: [PATCH] Use variadic arguments for `ShowFileDialogError` formatting Reduces duplicate code when calling the function. Also increase the size of the message popup message, as it was not large enough for all paths. --- src/game/editor/editor.cpp | 67 ++++++++++++-------------------------- src/game/editor/editor.h | 5 +-- src/game/editor/popups.cpp | 4 +-- 3 files changed, 24 insertions(+), 52 deletions(-) diff --git a/src/game/editor/editor.cpp b/src/game/editor/editor.cpp index c00ce2f97..0a7347128 100644 --- a/src/game/editor/editor.cpp +++ b/src/game/editor/editor.cpp @@ -818,9 +818,7 @@ bool CEditor::CallbackOpenMap(const char *pFileName, int StorageType, void *pUse } else { - char aError[64 + IO_MAX_PATH_LENGTH]; - str_format(aError, sizeof(aError), "Failed to load map from file '%s'.", pFileName); - pEditor->ShowFileDialogError(aError); + pEditor->ShowFileDialogError("Failed to load map from file '%s'.", pFileName); return false; } } @@ -836,9 +834,7 @@ bool CEditor::CallbackAppendMap(const char *pFileName, int StorageType, void *pU else { pEditor->m_aFileName[0] = 0; - char aError[64 + IO_MAX_PATH_LENGTH]; - str_format(aError, sizeof(aError), "Failed to load map from file '%s'.", pFileName); - pEditor->ShowFileDialogError(aError); + pEditor->ShowFileDialogError("Failed to load map from file '%s'.", pFileName); return false; } } @@ -864,9 +860,7 @@ bool CEditor::CallbackSaveMap(const char *pFileName, int StorageType, void *pUse } else { - char aError[64 + IO_MAX_PATH_LENGTH]; - str_format(aError, sizeof(aError), "Failed to save map to file '%s'.", pFileName); - pEditor->ShowFileDialogError(aError); + pEditor->ShowFileDialogError("Failed to save map to file '%s'.", pFileName); return false; } } @@ -890,9 +884,7 @@ bool CEditor::CallbackSaveCopyMap(const char *pFileName, int StorageType, void * } else { - char aError[64 + IO_MAX_PATH_LENGTH]; - str_format(aError, sizeof(aError), "Failed to save map to file '%s'.", pFileName); - pEditor->ShowFileDialogError(aError); + pEditor->ShowFileDialogError("Failed to save map to file '%s'.", pFileName); return false; } } @@ -3924,9 +3916,7 @@ bool CEditor::ReplaceImage(const char *pFileName, int StorageType, void *pUser) { if(!str_comp(pImage->m_aName, aBuf)) { - char aError[64 + IO_MAX_PATH_LENGTH]; - str_format(aError, sizeof(aError), "Image named '%s' was already added.", pImage->m_aName); - pEditor->ShowFileDialogError(aError); + pEditor->ShowFileDialogError("Image named '%s' was already added.", pImage->m_aName); return false; } } @@ -3934,9 +3924,7 @@ bool CEditor::ReplaceImage(const char *pFileName, int StorageType, void *pUser) CEditorImage ImgInfo(pEditor); if(!pEditor->Graphics()->LoadPNG(&ImgInfo, pFileName, StorageType)) { - char aError[64 + IO_MAX_PATH_LENGTH]; - str_format(aError, sizeof(aError), "Failed to load image from file '%s'.", pFileName); - pEditor->ShowFileDialogError(aError); + pEditor->ShowFileDialogError("Failed to load image from file '%s'.", pFileName); return false; } @@ -3984,9 +3972,7 @@ bool CEditor::AddImage(const char *pFileName, int StorageType, void *pUser) { if(!str_comp(pImage->m_aName, aBuf)) { - char aError[64 + IO_MAX_PATH_LENGTH]; - str_format(aError, sizeof(aError), "Image named '%s' was already added.", pImage->m_aName); - pEditor->ShowFileDialogError(aError); + pEditor->ShowFileDialogError("Image named '%s' was already added.", pImage->m_aName); return false; } } @@ -4001,9 +3987,7 @@ bool CEditor::AddImage(const char *pFileName, int StorageType, void *pUser) CEditorImage ImgInfo(pEditor); if(!pEditor->Graphics()->LoadPNG(&ImgInfo, pFileName, StorageType)) { - char aError[64 + IO_MAX_PATH_LENGTH]; - str_format(aError, sizeof(aError), "Failed to load image from file '%s'.", pFileName); - pEditor->ShowFileDialogError(aError); + pEditor->ShowFileDialogError("Failed to load image from file '%s'.", pFileName); return false; } @@ -4053,9 +4037,7 @@ bool CEditor::AddSound(const char *pFileName, int StorageType, void *pUser) { if(!str_comp(pSound->m_aName, aBuf)) { - char aError[64 + IO_MAX_PATH_LENGTH]; - str_format(aError, sizeof(aError), "Sound named '%s' was already added.", pSound->m_aName); - pEditor->ShowFileDialogError(aError); + pEditor->ShowFileDialogError("Sound named '%s' was already added.", pSound->m_aName); return false; } } @@ -4065,9 +4047,7 @@ bool CEditor::AddSound(const char *pFileName, int StorageType, void *pUser) unsigned DataSize; if(!pEditor->Storage()->ReadFile(pFileName, StorageType, &pData, &DataSize)) { - char aError[64 + IO_MAX_PATH_LENGTH]; - str_format(aError, sizeof(aError), "Failed to open sound file '%s'.", pFileName); - pEditor->ShowFileDialogError(aError); + pEditor->ShowFileDialogError("Failed to open sound file '%s'.", pFileName); return false; } @@ -4076,9 +4056,7 @@ bool CEditor::AddSound(const char *pFileName, int StorageType, void *pUser) if(SoundId == -1) { free(pData); - char aError[64 + IO_MAX_PATH_LENGTH]; - str_format(aError, sizeof(aError), "Failed to load sound from file '%s'.", pFileName); - pEditor->ShowFileDialogError(aError); + pEditor->ShowFileDialogError("Failed to load sound from file '%s'.", pFileName); return false; } @@ -4115,9 +4093,7 @@ bool CEditor::ReplaceSound(const char *pFileName, int StorageType, void *pUser) { if(!str_comp(pSound->m_aName, aBuf)) { - char aError[64 + IO_MAX_PATH_LENGTH]; - str_format(aError, sizeof(aError), "Sound named '%s' was already added.", pSound->m_aName); - pEditor->ShowFileDialogError(aError); + pEditor->ShowFileDialogError("Sound named '%s' was already added.", pSound->m_aName); return false; } } @@ -4127,9 +4103,7 @@ bool CEditor::ReplaceSound(const char *pFileName, int StorageType, void *pUser) unsigned DataSize; if(!pEditor->Storage()->ReadFile(pFileName, StorageType, &pData, &DataSize)) { - char aError[64 + IO_MAX_PATH_LENGTH]; - str_format(aError, sizeof(aError), "Failed to open sound file '%s'.", pFileName); - pEditor->ShowFileDialogError(aError); + pEditor->ShowFileDialogError("Failed to open sound file '%s'.", pFileName); return false; } @@ -4138,9 +4112,7 @@ bool CEditor::ReplaceSound(const char *pFileName, int StorageType, void *pUser) if(SoundId == -1) { free(pData); - char aError[64 + IO_MAX_PATH_LENGTH]; - str_format(aError, sizeof(aError), "Failed to load sound from file '%s'.", pFileName); - pEditor->ShowFileDialogError(aError); + pEditor->ShowFileDialogError("Failed to load sound from file '%s'.", pFileName); return false; } @@ -5090,9 +5062,7 @@ void CEditor::RenderFileDialog() { if(!open_file(aPath)) { - char aError[64 + IO_MAX_PATH_LENGTH]; - str_format(aError, sizeof(aError), "Failed to open the directory '%s'.", aPath); - ShowFileDialogError(aError); + ShowFileDialogError("Failed to open the directory '%s'.", aPath); } } @@ -5204,11 +5174,14 @@ void CEditor::InvokeFileDialog(int StorageType, int FileType, const char *pTitle m_Dialog = DIALOG_FILE; } -void CEditor::ShowFileDialogError(const char *pError) +void CEditor::ShowFileDialogError(const char *pFormat, ...) { static SMessagePopupContext s_MessagePopupContext; s_MessagePopupContext.ErrorColor(); - str_copy(s_MessagePopupContext.m_aMessage, pError); + va_list VarArgs; + va_start(VarArgs, pFormat); + str_format_v(s_MessagePopupContext.m_aMessage, sizeof(s_MessagePopupContext.m_aMessage), pFormat, VarArgs); + va_end(VarArgs); ShowPopupMessage(UI()->MouseX(), UI()->MouseY(), &s_MessagePopupContext); } diff --git a/src/game/editor/editor.h b/src/game/editor/editor.h index ecc23738a..8334e7b36 100644 --- a/src/game/editor/editor.h +++ b/src/game/editor/editor.h @@ -899,7 +899,8 @@ public: void InvokeFileDialog(int StorageType, int FileType, const char *pTitle, const char *pButtonText, const char *pBasepath, const char *pDefaultName, bool (*pfnFunc)(const char *pFilename, int StorageType, void *pUser), void *pUser); - void ShowFileDialogError(const char *pError); + void ShowFileDialogError(const char *pFormat, ...) + GNUC_ATTRIBUTE((format(printf, 2, 3))); void Reset(bool CreateDefault = true); bool Save(const char *pFilename) override; @@ -1209,7 +1210,7 @@ public: { static constexpr float POPUP_MAX_WIDTH = 200.0f; static constexpr float POPUP_FONT_SIZE = 10.0f; - char m_aMessage[256]; + char m_aMessage[1024]; ColorRGBA m_TextColor; void DefaultColor(class ITextRender *pTextRender); diff --git a/src/game/editor/popups.cpp b/src/game/editor/popups.cpp index 19d17f2e9..a24f7b138 100644 --- a/src/game/editor/popups.cpp +++ b/src/game/editor/popups.cpp @@ -1061,9 +1061,7 @@ int CEditor::PopupNewFolder(CEditor *pEditor, CUIRect View, void *pContext) } else { - char aError[64 + IO_MAX_PATH_LENGTH]; - str_format(aError, sizeof(aError), "Failed to create the folder '%s'.", aBuf); - pEditor->ShowFileDialogError(aError); + pEditor->ShowFileDialogError("Failed to create the folder '%s'.", aBuf); } } }