GameTestMainUtil.java

net.minecraft.gametest.framework.GameTestMainUtil

信息

  • 全限定名:net.minecraft.gametest.framework.GameTestMainUtil
  • 类型:public class
  • 包:net.minecraft.gametest.framework
  • 源码路径:src/main/java/net/minecraft/gametest/framework/GameTestMainUtil.java
  • 起始行号:L25
  • 职责:

    TODO

字段/常量

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L26
    • 说明:

      TODO

  • DEFAULT_UNIVERSE_DIR

    • 类型: String
    • 修饰符: private static final
    • 源码定位: L27
    • 说明:

      TODO

  • LEVEL_NAME

    • 类型: String
    • 修饰符: private static final
    • 源码定位: L28
    • 说明:

      TODO

  • parser

    • 类型: OptionParser
    • 修饰符: private static final
    • 源码定位: L29
    • 说明:

      TODO

  • universe

    • 类型: OptionSpec<String>
    • 修饰符: private static final
    • 源码定位: L30
    • 说明:

      TODO

  • report

    • 类型: OptionSpec<File>
    • 修饰符: private static final
    • 源码定位: L35
    • 说明:

      TODO

  • tests

    • 类型: OptionSpec<String>
    • 修饰符: private static final
    • 源码定位: L38
    • 说明:

      TODO

  • verify

    • 类型: OptionSpec<Boolean>
    • 修饰符: private static final
    • 源码定位: L42
    • 说明:

      TODO

  • repeatCount

    • 类型: OptionSpec<Integer>
    • 修饰符: private static final
    • 源码定位: L48
    • 说明:

      TODO

  • packs

    • 类型: OptionSpec<String>
    • 修饰符: private static final
    • 源码定位: L52
    • 说明:

      TODO

  • help

    • 类型: OptionSpec<Void>
    • 修饰符: private static final
    • 源码定位: L53
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

下面的方法块按源码顺序生成。

public static void runGameTestServer(String[] args, Consumer<String> onUniverseCreated) @ L55

  • 方法名:runGameTestServer
  • 源码定位:L55
  • 返回类型:void
  • 修饰符:public static

参数:

  • args: String[]
  • onUniverseCreated: Consumer

说明:

TODO

private static Optional<String> optionalFromOption(OptionSet options, OptionSpec<String> option) @ L96

  • 方法名:optionalFromOption
  • 源码定位:L96
  • 返回类型:Optional
  • 修饰符:private static

参数:

  • options: OptionSet
  • option: OptionSpec

说明:

TODO

private static void createOrResetDir(String universePath) @ L100

  • 方法名:createOrResetDir
  • 源码定位:L100
  • 返回类型:void
  • 修饰符:private static

参数:

  • universePath: String

说明:

TODO

private static void copyPacks(String serverPath, String packSourcePath) @ L109

  • 方法名:copyPacks
  • 源码定位:L109
  • 返回类型:void
  • 修饰符:private static

参数:

  • serverPath: String
  • packSourcePath: String

说明:

TODO

代码

public class GameTestMainUtil {
    private static final Logger LOGGER = LogUtils.getLogger();
    private static final String DEFAULT_UNIVERSE_DIR = "gametestserver";
    private static final String LEVEL_NAME = "gametestworld";
    private static final OptionParser parser = new OptionParser();
    private static final OptionSpec<String> universe = parser.accepts(
            "universe", "The path to where the test server world will be created. Any existing folder will be replaced."
        )
        .withRequiredArg()
        .defaultsTo("gametestserver");
    private static final OptionSpec<File> report = parser.accepts("report", "Exports results in a junit-like XML report at the given path.")
        .withRequiredArg()
        .ofType(File.class);
    private static final OptionSpec<String> tests = parser.accepts(
            "tests", "Which test(s) to run (namespaced ID selector using wildcards). Empty means run all."
        )
        .withRequiredArg();
    private static final OptionSpec<Boolean> verify = parser.accepts(
            "verify", "Runs the tests specified with `test` or `testNamespace` 100 times for each 90 degree rotation step"
        )
        .withRequiredArg()
        .ofType(Boolean.class)
        .defaultsTo(false);
    private static final OptionSpec<Integer> repeatCount = parser.accepts("repeatCount", "Runs each of the specified tests this many times")
        .withRequiredArg()
        .ofType(Integer.class)
        .defaultsTo(1);
    private static final OptionSpec<String> packs = parser.accepts("packs", "A folder of datapacks to include in the world").withRequiredArg();
    private static final OptionSpec<Void> help = parser.accepts("help").forHelp();
 
    @SuppressForbidden(reason = "Using System.err due to no bootstrap")
    public static void runGameTestServer(String[] args, Consumer<String> onUniverseCreated) throws Exception {
        parser.allowsUnrecognizedOptions();
        OptionSet options = parser.parse(args);
        if (options.has(help)) {
            parser.printHelpOn(System.err);
        } else {
            if (options.valueOf(verify) && !options.has(tests)) {
                LOGGER.error("Please specify a test selection to run the verify option. For example: --verify --tests example:test_something_*");
                System.exit(-1);
            }
 
            if (options.valueOf(verify) && options.has(repeatCount)) {
                LOGGER.info("Flag --verify is true, the --repeatCount value will be ignored");
            }
 
            LOGGER.info("Running GameTestMain with cwd '{}', universe path '{}'", System.getProperty("user.dir"), options.valueOf(universe));
            if (options.has(report)) {
                GlobalTestReporter.replaceWith(new JUnitLikeTestReporter(report.value(options)));
            }
 
            Bootstrap.bootStrap();
            Util.startTimerHackThread();
            String universePath = options.valueOf(universe);
            createOrResetDir(universePath);
            onUniverseCreated.accept(universePath);
            if (options.has(packs)) {
                String packFolder = options.valueOf(packs);
                copyPacks(universePath, packFolder);
            }
 
            LevelStorageSource.LevelStorageAccess levelStorageSource = LevelStorageSource.createDefault(Paths.get(universePath)).createAccess("gametestworld");
            PackRepository packRepository = ServerPacksSource.createPackRepository(levelStorageSource);
            MinecraftServer.spin(
                thread -> GameTestServer.create(
                    thread, levelStorageSource, packRepository, optionalFromOption(options, tests), options.valueOf(verify), options.valueOf(repeatCount)
                )
            );
        }
    }
 
    private static Optional<String> optionalFromOption(OptionSet options, OptionSpec<String> option) {
        return options.has(option) ? Optional.of(options.valueOf(option)) : Optional.empty();
    }
 
    private static void createOrResetDir(String universePath) throws IOException {
        Path universeDir = Paths.get(universePath);
        if (Files.exists(universeDir)) {
            FileUtils.deleteDirectory(universeDir.toFile());
        }
 
        Files.createDirectories(universeDir);
    }
 
    private static void copyPacks(String serverPath, String packSourcePath) throws IOException {
        Path worldPackFolder = Paths.get(serverPath).resolve("gametestworld").resolve("datapacks");
        if (!Files.exists(worldPackFolder)) {
            Files.createDirectories(worldPackFolder);
        }
 
        Path sourceFolder = Paths.get(packSourcePath);
        if (Files.exists(sourceFolder)) {
            try (Stream<Path> list = Files.list(sourceFolder)) {
                for (Path path : list.toList()) {
                    Path destination = worldPackFolder.resolve(path.getFileName());
                    if (Files.isDirectory(path)) {
                        if (Files.isRegularFile(path.resolve("pack.mcmeta"))) {
                            FileUtils.copyDirectory(path.toFile(), destination.toFile());
                            LOGGER.info("Included folder pack {}", path.getFileName());
                        }
                    } else if (path.toString().endsWith(".zip")) {
                        Files.copy(path, destination);
                        LOGGER.info("Included zip pack {}", path.getFileName());
                    }
                }
            }
        }
    }
}

引用的其他类

  • LoggedChatMessage

    • 引用位置: 方法调用
    • 关联成员: System.exit(), System.getProperty()
  • GameTestServer

    • 引用位置: 方法调用
    • 关联成员: GameTestServer.create()
  • GlobalTestReporter

    • 引用位置: 方法调用
    • 关联成员: GlobalTestReporter.replaceWith()
  • JUnitLikeTestReporter

    • 引用位置: 构造调用
    • 关联成员: JUnitLikeTestReporter()
  • Bootstrap

    • 引用位置: 方法调用
    • 关联成员: Bootstrap.bootStrap()
  • MinecraftServer

    • 引用位置: 方法调用
    • 关联成员: MinecraftServer.spin()
  • ServerPacksSource

    • 引用位置: 方法调用
    • 关联成员: ServerPacksSource.createPackRepository()
  • Util

    • 引用位置: 方法调用
    • 关联成员: Util.startTimerHackThread()
  • EventLogDirectory

    • 引用位置: 字段
  • LevelStorageSource

    • 引用位置: 方法调用
    • 关联成员: LevelStorageSource.createDefault()