Fix long demo names being truncated in popups and UI

Use `IO_MAX_PATH_LENGTH` for all demo filenames and paths, so long demo names and demo names containing many unicode characters are not so easily truncated in the cut, rename and render dialogs.

Use combination of `str_endswith` and `str_append` to append file extensions, instead of using `str_comp_nocase` and `str_format`. Thereby only support creating demos and video files with lower case file extension, as only demo files with lower case file extension are shown in the client anyway.
This commit is contained in:
Robert Müller 2022-10-24 20:17:11 +02:00
parent b486028838
commit 6ef3adaa23
3 changed files with 19 additions and 27 deletions

View file

@ -1995,14 +1995,12 @@ int CMenus::Render()
// rename demo
if(m_DemolistSelectedIndex >= 0 && !m_DemolistSelectedIsDir)
{
char aBufOld[512];
char aBufOld[IO_MAX_PATH_LENGTH];
str_format(aBufOld, sizeof(aBufOld), "%s/%s", m_aCurrentDemoFolder, m_vDemos[m_DemolistSelectedIndex].m_aFilename);
int Length = str_length(m_aCurrentDemoFile);
char aBufNew[512];
if(Length <= 4 || m_aCurrentDemoFile[Length - 5] != '.' || str_comp_nocase(m_aCurrentDemoFile + Length - 4, "demo"))
str_format(aBufNew, sizeof(aBufNew), "%s/%s.demo", m_aCurrentDemoFolder, m_aCurrentDemoFile);
else
str_format(aBufNew, sizeof(aBufNew), "%s/%s", m_aCurrentDemoFolder, m_aCurrentDemoFile);
char aBufNew[IO_MAX_PATH_LENGTH];
str_format(aBufNew, sizeof(aBufNew), "%s/%s", m_aCurrentDemoFolder, m_aCurrentDemoFile);
if(!str_endswith(aBufNew, ".demo"))
str_append(aBufNew, ".demo", sizeof(aBufNew));
if(Storage()->RenameFile(aBufOld, aBufNew, m_vDemos[m_DemolistSelectedIndex].m_StorageType))
{
DemolistPopulate();
@ -2054,17 +2052,12 @@ int CMenus::Render()
// name video
if(m_DemolistSelectedIndex >= 0 && !m_DemolistSelectedIsDir)
{
char aBufOld[512];
char aBufOld[IO_MAX_PATH_LENGTH];
str_format(aBufOld, sizeof(aBufOld), "%s/%s", m_aCurrentDemoFolder, m_vDemos[m_DemolistSelectedIndex].m_aFilename);
int Length = str_length(m_aCurrentDemoFile);
char aBufNew[512];
if(Length <= 3 || m_aCurrentDemoFile[Length - 4] != '.' || str_comp_nocase(m_aCurrentDemoFile + Length - 3, "mp4"))
str_format(aBufNew, sizeof(aBufNew), "%s.mp4", m_aCurrentDemoFile);
else
str_format(aBufNew, sizeof(aBufNew), "%s", m_aCurrentDemoFile);
char aWholePath[1024];
if(!str_endswith(m_aCurrentDemoFile, ".mp4"))
str_append(m_aCurrentDemoFile, ".mp4", sizeof(m_aCurrentDemoFile));
char aWholePath[IO_MAX_PATH_LENGTH];
// store new video filename to origin buffer
str_copy(m_aCurrentDemoFile, aBufNew);
if(Storage()->FindFile(m_aCurrentDemoFile, "videos", IStorage::TYPE_ALL, aWholePath, sizeof(aWholePath)))
{
m_Popup = POPUP_REPLACE_VIDEO;

View file

@ -374,7 +374,7 @@ protected:
struct CDemoItem
{
char m_aFilename[IO_MAX_PATH_LENGTH];
char m_aName[128];
char m_aName[IO_MAX_PATH_LENGTH];
bool m_IsDir;
int m_StorageType;
time_t m_Date;
@ -434,8 +434,8 @@ protected:
}
};
char m_aCurrentDemoFolder[256];
char m_aCurrentDemoFile[64];
char m_aCurrentDemoFolder[IO_MAX_PATH_LENGTH];
char m_aCurrentDemoFile[IO_MAX_PATH_LENGTH];
int m_DemolistSelectedIndex;
bool m_DemolistSelectedIsDir;
int m_DemolistStorageType;

View file

@ -542,9 +542,9 @@ void CMenus::RenderDemoPlayer(CUIRect MainView)
GameClient()->m_Tooltips.DoToolTip(&s_KeyboardShortcutsButton, &Button, Localize("Toggle keyboard shortcuts"));
// demo name
char aDemoName[64] = {0};
char aDemoName[IO_MAX_PATH_LENGTH];
DemoPlayer()->GetDemoName(aDemoName, sizeof(aDemoName));
char aBuf[128];
char aBuf[IO_MAX_PATH_LENGTH + 128];
str_format(aBuf, sizeof(aBuf), Localize("Demofile: %s"), aDemoName);
CTextCursor Cursor;
TextRender()->SetCursor(&Cursor, NameBar.x, NameBar.y + (NameBar.h - (Button.h * 0.5f)) / 2.f, Button.h * 0.5f, TEXTFLAG_RENDER | TEXTFLAG_STOP_AT_END);
@ -824,7 +824,7 @@ int CMenus::DemolistFetchCallback(const CFsFileInfo *pInfo, int IsDir, int Stora
}
else
{
str_truncate(Item.m_aName, sizeof(Item.m_aName), pInfo->m_pName, str_length(pInfo->m_pName) - 5);
str_truncate(Item.m_aName, sizeof(Item.m_aName), pInfo->m_pName, str_length(pInfo->m_pName) - str_length(".demo"));
Item.m_InfosLoaded = false;
Item.m_Date = pInfo->m_TimeModified;
}
@ -887,7 +887,7 @@ bool CMenus::FetchHeader(CDemoItem &Item)
{
if(!Item.m_InfosLoaded)
{
char aBuffer[512];
char aBuffer[IO_MAX_PATH_LENGTH];
str_format(aBuffer, sizeof(aBuffer), "%s/%s", m_aCurrentDemoFolder, Item.m_aFilename);
Item.m_Valid = DemoPlayer()->GetDemoInfo(Storage(), aBuffer, Item.m_StorageType, &Item.m_Info, &Item.m_TimelineMarkers, &Item.m_MapInfo);
Item.m_InfosLoaded = true;
@ -1302,9 +1302,8 @@ void CMenus::RenderDemoList(CUIRect MainView)
fs_parent_dir(m_aCurrentDemoFolder);
else // sub folder
{
char aTemp[256];
str_copy(aTemp, m_aCurrentDemoFolder);
str_format(m_aCurrentDemoFolder, sizeof(m_aCurrentDemoFolder), "%s/%s", aTemp, m_vDemos[m_DemolistSelectedIndex].m_aFilename);
str_append(m_aCurrentDemoFolder, "/", sizeof(m_aCurrentDemoFolder));
str_append(m_aCurrentDemoFolder, m_vDemos[m_DemolistSelectedIndex].m_aFilename, sizeof(m_aCurrentDemoFolder));
m_DemolistStorageType = m_vDemos[m_DemolistSelectedIndex].m_StorageType;
}
DemolistPopulate();
@ -1312,7 +1311,7 @@ void CMenus::RenderDemoList(CUIRect MainView)
}
else // file
{
char aBuf[512];
char aBuf[IO_MAX_PATH_LENGTH];
str_format(aBuf, sizeof(aBuf), "%s/%s", m_aCurrentDemoFolder, m_vDemos[m_DemolistSelectedIndex].m_aFilename);
const char *pError = Client()->DemoPlayer_Play(aBuf, m_vDemos[m_DemolistSelectedIndex].m_StorageType);
if(pError)