PathPackResources.java

net.minecraft.server.packs.PathPackResources

信息

  • 全限定名:net.minecraft.server.packs.PathPackResources
  • 类型:public class
  • 包:net.minecraft.server.packs
  • 源码路径:src/main/java/net/minecraft/server/packs/PathPackResources.java
  • 起始行号:L30
  • 继承:AbstractPackResources
  • 职责:

    TODO

字段/常量

  • LOGGER

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

      TODO

  • PATH_JOINER

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

      TODO

  • root

    • 类型: Path
    • 修饰符: private final
    • 源码定位: L33
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.server.packs.PathPackResources.PathResourcesSupplier
    • 类型: class
    • 修饰符: public static
    • 源码定位: L159
    • 说明:

      TODO

构造器

public PathPackResources(PackLocationInfo location, Path root) @ L35

  • 构造器名:PathPackResources
  • 源码定位:L35
  • 修饰符:public

参数:

  • location: PackLocationInfo
  • root: Path

说明:

TODO

方法

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

public IoSupplier<InputStream> getRootResource(String... path) @ L40

  • 方法名:getRootResource
  • 源码定位:L40
  • 返回类型:IoSupplier
  • 修饰符:public

参数:

  • path: String…

说明:

TODO

public static boolean validatePath(Path path) @ L47

  • 方法名:validatePath
  • 源码定位:L47
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • path: Path

说明:

TODO

private Path topPackDir(PackType type) @ L62

  • 方法名:topPackDir
  • 源码定位:L62
  • 返回类型:Path
  • 修饰符:private

参数:

  • type: PackType

说明:

TODO

public IoSupplier<InputStream> getResource(PackType type, Identifier location) @ L66

  • 方法名:getResource
  • 源码定位:L66
  • 返回类型:IoSupplier
  • 修饰符:public

参数:

  • type: PackType
  • location: Identifier

说明:

TODO

public static IoSupplier<InputStream> getResource(Path topDir, Identifier location) @ L72

  • 方法名:getResource
  • 源码定位:L72
  • 返回类型:IoSupplier
  • 修饰符:public static

参数:

  • topDir: Path
  • location: Identifier

说明:

TODO

private static IoSupplier<InputStream> returnFileIfExists(Path resolvedPath) @ L83

  • 方法名:returnFileIfExists
  • 源码定位:L83
  • 返回类型:IoSupplier
  • 修饰符:private static

参数:

  • resolvedPath: Path

说明:

TODO

public void listResources(PackType type, String namespace, String directory, PackResources.ResourceOutput output) @ L87

  • 方法名:listResources
  • 源码定位:L87
  • 返回类型:void
  • 修饰符:public

参数:

  • type: PackType
  • namespace: String
  • directory: String
  • output: PackResources.ResourceOutput

说明:

TODO

public static void listResources(Path topPath, String namespace, String directory, PackResources.ResourceOutput output) @ L93

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

参数:

  • topPath: Path
  • namespace: String
  • directory: String
  • output: PackResources.ResourceOutput

说明:

TODO

public static void listPath(String namespace, Path topDir, List<String> decomposedPrefixPath, PackResources.ResourceOutput output) @ L100

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

参数:

  • namespace: String
  • topDir: Path
  • decomposedPrefixPath: List
  • output: PackResources.ResourceOutput

说明:

TODO

private static boolean isRegularFile(Path file, BasicFileAttributes attributes) @ L119

  • 方法名:isRegularFile
  • 源码定位:L119
  • 返回类型:boolean
  • 修饰符:private static

参数:

  • file: Path
  • attributes: BasicFileAttributes

说明:

TODO

public Set<String> getNamespaces(PackType type) @ L125

  • 方法名:getNamespaces
  • 源码定位:L125
  • 返回类型:Set
  • 修饰符:public

参数:

  • type: PackType

说明:

TODO

public static Set<String> getNamespaces(Path rootDir) @ L131

  • 方法名:getNamespaces
  • 源码定位:L131
  • 返回类型:Set
  • 修饰符:public static

参数:

  • rootDir: Path

说明:

TODO

public void close() @ L155

  • 方法名:close
  • 源码定位:L155
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

代码

public class PathPackResources extends AbstractPackResources {
    private static final Logger LOGGER = LogUtils.getLogger();
    private static final Joiner PATH_JOINER = Joiner.on("/");
    private final Path root;
 
    public PathPackResources(PackLocationInfo location, Path root) {
        super(location);
        this.root = root;
    }
 
    @Override
    public @Nullable IoSupplier<InputStream> getRootResource(String... path) {
        FileUtil.validatePath(path);
        Path pathInRoot = FileUtil.resolvePath(this.root, List.of(path));
        return Files.exists(pathInRoot) ? IoSupplier.create(pathInRoot) : null;
    }
 
    public static boolean validatePath(Path path) {
        if (!SharedConstants.DEBUG_VALIDATE_RESOURCE_PATH_CASE) {
            return true;
        } else if (path.getFileSystem() != FileSystems.getDefault()) {
            return true;
        } else {
            try {
                return path.toRealPath().endsWith(path);
            } catch (IOException var2) {
                LOGGER.warn("Failed to resolve real path for {}", path, var2);
                return false;
            }
        }
    }
 
    private Path topPackDir(PackType type) {
        return this.root.resolve(type.getDirectory());
    }
 
    @Override
    public @Nullable IoSupplier<InputStream> getResource(PackType type, Identifier location) {
        Path topDir = this.topPackDir(type);
        return getResource(topDir, location);
    }
 
    public static @Nullable IoSupplier<InputStream> getResource(Path topDir, Identifier location) {
        Path namespaceDir = topDir.resolve(location.getNamespace());
        return FileUtil.decomposePath(location.getPath()).mapOrElse(decomposedPath -> {
            Path resolvedPath = FileUtil.resolvePath(namespaceDir, (List<String>)decomposedPath);
            return returnFileIfExists(resolvedPath);
        }, error -> {
            LOGGER.error("Invalid path {}: {}", location, error.message());
            return null;
        });
    }
 
    private static @Nullable IoSupplier<InputStream> returnFileIfExists(Path resolvedPath) {
        return Files.exists(resolvedPath) && validatePath(resolvedPath) ? IoSupplier.create(resolvedPath) : null;
    }
 
    @Override
    public void listResources(PackType type, String namespace, String directory, PackResources.ResourceOutput output) {
        Path topDir = this.topPackDir(type);
        listResources(topDir, namespace, directory, output);
    }
 
    public static void listResources(Path topPath, String namespace, String directory, PackResources.ResourceOutput output) {
        FileUtil.decomposePath(directory).ifSuccess(decomposedPath -> {
            Path namespaceDir = topPath.resolve(namespace);
            listPath(namespace, namespaceDir, (List<String>)decomposedPath, output);
        }).ifError(error -> LOGGER.error("Invalid path {}: {}", directory, error.message()));
    }
 
    public static void listPath(String namespace, Path topDir, List<String> decomposedPrefixPath, PackResources.ResourceOutput output) {
        Path targetPath = FileUtil.resolvePath(topDir, decomposedPrefixPath);
 
        try (Stream<Path> files = Files.find(targetPath, Integer.MAX_VALUE, PathPackResources::isRegularFile)) {
            files.forEach(file -> {
                String resourcePath = PATH_JOINER.join(topDir.relativize(file));
                Identifier identifier = Identifier.tryBuild(namespace, resourcePath);
                if (identifier == null) {
                    Util.logAndPauseIfInIde(String.format(Locale.ROOT, "Invalid path in pack: %s:%s, ignoring", namespace, resourcePath));
                } else {
                    output.accept(identifier, IoSupplier.create(file));
                }
            });
        } catch (NotDirectoryException | NoSuchFileException var10) {
        } catch (IOException var11) {
            LOGGER.error("Failed to list path {}", targetPath, var11);
        }
    }
 
    private static boolean isRegularFile(Path file, BasicFileAttributes attributes) {
        return !SharedConstants.IS_RUNNING_IN_IDE
            ? attributes.isRegularFile()
            : attributes.isRegularFile() && !StringUtils.equalsIgnoreCase(file.getFileName().toString(), ".ds_store");
    }
 
    @Override
    public Set<String> getNamespaces(PackType type) {
        Path assetRoot = this.topPackDir(type);
        return getNamespaces(assetRoot);
    }
 
    public static Set<String> getNamespaces(Path rootDir) {
        Set<String> namespaces = new HashSet<>();
 
        try (DirectoryStream<Path> directDirs = Files.newDirectoryStream(rootDir)) {
            for (Path directDir : directDirs) {
                if (!Files.isDirectory(directDir)) {
                    LOGGER.warn("Non-directory entry {} found in namespace directory, rejecting", directDir);
                } else {
                    String namespace = directDir.getFileName().toString();
                    if (Identifier.isValidNamespace(namespace)) {
                        namespaces.add(namespace);
                    } else {
                        LOGGER.warn("Non {} character in namespace {} in pack directory {}, ignoring", "[a-z0-9_.-]", namespace, rootDir);
                    }
                }
            }
        } catch (NotDirectoryException | NoSuchFileException var8) {
        } catch (IOException var9) {
            LOGGER.error("Failed to list path {}", rootDir, var9);
        }
 
        return namespaces;
    }
 
    @Override
    public void close() {
    }
 
    public static class PathResourcesSupplier implements Pack.ResourcesSupplier {
        private final Path content;
 
        public PathResourcesSupplier(Path content) {
            this.content = content;
        }
 
        @Override
        public PackResources openPrimary(PackLocationInfo location) {
            return new PathPackResources(location, this.content);
        }
 
        @Override
        public PackResources openFull(PackLocationInfo location, Pack.Metadata metadata) {
            PackResources primary = this.openPrimary(location);
            List<String> overlays = metadata.overlays();
            if (overlays.isEmpty()) {
                return primary;
            } else {
                List<PackResources> overlayResources = new ArrayList<>(overlays.size());
 
                for (String overlay : overlays) {
                    Path overlayRoot = this.content.resolve(overlay);
                    overlayResources.add(new PathPackResources(location, overlayRoot));
                }
 
                return new CompositePackResources(primary, overlayResources);
            }
        }
    }
}

引用的其他类

  • Identifier

    • 引用位置: 参数/方法调用
    • 关联成员: Identifier.isValidNamespace(), Identifier.tryBuild()
  • AbstractPackResources

    • 引用位置: 继承
  • CompositePackResources

    • 引用位置: 构造调用
    • 关联成员: CompositePackResources()
  • PackLocationInfo

    • 引用位置: 参数
  • PackResources

    • 引用位置: 参数
  • PackType

    • 引用位置: 参数
  • IoSupplier

    • 引用位置: 方法调用/返回值
    • 关联成员: IoSupplier.create()
  • FileUtil

    • 引用位置: 方法调用
    • 关联成员: FileUtil.decomposePath(), FileUtil.resolvePath(), FileUtil.validatePath()
  • Util

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