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();
}
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();
}
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);
}
}
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;
}
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();
}
Aggregations