ryujinx-mirror/Ryujinx.HLE/HOS/Services/FspSrv/IDirectory.cs
Alex Barney f723f6f39a Update to LibHac 0.5.0 (#725)
* Update to libhac 0.5

* Catch HorizonResultException in IFileSystemProxy

* Changes based on feedback
2019-07-10 19:20:01 +02:00

94 lines
2.8 KiB
C#

using LibHac;
using Ryujinx.HLE.HOS.Ipc;
using System.Collections.Generic;
using System.Text;
namespace Ryujinx.HLE.HOS.Services.FspSrv
{
class IDirectory : IpcService
{
private const int DirectoryEntrySize = 0x310;
private Dictionary<int, ServiceProcessRequest> _commands;
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
private IEnumerator<LibHac.Fs.DirectoryEntry> _enumerator;
private LibHac.Fs.IDirectory _baseDirectory;
public IDirectory(LibHac.Fs.IDirectory directory)
{
_commands = new Dictionary<int, ServiceProcessRequest>
{
{ 0, Read },
{ 1, GetEntryCount }
};
_baseDirectory = directory;
_enumerator = directory.Read().GetEnumerator();
}
// Read() -> (u64 count, buffer<nn::fssrv::sf::IDirectoryEntry, 6, 0> entries)
public long Read(ServiceCtx context)
{
long bufferPosition = context.Request.ReceiveBuff[0].Position;
long bufferLen = context.Request.ReceiveBuff[0].Size;
int maxReadCount = (int)(bufferLen / DirectoryEntrySize);
int readCount = 0;
try
{
while (readCount < maxReadCount && _enumerator.MoveNext())
{
long position = bufferPosition + readCount * DirectoryEntrySize;
WriteDirectoryEntry(context, position, _enumerator.Current);
readCount++;
}
}
catch (HorizonResultException ex)
{
return ex.ResultValue.Value;
}
context.ResponseData.Write((long)readCount);
return 0;
}
private void WriteDirectoryEntry(ServiceCtx context, long position, LibHac.Fs.DirectoryEntry entry)
{
for (int offset = 0; offset < 0x300; offset += 8)
{
context.Memory.WriteInt64(position + offset, 0);
}
byte[] nameBuffer = Encoding.UTF8.GetBytes(entry.Name);
context.Memory.WriteBytes(position, nameBuffer);
context.Memory.WriteInt32(position + 0x300, (int)entry.Attributes);
context.Memory.WriteInt32(position + 0x304, (byte)entry.Type);
context.Memory.WriteInt64(position + 0x308, entry.Size);
}
// GetEntryCount() -> u64
public long GetEntryCount(ServiceCtx context)
{
try
{
context.ResponseData.Write((long)_baseDirectory.GetEntryCount());
}
catch (HorizonResultException ex)
{
return ex.ResultValue.Value;
}
return 0;
}
}
}