2
0
mirror of https://github.com/Alia5/GlosSI.git synced 2024-11-01 09:20:17 +00:00
GlosSI/GlosSIConfig/UIModel.cpp

833 lines
30 KiB
C++
Raw Normal View History

/*
Copyright 2021-2022 Peter Repukat - FlatspotSoftware
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "UIModel.h"
#include <QDir>
#include <QFont>
2021-10-25 16:00:34 +00:00
#include <QGuiApplication>
2021-10-28 09:41:27 +00:00
#include <QJsonDocument>
#include <QJsonArray>
2022-09-26 02:33:21 +00:00
#include <QNetworkAccessManager>
#include <QNetworkReply>
2021-10-25 16:00:34 +00:00
#include <WinReg/WinReg.hpp>
2022-09-26 02:33:21 +00:00
#include <ranges>
2021-10-28 09:41:27 +00:00
#ifdef _WIN32
#include "UWPFetch.h"
#include <Windows.h>
#include <shlobj.h>
2021-10-24 23:14:34 +00:00
#endif
#include "ExeImageProvider.h"
2022-09-25 12:29:52 +00:00
#include "../version.hpp"
UIModel::UIModel() : QObject(nullptr)
{
wchar_t* localAppDataFolder;
std::filesystem::path path;
if (SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, NULL, &localAppDataFolder) != S_OK) {
path = std::filesystem::temp_directory_path().parent_path().parent_path().parent_path();
}
else {
path = std::filesystem::path(localAppDataFolder).parent_path();
}
path /= "Roaming";
path /= "GlosSI";
if (!std::filesystem::exists(path))
std::filesystem::create_directories(path);
2022-09-25 12:31:04 +00:00
qDebug() << "Version: " << getVersionString();
config_path_ = path;
config_dir_name_ = QString::fromStdWString((path /= "Targets").wstring());
2021-10-24 20:21:27 +00:00
if (!std::filesystem::exists(path))
std::filesystem::create_directories(path);
parseShortcutVDF();
readTargetConfigs();
2022-09-26 02:33:21 +00:00
updateCheck();
2022-10-15 13:50:40 +00:00
auto font = QGuiApplication::font();
font.setPointSize(11);
font.setFamily("Roboto");
QGuiApplication::setFont(font);
}
void UIModel::readTargetConfigs()
{
QDir dir(config_dir_name_);
auto entries = dir.entryList(QDir::Files, QDir::SortFlag::Name);
2022-09-26 02:33:21 +00:00
entries.removeIf([](const auto& entry) { return !entry.endsWith(".json"); });
2021-10-28 09:41:27 +00:00
std::for_each(entries.begin(), entries.end(), [this](const auto& name) {
auto path = config_path_;
path /= config_dir_name_.toStdWString();
path /= name.toStdWString();
2021-10-28 09:41:27 +00:00
QFile file(path);
if (!file.open(QIODevice::Text | QIODevice::ReadOnly)) {
// meh
return;
}
const auto data = file.readAll();
file.close();
const auto jsondoc = QJsonDocument::fromJson(data);
auto filejson = jsondoc.object();
2022-09-26 02:33:21 +00:00
filejson["name"] = filejson.contains("name") ? filejson["name"].toString()
: QString(name).replace(QRegularExpression("\\.json"), "");
targets_.append(filejson.toVariantMap());
2021-10-28 09:41:27 +00:00
});
emit targetListChanged();
}
2022-09-26 02:33:21 +00:00
QVariantList UIModel::getTargetList() const { return targets_; }
void UIModel::addTarget(QVariant shortcut)
{
2021-10-24 20:21:27 +00:00
const auto map = shortcut.toMap();
const auto json = QJsonObject::fromVariantMap(map);
writeTarget(json, map["name"].toString());
targets_.append(QJsonDocument(json).toVariant());
emit targetListChanged();
}
2022-10-15 13:50:04 +00:00
bool UIModel::updateTarget(int index, QVariant shortcut)
{
2021-10-24 20:21:27 +00:00
const auto map = shortcut.toMap();
const auto json = QJsonObject::fromVariantMap(map);
2022-10-15 13:50:04 +00:00
auto oldSteamName = targets_[index].toMap()["name"].toString();
2022-09-26 02:33:21 +00:00
auto oldName =
targets_[index].toMap()["name"].toString().replace(QRegularExpression("[\\\\/:*?\"<>|]"), "") + ".json";
2022-10-15 13:50:04 +00:00
auto oldPath = config_path_;
oldPath /= config_dir_name_.toStdString();
oldPath /= (oldName).toStdString();
std::filesystem::remove(oldPath);
writeTarget(json, map["name"].toString());
2021-10-24 23:14:34 +00:00
targets_.replace(index, QJsonDocument(json).toVariant());
emit targetListChanged();
2022-10-15 13:50:04 +00:00
auto path = config_path_;
path /= config_dir_name_.toStdString();
path /= (map["name"].toString()).toStdString();
if (removeFromSteam(oldSteamName, QString::fromStdWString(path.wstring()))) {
if (!addToSteam(shortcut, QString::fromStdWString(path.wstring()))) {
qDebug() << "Couldn't add shortcut \"" << (map["name"].toString()) << "\" to Steam when updating";
return false;
}
return true;
}
qDebug() << "Couldn't remove shortcut \"" << oldName << "\" from Steam when updating";
return false;
}
2021-10-24 23:14:34 +00:00
void UIModel::deleteTarget(int index)
{
2022-09-26 02:33:21 +00:00
auto oldName =
targets_[index].toMap()["name"].toString().replace(QRegularExpression("[\\\\/:*?\"<>|]"), "") + ".json";
2021-10-24 23:14:34 +00:00
auto path = config_path_;
path /= config_dir_name_.toStdString();
path /= (oldName).toStdString();
std::filesystem::remove(path);
targets_.remove(index);
emit targetListChanged();
}
bool UIModel::isInSteam(QVariant shortcut) const
{
const auto map = shortcut.toMap();
2022-09-08 19:40:29 +00:00
for (auto& steam_shortcut : shortcuts_vdf_) {
if (map["name"].toString() == QString::fromStdString(steam_shortcut.appname)) {
if (QString::fromStdString(steam_shortcut.exe).toLower().contains("glossitarget.exe")) {
return true;
}
}
}
return false;
}
uint32_t UIModel::getAppId(QVariant shortcut)
{
if (!isInSteam(shortcut)) {
return 0;
}
const auto map = shortcut.toMap();
for (auto& steam_shortcut : shortcuts_vdf_) {
if (map["name"].toString() == QString::fromStdString(steam_shortcut.appname)) {
if (QString::fromStdString(steam_shortcut.exe).toLower().contains("glossitarget.exe")) {
if (steam_shortcut.appid == 0) {
parseShortcutVDF();
return getAppId(shortcut);
}
return steam_shortcut.appid;
}
}
}
return 0;
}
bool UIModel::addToSteam(QVariant shortcut, const QString& shortcutspath, bool from_cmd)
{
2021-10-28 18:33:59 +00:00
QDir appDir = QGuiApplication::applicationDirPath();
const auto map = shortcut.toMap();
const auto name = map["name"].toString();
const auto maybeLaunchPath = map["launchPath"].toString();
const auto launch = map["launch"].toBool();
VDFParser::Shortcut vdfshortcut;
2022-09-08 19:40:29 +00:00
vdfshortcut.appname = name.toStdString();
vdfshortcut.exe = ("\"" + appDir.absolutePath() + "/GlosSITarget.exe" + "\"").toStdString();
2022-09-26 02:33:21 +00:00
vdfshortcut.StartDir =
(launch && !maybeLaunchPath.isEmpty()
? (std::string("\"") + std::filesystem::path(maybeLaunchPath.toStdString()).parent_path().string() + "\"")
: ("\"" + appDir.absolutePath() + "\"").toStdString());
// ShortcutPath; default
2022-09-26 02:33:21 +00:00
vdfshortcut.LaunchOptions =
(QString(name).replace(QRegularExpression("[\\\\/:*?\"<>|]"), "") + ".json").toStdString();
// IsHidden; default
// AllowDesktopConfig; default
// AllowOverlay; default
// openvr; default
// Devkit; default
// DevkitGameID; default
// DevkitOverrideAppID; default
// LastPlayTime; default
auto maybeIcon = map["icon"].toString();
2021-10-28 09:41:27 +00:00
if (maybeIcon.isEmpty()) {
if (launch && !maybeLaunchPath.isEmpty())
2022-09-08 19:40:29 +00:00
vdfshortcut.icon =
2022-09-26 02:33:21 +00:00
"\"" +
(is_windows_ ? QString(maybeLaunchPath).replace(QRegularExpression("\\/"), "\\").toStdString()
: maybeLaunchPath.toStdString()) +
"\"";
2021-10-28 09:41:27 +00:00
}
else {
2022-09-26 02:33:21 +00:00
vdfshortcut.icon = "\"" +
(is_windows_ ? QString(maybeIcon).replace(QRegularExpression("\\/"), "\\").toStdString()
: maybeIcon.toStdString()) +
"\"";
}
// Add installed locally and GlosSI tag
2022-09-08 19:40:29 +00:00
vdfshortcut.tags.push_back("Installed locally");
vdfshortcut.tags.push_back("GlosSI");
2022-09-08 19:40:29 +00:00
shortcuts_vdf_.push_back(vdfshortcut);
return writeShortcutsVDF(L"add", name.toStdWString(), shortcutspath.toStdWString(), from_cmd);
}
bool UIModel::addToSteam(const QString& name, const QString& shortcutspath, bool from_cmd)
{
qDebug() << "trying to add " << name << " to steam";
const auto target = std::find_if(targets_.begin(), targets_.end(), [&name](const auto& target) {
const auto map = target.toMap();
const auto target_name = map["name"].toString().replace(QRegularExpression("[\\\\/:*?\"<>|]"), "");
return name == target_name;
});
if (target != targets_.end()) {
return addToSteam(*target, shortcutspath, from_cmd);
}
qDebug() << name << " not found!";
return false;
}
bool UIModel::removeFromSteam(const QString& name, const QString& shortcutspath, bool from_cmd)
{
qDebug() << "trying to remove " << name << " from steam";
2022-09-26 02:33:21 +00:00
shortcuts_vdf_.erase(
std::ranges::remove_if(shortcuts_vdf_,
[&name](const auto& shortcut) { return shortcut.appname == name.toStdString(); })
.begin(),
shortcuts_vdf_.end());
return writeShortcutsVDF(L"remove", name.toStdWString(), shortcutspath.toStdWString(), from_cmd);
}
QVariantMap UIModel::manualProps(QVariant shortcut)
{
QDir appDir = QGuiApplication::applicationDirPath();
const auto map = shortcut.toMap();
const auto name = map["name"].toString().replace(QRegularExpression("[\\\\/:*?\"<>|]"), "");
const auto maybeLaunchPath = map["launchPath"].toString();
const auto launch = map["launch"].toBool();
QVariantMap res;
res.insert("name", name);
res.insert("config", name + ".json");
res.insert("launch", ("\"" + appDir.absolutePath() + "/GlosSITarget.exe" + "\""));
2022-09-26 02:33:21 +00:00
res.insert(
"launchDir",
(launch && !maybeLaunchPath.isEmpty()
? (QString("\"") +
QString::fromStdString(std::filesystem::path(maybeLaunchPath.toStdString()).parent_path().string()) +
"\"")
: ("\"" + appDir.absolutePath() + "\"")));
return res;
}
void UIModel::enableSteamInputXboxSupport()
{
if (foundSteam()) {
2022-09-26 02:33:21 +00:00
const std::filesystem::path config_path = std::wstring(getSteamPath()) + user_data_path_.toStdWString() +
getSteamUserId() + user_config_file_.toStdWString();
if (!std::filesystem::exists(config_path)) {
qDebug() << "localconfig.vdf does not exist.";
}
QFile file(config_path);
if (file.open(QIODevice::Text | QIODevice::ReadOnly)) {
QTextStream in(&file);
QStringList lines;
QString line = in.readLine();
// simple approach is enough...
while (!in.atEnd()) {
if (line.contains("SteamController_XBoxSupport")) {
if (line.contains("1")) {
qDebug() << "\"SteamController_XBoxSupport\" is already enabled! aborting write...";
file.close();
return;
}
qDebug() << "found \"SteamController_XBoxSupport\" line, replacing value...";
line.replace("0", "1");
}
lines.push_back(line);
line = in.readLine();
}
file.close();
QFile updatedFile(config_path);
if (updatedFile.open(QFile::WriteOnly | QFile::Truncate | QFile::Text)) {
qDebug() << "writing localconfig.vdf...";
QTextStream out(&updatedFile);
for (const auto& l : lines) {
out << l << "\n";
}
}
updatedFile.close();
}
}
}
bool UIModel::restartSteam(const QString& steamURL)
{
const auto path = getSteamPath();
if (QProcess::execute("taskkill.exe", {"/im", steam_executable_name_, "/f"}) != 0) {
return false;
}
if (steamURL.isEmpty()) {
QProcess::startDetached(QString::fromStdWString(path) + "/" + steam_executable_name_);
}
else {
system((QString::fromLatin1("start ") + steamURL).toStdString().c_str());
}
return true;
}
2022-09-26 02:33:21 +00:00
void UIModel::updateCheck()
2021-10-24 23:14:34 +00:00
{
2022-09-26 02:33:21 +00:00
auto manager = new QNetworkAccessManager();
QNetworkRequest request;
QNetworkReply* reply = NULL;
QSslConfiguration config = QSslConfiguration::defaultConfiguration();
config.setProtocol(QSsl::TlsV1_2);
request.setSslConfiguration(config);
request.setUrl(QUrl("https://glossi.1-3-3-7.dev/api/availFiles"));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
reply = manager->get(request);
// connect(
// manager, &QNetworkAccessManager::finished, this, [](QNetworkReply* rep) {
// qDebug() << rep->readAll();
// });
connect(manager, &QNetworkAccessManager::finished, this, &UIModel::onAvailFilesResponse);
2021-10-24 23:14:34 +00:00
}
2022-09-26 02:33:21 +00:00
QVariantMap UIModel::getDefaultConf() const
{
wchar_t* localAppDataFolder;
std::filesystem::path path;
if (SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, NULL, &localAppDataFolder) != S_OK) {
path = std::filesystem::temp_directory_path().parent_path().parent_path().parent_path();
}
else {
path = std::filesystem::path(localAppDataFolder).parent_path();
}
path /= "Roaming";
path /= "GlosSI";
path /= "default.json";
QJsonObject defaults = {
{"icon", QJsonValue::Null},
{"name", QJsonValue::Null},
{"version", 1},
{"extendedLogging", false},
{"snapshotNotify", false},
{"steamgridApiKey", QJsonValue::Null},
{"controller", QJsonObject{{"maxControllers", 1}, {"emulateDS4", false}, {"allowDesktopConfig", false}}},
{"devices",
QJsonObject{
{"hideDevices", true},
{"realDeviceIds", false},
}},
{"launch",
QJsonObject{
{"closeOnExit", true},
{"launch", false},
{"launchAppArgs", QJsonValue::Null},
{"launchPath", QJsonValue::Null},
{"waitForChildProcs", true},
{"launcherProcesses", QJsonArray{}},
{"ignoreLauncher", true},
{"killLauncher", false},
}},
{"window",
QJsonObject{
{"disableOverlay", false},
{"maxFps", QJsonValue::Null},
{"scale", QJsonValue::Null},
{"windowMode", false},
}},
};
if (std::filesystem::exists(path)) {
QFile file(QString::fromStdWString(path));
if (file.open(QIODevice::ReadOnly)) {
const auto data = file.readAll();
file.close();
auto json = QJsonDocument::fromJson(data).object();
const auto applyDefaults = [](QJsonObject obj, const QJsonObject& defaults,
auto applyDefaultsFn) -> QJsonObject {
for (const auto& key : defaults.keys()) {
qDebug() << key << ": " << obj[key];
if ((obj[key].isUndefined() || obj[key].isNull()) && !defaults[key].isNull()) {
obj[key] = defaults.value(key);
}
if (obj.value(key).isObject()) {
obj[key] =
applyDefaultsFn(obj[key].toObject(), defaults.value(key).toObject(), applyDefaultsFn);
}
}
return obj;
};
json = applyDefaults(json, defaults, applyDefaults);
return json.toVariantMap();
}
}
saveDefaultConf(defaults.toVariantMap());
return getDefaultConf();
}
void UIModel::saveDefaultConf(QVariantMap conf) const
{
wchar_t* localAppDataFolder;
std::filesystem::path path;
if (SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, NULL, &localAppDataFolder) != S_OK) {
path = std::filesystem::temp_directory_path().parent_path().parent_path().parent_path();
}
else {
path = std::filesystem::path(localAppDataFolder).parent_path();
}
path /= "Roaming";
path /= "GlosSI";
path /= "default.json";
QFile file(path);
if (!file.open(QIODevice::Text | QIODevice::ReadWrite)) {
qDebug() << "Couldn't open file for writing: " << path;
return;
}
file.write(QString(QJsonDocument::fromVariant(conf).toJson(QJsonDocument::Indented)).toStdString().data());
file.close();
}
2022-09-26 02:33:21 +00:00
#ifdef _WIN32
QVariantList UIModel::uwpApps() { return UWPFetch::UWPAppList(); }
2021-10-24 23:14:34 +00:00
#endif
QVariantList UIModel::egsGamesList() const
{
wchar_t* program_data_path_str;
std::filesystem::path path;
if (SHGetKnownFolderPath(FOLDERID_ProgramData, KF_FLAG_CREATE, NULL, &program_data_path_str) != S_OK) {
qDebug() << "Couldn't get ProgramDataPath";
return {{"InstallLocation", "Error"}};
}
path = std::filesystem::path(program_data_path_str);
path /= egs_games_json_path_;
QFile file(path);
if (file.open(QIODevice::ReadOnly)) {
const auto data = file.readAll();
file.close();
auto json = QJsonDocument::fromJson(data).object();
if (json["InstallationList"].isArray()) {
return json["InstallationList"].toVariant().toList();
}
qDebug() << "InstallationList does not exist!";
}
qDebug() << "Couldn't read EGS LauncherInstalled.dat " << path;
return {{"InstallLocation", "Error"}};
}
2022-10-15 22:04:33 +00:00
void UIModel::loadSteamGridImages()
{
std::filesystem::path path = QCoreApplication::applicationDirPath().toStdWString();
path /= "steamgrid.exe";
auto api_key = getDefaultConf().value("steamgridApiKey").toString();
steamgrid_output_.clear();
2022-10-15 22:04:33 +00:00
steamgrid_proc_.setProgram(path.string().c_str());
steamgrid_proc_.setArguments({"-nonsteamonly", "--onlymissingartwork", "--steamgriddb", api_key});
2022-10-15 22:04:33 +00:00
connect(&steamgrid_proc_, &QProcess::readyReadStandardOutput, this, &UIModel::onSteamGridReadReady);
steamgrid_proc_.start();
steamgrid_proc_.write("\n");
}
QString UIModel::getGridImagePath(QVariant shortcut)
{
if (!foundSteam()) {
return "";
}
const auto& app_id = getAppId(shortcut);
if (app_id == 0) {
return "";
}
const std::filesystem::path grid_dir =
std::wstring(getSteamPath()) + user_data_path_.toStdWString() + getSteamUserId() + L"/config/grid";
if (!std::filesystem::exists(grid_dir)) {
return "";
}
const std::vector<std::string> extensions = {".png", ".jpg"};
for (const auto& entry : std::filesystem::directory_iterator(grid_dir)) {
2022-10-15 22:04:33 +00:00
if (entry.is_regular_file() &&
std::ranges::find(extensions, entry.path().extension().string()) != extensions.end() &&
entry.path().filename().string().find(std::to_string(app_id)) != std::string::npos) {
return QString::fromStdString(entry.path().string());
}
}
return "";
}
2022-09-26 02:33:21 +00:00
bool UIModel::writeShortcutsVDF(const std::wstring& mode, const std::wstring& name, const std::wstring& shortcutspath,
bool is_admin_try) const
{
#ifdef _WIN32
const std::filesystem::path config_path = is_admin_try
? shortcutspath
2022-09-26 02:33:21 +00:00
: std::wstring(getSteamPath()) + user_data_path_.toStdWString() +
getSteamUserId() + shortcutsfile_.toStdWString();
qDebug() << "Steam config Path: " << config_path;
qDebug() << "Trying to write config as admin: " << is_admin_try;
bool write_res;
try {
2022-09-08 19:40:29 +00:00
write_res = VDFParser::Parser::writeShortcuts(config_path, shortcuts_vdf_, qDebug());
}
catch (const std::exception& e) {
qDebug() << "Couldn't backup shortcuts file: " << e.what();
}
if (!write_res && !is_admin_try) {
wchar_t szPath[MAX_PATH];
if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath))) {
// Launch itself as admin
SHELLEXECUTEINFO sei = {sizeof(sei)};
sei.lpVerb = L"runas";
qDebug() << QString("exepath: %1").arg(szPath);
sei.lpFile = szPath;
const std::wstring paramstr = mode + L" " + name + L" \"" + config_path.wstring() + L"\"";
sei.lpParameters = paramstr.c_str();
sei.hwnd = NULL;
sei.nShow = SW_NORMAL;
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
if (!ShellExecuteEx(&sei)) {
DWORD dwError = GetLastError();
if (dwError == ERROR_CANCELLED) {
qDebug() << "User cancelled UAC Prompt";
return false;
}
}
else {
qDebug() << QString("HProc: %1").arg((int)sei.hProcess);
if (sei.hProcess && WAIT_OBJECT_0 == WaitForSingleObject(sei.hProcess, INFINITE)) {
DWORD exitcode = 1;
GetExitCodeProcess(sei.hProcess, &exitcode);
qDebug() << QString("Exitcode: %1").arg((int)exitcode);
if (exitcode == 0) {
return true;
}
}
return false;
}
2022-03-05 15:27:32 +00:00
}
}
return write_res;
#else
return VDFParser::Parser::writeShortcuts(config_path, shortcuts_vdf_);
#endif
}
bool UIModel::getIsDebug() const
{
#ifdef _DEBUG
return true;
#else
return false;
#endif
}
2022-09-26 02:33:21 +00:00
bool UIModel::getIsWindows() const { return is_windows_; }
2022-09-26 02:33:21 +00:00
bool UIModel::hasAcrylicEffect() const { return has_acrylic_affect_; }
void UIModel::setAcrylicEffect(bool has_acrylic_affect)
{
has_acrylic_affect_ = has_acrylic_affect;
emit acrylicChanged();
}
2021-10-24 20:21:27 +00:00
2022-10-15 22:04:33 +00:00
QStringList UIModel::getSteamgridOutput() const { return steamgrid_output_; }
2022-09-26 02:33:21 +00:00
void UIModel::onAvailFilesResponse(QNetworkReply* reply)
{
const QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
qDebug() << "http status: " << status_code;
if (status_code.isValid() && status_code.toInt() == 200) {
const auto respStr = reply->readAll();
qDebug() << "AvailFiles response: " << respStr;
QJsonObject json = QJsonDocument::fromJson(respStr).object();
const auto defaultConf = getDefaultConf();
bool snapshotNotify =
defaultConf.contains("snapshotNotify") ? defaultConf["snapshotNotify"].toJsonValue().toBool() : false;
2022-09-26 02:33:21 +00:00
struct VersionInfo {
int major;
int minor;
int patch;
int revision;
int commits_since_last;
};
std::vector<std::pair<QString, VersionInfo>> new_versions;
for (const auto& info :
json.keys() | std::ranges::views::filter([this, &json, snapshotNotify](const auto& key) {
return notify_on_snapshots_
? true
: json[key].toObject().value("type") == (snapshotNotify ? "snapshot" : "release");
2022-09-26 02:33:21 +00:00
}) | std::ranges::views::transform([&json](const auto& key) -> std::pair<QString, VersionInfo> {
const auto versionString = json[key].toObject().value("version").toString();
const auto cleanVersion = versionString.split("-")[0];
const auto versionSplits = cleanVersion.split(".");
return {key,
{versionSplits[0].toInt(), versionSplits[1].toInt(), versionSplits[2].toInt(),
versionSplits[3].toInt(),
versionString.count('-') == 2 ? versionString.split("-")[1].toInt() : 0}};
}) | std::views::filter([](const auto& info) {
if (info.second.major > version::VERSION_MAJOR) {
return true;
}
if (info.second.major == version::VERSION_MAJOR && info.second.minor > version::VERSION_MINOR) {
return true;
}
if (info.second.major == version::VERSION_MAJOR && info.second.minor == version::VERSION_MINOR &&
info.second.patch > version::VERSION_PATCH) {
return true;
}
if (info.second.major == version::VERSION_MAJOR && info.second.minor == version::VERSION_MINOR &&
info.second.patch == version::VERSION_PATCH && info.second.revision > version::VERSION_REVISION) {
return true;
}
if (info.second.major == version::VERSION_MAJOR && info.second.minor == version::VERSION_MINOR &&
info.second.patch == version::VERSION_PATCH && info.second.revision == version::VERSION_REVISION &&
info.second.commits_since_last > (QString(version::VERSION_STR).count('-') == 2
? QString(version::VERSION_STR).split("-")[1].toInt()
: 0)) {
return true;
}
return false;
2022-09-26 02:33:21 +00:00
}) | std::ranges::views::all) {
new_versions.push_back(info);
}
std::ranges::sort(new_versions, [](const auto& a, const auto& b) { return a.first > b.first; });
if (!new_versions.empty()) {
qDebug() << "New version available: " << new_versions[0].first;
new_version_name_ = new_versions[0].first;
emit newVersionAvailable();
}
}
}
2022-10-15 22:04:33 +00:00
void UIModel::onSteamGridReadReady()
{
const auto output = QString::fromLocal8Bit(steamgrid_proc_.readAllStandardOutput());
steamgrid_output_.push_back(output);
2022-10-15 22:04:33 +00:00
emit steamgridOutputChanged();
if (output.toLower().contains("token is missing or invalid")) {
steamgrid_proc_.kill();
}
2022-10-15 22:04:33 +00:00
}
void UIModel::writeTarget(const QJsonObject& json, const QString& name) const
2021-10-24 20:21:27 +00:00
{
auto path = config_path_;
path /= config_dir_name_.toStdWString();
path /= (QString(name).replace(QRegularExpression("[\\\\/:*?\"<>|]"), "") + ".json").toStdWString();
2021-10-24 20:21:27 +00:00
QFile file(path);
2021-10-28 09:41:27 +00:00
if (!file.open(QIODevice::Text | QIODevice::ReadWrite)) {
qDebug() << "Couldn't open file for writing: " << path;
2021-10-24 20:21:27 +00:00
return;
}
auto json_ob = QJsonDocument(json).object();
json_ob.remove("steamgridApiKey");
file.write(QString(QJsonDocument(json_ob).toJson(QJsonDocument::Indented)).toStdString().data());
2021-10-24 20:21:27 +00:00
file.close();
}
2022-09-26 02:33:21 +00:00
QString UIModel::getVersionString() const { return QString(version::VERSION_STR); }
QString UIModel::getNewVersionName() const { return new_version_name_; }
2022-09-25 12:29:52 +00:00
std::filesystem::path UIModel::getSteamPath() const
{
2022-09-11 21:38:01 +00:00
try {
#ifdef _WIN32
2022-09-11 21:38:01 +00:00
// TODO: check if keys/value exist
// steam should always be open and have written reg values...
winreg::RegKey key{HKEY_CURRENT_USER, L"SOFTWARE\\Valve\\Steam"};
if (!key.IsValid()) {
return "";
}
const auto res = key.GetStringValue(L"SteamPath");
return res;
}
catch (...) {
return "";
}
#else
2022-09-26 02:33:21 +00:00
return L""; // TODO LINUX
#endif
}
std::wstring UIModel::getSteamUserId() const
{
#ifdef _WIN32
2022-09-11 21:38:01 +00:00
try {
// TODO: check if keys/value exist
// steam should always be open and have written reg values...
winreg::RegKey key{HKEY_CURRENT_USER, L"SOFTWARE\\Valve\\Steam\\ActiveProcess"};
if (!key.IsValid()) {
return L"0";
}
const auto res = std::to_wstring(key.GetDwordValue(L"ActiveUser"));
if (res == L"0") {
qDebug() << "Steam not open?";
}
return res;
2022-09-26 02:33:21 +00:00
}
catch (...) {
2022-09-11 21:38:01 +00:00
return L"0";
}
#else
2022-09-26 02:33:21 +00:00
return L""; // TODO LINUX
#endif
}
2022-09-11 21:38:01 +00:00
bool UIModel::foundSteam() const
{
if (getSteamPath() == "" || getSteamUserId() == L"0") {
return false;
}
2022-09-26 02:33:21 +00:00
const std::filesystem::path user_config_dir =
std::wstring(getSteamPath()) + user_data_path_.toStdWString() + getSteamUserId();
2022-09-11 21:38:01 +00:00
if (!std::filesystem::exists(user_config_dir)) {
return false;
}
return true;
}
void UIModel::parseShortcutVDF()
{
2022-09-26 02:33:21 +00:00
const std::filesystem::path config_path = std::wstring(getSteamPath()) + user_data_path_.toStdWString() +
getSteamUserId() + shortcutsfile_.toStdWString();
if (!std::filesystem::exists(config_path)) {
qDebug() << "Shortcuts file does not exist.";
return;
}
try {
2022-09-08 19:40:29 +00:00
shortcuts_vdf_ = VDFParser::Parser::parseShortcuts(config_path, qDebug());
}
catch (const std::exception& e) {
qDebug() << "Error parsing VDF: " << e.what();
}
}
bool UIModel::isSteamInputXboxSupportEnabled() const
{
// return true as default to not bug the user in error cases.
if (foundSteam()) {
2022-09-26 02:33:21 +00:00
const std::filesystem::path config_path = std::wstring(getSteamPath()) + user_data_path_.toStdWString() +
getSteamUserId() + user_config_file_.toStdWString();
if (!std::filesystem::exists(config_path)) {
qDebug() << "localconfig.vdf does not exist.";
return true;
}
QFile file(config_path);
if (file.open(QIODevice::Text | QIODevice::ReadOnly)) {
QTextStream in(&file);
QString line = in.readLine();
// simple, regex approach should be enough...
while (!in.atEnd()) {
if (line.contains("SteamController_XBoxSupport")) {
file.close();
if (line.contains("1")) {
qDebug() << "\"SteamController_XBoxSupport\" is enabled!";
return true;
}
qDebug() << "\"SteamController_XBoxSupport\" is disabled!";
return false;
}
line = in.readLine();
}
qDebug() << "couldn't find \"SteamController_XBoxSupport\" in localconfig.vdf";
file.close();
}
else {
qDebug() << "could not open localconfig.vdf";
}
}
return true;
}