| 1 | package net.minecraft.client.gui.components.debug;
|
| 2 |
|
| 3 | import com.google.gson.JsonElement;
|
| 4 | import com.google.gson.JsonSyntaxException;
|
| 5 | import com.mojang.datafixers.DataFixer;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import com.mojang.serialization.Codec;
|
| 8 | import com.mojang.serialization.Dynamic;
|
| 9 | import com.mojang.serialization.JsonOps;
|
| 10 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 11 | import java.io.File;
|
| 12 | import java.io.IOException;
|
| 13 | import java.nio.charset.StandardCharsets;
|
| 14 | import java.util.ArrayList;
|
| 15 | import java.util.Collection;
|
| 16 | import java.util.Comparator;
|
| 17 | import java.util.HashMap;
|
| 18 | import java.util.List;
|
| 19 | import java.util.Map;
|
| 20 | import java.util.Optional;
|
| 21 | import net.fabricmc.api.EnvType;
|
| 22 | import net.fabricmc.api.Environment;
|
| 23 | import net.minecraft.client.Minecraft;
|
| 24 | import net.minecraft.resources.Identifier;
|
| 25 | import net.minecraft.util.StrictJsonParser;
|
| 26 | import net.minecraft.util.datafix.DataFixTypes;
|
| 27 | import org.apache.commons.io.FileUtils;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 | import org.slf4j.Logger;
|
| 30 |
|
| 31 | @Environment(EnvType.CLIENT)
|
| 32 | public class DebugScreenEntryList {
|
| 33 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 34 | private static final int DEFAULT_DEBUG_PROFILE_VERSION = 4649;
|
| 35 | private final Map<Identifier, DebugScreenEntryStatus> allStatuses = new HashMap<>();
|
| 36 | private final List<Identifier> currentlyEnabled = new ArrayList<>();
|
| 37 | private boolean isOverlayVisible = false;
|
| 38 | private @Nullable DebugScreenProfile profile;
|
| 39 | private final File debugProfileFile;
|
| 40 | private long currentlyEnabledVersion;
|
| 41 | private final Codec<DebugScreenEntryList.SerializedOptions> codec;
|
| 42 |
|
| 43 | public DebugScreenEntryList(final File workingDirectory, final DataFixer dataFixer) {
|
| 44 | this.debugProfileFile = new File(workingDirectory, "debug-profile.json");
|
| 45 | this.codec = DataFixTypes.DEBUG_PROFILE.wrapCodec(DebugScreenEntryList.SerializedOptions.CODEC, dataFixer, 4649);
|
| 46 | this.load();
|
| 47 | }
|
| 48 |
|
| 49 | public void load() {
|
| 50 | try {
|
| 51 | if (!this.debugProfileFile.isFile()) {
|
| 52 | this.resetToProfile(DebugScreenProfile.DEFAULT);
|
| 53 | this.rebuildCurrentList();
|
| 54 | return;
|
| 55 | }
|
| 56 |
|
| 57 | Dynamic<JsonElement> data = new Dynamic<>(
|
| 58 | JsonOps.INSTANCE, StrictJsonParser.parse(FileUtils.readFileToString(this.debugProfileFile, StandardCharsets.UTF_8))
|
| 59 | );
|
| 60 | DebugScreenEntryList.SerializedOptions serializedOptions = this.codec
|
| 61 | .parse(data)
|
| 62 | .getOrThrow(error -> new IOException("Could not parse debug profile JSON: " + error));
|
| 63 | if (serializedOptions.profile().isPresent()) {
|
| 64 | this.resetToProfile(serializedOptions.profile().get());
|
| 65 | } else {
|
| 66 | this.resetStatuses(serializedOptions.custom().orElse(Map.of()));
|
| 67 | this.profile = null;
|
| 68 | }
|
| 69 | } catch (IOException | JsonSyntaxException e) {
|
| 70 | LOGGER.error("Couldn't read debug profile file {}, resetting to default", this.debugProfileFile, e);
|
| 71 | this.resetToProfile(DebugScreenProfile.DEFAULT);
|
| 72 | this.save();
|
| 73 | }
|
| 74 |
|
| 75 | this.rebuildCurrentList();
|
| 76 | }
|
| 77 |
|
| 78 | private void resetStatuses(final Map<Identifier, DebugScreenEntryStatus> newEntries) {
|
| 79 | this.allStatuses.clear();
|
| 80 | this.allStatuses.putAll(newEntries);
|
| 81 | }
|
| 82 |
|
| 83 | private void resetToProfile(final DebugScreenProfile profile) {
|
| 84 | this.profile = profile;
|
| 85 | this.resetStatuses(DebugScreenEntries.PROFILES.get(profile));
|
| 86 | }
|
| 87 |
|
| 88 | public void loadProfile(final DebugScreenProfile profile) {
|
| 89 | this.resetToProfile(profile);
|
| 90 | this.rebuildCurrentList();
|
| 91 | }
|
| 92 |
|
| 93 | public DebugScreenEntryStatus getStatus(final Identifier location) {
|
| 94 | return this.allStatuses.getOrDefault(location, DebugScreenEntryStatus.NEVER);
|
| 95 | }
|
| 96 |
|
| 97 | public boolean isCurrentlyEnabled(final Identifier location) {
|
| 98 | return this.currentlyEnabled.contains(location);
|
| 99 | }
|
| 100 |
|
| 101 | public void setStatus(final Identifier location, final DebugScreenEntryStatus status) {
|
| 102 | this.profile = null;
|
| 103 | this.allStatuses.put(location, status);
|
| 104 | this.rebuildCurrentList();
|
| 105 | this.save();
|
| 106 | }
|
| 107 |
|
| 108 | public boolean toggleStatus(final Identifier location) {
|
| 109 | DebugScreenEntryStatus status = this.allStatuses.get(location);
|
| 110 | switch (status) {
|
| 111 | case ALWAYS_ON:
|
| 112 | this.setStatus(location, DebugScreenEntryStatus.NEVER);
|
| 113 | return false;
|
| 114 | case IN_OVERLAY:
|
| 115 | if (this.isOverlayVisible) {
|
| 116 | this.setStatus(location, DebugScreenEntryStatus.NEVER);
|
| 117 | return false;
|
| 118 | }
|
| 119 |
|
| 120 | this.setStatus(location, DebugScreenEntryStatus.ALWAYS_ON);
|
| 121 | return true;
|
| 122 | case NEVER:
|
| 123 | if (this.isOverlayVisible) {
|
| 124 | this.setStatus(location, DebugScreenEntryStatus.IN_OVERLAY);
|
| 125 | } else {
|
| 126 | this.setStatus(location, DebugScreenEntryStatus.ALWAYS_ON);
|
| 127 | }
|
| 128 |
|
| 129 | return true;
|
| 130 | case null:
|
| 131 | default:
|
| 132 | this.setStatus(location, DebugScreenEntryStatus.ALWAYS_ON);
|
| 133 | return true;
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | public Collection<Identifier> getCurrentlyEnabled() {
|
| 138 | return List.copyOf(this.currentlyEnabled);
|
| 139 | }
|
| 140 |
|
| 141 | public void toggleDebugOverlay() {
|
| 142 | this.setOverlayVisible(!this.isOverlayVisible);
|
| 143 | }
|
| 144 |
|
| 145 | public void setOverlayVisible(final boolean visible) {
|
| 146 | if (this.isOverlayVisible != visible) {
|
| 147 | this.isOverlayVisible = visible;
|
| 148 | this.rebuildCurrentList();
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | public boolean isOverlayVisible() {
|
| 153 | return this.isOverlayVisible;
|
| 154 | }
|
| 155 |
|
| 156 | public void rebuildCurrentList() {
|
| 157 | this.currentlyEnabled.clear();
|
| 158 | boolean isReducedDebugInfo = Minecraft.getInstance().showOnlyReducedInfo();
|
| 159 | this.allStatuses.forEach((key, value) -> {
|
| 160 | if (value == DebugScreenEntryStatus.ALWAYS_ON || this.isOverlayVisible && value == DebugScreenEntryStatus.IN_OVERLAY) {
|
| 161 | DebugScreenEntry debug = DebugScreenEntries.getEntry(key);
|
| 162 | if (debug != null && debug.isAllowed(isReducedDebugInfo)) {
|
| 163 | this.currentlyEnabled.add(key);
|
| 164 | }
|
| 165 | }
|
| 166 | });
|
| 167 | this.currentlyEnabled.sort(Comparator.naturalOrder());
|
| 168 | this.currentlyEnabledVersion++;
|
| 169 | }
|
| 170 |
|
| 171 | public long getCurrentlyEnabledVersion() {
|
| 172 | return this.currentlyEnabledVersion;
|
| 173 | }
|
| 174 |
|
| 175 | public boolean isUsingProfile(final DebugScreenProfile profile) {
|
| 176 | return this.profile == profile;
|
| 177 | }
|
| 178 |
|
| 179 | public void save() {
|
| 180 | DebugScreenEntryList.SerializedOptions serializedOptions = new DebugScreenEntryList.SerializedOptions(
|
| 181 | Optional.ofNullable(this.profile), this.profile == null ? Optional.of(this.allStatuses) : Optional.empty()
|
| 182 | );
|
| 183 |
|
| 184 | try {
|
| 185 | FileUtils.writeStringToFile(
|
| 186 | this.debugProfileFile, this.codec.encodeStart(JsonOps.INSTANCE, serializedOptions).getOrThrow().toString(), StandardCharsets.UTF_8
|
| 187 | );
|
| 188 | } catch (IOException e) {
|
| 189 | LOGGER.error("Failed to save debug profile file {}", this.debugProfileFile, e);
|
| 190 | }
|
| 191 | }
|
| 192 |
|
| 193 | @Environment(EnvType.CLIENT)
|
| 194 | private record SerializedOptions(Optional<DebugScreenProfile> profile, Optional<Map<Identifier, DebugScreenEntryStatus>> custom) {
|
| 195 | private static final Codec<Map<Identifier, DebugScreenEntryStatus>> CUSTOM_ENTRIES_CODEC = Codec.unboundedMap(Identifier.CODEC, DebugScreenEntryStatus.CODEC);
|
| 196 | public static final Codec<DebugScreenEntryList.SerializedOptions> CODEC = RecordCodecBuilder.create(
|
| 197 | i -> i.group(
|
| 198 | DebugScreenProfile.CODEC.optionalFieldOf("profile").forGetter(DebugScreenEntryList.SerializedOptions::profile),
|
| 199 | CUSTOM_ENTRIES_CODEC.optionalFieldOf("custom").forGetter(DebugScreenEntryList.SerializedOptions::custom)
|
| 200 | )
|
| 201 | .apply(i, DebugScreenEntryList.SerializedOptions::new)
|
| 202 | );
|
| 203 | }
|
| 204 | }
|
| 205 |
|