Skip to content

Commit

Permalink
Fix[mcdl]: Misc fixes
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
artdeell committed Dec 19, 2023
1 parent 0e34866 commit c4764aa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import net.kdt.pojavlaunch.tasks.AsyncMinecraftDownloader;
import net.kdt.pojavlaunch.tasks.AsyncVersionList;
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.value.launcherprofiles.LauncherProfiles;
import net.kdt.pojavlaunch.value.launcherprofiles.MinecraftProfile;
Expand Down Expand Up @@ -133,7 +133,7 @@ public void onFragmentResumed(@NonNull FragmentManager fm, @NonNull Fragment f)
}
String normalizedVersionId = AsyncMinecraftDownloader.normalizeVersionId(prof.lastVersionId);
JMinecraftVersionList.Version mcVersion = AsyncMinecraftDownloader.getListedVersion(normalizedVersionId);
new NewMinecraftDownloader().start(
new MinecraftDownloader().start(
this,
mcVersion,
normalizedVersionId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import net.kdt.pojavlaunch.Tools;
import net.kdt.pojavlaunch.progresskeeper.ProgressKeeper;
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 java.io.File;
Expand Down Expand Up @@ -89,7 +89,7 @@ public boolean downloadMinecraft(String minecraftVersion) {
if(minecraftJsonVersion == null) return false;
try {
synchronized (mMinecraftDownloadLock) {
new NewMinecraftDownloader().start(null, minecraftJsonVersion, minecraftVersion, this);
new MinecraftDownloader().start(null, minecraftJsonVersion, minecraftVersion, this);
mMinecraftDownloadLock.wait();
}
}catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

public class NewMinecraftDownloader {
public class MinecraftDownloader {
public static final String MINECRAFT_RES = "https://resources.download.minecraft.net/";
private AtomicReference<Exception> mDownloaderThreadException;
private ArrayList<DownloaderTask> mScheduledDownloadTasks;
Expand Down Expand Up @@ -231,14 +231,15 @@ private void growDownloadList(int addedElementCount) {
}

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++;
mScheduledDownloadTasks.add(
new DownloaderTask(targetFile, downloadClass, url, sha1, size, skipIfFailed)
);
}

private void scheduleLibraryDownloads(DependentLibrary[] dependentLibraries) {
private void scheduleLibraryDownloads(DependentLibrary[] dependentLibraries) throws IOException {
Tools.preProcessLibraries(dependentLibraries);
growDownloadList(dependentLibraries.length);
for(DependentLibrary dependentLibrary : dependentLibraries) {
Expand Down Expand Up @@ -276,7 +277,7 @@ private void scheduleLibraryDownloads(DependentLibrary[] dependentLibraries) {
}
}

private void scheduleAssetDownloads(JAssets assets) {
private void scheduleAssetDownloads(JAssets assets) throws IOException {
Map<String, JAssetInfo> assetObjects = assets.objects;
if(assetObjects == null) return;
Set<String> assetNames = assetObjects.keySet();
Expand All @@ -286,10 +287,11 @@ private void scheduleAssetDownloads(JAssets assets) {
if(assetInfo == null) continue;
File targetFile;
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) {
targetFile = new File(Tools.OBSOLETE_RESOURCES_PATH, asset);
targetFile = new File(basePath, asset);
} 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;
scheduleDownload(targetFile,
Expand All @@ -301,7 +303,7 @@ private void scheduleAssetDownloads(JAssets assets) {
}
}

private void scheduleLoggingAssetDownloadIfNeeded(JMinecraftVersionList.LoggingConfig loggingConfig) {
private void scheduleLoggingAssetDownloadIfNeeded(JMinecraftVersionList.LoggingConfig loggingConfig) throws IOException {
if(loggingConfig.client == null || loggingConfig.client.file == null) return;
JMinecraftVersionList.FileProperties loggingFileProperties = loggingConfig.client.file;
File internalLoggingConfig = new File(Tools.DIR_DATA + File.separator + "security",
Expand All @@ -316,7 +318,7 @@ private void scheduleLoggingAssetDownloadIfNeeded(JMinecraftVersionList.LoggingC
false);
}

private void scheduleGameJarDownload(MinecraftClientInfo minecraftClientInfo, String versionName) {
private void scheduleGameJarDownload(MinecraftClientInfo minecraftClientInfo, String versionName) throws IOException {
File clientJar = createGameJarPath(versionName);
String clientSha1 = LauncherPreferences.PREF_CHECK_LIBRARY_SHA ?
minecraftClientInfo.sha1 : null;
Expand All @@ -335,12 +337,12 @@ private void scheduleGameJarDownload(MinecraftClientInfo minecraftClientInfo, St
private static byte[] getLocalBuffer() {
byte[] tlb = sThreadLocalDownloadBuffer.get();
if(tlb != null) return tlb;
tlb = new byte[65535];
tlb = new byte[32768];
sThreadLocalDownloadBuffer.set(tlb);
return tlb;
}

class DownloaderTask implements Runnable, Tools.DownloaderFeedback {
private final class DownloaderTask implements Runnable, Tools.DownloaderFeedback {
private final File mTargetPath;
private final String mTargetUrl;
private String mTargetSha1;
Expand Down Expand Up @@ -369,7 +371,6 @@ public void run() {
}

private void runCatching() throws Exception {
FileUtils.ensureParentDirectory(mTargetPath);
if(Tools.isValidString(mTargetSha1)) {
verifyFileSha1();
}else {
Expand Down

0 comments on commit c4764aa

Please sign in to comment.