From 9cbfde5a3b4644536c3c7d05fddecc894fa32e59 Mon Sep 17 00:00:00 2001 From: Uladzislau Nikalayevich Date: Sun, 3 Sep 2023 15:00:45 +0300 Subject: [PATCH] Fix Unicode file path passed in CClientIMG #3166 --- Client/game_sa/CStreamingSA.cpp | 4 ++-- Client/game_sa/CStreamingSA.h | 2 +- Client/mods/deathmatch/logic/CClientIMG.cpp | 4 ++-- Client/mods/deathmatch/logic/CClientIMG.h | 2 +- Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp | 5 ++++- Client/sdk/game/CStreaming.h | 2 +- 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Client/game_sa/CStreamingSA.cpp b/Client/game_sa/CStreamingSA.cpp index 8928561682..baf355d410 100644 --- a/Client/game_sa/CStreamingSA.cpp +++ b/Client/game_sa/CStreamingSA.cpp @@ -381,7 +381,7 @@ unsigned char CStreamingSA::GetUnusedStreamHandle() return INVALID_STREAM_ID; } -unsigned char CStreamingSA::AddArchive(const char* szFilePath) +unsigned char CStreamingSA::AddArchive(const wchar_t* szFilePath) { auto ucArchiveId = GetUnusedArchive(); if (ucArchiveId == INVALID_ARCHIVE_ID) @@ -402,7 +402,7 @@ unsigned char CStreamingSA::AddArchive(const char* szFilePath) // Create new stream handler const auto streamCreateFlags = *(DWORD*)0x8E3FE0; - HANDLE hFile = CreateFileA(szFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, + HANDLE hFile = CreateFileW(szFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, streamCreateFlags | FILE_ATTRIBUTE_READONLY | FILE_FLAG_RANDOM_ACCESS, NULL); if (hFile == INVALID_HANDLE_VALUE) diff --git a/Client/game_sa/CStreamingSA.h b/Client/game_sa/CStreamingSA.h index e6ed1db368..bb81b2afeb 100644 --- a/Client/game_sa/CStreamingSA.h +++ b/Client/game_sa/CStreamingSA.h @@ -69,7 +69,7 @@ class CStreamingSA final : public CStreaming void SetStreamingInfo(uint32 modelid, unsigned char usStreamID, uint uiOffset, ushort usSize, uint uiNextInImg = -1); unsigned char GetUnusedArchive(); unsigned char GetUnusedStreamHandle(); - unsigned char AddArchive(const char* szFilePath); + unsigned char AddArchive(const wchar_t* szFilePath); void RemoveArchive(unsigned char ucArchiveID); void SetStreamingBufferSize(uint32 uiSize); uint32 GetStreamingBufferSize() { return ms_streamingHalfOfBufferSize * 2; }; // In bytes diff --git a/Client/mods/deathmatch/logic/CClientIMG.cpp b/Client/mods/deathmatch/logic/CClientIMG.cpp index 278bdb30b8..0778385184 100644 --- a/Client/mods/deathmatch/logic/CClientIMG.cpp +++ b/Client/mods/deathmatch/logic/CClientIMG.cpp @@ -49,7 +49,7 @@ bool CClientIMG::Load(fs::path filePath) if (!fs::exists(filePath)) return false; - m_filePath = filePath.string(); + m_filePath = filePath; m_ifs = std::ifstream(filePath, std::ios::binary); // Open the file @@ -157,7 +157,7 @@ bool CClientIMG::StreamEnable() m_LargestFileSizeBlocks = std::max(m_LargestFileSizeBlocks, (size_t)fileInfo.usSize); } - m_ucArchiveID = g_pGame->GetStreaming()->AddArchive(m_filePath.c_str()); + m_ucArchiveID = g_pGame->GetStreaming()->AddArchive(m_filePath.wstring().c_str()); if (IsStreamed()) { diff --git a/Client/mods/deathmatch/logic/CClientIMG.h b/Client/mods/deathmatch/logic/CClientIMG.h index 0aa51d178d..5cd50c93dd 100644 --- a/Client/mods/deathmatch/logic/CClientIMG.h +++ b/Client/mods/deathmatch/logic/CClientIMG.h @@ -78,7 +78,7 @@ class CClientIMG : public CClientEntity class CClientIMGManager* m_pImgManager; std::ifstream m_ifs; - std::string m_filePath; + fs::path m_filePath; unsigned char m_ucArchiveID; std::vector m_fileInfos; size_t m_LargestFileSizeBlocks; // The size of the largest file [in streaming blocks/sectors] diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp index 81490bda9f..c6daf1c5ed 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaEngineDefs.cpp @@ -653,8 +653,11 @@ CClientIMG* CLuaEngineDefs::EngineLoadIMG(lua_State* const luaVM, std::string st // Create the img handle CClientIMG* pImg = new CClientIMG(m_pManager, INVALID_ELEMENT_ID); + // Fix path encoding for sdt::filesystem + std::wstring utf8Path = SharedUtil::MbUTF8ToUTF16(strFullPath); + // Attempt loading the file - if (pImg->Load(std::move(strFullPath))) + if (pImg->Load(std::move(utf8Path))) { // Success. Make it a child of the resource img root pImg->SetParent(pRoot); diff --git a/Client/sdk/game/CStreaming.h b/Client/sdk/game/CStreaming.h index 1738b6054b..c6712f857e 100644 --- a/Client/sdk/game/CStreaming.h +++ b/Client/sdk/game/CStreaming.h @@ -39,7 +39,7 @@ class CStreaming virtual void RequestSpecialModel(DWORD model, const char* szTexture, DWORD channel) = 0; virtual CStreamingInfo* GetStreamingInfo(uint32 id) = 0; virtual void ReinitStreaming() = 0; - virtual unsigned char AddArchive(const char* szFilePath) = 0; + virtual unsigned char AddArchive(const wchar_t *szFilePath) = 0; virtual void RemoveArchive(unsigned char ucArchiveID) = 0; virtual void SetStreamingInfo(unsigned int id, unsigned char usStreamID, unsigned int uiOffset, unsigned short usSize, unsigned int uiNextInImg = -1) = 0; virtual void SetStreamingBufferSize(uint32 uiSize) = 0;