1 // Copyright 2018 yuzu emulator team
2 // Licensed under GPLv2 or any later version
3 // Refer to the license.txt file included.
4 
5 #pragma once
6 
7 #include <memory>
8 #include <string>
9 #include "common/common_funcs.h"
10 #include "common/common_types.h"
11 #include "common/swap.h"
12 #include "core/file_sys/vfs.h"
13 #include "core/hle/result.h"
14 
15 namespace Core {
16 class System;
17 }
18 
19 namespace FileSys {
20 
21 enum class SaveDataSpaceId : u8 {
22     NandSystem = 0,
23     NandUser = 1,
24     SdCardSystem = 2,
25     TemporaryStorage = 3,
26     SdCardUser = 4,
27     ProperSystem = 100,
28     SafeMode = 101,
29 };
30 
31 enum class SaveDataType : u8 {
32     SystemSaveData = 0,
33     SaveData = 1,
34     BcatDeliveryCacheStorage = 2,
35     DeviceSaveData = 3,
36     TemporaryStorage = 4,
37     CacheStorage = 5,
38     SystemBcat = 6,
39 };
40 
41 enum class SaveDataRank : u8 {
42     Primary = 0,
43     Secondary = 1,
44 };
45 
46 enum class SaveDataFlags : u32 {
47     None = (0 << 0),
48     KeepAfterResettingSystemSaveData = (1 << 0),
49     KeepAfterRefurbishment = (1 << 1),
50     KeepAfterResettingSystemSaveDataWithoutUserSaveData = (1 << 2),
51     NeedsSecureDelete = (1 << 3),
52 };
53 
54 struct SaveDataAttribute {
55     u64 title_id;
56     u128 user_id;
57     u64 save_id;
58     SaveDataType type;
59     SaveDataRank rank;
60     u16 index;
61     INSERT_PADDING_BYTES(4);
62     u64 zero_1;
63     u64 zero_2;
64     u64 zero_3;
65 
66     std::string DebugInfo() const;
67 };
68 static_assert(sizeof(SaveDataAttribute) == 0x40, "SaveDataAttribute has incorrect size.");
69 
70 struct SaveDataExtraData {
71     SaveDataAttribute attr;
72     u64 owner_id;
73     s64 timestamp;
74     SaveDataFlags flags;
75     INSERT_PADDING_BYTES(4);
76     s64 available_size;
77     s64 journal_size;
78     s64 commit_id;
79     std::array<u8, 0x190> unused;
80 };
81 static_assert(sizeof(SaveDataExtraData) == 0x200, "SaveDataExtraData has incorrect size.");
82 
83 struct SaveDataSize {
84     u64 normal;
85     u64 journal;
86 };
87 
88 /// File system interface to the SaveData archive
89 class SaveDataFactory {
90 public:
91     explicit SaveDataFactory(Core::System& system_, VirtualDir save_directory_);
92     ~SaveDataFactory();
93 
94     ResultVal<VirtualDir> Create(SaveDataSpaceId space, const SaveDataAttribute& meta) const;
95     ResultVal<VirtualDir> Open(SaveDataSpaceId space, const SaveDataAttribute& meta) const;
96 
97     VirtualDir GetSaveDataSpaceDirectory(SaveDataSpaceId space) const;
98 
99     static std::string GetSaveDataSpaceIdPath(SaveDataSpaceId space);
100     static std::string GetFullPath(Core::System& system, SaveDataSpaceId space, SaveDataType type,
101                                    u64 title_id, u128 user_id, u64 save_id);
102 
103     SaveDataSize ReadSaveDataSize(SaveDataType type, u64 title_id, u128 user_id) const;
104     void WriteSaveDataSize(SaveDataType type, u64 title_id, u128 user_id,
105                            SaveDataSize new_value) const;
106 
107 private:
108     VirtualDir dir;
109     Core::System& system;
110 };
111 
112 } // namespace FileSys
113