| 1 | package com.mojang.realmsclient.client.worldupload;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.realmsclient.client.FileUpload;
|
| 5 | import com.mojang.realmsclient.client.RealmsClient;
|
| 6 | import com.mojang.realmsclient.dto.RealmsSlot;
|
| 7 | import com.mojang.realmsclient.dto.UploadInfo;
|
| 8 | import com.mojang.realmsclient.exception.RealmsServiceException;
|
| 9 | import com.mojang.realmsclient.exception.RetryCallException;
|
| 10 | import com.mojang.realmsclient.gui.screens.UploadResult;
|
| 11 | import com.mojang.realmsclient.util.UploadTokenCache;
|
| 12 | import java.io.File;
|
| 13 | import java.nio.file.Path;
|
| 14 | import java.util.concurrent.CancellationException;
|
| 15 | import java.util.concurrent.CompletableFuture;
|
| 16 | import java.util.concurrent.CompletionException;
|
| 17 | import net.fabricmc.api.EnvType;
|
| 18 | import net.fabricmc.api.Environment;
|
| 19 | import net.minecraft.SharedConstants;
|
| 20 | import net.minecraft.client.User;
|
| 21 | import net.minecraft.util.Util;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 | import org.slf4j.Logger;
|
| 24 |
|
| 25 | @Environment(EnvType.CLIENT)
|
| 26 | public class RealmsWorldUpload {
|
| 27 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 28 | public static final int UPLOAD_RETRIES = 20;
|
| 29 | private final RealmsClient client = RealmsClient.getOrCreate();
|
| 30 | private final Path worldFolder;
|
| 31 | private final RealmsSlot realmsSlot;
|
| 32 | private final User user;
|
| 33 | private final long realmId;
|
| 34 | private final RealmsWorldUploadStatusTracker statusCallback;
|
| 35 | private volatile boolean cancelled;
|
| 36 | private volatile @Nullable CompletableFuture<?> uploadTask;
|
| 37 |
|
| 38 | public RealmsWorldUpload(
|
| 39 | final Path worldFolder, final RealmsSlot realmsSlot, final User user, final long realmId, final RealmsWorldUploadStatusTracker statusCallback
|
| 40 | ) {
|
| 41 | this.worldFolder = worldFolder;
|
| 42 | this.realmsSlot = realmsSlot;
|
| 43 | this.user = user;
|
| 44 | this.realmId = realmId;
|
| 45 | this.statusCallback = statusCallback;
|
| 46 | }
|
| 47 |
|
| 48 | public CompletableFuture<?> packAndUpload() {
|
| 49 | return CompletableFuture.runAsync(
|
| 50 | () -> {
|
| 51 | File archive = null;
|
| 52 |
|
| 53 | try {
|
| 54 | UploadInfo uploadInfo = this.requestUploadInfoWithRetries();
|
| 55 | archive = RealmsUploadWorldPacker.pack(this.worldFolder, () -> this.cancelled);
|
| 56 | this.statusCallback.setUploading();
|
| 57 |
|
| 58 | try (FileUpload fileUpload = new FileUpload(
|
| 59 | archive,
|
| 60 | this.realmId,
|
| 61 | this.realmsSlot.slotId,
|
| 62 | uploadInfo,
|
| 63 | this.user,
|
| 64 | SharedConstants.getCurrentVersion().name(),
|
| 65 | this.realmsSlot.options.version,
|
| 66 | this.statusCallback.getUploadStatus()
|
| 67 | )) {
|
| 68 | CompletableFuture<UploadResult> uploadTask = fileUpload.startUpload();
|
| 69 | this.uploadTask = uploadTask;
|
| 70 | if (!this.cancelled) {
|
| 71 | UploadResult join;
|
| 72 | try {
|
| 73 | join = uploadTask.join();
|
| 74 | } catch (CompletionException e) {
|
| 75 | throw e.getCause();
|
| 76 | }
|
| 77 |
|
| 78 | String errorMessage = join.getSimplifiedErrorMessage();
|
| 79 | if (errorMessage != null) {
|
| 80 | throw new RealmsUploadFailedException(errorMessage);
|
| 81 | }
|
| 82 |
|
| 83 | UploadTokenCache.invalidate(this.realmId);
|
| 84 | this.client.updateSlot(this.realmId, this.realmsSlot.slotId, this.realmsSlot.options, this.realmsSlot.settings);
|
| 85 | return;
|
| 86 | }
|
| 87 |
|
| 88 | uploadTask.cancel(true);
|
| 89 | }
|
| 90 | } catch (RealmsServiceException e) {
|
| 91 | throw new RealmsUploadFailedException(e.realmsError.errorMessage());
|
| 92 | } catch (InterruptedException | CancellationException e) {
|
| 93 | throw new RealmsUploadCanceledException();
|
| 94 | } catch (RealmsUploadException e) {
|
| 95 | throw e;
|
| 96 | } catch (Throwable e) {
|
| 97 | if (e instanceof Error error) {
|
| 98 | throw error;
|
| 99 | }
|
| 100 |
|
| 101 | throw new RealmsUploadFailedException(e.getMessage());
|
| 102 | } finally {
|
| 103 | if (archive != null) {
|
| 104 | LOGGER.debug("Deleting file {}", archive.getAbsolutePath());
|
| 105 | archive.delete();
|
| 106 | }
|
| 107 | }
|
| 108 | },
|
| 109 | Util.backgroundExecutor()
|
| 110 | );
|
| 111 | }
|
| 112 |
|
| 113 | public void cancel() {
|
| 114 | this.cancelled = true;
|
| 115 | CompletableFuture<?> uploadTask = this.uploadTask;
|
| 116 | if (uploadTask != null) {
|
| 117 | uploadTask.cancel(true);
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | private UploadInfo requestUploadInfoWithRetries() throws RealmsServiceException, InterruptedException {
|
| 122 | for (int i = 0; i < 20; i++) {
|
| 123 | try {
|
| 124 | UploadInfo uploadInfo = this.client.requestUploadInfo(this.realmId);
|
| 125 | if (this.cancelled) {
|
| 126 | throw new RealmsUploadCanceledException();
|
| 127 | }
|
| 128 |
|
| 129 | if (uploadInfo != null) {
|
| 130 | if (!uploadInfo.worldClosed()) {
|
| 131 | throw new RealmsUploadWorldNotClosedException();
|
| 132 | }
|
| 133 |
|
| 134 | return uploadInfo;
|
| 135 | }
|
| 136 | } catch (RetryCallException e) {
|
| 137 | Thread.sleep(e.delaySeconds * 1000L);
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | throw new RealmsUploadWorldNotClosedException();
|
| 142 | }
|
| 143 | }
|
| 144 |
|