Movie: Replace some C style arrays with std::array

This replaces some C style arrays with std::array to make copying
and comparing arrays slightly nicer.
This commit is contained in:
Léo Lam 2018-04-08 13:10:22 +02:00
parent 58b96eeb9d
commit 75d056bc48
3 changed files with 34 additions and 37 deletions

View File

@ -29,8 +29,7 @@ static void LoadFromDTM(Config::Layer* config_layer, Movie::DTMHeader* dtm)
config_layer->Set(Config::MAIN_FAST_DISC_SPEED, dtm->bFastDiscSpeed); config_layer->Set(Config::MAIN_FAST_DISC_SPEED, dtm->bFastDiscSpeed);
config_layer->Set(Config::MAIN_CPU_CORE, static_cast<int>(dtm->CPUCore)); config_layer->Set(Config::MAIN_CPU_CORE, static_cast<int>(dtm->CPUCore));
config_layer->Set(Config::MAIN_SYNC_GPU, dtm->bSyncGPU); config_layer->Set(Config::MAIN_SYNC_GPU, dtm->bSyncGPU);
config_layer->Set(Config::MAIN_GFX_BACKEND, config_layer->Set(Config::MAIN_GFX_BACKEND, std::string(dtm->videoBackend.data()));
std::string(reinterpret_cast<char*>(dtm->videoBackend)));
config_layer->Set(Config::SYSCONF_PROGRESSIVE_SCAN, dtm->bProgressive); config_layer->Set(Config::SYSCONF_PROGRESSIVE_SCAN, dtm->bProgressive);
config_layer->Set(Config::SYSCONF_PAL60, dtm->bPAL60); config_layer->Set(Config::SYSCONF_PAL60, dtm->bPAL60);
@ -73,8 +72,7 @@ void SaveToDTM(Movie::DTMHeader* dtm)
dtm->bEFBCopyEnable = true; dtm->bEFBCopyEnable = true;
dtm->bEFBCopyCacheEnable = false; dtm->bEFBCopyCacheEnable = false;
strncpy(reinterpret_cast<char*>(dtm->videoBackend), video_backend.c_str(), strncpy(dtm->videoBackend.data(), video_backend.c_str(), dtm->videoBackend.size());
ArraySize(dtm->videoBackend));
} }
// TODO: Future project, let this support all the configuration options. // TODO: Future project, let this support all the configuration options.

View File

@ -84,9 +84,9 @@ static bool s_bDiscChange = false;
static bool s_bReset = false; static bool s_bReset = false;
static std::string s_author = ""; static std::string s_author = "";
static std::string s_discChange = ""; static std::string s_discChange = "";
static u8 s_MD5[16]; static std::array<u8, 16> s_MD5;
static u8 s_bongos, s_memcards; static u8 s_bongos, s_memcards;
static u8 s_revision[20]; static std::array<u8, 20> s_revision;
static u32 s_DSPiromHash = 0; static u32 s_DSPiromHash = 0;
static u32 s_DSPcoefHash = 0; static u32 s_DSPcoefHash = 0;
@ -103,7 +103,7 @@ static WiiManipFunction s_wii_manip_func;
static std::string s_current_file_name; static std::string s_current_file_name;
static void GetSettings(); static void GetSettings();
static bool IsMovieHeader(u8 magic[4]) static bool IsMovieHeader(const std::array<u8, 4>& magic)
{ {
return magic[0] == 'D' && magic[1] == 'T' && magic[2] == 'M' && magic[3] == 0x1A; return magic[0] == 'D' && magic[1] == 'T' && magic[2] == 'M' && magic[3] == 0x1A;
} }
@ -215,10 +215,10 @@ void Init(const BootParameters& boot)
ReadHeader(); ReadHeader();
std::thread md5thread(CheckMD5); std::thread md5thread(CheckMD5);
md5thread.detach(); md5thread.detach();
if (strncmp(tmpHeader.gameID, SConfig::GetInstance().GetGameID().c_str(), 6)) if (strncmp(tmpHeader.gameID.data(), SConfig::GetInstance().GetGameID().c_str(), 6))
{ {
PanicAlertT("The recorded game (%s) is not the same as the selected game (%s)", PanicAlertT("The recorded game (%s) is not the same as the selected game (%s)",
tmpHeader.gameID, SConfig::GetInstance().GetGameID().c_str()); tmpHeader.gameID.data(), SConfig::GetInstance().GetGameID().c_str());
EndPlayInput(false); EndPlayInput(false);
} }
} }
@ -848,16 +848,16 @@ void ReadHeader()
s_memcards = tmpHeader.memcards; s_memcards = tmpHeader.memcards;
s_bongos = tmpHeader.bongos; s_bongos = tmpHeader.bongos;
s_bNetPlay = tmpHeader.bNetPlay; s_bNetPlay = tmpHeader.bNetPlay;
memcpy(s_revision, tmpHeader.revision, ArraySize(s_revision)); s_revision = tmpHeader.revision;
} }
else else
{ {
GetSettings(); GetSettings();
} }
s_discChange = (char*)tmpHeader.discChange; s_discChange = {tmpHeader.discChange.begin(), tmpHeader.discChange.end()};
s_author = (char*)tmpHeader.author; s_author = {tmpHeader.author.begin(), tmpHeader.author.end()};
memcpy(s_MD5, tmpHeader.md5, 16); s_MD5 = tmpHeader.md5;
s_DSPiromHash = tmpHeader.DSPiromHash; s_DSPiromHash = tmpHeader.DSPiromHash;
s_DSPcoefHash = tmpHeader.DSPcoefHash; s_DSPcoefHash = tmpHeader.DSPcoefHash;
} }
@ -1298,7 +1298,7 @@ void SaveRecording(const std::string& filename)
header.filetype[1] = 'T'; header.filetype[1] = 'T';
header.filetype[2] = 'M'; header.filetype[2] = 'M';
header.filetype[3] = 0x1A; header.filetype[3] = 0x1A;
strncpy(header.gameID, SConfig::GetInstance().GetGameID().c_str(), 6); strncpy(header.gameID.data(), SConfig::GetInstance().GetGameID().c_str(), 6);
header.bWii = SConfig::GetInstance().bWii; header.bWii = SConfig::GetInstance().bWii;
header.controllers = s_controllers & (SConfig::GetInstance().bWii ? 0xFF : 0x0F); header.controllers = s_controllers & (SConfig::GetInstance().bWii ? 0xFF : 0x0F);
@ -1314,11 +1314,11 @@ void SaveRecording(const std::string& filename)
header.memcards = s_memcards; header.memcards = s_memcards;
header.bClearSave = s_bClearSave; header.bClearSave = s_bClearSave;
header.bNetPlay = s_bNetPlay; header.bNetPlay = s_bNetPlay;
strncpy((char*)header.discChange, s_discChange.c_str(), ArraySize(header.discChange)); strncpy(header.discChange.data(), s_discChange.c_str(), header.discChange.size());
strncpy((char*)header.author, s_author.c_str(), ArraySize(header.author)); strncpy(header.author.data(), s_author.c_str(), header.author.size());
memcpy(header.md5, s_MD5, 16); header.md5 = s_MD5;
header.bongos = s_bongos; header.bongos = s_bongos;
memcpy(header.revision, s_revision, ArraySize(header.revision)); header.revision = s_revision;
header.DSPiromHash = s_DSPiromHash; header.DSPiromHash = s_DSPiromHash;
header.DSPcoefHash = s_DSPcoefHash; header.DSPcoefHash = s_DSPcoefHash;
header.tickCount = s_totalTickCount; header.tickCount = s_totalTickCount;
@ -1400,8 +1400,7 @@ void GetSettings()
SConfig::GetInstance().m_EXIDevice[1] == ExpansionInterface::EXIDEVICE_MEMORYCARDFOLDER) SConfig::GetInstance().m_EXIDevice[1] == ExpansionInterface::EXIDEVICE_MEMORYCARDFOLDER)
<< 1; << 1;
std::array<u8, 20> revision = ConvertGitRevisionToBytes(Common::scm_rev_git_str); s_revision = ConvertGitRevisionToBytes(Common::scm_rev_git_str);
std::copy(std::begin(revision), std::end(revision), std::begin(s_revision));
if (!Config::Get(Config::MAIN_DSP_HLE)) if (!Config::Get(Config::MAIN_DSP_HLE))
{ {
@ -1455,10 +1454,10 @@ static void CheckMD5()
} }
Core::DisplayMessage("Verifying checksum...", 2000); Core::DisplayMessage("Verifying checksum...", 2000);
unsigned char gameMD5[16]; std::array<u8, 16> game_md5;
mbedtls_md_file(s_md5_info, s_current_file_name.c_str(), gameMD5); mbedtls_md_file(s_md5_info, s_current_file_name.c_str(), game_md5.data());
if (memcmp(gameMD5, s_MD5, 16) == 0) if (game_md5 == s_MD5)
Core::DisplayMessage("Checksum of current game matches the recorded game.", 2000); Core::DisplayMessage("Checksum of current game matches the recorded game.", 2000);
else else
Core::DisplayMessage("Checksum of current game does not match the recorded game!", 3000); Core::DisplayMessage("Checksum of current game does not match the recorded game!", 3000);
@ -1471,8 +1470,7 @@ static void GetMD5()
return; return;
Core::DisplayMessage("Calculating checksum of game file...", 2000); Core::DisplayMessage("Calculating checksum of game file...", 2000);
memset(s_MD5, 0, sizeof(s_MD5)); mbedtls_md_file(s_md5_info, s_current_file_name.c_str(), s_MD5.data());
mbedtls_md_file(s_md5_info, s_current_file_name.c_str(), s_MD5);
Core::DisplayMessage("Finished calculating checksum.", 2000); Core::DisplayMessage("Finished calculating checksum.", 2000);
} }

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <array>
#include <functional> #include <functional>
#include <optional> #include <optional>
#include <string> #include <string>
@ -58,10 +59,10 @@ static_assert(sizeof(ControllerState) == 8, "ControllerState should be 8 bytes")
#pragma pack(push, 1) #pragma pack(push, 1)
struct DTMHeader struct DTMHeader
{ {
u8 filetype[4]; // Unique Identifier (always "DTM"0x1A) std::array<u8, 4> filetype; // Unique Identifier (always "DTM"0x1A)
char gameID[6]; // The Game ID std::array<char, 6> gameID; // The Game ID
bool bWii; // Wii game bool bWii; // Wii game
u8 controllers; // Controllers plugged in (from least to most significant, u8 controllers; // Controllers plugged in (from least to most significant,
// the bits are GC controllers 1-4 and Wiimotes 1-4) // the bits are GC controllers 1-4 and Wiimotes 1-4)
@ -73,11 +74,11 @@ struct DTMHeader
u64 lagCount; // Number of lag frames in the recording u64 lagCount; // Number of lag frames in the recording
u64 uniqueID; // (not implemented) A Unique ID comprised of: md5(time + Game ID) u64 uniqueID; // (not implemented) A Unique ID comprised of: md5(time + Game ID)
u32 numRerecords; // Number of rerecords/'cuts' of this TAS u32 numRerecords; // Number of rerecords/'cuts' of this TAS
u8 author[32]; // Author's name (encoded in UTF-8) std::array<char, 32> author; // Author's name (encoded in UTF-8)
u8 videoBackend[16]; // UTF-8 representation of the video backend std::array<char, 16> videoBackend; // UTF-8 representation of the video backend
u8 audioEmulator[16]; // UTF-8 representation of the audio emulator std::array<char, 16> audioEmulator; // UTF-8 representation of the audio emulator
u8 md5[16]; // MD5 of game iso std::array<u8, 16> md5; // MD5 of game iso
u64 recordingStartTime; // seconds since 1970 that recording started (used for RTC) u64 recordingStartTime; // seconds since 1970 that recording started (used for RTC)
@ -102,13 +103,13 @@ struct DTMHeader
bool bNetPlay; bool bNetPlay;
bool bPAL60; bool bPAL60;
u8 language; u8 language;
u8 reserved[11]; // Padding for any new config options std::array<u8, 11> reserved; // Padding for any new config options
u8 discChange[40]; // Name of iso file to switch to, for two disc games. std::array<char, 40> discChange; // Name of iso file to switch to, for two disc games.
u8 revision[20]; // Git hash std::array<u8, 20> revision; // Git hash
u32 DSPiromHash; u32 DSPiromHash;
u32 DSPcoefHash; u32 DSPcoefHash;
u64 tickCount; // Number of ticks in the recording u64 tickCount; // Number of ticks in the recording
u8 reserved2[11]; // Make heading 256 bytes, just because we can std::array<u8, 11> reserved2; // Make heading 256 bytes, just because we can
}; };
static_assert(sizeof(DTMHeader) == 256, "DTMHeader should be 256 bytes"); static_assert(sizeof(DTMHeader) == 256, "DTMHeader should be 256 bytes");