summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/mii/mii.cpp
blob: 8de806cfba6a724392bac3bd058cef4b5383fd70 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <memory>

#include "common/logging/log.h"
#include "core/hle/service/ipc_helpers.h"
#include "core/hle/service/mii/mii.h"
#include "core/hle/service/mii/mii_manager.h"
#include "core/hle/service/mii/mii_result.h"
#include "core/hle/service/mii/types/char_info.h"
#include "core/hle/service/mii/types/store_data.h"
#include "core/hle/service/mii/types/ver3_store_data.h"
#include "core/hle/service/server_manager.h"
#include "core/hle/service/service.h"

namespace Service::Mii {

class IDatabaseService final : public ServiceFramework<IDatabaseService> {
public:
    explicit IDatabaseService(Core::System& system_, bool is_system_)
        : ServiceFramework{system_, "IDatabaseService"}, is_system{is_system_} {
        // clang-format off
        static const FunctionInfo functions[] = {
            {0, &IDatabaseService::IsUpdated, "IsUpdated"},
            {1, &IDatabaseService::IsFullDatabase, "IsFullDatabase"},
            {2, &IDatabaseService::GetCount, "GetCount"},
            {3, &IDatabaseService::Get, "Get"},
            {4, &IDatabaseService::Get1, "Get1"},
            {5, &IDatabaseService::UpdateLatest, "UpdateLatest"},
            {6, &IDatabaseService::BuildRandom, "BuildRandom"},
            {7, &IDatabaseService::BuildDefault, "BuildDefault"},
            {8, &IDatabaseService::Get2, "Get2"},
            {9, &IDatabaseService::Get3, "Get3"},
            {10, &IDatabaseService::UpdateLatest1, "UpdateLatest1"},
            {11, &IDatabaseService::FindIndex, "FindIndex"},
            {12, &IDatabaseService::Move, "Move"},
            {13, &IDatabaseService::AddOrReplace, "AddOrReplace"},
            {14, &IDatabaseService::Delete, "Delete"},
            {15, &IDatabaseService::DestroyFile, "DestroyFile"},
            {16, &IDatabaseService::DeleteFile, "DeleteFile"},
            {17, &IDatabaseService::Format, "Format"},
            {18, nullptr, "Import"},
            {19, nullptr, "Export"},
            {20, &IDatabaseService::IsBrokenDatabaseWithClearFlag, "IsBrokenDatabaseWithClearFlag"},
            {21, &IDatabaseService::GetIndex, "GetIndex"},
            {22, &IDatabaseService::SetInterfaceVersion, "SetInterfaceVersion"},
            {23, &IDatabaseService::Convert, "Convert"},
            {24, &IDatabaseService::ConvertCoreDataToCharInfo, "ConvertCoreDataToCharInfo"},
            {25, &IDatabaseService::ConvertCharInfoToCoreData, "ConvertCharInfoToCoreData"},
            {26,  &IDatabaseService::Append, "Append"},
        };
        // clang-format on

        RegisterHandlers(functions);

        manager.Initialize(metadata);
    }

private:
    void IsUpdated(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto source_flag{rp.PopRaw<SourceFlag>()};

        LOG_DEBUG(Service_Mii, "called with source_flag={}", source_flag);

        const bool is_updated = manager.IsUpdated(metadata, source_flag);

        IPC::ResponseBuilder rb{ctx, 3};
        rb.Push(ResultSuccess);
        rb.Push<u8>(is_updated);
    }

    void IsFullDatabase(HLERequestContext& ctx) {
        LOG_DEBUG(Service_Mii, "called");

        const bool is_full_database = manager.IsFullDatabase();

        IPC::ResponseBuilder rb{ctx, 3};
        rb.Push(ResultSuccess);
        rb.Push<u8>(is_full_database);
    }

    void GetCount(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto source_flag{rp.PopRaw<SourceFlag>()};

        const u32 mii_count = manager.GetCount(metadata, source_flag);

        LOG_DEBUG(Service_Mii, "called with source_flag={}, mii_count={}", source_flag, mii_count);

        IPC::ResponseBuilder rb{ctx, 3};
        rb.Push(ResultSuccess);
        rb.Push(mii_count);
    }

    void Get(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto source_flag{rp.PopRaw<SourceFlag>()};
        const auto output_size{ctx.GetWriteBufferNumElements<CharInfoElement>()};

        u32 mii_count{};
        std::vector<CharInfoElement> char_info_elements(output_size);
        const auto result = manager.Get(metadata, char_info_elements, mii_count, source_flag);

        if (mii_count != 0) {
            ctx.WriteBuffer(char_info_elements);
        }

        LOG_INFO(Service_Mii, "called with source_flag={}, out_size={}, mii_count={}", source_flag,
                 output_size, mii_count);

        IPC::ResponseBuilder rb{ctx, 3};
        rb.Push(result);
        rb.Push(mii_count);
    }

    void Get1(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto source_flag{rp.PopRaw<SourceFlag>()};
        const auto output_size{ctx.GetWriteBufferNumElements<CharInfo>()};

        u32 mii_count{};
        std::vector<CharInfo> char_info(output_size);
        const auto result = manager.Get(metadata, char_info, mii_count, source_flag);

        if (mii_count != 0) {
            ctx.WriteBuffer(char_info);
        }

        LOG_INFO(Service_Mii, "called with source_flag={}, out_size={}, mii_count={}", source_flag,
                 output_size, mii_count);

        IPC::ResponseBuilder rb{ctx, 3};
        rb.Push(result);
        rb.Push(mii_count);
    }

    void UpdateLatest(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto char_info{rp.PopRaw<CharInfo>()};
        const auto source_flag{rp.PopRaw<SourceFlag>()};

        LOG_INFO(Service_Mii, "called with source_flag={}", source_flag);

        CharInfo new_char_info{};
        const auto result = manager.UpdateLatest(metadata, new_char_info, char_info, source_flag);
        if (result.IsFailure()) {
            IPC::ResponseBuilder rb{ctx, 2};
            rb.Push(result);
            return;
        }

        IPC::ResponseBuilder rb{ctx, 2 + sizeof(CharInfo) / sizeof(u32)};
        rb.Push(ResultSuccess);
        rb.PushRaw(new_char_info);
    }

    void BuildRandom(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto age{rp.PopRaw<Age>()};
        const auto gender{rp.PopRaw<Gender>()};
        const auto race{rp.PopRaw<Race>()};

        LOG_DEBUG(Service_Mii, "called with age={}, gender={}, race={}", age, gender, race);

        if (age > Age::All) {
            IPC::ResponseBuilder rb{ctx, 2};
            rb.Push(ResultInvalidArgument);
            return;
        }

        if (gender > Gender::All) {
            IPC::ResponseBuilder rb{ctx, 2};
            rb.Push(ResultInvalidArgument);
            return;
        }

        if (race > Race::All) {
            IPC::ResponseBuilder rb{ctx, 2};
            rb.Push(ResultInvalidArgument);
            return;
        }

        CharInfo char_info{};
        manager.BuildRandom(char_info, age, gender, race);

        IPC::ResponseBuilder rb{ctx, 2 + sizeof(CharInfo) / sizeof(u32)};
        rb.Push(ResultSuccess);
        rb.PushRaw(char_info);
    }

    void BuildDefault(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto index{rp.Pop<u32>()};

        LOG_DEBUG(Service_Mii, "called with index={}", index);

        if (index > 5) {
            IPC::ResponseBuilder rb{ctx, 2};
            rb.Push(ResultInvalidArgument);
            return;
        }

        CharInfo char_info{};
        manager.BuildDefault(char_info, index);

        IPC::ResponseBuilder rb{ctx, 2 + sizeof(CharInfo) / sizeof(u32)};
        rb.Push(ResultSuccess);
        rb.PushRaw(char_info);
    }

    void Get2(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto source_flag{rp.PopRaw<SourceFlag>()};
        const auto output_size{ctx.GetWriteBufferNumElements<StoreDataElement>()};

        u32 mii_count{};
        std::vector<StoreDataElement> store_data_elements(output_size);
        const auto result = manager.Get(metadata, store_data_elements, mii_count, source_flag);

        if (mii_count != 0) {
            ctx.WriteBuffer(store_data_elements);
        }

        LOG_INFO(Service_Mii, "called with source_flag={}, out_size={}, mii_count={}", source_flag,
                 output_size, mii_count);

        IPC::ResponseBuilder rb{ctx, 3};
        rb.Push(result);
        rb.Push(mii_count);
    }

    void Get3(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto source_flag{rp.PopRaw<SourceFlag>()};
        const auto output_size{ctx.GetWriteBufferNumElements<StoreData>()};

        u32 mii_count{};
        std::vector<StoreData> store_data(output_size);
        const auto result = manager.Get(metadata, store_data, mii_count, source_flag);

        if (mii_count != 0) {
            ctx.WriteBuffer(store_data);
        }

        LOG_INFO(Service_Mii, "called with source_flag={}, out_size={}, mii_count={}", source_flag,
                 output_size, mii_count);

        IPC::ResponseBuilder rb{ctx, 3};
        rb.Push(result);
        rb.Push(mii_count);
    }

    void UpdateLatest1(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto store_data{rp.PopRaw<StoreData>()};
        const auto source_flag{rp.PopRaw<SourceFlag>()};

        LOG_INFO(Service_Mii, "called with source_flag={}", source_flag);

        Result result = ResultSuccess;
        if (!is_system) {
            result = ResultPermissionDenied;
        }

        StoreData new_store_data{};
        if (result.IsSuccess()) {
            result = manager.UpdateLatest(metadata, new_store_data, store_data, source_flag);
        }

        if (result.IsFailure()) {
            IPC::ResponseBuilder rb{ctx, 2};
            rb.Push(result);
            return;
        }

        IPC::ResponseBuilder rb{ctx, 2 + sizeof(StoreData) / sizeof(u32)};
        rb.Push(ResultSuccess);
        rb.PushRaw<StoreData>(new_store_data);
    }

    void FindIndex(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto create_id{rp.PopRaw<Common::UUID>()};
        const auto is_special{rp.PopRaw<bool>()};

        LOG_INFO(Service_Mii, "called with create_id={}, is_special={}",
                 create_id.FormattedString(), is_special);

        const s32 index = manager.FindIndex(create_id, is_special);

        IPC::ResponseBuilder rb{ctx, 3};
        rb.Push(ResultSuccess);
        rb.Push(index);
    }

    void Move(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto create_id{rp.PopRaw<Common::UUID>()};
        const auto new_index{rp.PopRaw<s32>()};

        LOG_INFO(Service_Mii, "called with create_id={}, new_index={}", create_id.FormattedString(),
                 new_index);

        Result result = ResultSuccess;
        if (!is_system) {
            result = ResultPermissionDenied;
        }

        if (result.IsSuccess()) {
            const u32 count = manager.GetCount(metadata, SourceFlag::Database);
            if (new_index < 0 || new_index >= static_cast<s32>(count)) {
                result = ResultInvalidArgument;
            }
        }

        if (result.IsSuccess()) {
            result = manager.Move(metadata, new_index, create_id);
        }

        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
    }

    void AddOrReplace(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto store_data{rp.PopRaw<StoreData>()};

        LOG_INFO(Service_Mii, "called");

        Result result = ResultSuccess;

        if (!is_system) {
            result = ResultPermissionDenied;
        }

        if (result.IsSuccess()) {
            result = manager.AddOrReplace(metadata, store_data);
        }

        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
    }

    void Delete(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto create_id{rp.PopRaw<Common::UUID>()};

        LOG_INFO(Service_Mii, "called, create_id={}", create_id.FormattedString());

        Result result = ResultSuccess;

        if (!is_system) {
            result = ResultPermissionDenied;
        }

        if (result.IsSuccess()) {
            result = manager.Delete(metadata, create_id);
        }

        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
    }

    void DestroyFile(HLERequestContext& ctx) {
        // This calls nn::settings::fwdbg::GetSettingsItemValue("is_db_test_mode_enabled");
        const bool is_db_test_mode_enabled = false;

        LOG_INFO(Service_Mii, "called is_db_test_mode_enabled={}", is_db_test_mode_enabled);

        Result result = ResultSuccess;

        if (!is_db_test_mode_enabled) {
            result = ResultTestModeOnly;
        }

        if (result.IsSuccess()) {
            result = manager.DestroyFile(metadata);
        }

        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
    }

    void DeleteFile(HLERequestContext& ctx) {
        // This calls nn::settings::fwdbg::GetSettingsItemValue("is_db_test_mode_enabled");
        const bool is_db_test_mode_enabled = false;

        LOG_INFO(Service_Mii, "called is_db_test_mode_enabled={}", is_db_test_mode_enabled);

        Result result = ResultSuccess;

        if (!is_db_test_mode_enabled) {
            result = ResultTestModeOnly;
        }

        if (result.IsSuccess()) {
            result = manager.DeleteFile();
        }

        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
    }

    void Format(HLERequestContext& ctx) {
        // This calls nn::settings::fwdbg::GetSettingsItemValue("is_db_test_mode_enabled");
        const bool is_db_test_mode_enabled = false;

        LOG_INFO(Service_Mii, "called is_db_test_mode_enabled={}", is_db_test_mode_enabled);

        Result result = ResultSuccess;

        if (!is_db_test_mode_enabled) {
            result = ResultTestModeOnly;
        }

        if (result.IsSuccess()) {
            result = manager.Format(metadata);
        }

        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
    }

    void IsBrokenDatabaseWithClearFlag(HLERequestContext& ctx) {
        LOG_DEBUG(Service_Mii, "called");

        bool is_broken_with_clear_flag = false;
        Result result = ResultSuccess;

        if (!is_system) {
            result = ResultPermissionDenied;
        }

        if (result.IsSuccess()) {
            is_broken_with_clear_flag = manager.IsBrokenWithClearFlag(metadata);
        }

        IPC::ResponseBuilder rb{ctx, 3};
        rb.Push(result);
        rb.Push<u8>(is_broken_with_clear_flag);
    }

    void GetIndex(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto info{rp.PopRaw<CharInfo>()};

        LOG_DEBUG(Service_Mii, "called");

        s32 index{};
        const auto result = manager.GetIndex(metadata, info, index);

        IPC::ResponseBuilder rb{ctx, 3};
        rb.Push(result);
        rb.Push(index);
    }

    void SetInterfaceVersion(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto interface_version{rp.PopRaw<u32>()};

        LOG_INFO(Service_Mii, "called, interface_version={:08X}", interface_version);

        manager.SetInterfaceVersion(metadata, interface_version);

        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(ResultSuccess);
    }

    void Convert(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto mii_v3{rp.PopRaw<Ver3StoreData>()};

        LOG_INFO(Service_Mii, "called");

        CharInfo char_info{};
        const auto result = manager.ConvertV3ToCharInfo(char_info, mii_v3);

        IPC::ResponseBuilder rb{ctx, 2 + sizeof(CharInfo) / sizeof(u32)};
        rb.Push(result);
        rb.PushRaw<CharInfo>(char_info);
    }

    void ConvertCoreDataToCharInfo(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto core_data{rp.PopRaw<CoreData>()};

        LOG_INFO(Service_Mii, "called");

        CharInfo char_info{};
        const auto result = manager.ConvertCoreDataToCharInfo(char_info, core_data);

        IPC::ResponseBuilder rb{ctx, 2 + sizeof(CharInfo) / sizeof(u32)};
        rb.Push(result);
        rb.PushRaw<CharInfo>(char_info);
    }

    void ConvertCharInfoToCoreData(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto char_info{rp.PopRaw<CharInfo>()};

        LOG_INFO(Service_Mii, "called");

        CoreData core_data{};
        const auto result = manager.ConvertCharInfoToCoreData(core_data, char_info);

        IPC::ResponseBuilder rb{ctx, 2 + sizeof(CoreData) / sizeof(u32)};
        rb.Push(result);
        rb.PushRaw<CoreData>(core_data);
    }

    void Append(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto char_info{rp.PopRaw<CharInfo>()};

        LOG_INFO(Service_Mii, "called");

        const auto result = manager.Append(metadata, char_info);

        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
    }

    MiiManager manager{};
    DatabaseSessionMetadata metadata{};
    bool is_system{};
};

class MiiDBModule final : public ServiceFramework<MiiDBModule> {
public:
    explicit MiiDBModule(Core::System& system_, const char* name_, bool is_system_)
        : ServiceFramework{system_, name_}, is_system{is_system_} {
        // clang-format off
        static const FunctionInfo functions[] = {
            {0, &MiiDBModule::GetDatabaseService, "GetDatabaseService"},
        };
        // clang-format on

        RegisterHandlers(functions);
    }

private:
    void GetDatabaseService(HLERequestContext& ctx) {
        IPC::ResponseBuilder rb{ctx, 2, 0, 1};
        rb.Push(ResultSuccess);
        rb.PushIpcInterface<IDatabaseService>(system, is_system);

        LOG_DEBUG(Service_Mii, "called");
    }

    bool is_system{};
};

class MiiImg final : public ServiceFramework<MiiImg> {
public:
    explicit MiiImg(Core::System& system_) : ServiceFramework{system_, "miiimg"} {
        // clang-format off
        static const FunctionInfo functions[] = {
            {0, &MiiImg::Initialize, "Initialize"},
            {10, nullptr, "Reload"},
            {11, &MiiImg::GetCount, "GetCount"},
            {12, nullptr, "IsEmpty"},
            {13, nullptr, "IsFull"},
            {14, nullptr, "GetAttribute"},
            {15, nullptr, "LoadImage"},
            {16, nullptr, "AddOrUpdateImage"},
            {17, nullptr, "DeleteImages"},
            {100, nullptr, "DeleteFile"},
            {101, nullptr, "DestroyFile"},
            {102, nullptr, "ImportFile"},
            {103, nullptr, "ExportFile"},
            {104, nullptr, "ForceInitialize"},
        };
        // clang-format on

        RegisterHandlers(functions);
    }

private:
    void Initialize(HLERequestContext& ctx) {
        LOG_INFO(Service_Mii, "called");

        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(ResultSuccess);
    }

    void GetCount(HLERequestContext& ctx) {
        LOG_DEBUG(Service_Mii, "called");

        IPC::ResponseBuilder rb{ctx, 3};
        rb.Push(ResultSuccess);
        rb.Push(0);
    }
};

void LoopProcess(Core::System& system) {
    auto server_manager = std::make_unique<ServerManager>(system);

    server_manager->RegisterNamedService("mii:e",
                                         std::make_shared<MiiDBModule>(system, "mii:e", true));
    server_manager->RegisterNamedService("mii:u",
                                         std::make_shared<MiiDBModule>(system, "mii:u", false));
    server_manager->RegisterNamedService("miiimg", std::make_shared<MiiImg>(system));
    ServerManager::RunServer(std::move(server_manager));
}

} // namespace Service::Mii