Search in sources :

Example 1 with PrefabFormat

use of org.terasology.entitySystem.prefab.internal.PrefabFormat 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 2 with PrefabFormat

use of org.terasology.entitySystem.prefab.internal.PrefabFormat in project Terasology by MovingBlocks.

the class PrefabTest method setup.

@Before
public void setup() throws Exception {
    ContextImpl context = new ContextImpl();
    CoreRegistry.setContext(context);
    ModuleManager moduleManager = ModuleManagerFactory.create();
    context.put(ModuleManager.class, moduleManager);
    EntitySystemSetupUtil.addReflectionBasedLibraries(context);
    ModuleAwareAssetTypeManager assetTypeManager = new ModuleAwareAssetTypeManager();
    assetTypeManager.registerCoreAssetType(Prefab.class, (AssetFactory<Prefab, PrefabData>) PojoPrefab::new, "prefabs");
    ComponentLibrary componentLibrary = context.get(ComponentLibrary.class);
    TypeSerializationLibrary typeSerializationLibrary = context.get(TypeSerializationLibrary.class);
    PrefabFormat prefabFormat = new PrefabFormat(componentLibrary, typeSerializationLibrary);
    assetTypeManager.registerCoreFormat(Prefab.class, prefabFormat);
    assetTypeManager.switchEnvironment(moduleManager.getEnvironment());
    context.put(AssetManager.class, assetTypeManager.getAssetManager());
    NetworkSystem networkSystem = mock(NetworkSystem.class);
    when(networkSystem.getMode()).thenReturn(NetworkMode.NONE);
    context.put(NetworkSystem.class, networkSystem);
    EntitySystemSetupUtil.addEntityManagementRelatedClasses(context);
    prefabManager = new PojoPrefabManager(context);
}
Also used : PojoPrefabManager(org.terasology.entitySystem.prefab.internal.PojoPrefabManager) PrefabData(org.terasology.entitySystem.prefab.PrefabData) ModuleAwareAssetTypeManager(org.terasology.assets.module.ModuleAwareAssetTypeManager) NetworkSystem(org.terasology.network.NetworkSystem) ComponentLibrary(org.terasology.entitySystem.metadata.ComponentLibrary) PrefabFormat(org.terasology.entitySystem.prefab.internal.PrefabFormat) TypeSerializationLibrary(org.terasology.persistence.typeHandling.TypeSerializationLibrary) ContextImpl(org.terasology.context.internal.ContextImpl) ModuleManager(org.terasology.engine.module.ModuleManager) Prefab(org.terasology.entitySystem.prefab.Prefab) PojoPrefab(org.terasology.entitySystem.prefab.internal.PojoPrefab) Before(org.junit.Before)

Aggregations

ModuleAwareAssetTypeManager (org.terasology.assets.module.ModuleAwareAssetTypeManager)2 ModuleManager (org.terasology.engine.module.ModuleManager)2 ComponentLibrary (org.terasology.entitySystem.metadata.ComponentLibrary)2 PrefabFormat (org.terasology.entitySystem.prefab.internal.PrefabFormat)2 TypeSerializationLibrary (org.terasology.persistence.typeHandling.TypeSerializationLibrary)2 Before (org.junit.Before)1 ContextImpl (org.terasology.context.internal.ContextImpl)1 EntitySystemLibrary (org.terasology.entitySystem.metadata.EntitySystemLibrary)1 Prefab (org.terasology.entitySystem.prefab.Prefab)1 PrefabData (org.terasology.entitySystem.prefab.PrefabData)1 PojoPrefab (org.terasology.entitySystem.prefab.internal.PojoPrefab)1 PojoPrefabManager (org.terasology.entitySystem.prefab.internal.PojoPrefabManager)1 PrefabDeltaFormat (org.terasology.entitySystem.prefab.internal.PrefabDeltaFormat)1 NetworkSystem (org.terasology.network.NetworkSystem)1 CollisionGroupTypeHandler (org.terasology.persistence.typeHandling.extensionTypes.CollisionGroupTypeHandler)1 CopyStrategyLibrary (org.terasology.reflection.copy.CopyStrategyLibrary)1 RegisterCopyStrategy (org.terasology.reflection.copy.RegisterCopyStrategy)1 ReflectFactory (org.terasology.reflection.reflect.ReflectFactory)1 BlockFamilyFactoryRegistry (org.terasology.world.block.family.BlockFamilyFactoryRegistry)1 DefaultBlockFamilyFactoryRegistry (org.terasology.world.block.family.DefaultBlockFamilyFactoryRegistry)1