| 1 | package net.minecraft.data;
|
| 2 |
|
| 3 | import com.google.common.base.Stopwatch;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import java.io.IOException;
|
| 6 | import java.nio.file.Path;
|
| 7 | import java.util.HashSet;
|
| 8 | import java.util.LinkedHashMap;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.Set;
|
| 11 | import java.util.concurrent.TimeUnit;
|
| 12 | import net.minecraft.WorldVersion;
|
| 13 | import net.minecraft.server.Bootstrap;
|
| 14 | import org.slf4j.Logger;
|
| 15 |
|
| 16 | public abstract class DataGenerator {
|
| 17 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 18 | protected final PackOutput vanillaPackOutput;
|
| 19 | protected final Set<String> allProviderIds = new HashSet<>();
|
| 20 | protected final Map<String, DataProvider> providersToRun = new LinkedHashMap<>();
|
| 21 |
|
| 22 | public DataGenerator(final Path output) {
|
| 23 | this.vanillaPackOutput = new PackOutput(output);
|
| 24 | }
|
| 25 |
|
| 26 | public abstract void run() throws IOException;
|
| 27 |
|
| 28 | public DataGenerator.PackGenerator getVanillaPack(final boolean toRun) {
|
| 29 | return new DataGenerator.PackGenerator(toRun, "vanilla", this.vanillaPackOutput);
|
| 30 | }
|
| 31 |
|
| 32 | public DataGenerator.PackGenerator getBuiltinDatapack(final boolean toRun, final String packId) {
|
| 33 | Path packOutputDir = this.vanillaPackOutput.getOutputFolder(PackOutput.Target.DATA_PACK).resolve("minecraft").resolve("datapacks").resolve(packId);
|
| 34 | return new DataGenerator.PackGenerator(toRun, packId, new PackOutput(packOutputDir));
|
| 35 | }
|
| 36 |
|
| 37 | static {
|
| 38 | Bootstrap.bootStrap();
|
| 39 | }
|
| 40 |
|
| 41 | public static class Cached extends DataGenerator {
|
| 42 | private final Path rootOutputFolder;
|
| 43 | private final WorldVersion version;
|
| 44 | private final boolean alwaysGenerate;
|
| 45 |
|
| 46 | public Cached(final Path output, final WorldVersion version, final boolean alwaysGenerate) {
|
| 47 | super(output);
|
| 48 | this.rootOutputFolder = output;
|
| 49 | this.alwaysGenerate = alwaysGenerate;
|
| 50 | this.version = version;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public void run() throws IOException {
|
| 55 | HashCache cache = new HashCache(this.rootOutputFolder, this.allProviderIds, this.version);
|
| 56 | Stopwatch totalTime = Stopwatch.createStarted();
|
| 57 | Stopwatch stopwatch = Stopwatch.createUnstarted();
|
| 58 | this.providersToRun.forEach((providerId, provider) -> {
|
| 59 | if (!this.alwaysGenerate && !cache.shouldRunInThisVersion(providerId)) {
|
| 60 | DataGenerator.LOGGER.debug("Generator {} already run for version {}", providerId, this.version.name());
|
| 61 | } else {
|
| 62 | DataGenerator.LOGGER.info("Starting provider: {}", providerId);
|
| 63 | stopwatch.start();
|
| 64 | cache.applyUpdate(cache.generateUpdate(providerId, provider::run).join());
|
| 65 | stopwatch.stop();
|
| 66 | DataGenerator.LOGGER.info("{} finished after {} ms", providerId, stopwatch.elapsed(TimeUnit.MILLISECONDS));
|
| 67 | stopwatch.reset();
|
| 68 | }
|
| 69 | });
|
| 70 | DataGenerator.LOGGER.info("All providers took: {} ms", totalTime.elapsed(TimeUnit.MILLISECONDS));
|
| 71 | cache.purgeStaleAndWrite();
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | public class PackGenerator {
|
| 76 | private final boolean toRun;
|
| 77 | private final String providerPrefix;
|
| 78 | private final PackOutput output;
|
| 79 |
|
| 80 | private PackGenerator(final boolean toRun, final String providerPrefix, final PackOutput output) {
|
| 81 | this.toRun = toRun;
|
| 82 | this.providerPrefix = providerPrefix;
|
| 83 | this.output = output;
|
| 84 | }
|
| 85 |
|
| 86 | public <T extends DataProvider> T addProvider(final DataProvider.Factory<T> factory) {
|
| 87 | T provider = factory.create(this.output);
|
| 88 | String providerId = this.providerPrefix + "/" + provider.getName();
|
| 89 | if (!DataGenerator.this.allProviderIds.add(providerId)) {
|
| 90 | throw new IllegalStateException("Duplicate provider: " + providerId);
|
| 91 | }
|
| 92 |
|
| 93 | if (this.toRun) {
|
| 94 | DataGenerator.this.providersToRun.put(providerId, provider);
|
| 95 | }
|
| 96 |
|
| 97 | return provider;
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | public static class Uncached extends DataGenerator {
|
| 102 | public Uncached(final Path output) {
|
| 103 | super(output);
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public void run() throws IOException {
|
| 108 | Stopwatch totalTime = Stopwatch.createStarted();
|
| 109 | Stopwatch stopwatch = Stopwatch.createUnstarted();
|
| 110 | this.providersToRun.forEach((providerId, provider) -> {
|
| 111 | DataGenerator.LOGGER.info("Starting uncached provider: {}", providerId);
|
| 112 | stopwatch.start();
|
| 113 | provider.run(CachedOutput.NO_CACHE).join();
|
| 114 | stopwatch.stop();
|
| 115 | DataGenerator.LOGGER.info("{} finished after {} ms", providerId, stopwatch.elapsed(TimeUnit.MILLISECONDS));
|
| 116 | stopwatch.reset();
|
| 117 | });
|
| 118 | DataGenerator.LOGGER.info("All providers took: {} ms", totalTime.elapsed(TimeUnit.MILLISECONDS));
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|