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