aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.UI.Common/SystemInfo/LinuxSystemInfo.cs
blob: c7fe05a0921da700bc1fe3e849fdffeec98dde5b (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
using Ryujinx.Common.Logging;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.Versioning;

namespace Ryujinx.UI.Common.SystemInfo
{
    [SupportedOSPlatform("linux")]
    class LinuxSystemInfo : SystemInfo
    {
        internal LinuxSystemInfo()
        {
            string cpuName = GetCpuidCpuName();

            if (cpuName == null)
            {
                var cpuDict = new Dictionary<string, string>(StringComparer.Ordinal)
                {
                    ["model name"] = null,
                    ["Processor"] = null,
                    ["Hardware"] = null,
                };

                ParseKeyValues("/proc/cpuinfo", cpuDict);

                cpuName = cpuDict["model name"] ?? cpuDict["Processor"] ?? cpuDict["Hardware"] ?? "Unknown";
            }

            var memDict = new Dictionary<string, string>(StringComparer.Ordinal)
            {
                ["MemTotal"] = null,
                ["MemAvailable"] = null,
            };

            ParseKeyValues("/proc/meminfo", memDict);

            // Entries are in KiB
            ulong.TryParse(memDict["MemTotal"]?.Split(' ')[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out ulong totalKiB);
            ulong.TryParse(memDict["MemAvailable"]?.Split(' ')[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out ulong availableKiB);

            CpuName = $"{cpuName} ; {LogicalCoreCount} logical";
            RamTotal = totalKiB * 1024;
            RamAvailable = availableKiB * 1024;
        }

        private static void ParseKeyValues(string filePath, Dictionary<string, string> itemDict)
        {
            if (!File.Exists(filePath))
            {
                Logger.Error?.Print(LogClass.Application, $"File \"{filePath}\" not found");

                return;
            }

            int count = itemDict.Count;

            using StreamReader file = new(filePath);

            string line;
            while ((line = file.ReadLine()) != null)
            {
                string[] kvPair = line.Split(':', 2, StringSplitOptions.TrimEntries);

                if (kvPair.Length < 2)
                {
                    continue;
                }

                string key = kvPair[0];

                if (itemDict.TryGetValue(key, out string value) && value == null)
                {
                    itemDict[key] = kvPair[1];

                    if (--count <= 0)
                    {
                        break;
                    }
                }
            }
        }
    }
}