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