summaryrefslogtreecommitdiff
path: root/meshmc/libraries/systeminfo/src
diff options
context:
space:
mode:
Diffstat (limited to 'meshmc/libraries/systeminfo/src')
-rw-r--r--meshmc/libraries/systeminfo/src/distroutils.cpp267
-rw-r--r--meshmc/libraries/systeminfo/src/sys_apple.cpp93
-rw-r--r--meshmc/libraries/systeminfo/src/sys_test.cpp52
-rw-r--r--meshmc/libraries/systeminfo/src/sys_unix.cpp128
-rw-r--r--meshmc/libraries/systeminfo/src/sys_win32.cpp79
5 files changed, 619 insertions, 0 deletions
diff --git a/meshmc/libraries/systeminfo/src/distroutils.cpp b/meshmc/libraries/systeminfo/src/distroutils.cpp
new file mode 100644
index 0000000000..3735874e7b
--- /dev/null
+++ b/meshmc/libraries/systeminfo/src/distroutils.cpp
@@ -0,0 +1,267 @@
+/*
+
+ SPDX-FileCopyrightText: 2026 Project Tick
+ SPDX-FileContributor: Project Tick
+ SPDX-License-Identifier: GPL-3.0-or-later
+
+Code has been taken from https://github.com/natefoo/lionshead and loosely
+translated to C++ laced with Qt.
+
+ MeshMC - A Custom Launcher for Minecraft
+ Copyright (C) 2026 Project Tick
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+ This file incorporates work covered by the following copyright and
+ permission notice:
+
+MIT License
+
+Copyright (c) 2017 Nate Coraor
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+*/
+
+#include "distroutils.h"
+
+#include <QStringList>
+#include <QMap>
+#include <QSettings>
+#include <QFile>
+#include <QProcess>
+#include <QDebug>
+#include <QDir>
+#include <QRegularExpression>
+
+#include <functional>
+
+Sys::DistributionInfo Sys::read_os_release()
+{
+ Sys::DistributionInfo out;
+ QStringList files = {"/etc/os-release", "/usr/lib/os-release"};
+ QString name;
+ QString version;
+ for (auto& file : files) {
+ if (!QFile::exists(file)) {
+ continue;
+ }
+ QSettings settings(file, QSettings::IniFormat);
+ if (settings.contains("ID")) {
+ name = settings.value("ID").toString().toLower();
+ } else if (settings.contains("NAME")) {
+ name = settings.value("NAME").toString().toLower();
+ } else {
+ continue;
+ }
+
+ if (settings.contains("VERSION_ID")) {
+ version = settings.value("VERSION_ID").toString().toLower();
+ } else if (settings.contains("VERSION")) {
+ version = settings.value("VERSION").toString().toLower();
+ }
+ break;
+ }
+ if (name.isEmpty()) {
+ return out;
+ }
+ out.distributionName = name;
+ out.distributionVersion = version;
+ return out;
+}
+
+bool Sys::main_lsb_info(Sys::LsbInfo& out)
+{
+ int status = 0;
+ QProcess lsbProcess;
+ lsbProcess.start("lsb_release -a");
+ lsbProcess.waitForFinished();
+ status = lsbProcess.exitStatus();
+ QString output = lsbProcess.readAllStandardOutput();
+ qDebug() << output;
+ lsbProcess.close();
+ if (status == 0) {
+ auto lines = output.split('\n');
+ for (auto line : lines) {
+ int index = line.indexOf(':');
+ auto key = line.left(index).trimmed();
+ auto value = line.mid(index + 1).toLower().trimmed();
+ if (key == "Distributor ID")
+ out.distributor = value;
+ else if (key == "Release")
+ out.version = value;
+ else if (key == "Description")
+ out.description = value;
+ else if (key == "Codename")
+ out.codename = value;
+ }
+ return !out.distributor.isEmpty();
+ }
+ return false;
+}
+
+bool Sys::fallback_lsb_info(Sys::LsbInfo& out)
+{
+ // running lsb_release failed, try to read the file instead
+ // /etc/lsb-release format, if the file even exists, is non-standard.
+ // Only the `lsb_release` command is specified by LSB. Nonetheless, some
+ // distributions install an /etc/lsb-release as part of the base
+ // distribution, but `lsb_release` remains optional.
+ QString file = "/etc/lsb-release";
+ if (QFile::exists(file)) {
+ QSettings settings(file, QSettings::IniFormat);
+ if (settings.contains("DISTRIB_ID")) {
+ out.distributor = settings.value("DISTRIB_ID").toString().toLower();
+ }
+ if (settings.contains("DISTRIB_RELEASE")) {
+ out.version =
+ settings.value("DISTRIB_RELEASE").toString().toLower();
+ }
+ return !out.distributor.isEmpty();
+ }
+ return false;
+}
+
+void Sys::lsb_postprocess(Sys::LsbInfo& lsb, Sys::DistributionInfo& out)
+{
+ QString dist = lsb.distributor;
+ QString vers = lsb.version;
+ if (dist.startsWith("redhatenterprise")) {
+ dist = "rhel";
+ } else if (dist == "archlinux") {
+ dist = "arch";
+ } else if (dist.startsWith("suse")) {
+ if (lsb.description.startsWith("opensuse")) {
+ dist = "opensuse";
+ } else if (lsb.description.startsWith("suse linux enterprise")) {
+ dist = "sles";
+ }
+ } else if (dist == "debian" and vers == "testing") {
+ vers = lsb.codename;
+ } else {
+ // ubuntu, debian, gentoo, scientific, slackware, ... ?
+ auto parts = dist.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
+ if (parts.size()) {
+ dist = parts[0];
+ }
+ }
+ if (!dist.isEmpty()) {
+ out.distributionName = dist;
+ out.distributionVersion = vers;
+ }
+}
+
+Sys::DistributionInfo Sys::read_lsb_release()
+{
+ LsbInfo lsb;
+ if (!main_lsb_info(lsb)) {
+ if (!fallback_lsb_info(lsb)) {
+ return Sys::DistributionInfo();
+ }
+ }
+ Sys::DistributionInfo out;
+ lsb_postprocess(lsb, out);
+ return out;
+}
+
+QString Sys::_extract_distribution(const QString& x)
+{
+ QString release = x.toLower();
+ if (release.startsWith("red hat enterprise")) {
+ return "rhel";
+ }
+ if (release.startsWith("suse linux enterprise")) {
+ return "sles";
+ }
+ QStringList list =
+ release.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
+ if (list.size()) {
+ return list[0];
+ }
+ return QString();
+}
+
+QString Sys::_extract_version(const QString& x)
+{
+ QRegularExpression versionish_string("\\d+(?:\\.\\d+)*$");
+ QStringList list = x.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
+ for (int i = list.size() - 1; i >= 0; --i) {
+ QString chunk = list[i];
+ if (versionish_string.match(chunk).hasMatch()) {
+ return chunk;
+ }
+ }
+ return QString();
+}
+
+Sys::DistributionInfo Sys::read_legacy_release()
+{
+ struct checkEntry {
+ QString file;
+ std::function<QString(const QString&)> extract_distro;
+ std::function<QString(const QString&)> extract_version;
+ };
+ QList<checkEntry> checks = {
+ {"/etc/arch-release", [](const QString&) { return "arch"; },
+ [](const QString&) { return "rolling"; }},
+ {"/etc/slackware-version", &Sys::_extract_distribution,
+ &Sys::_extract_version},
+ {QString(), &Sys::_extract_distribution, &Sys::_extract_version},
+ {"/etc/debian_version", [](const QString&) { return "debian"; },
+ [](const QString& x) { return x; }},
+ };
+ for (auto& check : checks) {
+ QStringList files;
+ if (check.file.isNull()) {
+ QDir etcDir("/etc");
+ etcDir.setNameFilters({"*-release"});
+ etcDir.setFilter(QDir::Files | QDir::NoDot | QDir::NoDotDot |
+ QDir::Readable | QDir::Hidden);
+ files = etcDir.entryList();
+ } else {
+ files.append(check.file);
+ }
+ for (auto file : files) {
+ QFile relfile(file);
+ if (!relfile.open(QIODevice::ReadOnly | QIODevice::Text))
+ continue;
+ QString contents = QString::fromUtf8(relfile.readLine()).trimmed();
+ QString dist = check.extract_distro(contents);
+ QString vers = check.extract_version(contents);
+ if (!dist.isEmpty()) {
+ Sys::DistributionInfo out;
+ out.distributionName = dist;
+ out.distributionVersion = vers;
+ return out;
+ }
+ }
+ }
+ return Sys::DistributionInfo();
+}
diff --git a/meshmc/libraries/systeminfo/src/sys_apple.cpp b/meshmc/libraries/systeminfo/src/sys_apple.cpp
new file mode 100644
index 0000000000..a6e44f3c08
--- /dev/null
+++ b/meshmc/libraries/systeminfo/src/sys_apple.cpp
@@ -0,0 +1,93 @@
+/* SPDX-FileCopyrightText: 2026 Project Tick
+ * SPDX-FileContributor: Project Tick
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *
+ * MeshMC - A Custom Launcher for Minecraft
+ * Copyright (C) 2026 Project Tick
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "sys.h"
+
+#include <sys/utsname.h>
+
+#include <QString>
+#include <QStringList>
+#include <QDebug>
+
+Sys::KernelInfo Sys::getKernelInfo()
+{
+ Sys::KernelInfo out;
+ struct utsname buf;
+ uname(&buf);
+ out.kernelType = KernelType::Darwin;
+ out.kernelName = buf.sysname;
+ QString release = out.kernelVersion = buf.release;
+
+ // TODO: figure out how to detect cursed-ness (macOS emulated on linux via
+ // mad hacks and so on)
+ out.isCursed = false;
+
+ out.kernelMajor = 0;
+ out.kernelMinor = 0;
+ out.kernelPatch = 0;
+ auto sections = release.split('-');
+ if (sections.size() >= 1) {
+ auto versionParts = sections[0].split('.');
+ if (versionParts.size() >= 3) {
+ out.kernelMajor = versionParts[0].toInt();
+ out.kernelMinor = versionParts[1].toInt();
+ out.kernelPatch = versionParts[2].toInt();
+ } else {
+ qWarning() << "Not enough version numbers in " << sections[0]
+ << " found " << versionParts.size();
+ }
+ } else {
+ qWarning() << "Not enough '-' sections in " << release << " found "
+ << sections.size();
+ }
+ return out;
+}
+
+#include <sys/sysctl.h>
+
+uint64_t Sys::getSystemRam()
+{
+ uint64_t memsize;
+ size_t memsizesize = sizeof(memsize);
+ if (!sysctlbyname("hw.memsize", &memsize, &memsizesize, NULL, 0)) {
+ return memsize;
+ } else {
+ return 0;
+ }
+}
+
+bool Sys::isCPU64bit()
+{
+ // not even going to pretend I'm going to support anything else
+ return true;
+}
+
+bool Sys::isSystem64bit()
+{
+ // yep. maybe when we have 128bit CPUs on consumer devices.
+ return true;
+}
+
+Sys::DistributionInfo Sys::getDistributionInfo()
+{
+ DistributionInfo result;
+ return result;
+}
diff --git a/meshmc/libraries/systeminfo/src/sys_test.cpp b/meshmc/libraries/systeminfo/src/sys_test.cpp
new file mode 100644
index 0000000000..a4c90321b3
--- /dev/null
+++ b/meshmc/libraries/systeminfo/src/sys_test.cpp
@@ -0,0 +1,52 @@
+/* SPDX-FileCopyrightText: 2026 Project Tick
+ * SPDX-FileContributor: Project Tick
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *
+ * MeshMC - A Custom Launcher for Minecraft
+ * Copyright (C) 2026 Project Tick
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <QTest>
+#include "TestUtil.h"
+
+#include <sys.h>
+
+class SysTest : public QObject
+{
+ Q_OBJECT
+ private slots:
+
+ void test_kernelNotNull()
+ {
+ auto kinfo = Sys::getKernelInfo();
+ QVERIFY(!kinfo.kernelName.isEmpty());
+ QVERIFY(kinfo.kernelVersion != "0.0");
+ }
+ /*
+ void test_systemDistroNotNull()
+ {
+ auto kinfo = Sys::getDistributionInfo();
+ QVERIFY(!kinfo.distributionName.isEmpty());
+ QVERIFY(!kinfo.distributionVersion.isEmpty());
+ qDebug() << "Distro: " << kinfo.distributionName << "version" <<
+ kinfo.distributionVersion;
+ }
+ */
+};
+
+QTEST_GUILESS_MAIN(SysTest)
+
+#include "sys_test.moc"
diff --git a/meshmc/libraries/systeminfo/src/sys_unix.cpp b/meshmc/libraries/systeminfo/src/sys_unix.cpp
new file mode 100644
index 0000000000..6d081678cf
--- /dev/null
+++ b/meshmc/libraries/systeminfo/src/sys_unix.cpp
@@ -0,0 +1,128 @@
+/* SPDX-FileCopyrightText: 2026 Project Tick
+ * SPDX-FileContributor: Project Tick
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *
+ * MeshMC - A Custom Launcher for Minecraft
+ * Copyright (C) 2026 Project Tick
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "sys.h"
+
+#include "distroutils.h"
+
+#include <sys/utsname.h>
+#include <fstream>
+#include <limits>
+
+#include <QString>
+#include <QStringList>
+#include <QDebug>
+
+Sys::KernelInfo Sys::getKernelInfo()
+{
+ Sys::KernelInfo out;
+ struct utsname buf;
+ uname(&buf);
+ // NOTE: we assume linux here. this needs further elaboration
+ out.kernelType = KernelType::Linux;
+ out.kernelName = buf.sysname;
+ QString release = out.kernelVersion = buf.release;
+
+ // linux binary running on WSL is cursed.
+ out.isCursed = release.contains("WSL", Qt::CaseInsensitive) ||
+ release.contains("Microsoft", Qt::CaseInsensitive);
+
+ out.kernelMajor = 0;
+ out.kernelMinor = 0;
+ out.kernelPatch = 0;
+ auto sections = release.split('-');
+ if (sections.size() >= 1) {
+ auto versionParts = sections[0].split('.');
+ if (versionParts.size() >= 3) {
+ out.kernelMajor = versionParts[0].toInt();
+ out.kernelMinor = versionParts[1].toInt();
+ out.kernelPatch = versionParts[2].toInt();
+ } else {
+ qWarning() << "Not enough version numbers in " << sections[0]
+ << " found " << versionParts.size();
+ }
+ } else {
+ qWarning() << "Not enough '-' sections in " << release << " found "
+ << sections.size();
+ }
+ return out;
+}
+
+uint64_t Sys::getSystemRam()
+{
+ std::string token;
+#ifdef Q_OS_LINUX
+ std::ifstream file("/proc/meminfo");
+ while (file >> token) {
+ if (token == "MemTotal:") {
+ uint64_t mem;
+ if (file >> mem) {
+ return mem * 1024ull;
+ } else {
+ return 0;
+ }
+ }
+ // ignore rest of the line
+ file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
+ }
+#elif defined(Q_OS_FREEBSD)
+ char buff[512];
+ FILE* fp = popen("sysctl hw.physmem", "r");
+ if (fp != NULL) {
+ while (fgets(buff, 512, fp) != NULL) {
+ std::string str(buff);
+ uint64_t mem = std::stoull(str.substr(12, std::string::npos));
+ return mem * 1024ull;
+ }
+ }
+#endif
+ return 0; // nothing found
+}
+
+bool Sys::isCPU64bit()
+{
+ return isSystem64bit();
+}
+
+bool Sys::isSystem64bit()
+{
+ // kernel build arch on linux
+ return QSysInfo::currentCpuArchitecture() == "x86_64";
+}
+
+Sys::DistributionInfo Sys::getDistributionInfo()
+{
+ DistributionInfo systemd_info = read_os_release();
+ DistributionInfo lsb_info = read_lsb_release();
+ DistributionInfo legacy_info = read_legacy_release();
+ DistributionInfo result = systemd_info + lsb_info + legacy_info;
+ if (result.distributionName.isNull()) {
+ result.distributionName = "unknown";
+ }
+ if (result.distributionVersion.isNull()) {
+ if (result.distributionName == "arch") {
+ result.distributionVersion = "rolling";
+ } else {
+ result.distributionVersion = "unknown";
+ }
+ }
+ return result;
+}
diff --git a/meshmc/libraries/systeminfo/src/sys_win32.cpp b/meshmc/libraries/systeminfo/src/sys_win32.cpp
new file mode 100644
index 0000000000..e75a5dcb8b
--- /dev/null
+++ b/meshmc/libraries/systeminfo/src/sys_win32.cpp
@@ -0,0 +1,79 @@
+/* SPDX-FileCopyrightText: 2026 Project Tick
+ * SPDX-FileContributor: Project Tick
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *
+ * MeshMC - A Custom Launcher for Minecraft
+ * Copyright (C) 2026 Project Tick
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "sys.h"
+
+#include <windows.h>
+
+Sys::KernelInfo Sys::getKernelInfo()
+{
+ Sys::KernelInfo out;
+ out.kernelType = KernelType::Windows;
+ out.kernelName = "Windows";
+ OSVERSIONINFOW osvi;
+ ZeroMemory(&osvi, sizeof(OSVERSIONINFOW));
+ osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
+ GetVersionExW(&osvi);
+ out.kernelVersion =
+ QString("%1.%2").arg(osvi.dwMajorVersion).arg(osvi.dwMinorVersion);
+ out.kernelMajor = osvi.dwMajorVersion;
+ out.kernelMinor = osvi.dwMinorVersion;
+ out.kernelPatch = osvi.dwBuildNumber;
+ return out;
+}
+
+uint64_t Sys::getSystemRam()
+{
+ MEMORYSTATUSEX status;
+ status.dwLength = sizeof(status);
+ GlobalMemoryStatusEx(&status);
+ // bytes
+ return (uint64_t)status.ullTotalPhys;
+}
+
+bool Sys::isSystem64bit()
+{
+#if defined(_WIN64)
+ return true;
+#elif defined(_WIN32)
+ BOOL f64 = false;
+ return IsWow64Process(GetCurrentProcess(), &f64) && f64;
+#else
+ // it's some other kind of system...
+ return false;
+#endif
+}
+
+bool Sys::isCPU64bit()
+{
+ SYSTEM_INFO info;
+ ZeroMemory(&info, sizeof(SYSTEM_INFO));
+ GetNativeSystemInfo(&info);
+ auto arch = info.wProcessorArchitecture;
+ return arch == PROCESSOR_ARCHITECTURE_AMD64 ||
+ arch == PROCESSOR_ARCHITECTURE_IA64;
+}
+
+Sys::DistributionInfo Sys::getDistributionInfo()
+{
+ DistributionInfo result;
+ return result;
+}