aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/SystemState/SystemStateMgr.cs
blob: d1d54d0eaf8af89a9fd65d3c7483dac6e2b8dcff (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
using Ryujinx.HLE.Utilities;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

namespace Ryujinx.HLE.HOS.SystemState
{
    public class SystemStateMgr
    {
        internal static string[] LanguageCodes = new string[]
        {
            "ja",
            "en-US",
            "fr",
            "de",
            "it",
            "es",
            "zh-CN",
            "ko",
            "nl",
            "pt",
            "ru",
            "zh-TW",
            "en-GB",
            "fr-CA",
            "es-419",
            "zh-Hans",
            "zh-Hant"
        };

        internal static string[] AudioOutputs = new string[]
        {
            "AudioTvOutput",
            "AudioStereoJackOutput",
            "AudioBuiltInSpeakerOutput"
        };

        internal long DesiredLanguageCode { get; private set; }

        public TitleLanguage DesiredTitleLanguage { get; private set; }

        internal string ActiveAudioOutput { get; private set; }

        public bool DockedMode { get; set; }

        public ColorSet ThemeColor { get; set; }

        public bool InstallContents { get; set; }

        private ConcurrentDictionary<string, UserProfile> _profiles;

        internal UserProfile LastOpenUser { get; private set; }

        public SystemStateMgr()
        {
            SetAudioOutputAsBuiltInSpeaker();

            _profiles = new ConcurrentDictionary<string, UserProfile>();

            UInt128 defaultUuid = new UInt128("00000000000000000000000000000001");

            AddUser(defaultUuid, "Player");

            OpenUser(defaultUuid);
        }

        public void SetLanguage(SystemLanguage language)
        {
            DesiredLanguageCode = GetLanguageCode((int)language);

            switch (language)
            {
                case SystemLanguage.Taiwanese:
                case SystemLanguage.TraditionalChinese:
                    DesiredTitleLanguage = TitleLanguage.Taiwanese;
                    break;
                case SystemLanguage.Chinese:
                case SystemLanguage.SimplifiedChinese:
                    DesiredTitleLanguage = TitleLanguage.Chinese;
                    break;
                default:
                    DesiredTitleLanguage = Enum.Parse<TitleLanguage>(Enum.GetName(typeof(SystemLanguage), language));
                    break;
            }
        }

        public void SetAudioOutputAsTv()
        {
            ActiveAudioOutput = AudioOutputs[0];
        }

        public void SetAudioOutputAsStereoJack()
        {
            ActiveAudioOutput = AudioOutputs[1];
        }

        public void SetAudioOutputAsBuiltInSpeaker()
        {
            ActiveAudioOutput = AudioOutputs[2];
        }

        public void AddUser(UInt128 uuid, string name)
        {
            UserProfile profile = new UserProfile(uuid, name);

            _profiles.AddOrUpdate(uuid.ToString(), profile, (key, old) => profile);
        }

        public void OpenUser(UInt128 uuid)
        {
            if (_profiles.TryGetValue(uuid.ToString(), out UserProfile profile))
            {
                (LastOpenUser = profile).AccountState = OpenCloseState.Open;
            }
        }

        public void CloseUser(UInt128 uuid)
        {
            if (_profiles.TryGetValue(uuid.ToString(), out UserProfile profile))
            {
                profile.AccountState = OpenCloseState.Closed;
            }
        }

        public int GetUserCount()
        {
            return _profiles.Count;
        }

        internal bool TryGetUser(UInt128 uuid, out UserProfile profile)
        {
            return _profiles.TryGetValue(uuid.ToString(), out profile);
        }

        internal IEnumerable<UserProfile> GetAllUsers()
        {
            return _profiles.Values;
        }

        internal IEnumerable<UserProfile> GetOpenUsers()
        {
            return _profiles.Values.Where(x => x.AccountState == OpenCloseState.Open);
        }

        internal static long GetLanguageCode(int index)
        {
            if ((uint)index >= LanguageCodes.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }

            long code  = 0;
            int  shift = 0;

            foreach (char chr in LanguageCodes[index])
            {
                code |= (long)(byte)chr << shift++ * 8;
            }

            return code;
        }
    }
}