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