Skip to content

Commit

Permalink
Improve RP Replacing Behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenny-Hui committed Sep 2, 2024
1 parent 32473eb commit 1122089
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 37 deletions.
61 changes: 47 additions & 14 deletions src/main/java/com/lx862/rphelper/data/manager/PackManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
import net.minecraft.resource.ResourcePackProfile;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;

Expand Down Expand Up @@ -60,11 +64,12 @@ public static void downloadOrUpdate(boolean init) {

public static void downloadPack(PackEntry packEntry, File outputLocation) {
ToastManager.setupNewDownloadToast(packEntry);
File tmpOutputLocation = outputLocation.getParentFile().toPath().resolve(UUID.randomUUID().toString()).toFile();

try {
final long[] lastMs = {System.currentTimeMillis()};

DownloadManager.download(packEntry.sourceUrl, outputLocation, (prg) -> {
DownloadManager.download(packEntry.sourceUrl, tmpOutputLocation, (prg) -> {
// Print every 500ms
if (System.currentTimeMillis() - lastMs[0] > 500) {
lastMs[0] = System.currentTimeMillis();
Expand All @@ -73,26 +78,54 @@ public static void downloadPack(PackEntry packEntry, File outputLocation) {

ToastManager.updateDownloadToastProgress(packEntry, prg);
}, (errorMsg) -> {
if(errorMsg == null) {
ToastManager.updateDownloadToastProgress(packEntry, 1);

if(HashManager.compareRemoteHash(packEntry, outputLocation) == HashComparisonResult.MISMATCH) {
logPackWarn(packEntry, "Resource pack downloaded but the file hash does not match, not applying!");
packEntry.ready = false;
ToastManager.fail(packEntry.name, "Hash Mismatch, file might be corrupted.");
} else {
logPackInfo(packEntry, "Download successful!");
packEntry.ready = true;
ServerLockManager.reloadPackDueToUpdate();
}
} else {
if(errorMsg != null) {
logPackWarn(packEntry, "Failed to download resource pack!");
packEntry.ready = false;
ToastManager.fail(packEntry.name, errorMsg);
return;
}

MinecraftClient mc = MinecraftClient.getInstance();
ToastManager.updateDownloadToastProgress(packEntry, 1);

if(HashManager.compareRemoteHash(packEntry, tmpOutputLocation) == HashComparisonResult.MISMATCH) {
logPackWarn(packEntry, "Resource pack downloaded but the file hash does not match, not applying!");
packEntry.ready = false;
ToastManager.fail(packEntry.name, "Hash Mismatch, file might be corrupted.");
return;
}

logPackInfo(packEntry, "Download successful!");
packEntry.ready = true;

if(outputLocation.exists()) { // Files on Windows are locked, we have to unload the rp first before replacing
List<String> oldPackList = mc.options.resourcePacks;
mc.options.resourcePacks.clear();
Log.info("Clearing all resource pack");

mc.reloadResources().whenComplete((r, v) -> {
try {
Files.deleteIfExists(outputLocation.toPath());
Files.move(tmpOutputLocation.toPath(), outputLocation.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(e);
}
Log.info("Reapplying resource pack");
mc.options.resourcePacks.addAll(oldPackList);
ServerLockManager.reloadPackDueToUpdate();
});
} else {
try {
Files.move(tmpOutputLocation.toPath(), outputLocation.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(e);
}
ServerLockManager.reloadPackDueToUpdate();
}
});
} catch (Exception e) {
e.printStackTrace();
ToastManager.fail(packEntry.name, "Please check console for error.");
}
}

Expand Down
29 changes: 6 additions & 23 deletions src/main/java/com/lx862/rphelper/network/DownloadManager.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.lx862.rphelper.network;

import com.lx862.rphelper.data.Log;
import net.minecraft.client.MinecraftClient;

import java.io.*;
import java.net.*;
import java.nio.file.Files;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class DownloadManager {
Expand Down Expand Up @@ -54,8 +53,6 @@ public static void download(URL url, File outputLocation, Consumer<Double> callb
return;
}



boolean supportsHttpRange = httpUrlConnection.getResponseCode() == 206;
long totalPackSize = supportsHttpRange ? Long.parseLong(httpUrlConnection.getHeaderField("Content-Range").split("/")[1]) : httpUrlConnection.getContentLength();

Expand Down Expand Up @@ -108,24 +105,10 @@ public static void download(URL url, File outputLocation, Consumer<Double> callb

// Merge files
if(successfulSoFar) {
MinecraftClient mc = MinecraftClient.getInstance();
List<String> oldPackList = mc.options.resourcePacks;
mc.options.resourcePacks.clear();

Log.info("Clearing all resource pack");
MinecraftClient.getInstance().reloadResources().whenComplete((r, v) -> {
try {
Files.deleteIfExists(outputLocation.toPath());
} catch (IOException e) {
e.printStackTrace();
}
mergePartFile(outputLocation, totalParts);
cleanupPartFile(outputLocation, totalParts);
finishedCallback.accept(null);
Log.info("Reapplying resource pack");
mc.options.resourcePacks.addAll(oldPackList);
MinecraftClient.getInstance().reloadResources();
});
if(outputLocation.exists()) outputLocation.delete();
mergePartFile(outputLocation, totalParts);
cleanupPartFile(outputLocation, totalParts);
finishedCallback.accept(null);
}
}
}

0 comments on commit 1122089

Please sign in to comment.