- Moved link obtainment into a separate interface
- Adjusted ModDownloader to obtain links while downloading
- Moved getFileName from Tools to FileUtils
- Fixed wrong Forge version number
This commit is contained in:
BuildTools 2023-08-13 11:31:47 +03:00 committed by ArtDev
parent 19a781ad18
commit 08f16f50cc
6 changed files with 72 additions and 35 deletions

View File

@ -987,12 +987,6 @@ public final class Tools {
return prefixedName.substring(Tools.LAUNCHERPROFILES_RTPREFIX.length()); return prefixedName.substring(Tools.LAUNCHERPROFILES_RTPREFIX.length());
} }
public static String getFileName(String pathOrUrl) {
int lastSlashIndex = pathOrUrl.lastIndexOf('/');
if(lastSlashIndex == -1) return null;
return pathOrUrl.substring(lastSlashIndex);
}
public static String getSelectedRuntime(MinecraftProfile minecraftProfile) { public static String getSelectedRuntime(MinecraftProfile minecraftProfile) {
String runtime = LauncherPreferences.PREF_DEFAULT_RUNTIME; String runtime = LauncherPreferences.PREF_DEFAULT_RUNTIME;
String profileRuntime = getRuntimeName(minecraftProfile.javaDir); String profileRuntime = getRuntimeName(minecraftProfile.javaDir);

View File

@ -15,6 +15,7 @@ import net.kdt.pojavlaunch.modloaders.modpacks.models.ModDetail;
import net.kdt.pojavlaunch.modloaders.modpacks.models.ModItem; import net.kdt.pojavlaunch.modloaders.modpacks.models.ModItem;
import net.kdt.pojavlaunch.modloaders.modpacks.models.SearchFilters; import net.kdt.pojavlaunch.modloaders.modpacks.models.SearchFilters;
import net.kdt.pojavlaunch.progresskeeper.ProgressKeeper; import net.kdt.pojavlaunch.progresskeeper.ProgressKeeper;
import net.kdt.pojavlaunch.utils.FileUtils;
import net.kdt.pojavlaunch.utils.ZipUtils; import net.kdt.pojavlaunch.utils.ZipUtils;
import java.io.File; import java.io.File;
@ -140,16 +141,18 @@ public class CurseforgeApi implements ModpackApi{
ModDownloader modDownloader = new ModDownloader(new File(instanceDestination,"mods"), true); ModDownloader modDownloader = new ModDownloader(new File(instanceDestination,"mods"), true);
int fileCount = curseManifest.files.length; int fileCount = curseManifest.files.length;
for(int i = 0; i < fileCount; i++) { for(int i = 0; i < fileCount; i++) {
CurseManifest.CurseFile curseFile = curseManifest.files[i]; final CurseManifest.CurseFile curseFile = curseManifest.files[i];
String downloadUrl = getDownloadUrl(curseFile.projectID, curseFile.fileID); modDownloader.submitDownload(()->{
if(downloadUrl == null && curseFile.required) throw new IOException("Failed to obtain download URL for "+curseFile.projectID+" "+curseFile.fileID); String url = getDownloadUrl(curseFile.projectID, curseFile.fileID);
else if(downloadUrl == null) continue; if(url == null && curseFile.required)
modDownloader.submitDownload(Tools.getFileName(downloadUrl), downloadUrl); throw new IOException("Failed to obtain download URL for "+curseFile.projectID+" "+curseFile.fileID);
ProgressKeeper.submitProgress(ProgressLayout.INSTALL_MODPACK, (int) Math.max((float)i/fileCount*100,0), R.string.modpack_download_getting_links, i, fileCount); else if(url == null) return null;
} return new ModDownloader.FileInfo(url, FileUtils.getFileName(url));
modDownloader.awaitFinish((c,m)->{ // insert joke about semen
ProgressKeeper.submitProgress(ProgressLayout.INSTALL_MODPACK, (int) Math.max((float)c/m*100,0), R.string.modpack_download_downloading_mods_fc, c, m);
}); });
}
modDownloader.awaitFinish((c,m)->
ProgressKeeper.submitProgress(ProgressLayout.INSTALL_MODPACK, (int) Math.max((float)c/m*100,0), R.string.modpack_download_downloading_mods_fc, c, m)
);
String overridesDir = "overrides"; String overridesDir = "overrides";
if(curseManifest.overrides != null) overridesDir = curseManifest.overrides; if(curseManifest.overrides != null) overridesDir = curseManifest.overrides;
ZipUtils.zipExtract(modpackZipFile, overridesDir, instanceDestination); ZipUtils.zipExtract(modpackZipFile, overridesDir, instanceDestination);
@ -170,7 +173,8 @@ public class CurseforgeApi implements ModpackApi{
int dashIndex = modLoaderId.indexOf('-'); int dashIndex = modLoaderId.indexOf('-');
String modLoaderName = modLoaderId.substring(0, dashIndex); String modLoaderName = modLoaderId.substring(0, dashIndex);
String modLoaderVersion = modLoaderId.substring(dashIndex+1); String modLoaderVersion = modLoaderId.substring(dashIndex+1);
int modLoaderTypeInt = -1; Log.i("CurseforgeApi", modLoaderId + " " + modLoaderName + " "+modLoaderVersion);
int modLoaderTypeInt;
switch (modLoaderName) { switch (modLoaderName) {
case "forge": case "forge":
modLoaderTypeInt = ModLoaderInfo.MOD_LOADER_FORGE; modLoaderTypeInt = ModLoaderInfo.MOD_LOADER_FORGE;
@ -178,9 +182,10 @@ public class CurseforgeApi implements ModpackApi{
case "fabric": case "fabric":
modLoaderTypeInt = ModLoaderInfo.MOD_LOADER_FABRIC; modLoaderTypeInt = ModLoaderInfo.MOD_LOADER_FABRIC;
break; break;
default:
return null;
//TODO: Quilt is also Forge? How does that work? //TODO: Quilt is also Forge? How does that work?
} }
if(modLoaderTypeInt == -1) return null;
return new ModLoaderInfo(modLoaderTypeInt, modLoaderVersion, minecraft.version); return new ModLoaderInfo(modLoaderTypeInt, modLoaderVersion, minecraft.version);
} }

View File

@ -29,6 +29,7 @@ public class ModDownloader {
} }
public ModDownloader(File destinationDirectory, boolean useFileCount) { public ModDownloader(File destinationDirectory, boolean useFileCount) {
this.mDownloadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
this.mDestinationDirectory = destinationDirectory; this.mDestinationDirectory = destinationDirectory;
this.mUseFileCount = useFileCount; this.mUseFileCount = useFileCount;
} }
@ -39,10 +40,10 @@ public class ModDownloader {
mDownloadPool.execute(new DownloadTask(url, new File(mDestinationDirectory, relativePath))); mDownloadPool.execute(new DownloadTask(url, new File(mDestinationDirectory, relativePath)));
} }
public void submitDownload(String relativePath, String... url) { public void submitDownload(FileInfoProvider infoProvider) {
if(!mUseFileCount) throw new RuntimeException("This method can only be used in a file-counting ModDownloader"); if(!mUseFileCount) throw new RuntimeException("This method can only be used in a file-counting ModDownloader");
mTotalSize += 1; mTotalSize += 1;
mDownloadPool.execute(new DownloadTask(url, new File(mDestinationDirectory, relativePath))); mDownloadPool.execute(new FileInfoQueryTask(infoProvider));
} }
public void awaitFinish(Tools.DownloaderFeedback feedback) throws IOException { public void awaitFinish(Tools.DownloaderFeedback feedback) throws IOException {
@ -51,8 +52,8 @@ public class ModDownloader {
while(!mDownloadPool.awaitTermination(20, TimeUnit.MILLISECONDS) && !mTerminator.get()) { while(!mDownloadPool.awaitTermination(20, TimeUnit.MILLISECONDS) && !mTerminator.get()) {
feedback.updateProgress((int) mDownloadSize.get(), (int) mTotalSize); feedback.updateProgress((int) mDownloadSize.get(), (int) mTotalSize);
} }
if(mTerminator.get()) { if(mTerminator.get()) {
mDownloadPool.shutdownNow();
synchronized (mExceptionSyncPoint) { synchronized (mExceptionSyncPoint) {
if(mFirstIOException == null) mExceptionSyncPoint.wait(); if(mFirstIOException == null) mExceptionSyncPoint.wait();
throw mFirstIOException; throw mFirstIOException;
@ -71,6 +72,34 @@ public class ModDownloader {
return buffer; return buffer;
} }
private void downloadFailed(IOException exception) {
mTerminator.set(true);
synchronized (mExceptionSyncPoint) {
if(mFirstIOException == null) {
mFirstIOException = exception;
mExceptionSyncPoint.notify();
}
}
}
class FileInfoQueryTask implements Runnable {
private final FileInfoProvider mFileInfoProvider;
public FileInfoQueryTask(FileInfoProvider fileInfoProvider) {
this.mFileInfoProvider = fileInfoProvider;
}
@Override
public void run() {
try {
FileInfo fileInfo = mFileInfoProvider.getFileInfo();
if(fileInfo == null) return;
new DownloadTask(new String[]{fileInfo.url},
new File(mDestinationDirectory, fileInfo.relativePath)).run();
}catch (IOException e) {
downloadFailed(e);
}
}
}
class DownloadTask implements Runnable, Tools.DownloaderFeedback { class DownloadTask implements Runnable, Tools.DownloaderFeedback {
private final String[] mDownloadUrls; private final String[] mDownloadUrls;
private final File mDestination; private final File mDestination;
@ -94,12 +123,7 @@ public class ModDownloader {
} }
} }
if(exception != null) { if(exception != null) {
synchronized (mExceptionSyncPoint) { downloadFailed(exception);
if(mFirstIOException == null) {
mFirstIOException = exception;
mExceptionSyncPoint.notify();
}
}
} }
} }
@ -131,4 +155,18 @@ public class ModDownloader {
last = curr; last = curr;
} }
} }
public static class FileInfo {
public final String url;
public final String relativePath;
public FileInfo(String url, String relativePath) {
this.url = url;
this.relativePath = relativePath;
}
}
public interface FileInfoProvider {
FileInfo getFileInfo() throws IOException;
}
} }

View File

@ -1,10 +1,5 @@
package net.kdt.pojavlaunch.modloaders.modpacks.api; package net.kdt.pojavlaunch.modloaders.modpacks.api;
import android.content.Context;
import android.content.Intent;
import net.kdt.pojavlaunch.JavaGUILauncherActivity;
public class ModLoaderInfo { public class ModLoaderInfo {
public static final int MOD_LOADER_FORGE = 0; public static final int MOD_LOADER_FORGE = 0;
public static final int MOD_LOADER_FABRIC = 1; public static final int MOD_LOADER_FABRIC = 1;
@ -22,11 +17,11 @@ public class ModLoaderInfo {
public String getVersionId() { public String getVersionId() {
switch (modLoaderType) { switch (modLoaderType) {
case MOD_LOADER_FORGE: case MOD_LOADER_FORGE:
return minecraftVersion+"-forge-"+modLoaderType; return minecraftVersion+"-forge-"+modLoaderVersion;
case MOD_LOADER_FABRIC: case MOD_LOADER_FABRIC:
return "fabric-loader-"+modLoaderVersion+"-"+minecraftVersion; return "fabric-loader-"+modLoaderVersion+"-"+minecraftVersion;
case MOD_LOADER_QUILT: case MOD_LOADER_QUILT:
throw new RuntimeException("Quilt is gay af"); throw new RuntimeException("Quilt is not supported");
default: default:
return null; return null;
} }

View File

@ -41,6 +41,12 @@ public class FileUtils {
zis.close(); zis.close();
} }
public static String getFileName(String pathOrUrl) {
int lastSlashIndex = pathOrUrl.lastIndexOf('/');
if(lastSlashIndex == -1) return null;
return pathOrUrl.substring(lastSlashIndex);
}
public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException { public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
File destFile = new File(destinationDir, zipEntry.getName()); File destFile = new File(destinationDir, zipEntry.getName());

View File

@ -416,7 +416,6 @@
<string name="modpack_download_downloading_metadata">Downloading modpack metadata (%.2f MB / %.2f MB)</string> <string name="modpack_download_downloading_metadata">Downloading modpack metadata (%.2f MB / %.2f MB)</string>
<string name="modpack_download_downloading_mods">Downloading mods (%.2f MB / %.2f MB)</string> <string name="modpack_download_downloading_mods">Downloading mods (%.2f MB / %.2f MB)</string>
<string name="modpack_download_getting_links">Downloading mod links (%d/%d)</string>
<string name="modpack_download_downloading_mods_fc">Downloading mods (File %d out of %d)</string> <string name="modpack_download_downloading_mods_fc">Downloading mods (File %d out of %d)</string>
<string name="modpack_download_applying_overrides">Applying overrides (%d/%d)</string> <string name="modpack_download_applying_overrides">Applying overrides (%d/%d)</string>
</resources> </resources>