input-rec: move IRFileHeader & IRSavestate privately into the IRFile class

+ Add getters for the header char array variables. No longer allow direct access to header content outside of the class.
This commit is contained in:
sonicfind 2022-09-24 22:07:26 -05:00 committed by refractionpcsx2
parent 7987430825
commit af1e41681c
3 changed files with 54 additions and 47 deletions

View File

@ -533,9 +533,9 @@ bool InputRecording::create(const std::string& fileName, const bool fromSaveStat
VMManager::Reset();
}
m_file.getHeader().SetEmulatorVersion();
m_file.getHeader().SetAuthor(authorName);
m_file.getHeader().SetGameName(resolveGameName());
m_file.SetEmulatorVersion();
m_file.SetAuthor(authorName);
m_file.SetGameName(resolveGameName());
m_file.WriteHeader();
initializeState();
InputRec::log("Started new input recording");
@ -586,9 +586,9 @@ bool InputRecording::play(const std::string& filename)
initializeState();
InputRec::log("Replaying input recording");
m_file.logRecordingMetadata();
if (resolveGameName() != m_file.getHeader().m_gameName)
if (resolveGameName() != m_file.getGameName())
{
InputRec::consoleLog(fmt::format("Input recording was possibly constructed for a different game. Expected: {}, Actual: {}", m_file.getHeader().m_gameName, resolveGameName()));
InputRec::consoleLog(fmt::format("Input recording was possibly constructed for a different game. Expected: {}, Actual: {}", m_file.getGameName(), resolveGameName()));
}
return true;

View File

@ -245,25 +245,40 @@ bool InputRecordingFile::verifyRecordingFileHeader()
#include <vector>
#include <array>
void InputRecordingFileHeader::Init() noexcept
void InputRecordingFile::InputRecordingFileHeader::Init() noexcept
{
m_fileVersion = 1;
}
void InputRecordingFileHeader::SetEmulatorVersion()
void InputRecordingFile::SetEmulatorVersion()
{
static const std::string emuVersion = fmt::format("PCSX2-{}.{}.{}", PCSX2_VersionHi, PCSX2_VersionMid, PCSX2_VersionLo);
strncpy(m_emulatorVersion, emuVersion.c_str(), sizeof(m_emulatorVersion) - 1);
strncpy(m_header.m_emulatorVersion, emuVersion.c_str(), sizeof(m_header.m_emulatorVersion) - 1);
}
void InputRecordingFileHeader::SetAuthor(const std::string& _author)
void InputRecordingFile::SetAuthor(const std::string& _author)
{
strncpy(m_author, _author.data(), sizeof(m_author) - 1);
strncpy(m_header.m_author, _author.data(), sizeof(m_header.m_author) - 1);
}
void InputRecordingFileHeader::SetGameName(const std::string& _gameName)
void InputRecordingFile::SetGameName(const std::string& _gameName)
{
strncpy(m_gameName, _gameName.data(), sizeof(m_gameName) - 1);
strncpy(m_header.m_gameName, _gameName.data(), sizeof(m_header.m_gameName) - 1);
}
const char* InputRecordingFile::getEmulatorVersion() const noexcept
{
return m_header.m_emulatorVersion;
}
const char* InputRecordingFile::getAuthor() const noexcept
{
return m_header.m_author;
}
const char* InputRecordingFile::getGameName() const noexcept
{
return m_header.m_gameName;
}
bool InputRecordingFile::Close() noexcept
@ -283,11 +298,6 @@ const std::string& InputRecordingFile::getFilename() const noexcept
return m_filename;
}
InputRecordingFileHeader& InputRecordingFile::getHeader() noexcept
{
return m_header;
}
unsigned long InputRecordingFile::getTotalFrames() const noexcept
{
return m_totalFrames;
@ -300,7 +310,7 @@ unsigned long InputRecordingFile::getUndoCount() const noexcept
bool InputRecordingFile::FromSaveState() const noexcept
{
return m_savestate.fromSavestate;
return m_savestate;
}
void InputRecordingFile::IncrementUndoCount()
@ -326,7 +336,7 @@ bool InputRecordingFile::OpenNew(const std::string& path, bool fromSavestate)
m_totalFrames = 0;
m_undoCount = 0;
m_header.Init();
m_savestate.fromSavestate = fromSavestate;
m_savestate = fromSavestate;
return true;
}
@ -415,10 +425,10 @@ bool InputRecordingFile::WriteKeyBuffer(const uint frame, const uint port, const
void InputRecordingFile::logRecordingMetadata()
{
InputRec::consoleMultiLog({fmt::format("File: {}", getFilename()),
fmt::format("PCSX2 Version Used: {}", getHeader().m_emulatorVersion),
fmt::format("Recording File Version: {}", getHeader().m_fileVersion),
fmt::format("Associated Game Name or ISO Filename: {}", getHeader().m_gameName),
fmt::format("Author: {}", getHeader().m_author),
fmt::format("PCSX2 Version Used: {}",m_header.m_emulatorVersion),
fmt::format("Recording File Version: {}", m_header.m_fileVersion),
fmt::format("Associated Game Name or ISO Filename: {}", m_header.m_gameName),
fmt::format("Author: {}", m_header.m_author),
fmt::format("Total Frames: {}", getTotalFrames()),
fmt::format("Undo Count: {}", getUndoCount())});
}
@ -477,7 +487,7 @@ bool InputRecordingFile::verifyRecordingFileHeader()
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)
fread(&m_savestate, sizeof(bool), 1, m_recordingFile) != 1)
{
return false;
}

View File

@ -119,32 +119,31 @@ private:
// NOTE / TODOs for Version 2
// - Move fromSavestate, undoCount, and total frames into the header
struct InputRecordingFileHeader
{
u8 m_fileVersion = 1;
char m_emulatorVersion[50]{};
char m_author[255]{};
char m_gameName[255]{};
public:
void SetEmulatorVersion();
void Init() noexcept;
void SetAuthor(const std::string& author);
void SetGameName(const std::string& cdrom);
};
// DEPRECATED / Slated for Removal
struct InputRecordingSavestate
{
// Whether we start from the savestate or from power-on
bool fromSavestate = false;
};
// Handles all operations on the input recording file
class InputRecordingFile
{
struct InputRecordingFileHeader
{
u8 m_fileVersion = 1;
char m_emulatorVersion[50]{};
char m_author[255]{};
char m_gameName[255]{};
public:
void Init() noexcept;
} m_header;
public:
void SetEmulatorVersion();
void SetAuthor(const std::string& author);
void SetGameName(const std::string& cdrom);
const char* getEmulatorVersion() const noexcept;
const char* getAuthor() const noexcept;
const char* getGameName() const noexcept;
~InputRecordingFile() { Close(); }
// Closes the underlying input recording file, writing the header and
@ -176,7 +175,6 @@ public:
// Retrieve the input recording's filename (not the path)
const std::string& getFilename() const noexcept;
InputRecordingFileHeader& getHeader() noexcept;
unsigned long getTotalFrames() const noexcept;
unsigned long getUndoCount() const noexcept;
@ -196,10 +194,9 @@ private:
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 = "";
FILE* m_recordingFile = nullptr;
InputRecordingSavestate m_savestate;
bool m_savestate = false;
// An signed 32-bit frame limit is equivalent to 1.13 years of continuous 60fps footage
unsigned long m_totalFrames = 0;