2018-06-10 20:46:42 -04:00
|
|
|
using Ryujinx.HLE.OsHle.Ipc;
|
2018-02-20 06:03:04 -05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
|
2018-06-10 20:46:42 -04:00
|
|
|
namespace Ryujinx.HLE.OsHle.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
|
|
|
|
|
|
|
private Dictionary<int, ServiceProcessRequest> m_Commands;
|
|
|
|
|
2018-03-19 14:58:46 -04:00
|
|
|
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
|
2018-02-20 06:03:04 -05:00
|
|
|
|
2018-02-21 16:56:52 -05:00
|
|
|
private List<string> DirectoryEntries;
|
2018-02-20 06:03:04 -05:00
|
|
|
|
2018-02-21 16:56:52 -05:00
|
|
|
private int CurrentItemIndex;
|
|
|
|
|
|
|
|
public event EventHandler<EventArgs> Disposed;
|
|
|
|
|
|
|
|
public string HostPath { get; private set; }
|
|
|
|
|
|
|
|
public IDirectory(string HostPath, int Flags)
|
2018-02-20 06:03:04 -05:00
|
|
|
{
|
|
|
|
m_Commands = new Dictionary<int, ServiceProcessRequest>()
|
|
|
|
{
|
2018-02-21 16:56:52 -05:00
|
|
|
{ 0, Read },
|
|
|
|
{ 1, GetEntryCount }
|
2018-02-20 06:03:04 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
this.HostPath = HostPath;
|
|
|
|
|
2018-02-21 16:56:52 -05:00
|
|
|
DirectoryEntries = new List<string>();
|
|
|
|
|
|
|
|
if ((Flags & 1) != 0)
|
2018-02-20 06:03:04 -05:00
|
|
|
{
|
2018-02-21 16:56:52 -05:00
|
|
|
DirectoryEntries.AddRange(Directory.GetDirectories(HostPath));
|
2018-02-20 06:03:04 -05:00
|
|
|
}
|
|
|
|
|
2018-02-21 16:56:52 -05:00
|
|
|
if ((Flags & 2) != 0)
|
2018-02-20 06:03:04 -05:00
|
|
|
{
|
2018-02-21 16:56:52 -05:00
|
|
|
DirectoryEntries.AddRange(Directory.GetFiles(HostPath));
|
2018-02-20 06:03:04 -05:00
|
|
|
}
|
2018-02-21 16:56:52 -05:00
|
|
|
|
|
|
|
CurrentItemIndex = 0;
|
2018-02-20 06:03:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public long Read(ServiceCtx Context)
|
|
|
|
{
|
|
|
|
long BufferPosition = Context.Request.ReceiveBuff[0].Position;
|
2018-02-21 16:56:52 -05:00
|
|
|
long BufferLen = Context.Request.ReceiveBuff[0].Size;
|
2018-02-20 06:03:04 -05:00
|
|
|
|
2018-02-21 16:56:52 -05:00
|
|
|
int MaxReadCount = (int)(BufferLen / DirectoryEntrySize);
|
2018-02-20 06:03:04 -05:00
|
|
|
|
2018-02-21 16:56:52 -05:00
|
|
|
int Count = Math.Min(DirectoryEntries.Count - CurrentItemIndex, MaxReadCount);
|
2018-02-20 06:03:04 -05:00
|
|
|
|
2018-02-21 16:56:52 -05:00
|
|
|
for (int Index = 0; Index < Count; Index++)
|
|
|
|
{
|
|
|
|
long Position = BufferPosition + Index * DirectoryEntrySize;
|
2018-02-20 06:03:04 -05:00
|
|
|
|
2018-02-21 16:56:52 -05:00
|
|
|
WriteDirectoryEntry(Context, Position, DirectoryEntries[CurrentItemIndex++]);
|
2018-02-20 06:03:04 -05:00
|
|
|
}
|
|
|
|
|
2018-02-21 16:56:52 -05:00
|
|
|
Context.ResponseData.Write((long)Count);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WriteDirectoryEntry(ServiceCtx Context, long Position, string FullPath)
|
|
|
|
{
|
|
|
|
for (int Offset = 0; Offset < 0x300; Offset += 8)
|
2018-02-20 06:03:04 -05:00
|
|
|
{
|
2018-02-21 16:56:52 -05:00
|
|
|
Context.Memory.WriteInt64(Position + Offset, 0);
|
2018-02-20 06:03:04 -05:00
|
|
|
}
|
2018-02-21 16:56:52 -05:00
|
|
|
|
|
|
|
byte[] NameBuffer = Encoding.UTF8.GetBytes(Path.GetFileName(FullPath));
|
|
|
|
|
2018-06-09 12:05:41 -04:00
|
|
|
Context.Memory.WriteBytes(Position, NameBuffer);
|
2018-02-21 16:56:52 -05:00
|
|
|
|
|
|
|
int Type = 0;
|
|
|
|
long Size = 0;
|
|
|
|
|
|
|
|
if (File.Exists(FullPath))
|
2018-02-20 06:03:04 -05:00
|
|
|
{
|
2018-02-21 16:56:52 -05:00
|
|
|
Type = 1;
|
|
|
|
Size = new FileInfo(FullPath).Length;
|
2018-02-20 06:03:04 -05:00
|
|
|
}
|
|
|
|
|
2018-02-21 16:56:52 -05:00
|
|
|
Context.Memory.WriteInt32(Position + 0x300, 0); //Padding?
|
|
|
|
Context.Memory.WriteInt32(Position + 0x304, Type);
|
|
|
|
Context.Memory.WriteInt64(Position + 0x308, Size);
|
2018-02-20 06:03:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public long GetEntryCount(ServiceCtx Context)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|