Fix[mcdl]: Misc fixes

- Renamed class to MinecraftDownloader
- Shrunk download buffer to 32k
- Made DownloaderTask private and final
- Moved directory ensurement to the scheduling step, to avoid race conditions
- Fixed broken asset downloads for 1.6.x
This commit is contained in:
artdeell 2023-12-17 22:57:07 +03:00 committed by Maksim Belov
parent 0e34866345
commit c4764aa9f1
3 changed files with 16 additions and 15 deletions

View File

@ -41,7 +41,7 @@ import net.kdt.pojavlaunch.services.ProgressServiceKeeper;
import net.kdt.pojavlaunch.tasks.AsyncMinecraftDownloader; import net.kdt.pojavlaunch.tasks.AsyncMinecraftDownloader;
import net.kdt.pojavlaunch.tasks.AsyncVersionList; import net.kdt.pojavlaunch.tasks.AsyncVersionList;
import net.kdt.pojavlaunch.lifecycle.ContextAwareDoneListener; import net.kdt.pojavlaunch.lifecycle.ContextAwareDoneListener;
import net.kdt.pojavlaunch.tasks.NewMinecraftDownloader; import net.kdt.pojavlaunch.tasks.MinecraftDownloader;
import net.kdt.pojavlaunch.utils.NotificationUtils; import net.kdt.pojavlaunch.utils.NotificationUtils;
import net.kdt.pojavlaunch.value.launcherprofiles.LauncherProfiles; import net.kdt.pojavlaunch.value.launcherprofiles.LauncherProfiles;
import net.kdt.pojavlaunch.value.launcherprofiles.MinecraftProfile; import net.kdt.pojavlaunch.value.launcherprofiles.MinecraftProfile;
@ -133,7 +133,7 @@ public class LauncherActivity extends BaseActivity {
} }
String normalizedVersionId = AsyncMinecraftDownloader.normalizeVersionId(prof.lastVersionId); String normalizedVersionId = AsyncMinecraftDownloader.normalizeVersionId(prof.lastVersionId);
JMinecraftVersionList.Version mcVersion = AsyncMinecraftDownloader.getListedVersion(normalizedVersionId); JMinecraftVersionList.Version mcVersion = AsyncMinecraftDownloader.getListedVersion(normalizedVersionId);
new NewMinecraftDownloader().start( new MinecraftDownloader().start(
this, this,
mcVersion, mcVersion,
normalizedVersionId, normalizedVersionId,

View File

@ -7,7 +7,7 @@ import net.kdt.pojavlaunch.R;
import net.kdt.pojavlaunch.Tools; import net.kdt.pojavlaunch.Tools;
import net.kdt.pojavlaunch.progresskeeper.ProgressKeeper; import net.kdt.pojavlaunch.progresskeeper.ProgressKeeper;
import net.kdt.pojavlaunch.tasks.AsyncMinecraftDownloader; import net.kdt.pojavlaunch.tasks.AsyncMinecraftDownloader;
import net.kdt.pojavlaunch.tasks.NewMinecraftDownloader; import net.kdt.pojavlaunch.tasks.MinecraftDownloader;
import net.kdt.pojavlaunch.utils.DownloadUtils; import net.kdt.pojavlaunch.utils.DownloadUtils;
import java.io.File; import java.io.File;
@ -89,7 +89,7 @@ public class OptiFineDownloadTask implements Runnable, Tools.DownloaderFeedback,
if(minecraftJsonVersion == null) return false; if(minecraftJsonVersion == null) return false;
try { try {
synchronized (mMinecraftDownloadLock) { synchronized (mMinecraftDownloadLock) {
new NewMinecraftDownloader().start(null, minecraftJsonVersion, minecraftVersion, this); new MinecraftDownloader().start(null, minecraftJsonVersion, minecraftVersion, this);
mMinecraftDownloadLock.wait(); mMinecraftDownloadLock.wait();
} }
}catch (InterruptedException e) { }catch (InterruptedException e) {

View File

@ -36,7 +36,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
public class NewMinecraftDownloader { public class MinecraftDownloader {
public static final String MINECRAFT_RES = "https://resources.download.minecraft.net/"; public static final String MINECRAFT_RES = "https://resources.download.minecraft.net/";
private AtomicReference<Exception> mDownloaderThreadException; private AtomicReference<Exception> mDownloaderThreadException;
private ArrayList<DownloaderTask> mScheduledDownloadTasks; private ArrayList<DownloaderTask> mScheduledDownloadTasks;
@ -231,14 +231,15 @@ public class NewMinecraftDownloader {
} }
private void scheduleDownload(File targetFile, int downloadClass, String url, String sha1, private void scheduleDownload(File targetFile, int downloadClass, String url, String sha1,
long size, boolean skipIfFailed) { long size, boolean skipIfFailed) throws IOException {
FileUtils.ensureParentDirectory(targetFile);
mDownloadFileCount++; mDownloadFileCount++;
mScheduledDownloadTasks.add( mScheduledDownloadTasks.add(
new DownloaderTask(targetFile, downloadClass, url, sha1, size, skipIfFailed) new DownloaderTask(targetFile, downloadClass, url, sha1, size, skipIfFailed)
); );
} }
private void scheduleLibraryDownloads(DependentLibrary[] dependentLibraries) { private void scheduleLibraryDownloads(DependentLibrary[] dependentLibraries) throws IOException {
Tools.preProcessLibraries(dependentLibraries); Tools.preProcessLibraries(dependentLibraries);
growDownloadList(dependentLibraries.length); growDownloadList(dependentLibraries.length);
for(DependentLibrary dependentLibrary : dependentLibraries) { for(DependentLibrary dependentLibrary : dependentLibraries) {
@ -276,7 +277,7 @@ public class NewMinecraftDownloader {
} }
} }
private void scheduleAssetDownloads(JAssets assets) { private void scheduleAssetDownloads(JAssets assets) throws IOException {
Map<String, JAssetInfo> assetObjects = assets.objects; Map<String, JAssetInfo> assetObjects = assets.objects;
if(assetObjects == null) return; if(assetObjects == null) return;
Set<String> assetNames = assetObjects.keySet(); Set<String> assetNames = assetObjects.keySet();
@ -286,10 +287,11 @@ public class NewMinecraftDownloader {
if(assetInfo == null) continue; if(assetInfo == null) continue;
File targetFile; File targetFile;
String hashedPath = assetInfo.hash.substring(0, 2) + File.separator + assetInfo.hash; String hashedPath = assetInfo.hash.substring(0, 2) + File.separator + assetInfo.hash;
String basePath = assets.mapToResources ? Tools.OBSOLETE_RESOURCES_PATH : Tools.ASSETS_PATH;
if(assets.virtual || assets.mapToResources) { if(assets.virtual || assets.mapToResources) {
targetFile = new File(Tools.OBSOLETE_RESOURCES_PATH, asset); targetFile = new File(basePath, asset);
} else { } else {
targetFile = new File(Tools.ASSETS_PATH, "objects" + File.separator + hashedPath); targetFile = new File(basePath, "objects" + File.separator + hashedPath);
} }
String sha1 = LauncherPreferences.PREF_CHECK_LIBRARY_SHA ? assetInfo.hash : null; String sha1 = LauncherPreferences.PREF_CHECK_LIBRARY_SHA ? assetInfo.hash : null;
scheduleDownload(targetFile, scheduleDownload(targetFile,
@ -301,7 +303,7 @@ public class NewMinecraftDownloader {
} }
} }
private void scheduleLoggingAssetDownloadIfNeeded(JMinecraftVersionList.LoggingConfig loggingConfig) { private void scheduleLoggingAssetDownloadIfNeeded(JMinecraftVersionList.LoggingConfig loggingConfig) throws IOException {
if(loggingConfig.client == null || loggingConfig.client.file == null) return; if(loggingConfig.client == null || loggingConfig.client.file == null) return;
JMinecraftVersionList.FileProperties loggingFileProperties = loggingConfig.client.file; JMinecraftVersionList.FileProperties loggingFileProperties = loggingConfig.client.file;
File internalLoggingConfig = new File(Tools.DIR_DATA + File.separator + "security", File internalLoggingConfig = new File(Tools.DIR_DATA + File.separator + "security",
@ -316,7 +318,7 @@ public class NewMinecraftDownloader {
false); false);
} }
private void scheduleGameJarDownload(MinecraftClientInfo minecraftClientInfo, String versionName) { private void scheduleGameJarDownload(MinecraftClientInfo minecraftClientInfo, String versionName) throws IOException {
File clientJar = createGameJarPath(versionName); File clientJar = createGameJarPath(versionName);
String clientSha1 = LauncherPreferences.PREF_CHECK_LIBRARY_SHA ? String clientSha1 = LauncherPreferences.PREF_CHECK_LIBRARY_SHA ?
minecraftClientInfo.sha1 : null; minecraftClientInfo.sha1 : null;
@ -335,12 +337,12 @@ public class NewMinecraftDownloader {
private static byte[] getLocalBuffer() { private static byte[] getLocalBuffer() {
byte[] tlb = sThreadLocalDownloadBuffer.get(); byte[] tlb = sThreadLocalDownloadBuffer.get();
if(tlb != null) return tlb; if(tlb != null) return tlb;
tlb = new byte[65535]; tlb = new byte[32768];
sThreadLocalDownloadBuffer.set(tlb); sThreadLocalDownloadBuffer.set(tlb);
return tlb; return tlb;
} }
class DownloaderTask implements Runnable, Tools.DownloaderFeedback { private final class DownloaderTask implements Runnable, Tools.DownloaderFeedback {
private final File mTargetPath; private final File mTargetPath;
private final String mTargetUrl; private final String mTargetUrl;
private String mTargetSha1; private String mTargetSha1;
@ -369,7 +371,6 @@ public class NewMinecraftDownloader {
} }
private void runCatching() throws Exception { private void runCatching() throws Exception {
FileUtils.ensureParentDirectory(mTargetPath);
if(Tools.isValidString(mTargetSha1)) { if(Tools.isValidString(mTargetSha1)) {
verifyFileSha1(); verifyFileSha1();
}else { }else {