Search in sources :

Example 1 with CopyStrategyLibrary

use of org.terasology.reflection.copy.CopyStrategyLibrary in project Terasology by MovingBlocks.

the class PropertyProvider method createProperties.

public List<Property<?, ?>> createProperties(Object target) {
    List<Property<?, ?>> properties = Lists.newArrayList();
    try {
        Class<?> type = target.getClass();
        ReflectFactory reflectFactory = CoreRegistry.get(ReflectFactory.class);
        CopyStrategyLibrary copyStrategies = new CopyStrategyLibrary(reflectFactory);
        ClassMetadata<?, ?> classMetadata = new DefaultClassMetadata<>(new SimpleUri(), type, reflectFactory, copyStrategies);
        for (Field field : getAllFields(type)) {
            Annotation annotation = getFactory(field);
            if (annotation != null) {
                FieldMetadata<Object, ?> fieldMetadata = (FieldMetadata<Object, ?>) classMetadata.getField(field.getName());
                PropertyFactory factory = factories.get(annotation.annotationType());
                Property property = factory.create(target, fieldMetadata, field.getName(), annotation);
                properties.add(property);
            }
        }
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
    return properties;
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) SimpleUri(org.terasology.engine.SimpleUri) CopyStrategyLibrary(org.terasology.reflection.copy.CopyStrategyLibrary) Annotation(java.lang.annotation.Annotation) ReflectFactory(org.terasology.reflection.reflect.ReflectFactory) Field(java.lang.reflect.Field) DefaultClassMetadata(org.terasology.reflection.metadata.DefaultClassMetadata)

Example 2 with CopyStrategyLibrary

use of org.terasology.reflection.copy.CopyStrategyLibrary in project Terasology by MovingBlocks.

the class EnvironmentSwitchHandler method handleSwitchToGameEnvironment.

@SuppressWarnings("unchecked")
public void handleSwitchToGameEnvironment(Context context) {
    ModuleManager moduleManager = context.get(ModuleManager.class);
    CopyStrategyLibrary copyStrategyLibrary = context.get(CopyStrategyLibrary.class);
    copyStrategyLibrary.clear();
    for (Class<? extends CopyStrategy> copyStrategy : moduleManager.getEnvironment().getSubtypesOf(CopyStrategy.class)) {
        if (copyStrategy.getAnnotation(RegisterCopyStrategy.class) == null) {
            continue;
        }
        Class<?> targetType = ReflectionUtil.getTypeParameterForSuper(copyStrategy, CopyStrategy.class, 0);
        if (targetType != null) {
            registerCopyStrategy(copyStrategyLibrary, targetType, copyStrategy);
        } else {
            logger.error("Cannot register CopyStrategy '{}' - unable to determine target type", copyStrategy);
        }
    }
    ReflectFactory reflectFactory = context.get(ReflectFactory.class);
    TypeSerializationLibrary typeSerializationLibrary = TypeSerializationLibrary.createDefaultLibrary(reflectFactory, copyStrategyLibrary);
    typeSerializationLibrary.add(CollisionGroup.class, new CollisionGroupTypeHandler(context.get(CollisionGroupManager.class)));
    context.put(TypeSerializationLibrary.class, typeSerializationLibrary);
    // Entity System Library
    EntitySystemLibrary library = new EntitySystemLibrary(context, typeSerializationLibrary);
    context.put(EntitySystemLibrary.class, library);
    ComponentLibrary componentLibrary = library.getComponentLibrary();
    context.put(ComponentLibrary.class, componentLibrary);
    context.put(EventLibrary.class, library.getEventLibrary());
    context.put(ClassMetaLibrary.class, new ClassMetaLibraryImpl(context));
    registerComponents(componentLibrary, moduleManager.getEnvironment());
    registerTypeHandlers(context, typeSerializationLibrary, moduleManager.getEnvironment());
    BlockFamilyFactoryRegistry blockFamilyFactoryRegistry = context.get(BlockFamilyFactoryRegistry.class);
    loadFamilies((DefaultBlockFamilyFactoryRegistry) blockFamilyFactoryRegistry, moduleManager.getEnvironment());
    ModuleAwareAssetTypeManager assetTypeManager = context.get(ModuleAwareAssetTypeManager.class);
    /*
         * The registering of the prefab formats is done in this method, because it needs to be done before
         * the environment of the asset manager gets changed.
         *
         * It can't be done before this method gets called because the ComponentLibrary isn't
         * existing then yet.
         */
    unregisterPrefabFormats(assetTypeManager);
    registeredPrefabFormat = new PrefabFormat(componentLibrary, typeSerializationLibrary);
    assetTypeManager.registerCoreFormat(Prefab.class, registeredPrefabFormat);
    registeredPrefabDeltaFormat = new PrefabDeltaFormat(componentLibrary, typeSerializationLibrary);
    assetTypeManager.registerCoreDeltaFormat(Prefab.class, registeredPrefabDeltaFormat);
    assetTypeManager.switchEnvironment(moduleManager.getEnvironment());
}
Also used : ModuleAwareAssetTypeManager(org.terasology.assets.module.ModuleAwareAssetTypeManager) PrefabFormat(org.terasology.entitySystem.prefab.internal.PrefabFormat) CopyStrategyLibrary(org.terasology.reflection.copy.CopyStrategyLibrary) PrefabDeltaFormat(org.terasology.entitySystem.prefab.internal.PrefabDeltaFormat) ModuleManager(org.terasology.engine.module.ModuleManager) RegisterCopyStrategy(org.terasology.reflection.copy.RegisterCopyStrategy) CollisionGroupTypeHandler(org.terasology.persistence.typeHandling.extensionTypes.CollisionGroupTypeHandler) ReflectFactory(org.terasology.reflection.reflect.ReflectFactory) EntitySystemLibrary(org.terasology.entitySystem.metadata.EntitySystemLibrary) ComponentLibrary(org.terasology.entitySystem.metadata.ComponentLibrary) TypeSerializationLibrary(org.terasology.persistence.typeHandling.TypeSerializationLibrary) DefaultBlockFamilyFactoryRegistry(org.terasology.world.block.family.DefaultBlockFamilyFactoryRegistry) BlockFamilyFactoryRegistry(org.terasology.world.block.family.BlockFamilyFactoryRegistry)

Example 3 with CopyStrategyLibrary

use of org.terasology.reflection.copy.CopyStrategyLibrary in project Terasology by MovingBlocks.

the class EntitySystemSetupUtil method addReflectionBasedLibraries.

public static void addReflectionBasedLibraries(Context context) {
    ReflectionReflectFactory reflectFactory = new ReflectionReflectFactory();
    context.put(ReflectFactory.class, reflectFactory);
    CopyStrategyLibrary copyStrategyLibrary = new CopyStrategyLibrary(reflectFactory);
    context.put(CopyStrategyLibrary.class, copyStrategyLibrary);
    TypeSerializationLibrary typeSerializationLibrary = TypeSerializationLibrary.createDefaultLibrary(reflectFactory, copyStrategyLibrary);
    context.put(TypeSerializationLibrary.class, typeSerializationLibrary);
    EntitySystemLibrary library = new EntitySystemLibrary(context, typeSerializationLibrary);
    context.put(EntitySystemLibrary.class, library);
    context.put(ComponentLibrary.class, library.getComponentLibrary());
    context.put(EventLibrary.class, library.getEventLibrary());
}
Also used : ReflectionReflectFactory(org.terasology.reflection.reflect.ReflectionReflectFactory) EntitySystemLibrary(org.terasology.entitySystem.metadata.EntitySystemLibrary) CopyStrategyLibrary(org.terasology.reflection.copy.CopyStrategyLibrary) TypeSerializationLibrary(org.terasology.persistence.typeHandling.TypeSerializationLibrary)

Example 4 with CopyStrategyLibrary

use of org.terasology.reflection.copy.CopyStrategyLibrary in project Terasology by MovingBlocks.

the class PojoEventSystemTests method setup.

@Before
public void setup() {
    ContextImpl context = new ContextImpl();
    CoreRegistry.setContext(context);
    ReflectFactory reflectFactory = new ReflectionReflectFactory();
    CopyStrategyLibrary copyStrategies = new CopyStrategyLibrary(reflectFactory);
    TypeSerializationLibrary serializationLibrary = new TypeSerializationLibrary(reflectFactory, copyStrategies);
    EntitySystemLibrary entitySystemLibrary = new EntitySystemLibrary(context, serializationLibrary);
    compLibrary = entitySystemLibrary.getComponentLibrary();
    entityManager = new PojoEntityManager();
    entityManager.setComponentLibrary(entitySystemLibrary.getComponentLibrary());
    entityManager.setPrefabManager(new PojoPrefabManager(context));
    NetworkSystem networkSystem = mock(NetworkSystem.class);
    when(networkSystem.getMode()).thenReturn(NetworkMode.NONE);
    eventSystem = new EventSystemImpl(entitySystemLibrary.getEventLibrary(), networkSystem);
    entityManager.setEventSystem(eventSystem);
    entity = entityManager.create();
}
Also used : ReflectionReflectFactory(org.terasology.reflection.reflect.ReflectionReflectFactory) ReflectionReflectFactory(org.terasology.reflection.reflect.ReflectionReflectFactory) ReflectFactory(org.terasology.reflection.reflect.ReflectFactory) PojoPrefabManager(org.terasology.entitySystem.prefab.internal.PojoPrefabManager) EntitySystemLibrary(org.terasology.entitySystem.metadata.EntitySystemLibrary) PojoEntityManager(org.terasology.entitySystem.entity.internal.PojoEntityManager) NetworkSystem(org.terasology.network.NetworkSystem) CopyStrategyLibrary(org.terasology.reflection.copy.CopyStrategyLibrary) TypeSerializationLibrary(org.terasology.persistence.typeHandling.TypeSerializationLibrary) ContextImpl(org.terasology.context.internal.ContextImpl) EventSystemImpl(org.terasology.entitySystem.event.internal.EventSystemImpl) Before(org.junit.Before)

Example 5 with CopyStrategyLibrary

use of org.terasology.reflection.copy.CopyStrategyLibrary in project Terasology by MovingBlocks.

the class EnvironmentSwitchHandler method handleSwitchToGameEnvironment.

public void handleSwitchToGameEnvironment(Context context) {
    ModuleManager moduleManager = context.get(ModuleManager.class);
    ModuleEnvironment environment = moduleManager.getEnvironment();
    ModuleTypeRegistry typeRegistry = context.get(ModuleTypeRegistry.class);
    typeRegistry.reload(environment);
    CopyStrategyLibrary copyStrategyLibrary = context.get(CopyStrategyLibrary.class);
    copyStrategyLibrary.clear();
    for (CopyStrategyEntry<?> entry : typesWithCopyConstructors) {
        entry.registerWith(copyStrategyLibrary);
    }
    // TODO: find a permanent fix over just creating a new typehandler
    // https://github.com/Terasology/JoshariasSurvival/issues/31
    // TypeHandlerLibrary typeHandlerLibrary = context.get(TypeHandlerLibrary.class);
    // typeHandlerLibrary.addTypeHandler(CollisionGroup.class, new CollisionGroupTypeHandler(context.get(CollisionGroupManager.class)));
    TypeHandlerLibrary typeHandlerLibrary = TypeHandlerLibraryImpl.forModuleEnvironment(moduleManager, typeRegistry);
    typeHandlerLibrary.addTypeHandler(CollisionGroup.class, new CollisionGroupTypeHandler(context.get(CollisionGroupManager.class)));
    context.put(TypeHandlerLibrary.class, typeHandlerLibrary);
    // Entity System Library
    EntitySystemLibrary library = new EntitySystemLibrary(context, typeHandlerLibrary);
    context.put(EntitySystemLibrary.class, library);
    ComponentLibrary componentLibrary = library.getComponentLibrary();
    context.put(ComponentLibrary.class, componentLibrary);
    context.put(EventLibrary.class, library.getEventLibrary());
    context.put(ClassMetaLibrary.class, new ClassMetaLibraryImpl(context));
    registerComponents(componentLibrary, environment);
    registerTypeHandlers(context, typeHandlerLibrary, environment);
    // Load configs for the new environment
    AutoConfigManager autoConfigManager = context.get(AutoConfigManager.class);
    autoConfigManager.loadConfigsIn(context);
    ModuleAwareAssetTypeManager assetTypeManager = context.get(ModuleAwareAssetTypeManager.class);
    /*
         * The registering of the prefab formats is done in this method, because it needs to be done before
         * the environment of the asset manager gets changed.
         *
         * It can't be done before this method gets called because the ComponentLibrary isn't
         * existing then yet.
         */
    unregisterPrefabFormats(assetTypeManager);
    registeredPrefabFormat = new PrefabFormat(componentLibrary, typeHandlerLibrary);
    assetTypeManager.getAssetFileDataProducer(assetTypeManager.getAssetType(Prefab.class).orElseThrow(() -> new RuntimeException("Cannot get Prefab Asset typee"))).addAssetFormat(registeredPrefabFormat);
    registeredPrefabDeltaFormat = new PrefabDeltaFormat(componentLibrary, typeHandlerLibrary);
    assetTypeManager.getAssetFileDataProducer(assetTypeManager.getAssetType(Prefab.class).orElseThrow(() -> new RuntimeException("Cannot get Prefab Asset type"))).addDeltaFormat(registeredPrefabDeltaFormat);
    assetTypeManager.switchEnvironment(environment);
    assetTypeManager.reloadAssets();
}
Also used : ModuleTypeRegistry(org.terasology.reflection.ModuleTypeRegistry) ModuleAwareAssetTypeManager(org.terasology.gestalt.assets.module.ModuleAwareAssetTypeManager) PrefabFormat(org.terasology.engine.entitySystem.prefab.internal.PrefabFormat) CopyStrategyLibrary(org.terasology.reflection.copy.CopyStrategyLibrary) PrefabDeltaFormat(org.terasology.engine.entitySystem.prefab.internal.PrefabDeltaFormat) ModuleManager(org.terasology.engine.core.module.ModuleManager) CollisionGroupTypeHandler(org.terasology.engine.persistence.typeHandling.extensionTypes.CollisionGroupTypeHandler) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) TypeHandlerLibrary(org.terasology.persistence.typeHandling.TypeHandlerLibrary) EntitySystemLibrary(org.terasology.engine.entitySystem.metadata.EntitySystemLibrary) ComponentLibrary(org.terasology.engine.entitySystem.metadata.ComponentLibrary) AutoConfigManager(org.terasology.engine.config.flexible.AutoConfigManager) Prefab(org.terasology.engine.entitySystem.prefab.Prefab)

Aggregations

CopyStrategyLibrary (org.terasology.reflection.copy.CopyStrategyLibrary)10 ReflectFactory (org.terasology.reflection.reflect.ReflectFactory)7 ReflectionReflectFactory (org.terasology.reflection.reflect.ReflectionReflectFactory)7 TypeSerializationLibrary (org.terasology.persistence.typeHandling.TypeSerializationLibrary)5 EntitySystemLibrary (org.terasology.entitySystem.metadata.EntitySystemLibrary)4 ModuleAwareAssetTypeManager (org.terasology.assets.module.ModuleAwareAssetTypeManager)3 ModuleManager (org.terasology.engine.core.module.ModuleManager)3 ModuleManager (org.terasology.engine.module.ModuleManager)3 Before (org.junit.Before)2 ContextImpl (org.terasology.context.internal.ContextImpl)2 EntitySystemLibrary (org.terasology.engine.entitySystem.metadata.EntitySystemLibrary)2 PojoPrefabManager (org.terasology.entitySystem.prefab.internal.PojoPrefabManager)2 AutoReloadAssetTypeManager (org.terasology.gestalt.assets.module.autoreload.AutoReloadAssetTypeManager)2 TypeHandlerLibrary (org.terasology.persistence.typeHandling.TypeHandlerLibrary)2 ModuleTypeRegistry (org.terasology.reflection.ModuleTypeRegistry)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Config (org.terasology.config.Config)1 SimpleUri (org.terasology.engine.SimpleUri)1 Config (org.terasology.engine.config.Config)1