diff --git a/Minecraft.Client/Platform/Common/UI/UITTFFont.cpp b/Minecraft.Client/Platform/Common/UI/UITTFFont.cpp index 8f1ad34af..81ff9b33b 100644 --- a/Minecraft.Client/Platform/Common/UI/UITTFFont.cpp +++ b/Minecraft.Client/Platform/Common/UI/UITTFFont.cpp @@ -1,40 +1,51 @@ #include "../../Minecraft.World/Platform/stdafx.h" #include "UI.h" -#include "../../Minecraft.World/Util/StringHelpers.h" -#include "../../Minecraft.World/IO/Files/File.h" +#include "../../Minecraft.World/Util/PortableFileIO.h" #include "UITTFFont.h" UITTFFont::UITTFFont(const std::string &path, S32 fallbackCharacter) { app.DebugPrintf("UITTFFont opening %s\n",path.c_str()); + pbData = NULL; -#if defined(_UNICODE) && !defined(__linux__) std::wstring wPath = convStringToWstring(path); - HANDLE file = CreateFile(wPath.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); -#else - HANDLE file = CreateFile(path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); -#endif - if( file == INVALID_HANDLE_VALUE ) + std::FILE *file = PortableFileIO::OpenBinaryFileForRead(wPath); + if( file == NULL ) { - DWORD error = GetLastError(); - app.DebugPrintf("Failed to open TTF file with error code %d (%x)\n", error, error); + app.DebugPrintf("Failed to open TTF file\n"); assert(false); } - DWORD dwHigh=0; - DWORD dwFileSize = GetFileSize(file,&dwHigh); - - if(dwFileSize!=0) + if(!PortableFileIO::Seek(file, 0, SEEK_END)) { - DWORD bytesRead; + std::fclose(file); + app.FatalLoadError(); + } - pbData = (PBYTE) new BYTE[dwFileSize]; - BOOL bSuccess = ReadFile(file,pbData,dwFileSize,&bytesRead,NULL); - if(bSuccess==FALSE) + const __int64 endPosition = PortableFileIO::Tell(file); + if(endPosition < 0) + { + std::fclose(file); + app.FatalLoadError(); + } + + const std::size_t fileSize = static_cast(endPosition); + if(fileSize != 0) + { + if(!PortableFileIO::Seek(file, 0, SEEK_SET)) + { + std::fclose(file); + app.FatalLoadError(); + } + + pbData = new std::uint8_t[fileSize]; + const std::size_t bytesRead = std::fread(pbData, 1, fileSize, file); + const bool failed = std::ferror(file) != 0 || bytesRead != fileSize; + std::fclose(file); + if(failed) { app.FatalLoadError(); } - CloseHandle(file); IggyFontInstallTruetypeUTF8 ( (void *)pbData, IGGY_TTC_INDEX_none, "Mojangles_TTF", -1, IGGY_FONTFLAG_none ); @@ -44,8 +55,12 @@ UITTFFont::UITTFFont(const std::string &path, S32 fallbackCharacter) IggyFontInstallTruetypeUTF8 ( (void *)pbData, IGGY_TTC_INDEX_none, "Times New Roman", -1, IGGY_FONTFLAG_none ); IggyFontInstallTruetypeUTF8 ( (void *)pbData, IGGY_TTC_INDEX_none, "Arial", -1, IGGY_FONTFLAG_none ); } + else + { + std::fclose(file); + } } UITTFFont::~UITTFFont() { -} \ No newline at end of file +} diff --git a/Minecraft.Client/Platform/Common/UI/UITTFFont.h b/Minecraft.Client/Platform/Common/UI/UITTFFont.h index e489e1e69..6053f9b49 100644 --- a/Minecraft.Client/Platform/Common/UI/UITTFFont.h +++ b/Minecraft.Client/Platform/Common/UI/UITTFFont.h @@ -1,9 +1,11 @@ #pragma once +#include + class UITTFFont { private: - PBYTE pbData; + std::uint8_t *pbData; //DWORD dwDataSize; public: