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

Add download speed #5685

Open
wants to merge 3 commits into
base: v3_openjdk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1173,4 +1173,11 @@ public static void releaseRenderersCache() {
sCompatibleRenderers = null;
System.gc();
}

public static String trimSecondsToMiniutes(double seconds) {
artdeell marked this conversation as resolved.
Show resolved Hide resolved
if (seconds >= 60) {
return (seconds / 60.0) + "min" + (seconds % 60.0) + "s";
artdeell marked this conversation as resolved.
Show resolved Hide resolved
}
return seconds + "s";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
import java.util.concurrent.atomic.AtomicReference;

public class MinecraftDownloader {
private static final double BYTES_TO_MB = 1024d * 1024d;
public static final String MINECRAFT_RES = "https://resources.download.minecraft.net/";
private AtomicReference<Exception> mDownloaderThreadException;
private ArrayList<DownloaderTask> mScheduledDownloadTasks;
private AtomicLong mDownloadFileCounter;
private AtomicLong mDownloadSizeCounter;
private long mDownloadFileCount;
private long mTotalDownloadFileSize;
private File mSourceJarFile; // The source client JAR picked during the inheritance process
private File mTargetJarFile; // The destination client JAR to which the source will be copied to.

Expand Down Expand Up @@ -83,7 +83,6 @@ private void downloadGame(Activity activity, JMinecraftVersionList.Version verIn

mTargetJarFile = createGameJarPath(versionName);
mScheduledDownloadTasks = new ArrayList<>();
mDownloadFileCounter = new AtomicLong(0);
mDownloadSizeCounter = new AtomicLong(0);
mDownloaderThreadException = new AtomicReference<>(null);

Expand All @@ -102,13 +101,25 @@ private void downloadGame(Activity activity, JMinecraftVersionList.Version verIn
downloaderPool.shutdown();

try {
long startTime = System.currentTimeMillis();
while (mDownloaderThreadException.get() == null &&
!downloaderPool.awaitTermination(33, TimeUnit.MILLISECONDS)) {
long dlFileCounter = mDownloadFileCounter.get();
int progress = (int)((dlFileCounter * 100L) / mDownloadFileCount);
long elapsedTime = System.currentTimeMillis() - startTime;
long downloadedSize = mDownloadSizeCounter.get();

double downloadSpeed = 0.0; // bytes per second
double remainTime = 0.0; // seconds
if (elapsedTime > 0) { // Prevent divide by zero
downloadSpeed = downloadedSize / (elapsedTime / 1000.0);
remainTime = (mTotalDownloadFileSize - downloadedSize) / (long) downloadSpeed;
}
// Some files are relatively large, but others are relatively small.
// Using the number of files as a basis for progress bars is inaccurate
f401 marked this conversation as resolved.
Show resolved Hide resolved
int progress = (int)((downloadedSize * 100L) / mTotalDownloadFileSize);
ProgressLayout.setProgress(ProgressLayout.DOWNLOAD_MINECRAFT, progress,
R.string.newdl_downloading_game_files, dlFileCounter,
mDownloadFileCount, (double)mDownloadSizeCounter.get() / (1024d * 1024d));
R.string.newdl_downloading_game_files,
downloadSpeed / BYTES_TO_MB, Tools.trimSecondsToMiniutes(remainTime),
(double) downloadedSize / BYTES_TO_MB, mTotalDownloadFileSize / BYTES_TO_MB);
}
Exception thrownException = mDownloaderThreadException.get();
if(thrownException != null) {
Expand Down Expand Up @@ -233,7 +244,6 @@ private void growDownloadList(int addedElementCount) {
private void scheduleDownload(File targetFile, int downloadClass, String url, String sha1,
long size, boolean skipIfFailed) throws IOException {
FileUtils.ensureParentDirectory(targetFile);
mDownloadFileCount++;
mScheduledDownloadTasks.add(
new DownloaderTask(targetFile, downloadClass, url, sha1, size, skipIfFailed)
);
Expand Down Expand Up @@ -359,6 +369,8 @@ private final class DownloaderTask implements Runnable, Tools.DownloaderFeedback
this.mDownloadClass = downloadClass;
this.mDownloadSize = downloadSize;
this.mSkipIfFailed = skipIfFailed;

mTotalDownloadFileSize += downloadSize;
}

@Override
Expand Down Expand Up @@ -401,11 +413,9 @@ private void downloadFile() throws Exception {
}catch (Exception e) {
if(!mSkipIfFailed) throw e;
}
mDownloadFileCounter.incrementAndGet();
}

private void finishWithoutDownloading() {
mDownloadFileCounter.incrementAndGet();
mDownloadSizeCounter.addAndGet(mDownloadSize);
}

Expand Down
2 changes: 1 addition & 1 deletion app_pojavlauncher/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@
<string name="exception_failed_to_unpack_jre17">Failed to install JRE 17</string>
<string name="newdl_starting">Reading game metadata…</string>
<string name="newdl_downloading_metadata">Downloading game metadata (%s)</string>
<string name="newdl_downloading_game_files">Downloading game files… (%d/%d, %.2f MB)</string>
<string name="newdl_downloading_game_files">Downloading at %.2f MB/s ETA: %s(%.2f MB/%.2f MB) </string>
artdeell marked this conversation as resolved.
Show resolved Hide resolved
<string name="cropper_title">Select image region</string>
<string name="cropper_lock_vertical">V. lock</string>
<string name="cropper_lock_horizontal">H. lock</string>
Expand Down