File size: 3,970 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package net.minecraft.client;

import com.google.common.collect.ImmutableList;
import com.mojang.logging.LogUtils;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.CrashReport;
import net.minecraft.CrashReportCategory;
import net.minecraft.server.packs.PackResources;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.slf4j.Logger;

@OnlyIn(Dist.CLIENT)
public class ResourceLoadStateTracker {
    private static final Logger LOGGER = LogUtils.getLogger();
    @Nullable
    private ResourceLoadStateTracker.ReloadState reloadState;
    private int reloadCount;

    public void startReload(ResourceLoadStateTracker.ReloadReason p_168558_, List<PackResources> p_168559_) {
        this.reloadCount++;
        if (this.reloadState != null && !this.reloadState.finished) {
            LOGGER.warn("Reload already ongoing, replacing");
        }

        this.reloadState = new ResourceLoadStateTracker.ReloadState(
            p_168558_, p_168559_.stream().map(PackResources::packId).collect(ImmutableList.toImmutableList())
        );
    }

    public void startRecovery(Throwable p_168561_) {
        if (this.reloadState == null) {
            LOGGER.warn("Trying to signal reload recovery, but nothing was started");
            this.reloadState = new ResourceLoadStateTracker.ReloadState(ResourceLoadStateTracker.ReloadReason.UNKNOWN, ImmutableList.of());
        }

        this.reloadState.recoveryReloadInfo = new ResourceLoadStateTracker.RecoveryInfo(p_168561_);
    }

    public void finishReload() {
        if (this.reloadState == null) {
            LOGGER.warn("Trying to finish reload, but nothing was started");
        } else {
            this.reloadState.finished = true;
        }
    }

    public void fillCrashReport(CrashReport p_168563_) {
        CrashReportCategory crashreportcategory = p_168563_.addCategory("Last reload");
        crashreportcategory.setDetail("Reload number", this.reloadCount);
        if (this.reloadState != null) {
            this.reloadState.fillCrashInfo(crashreportcategory);
        }
    }

    @OnlyIn(Dist.CLIENT)
    static class RecoveryInfo {
        private final Throwable error;

        RecoveryInfo(Throwable p_168566_) {
            this.error = p_168566_;
        }

        public void fillCrashInfo(CrashReportCategory p_168569_) {
            p_168569_.setDetail("Recovery", "Yes");
            p_168569_.setDetail("Recovery reason", () -> {
                StringWriter stringwriter = new StringWriter();
                this.error.printStackTrace(new PrintWriter(stringwriter));
                return stringwriter.toString();
            });
        }
    }

    @OnlyIn(Dist.CLIENT)
    public static enum ReloadReason {
        INITIAL("initial"),
        MANUAL("manual"),
        UNKNOWN("unknown");

        final String name;

        private ReloadReason(final String p_168579_) {
            this.name = p_168579_;
        }
    }

    @OnlyIn(Dist.CLIENT)
    static class ReloadState {
        private final ResourceLoadStateTracker.ReloadReason reloadReason;
        private final List<String> packs;
        @Nullable
        ResourceLoadStateTracker.RecoveryInfo recoveryReloadInfo;
        boolean finished;

        ReloadState(ResourceLoadStateTracker.ReloadReason p_168589_, List<String> p_168590_) {
            this.reloadReason = p_168589_;
            this.packs = p_168590_;
        }

        public void fillCrashInfo(CrashReportCategory p_168593_) {
            p_168593_.setDetail("Reload reason", this.reloadReason.name);
            p_168593_.setDetail("Finished", this.finished ? "Yes" : "No");
            p_168593_.setDetail("Packs", () -> String.join(", ", this.packs));
            if (this.recoveryReloadInfo != null) {
                this.recoveryReloadInfo.fillCrashInfo(p_168593_);
            }
        }
    }
}