Search in sources :

Example 6 with Asset

use of org.lanternpowered.api.asset.Asset in project LanternServer by LanternPowered.

the class DirectoryAssetRepository method getAssetsMap.

@Override
public Multimap<String, Asset> getAssetsMap(String path, boolean checkChildDirectories) {
    final ImmutableMultimap.Builder<String, Asset> builder = ImmutableMultimap.builder();
    final Pattern pattern = Pattern.compile(generateRegex(path));
    final int length = this.directory.toString().length() + 1;
    try {
        Files.walkFileTree(this.directory, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                final String file0 = file.toString().substring(length).replace('\\', '/');
                final Matcher matcher = pattern.matcher(file0);
                if (matcher.matches()) {
                    final String id = matcher.group(2).toLowerCase(Locale.ENGLISH);
                    final int index = id.indexOf('/');
                    if (index == -1 || (checkChildDirectories && index != id.lastIndexOf('/'))) {
                        return FileVisitResult.CONTINUE;
                    }
                    final String plugin = matcher.group(1).toLowerCase(Locale.ENGLISH);
                    builder.put(id.substring(0, index), registerAsset(plugin, plugin + ':' + id, file));
                }
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
    return builder.build();
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) Asset(org.lanternpowered.api.asset.Asset) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 7 with Asset

use of org.lanternpowered.api.asset.Asset in project LanternServer by LanternPowered.

the class DirectoryAssetRepository method getAssets.

@Override
public Collection<Asset> getAssets(String path, boolean checkChildDirectories) {
    final ImmutableList.Builder<Asset> builder = ImmutableList.builder();
    final Pattern pattern = Pattern.compile(generateRegex(path));
    final int length = this.directory.toString().length() + 1;
    try {
        Files.walkFileTree(this.directory, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                final String file0 = file.toString().substring(length).replace('\\', '/');
                final Matcher matcher = pattern.matcher(file0);
                if (matcher.matches()) {
                    final String id = matcher.group(2).toLowerCase(Locale.ENGLISH);
                    int index;
                    if (!checkChildDirectories && (index = id.indexOf('/')) != -1 && id.lastIndexOf('/') != index) {
                        return FileVisitResult.CONTINUE;
                    }
                    final String plugin = matcher.group(1).toLowerCase(Locale.ENGLISH);
                    builder.add(registerAsset(plugin, plugin + ':' + id, file));
                }
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
    return builder.build();
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) Asset(org.lanternpowered.api.asset.Asset) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

Asset (org.lanternpowered.api.asset.Asset)7 IOException (java.io.IOException)4 Matcher (java.util.regex.Matcher)4 Pattern (java.util.regex.Pattern)4 Path (java.nio.file.Path)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)2 URL (java.net.URL)2 FileVisitResult (java.nio.file.FileVisitResult)2 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)2 ZipEntry (java.util.zip.ZipEntry)2 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)1 LoadingCache (com.github.benmanes.caffeine.cache.LoadingCache)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Sets (com.google.common.collect.Sets)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 Map (java.util.Map)1