Search in sources :

Example 1 with AssetRepository

use of org.lanternpowered.server.asset.AssetRepository in project LanternServer by LanternPowered.

the class AssetRepositoryJsonDeserializer method deserialize.

@Override
public AssetRepository deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    final MultiAssetRepository repository = new MultiAssetRepository();
    // The class loader asset repository will always be present,
    // this cannot be overridden, but the assets themselves can
    // be overridden like the minecraft resource pack system
    final ClassLoaderAssetRepository classLoaderAssetRepository = new ClassLoaderAssetRepository(this.pluginManager);
    repository.add(classLoaderAssetRepository);
    final LanternClassLoader classLoader = LanternClassLoader.get();
    final Consumer<URL> consumer = url -> {
        final Path path = PathUtils.toPath(url);
        if (Files.isDirectory(path) && Files.exists(path.resolve("data-packs.info"))) {
            Lantern.getLogger().debug("Registered a data pack asset repository: " + path);
            repository.add(new PacksAssetRepository(this.pluginManager, path));
        } else {
            classLoaderAssetRepository.addRepository(path);
        }
    };
    classLoader.getBaseURLs().forEach(consumer);
    classLoader.addBaseURLTracker(consumer);
    final JsonArray array = json.getAsJsonArray();
    for (int i = 0; i < array.size(); i++) {
        final JsonObject obj = array.get(i).getAsJsonObject();
        final String type = obj.get("type").getAsString().toLowerCase(Locale.ENGLISH);
        Path path;
        switch(type) {
            // Currently only directory asset repositories
            case "dir":
            case "directory":
                path = Paths.get(obj.get("path").getAsString());
                Lantern.getLogger().debug("Registered a directory asset repository: " + path);
                repository.add(new DirectoryAssetRepository(this.pluginManager, path));
                break;
            // Also support a directory with data/asset packs
            case "packs":
                path = Paths.get(obj.get("path").getAsString());
                Lantern.getLogger().debug("Registered a data pack asset repository: " + path);
                repository.add(new PacksAssetRepository(this.pluginManager, path));
                break;
            default:
                throw new JsonParseException("Unknown repository type: " + type);
        }
        if (!Files.exists(path)) {
            try {
                Files.createDirectories(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return repository;
}
Also used : JsonParseException(com.google.gson.JsonParseException) JsonObject(com.google.gson.JsonObject) Files(java.nio.file.Files) URL(java.net.URL) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) MultiAssetRepository(org.lanternpowered.server.asset.MultiAssetRepository) ClassLoaderAssetRepository(org.lanternpowered.server.asset.ClassLoaderAssetRepository) LanternPluginManager(org.lanternpowered.server.plugin.LanternPluginManager) AssetRepository(org.lanternpowered.server.asset.AssetRepository) IOException(java.io.IOException) PathUtils(org.lanternpowered.server.util.PathUtils) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) LanternClassLoader(org.lanternpowered.launch.LanternClassLoader) JsonElement(com.google.gson.JsonElement) Consumer(java.util.function.Consumer) JsonArray(com.google.gson.JsonArray) DirectoryAssetRepository(org.lanternpowered.server.asset.DirectoryAssetRepository) Lantern(org.lanternpowered.server.game.Lantern) Type(java.lang.reflect.Type) Paths(java.nio.file.Paths) Locale(java.util.Locale) JsonDeserializer(com.google.gson.JsonDeserializer) PacksAssetRepository(org.lanternpowered.server.asset.PacksAssetRepository) Path(java.nio.file.Path) Path(java.nio.file.Path) DirectoryAssetRepository(org.lanternpowered.server.asset.DirectoryAssetRepository) LanternClassLoader(org.lanternpowered.launch.LanternClassLoader) MultiAssetRepository(org.lanternpowered.server.asset.MultiAssetRepository) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) URL(java.net.URL) JsonArray(com.google.gson.JsonArray) PacksAssetRepository(org.lanternpowered.server.asset.PacksAssetRepository) ClassLoaderAssetRepository(org.lanternpowered.server.asset.ClassLoaderAssetRepository)

Example 2 with AssetRepository

use of org.lanternpowered.server.asset.AssetRepository in project LanternServer by LanternPowered.

the class TranslationManagerRegistryModule method registerDefaults.

@EarlyRegistration
@Override
public void registerDefaults() {
    this.translationManager = new CombinedTranslationManager();
    // Get the asset repository
    final AssetRepository assetRepository = Lantern.getAssetRepository();
    // Add the translation manager as a reload listener
    assetRepository.addReloadListener(this.translationManager);
    // Add the minecraft language file for defaults and
    // the client translations
    this.translationManager.addManager(new MinecraftTranslationManager());
    final LanternTranslationManager lanternTranslationManager = new LanternTranslationManager();
    // Add the lantern languages
    lanternTranslationManager.addResourceBundle(assetRepository.get("lantern", "lang/en_us.properties").get(), Locale.ENGLISH);
    this.translationManager.addManager(lanternTranslationManager);
    this.translationManager.setDelegateManager(lanternTranslationManager);
}
Also used : MinecraftTranslationManager(org.lanternpowered.server.text.translation.MinecraftTranslationManager) AssetRepository(org.lanternpowered.server.asset.AssetRepository) CombinedTranslationManager(org.lanternpowered.server.text.translation.CombinedTranslationManager) LanternTranslationManager(org.lanternpowered.server.text.translation.LanternTranslationManager) EarlyRegistration(org.lanternpowered.server.game.registry.EarlyRegistration)

Example 3 with AssetRepository

use of org.lanternpowered.server.asset.AssetRepository in project LanternServer by LanternPowered.

the class LanternScriptGameRegistry method compile.

@Override
public Script<Object> compile(Object plugin, String asset) {
    final AssetRepository assetRepository = Lantern.getAssetRepository();
    final Asset theAsset = assetRepository.get(plugin, asset).orElseThrow(() -> new IllegalArgumentException("There is no asset with the specified id: " + asset + " for the specified plugin:" + plugin));
    return this.compile(theAsset);
}
Also used : AssetRepository(org.lanternpowered.server.asset.AssetRepository) Asset(org.spongepowered.api.asset.Asset)

Example 4 with AssetRepository

use of org.lanternpowered.server.asset.AssetRepository in project LanternServer by LanternPowered.

the class LanternScriptGameRegistry method compile0.

private Script<Object> compile0(String assetId) {
    final AssetRepository assetRepository = Lantern.getAssetRepository();
    final Asset theAsset = assetRepository.get(assetId).orElseThrow(() -> new IllegalArgumentException("There is no asset with the specified id: " + assetId));
    return this.compile(theAsset);
}
Also used : AssetRepository(org.lanternpowered.server.asset.AssetRepository) Asset(org.spongepowered.api.asset.Asset)

Example 5 with AssetRepository

use of org.lanternpowered.server.asset.AssetRepository in project LanternServer by LanternPowered.

the class LanternScriptGameRegistry method compile.

@Override
public <T> Script<T> compile(Object plugin, String asset, Class<T> function) {
    final AssetRepository assetRepository = Lantern.getAssetRepository();
    final Asset theAsset = assetRepository.get(plugin, asset).orElseThrow(() -> new IllegalArgumentException("There is no asset with the specified id: " + asset));
    return this.compile(theAsset, function);
}
Also used : AssetRepository(org.lanternpowered.server.asset.AssetRepository) Asset(org.spongepowered.api.asset.Asset)

Aggregations

AssetRepository (org.lanternpowered.server.asset.AssetRepository)6 Asset (org.spongepowered.api.asset.Asset)4 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 JsonArray (com.google.gson.JsonArray)1 JsonDeserializationContext (com.google.gson.JsonDeserializationContext)1 JsonDeserializer (com.google.gson.JsonDeserializer)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1 URL (java.net.URL)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Locale (java.util.Locale)1 Consumer (java.util.function.Consumer)1 LanternClassLoader (org.lanternpowered.launch.LanternClassLoader)1 ClassLoaderAssetRepository (org.lanternpowered.server.asset.ClassLoaderAssetRepository)1 DirectoryAssetRepository (org.lanternpowered.server.asset.DirectoryAssetRepository)1