From 2251f62eccd2f547c830cd125335731cdcd50050 Mon Sep 17 00:00:00 2001 From: Enorovan Date: Thu, 31 Oct 2024 10:23:54 +0100 Subject: [PATCH] cclcc/fix-videos-and-sysmesbox: check on empty message in sysmesbox, and fix file read --- src/games/cc/sysmesbox.cpp | 24 ++++++++++++++---------- src/io/physicalfilestream.cpp | 16 ++++++++-------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/games/cc/sysmesbox.cpp b/src/games/cc/sysmesbox.cpp index 56f1ee40..dde0331e 100644 --- a/src/games/cc/sysmesbox.cpp +++ b/src/games/cc/sysmesbox.cpp @@ -232,12 +232,14 @@ void SysMesBox::AddMessage(uint8_t* str) { TextLayoutPlainLine(&dummy, 255, Profile::Dialogue::DialogueFont, TextFontSize, Profile::Dialogue::ColorTable[0], 1.0f, glm::vec2(TextX, 0.0f), TextAlignment::Left); - float mesLen = 0.0f; - for (int i = 0; i < Messages[MessageCount].size(); i++) { - mesLen += Messages[MessageCount][i].DestRect.Width; + if (!Messages[MessageCount].empty()) { + float mesLen = 0.0f; + for (int i = 0; i < Messages[MessageCount].size(); i++) { + mesLen += Messages[MessageCount][i].DestRect.Width; + } + MessageWidths[MessageCount] = mesLen; + MessageCount++; } - MessageWidths[MessageCount] = mesLen; - MessageCount++; } void SysMesBox::AddChoice(uint8_t* str) { @@ -247,12 +249,14 @@ void SysMesBox::AddChoice(uint8_t* str) { TextLayoutPlainLine(&dummy, 255, Profile::Dialogue::DialogueFont, TextFontSize, Profile::Dialogue::ColorTable[0], 1.0f, glm::vec2(TextX, 0.0f), TextAlignment::Left); - float mesLen = 0.0f; - for (int i = 0; i < Choices[ChoiceCount].size(); i++) { - mesLen += Choices[ChoiceCount][i].DestRect.Width; + if (!Choices[ChoiceCount].empty()) { + float mesLen = 0.0f; + for (int i = 0; i < Choices[ChoiceCount].size(); i++) { + mesLen += Choices[ChoiceCount][i].DestRect.Width; + } + ChoiceWidths[ChoiceCount] = mesLen; + ChoiceCount++; } - ChoiceWidths[ChoiceCount] = mesLen; - ChoiceCount++; } } // namespace CC diff --git a/src/io/physicalfilestream.cpp b/src/io/physicalfilestream.cpp index 24500e18..b76269fe 100644 --- a/src/io/physicalfilestream.cpp +++ b/src/io/physicalfilestream.cpp @@ -95,22 +95,22 @@ IoError PhysicalFileStream::Create(std::string const& fileName, Stream** out, int64_t PhysicalFileStream::Read(void* buffer, int64_t sz) { if (sz < 0 || !(Flags & READ)) return IoError_Fail; - if (Position >= Meta.Size || FileStream.eof()) { - FileStream.clear(FileStream.rdstate() & ~std::ios::failbit & - ~std::ios::eofbit); // Clear only failbit and eofbit - return IoError_Eof; - }; + if (Position >= Meta.Size) return IoError_Eof; + int bytesToRead = std::min(sz, Meta.Size - Position); FileStream.read((char*)buffer, bytesToRead); - if (!FileStream) { + auto read = FileStream.gcount(); + + if (read == 0) { // Check if no data was read + if (FileStream.eof()) return IoError_Eof; ImpLog(LL_Error, LC_IO, "Read failed for file \"%s\" with error: \"%s\"\n", SourceFileName.c_str(), std::generic_category().message(errno).c_str()); FileStream.clear(FileStream.rdstate() & ~std::ios::failbit & - ~std::ios::eofbit); // Clear only failbit and eofbit + ~std::ios::eofbit); return IoError_Fail; } - auto read = FileStream.gcount(); + Position += read; return read; }