Something went wrong. Try again.
The Minecraft: Java Edition decompiled source code.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397package com.mojang.realmsclient.client;
import com.google.common.hash.Hashing;import com.google.common.io.Files;import com.mojang.logging.LogUtils;import com.mojang.realmsclient.dto.WorldDownload;import com.mojang.realmsclient.exception.RealmsDefaultUncaughtExceptionHandler;import com.mojang.realmsclient.gui.screens.RealmsDownloadLatestWorldScreen;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;import java.net.http.HttpRequest.Builder;import java.net.http.HttpResponse.BodyHandlers;import java.time.Duration;import java.util.Locale;import java.util.OptionalLong;import java.util.concurrent.CancellationException;import java.util.concurrent.CompletableFuture;import java.util.concurrent.CompletionException;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.annotation.CheckReturnValue;import net.minecraft.SharedConstants;import net.minecraft.client.Minecraft;import net.minecraft.nbt.NbtException;import net.minecraft.nbt.ReportedNbtException;import net.minecraft.util.Util;import net.minecraft.world.level.storage.LevelStorageSource;import net.minecraft.world.level.validation.ContentValidationException;import org.apache.commons.compress.archivers.tar.TarArchiveEntry;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.commons.io.IOUtils;import org.apache.commons.io.output.CountingOutputStream;import org.apache.commons.lang3.StringUtils;import org.jspecify.annotations.Nullable;import org.slf4j.Logger;
public class FileDownload { private static final Logger LOGGER = LogUtils.getLogger(); private volatile boolean cancelled; private volatile boolean finished; private volatile boolean error; private volatile boolean extracting; @Nullable private volatile File tempFile; private volatile File resourcePackPath; @Nullable private volatile CompletableFuture<?> pendingRequest; @Nullable private Thread currentThread; private static final String[] INVALID_FILE_NAMES = new String[]{ "CON", "COM", "PRN", "AUX", "CLOCK$", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" };
@Nullable private <T> T joinCancellableRequest(CompletableFuture<T> completableFuture) throws Throwable { this.pendingRequest = completableFuture; if (this.cancelled) { completableFuture.cancel(true); return null; } else { try { try { return (T)completableFuture.join(); } catch (CompletionException completionException) { throw completionException.getCause(); } } catch (CancellationException cancellationException) { return null; } } }
private static HttpClient createClient() { return HttpClient.newBuilder().executor(Util.nonCriticalIoPool()).connectTimeout(Duration.ofMinutes(2L)).build(); }
private static Builder createRequest(String string) { return HttpRequest.newBuilder(URI.create(string)).timeout(Duration.ofMinutes(2L)); }
@CheckReturnValue public static OptionalLong contentLength(String string) { try { OptionalLong optionalLong; try (HttpClient httpClient = createClient()) { HttpResponse httpResponse = httpClient.send(createRequest(string).HEAD().build(), BodyHandlers.discarding()); optionalLong = httpResponse.headers().firstValueAsLong("Content-Length"); }
return optionalLong; } catch (Exception exception) { LOGGER.error("Unable to get content length for download"); return OptionalLong.empty(); } }
public void download( WorldDownload worldDownload, String string, RealmsDownloadLatestWorldScreen.DownloadStatus downloadStatus, LevelStorageSource levelStorageSource ) { if (this.currentThread == null) { this.currentThread = new Thread(() -> { try (HttpClient httpClient = createClient()) { try { this.tempFile = File.createTempFile("backup", ".tar.gz"); this.download(downloadStatus, httpClient, worldDownload.downloadLink(), this.tempFile); this.finishWorldDownload(string.trim(), this.tempFile, levelStorageSource, downloadStatus); } catch (Exception exception1) { LOGGER.error("Caught exception while downloading world", exception1); this.error = true; } finally { this.pendingRequest = null; if (this.tempFile != null) { this.tempFile.delete(); }
this.tempFile = null; }
if (this.error) { return; }
String string2 = worldDownload.resourcePackUrl(); if (!string2.isEmpty() && !worldDownload.resourcePackHash().isEmpty()) { try { this.tempFile = File.createTempFile("resources", ".tar.gz"); this.download(downloadStatus, httpClient, string2, this.tempFile); this.finishResourcePackDownload(downloadStatus, this.tempFile, worldDownload); } catch (Exception exception) { LOGGER.error("Caught exception while downloading resource pack", exception); this.error = true; } finally { this.pendingRequest = null; if (this.tempFile != null) { this.tempFile.delete(); }
this.tempFile = null; } }
this.finished = true; } }); this.currentThread.setUncaughtExceptionHandler(new RealmsDefaultUncaughtExceptionHandler(LOGGER)); this.currentThread.start(); } }
private void download(RealmsDownloadLatestWorldScreen.DownloadStatus downloadStatus, HttpClient httpClient, String string, File file) throws IOException { HttpRequest httpRequest = createRequest(string).GET().build();
HttpResponse httpResponse; try { httpResponse = this.joinCancellableRequest(httpClient.sendAsync(httpRequest, BodyHandlers.ofInputStream())); } catch (Error errorx) { throw errorx; } catch (Throwable throwable) { LOGGER.error("Failed to download {}", string, throwable); this.error = true; return; }
if (httpResponse != null && !this.cancelled) { if (httpResponse.statusCode() != 200) { this.error = true; } else { downloadStatus.totalBytes = httpResponse.headers().firstValueAsLong("Content-Length").orElse(0L);
try ( InputStream inputStream = (InputStream)httpResponse.body(); FileOutputStream fileOutputStream = new FileOutputStream(file); ) { inputStream.transferTo(new FileDownload.DownloadCountingOutputStream(fileOutputStream, downloadStatus)); } } } }
public void cancel() { if (this.tempFile != null) { this.tempFile.delete(); this.tempFile = null; }
this.cancelled = true; CompletableFuture completableFuture = this.pendingRequest; if (completableFuture != null) { completableFuture.cancel(true); } }
public boolean isFinished() { return this.finished; }
public boolean isError() { return this.error; }
public boolean isExtracting() { return this.extracting; }
public static String findAvailableFolderName(String string) { string = string.replaceAll("[\\./\"]", "_");
for (String string1 : INVALID_FILE_NAMES) { if (string.equalsIgnoreCase(string1)) { string = "_" + string + "_"; } }
return string; }
private void untarGzipArchive(String string, @Nullable File file, LevelStorageSource levelStorageSource) throws IOException { Pattern pattern = Pattern.compile(".*-([0-9]+)$"); int i = 1;
for (char c : SharedConstants.ILLEGAL_FILE_CHARACTERS) { string = string.replace(c, '_'); }
if (StringUtils.isEmpty(string)) { string = "Realm"; }
string = findAvailableFolderName(string);
try { for (LevelStorageSource.LevelDirectory levelDirectory : levelStorageSource.findLevelCandidates()) { String string2 = levelDirectory.directoryName(); if (string2.toLowerCase(Locale.ROOT).startsWith(string.toLowerCase(Locale.ROOT))) { Matcher matcher = pattern.matcher(string2); if (matcher.matches()) { int j = Integer.parseInt(matcher.group(1)); if (j > i) { i = j; } } else { i++; } } } } catch (Exception exception1) { LOGGER.error("Error getting level list", exception1); this.error = true; return; }
String string1; if (levelStorageSource.isNewLevelIdAcceptable(string) && i <= 1) { string1 = string; } else { string1 = string + (i == 1 ? "" : "-" + i); if (!levelStorageSource.isNewLevelIdAcceptable(string1)) { boolean bl = false;
while (!bl) { i++; string1 = string + (i == 1 ? "" : "-" + i); if (levelStorageSource.isNewLevelIdAcceptable(string1)) { bl = true; } } } }
TarArchiveInputStream tarArchiveInputStream = null; File file1 = new File(Minecraft.getInstance().gameDirectory.getAbsolutePath(), "saves");
try { file1.mkdir(); tarArchiveInputStream = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(file))));
for (TarArchiveEntry tarArchiveEntry = tarArchiveInputStream.getNextTarEntry(); tarArchiveEntry != null; tarArchiveEntry = tarArchiveInputStream.getNextTarEntry() ) { File file2 = new File(file1, tarArchiveEntry.getName().replace("world", string1)); if (tarArchiveEntry.isDirectory()) { file2.mkdirs(); } else { file2.createNewFile();
try (FileOutputStream fileOutputStream = new FileOutputStream(file2)) { IOUtils.copy(tarArchiveInputStream, fileOutputStream); } } } } catch (Exception exception) { LOGGER.error("Error extracting world", exception); this.error = true; } finally { if (tarArchiveInputStream != null) { tarArchiveInputStream.close(); }
if (file != null) { file.delete(); }
try (LevelStorageSource.LevelStorageAccess levelStorageAccess = levelStorageSource.validateAndCreateAccess(string1)) { levelStorageAccess.renameAndDropPlayer(string1); } catch (NbtException | ReportedNbtException | IOException iOException) { LOGGER.error("Failed to modify unpacked realms level {}", string1, iOException); } catch (ContentValidationException contentValidationException) { LOGGER.warn("Failed to download file", contentValidationException); }
this.resourcePackPath = new File(file1, string1 + File.separator + "resources.zip"); } }
private void finishWorldDownload( String string, File file, LevelStorageSource levelStorageSource, RealmsDownloadLatestWorldScreen.DownloadStatus downloadStatus ) { if (downloadStatus.bytesWritten >= downloadStatus.totalBytes && !this.cancelled && !this.error) { try { this.extracting = true; this.untarGzipArchive(string, file, levelStorageSource); } catch (IOException iOException) { LOGGER.error("Error extracting archive", iOException); this.error = true; } } }
private void finishResourcePackDownload(RealmsDownloadLatestWorldScreen.DownloadStatus downloadStatus, File file, WorldDownload worldDownload) { if (downloadStatus.bytesWritten >= downloadStatus.totalBytes && !this.cancelled) { try { String string = Hashing.sha1().hashBytes(Files.toByteArray(file)).toString(); if (string.equals(worldDownload.resourcePackHash())) { FileUtils.copyFile(file, this.resourcePackPath); this.finished = true; } else { LOGGER.error("Resourcepack had wrong hash (expected {}, found {}). Deleting it.", worldDownload.resourcePackHash(), string); FileUtils.deleteQuietly(file); this.error = true; } } catch (IOException iOException) { LOGGER.error("Error copying resourcepack file: {}", iOException.getMessage()); this.error = true; } } }
static class DownloadCountingOutputStream extends CountingOutputStream { private final RealmsDownloadLatestWorldScreen.DownloadStatus downloadStatus;
public DownloadCountingOutputStream(OutputStream outputStream, RealmsDownloadLatestWorldScreen.DownloadStatus downloadStatus) { super(outputStream); this.downloadStatus = downloadStatusx; }
protected void afterWrite(int i) throws IOException { super.afterWrite(i); this.downloadStatus.bytesWritten = this.getByteCount(); } }}