Skip to content

Commit

Permalink
WiiSave: Use an enum class for save file type
Browse files Browse the repository at this point in the history
  • Loading branch information
leoetlino committed Jun 1, 2018
1 parent 9495367 commit 8af16fd
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions Source/Core/Core/HW/WiiSave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ class Storage
public:
struct SaveFile
{
u8 mode, attributes, type;
enum class Type : u8
{
File = 1,
Directory = 2,
};
u8 mode, attributes;
Type type;
std::string path;
// Only valid for regular (i.e. non-directory) files.
Common::Lazy<std::optional<std::vector<u8>>> data;
Expand Down Expand Up @@ -192,7 +198,7 @@ class NandStorage final : public Storage
SaveFile save_file;
save_file.mode = 0x3c;
save_file.attributes = 0;
save_file.type = file_info.IsDirectory() ? 2 : 1;
save_file.type = file_info.IsDirectory() ? SaveFile::Type::Directory : SaveFile::Type::File;
save_file.path = Common::UnescapeFileName(path.substr(m_wii_title_path.length() + 1));
save_file.data = [path]() -> std::optional<std::vector<u8>> {
File::IOFile file{path, "rb"};
Expand Down Expand Up @@ -224,15 +230,15 @@ class NandStorage final : public Storage
std::string file_path_full = m_wii_title_path + '/' + file_path;
File::CreateFullPath(file_path_full);

if (file.type == 1)
if (file.type == SaveFile::Type::File)
{
File::IOFile raw_save_file(file_path_full, "wb");
const std::optional<std::vector<u8>>& data = *file.data;
if (!data)
return false;
raw_save_file.WriteBytes(data->data(), data->size());
}
else if (file.type == 2)
else if (file.type == SaveFile::Type::Directory)
{
File::CreateDir(file_path_full);
if (!File::IsDirectory(file_path_full))
Expand Down Expand Up @@ -360,9 +366,12 @@ class DataBinStorage final : public Storage

save_file.mode = file_hdr.permissions;
save_file.attributes = file_hdr.attrib;
save_file.type = file_hdr.type;
const SaveFile::Type type = static_cast<SaveFile::Type>(file_hdr.type);
if (type != SaveFile::Type::Directory && type != SaveFile::Type::File)
return {};
save_file.type = type;
save_file.path = file_hdr.name.data();
if (file_hdr.type == 1)
if (type == SaveFile::Type::File)
{
const u32 rounded_size = Common::AlignUp<u32>(file_hdr.size, BLOCK_SZ);
const u64 pos = m_file.Tell();
Expand Down Expand Up @@ -409,7 +418,7 @@ class DataBinStorage final : public Storage
file_hdr.magic = FILE_HDR_MAGIC;
file_hdr.permissions = save_file.mode;
file_hdr.attrib = save_file.attributes;
file_hdr.type = save_file.type;
file_hdr.type = static_cast<u8>(save_file.type);
if (save_file.path.length() > 0x44)
return false;
std::strncpy(file_hdr.name.data(), save_file.path.data(), file_hdr.name.size());
Expand Down

0 comments on commit 8af16fd

Please sign in to comment.