| 1 | package net.minecraft.client;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import java.io.PrintWriter;
|
| 6 | import java.io.StringWriter;
|
| 7 | import java.util.List;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.CrashReport;
|
| 11 | import net.minecraft.CrashReportCategory;
|
| 12 | import net.minecraft.server.packs.PackResources;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 | import org.slf4j.Logger;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public class ResourceLoadStateTracker {
|
| 18 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 19 | private ResourceLoadStateTracker.@Nullable ReloadState reloadState;
|
| 20 | private int reloadCount;
|
| 21 |
|
| 22 | public void startReload(final ResourceLoadStateTracker.ReloadReason reloadReason, final List<PackResources> packs) {
|
| 23 | this.reloadCount++;
|
| 24 | if (this.reloadState != null && !this.reloadState.finished) {
|
| 25 | LOGGER.warn("Reload already ongoing, replacing");
|
| 26 | }
|
| 27 |
|
| 28 | this.reloadState = new ResourceLoadStateTracker.ReloadState(reloadReason, packs.stream().map(PackResources::packId).collect(ImmutableList.toImmutableList()));
|
| 29 | }
|
| 30 |
|
| 31 | public void startRecovery(final Throwable reason) {
|
| 32 | if (this.reloadState == null) {
|
| 33 | LOGGER.warn("Trying to signal reload recovery, but nothing was started");
|
| 34 | this.reloadState = new ResourceLoadStateTracker.ReloadState(ResourceLoadStateTracker.ReloadReason.UNKNOWN, ImmutableList.of());
|
| 35 | }
|
| 36 |
|
| 37 | this.reloadState.recoveryReloadInfo = new ResourceLoadStateTracker.RecoveryInfo(reason);
|
| 38 | }
|
| 39 |
|
| 40 | public void finishReload() {
|
| 41 | if (this.reloadState == null) {
|
| 42 | LOGGER.warn("Trying to finish reload, but nothing was started");
|
| 43 | } else {
|
| 44 | this.reloadState.finished = true;
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | public void fillCrashReport(final CrashReport report) {
|
| 49 | CrashReportCategory category = report.addCategory("Last reload");
|
| 50 | category.setDetail("Reload number", this.reloadCount);
|
| 51 | if (this.reloadState != null) {
|
| 52 | this.reloadState.fillCrashInfo(category);
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | @Environment(EnvType.CLIENT)
|
| 57 | private static class RecoveryInfo {
|
| 58 | private final Throwable error;
|
| 59 |
|
| 60 | private RecoveryInfo(final Throwable error) {
|
| 61 | this.error = error;
|
| 62 | }
|
| 63 |
|
| 64 | public void fillCrashInfo(final CrashReportCategory category) {
|
| 65 | category.setDetail("Recovery", "Yes");
|
| 66 | category.setDetail("Recovery reason", () -> {
|
| 67 | StringWriter writer = new StringWriter();
|
| 68 | this.error.printStackTrace(new PrintWriter(writer));
|
| 69 | return writer.toString();
|
| 70 | });
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | @Environment(EnvType.CLIENT)
|
| 75 | public enum ReloadReason {
|
| 76 | INITIAL("initial"),
|
| 77 | MANUAL("manual"),
|
| 78 | UNKNOWN("unknown");
|
| 79 |
|
| 80 | private final String name;
|
| 81 |
|
| 82 | ReloadReason(final String name) {
|
| 83 | this.name = name;
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | @Environment(EnvType.CLIENT)
|
| 88 | private static class ReloadState {
|
| 89 | private final ResourceLoadStateTracker.ReloadReason reloadReason;
|
| 90 | private final List<String> packs;
|
| 91 | private ResourceLoadStateTracker.@Nullable RecoveryInfo recoveryReloadInfo;
|
| 92 | private boolean finished;
|
| 93 |
|
| 94 | private ReloadState(final ResourceLoadStateTracker.ReloadReason reloadReason, final List<String> packs) {
|
| 95 | this.reloadReason = reloadReason;
|
| 96 | this.packs = packs;
|
| 97 | }
|
| 98 |
|
| 99 | public void fillCrashInfo(final CrashReportCategory category) {
|
| 100 | category.setDetail("Reload reason", this.reloadReason.name);
|
| 101 | category.setDetail("Finished", this.finished ? "Yes" : "No");
|
| 102 | category.setDetail("Packs", () -> String.join(", ", this.packs));
|
| 103 | if (this.recoveryReloadInfo != null) {
|
| 104 | this.recoveryReloadInfo.fillCrashInfo(category);
|
| 105 | }
|
| 106 | }
|
| 107 | }
|
| 108 | }
|
| 109 |
|