| 1 | package net.minecraft.util.worldupdate;
|
| 2 |
|
| 3 | import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
| 4 | import com.mojang.datafixers.DataFixer;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.util.ArrayList;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.Set;
|
| 10 | import java.util.concurrent.ThreadFactory;
|
| 11 | import java.util.function.BiFunction;
|
| 12 | import java.util.stream.Collectors;
|
| 13 | import net.minecraft.SharedConstants;
|
| 14 | import net.minecraft.core.Registry;
|
| 15 | import net.minecraft.core.RegistryAccess;
|
| 16 | import net.minecraft.core.registries.Registries;
|
| 17 | import net.minecraft.nbt.CompoundTag;
|
| 18 | import net.minecraft.nbt.ListTag;
|
| 19 | import net.minecraft.network.chat.Component;
|
| 20 | import net.minecraft.resources.ResourceKey;
|
| 21 | import net.minecraft.server.level.ChunkMap;
|
| 22 | import net.minecraft.util.Util;
|
| 23 | import net.minecraft.util.datafix.DataFixTypes;
|
| 24 | import net.minecraft.world.level.ChunkPos;
|
| 25 | import net.minecraft.world.level.Level;
|
| 26 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 27 | import net.minecraft.world.level.dimension.LevelStem;
|
| 28 | import net.minecraft.world.level.storage.LevelStorageSource;
|
| 29 | import net.minecraft.world.level.storage.SavedDataStorage;
|
| 30 | import org.slf4j.Logger;
|
| 31 |
|
| 32 | public class WorldUpgrader implements AutoCloseable {
|
| 33 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 34 | private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat("World Upgrader #%d").setDaemon(true).build();
|
| 35 | private final UpgradeStatusTranslator statusTranslator = new UpgradeStatusTranslator();
|
| 36 | private final Registry<LevelStem> dimensions;
|
| 37 | private final Set<ResourceKey<Level>> levels;
|
| 38 | private final boolean eraseCache;
|
| 39 | private final boolean recreateRegionFiles;
|
| 40 | private final LevelStorageSource.LevelStorageAccess levelStorage;
|
| 41 | private final Thread thread;
|
| 42 | private final DataFixer dataFixer;
|
| 43 | private final UpgradeProgress upgradeProgress = new UpgradeProgress();
|
| 44 | private final SavedDataStorage overworldSavedDataStorage;
|
| 45 |
|
| 46 | public WorldUpgrader(
|
| 47 | final LevelStorageSource.LevelStorageAccess levelSource,
|
| 48 | final DataFixer dataFixer,
|
| 49 | final RegistryAccess registryAccess,
|
| 50 | final boolean eraseCache,
|
| 51 | final boolean recreateRegionFiles
|
| 52 | ) {
|
| 53 | this.dimensions = registryAccess.lookupOrThrow(Registries.LEVEL_STEM);
|
| 54 | this.levels = this.dimensions.registryKeySet().stream().map(Registries::levelStemToLevel).collect(Collectors.toUnmodifiableSet());
|
| 55 | this.eraseCache = eraseCache;
|
| 56 | this.dataFixer = dataFixer;
|
| 57 | this.levelStorage = levelSource;
|
| 58 | this.overworldSavedDataStorage = new SavedDataStorage(this.levelStorage.getDimensionPath(Level.OVERWORLD).resolve("data"), dataFixer, registryAccess);
|
| 59 | this.recreateRegionFiles = recreateRegionFiles;
|
| 60 | this.thread = THREAD_FACTORY.newThread(this::work);
|
| 61 | this.thread.setUncaughtExceptionHandler((t, e) -> {
|
| 62 | LOGGER.error("Error upgrading world", e);
|
| 63 | this.upgradeProgress.setStatus(UpgradeProgress.Status.FAILED);
|
| 64 | this.upgradeProgress.setFinished(true);
|
| 65 | });
|
| 66 | this.thread.start();
|
| 67 | }
|
| 68 |
|
| 69 | public static CompoundTag getDataFixContextTag(final Registry<LevelStem> dimensions, final ResourceKey<Level> dimension) {
|
| 70 | ChunkGenerator generator = dimensions.getValueOrThrow(Registries.levelToLevelStem(dimension)).generator();
|
| 71 | return ChunkMap.getChunkDataFixContextTag(dimension, generator.getTypeNameForDataFixer());
|
| 72 | }
|
| 73 |
|
| 74 | public static boolean verifyChunkPosAndEraseCache(final ChunkPos pos, final CompoundTag upgradedTag) {
|
| 75 | verifyChunkPos(pos, upgradedTag);
|
| 76 | boolean changed = upgradedTag.contains("Heightmaps");
|
| 77 | upgradedTag.remove("Heightmaps");
|
| 78 | changed = changed || upgradedTag.contains("isLightOn");
|
| 79 | upgradedTag.remove("isLightOn");
|
| 80 | ListTag sections = upgradedTag.getListOrEmpty("sections");
|
| 81 |
|
| 82 | for (int i = 0; i < sections.size(); i++) {
|
| 83 | Optional<CompoundTag> maybeSection = sections.getCompound(i);
|
| 84 | if (!maybeSection.isEmpty()) {
|
| 85 | CompoundTag section = maybeSection.get();
|
| 86 | changed = changed || section.contains("BlockLight");
|
| 87 | section.remove("BlockLight");
|
| 88 | changed = changed || section.contains("SkyLight");
|
| 89 | section.remove("SkyLight");
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | return changed;
|
| 94 | }
|
| 95 |
|
| 96 | public static boolean verifyChunkPos(final ChunkPos pos, final CompoundTag upgradedTag) {
|
| 97 | ChunkPos storedPos = new ChunkPos(upgradedTag.getIntOr("xPos", 0), upgradedTag.getIntOr("zPos", 0));
|
| 98 | if (!storedPos.equals(pos)) {
|
| 99 | LOGGER.warn("Chunk {} has invalid position {}", pos, storedPos);
|
| 100 | }
|
| 101 |
|
| 102 | return false;
|
| 103 | }
|
| 104 |
|
| 105 | public void cancel() {
|
| 106 | this.upgradeProgress.setCanceled();
|
| 107 |
|
| 108 | try {
|
| 109 | this.thread.join();
|
| 110 | } catch (InterruptedException var2) {
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | private void work() {
|
| 115 | long conversionTime = Util.getMillis();
|
| 116 | int currentVersion = SharedConstants.getCurrentVersion().dataVersion().version();
|
| 117 | LOGGER.info("Upgrading entities");
|
| 118 | this.upgradeLevels(
|
| 119 | DataFixTypes.ENTITY_CHUNK,
|
| 120 | new RegionStorageUpgrader.Builder(this.dataFixer)
|
| 121 | .setTypeAndFolderName("entities")
|
| 122 | .setRecreateRegionFiles(this.recreateRegionFiles)
|
| 123 | .trackProgress(this.upgradeProgress)
|
| 124 | );
|
| 125 | LOGGER.info("Upgrading POIs");
|
| 126 | this.upgradeLevels(
|
| 127 | DataFixTypes.POI_CHUNK,
|
| 128 | new RegionStorageUpgrader.Builder(this.dataFixer)
|
| 129 | .setTypeAndFolderName("poi")
|
| 130 | .setDefaultVersion(1945)
|
| 131 | .setRecreateRegionFiles(this.recreateRegionFiles)
|
| 132 | .trackProgress(this.upgradeProgress)
|
| 133 | );
|
| 134 | LOGGER.info("Upgrading blocks");
|
| 135 | this.upgradeLevels(
|
| 136 | DataFixTypes.CHUNK,
|
| 137 | new RegionStorageUpgrader.Builder(this.dataFixer)
|
| 138 | .setType("chunk")
|
| 139 | .setFolderName("region")
|
| 140 | .setRecreateRegionFiles(this.recreateRegionFiles)
|
| 141 | .trackProgress(this.upgradeProgress),
|
| 142 | (levelSpecificBuilder, level) -> levelSpecificBuilder.setDataFixContextTag(getDataFixContextTag(this.dimensions, level))
|
| 143 | .addTagModifier(currentVersion, this.eraseCache ? WorldUpgrader::verifyChunkPosAndEraseCache : WorldUpgrader::verifyChunkPos)
|
| 144 | );
|
| 145 | this.overworldSavedDataStorage.saveAndJoin();
|
| 146 | conversionTime = Util.getMillis() - conversionTime;
|
| 147 | LOGGER.info("World optimization finished after {} seconds", conversionTime / 1000L);
|
| 148 | this.upgradeProgress.setFinished(true);
|
| 149 | }
|
| 150 |
|
| 151 | private void upgradeLevels(final DataFixTypes dataFixType, final RegionStorageUpgrader.Builder builder) {
|
| 152 | this.upgradeLevels(dataFixType, builder, (levelSpecificBuilder, level) -> levelSpecificBuilder);
|
| 153 | }
|
| 154 |
|
| 155 | private void upgradeLevels(
|
| 156 | final DataFixTypes dataFixType,
|
| 157 | final RegionStorageUpgrader.Builder builder,
|
| 158 | final BiFunction<RegionStorageUpgrader.Builder, ResourceKey<Level>, RegionStorageUpgrader.Builder> levelSpecificBuilder
|
| 159 | ) {
|
| 160 | List<RegionStorageUpgrader> upgraders = new ArrayList<>();
|
| 161 | this.upgradeProgress.reset(dataFixType);
|
| 162 | this.upgradeProgress.setType(UpgradeProgress.Type.REGIONS);
|
| 163 | builder.setDataFixType(dataFixType);
|
| 164 | int previousCopiesFileAmounts = 0;
|
| 165 |
|
| 166 | for (ResourceKey<Level> level : this.levels) {
|
| 167 | RegionStorageUpgrader upgrader = levelSpecificBuilder.apply(builder.copy(), level).build(previousCopiesFileAmounts);
|
| 168 | upgrader.init(level, this.levelStorage);
|
| 169 | previousCopiesFileAmounts += upgrader.fileAmount();
|
| 170 | upgraders.add(upgrader);
|
| 171 | }
|
| 172 |
|
| 173 | upgraders.forEach(RegionStorageUpgrader::upgrade);
|
| 174 | }
|
| 175 |
|
| 176 | public boolean isFinished() {
|
| 177 | return this.upgradeProgress.isFinished();
|
| 178 | }
|
| 179 |
|
| 180 | public Set<ResourceKey<Level>> levels() {
|
| 181 | return this.levels;
|
| 182 | }
|
| 183 |
|
| 184 | public float dimensionProgress(final ResourceKey<Level> dimension) {
|
| 185 | return this.upgradeProgress.getDimensionProgress(dimension);
|
| 186 | }
|
| 187 |
|
| 188 | public float getTotalProgress() {
|
| 189 | return this.upgradeProgress.getTotalProgress();
|
| 190 | }
|
| 191 |
|
| 192 | public int getTotalChunks() {
|
| 193 | return this.upgradeProgress.getTotalChunks();
|
| 194 | }
|
| 195 |
|
| 196 | public int getConverted() {
|
| 197 | return this.upgradeProgress.getConverted();
|
| 198 | }
|
| 199 |
|
| 200 | public int getSkipped() {
|
| 201 | return this.upgradeProgress.getSkipped();
|
| 202 | }
|
| 203 |
|
| 204 | public Component getStatus() {
|
| 205 | return this.statusTranslator.translate(this.upgradeProgress);
|
| 206 | }
|
| 207 |
|
| 208 | @Override
|
| 209 | public void close() {
|
| 210 | this.overworldSavedDataStorage.close();
|
| 211 | }
|
| 212 | }
|
| 213 |
|