Search in sources :

Example 1 with Asset

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

the class FileAssetRepository method getAssetsMap.

@Override
public Multimap<String, Asset> getAssetsMap(String path, boolean checkChildDirectories) {
    final ImmutableMultimap.Builder<String, Asset> builder = ImmutableMultimap.builder();
    final Enumeration<? extends ZipEntry> enumeration = getZipFile().entries();
    final Pattern pattern = Pattern.compile(generateRegex(path));
    while (enumeration.hasMoreElements()) {
        final ZipEntry zipEntry = enumeration.nextElement();
        final String name = zipEntry.getName().replace('\\', '/');
        final Matcher matcher = pattern.matcher(name);
        if (matcher.matches()) {
            final String id = matcher.group(2).toLowerCase(Locale.ENGLISH);
            final int index = id.indexOf('/');
            if (index == -1 || (checkChildDirectories && index != id.lastIndexOf('/'))) {
                continue;
            }
            final String plugin = matcher.group(1).toLowerCase(Locale.ENGLISH);
            builder.put(id.substring(0, index), registerAsset(plugin, plugin + ':' + id, generateAssetURL(name), Paths.get(name)));
        }
    }
    return builder.build();
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ZipEntry(java.util.zip.ZipEntry) Asset(org.lanternpowered.api.asset.Asset) ImmutableMultimap(com.google.common.collect.ImmutableMultimap)

Example 2 with Asset

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

the class FileAssetRepository method getAssets.

@Override
public Collection<Asset> getAssets(String path, boolean checkChildDirectories) {
    final ImmutableList.Builder<Asset> builder = ImmutableList.builder();
    final Enumeration<? extends ZipEntry> enumeration = getZipFile().entries();
    final Pattern pattern = Pattern.compile(generateRegex(path));
    while (enumeration.hasMoreElements()) {
        final ZipEntry zipEntry = enumeration.nextElement();
        final String name = zipEntry.getName().replace('\\', '/');
        final Matcher matcher = pattern.matcher(name);
        if (matcher.matches()) {
            final String id = matcher.group(2).toLowerCase(Locale.ENGLISH);
            int index;
            if (!checkChildDirectories && (index = id.indexOf('/')) != -1 && id.lastIndexOf('/') != index) {
                continue;
            }
            final String plugin = matcher.group(1).toLowerCase(Locale.ENGLISH);
            builder.add(registerAsset(plugin, plugin + ':' + id, generateAssetURL(name), Paths.get(name)));
        }
    }
    return builder.build();
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) ZipEntry(java.util.zip.ZipEntry) Asset(org.lanternpowered.api.asset.Asset)

Example 3 with Asset

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

the class LanternTranslationManager method loadAssetBundle.

private void loadAssetBundle(Asset asset, Locale locale, boolean refresh) {
    try {
        final InputStream inputStream = asset.getUrl().openStream();
        try {
            final ResourceBundle bundle = new PropertyResourceBundle(inputStream);
            this.bundles.computeIfAbsent(locale, locale0 -> Sets.newConcurrentHashSet()).add(bundle);
            if (refresh) {
                final Set<ResourceKey> refreshKeys = Sets.newHashSet();
                for (ResourceKey key : this.resourceBundlesCache.asMap().keySet()) {
                    Locale locale1 = key.locale == null ? Locale.ENGLISH : key.locale;
                    if (locale1.equals(locale) && bundle.containsKey(key.name)) {
                        refreshKeys.add(key);
                    }
                }
                if (!refreshKeys.isEmpty()) {
                    this.resourceBundlesCache.invalidateAll(refreshKeys);
                }
            }
        } catch (IOException e) {
            Lantern.getLogger().warn("Unable to create the resource bundle for: " + asset.getId(), e);
        }
    } catch (IOException e) {
        Lantern.getLogger().warn("Unable to open the asset stream for: " + asset.getId(), e);
    }
}
Also used : Caffeine(com.github.benmanes.caffeine.cache.Caffeine) ResourceBundleTranslation(org.spongepowered.api.text.translation.ResourceBundleTranslation) LoadingCache(com.github.benmanes.caffeine.cache.LoadingCache) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PropertyResourceBundle(java.util.PropertyResourceBundle) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) Sets(com.google.common.collect.Sets) ReloadListener(org.lanternpowered.server.asset.ReloadListener) Asset(org.lanternpowered.api.asset.Asset) ConcurrentMap(java.util.concurrent.ConcurrentMap) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ResourceBundle(java.util.ResourceBundle) Lantern(org.lanternpowered.server.game.Lantern) Locale(java.util.Locale) Map(java.util.Map) Optional(java.util.Optional) Conditions.checkNotNullOrEmpty(org.lanternpowered.server.util.Conditions.checkNotNullOrEmpty) Nullable(javax.annotation.Nullable) Translation(org.spongepowered.api.text.translation.Translation) InputStream(java.io.InputStream) Locale(java.util.Locale) InputStream(java.io.InputStream) PropertyResourceBundle(java.util.PropertyResourceBundle) ResourceBundle(java.util.ResourceBundle) IOException(java.io.IOException) PropertyResourceBundle(java.util.PropertyResourceBundle)

Example 4 with Asset

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

the class AbstractAssetRepository method registerAsset.

Asset registerAsset(Object plugin, String id, URL url, Path file) {
    final Optional<Asset> optAsset = this.loadedAssets.get(id);
    if (optAsset != null) {
        return optAsset.get();
    }
    PluginContainer pluginContainer;
    if (plugin instanceof String) {
        final String pluginId = (String) plugin;
        // Attempt to find a plugin container that is assigned to the id,
        // if not, create a plugin container that represents the plugin
        // with the id.
        pluginContainer = this.pluginManager.getPlugin(pluginId).orElse(null);
        if (pluginContainer == null) {
            // don't register a plugin in this case, just return a asset with a dummy one.
            if (INFO_FILE_PATTERN.matcher(id).matches()) {
                return new LanternAsset(new SimplePluginContainer(pluginId), id, url, file);
            }
            // Attempt to get plugin info from the repository, and use
            // that to define the plugin container
            final URL infoUrl = getAssetURL(Paths.get(DEFAULT_ASSET_DIR).resolve(pluginId).resolve("plugin.info"));
            if (infoUrl != null) {
                try {
                    final PluginMetadata pluginMetadata = InfoPluginContainer.readPluginInfo(pluginId, infoUrl);
                    // Construct a plugin container
                    pluginContainer = new InfoPluginContainer(pluginId, pluginMetadata);
                } catch (IOException e) {
                    Lantern.getLogger().error("Failed to read plugin.info");
                }
            }
            if (pluginContainer == null) {
                // Generate a simple plugin container
                pluginContainer = new SimplePluginContainer(pluginId);
            }
            // Register the plugin container
            this.pluginManager.registerPlugin(pluginContainer);
            Lantern.getLogger().info("Registered data pack plugin: {} {}", pluginContainer.getName(), pluginContainer.getVersion().orElse("unknown"));
        }
    } else {
        // Search for the plugin container based on the instance
        pluginContainer = this.pluginManager.fromInstance(plugin).get();
    }
    checkNotNull(pluginContainer);
    final LanternAsset asset = new LanternAsset(pluginContainer, id, url, file);
    this.loadedAssets.put(id, Optional.of(asset));
    return asset;
}
Also used : SimplePluginContainer(org.lanternpowered.server.plugin.SimplePluginContainer) PluginContainer(org.spongepowered.api.plugin.PluginContainer) InfoPluginContainer(org.lanternpowered.server.plugin.InfoPluginContainer) SimplePluginContainer(org.lanternpowered.server.plugin.SimplePluginContainer) Asset(org.lanternpowered.api.asset.Asset) PluginMetadata(org.spongepowered.plugin.meta.PluginMetadata) IOException(java.io.IOException) URL(java.net.URL) InfoPluginContainer(org.lanternpowered.server.plugin.InfoPluginContainer)

Example 5 with Asset

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

the class AbstractAssetRepository method get.

@Override
public Optional<Asset> get(Object plugin, String name) {
    checkNotNull(plugin, "plugin");
    checkNotNull(name, "name");
    String pluginId;
    if (plugin instanceof String) {
        pluginId = (String) plugin;
    } else {
        pluginId = this.pluginManager.fromInstance(plugin).get().getId();
    }
    // All the resource paths are lowercase
    pluginId = pluginId.toLowerCase(Locale.ENGLISH);
    name = name.toLowerCase(Locale.ENGLISH);
    // Generate the id and check if the resource is already loaded
    final String id = pluginId + ':' + name;
    Optional<Asset> asset = this.loadedAssets.get(id);
    if (asset != null) {
        return asset;
    }
    pluginId = pluginId.replace('.', File.separatorChar);
    if (!pluginId.endsWith(File.separator)) {
        pluginId += File.separator;
    }
    // Get the path for the asset and attempt to find the file url
    for (String dir : DEFAULT_ASSET_DIRS) {
        final Path path = Paths.get(dir).resolve(pluginId).resolve(name);
        final URL url = getAssetURL(path);
        if (url != null) {
            return Optional.of(registerAsset(plugin, id, url, path));
        }
    }
    this.loadedAssets.put(id, Optional.empty());
    return Optional.empty();
}
Also used : Path(java.nio.file.Path) Asset(org.lanternpowered.api.asset.Asset) URL(java.net.URL)

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