From ec4fc7171f645d12014a5a2666c2242bd84d2477 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Thu, 16 Jul 2020 00:58:00 +0200 Subject: [PATCH] GCMemcardUtils: Implement GenerateFilename() as a cleaner variant of GCMemcard::GCI_FileName(). --- .../Core/Core/HW/GCMemcard/GCMemcardUtils.cpp | 21 +++++++++++++++++++ .../Core/Core/HW/GCMemcard/GCMemcardUtils.h | 3 +++ 2 files changed, 24 insertions(+) diff --git a/Source/Core/Core/HW/GCMemcard/GCMemcardUtils.cpp b/Source/Core/Core/HW/GCMemcard/GCMemcardUtils.cpp index c603ca83f2..5ef387a4f4 100644 --- a/Source/Core/Core/HW/GCMemcard/GCMemcardUtils.cpp +++ b/Source/Core/Core/HW/GCMemcard/GCMemcardUtils.cpp @@ -6,6 +6,7 @@ #include "Common/CommonTypes.h" #include "Common/IOFile.h" +#include "Common/NandPaths.h" #include "Core/HW/GCMemcard/GCMemcard.h" @@ -283,4 +284,24 @@ bool WriteSavefile(const std::string& filename, const Savefile& savefile, Savefi return false; } } + +std::string GenerateFilename(const DEntry& entry) +{ + std::string maker(reinterpret_cast(entry.m_makercode.data()), + entry.m_makercode.size()); + std::string gamecode(reinterpret_cast(entry.m_gamecode.data()), + entry.m_gamecode.size()); + + // prevent going out of bounds when all bytes of m_filename are non-null + size_t length = 0; + for (size_t i = 0; i < entry.m_filename.size(); ++i) + { + if (entry.m_filename[i] == 0) + break; + ++length; + } + std::string filename(reinterpret_cast(entry.m_filename.data()), length); + + return Common::EscapeFileName(maker + '-' + gamecode + '-' + filename); +} } // namespace Memcard diff --git a/Source/Core/Core/HW/GCMemcard/GCMemcardUtils.h b/Source/Core/Core/HW/GCMemcard/GCMemcardUtils.h index 3734d03d28..1042c439fc 100644 --- a/Source/Core/Core/HW/GCMemcard/GCMemcardUtils.h +++ b/Source/Core/Core/HW/GCMemcard/GCMemcardUtils.h @@ -33,4 +33,7 @@ enum class SavefileFormat // Writes a Gamecube memory card savefile to a file. bool WriteSavefile(const std::string& filename, const Savefile& savefile, SavefileFormat format); + +// Generates a filename (without extension) for the given directory entry. +std::string GenerateFilename(const DEntry& entry); } // namespace Memcard