From 9e30fa81ded862ea40da6397f0364e23ea553346 Mon Sep 17 00:00:00 2001 From: Tyler Wilding Date: Wed, 15 Jun 2022 20:45:05 -0400 Subject: [PATCH] input-rec: refactor and cleanup the file and logging classes --- pcsx2/Recording/InputRecordingFile.cpp | 60 ++++++++++++++++--- pcsx2/Recording/InputRecordingFile.h | 19 +++--- .../Utilities/InputRecordingLogger.cpp | 40 +++++++------ .../Utilities/InputRecordingLogger.h | 2 +- 4 files changed, 87 insertions(+), 34 deletions(-) diff --git a/pcsx2/Recording/InputRecordingFile.cpp b/pcsx2/Recording/InputRecordingFile.cpp index 8cad05ff90..e3f4774870 100644 --- a/pcsx2/Recording/InputRecordingFile.cpp +++ b/pcsx2/Recording/InputRecordingFile.cpp @@ -242,6 +242,9 @@ bool InputRecordingFile::verifyRecordingFileHeader() #include +#include +#include + void InputRecordingFileHeader::Init() { memset(author, 0, std::size(author)); @@ -282,22 +285,22 @@ bool InputRecordingFile::Close() return true; } -const std::string& InputRecordingFile::GetFilename() +const std::string& InputRecordingFile::getFilename() const { return filename; } -InputRecordingFileHeader& InputRecordingFile::GetHeader() +InputRecordingFileHeader& InputRecordingFile::getHeader() { return header; } -long& InputRecordingFile::GetTotalFrames() +const long& InputRecordingFile::getTotalFrames() const { return totalFrames; } -unsigned long& InputRecordingFile::GetUndoCount() +const unsigned long& InputRecordingFile::getUndoCount() const { return undoCount; } @@ -339,10 +342,10 @@ bool InputRecordingFile::open(const std::string_view& path, bool newRecording) return true; } Close(); - inputRec::consoleLog("Input recording file header is invalid"); + InputRec::consoleLog("Input recording file header is invalid"); return false; } - inputRec::consoleLog(fmt::format("Input recording file opening failed. Error - {}", strerror(errno))); + InputRec::consoleLog(fmt::format("Input recording file opening failed. Error - {}", strerror(errno))); return false; } @@ -418,6 +421,49 @@ bool InputRecordingFile::WriteKeyBuffer(const uint& frame, const uint port, cons return true; } +void InputRecordingFile::logRecordingMetadata() +{ + InputRec::consoleMultiLog({fmt::format("File: {}", getFilename()), + fmt::format("PCSX2 Version Used: {}", std::string(getHeader().emu)), + fmt::format("Recording File Version: {}", getHeader().version), + fmt::format("Associated Game Name or ISO Filename: {}", std::string(getHeader().gameName)), + fmt::format("Author: {}", getHeader().author), + fmt::format("Total Frames: {}", getTotalFrames()), + fmt::format("Undo Count: {}", getUndoCount())}); +} + +std::vector InputRecordingFile::bulkReadPadData(long frameStart, long frameEnd, const uint port) +{ + std::vector data = {}; + + if (recordingFile == nullptr) + { + return data; + } + + frameStart = frameStart < 0 ? 0 : frameStart; + + std::array padBytes; + + // TODO - there are probably issues here if the file is too small / the frame counters are invalid! + for (int frame = frameStart; frame < frameEnd; frame++) + { + const long seek = getRecordingBlockSeekPoint(frame) + controllerInputBytes * port; + fseek(recordingFile, seek, SEEK_SET); + if (fread(&padBytes, 1, padBytes.size(), recordingFile)) + { + PadData frameData; + for (int i = 0; i < padBytes.size(); i++) + { + frameData.UpdateControllerData(i, padBytes.at(i)); + } + data.push_back(frameData); + } + } + + return data; +} + long InputRecordingFile::getRecordingBlockSeekPoint(const long& frame) { return headerSize + sizeof(bool) + frame * inputBytesPerFrame; @@ -439,7 +485,7 @@ bool InputRecordingFile::verifyRecordingFileHeader() // Check for current verison if (header.version != 1) { - inputRec::consoleLog(fmt::format("Input recording file is not a supported version - {}", header.version)); + InputRec::consoleLog(fmt::format("Input recording file is not a supported version - {}", header.version)); return false; } return true; diff --git a/pcsx2/Recording/InputRecordingFile.h b/pcsx2/Recording/InputRecordingFile.h index bc96ef2851..c12f5444f0 100644 --- a/pcsx2/Recording/InputRecordingFile.h +++ b/pcsx2/Recording/InputRecordingFile.h @@ -150,15 +150,10 @@ public: // Closes the underlying input recording file, writing the header and // prepares for a possible new recording to be started bool Close(); - // Retrieve the input recording's filename (not the path) - const std::string& GetFilename(); - // Retrieve the input recording's header which contains high-level metadata on the recording - InputRecordingFileHeader& GetHeader(); - // The maximum number of frames, or in other words, the length of the recording - long& GetTotalFrames(); + // The number of times a save-state has been loaded while recording this movie // this is also often referred to as a "re-record" - unsigned long& GetUndoCount(); + // Whether or not this input recording starts by loading a save-state or by booting the game fresh bool FromSaveState(); // Increment the number of undo actions and commit it to the recording file @@ -178,6 +173,16 @@ public: // 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); + + // 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; + + void logRecordingMetadata(); + std::vector bulkReadPadData(long frameStart, long frameEnd, const uint port); + private: static const int controllerPortsSupported = 2; static const int controllerInputBytes = 18; diff --git a/pcsx2/Recording/Utilities/InputRecordingLogger.cpp b/pcsx2/Recording/Utilities/InputRecordingLogger.cpp index 02baf3e243..7cb8370e6d 100644 --- a/pcsx2/Recording/Utilities/InputRecordingLogger.cpp +++ b/pcsx2/Recording/Utilities/InputRecordingLogger.cpp @@ -19,38 +19,40 @@ #include "DebugTools/Debug.h" #include "common/Console.h" -#include "GS.h" // GSosdlog +#include "GS.h" #include "Host.h" #include -namespace inputRec +namespace InputRec { void log(const std::string& log) { - if (log.empty()) - return; - - recordingConLog(fmt::format("[REC]: {}\n", log)); - - // NOTE - Color is not currently used for OSD logs - Host::AddOSDMessage(log, 15.0f); + if (!log.empty()) + { + recordingConLog(fmt::format("[REC]: {}\n", log)); + Host::AddOSDMessage(log, 15.0f); + } } void consoleLog(const std::string& log) { - if (log.empty()) - return; - - recordingConLog(fmt::format("[REC]: {}\n", log)); + if (!log.empty()) + { + recordingConLog(fmt::format("[REC]: {}\n", log)); + } } void consoleMultiLog(const std::vector& logs) { - std::string log; - for (std::string l : logs) - log.append(fmt::format("[REC]: {}\n", l)); - - recordingConLog(log); + if (!logs.empty()) + { + std::string log; + for (std::string l : logs) + { + log.append(fmt::format("[REC]: {}\n", l)); + } + recordingConLog(log); + } } -} // namespace inputRec +} // namespace InputRecording diff --git a/pcsx2/Recording/Utilities/InputRecordingLogger.h b/pcsx2/Recording/Utilities/InputRecordingLogger.h index d1d275816d..6d2935c877 100644 --- a/pcsx2/Recording/Utilities/InputRecordingLogger.h +++ b/pcsx2/Recording/Utilities/InputRecordingLogger.h @@ -19,7 +19,7 @@ #include #include -namespace inputRec +namespace InputRec { void log(const std::string& log); void consoleLog(const std::string& log);