aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Services/Mii/StaticService/DatabaseServiceImpl.cs
blob: 0d286e1cca44658823302a2c7b1a7717410390f7 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using Ryujinx.HLE.HOS.Services.Mii.Types;
using Ryujinx.HLE.HOS.Services.Settings;
using System;

namespace Ryujinx.HLE.HOS.Services.Mii.StaticService
{
    class DatabaseServiceImpl : IDatabaseService
    {
        private readonly DatabaseImpl _database;
        private readonly DatabaseSessionMetadata _metadata;
        private readonly bool _isSystem;

        public DatabaseServiceImpl(DatabaseImpl database, bool isSystem, SpecialMiiKeyCode miiKeyCode)
        {
            _database = database;
            _metadata = _database.CreateSessionMetadata(miiKeyCode);
            _isSystem = isSystem;
        }

        public bool IsDatabaseTestModeEnabled()
        {
            if (NxSettings.Settings.TryGetValue("mii!is_db_test_mode_enabled", out object isDatabaseTestModeEnabled))
            {
                return (bool)isDatabaseTestModeEnabled;
            }

            return false;
        }

        protected override bool IsUpdated(SourceFlag flag)
        {
            return _database.IsUpdated(_metadata, flag);
        }

        protected override bool IsFullDatabase()
        {
            return _database.IsFullDatabase();
        }

        protected override uint GetCount(SourceFlag flag)
        {
            return _database.GetCount(_metadata, flag);
        }

        protected override ResultCode Get(SourceFlag flag, out int count, Span<CharInfoElement> elements)
        {
            return _database.Get(_metadata, flag, out count, elements);
        }

        protected override ResultCode Get1(SourceFlag flag, out int count, Span<CharInfo> elements)
        {
            return _database.Get(_metadata, flag, out count, elements);
        }

        protected override ResultCode UpdateLatest(CharInfo oldCharInfo, SourceFlag flag, out CharInfo newCharInfo)
        {
            newCharInfo = default;

            return _database.UpdateLatest(_metadata, oldCharInfo, flag, newCharInfo);
        }

        protected override ResultCode BuildRandom(Age age, Gender gender, Race race, out CharInfo charInfo)
        {
            if (age > Age.All || gender > Gender.All || race > Race.All)
            {
                charInfo = default;

                return ResultCode.InvalidArgument;
            }

            _database.BuildRandom(age, gender, race, out charInfo);

            return ResultCode.Success;
        }

        protected override ResultCode BuildDefault(uint index, out CharInfo charInfo)
        {
            if (index >= DefaultMii.TableLength)
            {
                charInfo = default;

                return ResultCode.InvalidArgument;
            }

            _database.BuildDefault(index, out charInfo);

            return ResultCode.Success;
        }

        protected override ResultCode Get2(SourceFlag flag, out int count, Span<StoreDataElement> elements)
        {
            if (!_isSystem)
            {
                count = -1;

                return ResultCode.PermissionDenied;
            }

            return _database.Get(_metadata, flag, out count, elements);
        }

        protected override ResultCode Get3(SourceFlag flag, out int count, Span<StoreData> elements)
        {
            if (!_isSystem)
            {
                count = -1;

                return ResultCode.PermissionDenied;
            }

            return _database.Get(_metadata, flag, out count, elements);
        }

        protected override ResultCode UpdateLatest1(StoreData oldStoreData, SourceFlag flag, out StoreData newStoreData)
        {
            newStoreData = default;

            if (!_isSystem)
            {
                return ResultCode.PermissionDenied;
            }

            return _database.UpdateLatest(_metadata, oldStoreData, flag, newStoreData);
        }

        protected override ResultCode FindIndex(CreateId createId, bool isSpecial, out int index)
        {
            if (!_isSystem)
            {
                index = -1;

                return ResultCode.PermissionDenied;
            }

            index = _database.FindIndex(createId, isSpecial);

            return ResultCode.Success;
        }

        protected override ResultCode Move(CreateId createId, int newIndex)
        {
            if (!_isSystem)
            {
                return ResultCode.PermissionDenied;
            }

            if (newIndex > 0 && _database.GetCount(_metadata, SourceFlag.Database) > newIndex)
            {
                return _database.Move(_metadata, newIndex, createId);
            }

            return ResultCode.InvalidArgument;
        }

        protected override ResultCode AddOrReplace(StoreData storeData)
        {
            if (!_isSystem)
            {
                return ResultCode.PermissionDenied;
            }

            return _database.AddOrReplace(_metadata, storeData);
        }

        protected override ResultCode Delete(CreateId createId)
        {
            if (!_isSystem)
            {
                return ResultCode.PermissionDenied;
            }

            return _database.Delete(_metadata, createId);
        }

        protected override ResultCode DestroyFile()
        {
            if (!IsDatabaseTestModeEnabled())
            {
                return ResultCode.TestModeNotEnabled;
            }

            return _database.DestroyFile(_metadata);
        }

        protected override ResultCode DeleteFile()
        {
            if (!IsDatabaseTestModeEnabled())
            {
                return ResultCode.TestModeNotEnabled;
            }

            return _database.DeleteFile();
        }

        protected override ResultCode Format()
        {
            if (!IsDatabaseTestModeEnabled())
            {
                return ResultCode.TestModeNotEnabled;
            }

            _database.Format(_metadata);

            return ResultCode.Success;
        }

        protected override ResultCode Import(ReadOnlySpan<byte> data)
        {
            if (!IsDatabaseTestModeEnabled())
            {
                return ResultCode.TestModeNotEnabled;
            }

            throw new NotImplementedException();
        }

        protected override ResultCode Export(Span<byte> data)
        {
            if (!IsDatabaseTestModeEnabled())
            {
                return ResultCode.TestModeNotEnabled;
            }

            throw new NotImplementedException();
        }

        protected override ResultCode IsBrokenDatabaseWithClearFlag(out bool isBrokenDatabase)
        {
            if (!_isSystem)
            {
                isBrokenDatabase = false;

                return ResultCode.PermissionDenied;
            }

            isBrokenDatabase = _database.IsBrokenDatabaseWithClearFlag();

            return ResultCode.Success;
        }

        protected override ResultCode GetIndex(CharInfo charInfo, out int index)
        {
            return _database.GetIndex(_metadata, charInfo, out index);
        }

        protected override void SetInterfaceVersion(uint interfaceVersion)
        {
            _database.SetInterfaceVersion(_metadata, interfaceVersion);
        }

        protected override ResultCode Convert(Ver3StoreData ver3StoreData, out CharInfo charInfo)
        {
            throw new NotImplementedException();
        }

        protected override ResultCode ConvertCoreDataToCharInfo(CoreData coreData, out CharInfo charInfo)
        {
            return _database.ConvertCoreDataToCharInfo(coreData, out charInfo);
        }

        protected override ResultCode ConvertCharInfoToCoreData(CharInfo charInfo, out CoreData coreData)
        {
            return _database.ConvertCharInfoToCoreData(charInfo, out coreData);
        }
    }
}