Skip to content

Commit

Permalink
feat: upgrade resource manager
Browse files Browse the repository at this point in the history
  • Loading branch information
1zun4 committed Dec 15, 2024
1 parent ff24d88 commit 21ebe58
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 45 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.1+build.3
loader_version=0.15.10

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

loom_version=1.8-SNAPSHOT
Expand Down
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 @@ -220,45 +221,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 (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet(urlString);

try {
URL url = new URL(urlString);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

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 21ebe58

Please sign in to comment.