aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/FileSystem/SaveHelper.cs
blob: a107249827fbf60802a3e5918160c817badcf9b9 (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
using LibHac.Fs;
using Ryujinx.HLE.HOS;
using System.IO;

namespace Ryujinx.HLE.FileSystem
{
    static class SaveHelper
    {
        public static IFileSystem OpenSystemSaveData(ServiceCtx context, ulong saveId)
        {
            SaveInfo saveInfo = new SaveInfo(0, (long)saveId, SaveDataType.SystemSaveData, SaveSpaceId.NandSystem);
            string   savePath = context.Device.FileSystem.GetSavePath(context, saveInfo, false);

            if (File.Exists(savePath))
            {
                string tempDirectoryPath = $"{savePath}_temp";

                Directory.CreateDirectory(tempDirectoryPath);

                IFileSystem outputFolder = new LocalFileSystem(tempDirectoryPath);

                using (LocalStorage systemSaveData = new LocalStorage(savePath, FileAccess.Read, FileMode.Open))
                {
                    IFileSystem saveFs = new LibHac.Fs.Save.SaveDataFileSystem(context.Device.System.KeySet, systemSaveData, IntegrityCheckLevel.None, false);

                    saveFs.CopyFileSystem(outputFolder);
                }

                File.Delete(savePath);

                Directory.Move(tempDirectoryPath, savePath);
            }
            else
            {
                if (!Directory.Exists(savePath))
                {
                    Directory.CreateDirectory(savePath);
                }
            }

            return new LocalFileSystem(savePath);
        }
    }
}