Code spaghetti and more chaos

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97
2024-01-27 22:47:28 +02:00
parent 4cd236ed80
commit 7317105e4d
21 changed files with 613 additions and 391 deletions

View File

@@ -0,0 +1,103 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2024 Trial97 <alexandru.tripon97@gmail.com>
*
* 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, version 3.
*
* 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 "JavaDownload.h"
#include <qdialogbuttonbox.h>
#include <qlogging.h>
#include <qmessagebox.h>
#include <qpushbutton.h>
#include <memory>
#include "Application.h"
#include "BaseVersionList.h"
#include "FileSystem.h"
#include "QObjectPtr.h"
#include "SysInfo.h"
#include "java/JavaInstallList.h"
#include "java/download/ArchiveJavaDownloader.h"
#include "java/download/ManifestJavaDownloader.h"
#include "meta/Index.h"
#include "meta/Version.h"
#include "ui/dialogs/ProgressDialog.h"
#include "ui/java/ListModel.h"
#include "ui_JavaDownload.h"
JavaDownload::JavaDownload(QWidget* parent) : QDialog(parent), ui(new Ui::JavaDownload)
{
ui->setupUi(this);
ui->widget->initialize(new Java::JavaBaseVersionList("net.minecraft.java"));
ui->widget->selectCurrent();
connect(ui->widget, &VersionSelectWidget::selectedVersionChanged, this, &JavaDownload::setSelectedVersion);
auto reset = ui->buttonBox->button(QDialogButtonBox::Reset);
connect(reset, &QPushButton::clicked, this, &JavaDownload::refresh);
connect(ui->widget_2, &VersionSelectWidget::selectedVersionChanged, this, &JavaDownload::setSelectedVersion2);
}
JavaDownload::~JavaDownload()
{
delete ui;
}
void JavaDownload::setSelectedVersion(BaseVersion::Ptr version)
{
if (!version)
return;
auto dcast = std::dynamic_pointer_cast<Meta::Version>(version);
if (!dcast) {
return;
}
ui->widget_2->initialize(new Java::InstallList(dcast, this));
ui->widget_2->selectCurrent();
}
void JavaDownload::setSelectedVersion2(BaseVersion::Ptr version)
{
if (!version)
return;
m_selectedVersion = std::dynamic_pointer_cast<Java::JavaRuntime2>(ui->widget_2->selectedVersion());
}
void JavaDownload::accept()
{
if (!m_selectedVersion) {
m_selectedVersion = std::dynamic_pointer_cast<Java::JavaRuntime2>(ui->widget_2->selectedVersion());
qDebug() << "=========?" << (ui->widget_2->selectedVersion() != nullptr);
}
if (!m_selectedVersion) {
qDebug() << "faillllllllllllllllllllllllllll";
return;
}
auto meta = m_selectedVersion->meta;
Task::Ptr task;
auto final_path = FS::PathCombine(APPLICATION->dataRoot(), "java", meta->name);
qDebug() << "===============>>=>>" << meta->checksumType << meta->checksumHash;
switch (meta->downloadType) {
case JavaRuntime::DownloadType::Manifest:
task = makeShared<ManifestJavaDownloader>(meta->url, final_path, meta->checksumType, meta->checksumHash);
break;
case JavaRuntime::DownloadType::Archive:
task = makeShared<ArchiveJavaDownloader>(meta->url, final_path, meta->checksumType, meta->checksumHash);
break;
}
ProgressDialog pg(this);
pg.execWithTask(task.get());
QDialog::accept();
}
void JavaDownload::refresh()
{
ui->widget->loadList();
}

View File

@@ -0,0 +1,48 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2024 Trial97 <alexandru.tripon97@gmail.com>
*
* 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, version 3.
*
* 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/>.
*/
#pragma once
#include <QDialog>
#include "BaseVersion.h"
#include "ui/java/ListModel.h"
namespace Ui {
class JavaDownload;
}
class JavaDownload : public QDialog {
Q_OBJECT
public:
explicit JavaDownload(QWidget* parent = 0);
~JavaDownload();
void accept();
public slots:
void refresh();
protected slots:
void setSelectedVersion(BaseVersion::Ptr version);
void setSelectedVersion2(BaseVersion::Ptr version);
private:
Ui::JavaDownload* ui;
Java::JavaRuntimePtr m_selectedVersion;
};

View File

@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>JavaDownload</class>
<widget class="QDialog" name="JavaDownload">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>821</width>
<height>593</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,5">
<item>
<widget class="QGroupBox" name="majorGB">
<property name="title">
<string>Major</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="VersionSelectWidget" name="widget" native="true"/>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="runtimeGB">
<property name="title">
<string>Runtime</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="VersionSelectWidget" name="widget_2" native="true"/>
</item>
</layout>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::Retry</set>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>VersionSelectWidget</class>
<extends>QWidget</extends>
<header>ui/widgets/VersionSelectWidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>JavaDownload</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>JavaDownload</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@@ -0,0 +1,154 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2024 Trial97 <alexandru.tripon97@gmail.com>
*
* 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, version 3.
*
* 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 "ListModel.h"
#include <qlogging.h>
#include <memory>
#include "BaseVersionList.h"
#include "StringUtils.h"
#include "SysInfo.h"
namespace Java {
InstallList::InstallList(Meta::Version::Ptr version, QObject* parent) : BaseVersionList(parent), m_version(version)
{
if (version->isLoaded())
sortVersions();
}
Task::Ptr InstallList::getLoadTask()
{
m_version->load(Net::Mode::Online);
auto task = m_version->getCurrentTask();
connect(task.get(), &Task::finished, this, &InstallList::sortVersions);
return task;
}
const BaseVersion::Ptr InstallList::at(int i) const
{
return m_vlist.at(i);
}
bool InstallList::isLoaded()
{
return m_version->isLoaded();
}
int InstallList::count() const
{
return m_vlist.count();
}
QVariant InstallList::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() > count())
return QVariant();
auto version = (m_vlist[index.row()]);
switch (role) {
case SortRole:
return -index.row();
case VersionPointerRole:
return QVariant::fromValue(std::dynamic_pointer_cast<BaseVersion>(m_vlist[index.row()]));
case VersionIdRole:
return version->descriptor();
case VersionRole:
return version->meta->version.toString();
case RecommendedRole:
return version->meta->recommended;
case AliasRole:
return version->meta->name;
case ArchitectureRole:
return version->meta->vendor;
default:
return QVariant();
}
}
BaseVersionList::RoleList InstallList::providesRoles() const
{
return { VersionPointerRole, VersionIdRole, VersionRole, RecommendedRole, AliasRole, ArchitectureRole };
}
bool sortJavas(BaseVersion::Ptr left, BaseVersion::Ptr right)
{
auto rleft = std::dynamic_pointer_cast<JavaRuntime2>(right);
auto rright = std::dynamic_pointer_cast<JavaRuntime2>(left);
return (*rleft) > (*rright);
}
void InstallList::sortVersions()
{
QString versionStr = SysInfo::getSupportedJavaArchitecture();
beginResetModel();
auto runtimes = m_version->data()->runtimes;
if (versionStr.isEmpty() || !runtimes.contains(versionStr)) {
return;
}
auto javaruntimes = runtimes.value(versionStr);
for (auto v : javaruntimes) {
m_vlist.append(std::make_shared<JavaRuntime2>(v));
}
std::sort(m_vlist.begin(), m_vlist.end(), sortJavas);
endResetModel();
}
bool JavaRuntime2::operator<(const JavaRuntime2& rhs)
{
auto id = meta->version;
if (id < rhs.meta->version) {
return true;
}
if (id > rhs.meta->version) {
return false;
}
return StringUtils::naturalCompare(meta->name, rhs.meta->name, Qt::CaseInsensitive) < 0;
}
bool JavaRuntime2::operator==(const JavaRuntime2& rhs)
{
return meta->version == rhs.meta->version && meta->name == rhs.meta->name;
}
bool JavaRuntime2::operator>(const JavaRuntime2& rhs)
{
return (!operator<(rhs)) && (!operator==(rhs));
}
bool JavaRuntime2::operator<(BaseVersion& a)
{
try {
return operator<(dynamic_cast<JavaRuntime2&>(a));
} catch (const std::bad_cast& e) {
return BaseVersion::operator<(a);
}
}
bool JavaRuntime2::operator>(BaseVersion& a)
{
try {
return operator>(dynamic_cast<JavaRuntime2&>(a));
} catch (const std::bad_cast& e) {
return BaseVersion::operator>(a);
}
}
} // namespace Java

View File

@@ -0,0 +1,132 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2024 Trial97 <alexandru.tripon97@gmail.com>
*
* 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, version 3.
*
* 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/>.
*/
#pragma once
#include <qtmetamacros.h>
#include <QAbstractListModel>
#include <QSortFilterProxyModel>
#include "java/JavaRuntime.h"
#include "meta/VersionList.h"
namespace Java {
class JavaBaseVersionList : public Meta::VersionList {
Q_OBJECT
public:
explicit JavaBaseVersionList(const QString& uid, QObject* parent = nullptr) : VersionList(uid, parent) {}
BaseVersionList::RoleList providesRoles() const { return { VersionRole, RecommendedRole, VersionPointerRole }; }
};
struct JavaRuntime2 : public BaseVersion {
JavaRuntime2() {}
JavaRuntime2(JavaRuntime::MetaPtr m) : meta(m) {}
virtual QString descriptor() override { return meta->version.toString(); }
virtual QString name() override { return meta->name; }
virtual QString typeString() const override { return meta->vendor; }
virtual bool operator<(BaseVersion& a) override;
virtual bool operator>(BaseVersion& a) override;
bool operator<(const JavaRuntime2& rhs);
bool operator==(const JavaRuntime2& rhs);
bool operator>(const JavaRuntime2& rhs);
JavaRuntime::MetaPtr meta;
};
using JavaRuntimePtr = std::shared_ptr<JavaRuntime2>;
class InstallList : public BaseVersionList {
Q_OBJECT
public:
explicit InstallList(Meta::Version::Ptr m_version, QObject* parent = 0);
Task::Ptr getLoadTask() override;
bool isLoaded() override;
const BaseVersion::Ptr at(int i) const override;
int count() const override;
void sortVersions() override;
QVariant data(const QModelIndex& index, int role) const override;
RoleList providesRoles() const override;
protected slots:
void updateListData(QList<BaseVersion::Ptr>) override {}
protected:
Meta::Version::Ptr m_version;
QList<JavaRuntimePtr> m_vlist;
};
} // namespace Java
// class FilterModel : public QSortFilterProxyModel {
// Q_OBJECT
// public:
// FilterModel(QObject* parent = Q_NULLPTR);
// enum Sorting { ByName, ByGameVersion };
// const QMap<QString, Sorting> getAvailableSortings();
// QString translateCurrentSorting();
// void setSorting(Sorting sorting);
// Sorting getCurrentSorting();
// void setSearchTerm(QString term);
// protected:
// bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const override;
// bool lessThan(const QModelIndex& left, const QModelIndex& right) const override;
// private:
// QMap<QString, Sorting> sortings;
// Sorting currentSorting;
// QString searchTerm;
// };
// class ListModel : public QAbstractListModel {
// Q_OBJECT
// private:
// ModpackList modpacks;
// QStringList m_failedLogos;
// QStringList m_loadingLogos;
// FTBLogoMap m_logoMap;
// QMap<QString, LogoCallback> waitingCallbacks;
// void requestLogo(QString file);
// QString translatePackType(PackType type) const;
// private slots:
// void logoFailed(QString logo);
// void logoLoaded(QString logo, QIcon out);
// public:
// ListModel(QObject* parent);
// ~ListModel();
// int rowCount(const QModelIndex& parent) const override;
// int columnCount(const QModelIndex& parent) const override;
// QVariant data(const QModelIndex& index, int role) const override;
// Qt::ItemFlags flags(const QModelIndex& index) const override;
// void fill(ModpackList modpacks);
// void addPack(Modpack modpack);
// void clear();
// void remove(int row);
// Modpack at(int row);
// void getLogo(const QString& logo, LogoCallback callback);
// };