Search in sources :

Example 11 with SimpleUri

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

the class CopyStrategyLibrary method getStrategy.

// TODO: Consider CopyStrategyFactory system for Collections and similar
public CopyStrategy<?> getStrategy(Type genericType) {
    Class<?> typeClass = ReflectionUtil.getClassOfType(genericType);
    if (typeClass == null) {
        logger.error("Cannot obtain class for type {}, using default strategy", genericType);
        return defaultStrategy;
    }
    if (List.class.isAssignableFrom(typeClass)) {
        // For lists, create the handler for the contained type and wrap in a list type handler
        Type parameter = ReflectionUtil.getTypeParameter(genericType, 0);
        if (parameter != null) {
            CopyStrategy<?> contentStrategy = getStrategy(parameter);
            return new ListCopyStrategy<>(contentStrategy);
        }
        logger.error("List field is not parametrized - using default strategy");
        return new ListCopyStrategy<>(defaultStrategy);
    } else if (Set.class.isAssignableFrom(typeClass)) {
        // For sets:
        Type parameter = ReflectionUtil.getTypeParameter(genericType, 0);
        if (parameter != null) {
            CopyStrategy<?> contentStrategy = getStrategy(parameter);
            return new SetCopyStrategy<>(contentStrategy);
        }
        logger.error("Set field is not parametrized - using default strategy");
        return new SetCopyStrategy<>(defaultStrategy);
    } else if (Map.class.isAssignableFrom(typeClass)) {
        // For Maps, create the handler for the value type
        Type keyParameter = ReflectionUtil.getTypeParameter(genericType, 0);
        CopyStrategy<?> keyStrategy;
        if (keyParameter != null) {
            keyStrategy = getStrategy(keyParameter);
        } else {
            logger.error("Map field is missing key parameter - using default strategy");
            keyStrategy = defaultStrategy;
        }
        Type valueParameter = ReflectionUtil.getTypeParameter(genericType, 1);
        CopyStrategy<?> valueStrategy;
        if (valueParameter != null) {
            valueStrategy = getStrategy(valueParameter);
        } else {
            logger.error("Map field is missing value parameter - using default strategy");
            valueStrategy = defaultStrategy;
        }
        return new MapCopyStrategy<>(keyStrategy, valueStrategy);
    } else if (strategies.containsKey(typeClass)) {
        // For known types, just use the handler
        return strategies.get(typeClass);
    } else if (typeClass.getAnnotation(MappedContainer.class) != null) {
        if (Modifier.isAbstract(typeClass.getModifiers()) || typeClass.isLocalClass() || (typeClass.isMemberClass() && !Modifier.isStatic(typeClass.getModifiers()))) {
            logger.error("Type {} is not a valid mapped class", typeClass);
            return defaultStrategy;
        }
        try {
            ClassMetadata<?, ?> classMetadata = new DefaultClassMetadata<>(new SimpleUri(), typeClass, reflectFactory, this);
            return new MappedContainerCopyStrategy<>(classMetadata);
        } catch (NoSuchMethodException e) {
            logger.error("Unable to create copy strategy for field of type {}: no publicly accessible default constructor", typeClass.getSimpleName());
            return defaultStrategy;
        }
    } else {
        logger.debug("Using default copy strategy for {}", typeClass);
        strategies.put(typeClass, defaultStrategy);
        return defaultStrategy;
    }
}
Also used : MapCopyStrategy(org.terasology.reflection.copy.strategy.MapCopyStrategy) Set(java.util.Set) ListCopyStrategy(org.terasology.reflection.copy.strategy.ListCopyStrategy) MapCopyStrategy(org.terasology.reflection.copy.strategy.MapCopyStrategy) MappedContainerCopyStrategy(org.terasology.reflection.copy.strategy.MappedContainerCopyStrategy) SetCopyStrategy(org.terasology.reflection.copy.strategy.SetCopyStrategy) SimpleUri(org.terasology.engine.SimpleUri) MappedContainerCopyStrategy(org.terasology.reflection.copy.strategy.MappedContainerCopyStrategy) Type(java.lang.reflect.Type) DefaultClassMetadata(org.terasology.reflection.metadata.DefaultClassMetadata) ListCopyStrategy(org.terasology.reflection.copy.strategy.ListCopyStrategy) MappedContainer(org.terasology.reflection.MappedContainer)

Example 12 with SimpleUri

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

the class WorldGeneratorManager method createWorldGenerator.

/**
 * @param uri uri of the world generator to create.
 * @param context that will be used to inject teh world generator.
 * @param environment to be searched for the world generator class.
 * @return a new world generator with the specified uri.
 */
public static WorldGenerator createWorldGenerator(SimpleUri uri, Context context, ModuleEnvironment environment) throws UnresolvedWorldGeneratorException {
    for (Class<?> generatorClass : environment.getTypesAnnotatedWith(RegisterWorldGenerator.class)) {
        RegisterWorldGenerator annotation = generatorClass.getAnnotation(RegisterWorldGenerator.class);
        SimpleUri generatorUri = new SimpleUri(environment.getModuleProviding(generatorClass), annotation.id());
        if (generatorUri.equals(uri)) {
            WorldGenerator worldGenerator = loadGenerator(generatorClass, generatorUri);
            InjectionHelper.inject(worldGenerator, context);
            return worldGenerator;
        }
    }
    throw new UnresolvedWorldGeneratorException("Unable to resolve world generator '" + uri + "' - not found");
}
Also used : RegisterWorldGenerator(org.terasology.world.generator.RegisterWorldGenerator) RegisterWorldGenerator(org.terasology.world.generator.RegisterWorldGenerator) WorldGenerator(org.terasology.world.generator.WorldGenerator) UnresolvedWorldGeneratorException(org.terasology.world.generator.UnresolvedWorldGeneratorException) SimpleUri(org.terasology.engine.SimpleUri)

Example 13 with SimpleUri

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

the class CreateWorldEntity method step.

@Override
public boolean step() {
    EntityManager entityManager = context.get(EntityManager.class);
    ChunkProvider chunkProvider = context.get(ChunkProvider.class);
    Iterator<EntityRef> worldEntityIterator = entityManager.getEntitiesWith(WorldComponent.class).iterator();
    if (worldEntityIterator.hasNext()) {
        EntityRef worldEntity = worldEntityIterator.next();
        chunkProvider.setWorldEntity(worldEntity);
        // get the world generator config from the world entity
        // replace the world generator values from the components in the world entity
        WorldGenerator worldGenerator = context.get(WorldGenerator.class);
        WorldConfigurator worldConfigurator = worldGenerator.getConfigurator();
        Map<String, Component> params = worldConfigurator.getProperties();
        for (Map.Entry<String, Component> entry : params.entrySet()) {
            Class<? extends Component> clazz = entry.getValue().getClass();
            Component comp = worldEntity.getComponent(clazz);
            if (comp != null) {
                worldConfigurator.setProperty(entry.getKey(), comp);
            }
        }
    } else {
        EntityRef worldEntity = entityManager.create();
        worldEntity.addComponent(new WorldComponent());
        NetworkComponent networkComponent = new NetworkComponent();
        networkComponent.replicateMode = NetworkComponent.ReplicateMode.ALWAYS;
        worldEntity.addComponent(networkComponent);
        chunkProvider.setWorldEntity(worldEntity);
        // transfer all world generation parameters from Config to WorldEntity
        WorldGenerator worldGenerator = context.get(WorldGenerator.class);
        SimpleUri generatorUri = worldGenerator.getUri();
        Config config = context.get(Config.class);
        // get the map of properties from the world generator.
        // Replace its values with values from the config set by the UI.
        // Also set all the components to the world entity.
        WorldConfigurator worldConfigurator = worldGenerator.getConfigurator();
        Map<String, Component> params = worldConfigurator.getProperties();
        for (Map.Entry<String, Component> entry : params.entrySet()) {
            Class<? extends Component> clazz = entry.getValue().getClass();
            Component comp = config.getModuleConfig(generatorUri, entry.getKey(), clazz);
            if (comp != null) {
                worldEntity.addComponent(comp);
                worldConfigurator.setProperty(entry.getKey(), comp);
            } else {
                worldEntity.addComponent(entry.getValue());
            }
        }
    }
    return true;
}
Also used : WorldGenerator(org.terasology.world.generator.WorldGenerator) WorldConfigurator(org.terasology.world.generator.WorldConfigurator) Config(org.terasology.config.Config) SimpleUri(org.terasology.engine.SimpleUri) EntityManager(org.terasology.entitySystem.entity.EntityManager) NetworkComponent(org.terasology.network.NetworkComponent) ChunkProvider(org.terasology.world.chunks.ChunkProvider) WorldComponent(org.terasology.world.WorldComponent) NetworkComponent(org.terasology.network.NetworkComponent) WorldComponent(org.terasology.world.WorldComponent) Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) Map(java.util.Map)

Example 14 with SimpleUri

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

the class ConfigurationSubsystem method preInitialise.

@Override
public void preInitialise(Context rootContext) {
    config = new Config(rootContext);
    config.load();
    FlexibleConfigManager flexibleConfigManager = new FlexibleConfigManagerImpl();
    rootContext.put(FlexibleConfigManager.class, flexibleConfigManager);
    // TODO: Update rendering config description
    FlexibleConfig renderingFlexibleConfig = new FlexibleConfigImpl("Rendering Config");
    flexibleConfigManager.addConfig(new SimpleUri("engine:rendering"), renderingFlexibleConfig);
    flexibleConfigManager.loadAllConfigs();
    // Add settings to RenderingFC
    String serverPortProperty = System.getProperty(SERVER_PORT_PROPERTY);
    if (serverPortProperty != null) {
        try {
            config.getNetwork().setServerPort(Integer.parseInt(serverPortProperty));
        } catch (NumberFormatException e) {
            logger.error("Failed to set server port to invalid value: {}", serverPortProperty);
        }
    }
    if (Iterables.isEmpty(config.getDefaultModSelection().listModules())) {
        config.getDefaultModSelection().addModule(TerasologyConstants.CORE_GAMEPLAY_MODULE);
    }
    checkServerIdentity();
    // TODO: Move to display subsystem
    logger.info("Video Settings: {}", config.renderConfigAsJson(config.getRendering()));
    rootContext.put(Config.class, config);
    // add facades
    rootContext.put(InputDeviceConfiguration.class, new InputDeviceConfigurationImpl(config));
    rootContext.put(BindsConfiguration.class, new BindsConfigurationImpl(config));
}
Also used : FlexibleConfigImpl(org.terasology.config.flexible.FlexibleConfigImpl) BindsConfigurationImpl(org.terasology.config.facade.BindsConfigurationImpl) FlexibleConfig(org.terasology.config.flexible.FlexibleConfig) Config(org.terasology.config.Config) SimpleUri(org.terasology.engine.SimpleUri) FlexibleConfigManagerImpl(org.terasology.config.flexible.FlexibleConfigManagerImpl) FlexibleConfig(org.terasology.config.flexible.FlexibleConfig) InputDeviceConfigurationImpl(org.terasology.config.facade.InputDeviceConfigurationImpl) FlexibleConfigManager(org.terasology.config.flexible.FlexibleConfigManager)

Example 15 with SimpleUri

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

the class BindsSubsystem method registerAxisBinds.

private void registerAxisBinds(ModuleEnvironment environment) {
    Iterable<Class<?>> classes = environment.getTypesAnnotatedWith(RegisterBindAxis.class);
    for (Class<?> registerBindClass : classes) {
        RegisterBindAxis info = registerBindClass.getAnnotation(RegisterBindAxis.class);
        Name moduleId = environment.getModuleProviding(registerBindClass);
        SimpleUri id = new SimpleUri(moduleId, info.id());
        if (BindAxisEvent.class.isAssignableFrom(registerBindClass)) {
            BindableButton positiveButton = getBindButton(new SimpleUri(info.positiveButton()));
            BindableButton negativeButton = getBindButton(new SimpleUri(info.negativeButton()));
            if (positiveButton == null) {
                logger.warn("Failed to register axis \"{}\", missing positive button \"{}\"", id, info.positiveButton());
                continue;
            }
            if (negativeButton == null) {
                logger.warn("Failed to register axis \"{}\", missing negative button \"{}\"", id, info.negativeButton());
                continue;
            }
            try {
                BindableAxis bindAxis = registerBindAxis(id.toString(), (BindAxisEvent) registerBindClass.newInstance(), positiveButton, negativeButton);
                bindAxis.setSendEventMode(info.eventMode());
                logger.debug("Registered axis bind: {}", id);
            } catch (InstantiationException | IllegalAccessException e) {
                logger.error("Failed to register axis bind \"{}\"", id, e);
            }
        } else {
            logger.error("Failed to register axis bind \"{}\", does not extend BindAxisEvent", id);
        }
    }
}
Also used : BindableButton(org.terasology.input.BindableButton) RegisterBindAxis(org.terasology.input.RegisterBindAxis) SimpleUri(org.terasology.engine.SimpleUri) AbstractBindableAxis(org.terasology.input.internal.AbstractBindableAxis) BindableAxis(org.terasology.input.BindableAxis) Name(org.terasology.naming.Name)

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