aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/FileSystem/VirtualFileSystem.cs
blob: 6bb2847f188912fee3e30ae51fa85619626b990e (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using Ryujinx.HLE.FileSystem.Content;
using Ryujinx.HLE.HOS;
using System;
using System.IO;

namespace Ryujinx.HLE.FileSystem
{
    class VirtualFileSystem : IDisposable
    {
        public const string BasePath   = "RyuFs";
        public const string NandPath   = "nand";
        public const string SdCardPath = "sdmc";
        public const string SystemPath = "system";

        public static string SafeNandPath   = Path.Combine(NandPath, "safe");
        public static string SystemNandPath = Path.Combine(NandPath, "system");
        public static string UserNandPath   = Path.Combine(NandPath, "user");

        public Stream RomFs { get; private set; }

        public void LoadRomFs(string FileName)
        {
            RomFs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
        }

        public void SetRomFs(Stream RomfsStream)
        {
            RomFs?.Close();
            RomFs = RomfsStream;
        }

        public string GetFullPath(string BasePath, string FileName)
        {
            if (FileName.StartsWith("//"))
            {
                FileName = FileName.Substring(2);
            }
            else if (FileName.StartsWith('/'))
            {
                FileName = FileName.Substring(1);
            }
            else
            {
                return null;
            }

            string FullPath = Path.GetFullPath(Path.Combine(BasePath, FileName));

            if (!FullPath.StartsWith(GetBasePath()))
            {
                return null;
            }

            return FullPath;
        }

        public string GetSdCardPath() => MakeDirAndGetFullPath(SdCardPath);

        public string GetNandPath() => MakeDirAndGetFullPath(NandPath);

        public string GetSystemPath() => MakeDirAndGetFullPath(SystemPath);

        public string GetGameSavePath(SaveInfo Save, ServiceCtx Context)
        {
            return MakeDirAndGetFullPath(SaveHelper.GetSavePath(Save, Context));
        }

        public string GetFullPartitionPath(string PartitionPath)
        {
            return MakeDirAndGetFullPath(PartitionPath);
        }

        public string SwitchPathToSystemPath(string SwitchPath)
        {
            string[] Parts = SwitchPath.Split(":");

            if (Parts.Length != 2)
            {
                return null;
            }
            return GetFullPath(MakeDirAndGetFullPath(Parts[0]), Parts[1]);
        }

        public string SystemPathToSwitchPath(string SystemPath)
        {
            string BaseSystemPath = GetBasePath() + Path.DirectorySeparatorChar;

            if (SystemPath.StartsWith(BaseSystemPath))
            {
                string RawPath              = SystemPath.Replace(BaseSystemPath, "");
                int    FirstSeparatorOffset = RawPath.IndexOf(Path.DirectorySeparatorChar);

                if (FirstSeparatorOffset == -1)
                {
                    return $"{RawPath}:/";
                }

                string BasePath = RawPath.Substring(0, FirstSeparatorOffset);
                string FileName = RawPath.Substring(FirstSeparatorOffset + 1);

                return $"{BasePath}:/{FileName}";
            }
            return null;
        }

        private string MakeDirAndGetFullPath(string Dir)
        {
            // Handles Common Switch Content Paths
            switch (Dir)
            {
                case ContentPath.SdCard:
                case "@Sdcard":
                    Dir = SdCardPath;
                    break;
                case ContentPath.User:
                    Dir = UserNandPath;
                    break;
                case ContentPath.System:
                    Dir = SystemNandPath;
                    break;
                case ContentPath.SdCardContent:
                    Dir = Path.Combine(SdCardPath, "Nintendo", "Contents");
                    break;
                case ContentPath.UserContent:
                    Dir = Path.Combine(UserNandPath, "Contents");
                    break;
                case ContentPath.SystemContent:
                    Dir = Path.Combine(SystemNandPath, "Contents");
                    break;
            }

            string FullPath = Path.Combine(GetBasePath(), Dir);

            if (!Directory.Exists(FullPath))
            {
                Directory.CreateDirectory(FullPath);
            }

            return FullPath;
        }

        public DriveInfo GetDrive()
        {
            return new DriveInfo(Path.GetPathRoot(GetBasePath()));
        }

        public string GetBasePath()
        {
            string AppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            return Path.Combine(AppDataPath, BasePath);
        }

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

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                RomFs?.Dispose();
            }
        }
    }
}