diff --git a/pcsx2-qt/Tools/InputRecording/InputRecordingViewer.cpp b/pcsx2-qt/Tools/InputRecording/InputRecordingViewer.cpp index 7fcb0c2f0c..1227dfe3f4 100644 --- a/pcsx2-qt/Tools/InputRecording/InputRecordingViewer.cpp +++ b/pcsx2-qt/Tools/InputRecording/InputRecordingViewer.cpp @@ -84,7 +84,7 @@ void InputRecordingViewer::openFile() { } if (!fileNames.isEmpty()) { - std::string fileName = fileNames.first().toStdString(); + const std::string fileName = fileNames.first().toStdString(); m_file.OpenExisting(fileName); loadTable(); } diff --git a/pcsx2/Recording/InputRecording.cpp b/pcsx2/Recording/InputRecording.cpp index 7ab7fd2f2b..ec96928e49 100644 --- a/pcsx2/Recording/InputRecording.cpp +++ b/pcsx2/Recording/InputRecording.cpp @@ -502,7 +502,7 @@ void SaveStateBase::InputRecordingFreeze() InputRecording g_InputRecording; -bool InputRecording::create(const std::string_view& fileName, const bool fromSaveState, const std::string_view& authorName) +bool InputRecording::create(const std::string& fileName, const bool fromSaveState, const std::string& authorName) { if (!m_file.OpenNew(fileName, fromSaveState)) { @@ -542,7 +542,7 @@ bool InputRecording::create(const std::string_view& fileName, const bool fromSav return true; } -bool InputRecording::play(const std::string_view& filename) +bool InputRecording::play(const std::string& filename) { if (!m_file.OpenExisting(filename)) { diff --git a/pcsx2/Recording/InputRecording.h b/pcsx2/Recording/InputRecording.h index 4f71a2c13f..9d5f8d8e2f 100644 --- a/pcsx2/Recording/InputRecording.h +++ b/pcsx2/Recording/InputRecording.h @@ -162,8 +162,8 @@ public: FROM_SAVESTATE }; - bool create(const std::string_view& filename, const bool fromSaveState, const std::string_view& authorName); - bool play(const std::string_view& path); + bool create(const std::string& filename, const bool fromSaveState, const std::string& authorName); + bool play(const std::string& path); void stop(); void controllerInterrupt(u8& data, u8& port, u16& BufCount, u8 buf[]); diff --git a/pcsx2/Recording/InputRecordingFile.cpp b/pcsx2/Recording/InputRecordingFile.cpp index f92966e85f..b85ed07244 100644 --- a/pcsx2/Recording/InputRecordingFile.cpp +++ b/pcsx2/Recording/InputRecordingFile.cpp @@ -245,35 +245,28 @@ bool InputRecordingFile::verifyRecordingFileHeader() #include #include -void InputRecordingFileHeader::Init() +void InputRecordingFileHeader::Init() noexcept { - memset(m_author, 0, std::size(m_author)); - memset(m_gameName, 0, std::size(m_gameName)); + m_fileVersion = 1; } void InputRecordingFileHeader::SetEmulatorVersion() { - std::string emuVersion = fmt::format("PCSX2-{}.{}.{}", PCSX2_VersionHi, PCSX2_VersionMid, PCSX2_VersionLo); - int max = std::size(m_emulatorVersion) - 1; - strncpy(m_emulatorVersion, emuVersion.c_str(), max); - m_emulatorVersion[max] = 0; + static const std::string emuVersion = fmt::format("PCSX2-{}.{}.{}", PCSX2_VersionHi, PCSX2_VersionMid, PCSX2_VersionLo); + strncpy(m_emulatorVersion, emuVersion.c_str(), sizeof(m_emulatorVersion) - 1); } -void InputRecordingFileHeader::SetAuthor(const std::string_view& _author) +void InputRecordingFileHeader::SetAuthor(const std::string& _author) { - int max = std::size(m_author) - 1; - strncpy(m_author, _author.data(), max); - m_author[max] = 0; + strncpy(m_author, _author.data(), sizeof(m_author) - 1); } -void InputRecordingFileHeader::SetGameName(const std::string_view& _gameName) +void InputRecordingFileHeader::SetGameName(const std::string& _gameName) { - int max = std::size(m_gameName) - 1; - strncpy(m_gameName, _gameName.data(), max); - m_gameName[max] = 0; + strncpy(m_gameName, _gameName.data(), sizeof(m_gameName) - 1); } -bool InputRecordingFile::Close() +bool InputRecordingFile::Close() noexcept { if (m_recordingFile == nullptr) { @@ -281,31 +274,31 @@ bool InputRecordingFile::Close() } fclose(m_recordingFile); m_recordingFile = nullptr; - m_filename = ""; + m_filename.clear(); return true; } -const std::string& InputRecordingFile::getFilename() const +const std::string& InputRecordingFile::getFilename() const noexcept { return m_filename; } -InputRecordingFileHeader& InputRecordingFile::getHeader() +InputRecordingFileHeader& InputRecordingFile::getHeader() noexcept { return m_header; } -const long& InputRecordingFile::getTotalFrames() const +long InputRecordingFile::getTotalFrames() const noexcept { return m_totalFrames; } -const unsigned long& InputRecordingFile::getUndoCount() const +unsigned long InputRecordingFile::getUndoCount() const noexcept { return m_undoCount; } -bool InputRecordingFile::FromSaveState() +bool InputRecordingFile::FromSaveState() const noexcept { return m_savestate.fromSavestate; } @@ -349,7 +342,7 @@ bool InputRecordingFile::open(const std::string_view& path, bool newRecording) return false; } -bool InputRecordingFile::OpenNew(const std::string_view& path, bool fromSavestate) +bool InputRecordingFile::OpenNew(const std::string& path, bool fromSavestate) { if (!open(path, true)) return false; @@ -357,19 +350,19 @@ bool InputRecordingFile::OpenNew(const std::string_view& path, bool fromSavestat return true; } -bool InputRecordingFile::OpenExisting(const std::string_view& path) +bool InputRecordingFile::OpenExisting(const std::string& path) { return open(path, false); } -bool InputRecordingFile::ReadKeyBuffer(u8& result, const uint& frame, const uint port, const uint bufIndex) +bool InputRecordingFile::ReadKeyBuffer(u8& result, const uint frame, const uint port, const uint bufIndex) { if (m_recordingFile == nullptr) { return false; } - long seek = getRecordingBlockSeekPoint(frame) + s_controllerInputBytes * port + bufIndex; + const size_t seek = getRecordingBlockSeekPoint(frame) + s_controllerInputBytes * port + bufIndex; if (fseek(m_recordingFile, seek, SEEK_SET) != 0 || fread(&result, 1, 1, m_recordingFile) != 1) { return false; @@ -389,30 +382,34 @@ void InputRecordingFile::SetTotalFrames(long frame) fwrite(&m_totalFrames, 4, 1, m_recordingFile); } -bool InputRecordingFile::WriteHeader() +bool InputRecordingFile::WriteHeader() const { if (m_recordingFile == nullptr) { return false; } rewind(m_recordingFile); - if (fwrite(&m_header, sizeof(InputRecordingFileHeader), 1, m_recordingFile) != 1 || fwrite(&m_totalFrames, 4, 1, m_recordingFile) != 1 || fwrite(&m_undoCount, 4, 1, m_recordingFile) != 1 || fwrite(&m_savestate, 1, 1, m_recordingFile) != 1) + if (fwrite(&m_header, sizeof(InputRecordingFileHeader), 1, m_recordingFile) != 1 || + fwrite(&m_totalFrames, 4, 1, m_recordingFile) != 1 || + fwrite(&m_undoCount, 4, 1, m_recordingFile) != 1 || + fwrite(&m_savestate, 1, 1, m_recordingFile) != 1) { return false; } return true; } -bool InputRecordingFile::WriteKeyBuffer(const uint& frame, const uint port, const uint bufIndex, const u8& buf) +bool InputRecordingFile::WriteKeyBuffer(const uint frame, const uint port, const uint bufIndex, const u8 buf) const { if (m_recordingFile == nullptr) { return false; } - long seek = getRecordingBlockSeekPoint(frame) + 18 * port + bufIndex; - - if (fseek(m_recordingFile, seek, SEEK_SET) != 0 || fwrite(&buf, 1, 1, m_recordingFile) != 1) + const size_t seek = getRecordingBlockSeekPoint(frame) + s_controllerInputBytes * port + bufIndex; + + if (fseek(m_recordingFile, seek, SEEK_SET) != 0 || + fwrite(&buf, 1, 1, m_recordingFile) != 1) { return false; } @@ -424,9 +421,9 @@ bool InputRecordingFile::WriteKeyBuffer(const uint& frame, const uint port, cons void InputRecordingFile::logRecordingMetadata() { InputRec::consoleMultiLog({fmt::format("File: {}", getFilename()), - fmt::format("PCSX2 Version Used: {}", std::string(getHeader().m_emulatorVersion)), + fmt::format("PCSX2 Version Used: {}", getHeader().m_emulatorVersion), fmt::format("Recording File Version: {}", getHeader().m_fileVersion), - fmt::format("Associated Game Name or ISO Filename: {}", std::string(getHeader().m_gameName)), + fmt::format("Associated Game Name or ISO Filename: {}", getHeader().m_gameName), fmt::format("Author: {}", getHeader().m_author), fmt::format("Total Frames: {}", getTotalFrames()), fmt::format("Undo Count: {}", getUndoCount())}); @@ -464,7 +461,7 @@ std::vector InputRecordingFile::bulkReadPadData(long frameStart, long f return data; } -long InputRecordingFile::getRecordingBlockSeekPoint(const long& frame) +size_t InputRecordingFile::getRecordingBlockSeekPoint(const long frame) const noexcept { return s_headerSize + sizeof(bool) + frame * s_inputBytesPerFrame; } @@ -475,9 +472,12 @@ bool InputRecordingFile::verifyRecordingFileHeader() { return false; } - // Verify m_header contents + // Verify header contents rewind(m_recordingFile); - if (fread(&m_header, sizeof(InputRecordingFileHeader), 1, m_recordingFile) != 1 || fread(&m_totalFrames, 4, 1, m_recordingFile) != 1 || fread(&m_undoCount, 4, 1, m_recordingFile) != 1 || fread(&m_savestate.fromSavestate, sizeof(bool), 1, m_recordingFile) != 1) + if (fread(&m_header, sizeof(InputRecordingFileHeader), 1, m_recordingFile) != 1 || + fread(&m_totalFrames, 4, 1, m_recordingFile) != 1 || + fread(&m_undoCount, 4, 1, m_recordingFile) != 1 || + fread(&m_savestate.fromSavestate, sizeof(bool), 1, m_recordingFile) != 1) { return false; } @@ -485,7 +485,7 @@ bool InputRecordingFile::verifyRecordingFileHeader() // Check for current verison if (m_header.m_fileVersion != 1) { - InputRec::consoleLog(fmt::format("Input recording file is not a supported m_fileVersion - {}", m_header.m_fileVersion)); + InputRec::consoleLog(fmt::format("Input recording file is not a supported version - {}", m_header.m_fileVersion)); return false; } return true; diff --git a/pcsx2/Recording/InputRecordingFile.h b/pcsx2/Recording/InputRecordingFile.h index f872069bca..130807bb88 100644 --- a/pcsx2/Recording/InputRecordingFile.h +++ b/pcsx2/Recording/InputRecordingFile.h @@ -122,15 +122,15 @@ private: struct InputRecordingFileHeader { u8 m_fileVersion = 1; - char m_emulatorVersion[50] = ""; - char m_author[255] = ""; - char m_gameName[255] = ""; + char m_emulatorVersion[50]{}; + char m_author[255]{}; + char m_gameName[255]{}; public: void SetEmulatorVersion(); - void Init(); - void SetAuthor(const std::string_view& author); - void SetGameName(const std::string_view& cdrom); + void Init() noexcept; + void SetAuthor(const std::string& author); + void SetGameName(const std::string& cdrom); }; @@ -149,52 +149,52 @@ public: // Closes the underlying input recording file, writing the header and // prepares for a possible new recording to be started - bool Close(); + bool Close() noexcept; // The number of times a save-state has been loaded while recording this movie // this is also often referred to as a "re-record" // Whether or not this input recording starts by loading a save-state or by booting the game fresh - bool FromSaveState(); + bool FromSaveState() const noexcept; // Increment the number of undo actions and commit it to the recording file void IncrementUndoCount(); // Open an existing recording file - bool OpenExisting(const std::string_view& path); + bool OpenExisting(const std::string& path); // Create and open a brand new input recording, either starting from a save-state or from // booting the game - bool OpenNew(const std::string_view& path, bool fromSaveState); + bool OpenNew(const std::string& path, bool fromSaveState); // Reads the current frame's input data from the file in order to intercept and overwrite // the current frame's value from the emulator - bool ReadKeyBuffer(u8& result, const uint& frame, const uint port, const uint bufIndex); + bool ReadKeyBuffer(u8& result, const uint frame, const uint port, const uint bufIndex); // Updates the total frame counter and commit it to the recording file void SetTotalFrames(long frames); // Persist the input recording file header's current state to the file - bool WriteHeader(); + bool WriteHeader() const; // Writes the current frame's input data to the file so it can be replayed - bool WriteKeyBuffer(const uint& frame, const uint port, const uint bufIndex, const u8& buf); + bool WriteKeyBuffer(const uint frame, const uint port, const uint bufIndex, const u8 buf) const; // Retrieve the input recording's filename (not the path) - const std::string& getFilename() const; - InputRecordingFileHeader& getHeader(); - const long& getTotalFrames() const; - const unsigned long& getUndoCount() const; + const std::string& getFilename() const noexcept; + InputRecordingFileHeader& getHeader() noexcept; + long getTotalFrames() const noexcept; + unsigned long getUndoCount() const noexcept; void logRecordingMetadata(); std::vector bulkReadPadData(long frameStart, long frameEnd, const uint port); private: - static constexpr int s_controllerPortsSupported = 2; - static constexpr int s_controllerInputBytes = 18; - static constexpr int s_inputBytesPerFrame = s_controllerInputBytes * s_controllerPortsSupported; + static constexpr size_t s_controllerPortsSupported = 2; + static constexpr size_t s_controllerInputBytes = 18; + static constexpr size_t s_inputBytesPerFrame = s_controllerInputBytes * s_controllerPortsSupported; // TODO - version 2, this could be greatly simplified if everything was in the header // + 4 + 4 is the totalFrame and undoCount values - static constexpr int s_headerSize = sizeof(InputRecordingFileHeader) + 4 + 4; + static constexpr size_t s_headerSize = sizeof(InputRecordingFileHeader) + 4 + 4; // DEPRECATED / Slated for Removal - static constexpr int s_recordingSavestateHeaderSize = sizeof(bool); - static constexpr int s_seekpointTotalFrames = sizeof(InputRecordingFileHeader); - static constexpr int s_seekpointUndoCount = sizeof(InputRecordingFileHeader) + 4; - static constexpr int s_seekpointSaveStateHeader = s_seekpointUndoCount + 4; + static constexpr size_t s_recordingSavestateHeaderSize = sizeof(bool); + static constexpr size_t s_seekpointTotalFrames = sizeof(InputRecordingFileHeader); + static constexpr size_t s_seekpointUndoCount = sizeof(InputRecordingFileHeader) + 4; + static constexpr size_t s_seekpointSaveStateHeader = s_seekpointUndoCount + 4; InputRecordingFileHeader m_header; std::string m_filename = ""; @@ -206,7 +206,7 @@ private: unsigned long m_undoCount = 0; // Calculates the position of the current frame in the input recording - long getRecordingBlockSeekPoint(const long& frame); + size_t getRecordingBlockSeekPoint(const long frame) const noexcept; bool open(const std::string_view& path, bool newRecording); bool verifyRecordingFileHeader(); };