2018-11-18 19:37:41 +00:00
|
|
|
using Ryujinx.HLE.FileSystem;
|
2018-08-16 23:47:36 +00:00
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
2018-02-20 11:03:04 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
|
2018-08-16 23:47:36 +00:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.FspSrv
|
2018-02-20 11:03:04 +00:00
|
|
|
{
|
2018-03-19 18:58:46 +00:00
|
|
|
class IDirectory : IpcService, IDisposable
|
2018-02-20 11:03:04 +00:00
|
|
|
{
|
2018-02-21 21:56:52 +00:00
|
|
|
private const int DirectoryEntrySize = 0x310;
|
2018-02-20 11:03:04 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private Dictionary<int, ServiceProcessRequest> _commands;
|
2018-02-20 11:03:04 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
|
2018-02-20 11:03:04 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private List<DirectoryEntry> _directoryEntries;
|
2018-02-20 11:03:04 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private int _currentItemIndex;
|
2018-02-21 21:56:52 +00:00
|
|
|
|
|
|
|
public event EventHandler<EventArgs> Disposed;
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
public string DirectoryPath { get; private set; }
|
2018-02-21 21:56:52 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private IFileSystemProvider _provider;
|
2018-11-18 19:37:41 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public IDirectory(string directoryPath, int flags, IFileSystemProvider provider)
|
2018-02-20 11:03:04 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_commands = new Dictionary<int, ServiceProcessRequest>
|
2018-02-20 11:03:04 +00:00
|
|
|
{
|
2018-02-21 21:56:52 +00:00
|
|
|
{ 0, Read },
|
|
|
|
{ 1, GetEntryCount }
|
2018-02-20 11:03:04 +00:00
|
|
|
};
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_provider = provider;
|
|
|
|
DirectoryPath = directoryPath;
|
2018-02-20 11:03:04 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_directoryEntries = new List<DirectoryEntry>();
|
2018-02-21 21:56:52 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if ((flags & 1) != 0)
|
2018-02-20 11:03:04 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_directoryEntries.AddRange(provider.GetDirectories(directoryPath));
|
2018-02-20 11:03:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if ((flags & 2) != 0)
|
2018-02-20 11:03:04 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_directoryEntries.AddRange(provider.GetFiles(directoryPath));
|
2018-02-20 11:03:04 +00:00
|
|
|
}
|
2018-02-21 21:56:52 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
_currentItemIndex = 0;
|
2018-02-20 11:03:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 19:37:41 +00:00
|
|
|
// Read() -> (u64 count, buffer<nn::fssrv::sf::IDirectoryEntry, 6, 0> entries)
|
2018-12-06 11:16:24 +00:00
|
|
|
public long Read(ServiceCtx context)
|
2018-02-20 11:03:04 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
long bufferPosition = context.Request.ReceiveBuff[0].Position;
|
|
|
|
long bufferLen = context.Request.ReceiveBuff[0].Size;
|
2018-02-20 11:03:04 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
int maxReadCount = (int)(bufferLen / DirectoryEntrySize);
|
2018-02-20 11:03:04 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
int count = Math.Min(_directoryEntries.Count - _currentItemIndex, maxReadCount);
|
2018-02-20 11:03:04 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
for (int index = 0; index < count; index++)
|
2018-02-21 21:56:52 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
long position = bufferPosition + index * DirectoryEntrySize;
|
2018-02-20 11:03:04 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
WriteDirectoryEntry(context, position, _directoryEntries[_currentItemIndex++]);
|
2018-02-20 11:03:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.ResponseData.Write((long)count);
|
2018-02-21 21:56:52 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private void WriteDirectoryEntry(ServiceCtx context, long position, DirectoryEntry entry)
|
2018-02-21 21:56:52 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
for (int offset = 0; offset < 0x300; offset += 8)
|
2018-02-20 11:03:04 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
context.Memory.WriteInt64(position + offset, 0);
|
2018-02-20 11:03:04 +00:00
|
|
|
}
|
2018-02-21 21:56:52 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
byte[] nameBuffer = Encoding.UTF8.GetBytes(Path.GetFileName(entry.Path));
|
2018-02-21 21:56:52 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.Memory.WriteBytes(position, nameBuffer);
|
2018-02-21 21:56:52 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.Memory.WriteInt32(position + 0x300, 0); //Padding?
|
|
|
|
context.Memory.WriteInt32(position + 0x304, (byte)entry.EntryType);
|
|
|
|
context.Memory.WriteInt64(position + 0x308, entry.Size);
|
2018-02-20 11:03:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 19:37:41 +00:00
|
|
|
// GetEntryCount() -> u64
|
2018-12-06 11:16:24 +00:00
|
|
|
public long GetEntryCount(ServiceCtx context)
|
2018-02-20 11:03:04 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
context.ResponseData.Write((long)_directoryEntries.Count);
|
2018-02-21 21:56:52 +00:00
|
|
|
|
2018-02-20 11:03:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-02-21 21:56:52 +00:00
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
Disposed?.Invoke(this, EventArgs.Empty);
|
|
|
|
}
|
|
|
|
}
|
2018-02-20 11:03:04 +00:00
|
|
|
}
|
|
|
|
}
|