2018-11-18 19:37:41 +00:00
|
|
|
using Ryujinx.HLE.FileSystem;
|
2018-08-17 00:47:36 +01: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-17 00:47:36 +01: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
|
|
|
|
|
|
|
private Dictionary<int, ServiceProcessRequest> m_Commands;
|
|
|
|
|
2018-03-19 18:58:46 +00:00
|
|
|
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
|
2018-02-20 11:03:04 +00:00
|
|
|
|
2018-11-18 19:37:41 +00:00
|
|
|
private List<DirectoryEntry> DirectoryEntries;
|
2018-02-20 11:03:04 +00:00
|
|
|
|
2018-02-21 21:56:52 +00:00
|
|
|
private int CurrentItemIndex;
|
|
|
|
|
|
|
|
public event EventHandler<EventArgs> Disposed;
|
|
|
|
|
2018-11-18 19:37:41 +00:00
|
|
|
public string DirectoryPath { get; private set; }
|
2018-02-21 21:56:52 +00:00
|
|
|
|
2018-11-18 19:37:41 +00:00
|
|
|
private IFileSystemProvider Provider;
|
|
|
|
|
|
|
|
public IDirectory(string DirectoryPath, int Flags, IFileSystemProvider Provider)
|
2018-02-20 11:03:04 +00:00
|
|
|
{
|
|
|
|
m_Commands = new Dictionary<int, ServiceProcessRequest>()
|
|
|
|
{
|
2018-02-21 21:56:52 +00:00
|
|
|
{ 0, Read },
|
|
|
|
{ 1, GetEntryCount }
|
2018-02-20 11:03:04 +00:00
|
|
|
};
|
|
|
|
|
2018-11-18 19:37:41 +00:00
|
|
|
this.Provider = Provider;
|
|
|
|
this.DirectoryPath = DirectoryPath;
|
2018-02-20 11:03:04 +00:00
|
|
|
|
2018-11-18 19:37:41 +00:00
|
|
|
DirectoryEntries = new List<DirectoryEntry>();
|
2018-02-21 21:56:52 +00:00
|
|
|
|
|
|
|
if ((Flags & 1) != 0)
|
2018-02-20 11:03:04 +00:00
|
|
|
{
|
2018-11-18 19:37:41 +00:00
|
|
|
DirectoryEntries.AddRange(Provider.GetDirectories(DirectoryPath));
|
2018-02-20 11:03:04 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 21:56:52 +00:00
|
|
|
if ((Flags & 2) != 0)
|
2018-02-20 11:03:04 +00:00
|
|
|
{
|
2018-11-18 19:37:41 +00:00
|
|
|
DirectoryEntries.AddRange(Provider.GetFiles(DirectoryPath));
|
2018-02-20 11:03:04 +00:00
|
|
|
}
|
2018-02-21 21:56:52 +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-02-20 11:03:04 +00:00
|
|
|
public long Read(ServiceCtx Context)
|
|
|
|
{
|
|
|
|
long BufferPosition = Context.Request.ReceiveBuff[0].Position;
|
2018-02-21 21:56:52 +00:00
|
|
|
long BufferLen = Context.Request.ReceiveBuff[0].Size;
|
2018-02-20 11:03:04 +00:00
|
|
|
|
2018-02-21 21:56:52 +00:00
|
|
|
int MaxReadCount = (int)(BufferLen / DirectoryEntrySize);
|
2018-02-20 11:03:04 +00:00
|
|
|
|
2018-02-21 21:56:52 +00:00
|
|
|
int Count = Math.Min(DirectoryEntries.Count - CurrentItemIndex, MaxReadCount);
|
2018-02-20 11:03:04 +00:00
|
|
|
|
2018-02-21 21:56:52 +00:00
|
|
|
for (int Index = 0; Index < Count; Index++)
|
|
|
|
{
|
|
|
|
long Position = BufferPosition + Index * DirectoryEntrySize;
|
2018-02-20 11:03:04 +00:00
|
|
|
|
2018-02-21 21:56:52 +00:00
|
|
|
WriteDirectoryEntry(Context, Position, DirectoryEntries[CurrentItemIndex++]);
|
2018-02-20 11:03:04 +00:00
|
|
|
}
|
|
|
|
|
2018-02-21 21:56:52 +00:00
|
|
|
Context.ResponseData.Write((long)Count);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-18 19:37:41 +00:00
|
|
|
private void WriteDirectoryEntry(ServiceCtx Context, long Position, DirectoryEntry Entry)
|
2018-02-21 21:56:52 +00:00
|
|
|
{
|
|
|
|
for (int Offset = 0; Offset < 0x300; Offset += 8)
|
2018-02-20 11:03:04 +00:00
|
|
|
{
|
2018-02-21 21:56:52 +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-11-18 19:37:41 +00:00
|
|
|
byte[] NameBuffer = Encoding.UTF8.GetBytes(Path.GetFileName(Entry.Path));
|
2018-02-21 21:56:52 +00:00
|
|
|
|
2018-06-09 17:05:41 +01:00
|
|
|
Context.Memory.WriteBytes(Position, NameBuffer);
|
2018-02-21 21:56:52 +00:00
|
|
|
|
|
|
|
Context.Memory.WriteInt32(Position + 0x300, 0); //Padding?
|
2018-11-18 19:37:41 +00:00
|
|
|
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-02-20 11:03:04 +00:00
|
|
|
public long GetEntryCount(ServiceCtx Context)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|