2018-08-17 01:47:36 +02:00
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
2018-02-20 12:03:04 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
|
2018-08-17 01:47:36 +02:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.FspSrv
|
2018-02-20 12:03:04 +01:00
|
|
|
{
|
2018-03-19 19:58:46 +01:00
|
|
|
class IDirectory : IpcService, IDisposable
|
2018-02-20 12:03:04 +01:00
|
|
|
{
|
2018-02-21 22:56:52 +01:00
|
|
|
private const int DirectoryEntrySize = 0x310;
|
2018-02-20 12:03:04 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private Dictionary<int, ServiceProcessRequest> _commands;
|
2018-02-20 12:03:04 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
|
2018-02-20 12:03:04 +01:00
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
private IEnumerator<LibHac.Fs.DirectoryEntry> _enumerator;
|
2018-02-21 22:56:52 +01:00
|
|
|
|
|
|
|
public event EventHandler<EventArgs> Disposed;
|
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
public string Path { get; }
|
2018-02-21 22:56:52 +01:00
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
private LibHac.Fs.IDirectory _provider;
|
2018-11-18 20:37:41 +01:00
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
public IDirectory(LibHac.Fs.IDirectory directory)
|
2018-02-20 12:03:04 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_commands = new Dictionary<int, ServiceProcessRequest>
|
2018-02-20 12:03:04 +01:00
|
|
|
{
|
2018-02-21 22:56:52 +01:00
|
|
|
{ 0, Read },
|
|
|
|
{ 1, GetEntryCount }
|
2018-02-20 12:03:04 +01:00
|
|
|
};
|
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
_provider = directory;
|
2018-02-20 12:03:04 +01:00
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
Path = directory.FullPath;
|
2018-02-21 22:56:52 +01:00
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
_enumerator = directory.Read().GetEnumerator();
|
2018-02-20 12:03:04 +01:00
|
|
|
}
|
|
|
|
|
2018-11-18 20:37:41 +01:00
|
|
|
// Read() -> (u64 count, buffer<nn::fssrv::sf::IDirectoryEntry, 6, 0> entries)
|
2018-12-06 12:16:24 +01:00
|
|
|
public long Read(ServiceCtx context)
|
2018-02-20 12:03:04 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
long bufferPosition = context.Request.ReceiveBuff[0].Position;
|
|
|
|
long bufferLen = context.Request.ReceiveBuff[0].Size;
|
2018-02-20 12:03:04 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int maxReadCount = (int)(bufferLen / DirectoryEntrySize);
|
2019-06-01 02:31:10 +02:00
|
|
|
int readCount = 0;
|
2018-02-20 12:03:04 +01:00
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
while (readCount < maxReadCount && _enumerator.MoveNext())
|
2018-02-21 22:56:52 +01:00
|
|
|
{
|
2019-06-01 02:31:10 +02:00
|
|
|
long position = bufferPosition + readCount * DirectoryEntrySize;
|
|
|
|
|
|
|
|
WriteDirectoryEntry(context, position, _enumerator.Current);
|
2018-02-20 12:03:04 +01:00
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
readCount++;
|
2018-02-20 12:03:04 +01:00
|
|
|
}
|
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
context.ResponseData.Write((long)readCount);
|
2018-02-21 22:56:52 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
private void WriteDirectoryEntry(ServiceCtx context, long position, LibHac.Fs.DirectoryEntry entry)
|
2018-02-21 22:56:52 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
for (int offset = 0; offset < 0x300; offset += 8)
|
2018-02-20 12:03:04 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
context.Memory.WriteInt64(position + offset, 0);
|
2018-02-20 12:03:04 +01:00
|
|
|
}
|
2018-02-21 22:56:52 +01:00
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
byte[] nameBuffer = Encoding.UTF8.GetBytes(entry.Name);
|
2018-02-21 22:56:52 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
context.Memory.WriteBytes(position, nameBuffer);
|
2018-02-21 22:56:52 +01:00
|
|
|
|
2019-06-01 02:31:10 +02:00
|
|
|
context.Memory.WriteInt32(position + 0x300, (int)entry.Attributes);
|
|
|
|
context.Memory.WriteInt32(position + 0x304, (byte)entry.Type);
|
2018-12-06 12:16:24 +01:00
|
|
|
context.Memory.WriteInt64(position + 0x308, entry.Size);
|
2018-02-20 12:03:04 +01:00
|
|
|
}
|
|
|
|
|
2018-11-18 20:37:41 +01:00
|
|
|
// GetEntryCount() -> u64
|
2018-12-06 12:16:24 +01:00
|
|
|
public long GetEntryCount(ServiceCtx context)
|
2018-02-20 12:03:04 +01:00
|
|
|
{
|
2019-06-01 02:31:10 +02:00
|
|
|
context.ResponseData.Write((long)_provider.GetEntryCount());
|
2018-02-21 22:56:52 +01:00
|
|
|
|
2018-02-20 12:03:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-02-21 22:56:52 +01:00
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
Disposed?.Invoke(this, EventArgs.Empty);
|
|
|
|
}
|
|
|
|
}
|
2018-02-20 12:03:04 +01:00
|
|
|
}
|
|
|
|
}
|