| 1 | package net.minecraft.util.filefix.fixes;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import com.mojang.datafixers.schemas.Schema;
|
| 5 | import com.mojang.serialization.Dynamic;
|
| 6 | import com.mojang.serialization.OptionalDynamic;
|
| 7 | import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
| 8 | import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
| 9 | import it.unimi.dsi.fastutil.longs.LongArrayList;
|
| 10 | import it.unimi.dsi.fastutil.longs.LongList;
|
| 11 | import it.unimi.dsi.fastutil.longs.Long2ObjectMap.Entry;
|
| 12 | import java.io.IOException;
|
| 13 | import java.util.ArrayList;
|
| 14 | import java.util.Comparator;
|
| 15 | import java.util.HashMap;
|
| 16 | import java.util.List;
|
| 17 | import java.util.Map;
|
| 18 | import java.util.Optional;
|
| 19 | import java.util.concurrent.CompletableFuture;
|
| 20 | import java.util.function.Function;
|
| 21 | import net.minecraft.core.registries.Registries;
|
| 22 | import net.minecraft.nbt.CompoundTag;
|
| 23 | import net.minecraft.nbt.NbtOps;
|
| 24 | import net.minecraft.nbt.Tag;
|
| 25 | import net.minecraft.resources.Identifier;
|
| 26 | import net.minecraft.resources.ResourceKey;
|
| 27 | import net.minecraft.server.level.ChunkMap;
|
| 28 | import net.minecraft.util.Util;
|
| 29 | import net.minecraft.util.datafix.DataFixTypes;
|
| 30 | import net.minecraft.util.datafix.fixes.References;
|
| 31 | import net.minecraft.util.filefix.CanceledFileFixException;
|
| 32 | import net.minecraft.util.filefix.FileFix;
|
| 33 | import net.minecraft.util.filefix.access.ChunkNbt;
|
| 34 | import net.minecraft.util.filefix.access.CompressedNbt;
|
| 35 | import net.minecraft.util.filefix.access.FileAccess;
|
| 36 | import net.minecraft.util.filefix.access.FileAccessProvider;
|
| 37 | import net.minecraft.util.filefix.access.FileRelation;
|
| 38 | import net.minecraft.util.filefix.access.FileResourceTypes;
|
| 39 | import net.minecraft.util.filefix.access.LevelDat;
|
| 40 | import net.minecraft.util.filefix.access.SavedDataNbt;
|
| 41 | import net.minecraft.util.worldupdate.UpgradeProgress;
|
| 42 | import net.minecraft.world.level.ChunkPos;
|
| 43 | import net.minecraft.world.level.Level;
|
| 44 | import net.minecraft.world.level.chunk.storage.RegionStorageInfo;
|
| 45 |
|
| 46 | public class LegacyStructureFileFix extends FileFix {
|
| 47 | public static final int STRUCTURE_RANGE = 8;
|
| 48 | public static final List<String> OVERWORLD_LEGACY_STRUCTURES = List.of("Monument", "Stronghold", "Mineshaft", "Temple", "Mansion");
|
| 49 | public static final Map<String, String> LEGACY_TO_CURRENT_MAP = Util.make(Maps.newHashMap(), map -> {
|
| 50 | map.put("Iglu", "Igloo");
|
| 51 | map.put("TeDP", "Desert_Pyramid");
|
| 52 | map.put("TeJP", "Jungle_Pyramid");
|
| 53 | map.put("TeSH", "Swamp_Hut");
|
| 54 | });
|
| 55 | public static final List<String> NETHER_LEGACY_STRUCTURES = List.of("Fortress");
|
| 56 | public static final List<String> END_LEGACY_STRUCTURES = List.of("EndCity");
|
| 57 | private static final ResourceKey<Level> OVERWORLD_KEY = ResourceKey.create(Registries.DIMENSION, Identifier.withDefaultNamespace("overworld"));
|
| 58 | private static final ResourceKey<Level> NETHER_KEY = ResourceKey.create(Registries.DIMENSION, Identifier.withDefaultNamespace("the_nether"));
|
| 59 | private static final ResourceKey<Level> END_KEY = ResourceKey.create(Registries.DIMENSION, Identifier.withDefaultNamespace("the_end"));
|
| 60 |
|
| 61 | public LegacyStructureFileFix(final Schema schema) {
|
| 62 | super(schema);
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public void makeFixer() {
|
| 67 | this.addFileContentFix(
|
| 68 | files -> {
|
| 69 | List<FileAccess<SavedDataNbt>> overworldStructureData = OVERWORLD_LEGACY_STRUCTURES.stream()
|
| 70 | .map(structureId -> getLegacyStructureData(files, structureId))
|
| 71 | .toList();
|
| 72 | RegionStorageInfo overworldInfo = new RegionStorageInfo("overworld", OVERWORLD_KEY, "chunk");
|
| 73 | List<FileAccess<SavedDataNbt>> netherStructureData = NETHER_LEGACY_STRUCTURES.stream()
|
| 74 | .map(structureId -> getLegacyStructureData(files, structureId))
|
| 75 | .toList();
|
| 76 | RegionStorageInfo netherInfo = new RegionStorageInfo("the_nether", NETHER_KEY, "chunk");
|
| 77 | List<FileAccess<SavedDataNbt>> endStructureData = END_LEGACY_STRUCTURES.stream().map(structureId -> getLegacyStructureData(files, structureId)).toList();
|
| 78 | RegionStorageInfo endInfo = new RegionStorageInfo("the_end", END_KEY, "chunk");
|
| 79 | FileAccess<LevelDat> levelDat = files.getFileAccess(FileResourceTypes.LEVEL_DAT, FileRelation.ORIGIN.forFile("level.dat"));
|
| 80 | FileAccess<ChunkNbt> overworldChunks = files.getFileAccess(
|
| 81 | FileResourceTypes.chunk(DataFixTypes.CHUNK, overworldInfo), FileRelation.OLD_OVERWORLD.resolve(FileRelation.REGION)
|
| 82 | );
|
| 83 | FileAccess<ChunkNbt> netherChunks = files.getFileAccess(
|
| 84 | FileResourceTypes.chunk(DataFixTypes.CHUNK, netherInfo), FileRelation.OLD_NETHER.resolve(FileRelation.REGION)
|
| 85 | );
|
| 86 | FileAccess<ChunkNbt> endChunks = files.getFileAccess(
|
| 87 | FileResourceTypes.chunk(DataFixTypes.CHUNK, endInfo), FileRelation.OLD_END.resolve(FileRelation.REGION)
|
| 88 | );
|
| 89 | return upgradeProgress -> {
|
| 90 | Optional<Dynamic<Tag>> levelData = levelDat.getOnlyFile().read();
|
| 91 | if (!levelData.isEmpty()) {
|
| 92 | upgradeProgress.setType(UpgradeProgress.Type.LEGACY_STRUCTURES);
|
| 93 | extractAndStoreLegacyStructureData(
|
| 94 | levelData.get(),
|
| 95 | List.of(
|
| 96 | new LegacyStructureFileFix.DimensionFixEntry(OVERWORLD_KEY, overworldStructureData, overworldChunks, new Long2ObjectOpenHashMap<>()),
|
| 97 | new LegacyStructureFileFix.DimensionFixEntry(NETHER_KEY, netherStructureData, netherChunks, new Long2ObjectOpenHashMap<>()),
|
| 98 | new LegacyStructureFileFix.DimensionFixEntry(END_KEY, endStructureData, endChunks, new Long2ObjectOpenHashMap<>())
|
| 99 | ),
|
| 100 | upgradeProgress
|
| 101 | );
|
| 102 | }
|
| 103 | };
|
| 104 | }
|
| 105 | );
|
| 106 | }
|
| 107 |
|
| 108 | private static void extractAndStoreLegacyStructureData(
|
| 109 | final Dynamic<Tag> levelData, final List<LegacyStructureFileFix.DimensionFixEntry> dimensionFixEntries, final UpgradeProgress upgradeProgress
|
| 110 | ) throws IOException {
|
| 111 | upgradeProgress.setStatus(UpgradeProgress.Status.COUNTING);
|
| 112 |
|
| 113 | for (LegacyStructureFileFix.DimensionFixEntry dimensionFixEntry : dimensionFixEntries) {
|
| 114 | Long2ObjectOpenHashMap<LegacyStructureFileFix.LegacyStructureData> structures = dimensionFixEntry.structures;
|
| 115 |
|
| 116 | for (FileAccess<SavedDataNbt> structureDataFileAccess : dimensionFixEntry.structureFileAccess) {
|
| 117 | SavedDataNbt targetFile = structureDataFileAccess.getOnlyFile();
|
| 118 | Optional<Dynamic<Tag>> structureData = targetFile.read();
|
| 119 | if (!structureData.isEmpty()) {
|
| 120 | extractLegacyStructureData(structureData.get(), structures);
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | upgradeProgress.addTotalFileFixOperations(structures.size());
|
| 125 | }
|
| 126 |
|
| 127 | upgradeProgress.setStatus(UpgradeProgress.Status.UPGRADING);
|
| 128 |
|
| 129 | for (LegacyStructureFileFix.DimensionFixEntry dimensionFixEntry : dimensionFixEntries) {
|
| 130 | ResourceKey<Level> dimensionKey = dimensionFixEntry.dimensionKey;
|
| 131 | ChunkNbt chunkNbt = dimensionFixEntry.chunkFileAccess.getOnlyFile();
|
| 132 | String chunkGeneratorType;
|
| 133 | if (dimensionKey == OVERWORLD_KEY) {
|
| 134 | String generatorName = levelData.get("generatorName").asString("buffet");
|
| 135 |
|
| 136 | chunkGeneratorType = switch (generatorName) {
|
| 137 | case "flat" -> "minecraft:flat";
|
| 138 | case "debug_all_block_states" -> "minecraft:debug";
|
| 139 | default -> "minecraft:noise";
|
| 140 | };
|
| 141 | } else {
|
| 142 | chunkGeneratorType = "minecraft:noise";
|
| 143 | }
|
| 144 |
|
| 145 | Optional<Identifier> generatorIdentifier = Optional.ofNullable(Identifier.tryParse(chunkGeneratorType));
|
| 146 | CompoundTag dataFixContext = ChunkMap.getChunkDataFixContextTag(dimensionKey, generatorIdentifier);
|
| 147 | storeLegacyStructureDataToChunks(dimensionFixEntry.structures, chunkNbt, dataFixContext, upgradeProgress);
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | private static FileAccess<SavedDataNbt> getLegacyStructureData(final FileAccessProvider files, final String structureId) {
|
| 152 | return files.getFileAccess(
|
| 153 | FileResourceTypes.savedData(References.SAVED_DATA_STRUCTURE_FEATURE_INDICES, CompressedNbt.MissingSeverity.MINOR),
|
| 154 | FileRelation.DATA.forFile(structureId + ".dat")
|
| 155 | );
|
| 156 | }
|
| 157 |
|
| 158 | private static void extractLegacyStructureData(
|
| 159 | final Dynamic<Tag> structureData, final Long2ObjectMap<LegacyStructureFileFix.LegacyStructureData> extractedDataContainer
|
| 160 | ) {
|
| 161 | OptionalDynamic<Tag> features = structureData.get("Features");
|
| 162 | Map<Dynamic<Tag>, Dynamic<Tag>> map = features.asMap(Function.identity(), Function.identity());
|
| 163 |
|
| 164 | for (Dynamic<Tag> value : map.values()) {
|
| 165 | long pos = ChunkPos.pack(value.get("ChunkX").asInt(0), value.get("ChunkZ").asInt(0));
|
| 166 | List<Dynamic<Tag>> childList = value.get("Children").asList(Function.identity());
|
| 167 | if (!childList.isEmpty()) {
|
| 168 | Optional<String> id = childList.getFirst().get("id").asString().result().map(LEGACY_TO_CURRENT_MAP::get);
|
| 169 | if (id.isPresent()) {
|
| 170 | value = value.set("id", value.createString(id.get()));
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | Dynamic<Tag> finalValue = value;
|
| 175 | value.get("id").asString().ifSuccess(id -> {
|
| 176 | extractedDataContainer.computeIfAbsent(pos, l -> new LegacyStructureFileFix.LegacyStructureData()).addStart(id, finalValue);
|
| 177 |
|
| 178 | for (int neighborX = ChunkPos.getX(pos) - 8; neighborX <= ChunkPos.getX(pos) + 8; neighborX++) {
|
| 179 | for (int neighborZ = ChunkPos.getZ(pos) - 8; neighborZ <= ChunkPos.getZ(pos) + 8; neighborZ++) {
|
| 180 | extractedDataContainer.computeIfAbsent(ChunkPos.pack(neighborX, neighborZ), l -> new LegacyStructureFileFix.LegacyStructureData()).addIndex(id, pos);
|
| 181 | }
|
| 182 | }
|
| 183 | });
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | private static void storeLegacyStructureDataToChunks(
|
| 188 | final Long2ObjectMap<LegacyStructureFileFix.LegacyStructureData> structures,
|
| 189 | final ChunkNbt chunksAccess,
|
| 190 | final CompoundTag dataFixContext,
|
| 191 | final UpgradeProgress upgradeProgress
|
| 192 | ) {
|
| 193 | List<Entry<LegacyStructureFileFix.LegacyStructureData>> entries = structures.long2ObjectEntrySet()
|
| 194 | .stream()
|
| 195 | .sorted(Comparator.comparingLong(entryx -> ChunkPos.pack(ChunkPos.getRegionX(entryx.getLongKey()), ChunkPos.getRegionZ(entryx.getLongKey()))))
|
| 196 | .toList();
|
| 197 | LegacyStructureFileFix.IncrementalFutureSequence futures = new LegacyStructureFileFix.IncrementalFutureSequence(8);
|
| 198 |
|
| 199 | for (Entry<LegacyStructureFileFix.LegacyStructureData> entry : entries) {
|
| 200 | if (upgradeProgress.isCanceled()) {
|
| 201 | throw new CanceledFileFixException();
|
| 202 | }
|
| 203 |
|
| 204 | long pos = entry.getLongKey();
|
| 205 | LegacyStructureFileFix.LegacyStructureData legacyData = entry.getValue();
|
| 206 | int finished = futures.push(chunksAccess.updateChunk(ChunkPos.unpack(pos), dataFixContext, tag -> {
|
| 207 | CompoundTag levelTag = tag.getCompoundOrEmpty("Level");
|
| 208 | CompoundTag structureTag = levelTag.getCompoundOrEmpty("Structures");
|
| 209 | CompoundTag startTag = structureTag.getCompoundOrEmpty("Starts");
|
| 210 | CompoundTag referencesTag = structureTag.getCompoundOrEmpty("References");
|
| 211 | legacyData.starts().forEach((id, value) -> startTag.put(id, value.convert(NbtOps.INSTANCE).getValue()));
|
| 212 | legacyData.indexes().forEach((id, indexes) -> referencesTag.putLongArray(id, indexes.toLongArray()));
|
| 213 | structureTag.put("Starts", startTag);
|
| 214 | structureTag.put("References", referencesTag);
|
| 215 | levelTag.put("Structures", structureTag);
|
| 216 | tag.put("Level", levelTag);
|
| 217 | return tag;
|
| 218 | }));
|
| 219 | upgradeProgress.incrementFinishedOperationsBy(finished);
|
| 220 | }
|
| 221 |
|
| 222 | upgradeProgress.incrementFinishedOperationsBy(futures.waitForAll());
|
| 223 | }
|
| 224 |
|
| 225 | private record DimensionFixEntry(
|
| 226 | ResourceKey<Level> dimensionKey,
|
| 227 | List<FileAccess<SavedDataNbt>> structureFileAccess,
|
| 228 | FileAccess<ChunkNbt> chunkFileAccess,
|
| 229 | Long2ObjectOpenHashMap<LegacyStructureFileFix.LegacyStructureData> structures
|
| 230 | ) {
|
| 231 | }
|
| 232 |
|
| 233 | private static class IncrementalFutureSequence {
|
| 234 | private final int maxConcurrency;
|
| 235 | private final List<CompletableFuture<?>> futures;
|
| 236 |
|
| 237 | public IncrementalFutureSequence(final int maxConcurrency) {
|
| 238 | this.maxConcurrency = maxConcurrency;
|
| 239 | this.futures = new ArrayList<>(maxConcurrency);
|
| 240 | }
|
| 241 |
|
| 242 | public int push(final CompletableFuture<?> future) {
|
| 243 | int finished = 0;
|
| 244 | if (this.futures.size() >= this.maxConcurrency) {
|
| 245 | finished += this.waitOnAny();
|
| 246 | }
|
| 247 |
|
| 248 | this.futures.add(future);
|
| 249 | return finished;
|
| 250 | }
|
| 251 |
|
| 252 | private int waitOnAny() {
|
| 253 | int oldSize = this.futures.size();
|
| 254 | CompletableFuture.anyOf(this.futures.toArray(CompletableFuture[]::new)).join();
|
| 255 | this.futures.removeIf(CompletableFuture::isDone);
|
| 256 | return oldSize - this.futures.size();
|
| 257 | }
|
| 258 |
|
| 259 | public int waitForAll() {
|
| 260 | int oldSize = this.futures.size();
|
| 261 | CompletableFuture.allOf(this.futures.toArray(CompletableFuture[]::new)).join();
|
| 262 | this.futures.clear();
|
| 263 | return oldSize;
|
| 264 | }
|
| 265 | }
|
| 266 |
|
| 267 | public record LegacyStructureData(Map<String, Dynamic<?>> starts, Map<String, LongList> indexes) {
|
| 268 | public LegacyStructureData() {
|
| 269 | this(new HashMap<>(), new HashMap<>());
|
| 270 | }
|
| 271 |
|
| 272 | public void addStart(final String id, final Dynamic<Tag> data) {
|
| 273 | this.starts.put(id, data);
|
| 274 | }
|
| 275 |
|
| 276 | public void addIndex(final String id, final long sourcePos) {
|
| 277 | this.indexes.computeIfAbsent(id, l -> new LongArrayList()).add(sourcePos);
|
| 278 | }
|
| 279 | }
|
| 280 | }
|
| 281 |
|