| 1 | package net.minecraft.commands.execution;
|
| 2 |
|
| 3 | import com.google.common.collect.Queues;
|
| 4 | import com.mojang.brigadier.context.ContextChain;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 7 | import java.util.Deque;
|
| 8 | import java.util.List;
|
| 9 | import net.minecraft.commands.CommandResultCallback;
|
| 10 | import net.minecraft.commands.ExecutionCommandSource;
|
| 11 | import net.minecraft.commands.execution.tasks.BuildContexts;
|
| 12 | import net.minecraft.commands.execution.tasks.CallFunction;
|
| 13 | import net.minecraft.commands.functions.InstantiatedFunction;
|
| 14 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 | import org.slf4j.Logger;
|
| 17 |
|
| 18 | public class ExecutionContext<T> implements AutoCloseable {
|
| 19 | private static final int MAX_QUEUE_DEPTH = 10000000;
|
| 20 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 21 | private final int commandLimit;
|
| 22 | private final int forkLimit;
|
| 23 | private final ProfilerFiller profiler;
|
| 24 | private @Nullable TraceCallbacks tracer;
|
| 25 | private int commandQuota;
|
| 26 | private boolean queueOverflow;
|
| 27 | private final Deque<CommandQueueEntry<T>> commandQueue = Queues.newArrayDeque();
|
| 28 | private final List<CommandQueueEntry<T>> newTopCommands = new ObjectArrayList<>();
|
| 29 | private int currentFrameDepth;
|
| 30 |
|
| 31 | public ExecutionContext(final int commandLimit, final int forkLimit, final ProfilerFiller profiler) {
|
| 32 | this.commandLimit = commandLimit;
|
| 33 | this.forkLimit = forkLimit;
|
| 34 | this.profiler = profiler;
|
| 35 | this.commandQuota = commandLimit;
|
| 36 | }
|
| 37 |
|
| 38 | private static <T extends ExecutionCommandSource<T>> Frame createTopFrame(final ExecutionContext<T> context, final CommandResultCallback frameResult) {
|
| 39 | if (context.currentFrameDepth == 0) {
|
| 40 | return new Frame(0, frameResult, context.commandQueue::clear);
|
| 41 | }
|
| 42 |
|
| 43 | int reentrantFrameDepth = context.currentFrameDepth + 1;
|
| 44 | return new Frame(reentrantFrameDepth, frameResult, context.frameControlForDepth(reentrantFrameDepth));
|
| 45 | }
|
| 46 |
|
| 47 | public static <T extends ExecutionCommandSource<T>> void queueInitialFunctionCall(
|
| 48 | final ExecutionContext<T> context, final InstantiatedFunction<T> function, final T sender, final CommandResultCallback functionReturn
|
| 49 | ) {
|
| 50 | context.queueNext(new CommandQueueEntry<>(createTopFrame(context, functionReturn), new CallFunction<>(function, sender.callback(), false).bind(sender)));
|
| 51 | }
|
| 52 |
|
| 53 | public static <T extends ExecutionCommandSource<T>> void queueInitialCommandExecution(
|
| 54 | final ExecutionContext<T> context, final String command, final ContextChain<T> executionChain, final T sender, final CommandResultCallback commandReturn
|
| 55 | ) {
|
| 56 | context.queueNext(new CommandQueueEntry<>(createTopFrame(context, commandReturn), new BuildContexts.TopLevel<>(command, executionChain, sender)));
|
| 57 | }
|
| 58 |
|
| 59 | private void handleQueueOverflow() {
|
| 60 | this.queueOverflow = true;
|
| 61 | this.newTopCommands.clear();
|
| 62 | this.commandQueue.clear();
|
| 63 | }
|
| 64 |
|
| 65 | public void queueNext(final CommandQueueEntry<T> entry) {
|
| 66 | if (this.newTopCommands.size() + this.commandQueue.size() > 10000000) {
|
| 67 | this.handleQueueOverflow();
|
| 68 | }
|
| 69 |
|
| 70 | if (!this.queueOverflow) {
|
| 71 | this.newTopCommands.add(entry);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | public void discardAtDepthOrHigher(final int depthToDiscard) {
|
| 76 | while (!this.commandQueue.isEmpty() && this.commandQueue.peek().frame().depth() >= depthToDiscard) {
|
| 77 | this.commandQueue.removeFirst();
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | public Frame.FrameControl frameControlForDepth(final int depthToDiscard) {
|
| 82 | return () -> this.discardAtDepthOrHigher(depthToDiscard);
|
| 83 | }
|
| 84 |
|
| 85 | public void runCommandQueue() {
|
| 86 | this.pushNewCommands();
|
| 87 |
|
| 88 | while (true) {
|
| 89 | if (this.commandQuota <= 0) {
|
| 90 | LOGGER.info("Command execution stopped due to limit (executed {} commands)", this.commandLimit);
|
| 91 | break;
|
| 92 | }
|
| 93 |
|
| 94 | CommandQueueEntry<T> command = this.commandQueue.pollFirst();
|
| 95 | if (command == null) {
|
| 96 | return;
|
| 97 | }
|
| 98 |
|
| 99 | this.currentFrameDepth = command.frame().depth();
|
| 100 | command.execute(this);
|
| 101 | if (this.queueOverflow) {
|
| 102 | LOGGER.error("Command execution stopped due to command queue overflow (max {})", 10000000);
|
| 103 | break;
|
| 104 | }
|
| 105 |
|
| 106 | this.pushNewCommands();
|
| 107 | }
|
| 108 |
|
| 109 | this.currentFrameDepth = 0;
|
| 110 | }
|
| 111 |
|
| 112 | private void pushNewCommands() {
|
| 113 | for (int i = this.newTopCommands.size() - 1; i >= 0; i--) {
|
| 114 | this.commandQueue.addFirst(this.newTopCommands.get(i));
|
| 115 | }
|
| 116 |
|
| 117 | this.newTopCommands.clear();
|
| 118 | }
|
| 119 |
|
| 120 | public void tracer(final @Nullable TraceCallbacks tracer) {
|
| 121 | this.tracer = tracer;
|
| 122 | }
|
| 123 |
|
| 124 | public @Nullable TraceCallbacks tracer() {
|
| 125 | return this.tracer;
|
| 126 | }
|
| 127 |
|
| 128 | public ProfilerFiller profiler() {
|
| 129 | return this.profiler;
|
| 130 | }
|
| 131 |
|
| 132 | public int forkLimit() {
|
| 133 | return this.forkLimit;
|
| 134 | }
|
| 135 |
|
| 136 | public void incrementCost() {
|
| 137 | this.commandQuota--;
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | public void close() {
|
| 142 | if (this.tracer != null) {
|
| 143 | this.tracer.close();
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 |
|