Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize Mod models / pages and APIs to Resources #675

Merged
merged 23 commits into from Jan 23, 2023

Conversation

flowln
Copy link
Contributor

@flowln flowln commented Dec 24, 2022

This aims to generalize the existing logic for the mod downloader, in order to allow for other resource downloaders (like RPs, TPs, Shaders and Worlds) to be implemented without much too code duplication. I wrote a big big description here, talking that stuff can still change as I develop those downloader and find things that could change for the better here, but GH deleted it, so yea that's basically it 😭

It also aims to decouple the model from the page, in order to facilitate a future switch to a QML based interface.

Be sure to check out the commit descriptions, I wrote quite a lot in a couple of them to explain what I was doing and why.

TODO:

  • Add some tests: There could be a couple automated tests, especially for the model, testing some basic stuff. For more complex stuff though, we could have something like a Dummy subclass of ResourceAPI to provide testing data for the model. We could have a minimal target compiled for testing that just shows the ResourceDownloadDialog with the model hooked up to that API. No idea how to implement that though, and it seems like a lot of work, so if anyone has better ideas, please share! 🙂
    I've concluded that if I really go forward with adding all those tests here, i'll never finish this PR bc I don't really want to make these tests at this very moment. So, I've laid down some basic initial infrastructure to support more tests in the future, when someone (or me) is up to the task.

@flowln flowln added enhancement New feature or request needs-testing PRs that should be tested a bit more before being merged labels Dec 24, 2022
@flowln flowln added this to the 7.0 milestone Dec 24, 2022
launcher/modplatform/ResourceAPI.h Fixed Show fixed Hide fixed
launcher/modplatform/ResourceAPI.h Fixed Show fixed Hide fixed
m_ui->sortByBox->addItem(sorting.readable_name, QVariant(sorting.index));
}

bool ResourcePage::setCurrentPack(ModPlatform::IndexedPack pack)

Check notice

Code scanning / CodeQL

Large object passed by value

This parameter of type [IndexedPack](1) is 152 bytes - consider passing a const pointer/reference instead.

class ResourcePage;

class ResourceDownloadDialog : public QDialog, public BasePageProvider {

Check notice

Code scanning / CodeQL

Undisciplined multiple inheritance

Multiple inheritance should not be used with 1 interfaces, 0 private implementations, 0 protected implementations, and 1 public implementations.
Comment on lines +82 to +88
switch (type) {
case ModPlatform::ResourceType::MOD:
return "mod";
default:
qWarning() << "Invalid resource type for Modrinth API!";
break;
}

Check notice

Code scanning / CodeQL

No trivial switch statements

This switch statement should either handle more cases, or be rewritten as an if statement.
Comment on lines +22 to +26
switch (type) {
default:
case ModPlatform::ResourceType::MOD:
return 6;
}

Check notice

Code scanning / CodeQL

No trivial switch statements

This switch statement should either handle more cases, or be rewritten as an if statement.
Comment on lines +361 to +369
switch (network_error_code) {
default:
// Network error
QMessageBox::critical(nullptr, tr("Error"), tr("A network error occurred. Could not load mods."));
break;
case 409:
// 409 Gone, notify user to update
QMessageBox::critical(nullptr, tr("Error"),
QString("%1").arg(tr("API version too old!\nPlease update %1!").arg(BuildConfig.LAUNCHER_DISPLAYNAME)));
break;
}

Check notice

Code scanning / CodeQL

No trivial switch statements

This switch statement should either handle more cases, or be rewritten as an if statement.
@flowln flowln force-pushed the generalize_mod_model branch 2 times, most recently from e3a58f3 to e8b099e Compare January 3, 2023 20:00
@flowln flowln marked this pull request as ready for review January 3, 2023 22:12
Firstly, this abstract away behavior in the mod download models that can
also be applied to other types of resources into a superclass, allowing
other resource types to be implemented without so much code duplication.

For that, this also generalizes the APIs used (currently, ModrinthAPI
and FlameAPI) to be able to make requests to other types of resources.

It also does a general cleanup of both of those. In particular, this
makes use of std::optional instead of invalid values for errors and,
well, optional values :p

This is a squash of some commits that were becoming too interlaced
together to be cleanly separated.

Signed-off-by: flow <flowlnlnln@gmail.com>
Puts them all inside the 'ResourceDownload' namespace, so that it's a
bit clearer from the outside that those belong to the same 'module'.

Signed-off-by: flow <flowlnlnln@gmail.com>
when i.e. clicking on links or just using the downloader at all, this
prevents some flickering and the widget never getting hidden in some
cases.

Signed-off-by: flow <flowlnlnln@gmail.com>
No need for multiple files since the subclasses are so small now

Signed-off-by: flow <flowlnlnln@gmail.com>
This makes it so that we don't need a reference to the parent page in
the model. It will be useful once we change the page from a widget-based
one to a QML page.

It also makes tasks be created in the dialog instead of the page, so
that the dialog can also have the necessary information to mark versions
as selected / deselected easily. It also makes the task pointers into
smart pointers.

Signed-off-by: flow <flowlnlnln@gmail.com>
in preparation for QML interop.

Signed-off-by: flow <flowlnlnln@gmail.com>
Signed-off-by: flow <flowlnlnln@gmail.com>
This refactors the sorting methods to join every bit of it into a single
list, easing maintanance. It also removes the weird index contraint on
the list of methods by adding an index field to the DS that holds the
method.

Lastly, it puts the available methods on their respective API, so other
resources on the same API can re-use them later on.

Signed-off-by: flow <flowlnlnln@gmail.com>
This prevents a crash in which the pack list gets updated in a search
request meanwhile a versions / extra info request is being processed.
Previously, this situation would cause the reference in the latter
callbacks to be invalidated by an internal relocation of the pack list.

Signed-off-by: flow <flowlnlnln@gmail.com>
This allows us to check whether a search request is already on-going, in
which case we don't need to make another one (and shouldn't).

Signed-off-by: flow <flowlnlnln@gmail.com>
Signed-off-by: flow <flowlnlnln@gmail.com>
While implementing the resource pack downloader in another branch, I
noticed that most of the code in the success callback was identical in
both cases, safe for a few minute differences in strings. So, this tries
to make it easier to share this piece of code.

However, it still leaves the possibility of extending the methods in
ResourceModel to accomodate for cases where this similarity may not
hold.

Signed-off-by: flow <flowlnlnln@gmail.com>
shush

Signed-off-by: flow <flowlnlnln@gmail.com>
This makes it easier to create resource apis that aren't network-based.

Signed-off-by: flow <flowlnlnln@gmail.com>
This allows the standard QAbstractItemModelTester to work without
shenanigans!

Signed-off-by: flow <flowlnlnln@gmail.com>
______very_____ basic indeed, creating tests is super boring :c

Signed-off-by: flow <flowlnlnln@gmail.com>
Signed-off-by: flow <flowlnlnln@gmail.com>
…rces

Signed-off-by: flow <flowlnlnln@gmail.com>
Prevents a single problematic mod from invalidating all the API
response.

Signed-off-by: flow <flowlnlnln@gmail.com>
The spec says that this can be null, and indeed some mods have it set to
null, and should still be considered as valid.

Signed-off-by: flow <flowlnlnln@gmail.com>
Copy link
Member

@Scrumplex Scrumplex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

About the license headers: I would suggest updating them everywhere you see fit.

Maybe add the updated SPDX lines as well

launcher/ResourceDownloadTask.cpp Outdated Show resolved Hide resolved
launcher/minecraft/PackProfile.h Show resolved Hide resolved
*sobbing in messy legal stuff i know nothing about*

Signed-off-by: flow <flowlnlnln@gmail.com>
@flowln flowln requested a review from Scrumplex January 23, 2023 14:14
@Scrumplex Scrumplex merged commit 16477a8 into PrismLauncher:develop Jan 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs-testing PRs that should be tested a bit more before being merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants