From 75d056bc486f01af48b2e3942a44bd9cbc8332f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Sun, 8 Apr 2018 13:10:22 +0200 Subject: [PATCH] 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. --- .../Core/ConfigLoaders/MovieConfigLoader.cpp | 6 +-- Source/Core/Core/Movie.cpp | 40 +++++++++---------- Source/Core/Core/Movie.h | 25 ++++++------ 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/Source/Core/Core/ConfigLoaders/MovieConfigLoader.cpp b/Source/Core/Core/ConfigLoaders/MovieConfigLoader.cpp index 36f7c12c2b..c0975fa57e 100644 --- a/Source/Core/Core/ConfigLoaders/MovieConfigLoader.cpp +++ b/Source/Core/Core/ConfigLoaders/MovieConfigLoader.cpp @@ -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_CPU_CORE, static_cast(dtm->CPUCore)); config_layer->Set(Config::MAIN_SYNC_GPU, dtm->bSyncGPU); - config_layer->Set(Config::MAIN_GFX_BACKEND, - std::string(reinterpret_cast(dtm->videoBackend))); + config_layer->Set(Config::MAIN_GFX_BACKEND, std::string(dtm->videoBackend.data())); config_layer->Set(Config::SYSCONF_PROGRESSIVE_SCAN, dtm->bProgressive); config_layer->Set(Config::SYSCONF_PAL60, dtm->bPAL60); @@ -73,8 +72,7 @@ void SaveToDTM(Movie::DTMHeader* dtm) dtm->bEFBCopyEnable = true; dtm->bEFBCopyCacheEnable = false; - strncpy(reinterpret_cast(dtm->videoBackend), video_backend.c_str(), - ArraySize(dtm->videoBackend)); + strncpy(dtm->videoBackend.data(), video_backend.c_str(), dtm->videoBackend.size()); } // TODO: Future project, let this support all the configuration options. diff --git a/Source/Core/Core/Movie.cpp b/Source/Core/Core/Movie.cpp index 980e75ee82..670949ccaf 100644 --- a/Source/Core/Core/Movie.cpp +++ b/Source/Core/Core/Movie.cpp @@ -84,9 +84,9 @@ static bool s_bDiscChange = false; static bool s_bReset = false; static std::string s_author = ""; static std::string s_discChange = ""; -static u8 s_MD5[16]; +static std::array s_MD5; static u8 s_bongos, s_memcards; -static u8 s_revision[20]; +static std::array s_revision; static u32 s_DSPiromHash = 0; static u32 s_DSPcoefHash = 0; @@ -103,7 +103,7 @@ static WiiManipFunction s_wii_manip_func; static std::string s_current_file_name; static void GetSettings(); -static bool IsMovieHeader(u8 magic[4]) +static bool IsMovieHeader(const std::array& magic) { return magic[0] == 'D' && magic[1] == 'T' && magic[2] == 'M' && magic[3] == 0x1A; } @@ -215,10 +215,10 @@ void Init(const BootParameters& boot) ReadHeader(); std::thread md5thread(CheckMD5); 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)", - tmpHeader.gameID, SConfig::GetInstance().GetGameID().c_str()); + tmpHeader.gameID.data(), SConfig::GetInstance().GetGameID().c_str()); EndPlayInput(false); } } @@ -848,16 +848,16 @@ void ReadHeader() s_memcards = tmpHeader.memcards; s_bongos = tmpHeader.bongos; s_bNetPlay = tmpHeader.bNetPlay; - memcpy(s_revision, tmpHeader.revision, ArraySize(s_revision)); + s_revision = tmpHeader.revision; } else { GetSettings(); } - s_discChange = (char*)tmpHeader.discChange; - s_author = (char*)tmpHeader.author; - memcpy(s_MD5, tmpHeader.md5, 16); + s_discChange = {tmpHeader.discChange.begin(), tmpHeader.discChange.end()}; + s_author = {tmpHeader.author.begin(), tmpHeader.author.end()}; + s_MD5 = tmpHeader.md5; s_DSPiromHash = tmpHeader.DSPiromHash; s_DSPcoefHash = tmpHeader.DSPcoefHash; } @@ -1298,7 +1298,7 @@ void SaveRecording(const std::string& filename) header.filetype[1] = 'T'; header.filetype[2] = 'M'; 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.controllers = s_controllers & (SConfig::GetInstance().bWii ? 0xFF : 0x0F); @@ -1314,11 +1314,11 @@ void SaveRecording(const std::string& filename) header.memcards = s_memcards; header.bClearSave = s_bClearSave; header.bNetPlay = s_bNetPlay; - strncpy((char*)header.discChange, s_discChange.c_str(), ArraySize(header.discChange)); - strncpy((char*)header.author, s_author.c_str(), ArraySize(header.author)); - memcpy(header.md5, s_MD5, 16); + strncpy(header.discChange.data(), s_discChange.c_str(), header.discChange.size()); + strncpy(header.author.data(), s_author.c_str(), header.author.size()); + header.md5 = s_MD5; header.bongos = s_bongos; - memcpy(header.revision, s_revision, ArraySize(header.revision)); + header.revision = s_revision; header.DSPiromHash = s_DSPiromHash; header.DSPcoefHash = s_DSPcoefHash; header.tickCount = s_totalTickCount; @@ -1400,8 +1400,7 @@ void GetSettings() SConfig::GetInstance().m_EXIDevice[1] == ExpansionInterface::EXIDEVICE_MEMORYCARDFOLDER) << 1; - std::array revision = ConvertGitRevisionToBytes(Common::scm_rev_git_str); - std::copy(std::begin(revision), std::end(revision), std::begin(s_revision)); + s_revision = ConvertGitRevisionToBytes(Common::scm_rev_git_str); if (!Config::Get(Config::MAIN_DSP_HLE)) { @@ -1455,10 +1454,10 @@ static void CheckMD5() } Core::DisplayMessage("Verifying checksum...", 2000); - unsigned char gameMD5[16]; - mbedtls_md_file(s_md5_info, s_current_file_name.c_str(), gameMD5); + std::array game_md5; + 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); else Core::DisplayMessage("Checksum of current game does not match the recorded game!", 3000); @@ -1471,8 +1470,7 @@ static void GetMD5() return; 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); + mbedtls_md_file(s_md5_info, s_current_file_name.c_str(), s_MD5.data()); Core::DisplayMessage("Finished calculating checksum.", 2000); } diff --git a/Source/Core/Core/Movie.h b/Source/Core/Core/Movie.h index 14d8fdf3c8..f8d1351151 100644 --- a/Source/Core/Core/Movie.h +++ b/Source/Core/Core/Movie.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include #include @@ -58,10 +59,10 @@ static_assert(sizeof(ControllerState) == 8, "ControllerState should be 8 bytes") #pragma pack(push, 1) struct DTMHeader { - u8 filetype[4]; // Unique Identifier (always "DTM"0x1A) + std::array filetype; // Unique Identifier (always "DTM"0x1A) - char gameID[6]; // The Game ID - bool bWii; // Wii game + std::array gameID; // The Game ID + bool bWii; // Wii game u8 controllers; // Controllers plugged in (from least to most significant, // 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 uniqueID; // (not implemented) A Unique ID comprised of: md5(time + Game ID) u32 numRerecords; // Number of rerecords/'cuts' of this TAS - u8 author[32]; // Author's name (encoded in UTF-8) + std::array author; // Author's name (encoded in UTF-8) - u8 videoBackend[16]; // UTF-8 representation of the video backend - u8 audioEmulator[16]; // UTF-8 representation of the audio emulator - u8 md5[16]; // MD5 of game iso + std::array videoBackend; // UTF-8 representation of the video backend + std::array audioEmulator; // UTF-8 representation of the audio emulator + std::array md5; // MD5 of game iso u64 recordingStartTime; // seconds since 1970 that recording started (used for RTC) @@ -102,13 +103,13 @@ struct DTMHeader bool bNetPlay; bool bPAL60; u8 language; - u8 reserved[11]; // Padding for any new config options - u8 discChange[40]; // Name of iso file to switch to, for two disc games. - u8 revision[20]; // Git hash + std::array reserved; // Padding for any new config options + std::array discChange; // Name of iso file to switch to, for two disc games. + std::array revision; // Git hash u32 DSPiromHash; u32 DSPcoefHash; - u64 tickCount; // Number of ticks in the recording - u8 reserved2[11]; // Make heading 256 bytes, just because we can + u64 tickCount; // Number of ticks in the recording + std::array reserved2; // Make heading 256 bytes, just because we can }; static_assert(sizeof(DTMHeader) == 256, "DTMHeader should be 256 bytes");