Search in sources :

Example 46 with SimpleUri

use of org.terasology.engine.SimpleUri in project Terasology by MovingBlocks.

the class TypeSerializationLibrary method getHandlerFor.

// TODO: Refactor
@SuppressWarnings("unchecked")
public TypeHandler<?> getHandlerFor(Type genericType) {
    Class<?> typeClass = ReflectionUtil.getClassOfType(genericType);
    if (typeClass == null) {
        logger.error("Unabled to get class from type {}", genericType);
        return null;
    }
    if (Enum.class.isAssignableFrom(typeClass)) {
        return new EnumTypeHandler(typeClass);
    } else if (List.class.isAssignableFrom(typeClass)) {
        // For lists, createEntityRef the handler for the contained type and wrap in a list type handler
        Type parameter = ReflectionUtil.getTypeParameter(genericType, 0);
        if (parameter != null) {
            TypeHandler<?> innerHandler = getHandlerFor(parameter);
            if (innerHandler != null) {
                return new ListTypeHandler<>(innerHandler);
            }
        }
        logger.error("List field is not parametrized, or holds unsupported type");
        return null;
    } else if (Set.class.isAssignableFrom(typeClass)) {
        // For sets:
        Type parameter = ReflectionUtil.getTypeParameter(genericType, 0);
        if (parameter != null) {
            TypeHandler<?> innerHandler = getHandlerFor(parameter);
            if (innerHandler != null) {
                return new SetTypeHandler<>(innerHandler);
            }
        }
        logger.error("Set field is not parametrized, or holds unsupported type");
        return null;
    } else if (Map.class.isAssignableFrom(typeClass)) {
        // For Maps, createEntityRef the handler for the value type (and maybe key too?)
        Type keyParameter = ReflectionUtil.getTypeParameter(genericType, 0);
        Type contentsParameter = ReflectionUtil.getTypeParameter(genericType, 1);
        if (keyParameter != null && contentsParameter != null && String.class == keyParameter) {
            TypeHandler<?> valueHandler = getHandlerFor(contentsParameter);
            if (valueHandler != null) {
                return new StringMapTypeHandler<>(valueHandler);
            }
        }
        logger.error("Map field is not parametrized, does not have a String key, or holds unsupported values");
    } else if (typeHandlers.containsKey(typeClass)) {
        // For known types, just use the handler
        return typeHandlers.get(typeClass);
    } else if (typeClass.getAnnotation(MappedContainer.class) != null && !Modifier.isAbstract(typeClass.getModifiers()) && !typeClass.isLocalClass() && !(typeClass.isMemberClass() && !Modifier.isStatic(typeClass.getModifiers()))) {
        try {
            ClassMetadata<?, ?> metadata = new DefaultClassMetadata<>(new SimpleUri(), typeClass, reflectFactory, copyStrategies);
            MappedContainerTypeHandler<?> mappedHandler = new MappedContainerTypeHandler(typeClass, getFieldHandlerMap(metadata));
            typeHandlers.put(typeClass, mappedHandler);
            return mappedHandler;
        } catch (NoSuchMethodException e) {
            logger.error("Unable to register field of type {}: no publicly accessible default constructor", typeClass.getSimpleName());
            return null;
        }
    } else {
        logger.error("Unable to register field of type {}: not a supported type or MappedContainer", typeClass.getSimpleName());
    }
    return null;
}
Also used : ClassMetadata(org.terasology.reflection.metadata.ClassMetadata) DefaultClassMetadata(org.terasology.reflection.metadata.DefaultClassMetadata) SimpleUri(org.terasology.engine.SimpleUri) SetTypeHandler(org.terasology.persistence.typeHandling.coreTypes.SetTypeHandler) EnumTypeHandler(org.terasology.persistence.typeHandling.coreTypes.EnumTypeHandler) Type(java.lang.reflect.Type) List(java.util.List) Vector2fTypeHandler(org.terasology.persistence.typeHandling.mathTypes.Vector2fTypeHandler) Vector4fTypeHandler(org.terasology.persistence.typeHandling.mathTypes.Vector4fTypeHandler) Vector3iTypeHandler(org.terasology.persistence.typeHandling.mathTypes.Vector3iTypeHandler) StringTypeHandler(org.terasology.persistence.typeHandling.coreTypes.StringTypeHandler) Vector2iTypeHandler(org.terasology.persistence.typeHandling.mathTypes.Vector2iTypeHandler) BooleanTypeHandler(org.terasology.persistence.typeHandling.coreTypes.BooleanTypeHandler) ColorTypeHandler(org.terasology.persistence.typeHandling.extensionTypes.ColorTypeHandler) StringMapTypeHandler(org.terasology.persistence.typeHandling.coreTypes.StringMapTypeHandler) Vector3fTypeHandler(org.terasology.persistence.typeHandling.mathTypes.Vector3fTypeHandler) EnumTypeHandler(org.terasology.persistence.typeHandling.coreTypes.EnumTypeHandler) NumberTypeHandler(org.terasology.persistence.typeHandling.coreTypes.NumberTypeHandler) ByteTypeHandler(org.terasology.persistence.typeHandling.coreTypes.ByteTypeHandler) SetTypeHandler(org.terasology.persistence.typeHandling.coreTypes.SetTypeHandler) Rect2iTypeHandler(org.terasology.persistence.typeHandling.mathTypes.Rect2iTypeHandler) FloatTypeHandler(org.terasology.persistence.typeHandling.coreTypes.FloatTypeHandler) AssetTypeHandler(org.terasology.persistence.typeHandling.extensionTypes.AssetTypeHandler) Quat4fTypeHandler(org.terasology.persistence.typeHandling.mathTypes.Quat4fTypeHandler) MappedContainerTypeHandler(org.terasology.persistence.typeHandling.coreTypes.MappedContainerTypeHandler) TextureRegionTypeHandler(org.terasology.persistence.typeHandling.extensionTypes.TextureRegionTypeHandler) NameTypeHandler(org.terasology.persistence.typeHandling.extensionTypes.NameTypeHandler) PrefabTypeHandler(org.terasology.persistence.typeHandling.extensionTypes.PrefabTypeHandler) IntTypeHandler(org.terasology.persistence.typeHandling.coreTypes.IntTypeHandler) Region3iTypeHandler(org.terasology.persistence.typeHandling.mathTypes.Region3iTypeHandler) ListTypeHandler(org.terasology.persistence.typeHandling.coreTypes.ListTypeHandler) LongTypeHandler(org.terasology.persistence.typeHandling.coreTypes.LongTypeHandler) Rect2fTypeHandler(org.terasology.persistence.typeHandling.mathTypes.Rect2fTypeHandler) DoubleTypeHandler(org.terasology.persistence.typeHandling.coreTypes.DoubleTypeHandler) Map(java.util.Map) MappedContainerTypeHandler(org.terasology.persistence.typeHandling.coreTypes.MappedContainerTypeHandler) MappedContainer(org.terasology.reflection.MappedContainer)

Example 47 with SimpleUri

use of org.terasology.engine.SimpleUri in project Terasology by MovingBlocks.

the class WorldGeneratorManager method refresh.

public void refresh() {
    ModuleManager moduleManager = context.get(ModuleManager.class);
    List<WorldGeneratorInfo> infos = Lists.newArrayList();
    for (Name moduleId : moduleManager.getRegistry().getModuleIds()) {
        Module module = moduleManager.getRegistry().getLatestModuleVersion(moduleId);
        if (module.isCodeModule()) {
            DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
            ResolutionResult resolutionResult = resolver.resolve(module.getId());
            if (resolutionResult.isSuccess()) {
                try (ModuleEnvironment tempEnvironment = moduleManager.loadEnvironment(resolutionResult.getModules(), false)) {
                    for (Class<?> generatorClass : tempEnvironment.getTypesAnnotatedWith(RegisterWorldGenerator.class)) {
                        if (tempEnvironment.getModuleProviding(generatorClass).equals(module.getId())) {
                            RegisterWorldGenerator annotation = generatorClass.getAnnotation(RegisterWorldGenerator.class);
                            if (isValidWorldGenerator(generatorClass)) {
                                SimpleUri uri = new SimpleUri(moduleId, annotation.id());
                                infos.add(new WorldGeneratorInfo(uri, annotation.displayName(), annotation.description()));
                            } else {
                                logger.error("{} marked to be registered as a World Generator, but is not a subclass of WorldGenerator or lacks the correct constructor", generatorClass);
                            }
                        }
                    }
                } catch (Exception e) {
                    logger.error("Error loading world generator in module {}, skipping", module.getId(), e);
                }
            } else {
                logger.warn("Could not resolve dependencies for module: {}", module);
            }
        }
    }
    Collections.sort(infos);
    generatorInfo = ImmutableList.copyOf(infos);
}
Also used : RegisterWorldGenerator(org.terasology.world.generator.RegisterWorldGenerator) ModuleEnvironment(org.terasology.module.ModuleEnvironment) ResolutionResult(org.terasology.module.ResolutionResult) SimpleUri(org.terasology.engine.SimpleUri) ModuleManager(org.terasology.engine.module.ModuleManager) Module(org.terasology.module.Module) UnresolvedWorldGeneratorException(org.terasology.world.generator.UnresolvedWorldGeneratorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Name(org.terasology.naming.Name) DependencyResolver(org.terasology.module.DependencyResolver)

Example 48 with SimpleUri

use of org.terasology.engine.SimpleUri in project Terasology by MovingBlocks.

the class AbstractClassLibrary method resolve.

@Override
public ClassMetadata<? extends T, ?> resolve(String name) {
    SimpleUri uri = new SimpleUri(name);
    if (uri.isValid()) {
        return getMetadata(uri);
    }
    List<ClassMetadata<? extends T, ?>> possibilities = getMetadata(name);
    if (possibilities.size() == 1) {
        return possibilities.get(0);
    }
    return null;
}
Also used : SimpleUri(org.terasology.engine.SimpleUri)

Example 49 with SimpleUri

use of org.terasology.engine.SimpleUri in project Terasology by MovingBlocks.

the class AbstractClassLibrary method resolve.

@Override
public ClassMetadata<? extends T, ?> resolve(String name, Module context) {
    SimpleUri uri = new SimpleUri(name);
    if (uri.isValid()) {
        return getMetadata(uri);
    }
    List<ClassMetadata<? extends T, ?>> possibilities = getMetadata(name);
    switch(possibilities.size()) {
        case 0:
            return null;
        case 1:
            return possibilities.get(0);
        default:
            if (context != null) {
                Set<Name> dependencies = moduleManager.getEnvironment().getDependencyNamesOf(context.getId());
                Iterator<ClassMetadata<? extends T, ?>> iterator = possibilities.iterator();
                while (iterator.hasNext()) {
                    ClassMetadata<? extends T, ?> metadata = iterator.next();
                    if (context.getId().equals(metadata.getUri().getModuleName())) {
                        return metadata;
                    }
                    if (!dependencies.contains(metadata.getUri().getModuleName())) {
                        iterator.remove();
                    }
                }
                if (possibilities.size() == 1) {
                    return possibilities.get(0);
                }
            }
            return null;
    }
}
Also used : SimpleUri(org.terasology.engine.SimpleUri) Name(org.terasology.naming.Name)

Example 50 with SimpleUri

use of org.terasology.engine.SimpleUri in project Terasology by MovingBlocks.

the class BindsSubsystem method addAxisDefaultsFor.

private void addAxisDefaultsFor(Name moduleId, Iterable<Class<?>> classes, BindsConfiguration config) {
    for (Class<?> axisEvent : classes) {
        if (AxisEvent.class.isAssignableFrom(axisEvent)) {
            RegisterRealBindAxis info = axisEvent.getAnnotation(RegisterRealBindAxis.class);
            SimpleUri bindUri = new SimpleUri(moduleId, info.id());
            if (!config.hasBinds(bindUri)) {
                addDefaultBindings(bindUri, axisEvent, config);
            }
        }
    }
}
Also used : RegisterRealBindAxis(org.terasology.input.RegisterRealBindAxis) SimpleUri(org.terasology.engine.SimpleUri)

Aggregations

SimpleUri (org.terasology.engine.SimpleUri)71 Test (org.junit.Test)18 Map (java.util.Map)10 Name (org.terasology.naming.Name)9 ResourceUrn (org.terasology.assets.ResourceUrn)7 DefaultClassMetadata (org.terasology.reflection.metadata.DefaultClassMetadata)7 Config (org.terasology.config.Config)6 Input (org.terasology.input.Input)6 FBO (org.terasology.rendering.opengl.FBO)6 ModuleManager (org.terasology.engine.module.ModuleManager)5 Command (org.terasology.logic.console.commandSystem.annotations.Command)5 ModuleEnvironment (org.terasology.module.ModuleEnvironment)5 List (java.util.List)4 DependencyResolver (org.terasology.module.DependencyResolver)4 ResolutionResult (org.terasology.module.ResolutionResult)4 UILabel (org.terasology.rendering.nui.widgets.UILabel)4 Lists (com.google.common.collect.Lists)3 IOException (java.io.IOException)3 Type (java.lang.reflect.Type)3 Locale (java.util.Locale)3