Skip to content

Commit

Permalink
Merge branch '1.21.1' into 1.21.4
Browse files Browse the repository at this point in the history
# Conflicts:
#	gradle.properties
  • Loading branch information
1zun4 committed Dec 15, 2024
2 parents 95709a1 + 96e4dd6 commit a67eba9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 54 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ yarn_mappings=1.21.4+build.2
loader_version=0.16.9

archives_base_name=mcef
mod_version=1.2.1-1.21.4
mod_version=1.3.0-1.21.4
maven_group=CCBlueX

loom_version=1.9-SNAPSHOT
Expand Down
110 changes: 58 additions & 52 deletions src/main/java/net/ccbluex/liquidbounce/mcef/MCEFResourceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,27 @@
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
* A downloader and extraction tool for java-cef builds.
* <p>
* Downloads for <a href="https://github.com/CinemaMod/java-cef">CinemaMod java-cef</a> are provided by the CinemaMod Group unless changed
* Downloads for <a href="https://github.com/CCBlueX/java-cef">CCBlueX MCEF java-cef</a> are provided by CCBlueX unless changed
* in the MCEFSettings properties file; see {@link MCEFSettings}.
* Email [email protected] for any questions or concerns regarding the file hosting.
* Email [email protected] for any questions or concerns regarding the file hosting.
*/
public class MCEFResourceManager {

private static final String JAVA_CEF_DOWNLOAD_URL =
"${host}/java-cef-builds/${java-cef-commit}/${platform}.tar.gz";
"${host}/mcef-cef/${java-cef-commit}/${platform}";
private static final String JAVA_CEF_CHECKSUM_DOWNLOAD_URL =
"${host}/java-cef-builds/${java-cef-commit}/${platform}.tar.gz.sha256";
"${host}/mcef-cef/${java-cef-commit}/${platform}/checksum";

private final String host;
private final String javaCefCommitHash;
Expand All @@ -65,7 +67,6 @@ private MCEFResourceManager(String host, String javaCefCommitHash, MCEFPlatform
public File getPlatformDirectory() {
return platformDirectory;
}

public File getCommitDirectory() {
return commitDirectory;
}
Expand Down Expand Up @@ -112,17 +113,22 @@ public void downloadJcef() throws IOException {
while (true) {
try {
var tarGzArchive = new File(commitDirectory, platform.getNormalizedName() + ".tar.gz");
if (tarGzArchive.exists() && !tarGzArchive.delete()) {
throw new IOException("Failed to delete existing tar.gz archive");
if (tarGzArchive.exists()) {
FileUtils.forceDelete(tarGzArchive);
}

// Download JCEF from file hosting
MCEF.INSTANCE.getLogger().info("Downloading JCEF...");
progressTracker.setTask("Downloading JCEF");
downloadFile(getJavaCefDownloadUrl(), tarGzArchive, progressTracker);

if (platformDirectory.exists() && !platformDirectory.delete()) {
throw new IOException("Failed to delete existing platform directory");
// Delete existing platform directory
if (platformDirectory.exists()) {
MCEF.INSTANCE.getLogger().info("Deleting existing platform directory...");

// Delete existing platform directory - if this fails,
// we hope [extractTarGz] will overwrite the existing files instead.
FileUtils.deleteQuietly(platformDirectory);
}

// Compare checksum of .tar.gz file with remote checksum file
Expand All @@ -139,9 +145,11 @@ public void downloadJcef() throws IOException {
// Extract JCEF from tar.gz
MCEF.INSTANCE.getLogger().info("Extracting JCEF...");
extractTarGz(tarGzArchive, commitDirectory, progressTracker);
if (tarGzArchive.exists() && !tarGzArchive.delete()) {
if (tarGzArchive.exists() && !FileUtils.deleteQuietly(tarGzArchive)) {
// Retry deletion on exit
tarGzArchive.deleteOnExit();
try {
FileUtils.forceDeleteOnExit(tarGzArchive);
} catch (Exception ignored) { }
}
break;
} catch (Exception e) {
Expand Down Expand Up @@ -190,15 +198,19 @@ private boolean compareChecksum(File checksumFile) throws IOException {
downloadFile(getJavaCefChecksumDownloadUrl(), tempChecksumFile, progressTracker);

if (checksumFile.exists()) {
boolean sameContent = FileUtils.contentEquals(checksumFile, tempChecksumFile);
boolean sameContent = FileUtils.readFileToString(checksumFile, "UTF-8").trim()
.equals(FileUtils.readFileToString(tempChecksumFile, "UTF-8").trim());

if (sameContent) {
tempChecksumFile.delete();
FileUtils.deleteQuietly(tempChecksumFile);
return true;
}

// Delete existing checksum file if it doesn't match the new checksum
FileUtils.delete(checksumFile);
}

tempChecksumFile.renameTo(checksumFile);
FileUtils.moveFile(tempChecksumFile, checksumFile);
return false;
}

Expand All @@ -220,45 +232,39 @@ private boolean compareChecksum(File checksumFile, File archiveFile) {
}

private void downloadFile(String urlString, File outputFile, MCEFProgressTracker percentCompleteConsumer) throws IOException {
try {
MCEF.INSTANCE.getLogger().debug("Downloading '{}' to '{}'", urlString, outputFile.getCanonicalPath());
} catch (IOException e) {
throw new RuntimeException("Error getting canonical path for file", e);
}

try {
URL url = new URL(urlString);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet(urlString);

int responseCode = httpConn.getResponseCode();
if(responseCode != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("HTTP error code: " + responseCode);
}
httpClient.execute(httpGet, response -> {
int status = response.getStatusLine().getStatusCode();
if (status < 200 || status >= 300) {
throw new IOException("Unexpected response status: " + status);
}

int fileSize = httpConn.getContentLength();
if (fileSize <= 0) {
throw new RuntimeException("Cannot read file size or file size is 0");
}
HttpEntity entity = response.getEntity();
if (entity == null) {
throw new IOException("No content returned from " + urlString);
}

try (BufferedInputStream inputStream = new BufferedInputStream(httpConn.getInputStream());
FileOutputStream outputStream = new FileOutputStream(outputFile)) {

byte[] buffer = new byte[2048];
int bytesRead;
int readBytes = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
readBytes += bytesRead;
float percentComplete = (float) readBytes / fileSize;
percentCompleteConsumer.setProgress(percentComplete);
long contentLength = entity.getContentLength();
try (InputStream inputStream = entity.getContent();
FileOutputStream outputStream = new FileOutputStream(outputFile)) {

byte[] buffer = new byte[8192];
int bytesRead;
long totalBytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
if (contentLength > 0) {
float percentComplete = (float) totalBytesRead / contentLength;
percentCompleteConsumer.setProgress(percentComplete);
}
}
}
} catch (IOException e) {
throw new IOException("Error writing to file from input stream", e);
}
} catch (MalformedURLException e) {
throw new RuntimeException("Invalid URL format for " + urlString, e);
} catch (IOException e) {
throw new IOException("Error connecting to " + urlString, e);
EntityUtils.consume(entity);
return null;
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public class MCEFSettings {

private String downloadMirror = "https://dl.liquidbounce.net/resources";
private String downloadMirror = "https://api.liquidbounce.net/api/v3/resource";
private String userAgent = null;
private List<String> cefSwitches = Arrays.asList(
"--autoplay-policy=no-user-gesture-required",
Expand Down

0 comments on commit a67eba9

Please sign in to comment.