fix: importing packs from CurseForge with distribution disabled not prompting to download

This commit is contained in:
Ryan Dowling 2023-07-09 11:21:36 +10:00
parent 1f927bcfad
commit 548394bea2
No known key found for this signature in database
GPG key ID: 5539FCDB88950EFD
2 changed files with 96 additions and 20 deletions

View file

@ -10,6 +10,7 @@ This changelog only contains the changes that are unreleased. For changes for in
### Fixes
- Add missing server_run analytics event
- Run analytics only on version specified in config
- Importing packs from CurseForge with distribution disabled not prompting to download
### Misc
- Upgrade to Gradle 8.2

View file

@ -29,8 +29,12 @@ import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.mini2Dx.gettext.GetText;
import com.atlauncher.App;
import com.atlauncher.FileSystem;
import com.atlauncher.Gsons;
import com.atlauncher.builders.HTMLBuilder;
import com.atlauncher.data.curseforge.CurseForgeFile;
import com.atlauncher.data.curseforge.CurseForgeFileHash;
import com.atlauncher.data.curseforge.CurseForgeProject;
@ -41,6 +45,7 @@ import com.atlauncher.data.modrinth.pack.ModrinthModpackManifest;
import com.atlauncher.data.multimc.MultiMCInstanceConfig;
import com.atlauncher.data.multimc.MultiMCManifest;
import com.atlauncher.gui.dialogs.InstanceInstallerDialog;
import com.atlauncher.managers.DialogManager;
import com.atlauncher.managers.LogManager;
import com.atlauncher.network.Download;
@ -108,30 +113,100 @@ public class ImportPackUtils {
CurseForgeFile curseFile = CurseForgeApi.getFileForProject(projectId, fileId);
Path tempZip = FileSystem.TEMP.resolve(curseFile.fileName);
try {
Download download = new Download().setUrl(curseFile.downloadUrl).downloadTo(tempZip)
.size(curseFile.fileLength);
if (curseFile.downloadUrl == null) {
if (!App.settings.seenCurseForgeProjectDistributionDialog) {
App.settings.seenCurseForgeProjectDistributionDialog = true;
App.settings.save();
Optional<CurseForgeFileHash> md5Hash = curseFile.hashes.stream().filter(h -> h.isMd5())
.findFirst();
Optional<CurseForgeFileHash> sha1Hash = curseFile.hashes.stream().filter(h -> h.isSha1())
.findFirst();
if (md5Hash.isPresent()) {
download = download.hash(md5Hash.get().value);
} else if (sha1Hash.isPresent()) {
download = download.hash(sha1Hash.get().value);
} else {
download = download.fingerprint(curseFile.packageFingerprint);
DialogManager.okDialog().setType(DialogManager.WARNING)
.setTitle(GetText.tr("Mod Not Available"))
.setContent(new HTMLBuilder().center().text(GetText.tr(
"We were unable to download this modpack.<br/>This is likely due to the author of the modpack disabling third party clients from downloading it.<br/><br/>You'll be prompted shortly to download the modpack manually through your browser to your downloads folder.<br/>Once you've downloaded the file that was opened in your browser to your downloads folder, we can continue with installing the modpack.<br/><br/>This process is unfortunate, but we don't have any choice in this matter and has to be done this way."))
.build())
.show();
}
download.downloadFile();
} catch (IOException e) {
LogManager.error("Failed to download modpack file from CurseForge");
return false;
}
String filename = curseFile.fileName.replace(" ", "+");
return loadCurseForgeFormat(tempZip.toFile(), projectId, fileId);
File fileLocation = FileSystem.DOWNLOADS.resolve(filename).toFile();
if (!fileLocation.exists()) {
File downloadsFolderFile = new File(FileSystem.getUserDownloadsPath().toFile(),
filename);
if (downloadsFolderFile.exists()) {
Utils.moveFile(downloadsFolderFile, fileLocation, true);
}
while (!fileLocation.exists()) {
int retValue = 1;
do {
if (retValue == 1) {
OS.openWebBrowser(project.getBrowserDownloadUrl(curseFile));
}
retValue = DialogManager.optionDialog()
.setTitle(GetText.tr("Downloading") + " "
+ filename)
.setContent(new HTMLBuilder().center().text(GetText.tr(
"Browser opened to download file {0}",
filename)
+ "<br/><br/>" + GetText.tr("Please save this file to the following location")
+ "<br/><br/>"
+ (OS.isUsingMacApp()
? FileSystem.getUserDownloadsPath().toFile().getAbsolutePath()
: FileSystem.DOWNLOADS.toAbsolutePath().toString()
+ " or<br/>"
+ FileSystem.getUserDownloadsPath().toFile()))
.build())
.addOption(GetText.tr("Open Folder"), true)
.addOption(GetText.tr("I've Downloaded This File")).setType(DialogManager.INFO)
.showWithFileMonitoring(fileLocation, downloadsFolderFile,
curseFile.fileLength, 1);
if (retValue == DialogManager.CLOSED_OPTION) {
return false;
} else if (retValue == 0) {
OS.openFileExplorer(FileSystem.DOWNLOADS);
}
} while (retValue != 1);
if (!fileLocation.exists()) {
// Check users downloads folder to see if it's there
if (downloadsFolderFile.exists()) {
Utils.moveFile(downloadsFolderFile, fileLocation, true);
}
}
}
}
FileUtils.moveFile(fileLocation.toPath(), tempZip, true);
return loadCurseForgeFormat(tempZip.toFile(), projectId, fileId);
} else {
try {
Download download = new Download().setUrl(curseFile.downloadUrl).downloadTo(tempZip)
.size(curseFile.fileLength);
Optional<CurseForgeFileHash> md5Hash = curseFile.hashes.stream().filter(h -> h.isMd5())
.findFirst();
Optional<CurseForgeFileHash> sha1Hash = curseFile.hashes.stream().filter(h -> h.isSha1())
.findFirst();
if (md5Hash.isPresent()) {
download = download.hash(md5Hash.get().value);
} else if (sha1Hash.isPresent()) {
download = download.hash(sha1Hash.get().value);
} else {
download = download.fingerprint(curseFile.packageFingerprint);
}
download.downloadFile();
} catch (IOException e) {
LogManager.error("Failed to download modpack file from CurseForge");
return false;
}
return loadCurseForgeFormat(tempZip.toFile(), projectId, fileId);
}
}
public static boolean loadFromModrinthUrl(String url) {