aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Services/Acc/IAccountServiceForApplication.cs
blob: 36dfd9efa9a3dea78712a306c6e2433de549b8e1 (plain)
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
using Ryujinx.HLE.HOS.Ipc;
using Ryujinx.HLE.HOS.SystemState;
using Ryujinx.HLE.Logging;
using System.Collections.Generic;

using static Ryujinx.HLE.HOS.ErrorCode;

namespace Ryujinx.HLE.HOS.Services.Acc
{
    class IAccountServiceForApplication : IpcService
    {
        private Dictionary<int, ServiceProcessRequest> m_Commands;

        public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;

        public IAccountServiceForApplication()
        {
            m_Commands = new Dictionary<int, ServiceProcessRequest>()
            {
                { 0,   GetUserCount                        },
                { 1,   GetUserExistence                    },
                { 2,   ListAllUsers                        },
                { 3,   ListOpenUsers                       },
                { 4,   GetLastOpenedUser                   },
                { 5,   GetProfile                          },
                { 100, InitializeApplicationInfo           },
                { 101, GetBaasAccountManagerForApplication }
            };
        }

        public long GetUserCount(ServiceCtx Context)
        {
            Context.ResponseData.Write(Context.Device.System.State.GetUserCount());

            return 0;
        }

        public long GetUserExistence(ServiceCtx Context)
        {
            UserId Uuid = new UserId(
                Context.RequestData.ReadInt64(),
                Context.RequestData.ReadInt64());

            Context.ResponseData.Write(Context.Device.System.State.TryGetUser(Uuid, out _) ? 1 : 0);

            return 0;
        }

        public long ListAllUsers(ServiceCtx Context)
        {
            return WriteUserList(Context, Context.Device.System.State.GetAllUsers());
        }

        public long ListOpenUsers(ServiceCtx Context)
        {
            return WriteUserList(Context, Context.Device.System.State.GetOpenUsers());
        }

        private long WriteUserList(ServiceCtx Context, IEnumerable<UserProfile> Profiles)
        {
            long OutputPosition = Context.Request.RecvListBuff[0].Position;
            long OutputSize     = Context.Request.RecvListBuff[0].Size;

            long Offset = 0;

            foreach (UserProfile Profile in Profiles)
            {
                if ((ulong)Offset + 16 > (ulong)OutputSize)
                {
                    break;
                }

                byte[] Uuid = Profile.Uuid.Bytes;

                for (int Index = Uuid.Length - 1; Index >= 0; Index--)
                {
                    Context.Memory.WriteByte(OutputPosition + Offset++, Uuid[Index]);
                }
            }

            return 0;
        }

        public long GetLastOpenedUser(ServiceCtx Context)
        {
            UserProfile LastOpened = Context.Device.System.State.LastOpenUser;

            LastOpened.Uuid.Write(Context.ResponseData);

            return 0;
        }

        public long GetProfile(ServiceCtx Context)
        {
            UserId Uuid = new UserId(
                Context.RequestData.ReadInt64(),
                Context.RequestData.ReadInt64());

            if (!Context.Device.System.State.TryGetUser(Uuid, out UserProfile Profile))
            {
                Context.Device.Log.PrintWarning(LogClass.ServiceAcc, $"User 0x{Uuid} not found!");

                return MakeError(ErrorModule.Account, AccErr.UserNotFound);
            }

            MakeObject(Context, new IProfile(Profile));

            return 0;
        }

        public long InitializeApplicationInfo(ServiceCtx Context)
        {
            Context.Device.Log.PrintStub(LogClass.ServiceAcc, "Stubbed.");

            return 0;
        }

        public long GetBaasAccountManagerForApplication(ServiceCtx Context)
        {
            MakeObject(Context, new IManagerForApplication());

            return 0;
        }
    }
}