Spaces:
Build error
Build error
File size: 18,129 Bytes
d46f4a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
package 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.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import net.minecraft.SharedConstants;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.NbtException;
import net.minecraft.nbt.ReportedNbtException;
import net.minecraft.world.level.storage.LevelStorageSource;
import net.minecraft.world.level.validation.ContentValidationException;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
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.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
@OnlyIn(Dist.CLIENT)
public class FileDownload {
static final Logger LOGGER = LogUtils.getLogger();
volatile boolean cancelled;
volatile boolean finished;
volatile boolean error;
volatile boolean extracting;
@Nullable
private volatile File tempFile;
volatile File resourcePackPath;
@Nullable
private volatile HttpGet request;
@Nullable
private Thread currentThread;
private final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(120000).setConnectTimeout(120000).build();
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"
};
public long contentLength(String p_86990_) {
CloseableHttpClient closeablehttpclient = null;
HttpGet httpget = null;
long i;
try {
httpget = new HttpGet(p_86990_);
closeablehttpclient = HttpClientBuilder.create().setDefaultRequestConfig(this.requestConfig).build();
CloseableHttpResponse closeablehttpresponse = closeablehttpclient.execute(httpget);
return Long.parseLong(closeablehttpresponse.getFirstHeader("Content-Length").getValue());
} catch (Throwable throwable) {
LOGGER.error("Unable to get content length for download");
i = 0L;
} finally {
if (httpget != null) {
httpget.releaseConnection();
}
if (closeablehttpclient != null) {
try {
closeablehttpclient.close();
} catch (IOException ioexception) {
LOGGER.error("Could not close http client", (Throwable)ioexception);
}
}
}
return i;
}
public void download(WorldDownload p_86983_, String p_86984_, RealmsDownloadLatestWorldScreen.DownloadStatus p_86985_, LevelStorageSource p_86986_) {
if (this.currentThread == null) {
this.currentThread = new Thread(
() -> {
CloseableHttpClient closeablehttpclient = null;
try {
this.tempFile = File.createTempFile("backup", ".tar.gz");
this.request = new HttpGet(p_86983_.downloadLink);
closeablehttpclient = HttpClientBuilder.create().setDefaultRequestConfig(this.requestConfig).build();
HttpResponse httpresponse = closeablehttpclient.execute(this.request);
p_86985_.totalBytes = Long.parseLong(httpresponse.getFirstHeader("Content-Length").getValue());
if (httpresponse.getStatusLine().getStatusCode() == 200) {
OutputStream outputstream = new FileOutputStream(this.tempFile);
FileDownload.ProgressListener filedownload$progresslistener = new FileDownload.ProgressListener(
p_86984_.trim(), this.tempFile, p_86986_, p_86985_
);
FileDownload.DownloadCountingOutputStream filedownload$downloadcountingoutputstream = new FileDownload.DownloadCountingOutputStream(
outputstream
);
filedownload$downloadcountingoutputstream.setListener(filedownload$progresslistener);
IOUtils.copy(httpresponse.getEntity().getContent(), filedownload$downloadcountingoutputstream);
return;
}
this.error = true;
this.request.abort();
} catch (Exception exception1) {
LOGGER.error("Caught exception while downloading: {}", exception1.getMessage());
this.error = true;
return;
} finally {
this.request.releaseConnection();
if (this.tempFile != null) {
this.tempFile.delete();
}
if (!this.error) {
if (!p_86983_.resourcePackUrl.isEmpty() && !p_86983_.resourcePackHash.isEmpty()) {
try {
this.tempFile = File.createTempFile("resources", ".tar.gz");
this.request = new HttpGet(p_86983_.resourcePackUrl);
HttpResponse httpresponse1 = closeablehttpclient.execute(this.request);
p_86985_.totalBytes = Long.parseLong(httpresponse1.getFirstHeader("Content-Length").getValue());
if (httpresponse1.getStatusLine().getStatusCode() != 200) {
this.error = true;
this.request.abort();
return;
}
OutputStream outputstream1 = new FileOutputStream(this.tempFile);
FileDownload.ResourcePackProgressListener filedownload$resourcepackprogresslistener = new FileDownload.ResourcePackProgressListener(
this.tempFile, p_86985_, p_86983_
);
FileDownload.DownloadCountingOutputStream filedownload$downloadcountingoutputstream1 = new FileDownload.DownloadCountingOutputStream(
outputstream1
);
filedownload$downloadcountingoutputstream1.setListener(filedownload$resourcepackprogresslistener);
IOUtils.copy(httpresponse1.getEntity().getContent(), filedownload$downloadcountingoutputstream1);
} catch (Exception exception) {
LOGGER.error("Caught exception while downloading: {}", exception.getMessage());
this.error = true;
} finally {
this.request.releaseConnection();
if (this.tempFile != null) {
this.tempFile.delete();
}
}
} else {
this.finished = true;
}
}
if (closeablehttpclient != null) {
try {
closeablehttpclient.close();
} catch (IOException ioexception) {
LOGGER.error("Failed to close Realms download client");
}
}
}
}
);
this.currentThread.setUncaughtExceptionHandler(new RealmsDefaultUncaughtExceptionHandler(LOGGER));
this.currentThread.start();
}
}
public void cancel() {
if (this.request != null) {
this.request.abort();
}
if (this.tempFile != null) {
this.tempFile.delete();
}
this.cancelled = true;
}
public boolean isFinished() {
return this.finished;
}
public boolean isError() {
return this.error;
}
public boolean isExtracting() {
return this.extracting;
}
public static String findAvailableFolderName(String p_87002_) {
p_87002_ = p_87002_.replaceAll("[\\./\"]", "_");
for (String s : INVALID_FILE_NAMES) {
if (p_87002_.equalsIgnoreCase(s)) {
p_87002_ = "_" + p_87002_ + "_";
}
}
return p_87002_;
}
void untarGzipArchive(String p_86992_, @Nullable File p_86993_, LevelStorageSource p_86994_) throws IOException {
Pattern pattern = Pattern.compile(".*-([0-9]+)$");
int i = 1;
for (char c0 : SharedConstants.ILLEGAL_FILE_CHARACTERS) {
p_86992_ = p_86992_.replace(c0, '_');
}
if (StringUtils.isEmpty(p_86992_)) {
p_86992_ = "Realm";
}
p_86992_ = findAvailableFolderName(p_86992_);
try {
for (LevelStorageSource.LevelDirectory levelstoragesource$leveldirectory : p_86994_.findLevelCandidates()) {
String s1 = levelstoragesource$leveldirectory.directoryName();
if (s1.toLowerCase(Locale.ROOT).startsWith(p_86992_.toLowerCase(Locale.ROOT))) {
Matcher matcher = pattern.matcher(s1);
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", (Throwable)exception1);
this.error = true;
return;
}
String s;
if (p_86994_.isNewLevelIdAcceptable(p_86992_) && i <= 1) {
s = p_86992_;
} else {
s = p_86992_ + (i == 1 ? "" : "-" + i);
if (!p_86994_.isNewLevelIdAcceptable(s)) {
boolean flag = false;
while (!flag) {
i++;
s = p_86992_ + (i == 1 ? "" : "-" + i);
if (p_86994_.isNewLevelIdAcceptable(s)) {
flag = 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(p_86993_))));
for (TarArchiveEntry tararchiveentry = tararchiveinputstream.getNextTarEntry();
tararchiveentry != null;
tararchiveentry = tararchiveinputstream.getNextTarEntry()
) {
File file2 = new File(file1, tararchiveentry.getName().replace("world", s));
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", (Throwable)exception);
this.error = true;
} finally {
if (tararchiveinputstream != null) {
tararchiveinputstream.close();
}
if (p_86993_ != null) {
p_86993_.delete();
}
try (LevelStorageSource.LevelStorageAccess levelstoragesource$levelstorageaccess = p_86994_.validateAndCreateAccess(s)) {
levelstoragesource$levelstorageaccess.renameAndDropPlayer(s);
} catch (NbtException | ReportedNbtException | IOException ioexception) {
LOGGER.error("Failed to modify unpacked realms level {}", s, ioexception);
} catch (ContentValidationException contentvalidationexception) {
LOGGER.warn("{}", contentvalidationexception.getMessage());
}
this.resourcePackPath = new File(file1, s + File.separator + "resources.zip");
}
}
@OnlyIn(Dist.CLIENT)
static class DownloadCountingOutputStream extends CountingOutputStream {
@Nullable
private ActionListener listener;
public DownloadCountingOutputStream(OutputStream p_193509_) {
super(p_193509_);
}
public void setListener(ActionListener p_87017_) {
this.listener = p_87017_;
}
@Override
protected void afterWrite(int p_87019_) throws IOException {
super.afterWrite(p_87019_);
if (this.listener != null) {
this.listener.actionPerformed(new ActionEvent(this, 0, null));
}
}
}
@OnlyIn(Dist.CLIENT)
class ProgressListener implements ActionListener {
private final String worldName;
private final File tempFile;
private final LevelStorageSource levelStorageSource;
private final RealmsDownloadLatestWorldScreen.DownloadStatus downloadStatus;
ProgressListener(
final String p_87027_, final File p_87028_, final LevelStorageSource p_87029_, final RealmsDownloadLatestWorldScreen.DownloadStatus p_87030_
) {
this.worldName = p_87027_;
this.tempFile = p_87028_;
this.levelStorageSource = p_87029_;
this.downloadStatus = p_87030_;
}
@Override
public void actionPerformed(ActionEvent p_87039_) {
this.downloadStatus.bytesWritten = ((FileDownload.DownloadCountingOutputStream)p_87039_.getSource()).getByteCount();
if (this.downloadStatus.bytesWritten >= this.downloadStatus.totalBytes && !FileDownload.this.cancelled && !FileDownload.this.error) {
try {
FileDownload.this.extracting = true;
FileDownload.this.untarGzipArchive(this.worldName, this.tempFile, this.levelStorageSource);
} catch (IOException ioexception) {
FileDownload.LOGGER.error("Error extracting archive", (Throwable)ioexception);
FileDownload.this.error = true;
}
}
}
}
@OnlyIn(Dist.CLIENT)
class ResourcePackProgressListener implements ActionListener {
private final File tempFile;
private final RealmsDownloadLatestWorldScreen.DownloadStatus downloadStatus;
private final WorldDownload worldDownload;
ResourcePackProgressListener(final File p_87046_, final RealmsDownloadLatestWorldScreen.DownloadStatus p_87047_, final WorldDownload p_87048_) {
this.tempFile = p_87046_;
this.downloadStatus = p_87047_;
this.worldDownload = p_87048_;
}
@Override
public void actionPerformed(ActionEvent p_87056_) {
this.downloadStatus.bytesWritten = ((FileDownload.DownloadCountingOutputStream)p_87056_.getSource()).getByteCount();
if (this.downloadStatus.bytesWritten >= this.downloadStatus.totalBytes && !FileDownload.this.cancelled) {
try {
String s = Hashing.sha1().hashBytes(Files.toByteArray(this.tempFile)).toString();
if (s.equals(this.worldDownload.resourcePackHash)) {
FileUtils.copyFile(this.tempFile, FileDownload.this.resourcePackPath);
FileDownload.this.finished = true;
} else {
FileDownload.LOGGER.error("Resourcepack had wrong hash (expected {}, found {}). Deleting it.", this.worldDownload.resourcePackHash, s);
FileUtils.deleteQuietly(this.tempFile);
FileDownload.this.error = true;
}
} catch (IOException ioexception) {
FileDownload.LOGGER.error("Error copying resourcepack file: {}", ioexception.getMessage());
FileDownload.this.error = true;
}
}
}
}
} |