Skip to content

Commit 207b765

Browse files
committed
Last minute fixes for 6.0
1 parent 092c27e commit 207b765

5 files changed

Lines changed: 18 additions & 14 deletions

File tree

src/main/java/pojlib/install/Installer.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public static CompletableFuture<String> installLibraries(VersionInfo versionInfo
121121
sha1 = artifact.sha1;
122122
if (!libraryFile.exists()) {
123123
Logger.getInstance().appendToLog("Downloading: " + library.name);
124-
DownloadUtils.downloadFile(artifact.url, libraryFile);
124+
DownloadUtils.downloadFile(artifact.url, libraryFile, artifact.size);
125125
}
126126
}
127127
if (DownloadUtils.compareSHA1(libraryFile, sha1)) {
@@ -151,14 +151,11 @@ public static CompletableFuture<String> installAssets(VersionInfo minecraftVersi
151151
Logger.getInstance().appendToLog("Checking assets");
152152
JsonObject assets = APIHandler.getFullUrl(minecraftVersionInfo.assetIndex.url, JsonObject.class);
153153

154-
int bytes = 0;
155-
156154
for (Map.Entry<String, JsonElement> entry : assets.getAsJsonObject("objects").entrySet()) {
157155
VersionInfo.Asset asset = new Gson().fromJson(entry.getValue(), VersionInfo.Asset.class);
158-
bytes += asset.size;
156+
DownloadManager.addTotalBytes(asset.size);
159157
}
160158

161-
DownloadManager.addTotalBytes(bytes);
162159
ThreadPoolExecutor tp = new ThreadPoolExecutor(8, 8, 100, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
163160

164161
for (Map.Entry<String, JsonElement> entry : assets.getAsJsonObject("objects").entrySet()) {
@@ -226,7 +223,7 @@ public void run() {
226223
if (!assetFile.exists()) {
227224
Logger.getInstance().appendToLog("Downloading: " + fileName);
228225
try {
229-
DownloadUtils.downloadFile(Constants.MOJANG_RESOURCES_URL + "/" + path, assetFile);
226+
DownloadUtils.downloadFile(Constants.MOJANG_RESOURCES_URL + "/" + path, assetFile, 0);
230227
} catch (IOException e) {
231228
throw new RuntimeException(e);
232229
}

src/main/java/pojlib/util/download/DownloadManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static boolean downloadsCompleted() {
2222
}
2323

2424
public static float getPercentComplete() {
25-
if(totalBytes == 0) {
25+
if(totalBytes == 0 || downloadedBytes > totalBytes) {
2626
return 100.0f;
2727
}
2828

src/main/java/pojlib/util/download/DownloadUtils.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import javax.annotation.Nullable;
1717

1818
public class DownloadUtils {
19-
private static void download(URL url, OutputStream os, String fileName) throws IOException {
19+
private static void download(URL url, OutputStream os, long size) throws IOException {
2020
final int MAX_RETRIES = 3;
2121
int attempts = 0;
2222

@@ -29,8 +29,11 @@ private static void download(URL url, OutputStream os, String fileName) throws I
2929
conn.connect();
3030

3131
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
32-
int totalBytes = conn.getContentLength(); // Get the total size of the file
33-
try (InputStream is = new StreamDL(conn.getInputStream(), totalBytes)) {
32+
if(size == -1) {
33+
size = conn.getContentLengthLong();
34+
}
35+
36+
try (InputStream is = new StreamDL(conn.getInputStream(), size)) {
3437
IOUtils.copy(is, os);
3538
}
3639
return;
@@ -44,11 +47,15 @@ private static void download(URL url, OutputStream os, String fileName) throws I
4447
}
4548

4649
public static void downloadFile(String url, File out) throws IOException {
50+
downloadFile(url, out, -1);
51+
}
52+
53+
public static void downloadFile(String url, File out, long size) throws IOException {
4754
Objects.requireNonNull(out.getParentFile()).mkdirs();
4855
File tempOut = File.createTempFile(out.getName(), ".part", out.getParentFile());
4956
try {
5057
try (OutputStream bos2 = new BufferedOutputStream(Files.newOutputStream(tempOut.toPath()))) {
51-
download(new URL(url), bos2, out.getName());
58+
download(new URL(url), bos2, size);
5259
tempOut.renameTo(out);
5360
bos2.close();
5461
if (tempOut.exists()) tempOut.delete();

src/main/java/pojlib/util/download/StreamDL.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class StreamDL extends InputStream {
1010
private int count;
1111
private final Collection<StreamListener> listeners = new ArrayList<>();
1212

13-
public StreamDL(InputStream in, int totalBytes) {
13+
public StreamDL(InputStream in, long totalBytes) {
1414
this.in = in;
1515
DownloadManager.addTotalBytes(totalBytes);
1616
}

src/main/java/pojlib/util/json/MinecraftInstances.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private void updateModByType(List<ProjectInfo> newMods) throws IOException {
216216
(legacyMod ? extMod.slug : extMod.fileName) + (extMod.type.equals("resourcepack") ? ".zip" : ".jar")
217217
);
218218
if(!mod.exists()) {
219-
DownloadUtils.downloadFile(extMod.download_link, mod);
219+
DownloadUtils.downloadFile(extMod.download_link, mod, 0);
220220
}
221221
}
222222
newExtMods.add(extMod);
@@ -232,7 +232,7 @@ private void downloadAllMods(List<ProjectInfo> newMods) throws IOException {
232232
gameDir + (newMod.type.equals("mod") ? "/mods" : "/resourcepacks"),
233233
(legacyMod ? newMod.slug : newMod.fileName) + (newMod.type.equals("resourcepack") ? ".zip" : ".jar")
234234
);
235-
DownloadUtils.downloadFile(newMod.download_link, mod);
235+
DownloadUtils.downloadFile(newMod.download_link, mod, 0);
236236
}
237237

238238
extProjects = newMods.toArray(new ProjectInfo[0]);

0 commit comments

Comments
 (0)