diff --git a/src/binary.hpp b/src/binary.hpp index d134869..47b7ff2 100644 --- a/src/binary.hpp +++ b/src/binary.hpp @@ -21,16 +21,15 @@ /** * @brief Look for a bit pattern that set 16:9 aspect ratio for Tomb Raider 4. - * + * @param[in] Open file object reference. * @retval 0 Success. * @retval 1 Pattern was not found in the file. * @retval 2 Could not open the file. * @retval 3 Could not write to the file. - * * @return error qint64. */ qint64 findReplacePattern(QFile* const file) { - qint64 status = 0; // Initialize the status variable + qint64 status = 0; QByteArray fileContent = file->readAll(); file->close(); @@ -40,59 +39,53 @@ qint64 findReplacePattern(QFile* const file) { // Find the pattern in the file content int index = fileContent.indexOf(pattern); - if (index == -1) { - qDebug() << "Pattern not found in the file."; - return 1; // Pattern not found - } - - // Replace the pattern - fileContent.replace(index, pattern.size(), replacement); + if (index != -1) { + // Replace the pattern + fileContent.replace(index, pattern.size(), replacement); - // Reopen the file for writing - if (!file->open(QIODevice::WriteOnly)) { // flawfinder: ignore - qCritical() << "Error opening file for writing!"; - status = 2; - } else if (file->write(fileContent) == -1) { - qCritical() << "Error writing to file!"; - status = 3; + // Reopen the file for writing + if (!file->open(QIODevice::WriteOnly)) { // flawfinder: ignore + qCritical() << "Error opening file for writing!"; + status = 2; + } else if (file->write(fileContent) == -1) { + qCritical() << "Error writing to file!"; + status = 3; + } else { + qDebug() << "Widescreen patch applied successfully!"; + } } else { - qDebug() << "Widescreen patch applied successfully!"; + qDebug() << "Pattern not found in the file."; + status = 1; } - file->close(); return status; } /** * @brief Widescreen in binary set function. - * + * @param[in] File path to windows exe. * @retval 0 Success. * @retval 1 Path was not an safe file regular file. * @retval 2 Could not preform the first read only opening of the file. - * * @return error qint64. */ qint64 widescreen_set(const QString& path) { qint64 status = 0; // Initialize the status variable - - // Validate the file path QFileInfo fileInfo(path); + QFile file(path); + + // Open the file if (!fileInfo.exists() || !fileInfo.isFile()) { qCritical() << "Error: The exe path is not a regular file: " << path; return 1; // Invalid file path - } - - // Open the file for reading in binary mode - QFile file(path); - if (!file.open(QIODevice::ReadOnly)) { // flawfinder: ignore + } else if (!file.open(QIODevice::ReadOnly)) { // flawfinder: ignore qCritical() << "Error opening file for reading!"; return 2; // File open error + } else { + // Perform the pattern replacement and propagate the status + status = findReplacePattern(&file); } - - // Perform the pattern replacement and propagate the status - status = findReplacePattern(&file); - - return status; // Single return point + return status; } #endif // SRC_BINARY_HPP_