| 1 | package net.minecraft.server.packs.resources;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.HashSet;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Objects;
|
| 7 | import java.util.Set;
|
| 8 | import java.util.concurrent.CompletableFuture;
|
| 9 | import java.util.concurrent.CompletionStage;
|
| 10 | import java.util.concurrent.Executor;
|
| 11 | import java.util.concurrent.atomic.AtomicInteger;
|
| 12 | import net.minecraft.util.Unit;
|
| 13 | import net.minecraft.util.Util;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | public class SimpleReloadInstance<S> implements ReloadInstance {
|
| 17 | private static final int PREPARATION_PROGRESS_WEIGHT = 2;
|
| 18 | private static final int EXTRA_RELOAD_PROGRESS_WEIGHT = 2;
|
| 19 | private static final int LISTENER_PROGRESS_WEIGHT = 1;
|
| 20 | private final CompletableFuture<Unit> allPreparations = new CompletableFuture<>();
|
| 21 | private @Nullable CompletableFuture<List<S>> allDone;
|
| 22 | private final Set<PreparableReloadListener> preparingListeners;
|
| 23 | private final int listenerCount;
|
| 24 | private final AtomicInteger startedTasks = new AtomicInteger();
|
| 25 | private final AtomicInteger finishedTasks = new AtomicInteger();
|
| 26 | private final AtomicInteger startedReloads = new AtomicInteger();
|
| 27 | private final AtomicInteger finishedReloads = new AtomicInteger();
|
| 28 |
|
| 29 | public static ReloadInstance of(
|
| 30 | final ResourceManager resourceManager,
|
| 31 | final List<PreparableReloadListener> listeners,
|
| 32 | final Executor taskExecutor,
|
| 33 | final Executor mainThreadExecutor,
|
| 34 | final CompletableFuture<Unit> initialTask
|
| 35 | ) {
|
| 36 | SimpleReloadInstance<Void> result = new SimpleReloadInstance<>(listeners);
|
| 37 | result.startTasks(taskExecutor, mainThreadExecutor, resourceManager, listeners, SimpleReloadInstance.StateFactory.SIMPLE, initialTask);
|
| 38 | return result;
|
| 39 | }
|
| 40 |
|
| 41 | protected SimpleReloadInstance(final List<PreparableReloadListener> listeners) {
|
| 42 | this.listenerCount = listeners.size();
|
| 43 | this.preparingListeners = new HashSet<>(listeners);
|
| 44 | }
|
| 45 |
|
| 46 | protected void startTasks(
|
| 47 | final Executor taskExecutor,
|
| 48 | final Executor mainThreadExecutor,
|
| 49 | final ResourceManager resourceManager,
|
| 50 | final List<PreparableReloadListener> listeners,
|
| 51 | final SimpleReloadInstance.StateFactory<S> stateFactory,
|
| 52 | final CompletableFuture<?> initialTask
|
| 53 | ) {
|
| 54 | this.allDone = this.prepareTasks(taskExecutor, mainThreadExecutor, resourceManager, listeners, stateFactory, initialTask);
|
| 55 | }
|
| 56 |
|
| 57 | protected CompletableFuture<List<S>> prepareTasks(
|
| 58 | final Executor taskExecutor,
|
| 59 | final Executor mainThreadExecutor,
|
| 60 | final ResourceManager resourceManager,
|
| 61 | final List<PreparableReloadListener> listeners,
|
| 62 | final SimpleReloadInstance.StateFactory<S> stateFactory,
|
| 63 | final CompletableFuture<?> initialTask
|
| 64 | ) {
|
| 65 | Executor countingTaskExecutor = r -> {
|
| 66 | this.startedTasks.incrementAndGet();
|
| 67 | taskExecutor.execute(() -> {
|
| 68 | r.run();
|
| 69 | this.finishedTasks.incrementAndGet();
|
| 70 | });
|
| 71 | };
|
| 72 | Executor countingReloadExecutor = r -> {
|
| 73 | this.startedReloads.incrementAndGet();
|
| 74 | mainThreadExecutor.execute(() -> {
|
| 75 | r.run();
|
| 76 | this.finishedReloads.incrementAndGet();
|
| 77 | });
|
| 78 | };
|
| 79 | this.startedTasks.incrementAndGet();
|
| 80 | initialTask.thenRun(this.finishedTasks::incrementAndGet);
|
| 81 | PreparableReloadListener.SharedState sharedState = new PreparableReloadListener.SharedState(resourceManager);
|
| 82 | listeners.forEach(listenerx -> listenerx.prepareSharedState(sharedState));
|
| 83 | CompletableFuture<?> barrier = initialTask;
|
| 84 | List<CompletableFuture<S>> allSteps = new ArrayList<>();
|
| 85 |
|
| 86 | for (PreparableReloadListener listener : listeners) {
|
| 87 | PreparableReloadListener.PreparationBarrier barrierForCurrentTask = this.createBarrierForListener(listener, barrier, mainThreadExecutor);
|
| 88 | CompletableFuture<S> state = stateFactory.create(sharedState, barrierForCurrentTask, listener, countingTaskExecutor, countingReloadExecutor);
|
| 89 | allSteps.add(state);
|
| 90 | barrier = state;
|
| 91 | }
|
| 92 |
|
| 93 | return Util.sequenceFailFast(allSteps);
|
| 94 | }
|
| 95 |
|
| 96 | private PreparableReloadListener.PreparationBarrier createBarrierForListener(
|
| 97 | final PreparableReloadListener listener, final CompletableFuture<?> previousBarrier, final Executor mainThreadExecutor
|
| 98 | ) {
|
| 99 | return new PreparableReloadListener.PreparationBarrier() {
|
| 100 | @Override
|
| 101 | public <T> CompletableFuture<T> wait(final T t) {
|
| 102 | mainThreadExecutor.execute(() -> {
|
| 103 | SimpleReloadInstance.this.preparingListeners.remove(listener);
|
| 104 | if (SimpleReloadInstance.this.preparingListeners.isEmpty()) {
|
| 105 | SimpleReloadInstance.this.allPreparations.complete(Unit.INSTANCE);
|
| 106 | }
|
| 107 | });
|
| 108 | return SimpleReloadInstance.this.allPreparations.thenCombine((CompletionStage<? extends T>)previousBarrier, (v1, v2) -> t);
|
| 109 | }
|
| 110 | };
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public CompletableFuture<?> done() {
|
| 115 | return Objects.requireNonNull(this.allDone, "not started");
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public float getActualProgress() {
|
| 120 | int preparationsDone = this.listenerCount - this.preparingListeners.size();
|
| 121 | float doneCount = weightProgress(this.finishedTasks.get(), this.finishedReloads.get(), preparationsDone);
|
| 122 | float totalCount = weightProgress(this.startedTasks.get(), this.startedReloads.get(), this.listenerCount);
|
| 123 | return doneCount / totalCount;
|
| 124 | }
|
| 125 |
|
| 126 | private static int weightProgress(final int preparationTasks, final int reloadTasks, final int listeners) {
|
| 127 | return preparationTasks * 2 + reloadTasks * 2 + listeners * 1;
|
| 128 | }
|
| 129 |
|
| 130 | public static ReloadInstance create(
|
| 131 | final ResourceManager resourceManager,
|
| 132 | final List<PreparableReloadListener> listeners,
|
| 133 | final Executor backgroundExecutor,
|
| 134 | final Executor mainThreadExecutor,
|
| 135 | final CompletableFuture<Unit> initialTask,
|
| 136 | final boolean enableProfiling
|
| 137 | ) {
|
| 138 | return enableProfiling
|
| 139 | ? ProfiledReloadInstance.of(resourceManager, listeners, backgroundExecutor, mainThreadExecutor, initialTask)
|
| 140 | : of(resourceManager, listeners, backgroundExecutor, mainThreadExecutor, initialTask);
|
| 141 | }
|
| 142 |
|
| 143 | @FunctionalInterface
|
| 144 | protected interface StateFactory<S> {
|
| 145 | SimpleReloadInstance.StateFactory<Void> SIMPLE = (currentReload, previousStep, listener, taskExecutor, reloadExecutor) -> listener.reload(
|
| 146 | currentReload, taskExecutor, previousStep, reloadExecutor
|
| 147 | );
|
| 148 |
|
| 149 | CompletableFuture<S> create(
|
| 150 | PreparableReloadListener.SharedState sharedState,
|
| 151 | PreparableReloadListener.PreparationBarrier previousStep,
|
| 152 | PreparableReloadListener listener,
|
| 153 | Executor taskExecutor,
|
| 154 | Executor reloadExecutor
|
| 155 | );
|
| 156 | }
|
| 157 | }
|
| 158 |
|