aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Services/FspSrv/IDirectory.cs
blob: c964eecbaca8530f8e8449d46cec0571405d6024 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using Ryujinx.HLE.FileSystem;
using Ryujinx.HLE.HOS.Ipc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Ryujinx.HLE.HOS.Services.FspSrv
{
    class IDirectory : IpcService, IDisposable
    {
        private const int DirectoryEntrySize = 0x310;

        private Dictionary<int, ServiceProcessRequest> m_Commands;

        public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;

        private List<DirectoryEntry> DirectoryEntries;

        private int CurrentItemIndex;

        public event EventHandler<EventArgs> Disposed;

        public string DirectoryPath { get; private set; }

        private IFileSystemProvider Provider;

        public IDirectory(string DirectoryPath, int Flags, IFileSystemProvider Provider)
        {
            m_Commands = new Dictionary<int, ServiceProcessRequest>()
            {
                { 0, Read          },
                { 1, GetEntryCount }
            };

            this.Provider      = Provider;
            this.DirectoryPath = DirectoryPath;

            DirectoryEntries = new List<DirectoryEntry>();

            if ((Flags & 1) != 0)
            {
                DirectoryEntries.AddRange(Provider.GetDirectories(DirectoryPath));
            }

            if ((Flags & 2) != 0)
            {
                DirectoryEntries.AddRange(Provider.GetFiles(DirectoryPath));
            }

            CurrentItemIndex = 0;
        }

        // Read() -> (u64 count, buffer<nn::fssrv::sf::IDirectoryEntry, 6, 0> entries)
        public long Read(ServiceCtx Context)
        {
            long BufferPosition = Context.Request.ReceiveBuff[0].Position;
            long BufferLen      = Context.Request.ReceiveBuff[0].Size;

            int MaxReadCount = (int)(BufferLen / DirectoryEntrySize);

            int Count = Math.Min(DirectoryEntries.Count - CurrentItemIndex, MaxReadCount);

            for (int Index = 0; Index < Count; Index++)
            {
                long Position = BufferPosition + Index * DirectoryEntrySize;

                WriteDirectoryEntry(Context, Position, DirectoryEntries[CurrentItemIndex++]);
            }

            Context.ResponseData.Write((long)Count);

            return 0;
        }

        private void WriteDirectoryEntry(ServiceCtx Context, long Position, DirectoryEntry Entry)
        {
            for (int Offset = 0; Offset < 0x300; Offset += 8)
            {
                Context.Memory.WriteInt64(Position + Offset, 0);
            }

            byte[] NameBuffer = Encoding.UTF8.GetBytes(Path.GetFileName(Entry.Path));

            Context.Memory.WriteBytes(Position, NameBuffer);

            Context.Memory.WriteInt32(Position + 0x300, 0); //Padding?
            Context.Memory.WriteInt32(Position + 0x304, (byte)Entry.EntryType);
            Context.Memory.WriteInt64(Position + 0x308, Entry.Size);
        }

        // GetEntryCount() -> u64
        public long GetEntryCount(ServiceCtx Context)
        {
            Context.ResponseData.Write((long)DirectoryEntries.Count);

            return 0;
        }

        public void Dispose()
        {
            Dispose(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                Disposed?.Invoke(this, EventArgs.Empty);
            }
        }
    }
}