diff --git a/src/LibHac/IO/Save/HierarchicalSaveFileTable.cs b/src/LibHac/IO/Save/HierarchicalSaveFileTable.cs index e7d22f9f..fd7b9ed9 100644 --- a/src/LibHac/IO/Save/HierarchicalSaveFileTable.cs +++ b/src/LibHac/IO/Save/HierarchicalSaveFileTable.cs @@ -5,13 +5,13 @@ namespace LibHac.IO.Save { public class HierarchicalSaveFileTable { - private SaveFsList FileTable { get; } - private SaveFsList DirectoryTable { get; } + private SaveFsList> FileTable { get; } + private SaveFsList> DirectoryTable { get; } public HierarchicalSaveFileTable(IStorage dirTable, IStorage fileTable) { - FileTable = new SaveFsList(fileTable); - DirectoryTable = new SaveFsList(dirTable); + FileTable = new SaveFsList>(fileTable); + DirectoryTable = new SaveFsList>(dirTable); } public bool TryOpenFile(string path, out SaveFileInfo fileInfo) @@ -22,9 +22,9 @@ namespace LibHac.IO.Save return false; } - if (FileTable.TryGetValue(ref key, out FileSaveEntry value)) + if (FileTable.TryGetValue(ref key, out TableEntry value)) { - fileInfo = value.Info; + fileInfo = value.Value; return true; } @@ -43,7 +43,7 @@ namespace LibHac.IO.Save Span nameBytes = stackalloc byte[FileTable.MaxNameLength]; - bool success = FileTable.TryGetValue((int)position.NextFile, out FileSaveEntry entry, ref nameBytes); + bool success = FileTable.TryGetValue((int)position.NextFile, out TableEntry entry, ref nameBytes); // todo error message if (!success) @@ -54,7 +54,7 @@ namespace LibHac.IO.Save } position.NextFile = entry.NextSibling; - info = entry.Info; + info = entry.Value; name = Util.GetUtf8StringNullTerminated(nameBytes); @@ -69,9 +69,9 @@ namespace LibHac.IO.Save return false; } - Span nameBytes = stackalloc byte[FileTable.MaxNameLength]; + Span nameBytes = stackalloc byte[DirectoryTable.MaxNameLength]; - bool success = DirectoryTable.TryGetValue(position.NextDirectory, out DirectorySaveEntry entry, ref nameBytes); + bool success = DirectoryTable.TryGetValue(position.NextDirectory, out TableEntry entry, ref nameBytes); // todo error message if (!success) @@ -110,15 +110,15 @@ namespace LibHac.IO.Save if (index < 0) { - var newEntry = new DirectorySaveEntry(); + var newEntry = new TableEntry(); index = DirectoryTable.Add(ref key, ref newEntry); if (prevIndex > 0) { - DirectoryTable.GetValue(prevIndex, out DirectorySaveEntry parentEntry); + DirectoryTable.GetValue(prevIndex, out TableEntry parentEntry); - newEntry.NextSibling = parentEntry.Pos.NextDirectory; - parentEntry.Pos.NextDirectory = index; + newEntry.NextSibling = parentEntry.Value.NextDirectory; + parentEntry.Value.NextDirectory = index; DirectoryTable.SetValue(prevIndex, ref parentEntry); DirectoryTable.SetValue(index, ref newEntry); @@ -132,21 +132,21 @@ namespace LibHac.IO.Save { int index = FileTable.GetIndexFromKey(ref key).Index; - var fileEntry = new FileSaveEntry(); + var fileEntry = new TableEntry(); if (index < 0) { index = FileTable.Add(ref key, ref fileEntry); - DirectoryTable.GetValue(prevIndex, out DirectorySaveEntry parentEntry); + DirectoryTable.GetValue(prevIndex, out TableEntry parentEntry); - fileEntry.NextSibling = (int)parentEntry.Pos.NextFile; - parentEntry.Pos.NextFile = index; + fileEntry.NextSibling = (int)parentEntry.Value.NextFile; + parentEntry.Value.NextFile = index; DirectoryTable.SetValue(prevIndex, ref parentEntry); } - fileEntry.Info = fileInfo; + fileEntry.Value = fileInfo; FileTable.SetValue(index, ref fileEntry); } } @@ -159,9 +159,9 @@ namespace LibHac.IO.Save return false; } - if (DirectoryTable.TryGetValue(ref key, out DirectorySaveEntry value)) + if (DirectoryTable.TryGetValue(ref key, out TableEntry entry)) { - position = value.Pos; + position = entry.Value; return true; } @@ -187,19 +187,10 @@ namespace LibHac.IO.Save } [StructLayout(LayoutKind.Sequential, Pack = 1)] - private struct DirectorySaveEntry + private struct TableEntry where T : struct { public int NextSibling; - public SaveFindPosition Pos; - public long Field10; - } - - [StructLayout(LayoutKind.Sequential, Pack = 1)] - private struct FileSaveEntry - { - public int NextSibling; - public SaveFileInfo Info; - public long Field10; + public T Value; } } } diff --git a/src/LibHac/IO/Save/SaveFsEntries.cs b/src/LibHac/IO/Save/SaveFsEntries.cs index 3eb5b9d1..ee4dd8d5 100644 --- a/src/LibHac/IO/Save/SaveFsEntries.cs +++ b/src/LibHac/IO/Save/SaveFsEntries.cs @@ -15,17 +15,18 @@ namespace LibHac.IO.Save } } - [StructLayout(LayoutKind.Sequential, Pack = 1)] + [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x14)] public struct SaveFileInfo { public int StartBlock; public long Length; + public long Reserved; } /// /// Represents the current position when enumerating a directory's contents. /// - [StructLayout(LayoutKind.Sequential, Pack = 1)] + [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x14)] public struct SaveFindPosition { /// The ID of the next directory to be enumerated. diff --git a/src/LibHac/IO/Save/SaveFsList.cs b/src/LibHac/IO/Save/SaveFsList.cs index 21c57d4d..dcb49103 100644 --- a/src/LibHac/IO/Save/SaveFsList.cs +++ b/src/LibHac/IO/Save/SaveFsList.cs @@ -5,7 +5,7 @@ using System.Runtime.InteropServices; namespace LibHac.IO.Save { - internal class SaveFsList where T : unmanaged + internal class SaveFsList where T : struct { private const int FreeListHeadIndex = 0; private const int UsedListHeadIndex = 1;