| 1 | package net.minecraft.client.resources.server;
|
| 2 |
|
| 3 | import com.google.common.hash.HashCode;
|
| 4 | import java.net.MalformedURLException;
|
| 5 | import java.net.URL;
|
| 6 | import java.nio.file.Path;
|
| 7 | import java.util.ArrayList;
|
| 8 | import java.util.Collection;
|
| 9 | import java.util.HashMap;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Map;
|
| 12 | import java.util.UUID;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import net.minecraft.server.packs.DownloadQueue.BatchResult;
|
| 16 | import net.minecraft.server.packs.DownloadQueue.DownloadRequest;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class ServerPackManager {
|
| 21 | private final PackDownloader downloader;
|
| 22 | private final PackLoadFeedback packLoadFeedback;
|
| 23 | private final PackReloadConfig reloadConfig;
|
| 24 | private final Runnable updateRequest;
|
| 25 | private ServerPackManager.PackPromptStatus packPromptStatus;
|
| 26 | private final List<ServerPackManager.ServerPackData> packs = new ArrayList<>();
|
| 27 |
|
| 28 | public ServerPackManager(
|
| 29 | final PackDownloader downloader,
|
| 30 | final PackLoadFeedback packLoadFeedback,
|
| 31 | final PackReloadConfig reloadConfig,
|
| 32 | final Runnable updateRequest,
|
| 33 | final ServerPackManager.PackPromptStatus packPromptStatus
|
| 34 | ) {
|
| 35 | this.downloader = downloader;
|
| 36 | this.packLoadFeedback = packLoadFeedback;
|
| 37 | this.reloadConfig = reloadConfig;
|
| 38 | this.updateRequest = updateRequest;
|
| 39 | this.packPromptStatus = packPromptStatus;
|
| 40 | }
|
| 41 |
|
| 42 | private void registerForUpdate() {
|
| 43 | this.updateRequest.run();
|
| 44 | }
|
| 45 |
|
| 46 | private void markExistingPacksAsRemoved(final UUID id) {
|
| 47 | for (ServerPackManager.ServerPackData pack : this.packs) {
|
| 48 | if (pack.id.equals(id)) {
|
| 49 | pack.setRemovalReasonIfNotSet(ServerPackManager.RemovalReason.SERVER_REPLACED);
|
| 50 | }
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | public void pushPack(final UUID id, final URL url, final @Nullable HashCode hash) {
|
| 55 | if (this.packPromptStatus == ServerPackManager.PackPromptStatus.DECLINED) {
|
| 56 | this.packLoadFeedback.reportFinalResult(id, PackLoadFeedback.FinalResult.DECLINED);
|
| 57 | } else {
|
| 58 | this.pushNewPack(id, new ServerPackManager.ServerPackData(id, url, hash));
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | public void pushLocalPack(final UUID id, final Path path) {
|
| 63 | if (this.packPromptStatus == ServerPackManager.PackPromptStatus.DECLINED) {
|
| 64 | this.packLoadFeedback.reportFinalResult(id, PackLoadFeedback.FinalResult.DECLINED);
|
| 65 | } else {
|
| 66 | URL url;
|
| 67 | try {
|
| 68 | url = path.toUri().toURL();
|
| 69 | } catch (MalformedURLException e) {
|
| 70 | throw new IllegalStateException("Can't convert path to URL " + path, e);
|
| 71 | }
|
| 72 |
|
| 73 | ServerPackManager.ServerPackData pack = new ServerPackManager.ServerPackData(id, url, null);
|
| 74 | pack.downloadStatus = ServerPackManager.PackDownloadStatus.DONE;
|
| 75 | pack.path = path;
|
| 76 | this.pushNewPack(id, pack);
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | private void pushNewPack(final UUID id, final ServerPackManager.ServerPackData pack) {
|
| 81 | this.markExistingPacksAsRemoved(id);
|
| 82 | this.packs.add(pack);
|
| 83 | if (this.packPromptStatus == ServerPackManager.PackPromptStatus.ALLOWED) {
|
| 84 | this.acceptPack(pack);
|
| 85 | }
|
| 86 |
|
| 87 | this.registerForUpdate();
|
| 88 | }
|
| 89 |
|
| 90 | private void acceptPack(final ServerPackManager.ServerPackData pack) {
|
| 91 | this.packLoadFeedback.reportUpdate(pack.id, PackLoadFeedback.Update.ACCEPTED);
|
| 92 | pack.promptAccepted = true;
|
| 93 | }
|
| 94 |
|
| 95 | private ServerPackManager.@Nullable ServerPackData findPackInfo(final UUID id) {
|
| 96 | for (ServerPackManager.ServerPackData pack : this.packs) {
|
| 97 | if (!pack.isRemoved() && pack.id.equals(id)) {
|
| 98 | return pack;
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | return null;
|
| 103 | }
|
| 104 |
|
| 105 | public void popPack(final UUID id) {
|
| 106 | ServerPackManager.ServerPackData packInfo = this.findPackInfo(id);
|
| 107 | if (packInfo != null) {
|
| 108 | packInfo.setRemovalReasonIfNotSet(ServerPackManager.RemovalReason.SERVER_REMOVED);
|
| 109 | this.registerForUpdate();
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | public void popAll() {
|
| 114 | for (ServerPackManager.ServerPackData pack : this.packs) {
|
| 115 | pack.setRemovalReasonIfNotSet(ServerPackManager.RemovalReason.SERVER_REMOVED);
|
| 116 | }
|
| 117 |
|
| 118 | this.registerForUpdate();
|
| 119 | }
|
| 120 |
|
| 121 | public void allowServerPacks() {
|
| 122 | this.packPromptStatus = ServerPackManager.PackPromptStatus.ALLOWED;
|
| 123 |
|
| 124 | for (ServerPackManager.ServerPackData pack : this.packs) {
|
| 125 | if (!pack.promptAccepted && !pack.isRemoved()) {
|
| 126 | this.acceptPack(pack);
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | this.registerForUpdate();
|
| 131 | }
|
| 132 |
|
| 133 | public void rejectServerPacks() {
|
| 134 | this.packPromptStatus = ServerPackManager.PackPromptStatus.DECLINED;
|
| 135 |
|
| 136 | for (ServerPackManager.ServerPackData pack : this.packs) {
|
| 137 | if (!pack.promptAccepted) {
|
| 138 | pack.setRemovalReasonIfNotSet(ServerPackManager.RemovalReason.DECLINED);
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | this.registerForUpdate();
|
| 143 | }
|
| 144 |
|
| 145 | public void resetPromptStatus() {
|
| 146 | this.packPromptStatus = ServerPackManager.PackPromptStatus.PENDING;
|
| 147 | }
|
| 148 |
|
| 149 | public void tick() {
|
| 150 | boolean downloadsPending = this.updateDownloads();
|
| 151 | if (!downloadsPending) {
|
| 152 | this.triggerReloadIfNeeded();
|
| 153 | }
|
| 154 |
|
| 155 | this.cleanupRemovedPacks();
|
| 156 | }
|
| 157 |
|
| 158 | private void cleanupRemovedPacks() {
|
| 159 | this.packs.removeIf(data -> {
|
| 160 | if (data.activationStatus != ServerPackManager.ActivationStatus.INACTIVE) {
|
| 161 | return false;
|
| 162 | }
|
| 163 |
|
| 164 | if (data.removalReason != null) {
|
| 165 | PackLoadFeedback.FinalResult response = data.removalReason.serverResponse;
|
| 166 | if (response != null) {
|
| 167 | this.packLoadFeedback.reportFinalResult(data.id, response);
|
| 168 | }
|
| 169 |
|
| 170 | return true;
|
| 171 | } else {
|
| 172 | return false;
|
| 173 | }
|
| 174 | });
|
| 175 | }
|
| 176 |
|
| 177 | private void onDownload(final Collection<ServerPackManager.ServerPackData> data, final BatchResult result) {
|
| 178 | if (!result.failed().isEmpty()) {
|
| 179 | for (ServerPackManager.ServerPackData pack : this.packs) {
|
| 180 | if (pack.activationStatus != ServerPackManager.ActivationStatus.ACTIVE) {
|
| 181 | if (result.failed().contains(pack.id)) {
|
| 182 | pack.setRemovalReasonIfNotSet(ServerPackManager.RemovalReason.DOWNLOAD_FAILED);
|
| 183 | } else {
|
| 184 | pack.setRemovalReasonIfNotSet(ServerPackManager.RemovalReason.DISCARDED);
|
| 185 | }
|
| 186 | }
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | for (ServerPackManager.ServerPackData pack : data) {
|
| 191 | Path packFile = result.downloaded().get(pack.id);
|
| 192 | if (packFile != null) {
|
| 193 | pack.downloadStatus = ServerPackManager.PackDownloadStatus.DONE;
|
| 194 | pack.path = packFile;
|
| 195 | if (!pack.isRemoved()) {
|
| 196 | this.packLoadFeedback.reportUpdate(pack.id, PackLoadFeedback.Update.DOWNLOADED);
|
| 197 | }
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | this.registerForUpdate();
|
| 202 | }
|
| 203 |
|
| 204 | private boolean updateDownloads() {
|
| 205 | List<ServerPackManager.ServerPackData> downloadPacks = new ArrayList<>();
|
| 206 | boolean downloadsInProgress = false;
|
| 207 |
|
| 208 | for (ServerPackManager.ServerPackData pack : this.packs) {
|
| 209 | if (!pack.isRemoved() && pack.promptAccepted) {
|
| 210 | if (pack.downloadStatus != ServerPackManager.PackDownloadStatus.DONE) {
|
| 211 | downloadsInProgress = true;
|
| 212 | }
|
| 213 |
|
| 214 | if (pack.downloadStatus == ServerPackManager.PackDownloadStatus.REQUESTED) {
|
| 215 | pack.downloadStatus = ServerPackManager.PackDownloadStatus.PENDING;
|
| 216 | downloadPacks.add(pack);
|
| 217 | }
|
| 218 | }
|
| 219 | }
|
| 220 |
|
| 221 | if (!downloadPacks.isEmpty()) {
|
| 222 | Map<UUID, DownloadRequest> downloadRequests = new HashMap<>();
|
| 223 |
|
| 224 | for (ServerPackManager.ServerPackData pack : downloadPacks) {
|
| 225 | downloadRequests.put(pack.id, new DownloadRequest(pack.url, pack.hash));
|
| 226 | }
|
| 227 |
|
| 228 | this.downloader.download(downloadRequests, result -> this.onDownload(downloadPacks, result));
|
| 229 | }
|
| 230 |
|
| 231 | return downloadsInProgress;
|
| 232 | }
|
| 233 |
|
| 234 | private void triggerReloadIfNeeded() {
|
| 235 | boolean needsReload = false;
|
| 236 | final List<ServerPackManager.ServerPackData> packsToLoad = new ArrayList<>();
|
| 237 | final List<ServerPackManager.ServerPackData> packsToUnload = new ArrayList<>();
|
| 238 |
|
| 239 | for (ServerPackManager.ServerPackData pack : this.packs) {
|
| 240 | if (pack.activationStatus == ServerPackManager.ActivationStatus.PENDING) {
|
| 241 | return;
|
| 242 | }
|
| 243 |
|
| 244 | boolean shouldBeActive = pack.promptAccepted && pack.downloadStatus == ServerPackManager.PackDownloadStatus.DONE && !pack.isRemoved();
|
| 245 | if (shouldBeActive && pack.activationStatus == ServerPackManager.ActivationStatus.INACTIVE) {
|
| 246 | packsToLoad.add(pack);
|
| 247 | needsReload = true;
|
| 248 | }
|
| 249 |
|
| 250 | if (pack.activationStatus == ServerPackManager.ActivationStatus.ACTIVE) {
|
| 251 | if (!shouldBeActive) {
|
| 252 | needsReload = true;
|
| 253 | packsToUnload.add(pack);
|
| 254 | } else {
|
| 255 | packsToLoad.add(pack);
|
| 256 | }
|
| 257 | }
|
| 258 | }
|
| 259 |
|
| 260 | if (needsReload) {
|
| 261 | for (ServerPackManager.ServerPackData pack : packsToLoad) {
|
| 262 | if (pack.activationStatus != ServerPackManager.ActivationStatus.ACTIVE) {
|
| 263 | pack.activationStatus = ServerPackManager.ActivationStatus.PENDING;
|
| 264 | }
|
| 265 | }
|
| 266 |
|
| 267 | for (ServerPackManager.ServerPackData pack : packsToUnload) {
|
| 268 | pack.activationStatus = ServerPackManager.ActivationStatus.PENDING;
|
| 269 | }
|
| 270 |
|
| 271 | this.reloadConfig.scheduleReload(new PackReloadConfig.Callbacks() {
|
| 272 | @Override
|
| 273 | public void onSuccess() {
|
| 274 | for (ServerPackManager.ServerPackData pack : packsToLoad) {
|
| 275 | pack.activationStatus = ServerPackManager.ActivationStatus.ACTIVE;
|
| 276 | if (pack.removalReason == null) {
|
| 277 | ServerPackManager.this.packLoadFeedback.reportFinalResult(pack.id, PackLoadFeedback.FinalResult.APPLIED);
|
| 278 | }
|
| 279 | }
|
| 280 |
|
| 281 | for (ServerPackManager.ServerPackData pack : packsToUnload) {
|
| 282 | pack.activationStatus = ServerPackManager.ActivationStatus.INACTIVE;
|
| 283 | }
|
| 284 |
|
| 285 | ServerPackManager.this.registerForUpdate();
|
| 286 | }
|
| 287 |
|
| 288 | @Override
|
| 289 | public void onFailure(final boolean isRecovery) {
|
| 290 | if (!isRecovery) {
|
| 291 | packsToLoad.clear();
|
| 292 |
|
| 293 | for (ServerPackManager.ServerPackData pack : ServerPackManager.this.packs) {
|
| 294 | switch (pack.activationStatus) {
|
| 295 | case INACTIVE:
|
| 296 | pack.setRemovalReasonIfNotSet(ServerPackManager.RemovalReason.DISCARDED);
|
| 297 | break;
|
| 298 | case PENDING:
|
| 299 | pack.activationStatus = ServerPackManager.ActivationStatus.INACTIVE;
|
| 300 | pack.setRemovalReasonIfNotSet(ServerPackManager.RemovalReason.ACTIVATION_FAILED);
|
| 301 | break;
|
| 302 | case ACTIVE:
|
| 303 | packsToLoad.add(pack);
|
| 304 | }
|
| 305 | }
|
| 306 |
|
| 307 | ServerPackManager.this.registerForUpdate();
|
| 308 | } else {
|
| 309 | for (ServerPackManager.ServerPackData pack : ServerPackManager.this.packs) {
|
| 310 | if (pack.activationStatus == ServerPackManager.ActivationStatus.PENDING) {
|
| 311 | pack.activationStatus = ServerPackManager.ActivationStatus.INACTIVE;
|
| 312 | }
|
| 313 | }
|
| 314 | }
|
| 315 | }
|
| 316 |
|
| 317 | @Override
|
| 318 | public List<PackReloadConfig.IdAndPath> packsToLoad() {
|
| 319 | return packsToLoad.stream().map(pack -> new PackReloadConfig.IdAndPath(pack.id, pack.path)).toList();
|
| 320 | }
|
| 321 | });
|
| 322 | }
|
| 323 | }
|
| 324 |
|
| 325 | @Environment(EnvType.CLIENT)
|
| 326 | private enum ActivationStatus {
|
| 327 | INACTIVE,
|
| 328 | PENDING,
|
| 329 | ACTIVE;
|
| 330 | }
|
| 331 |
|
| 332 | @Environment(EnvType.CLIENT)
|
| 333 | private enum PackDownloadStatus {
|
| 334 | REQUESTED,
|
| 335 | PENDING,
|
| 336 | DONE;
|
| 337 | }
|
| 338 |
|
| 339 | @Environment(EnvType.CLIENT)
|
| 340 | public enum PackPromptStatus {
|
| 341 | PENDING,
|
| 342 | ALLOWED,
|
| 343 | DECLINED;
|
| 344 | }
|
| 345 |
|
| 346 | @Environment(EnvType.CLIENT)
|
| 347 | private enum RemovalReason {
|
| 348 | DOWNLOAD_FAILED(PackLoadFeedback.FinalResult.DOWNLOAD_FAILED),
|
| 349 | ACTIVATION_FAILED(PackLoadFeedback.FinalResult.ACTIVATION_FAILED),
|
| 350 | DECLINED(PackLoadFeedback.FinalResult.DECLINED),
|
| 351 | DISCARDED(PackLoadFeedback.FinalResult.DISCARDED),
|
| 352 | SERVER_REMOVED(null),
|
| 353 | SERVER_REPLACED(null);
|
| 354 |
|
| 355 | private final PackLoadFeedback.@Nullable FinalResult serverResponse;
|
| 356 |
|
| 357 | RemovalReason(final PackLoadFeedback.@Nullable FinalResult serverResponse) {
|
| 358 | this.serverResponse = serverResponse;
|
| 359 | }
|
| 360 | }
|
| 361 |
|
| 362 | @Environment(EnvType.CLIENT)
|
| 363 | private static class ServerPackData {
|
| 364 | private final UUID id;
|
| 365 | private final URL url;
|
| 366 | private final @Nullable HashCode hash;
|
| 367 | private @Nullable Path path;
|
| 368 | private ServerPackManager.@Nullable RemovalReason removalReason;
|
| 369 | private ServerPackManager.PackDownloadStatus downloadStatus = ServerPackManager.PackDownloadStatus.REQUESTED;
|
| 370 | private ServerPackManager.ActivationStatus activationStatus = ServerPackManager.ActivationStatus.INACTIVE;
|
| 371 | private boolean promptAccepted;
|
| 372 |
|
| 373 | private ServerPackData(final UUID id, final URL url, final @Nullable HashCode hash) {
|
| 374 | this.id = id;
|
| 375 | this.url = url;
|
| 376 | this.hash = hash;
|
| 377 | }
|
| 378 |
|
| 379 | public void setRemovalReasonIfNotSet(final ServerPackManager.RemovalReason removalReason) {
|
| 380 | if (this.removalReason == null) {
|
| 381 | this.removalReason = removalReason;
|
| 382 | }
|
| 383 | }
|
| 384 |
|
| 385 | public boolean isRemoved() {
|
| 386 | return this.removalReason != null;
|
| 387 | }
|
| 388 | }
|
| 389 | }
|
| 390 |
|