Search in sources :

Example 31 with LionEngineException

use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.

the class FactoryGraphicAwt method saveImage.

@Override
public void saveImage(ImageBuffer image, Media media) {
    Check.notNull(media);
    final BufferedImage surface = image.getSurface();
    try (OutputStream output = media.getOutputStream()) {
        ToolsAwt.saveImage(surface, output);
    } catch (final IOException exception) {
        throw new LionEngineException(exception, media, ERROR_IMAGE_SAVE);
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) OutputStream(java.io.OutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Example 32 with LionEngineException

use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.

the class Factory method create.

/**
 * Create a {@link Featurable} from its {@link Media} using a generic way. The concerned class to instantiate and
 * its constructor must be public, and can have the following parameter: ({@link Setup}).
 * <p>
 * Automatically add {@link IdentifiableModel} if feature does not have {@link Identifiable} feature.
 * </p>
 * <p>
 * Destroyed {@link Featurable} can be cached to avoid {@link Featurable} creation if has {@link Recycler} and
 * {@link Recyclable} {@link Feature}s. If cache associated to media is available, it is
 * {@link Recyclable#recycle()} and then returned.
 * </p>
 *
 * @param <O> The featurable type.
 * @param media The featurable media.
 * @return The featurable instance.
 * @throws LionEngineException If {@link Media} is <code>null</code> or {@link Setup} not found.
 */
@SuppressWarnings("unchecked")
public <O extends Featurable> O create(Media media) {
    if (cache.containsKey(media) && !cache.get(media).isEmpty()) {
        final Featurable featurable = cache.get(media).poll();
        featurable.getFeature(Recycler.class).recycle();
        return (O) featurable;
    }
    final Setup setup = getSetup(media);
    final Class<O> type = setup.getConfigClass(classLoader);
    try {
        return createFeaturable(type, setup);
    } catch (final NoSuchMethodException exception) {
        throw new LionEngineException(exception, ERROR_CONSTRUCTOR_MISSING + media);
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException)

Example 33 with LionEngineException

use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.

the class Factory method create.

/**
 * Create a featurable from its {@link Media} using a generic way. The concerned class to instantiate and its
 * constructor must be public, and can have the following parameter: ({@link Setup}).
 * <p>
 * Automatically add {@link IdentifiableModel} if feature does not have {@link Identifiable} feature.
 * </p>
 * <p>
 * Destroyed {@link Featurable} can be cached to avoid {@link Featurable} creation if has {@link Recycler} and
 * {@link Recyclable} {@link Feature}s. If cache associated to media is available, it is
 * {@link Recyclable#recycle()} and then returned.
 * </p>
 *
 * @param <O> The featurable type.
 * @param media The featurable media.
 * @param type The specific class to use (override the one in the media).
 * @return The featurable instance.
 * @throws LionEngineException If {@link Media} is <code>null</code> or {@link Setup} not found.
 */
@SuppressWarnings("unchecked")
public <O extends Featurable> O create(Media media, Class<O> type) {
    if (cache.containsKey(media) && !cache.get(media).isEmpty()) {
        final Featurable featurable = cache.get(media).poll();
        featurable.getFeature(Recycler.class).recycle();
        return (O) featurable;
    }
    final Setup setup = getSetup(media, true);
    try {
        return createFeaturable(type, setup);
    } catch (final NoSuchMethodException exception) {
        throw new LionEngineException(exception, ERROR_CONSTRUCTOR_MISSING + media);
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException)

Example 34 with LionEngineException

use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.

the class FeaturableConfig method getClass.

/**
 * Get the class reference from its name using cache.
 *
 * @param <T> The class type.
 * @param loader The class loader reference.
 * @param className The class name.
 * @return The typed class instance.
 * @throws LionEngineException If invalid class.
 */
@SuppressWarnings("unchecked")
private static <T> Class<T> getClass(ClassLoader loader, String className) {
    if (CLASS_CACHE.containsKey(className)) {
        return (Class<T>) CLASS_CACHE.get(className);
    }
    try {
        final Class<?> clazz = loader.loadClass(className);
        CLASS_CACHE.put(className, clazz);
        return (Class<T>) clazz;
    } catch (final ClassNotFoundException exception) {
        throw new LionEngineException(exception, ERROR_CLASS_PRESENCE + className);
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException)

Example 35 with LionEngineException

use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.

the class FeaturableConfig method getFeatures.

/**
 * Get all available features.
 * Default constructor of each feature must be available or with {@link Setup} as single parameter.
 *
 * @param loader The class loader reference.
 * @param services The services reference.
 * @param setup The setup reference.
 * @param filter The type filter (can be <code>null</code> if none).
 * @return The available features.
 * @throws LionEngineException If invalid class.
 */
public static List<Feature> getFeatures(ClassLoader loader, Services services, Setup setup, Class<?> filter) {
    final Collection<XmlReader> children;
    final XmlReader root = setup.getRoot();
    if (root.hasNode(NODE_FEATURES)) {
        children = setup.getRoot().getChild(FeaturableConfig.NODE_FEATURES).getChildren(FeaturableConfig.NODE_FEATURE);
    } else {
        children = Collections.emptyList();
    }
    final List<Feature> features = new ArrayList<>(children.size());
    for (final XmlReader featureNode : children) {
        final String className = featureNode.getText();
        try {
            final Class<? extends Feature> clazz = getClass(loader, className);
            if (filter == null || filter.isAssignableFrom(clazz)) {
                final Feature feature = UtilReflection.createReduce(clazz, services, setup);
                features.add(feature);
            }
        } catch (final NoSuchMethodException | LionEngineException exception) {
            throw new LionEngineException(exception, setup.getMedia());
        }
    }
    children.clear();
    return features;
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) ArrayList(java.util.ArrayList) XmlReader(com.b3dgs.lionengine.XmlReader) Feature(com.b3dgs.lionengine.game.Feature)

Aggregations

LionEngineException (com.b3dgs.lionengine.LionEngineException)75 IOException (java.io.IOException)13 File (java.io.File)8 OutputStream (java.io.OutputStream)7 ArrayList (java.util.ArrayList)7 XmlReader (com.b3dgs.lionengine.XmlReader)6 Field (java.lang.reflect.Field)6 Test (org.junit.Test)6 Media (com.b3dgs.lionengine.Media)5 Configurer (com.b3dgs.lionengine.game.Configurer)4 UtilFile (com.b3dgs.lionengine.UtilFile)3 Graphic (com.b3dgs.lionengine.graphic.Graphic)3 UtilFile (com.b3dgs.lionengine.util.UtilFile)3 DisplayMode (java.awt.DisplayMode)3 BufferedImage (java.awt.image.BufferedImage)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 AfterAll (org.junit.jupiter.api.AfterAll)3 Animation (com.b3dgs.lionengine.Animation)2 AnimationConfig (com.b3dgs.lionengine.game.AnimationConfig)2 Feature (com.b3dgs.lionengine.game.Feature)2